1 #include "param.h" 2 #include "types.h" 3 #include "defs.h" 4 #include "x86.h" 5 #include "memlayout.h" 6 #include "mmu.h" 7 #include "proc.h" 8 #include "elf.h" 9 10 extern char data[]; // defined by kernel.ld 11 pde_t *kpgdir; // for use in scheduler() 12 13 // Set up CPU's kernel segment descriptors. 14 // Run once on entry on each CPU. 15 void 16 seginit(void) 17 { 18 struct cpu *c; 19 20 // Map "logical" addresses to virtual addresses using identity map. 21 // Cannot share a CODE descriptor for both kernel and user 22 // because it would have to have DPL_USR, but the CPU forbids 23 // an interrupt from CPL=0 to DPL=3. 24 c = &cpus[cpuid()]; 25 c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0); 26 c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0); 27 c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER); 28 c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER); 29 lgdt(c->gdt, sizeof(c->gdt)); 30 } 31 32 // Return the address of the PTE in page table pgdir 33 // that corresponds to virtual address va. If alloc!=0, 34 // create any required page table pages. 35 static pte_t * 36 walkpgdir(pde_t *pgdir, const void *va, int alloc) 37 { 38 pde_t *pde; 39 pte_t *pgtab; 40 41 pde = &pgdir[PDX(va)]; 42 if(*pde & PTE_P){ 43 pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); 44 } else { 45 if(!alloc || (pgtab = (pte_t*)kalloc()) == 0) 46 return 0; 47 // Make sure all those PTE_P bits are zero. 48 memset(pgtab, 0, PGSIZE); 49 // The permissions here are overly generous, but they can 50 // be further restricted by the permissions in the page table 51 // entries, if necessary. 52 *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U; 53 } 54 return &pgtab[PTX(va)]; 55 } 56 57 // Create PTEs for virtual addresses starting at va that refer to 58 // physical addresses starting at pa. va and size might not 59 // be page-aligned. 60 static int 61 mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) 62 { 63 char *a, *last; 64 pte_t *pte; 65 66 a = (char*)PGROUNDDOWN((uint)va); 67 last = (char*)PGROUNDDOWN(((uint)va) + size - 1); 68 for(;;){ 69 if((pte = walkpgdir(pgdir, a, 1)) == 0) 70 return -1; 71 if(*pte & PTE_P) 72 panic("remap"); 73 *pte = pa | perm | PTE_P; 74 if(a == last) 75 break; 76 a += PGSIZE; 77 pa += PGSIZE; 78 } 79 return 0; 80 } 81 82 // There is one page table per process, plus one that's used when 83 // a CPU is not running any process (kpgdir). The kernel uses the 84 // current process's page table during system calls and interrupts; 85 // page protection bits prevent user code from using the kernel's 86 // mappings. 87 // 88 // setupkvm() and exec() set up every page table like this: 89 // 90 // 0..KERNBASE: user memory (text+data+stack+heap), mapped to 91 // phys memory allocated by the kernel 92 // KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM (for I/O space) 93 // KERNBASE+EXTMEM..data: mapped to EXTMEM..V2P(data) 94 // for the kernel's instructions and r/o data 95 // data..KERNBASE+PHYSTOP: mapped to V2P(data)..PHYSTOP, 96 // rw data + free physical memory 97 // 0xfe000000..0: mapped direct (devices such as ioapic) 98 // 99 // The kernel allocates physical memory for its heap and for user memory 100 // between V2P(end) and the end of physical memory (PHYSTOP) 101 // (directly addressable from end..P2V(PHYSTOP)). 102 103 // This table defines the kernel's mappings, which are present in 104 // every process's page table. 105 static struct kmap { 106 void *virt; 107 uint phys_start; 108 uint phys_end; 109 int perm; 110 } kmap[] = { 111 { (void*)KERNBASE, 0, EXTMEM, PTE_W}, // I/O space 112 { (void*)KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kern text+rodata 113 { (void*)data, V2P(data), PHYSTOP, PTE_W}, // kern data+memory 114 { (void*)DEVSPACE, DEVSPACE, 0, PTE_W}, // more devices 115 }; 116 117 // Set up kernel part of a page table. 118 pde_t* 119 setupkvm(void) 120 { 121 pde_t *pgdir; 122 struct kmap *k; 123 124 if((pgdir = (pde_t*)kalloc()) == 0) 125 return 0; 126 memset(pgdir, 0, PGSIZE); 127 if (P2V(PHYSTOP) > (void*)DEVSPACE) 128 panic("PHYSTOP too high"); 129 for(k = kmap; k < &kmap[NELEM(kmap)]; k++) 130 if(mappages(pgdir, k->virt, k->phys_end - k->phys_start, 131 (uint)k->phys_start, k->perm) < 0) 132 return 0; 133 return pgdir; 134 } 135 136 // Allocate one page table for the machine for the kernel address 137 // space for scheduler processes. 138 void 139 kvmalloc(void) 140 { 141 kpgdir = setupkvm(); 142 switchkvm(); 143 } 144 145 // Switch h/w page table register to the kernel-only page table, 146 // for when no process is running. 147 void 148 switchkvm(void) 149 { 150 lcr3(V2P(kpgdir)); // switch to the kernel page table 151 } 152 153 // Switch TSS and h/w page table to correspond to process p. 154 void 155 switchuvm(struct proc *p) 156 { 157 if(p == 0) 158 panic("switchuvm: no process"); 159 if(p->kstack == 0) 160 panic("switchuvm: no kstack"); 161 if(p->pgdir == 0) 162 panic("switchuvm: no pgdir"); 163 164 pushcli(); 165 mycpu()->gdt[SEG_TSS] = SEG16(STS_T32A, &mycpu()->ts, sizeof(mycpu()->ts)-1, 0); 166 mycpu()->gdt[SEG_TSS].s = 0; 167 mycpu()->ts.ss0 = SEG_KDATA << 3; 168 mycpu()->ts.esp0 = (uint)p->kstack + KSTACKSIZE; 169 // setting IOPL=0 in eflags *and* iomb beyond the tss segment limit 170 // forbids I/O instructions (e.g., inb and outb) from user space 171 mycpu()->ts.iomb = (ushort) 0xFFFF; 172 ltr(SEG_TSS << 3); 173 lcr3(V2P(p->pgdir)); // switch to process's address space 174 popcli(); 175 } 176 177 // Load the initcode into address 0 of pgdir. 178 // sz must be less than a page. 179 void 180 inituvm(pde_t *pgdir, char *init, uint sz) 181 { 182 char *mem; 183 184 if(sz >= PGSIZE) 185 panic("inituvm: more than a page"); 186 mem = kalloc(); 187 memset(mem, 0, PGSIZE); 188 mappages(pgdir, 0, PGSIZE, V2P(mem), PTE_W|PTE_U); 189 memmove(mem, init, sz); 190 } 191 192 // Load a program segment into pgdir. addr must be page-aligned 193 // and the pages from addr to addr+sz must already be mapped. 194 int 195 loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) 196 { 197 uint i, pa, n; 198 pte_t *pte; 199 200 if((uint) addr % PGSIZE != 0) 201 panic("loaduvm: addr must be page aligned"); 202 for(i = 0; i < sz; i += PGSIZE){ 203 if((pte = walkpgdir(pgdir, addr+i, 0)) == 0) 204 panic("loaduvm: address should exist"); 205 pa = PTE_ADDR(*pte); 206 if(sz - i < PGSIZE) 207 n = sz - i; 208 else 209 n = PGSIZE; 210 if(readi(ip, P2V(pa), offset+i, n) != n) 211 return -1; 212 } 213 return 0; 214 } 215 216 // Allocate page tables and physical memory to grow process from oldsz to 217 // newsz, which need not be page aligned. Returns new size or 0 on error. 218 int 219 allocuvm(pde_t *pgdir, uint oldsz, uint newsz) 220 { 221 char *mem; 222 uint a; 223 224 if(newsz >= KERNBASE) 225 return 0; 226 if(newsz < oldsz) 227 return oldsz; 228 229 a = PGROUNDUP(oldsz); 230 for(; a < newsz; a += PGSIZE){ 231 mem = kalloc(); 232 if(mem == 0){ 233 cprintf("allocuvm out of memory\n"); 234 deallocuvm(pgdir, newsz, oldsz); 235 return 0; 236 } 237 memset(mem, 0, PGSIZE); 238 if(mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){ 239 cprintf("allocuvm out of memory (2)\n"); 240 deallocuvm(pgdir, newsz, oldsz); 241 kfree(mem); 242 return 0; 243 } 244 } 245 return newsz; 246 } 247 248 // Deallocate user pages to bring the process size from oldsz to 249 // newsz. oldsz and newsz need not be page-aligned, nor does newsz 250 // need to be less than oldsz. oldsz can be larger than the actual 251 // process size. Returns the new process size. 252 int 253 deallocuvm(pde_t *pgdir, uint oldsz, uint newsz) 254 { 255 pte_t *pte; 256 uint a, pa; 257 258 if(newsz >= oldsz) 259 return oldsz; 260 261 a = PGROUNDUP(newsz); 262 for(; a < oldsz; a += PGSIZE){ 263 pte = walkpgdir(pgdir, (char*)a, 0); 264 if(!pte) 265 a = PGADDR(PDX(a) + 1, 0, 0) - PGSIZE; 266 else if((*pte & PTE_P) != 0){ 267 pa = PTE_ADDR(*pte); 268 if(pa == 0) 269 panic("kfree"); 270 char *v = P2V(pa); 271 kfree(v); 272 *pte = 0; 273 } 274 } 275 return newsz; 276 } 277 278 // Free a page table and all the physical memory pages 279 // in the user part. 280 void 281 freevm(pde_t *pgdir) 282 { 283 uint i; 284 285 if(pgdir == 0) 286 panic("freevm: no pgdir"); 287 deallocuvm(pgdir, KERNBASE, 0); 288 for(i = 0; i < NPDENTRIES; i++){ 289 if(pgdir[i] & PTE_P){ 290 char * v = P2V(PTE_ADDR(pgdir[i])); 291 kfree(v); 292 } 293 } 294 kfree((char*)pgdir); 295 } 296 297 // Clear PTE_U on a page. Used to create an inaccessible 298 // page beneath the user stack. 299 void 300 clearpteu(pde_t *pgdir, char *uva) 301 { 302 pte_t *pte; 303 304 pte = walkpgdir(pgdir, uva, 0); 305 if(pte == 0) 306 panic("clearpteu"); 307 *pte &= ~PTE_U; 308 } 309 310 // Given a parent process's page table, create a copy 311 // of it for a child. 312 pde_t* 313 copyuvm(pde_t *pgdir, uint sz) 314 { 315 pde_t *d; 316 pte_t *pte; 317 uint pa, i, flags; 318 char *mem; 319 320 if((d = setupkvm()) == 0) 321 return 0; 322 for(i = 0; i < sz; i += PGSIZE){ 323 if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0) 324 panic("copyuvm: pte should exist"); 325 if(!(*pte & PTE_P)) 326 panic("copyuvm: page not present"); 327 pa = PTE_ADDR(*pte); 328 flags = PTE_FLAGS(*pte); 329 if((mem = kalloc()) == 0) 330 goto bad; 331 memmove(mem, (char*)P2V(pa), PGSIZE); 332 if(mappages(d, (void*)i, PGSIZE, V2P(mem), flags) < 0) 333 goto bad; 334 } 335 return d; 336 337 bad: 338 freevm(d); 339 return 0; 340 } 341 342 //PAGEBREAK! 343 // Map user virtual address to kernel address. 344 char* 345 uva2ka(pde_t *pgdir, char *uva) 346 { 347 pte_t *pte; 348 349 pte = walkpgdir(pgdir, uva, 0); 350 if((*pte & PTE_P) == 0) 351 return 0; 352 if((*pte & PTE_U) == 0) 353 return 0; 354 return (char*)P2V(PTE_ADDR(*pte)); 355 } 356 357 // Copy len bytes from p to user address va in page table pgdir. 358 // Most useful when pgdir is not the current page table. 359 // uva2ka ensures this only works for PTE_U pages. 360 int 361 copyout(pde_t *pgdir, uint va, void *p, uint len) 362 { 363 char *buf, *pa0; 364 uint n, va0; 365 366 buf = (char*)p; 367 while(len > 0){ 368 va0 = (uint)PGROUNDDOWN(va); 369 pa0 = uva2ka(pgdir, (char*)va0); 370 if(pa0 == 0) 371 return -1; 372 n = PGSIZE - (va - va0); 373 if(n > len) 374 n = len; 375 memmove(pa0 + (va - va0), buf, n); 376 len -= n; 377 buf += n; 378 va = va0 + PGSIZE; 379 } 380 return 0; 381 } 382 383 //PAGEBREAK! 384 // Blank page. 385 //PAGEBREAK! 386 // Blank page. 387 //PAGEBREAK! 388 // Blank page. 389 390