6
6
#include "proc.h"
7
7
#include "elf.h"
8
8
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 ;
11
30
static uint kerndata ;
12
31
static uint kerndsz ;
13
32
static uint kernend ;
14
33
static uint freesz ;
15
- static pde_t * kpgdir ;
34
+
35
+ pde_t * kpgdir ; // One kernel page table for scheduler procs
16
36
17
37
void
18
38
printstack ()
@@ -140,13 +160,14 @@ loadvm(struct proc *p)
140
160
lcr3 (PADDR (p -> pgdir )); // switch to new address space
141
161
popcli ();
142
162
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.
144
165
if (cpu -> id == 0 ) lapic_tlbflush (1 );
145
166
else lapic_tlbflush (0 );
146
167
}
147
168
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.
150
171
pde_t *
151
172
setupkvm (void )
152
173
{
@@ -157,7 +178,7 @@ setupkvm(void)
157
178
return 0 ;
158
179
memset (pgdir , 0 , PGSIZE );
159
180
// 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 ))
161
182
return 0 ;
162
183
// Map kernel text from kern text addr read-only
163
184
if (!mappages (pgdir , (void * ) kerntext , kerntsz , kerntext , 0 , 0 ))
@@ -190,7 +211,7 @@ allocuvm(pde_t *pgdir, char *addr, uint sz)
190
211
char * mem ;
191
212
192
213
n = PGROUNDUP (sz );
193
- if (addr + n >= 0xA0000 )
214
+ if (addr + n >= USERTOP )
194
215
return 0 ;
195
216
for (i = 0 ; i < n ; i += PGSIZE ) {
196
217
if (!(mem = kalloc (PGSIZE ))) { // XXX cleanup what we did?
@@ -217,7 +238,7 @@ freevm(pde_t *pgdir)
217
238
if (pgtab [j ] != 0 ) {
218
239
uint pa = PTE_ADDR (pgtab [j ]);
219
240
uint va = PGADDR (i , j , 0 );
220
- if (va >= 0xA0000 ) // done with user part?
241
+ if (va >= USERTOP ) // done with user part?
221
242
break ;
222
243
kfree ((void * ) pa , PGSIZE );
223
244
pgtab [j ] = 0 ;
@@ -305,12 +326,12 @@ pminit(void)
305
326
kerndata = ph [1 ].va ;
306
327
kerntsz = kerndata - kerntext ;
307
328
kerndsz = kernend - kerndata ;
308
- freesz = 0x300000 - kernend ; // XXX no more than 3 Mbyte of phys mem
329
+ freesz = PHYSTOP - kernend ;
309
330
310
331
cprintf ("kerntext@0x%x(sz=0x%x), kerndata@0x%x(sz=0x%x), kernend 0x%x freesz = 0x%x\n" ,
311
332
kerntext , kerntsz , kerndata , kerndsz , kernend , freesz );
312
333
313
- kinit ((char * )kernend , freesz ); // XXX should be called once on bootcpu
334
+ kinit ((char * )kernend , freesz );
314
335
}
315
336
316
337
// Jump to mainc on a properly-allocated kernel stack
@@ -331,20 +352,13 @@ kvmalloc(void)
331
352
kpgdir = setupkvm ();
332
353
}
333
354
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.
341
356
void
342
357
vminit (void )
343
358
{
344
359
uint cr0 ;
345
360
346
- loadkvm ();
347
- // Turn on paging.
361
+ lcr3 (PADDR (kpgdir ));
348
362
cr0 = rcr0 ();
349
363
cr0 |= CR0_PE |CR0_PG |CR0_AM |CR0_WP |CR0_NE |CR0_TS |CR0_EM |CR0_MP ;
350
364
cr0 &= ~(CR0_TS |CR0_EM );
0 commit comments