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 ;
1130static uint kerndata ;
1231static uint kerndsz ;
1332static uint kernend ;
1433static uint freesz ;
15- static pde_t * kpgdir ;
34+
35+ pde_t * kpgdir ; // One kernel page table for scheduler procs
1636
1737void
1838printstack ()
@@ -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.
150171pde_t *
151172setupkvm (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.
341356void
342357vminit (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