mirror of
https://github.com/cccriscv/mini-riscv-os.git
synced 2025-11-16 12:34:33 +00:00
update string.h and add virtio_tester()
This commit is contained in:
@@ -24,12 +24,12 @@ void os_start()
|
|||||||
trap_init();
|
trap_init();
|
||||||
plic_init();
|
plic_init();
|
||||||
timer_init(); // start timer interrupt ...
|
timer_init(); // start timer interrupt ...
|
||||||
|
virtio_tester();
|
||||||
}
|
}
|
||||||
|
|
||||||
int os_main(void)
|
int os_main(void)
|
||||||
{
|
{
|
||||||
os_start();
|
os_start();
|
||||||
|
|
||||||
int current_task = 0;
|
int current_task = 0;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,4 +9,32 @@ void *memset(void *dst, int c, unsigned int n)
|
|||||||
cdst[i] = c;
|
cdst[i] = c;
|
||||||
}
|
}
|
||||||
return dst;
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
memcpy(void *dst, const void *src, unsigned int n)
|
||||||
|
{
|
||||||
|
return memmove(dst, src, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
memmove(void *dst, const void *src, unsigned int n)
|
||||||
|
{
|
||||||
|
const char *s;
|
||||||
|
char *d;
|
||||||
|
|
||||||
|
s = src;
|
||||||
|
d = dst;
|
||||||
|
if (s < d && s + n > d)
|
||||||
|
{
|
||||||
|
s += n;
|
||||||
|
d += n;
|
||||||
|
while (n-- > 0)
|
||||||
|
*--d = *--s;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
while (n-- > 0)
|
||||||
|
*d++ = *s++;
|
||||||
|
|
||||||
|
return dst;
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,9 @@
|
|||||||
#define __STRING_H__
|
#define __STRING_H__
|
||||||
|
|
||||||
void *memset(void *, int, unsigned int);
|
void *memset(void *, int, unsigned int);
|
||||||
|
void *
|
||||||
|
memcpy(void *dst, const void *src, unsigned int n);
|
||||||
|
void *
|
||||||
|
memmove(void *dst, const void *src, unsigned int n);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -53,11 +53,13 @@ void user_task3(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern virtio_tester();
|
||||||
|
|
||||||
void user_init()
|
void user_init()
|
||||||
{
|
{
|
||||||
lock_init(&lock);
|
// lock_init(&lock);
|
||||||
task_create(&user_task0);
|
task_create(&user_task0);
|
||||||
task_create(&user_task1);
|
task_create(&user_task1);
|
||||||
task_create(&user_task2);
|
// task_create(&user_task2);
|
||||||
task_create(&user_task3);
|
// task_create(&user_task3);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,17 @@ struct buf
|
|||||||
unsigned char data[BSIZE];
|
unsigned char data[BSIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
lock_t lock;
|
||||||
|
struct buf buf[30];
|
||||||
|
|
||||||
|
// Linked list of all buffers, through prev/next.
|
||||||
|
// Sorted by how recently the buffer was used.
|
||||||
|
// head.next is most recent, head.prev is least.
|
||||||
|
struct buf head;
|
||||||
|
} bcache;
|
||||||
|
|
||||||
static struct disk
|
static struct disk
|
||||||
{
|
{
|
||||||
char pages[2 * PGSIZE];
|
char pages[2 * PGSIZE];
|
||||||
@@ -41,6 +52,40 @@ static struct disk
|
|||||||
struct lock vdisk_lock;
|
struct lock vdisk_lock;
|
||||||
} __attribute__((aligned(PGSIZE))) disk;
|
} __attribute__((aligned(PGSIZE))) disk;
|
||||||
|
|
||||||
|
void virtio_tester()
|
||||||
|
{
|
||||||
|
// int valid; // has data been read from disk?
|
||||||
|
// int disk; // does disk "own" buf?
|
||||||
|
// uint32 dev;
|
||||||
|
// uint32 blockno;
|
||||||
|
// lock_t lock;
|
||||||
|
// uint32 refcnt;
|
||||||
|
// struct buf *prev; // LRU cache list
|
||||||
|
// struct buf *next;
|
||||||
|
// unsigned char data[BSIZE];
|
||||||
|
struct buf b[3];
|
||||||
|
lib_puts("buffer init...\n");
|
||||||
|
for (size_t i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
b[i].valid = 1;
|
||||||
|
b[i].disk = 1;
|
||||||
|
b[i].dev = 1;
|
||||||
|
b[i].blockno = i;
|
||||||
|
for (size_t j = 0; j < BSIZE; j++)
|
||||||
|
{
|
||||||
|
b[i].data[j] = (j + i + 1) % 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock_init(&(b[i].lock));
|
||||||
|
}
|
||||||
|
lib_puts("buffer write...\n");
|
||||||
|
for (size_t i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
virtio_disk_rw(&b[i], 1);
|
||||||
|
}
|
||||||
|
lib_puts("wait...\n");
|
||||||
|
}
|
||||||
|
|
||||||
void virtio_disk_init()
|
void virtio_disk_init()
|
||||||
{
|
{
|
||||||
uint32 status = 0;
|
uint32 status = 0;
|
||||||
@@ -180,6 +225,7 @@ void virtio_disk_rw(struct buf *b, int write)
|
|||||||
// data, one for a 1-byte status result.
|
// data, one for a 1-byte status result.
|
||||||
|
|
||||||
// allocate the three descriptors.
|
// allocate the three descriptors.
|
||||||
|
lib_puts("rw init...\n");
|
||||||
int idx[3];
|
int idx[3];
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@@ -227,7 +273,7 @@ void virtio_disk_rw(struct buf *b, int write)
|
|||||||
|
|
||||||
// tell the device the first index in our chain of descriptors.
|
// tell the device the first index in our chain of descriptors.
|
||||||
disk.avail->ring[disk.avail->idx % NUM] = idx[0];
|
disk.avail->ring[disk.avail->idx % NUM] = idx[0];
|
||||||
|
lib_puts("rw wait...\n");
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
|
|
||||||
// tell the device another avail ring entry is available.
|
// tell the device another avail ring entry is available.
|
||||||
@@ -238,9 +284,9 @@ void virtio_disk_rw(struct buf *b, int write)
|
|||||||
*R(VIRTIO_MMIO_QUEUE_NOTIFY) = 0; // value is queue number
|
*R(VIRTIO_MMIO_QUEUE_NOTIFY) = 0; // value is queue number
|
||||||
|
|
||||||
// Wait for virtio_disk_intr() to say request has finished.
|
// Wait for virtio_disk_intr() to say request has finished.
|
||||||
while (b->disk == 1)
|
// while (b->disk == 1)
|
||||||
{
|
// {
|
||||||
}
|
// }
|
||||||
|
|
||||||
disk.info[idx[0]].b = 0;
|
disk.info[idx[0]].b = 0;
|
||||||
free_chain(idx[0]);
|
free_chain(idx[0]);
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ struct virtq_used
|
|||||||
+------------------+
|
+------------------+
|
||||||
```
|
```
|
||||||
|
|
||||||
為了精簡,我們會將 Buffer 抽離,完成 Device Driver 後會更方便我們在日後實現檔案系統。
|
完成 Device Driver 方便我們在日後實現檔案系統。
|
||||||
|
|
||||||
### 指定寫入的 Sector
|
### 指定寫入的 Sector
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user