Skip to content

Commit af6c35e

Browse files
committed
Introduce alloc3_desc and UsedArea to make code easier to read
1 parent a8305b7 commit af6c35e

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ initcode
1212
initcode.out
1313
kernelmemfs
1414
mkfs
15+
kernel/kernel
16+
user/usys.S
1517
.gdbinit

kernel/virtio.h

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#define VIRTIO_RING_F_INDIRECT_DESC 28
4444
#define VIRTIO_RING_F_EVENT_IDX 29
4545

46+
// this many virtio descriptors.
47+
// must be a power of two.
48+
#define NUM 8
49+
4650
struct VRingDesc {
4751
uint64 addr;
4852
uint32 len;
@@ -60,3 +64,9 @@ struct VRingUsedElem {
6064
// for disk ops
6165
#define VIRTIO_BLK_T_IN 0 // read the disk
6266
#define VIRTIO_BLK_T_OUT 1 // write the disk
67+
68+
struct UsedArea {
69+
uint16 flags;
70+
uint16 id;
71+
struct VRingUsedElem elems[NUM];
72+
};

kernel/virtio_disk.c

+28-33
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222

2323
struct spinlock virtio_disk_lock;
2424

25-
// this many virtio descriptors.
26-
// must be a power of two.
27-
#define NUM 8
28-
2925
// memory for virtio descriptors &c for queue 0.
3026
// this is a global instead of allocated because it has
3127
// to be multiple contiguous pages, which kalloc()
@@ -34,7 +30,7 @@ __attribute__ ((aligned (PGSIZE)))
3430
static char pages[2*PGSIZE];
3531
static struct VRingDesc *desc;
3632
static uint16 *avail;
37-
static char *used;
33+
static struct UsedArea *used;
3834

3935
// our own book-keeping.
4036
static char free[NUM]; // is a descriptor free?
@@ -106,7 +102,7 @@ virtio_disk_init(void)
106102

107103
desc = (struct VRingDesc *) pages;
108104
avail = (uint16*)(((char*)desc) + NUM*sizeof(struct VRingDesc));
109-
used = pages + PGSIZE;
105+
used = (struct UsedArea *) (pages + PGSIZE);
110106

111107
for(int i = 0; i < NUM; i++)
112108
free[i] = 1;
@@ -153,6 +149,21 @@ free_chain(int i)
153149
}
154150
}
155151

152+
static int
153+
alloc3_desc(int *idx)
154+
{
155+
for(int i = 0; i < 3; i++){
156+
idx[i] = alloc_desc();
157+
if(idx[i] < 0){
158+
for(int j = 0; j < i; j++)
159+
free_desc(idx[j]);
160+
return -1;
161+
break;
162+
}
163+
}
164+
return 0;
165+
}
166+
156167
void
157168
virtio_disk_rw(struct buf *b)
158169
{
@@ -167,21 +178,12 @@ virtio_disk_rw(struct buf *b)
167178
// allocate the three descriptors.
168179
int idx[3];
169180
while(1){
170-
int done = 1;
171-
for(int i = 0; i < 3; i++){
172-
idx[i] = alloc_desc();
173-
if(idx[i] < 0){
174-
for(int j = 0; j < i; j++)
175-
free_desc(idx[j]);
176-
done = 0;
177-
break;
178-
}
179-
}
180-
if(done)
181+
if(alloc3_desc(idx) == 0) {
181182
break;
183+
}
182184
sleep(&free[0], &virtio_disk_lock);
183185
}
184-
186+
185187
// format the three descriptors.
186188
// qemu's virtio-blk.c reads them.
187189

@@ -242,28 +244,21 @@ virtio_disk_rw(struct buf *b)
242244
void
243245
virtio_disk_intr()
244246
{
245-
// the used area is:
246-
// uint16 flags
247-
// uint16 idx
248-
// array of VRingUsedElem
249-
volatile uint16 *idxp = (uint16 *)(used + 2);
250-
volatile struct VRingUsedElem *e0 = (struct VRingUsedElem *)(used + 4);
251-
252247
acquire(&virtio_disk_lock);
253248

254-
while((used_idx % NUM) != (*idxp % NUM)){
255-
volatile struct VRingUsedElem *ue = &e0[used_idx];
249+
while((used_idx % NUM) != (used->id % NUM)){
250+
int id = used->elems[used_idx].id;
256251

257-
if(info[ue->id].status != 0)
252+
if(info[id].status != 0)
258253
panic("virtio_disk_intr status");
259254

260-
info[ue->id].b->flags |= B_VALID;
261-
info[ue->id].b->flags &= ~B_DIRTY;
255+
info[id].b->flags |= B_VALID;
256+
info[id].b->flags &= ~B_DIRTY;
262257

263-
wakeup(info[ue->id].b);
258+
wakeup(info[id].b);
264259

265-
info[ue->id].b = 0;
266-
free_chain(ue->id);
260+
info[id].b = 0;
261+
free_chain(id);
267262

268263
used_idx = (used_idx + 1) % NUM;
269264
}

0 commit comments

Comments
 (0)