Skip to content

Commit 30f5bf0

Browse files
Frans KaashoekFrans Kaashoek
Frans Kaashoek
authored and
Frans Kaashoek
committed
some cleanup
1 parent af03ab1 commit 30f5bf0

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

defs.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,22 @@ void uartputc(int);
155155

156156
// vm.c
157157
#define PGROUNDUP(sz) ((sz+PGSIZE-1) & ~(PGSIZE-1))
158+
extern pde_t *kpgdir;
158159
void pminit(void);
159160
void ksegment(void);
160161
void kvmalloc(void);
161-
void loadkvm(void);
162162
void vminit(void);
163163
void jkstack();
164164
void printstack(void);
165-
void printpgdir(uint*);
166-
uint* setupkvm(void); // XXX need pde_t*
167-
char* uva2ka(uint*, char*);
168-
int allocuvm(uint*, char*, uint); // XXX need pde_t*
169-
void freevm(uint*);
170-
void inituvm(uint*, char*, char*, uint);
171-
int loaduvm(uint*, char*, struct inode *ip, uint, uint);
172-
uint* copyuvm(uint*,uint);
165+
void printpgdir(pde_t *);
166+
pde_t* setupkvm(void);
167+
char* uva2ka(pde_t*, char*);
168+
int allocuvm(pde_t*, char*, uint);
169+
void freevm(pde_t*);
170+
void inituvm(pde_t*, char*, char*, uint);
171+
int loaduvm(pde_t*, char*, struct inode *ip, uint, uint);
172+
pde_t* copyuvm(pde_t*,uint);
173173
void loadvm(struct proc*);
174174

175175
// number of elements in fixed-size array
176176
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
177-

mmu.h

-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ struct segdesc {
148148
#define PTE_ADDR(pte) ((uint) (pte) & ~0xFFF)
149149

150150
typedef uint pte_t;
151-
typedef uint pde_t;
152151

153152
// Control Register flags
154153
#define CR0_PE 0x00000001 // Protection Enable

proc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ sched(void)
242242
panic("sched running");
243243
if(readeflags()&FL_IF)
244244
panic("sched interruptible");
245-
loadkvm(); // Switch to the kernel page table
245+
lcr3(PADDR(kpgdir)); // Switch to the kernel page table
246246
intena = cpu->intena;
247247
swtch(&proc->context, cpu->scheduler);
248248
cpu->intena = intena;

types.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
typedef unsigned int uint;
22
typedef unsigned short ushort;
33
typedef unsigned char uchar;
4+
typedef uint pde_t;

vm.c

+34-20
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,33 @@
66
#include "proc.h"
77
#include "elf.h"
88

9-
static uint kerntext; // linear/physical address of start of kernel text
10-
static uint kerntsz;
9+
// The mappings from logical to linear are one to one (i.e.,
10+
// segmentation doesn't do anything).
11+
// The mapping from linear to physical are one to one for the kernel.
12+
// The mappings for the kernel include all of physical memory (until
13+
// PHYSTOP), including the I/O hole, and the top of physical address
14+
// space, where additional devices are located.
15+
// The kernel itself is linked to be at 1MB, and its physical memory
16+
// is also at 1MB.
17+
// Physical memory for user programs is allocated from physical memory
18+
// between kernend and the end of physical memory (PHYSTOP).
19+
// The virtual address space of each user program includes the kernel
20+
// (which is inaccessible in user mode). The user program addresses
21+
// range from 0 till 640KB (USERTOP), which where the I/O hole starts
22+
// (both in physical memory and in the kernel's virtual address
23+
// space).
24+
25+
#define PHYSTOP 0x300000
26+
#define USERTOP 0xA0000
27+
28+
static uint kerntext; // Linker start kernel at 1MB
29+
static uint kerntsz;
1130
static uint kerndata;
1231
static uint kerndsz;
1332
static uint kernend;
1433
static uint freesz;
15-
static pde_t *kpgdir;
34+
35+
pde_t *kpgdir; // One kernel page table for scheduler procs
1636

1737
void
1838
printstack()
@@ -140,13 +160,14 @@ loadvm(struct proc *p)
140160
lcr3(PADDR(p->pgdir)); // switch to new address space
141161
popcli();
142162

143-
// Conservatively flush other processor's TLBs (XXX lazy--just 2 cpus)
163+
// Conservatively flush other processor's TLBs
164+
// XXX lazy--just 2 cpus, but xv6 doesn't need shootdown anyway.
144165
if (cpu->id == 0) lapic_tlbflush(1);
145166
else lapic_tlbflush(0);
146167
}
147168

148-
// Setup kernel part of page table. Linear adresses map one-to-one on
149-
// physical addresses.
169+
// Setup kernel part of a page table. Linear adresses map one-to-one
170+
// on physical addresses.
150171
pde_t*
151172
setupkvm(void)
152173
{
@@ -157,7 +178,7 @@ setupkvm(void)
157178
return 0;
158179
memset(pgdir, 0, PGSIZE);
159180
// Map IO space from 640K to 1Mbyte
160-
if (!mappages(pgdir, (void *)0xA0000, 0x60000, 0xA0000, PTE_W, 0))
181+
if (!mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W, 0))
161182
return 0;
162183
// Map kernel text from kern text addr read-only
163184
if (!mappages(pgdir, (void *) kerntext, kerntsz, kerntext, 0, 0))
@@ -190,7 +211,7 @@ allocuvm(pde_t *pgdir, char *addr, uint sz)
190211
char *mem;
191212

192213
n = PGROUNDUP(sz);
193-
if (addr + n >= 0xA0000)
214+
if (addr + n >= USERTOP)
194215
return 0;
195216
for (i = 0; i < n; i += PGSIZE) {
196217
if (!(mem = kalloc(PGSIZE))) { // XXX cleanup what we did?
@@ -217,7 +238,7 @@ freevm(pde_t *pgdir)
217238
if (pgtab[j] != 0) {
218239
uint pa = PTE_ADDR(pgtab[j]);
219240
uint va = PGADDR(i, j, 0);
220-
if (va >= 0xA0000) // done with user part?
241+
if (va >= USERTOP) // done with user part?
221242
break;
222243
kfree((void *) pa, PGSIZE);
223244
pgtab[j] = 0;
@@ -305,12 +326,12 @@ pminit(void)
305326
kerndata = ph[1].va;
306327
kerntsz = kerndata - kerntext;
307328
kerndsz = kernend - kerndata;
308-
freesz = 0x300000 - kernend; // XXX no more than 3 Mbyte of phys mem
329+
freesz = PHYSTOP - kernend;
309330

310331
cprintf("kerntext@0x%x(sz=0x%x), kerndata@0x%x(sz=0x%x), kernend 0x%x freesz = 0x%x\n",
311332
kerntext, kerntsz, kerndata, kerndsz, kernend, freesz);
312333

313-
kinit((char *)kernend, freesz); // XXX should be called once on bootcpu
334+
kinit((char *)kernend, freesz);
314335
}
315336

316337
// Jump to mainc on a properly-allocated kernel stack
@@ -331,20 +352,13 @@ kvmalloc(void)
331352
kpgdir = setupkvm();
332353
}
333354

334-
// Switch to the kernel page table (used by the scheduler)
335-
void
336-
loadkvm(void)
337-
{
338-
lcr3(PADDR(kpgdir));
339-
}
340-
355+
// Turn on paging.
341356
void
342357
vminit(void)
343358
{
344359
uint cr0;
345360

346-
loadkvm();
347-
// Turn on paging.
361+
lcr3(PADDR(kpgdir));
348362
cr0 = rcr0();
349363
cr0 |= CR0_PE|CR0_PG|CR0_AM|CR0_WP|CR0_NE|CR0_TS|CR0_EM|CR0_MP;
350364
cr0 &= ~(CR0_TS|CR0_EM);

0 commit comments

Comments
 (0)