diff options
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 523 |
1 files changed, 255 insertions, 268 deletions
@@ -6,81 +6,86 @@ #include "proc.h" #include "elf.h" -// The mappings from logical to linear are one to one (i.e., -// segmentation doesn't do anything). -// There is one page table per process, plus one that's used -// when a CPU is not running any process (kpgdir). -// A user process uses the same page table as the kernel; the -// page protection bits prevent it from using anything other -// than its memory. -// -// setupkvm() and exec() set up every page table like this: -// 0..640K : user memory (text, data, stack, heap) -// 640K..1M : mapped direct (for IO space) -// 1M..kernend : mapped direct (for the kernel's text and data) -// kernend..PHYSTOP : mapped direct (kernel heap and user pages) -// 0xfe000000..0 : mapped direct (devices such as ioapic) -// -// The kernel allocates memory for its heap and for user memory -// between kernend and the end of physical memory (PHYSTOP). -// The virtual address space of each user program includes the kernel -// (which is inaccessible in user mode). The user program addresses -// range from 0 till 640KB (USERTOP), which where the I/O hole starts -// (both in physical memory and in the kernel's virtual address -// space). - -#define USERTOP 0xA0000 +extern char data[]; // defined in data.S -static uint kerntext; // Linker starts kernel at 1MB -static uint kerntsz; -static uint kerndata; -static uint kerndsz; -static uint kernend; -static uint freesz; static pde_t *kpgdir; // for use in scheduler() -// return the address of the PTE in page table pgdir -// that corresponds to linear address va. if create!=0, +// Allocate one page table for the machine for the kernel address +// space for scheduler processes. +void +kvmalloc(void) +{ + kpgdir = setupkvm(); +} + +// Set up CPU's kernel segment descriptors. +// Run once at boot time on each CPU. +void +seginit(void) +{ + struct cpu *c; + + // Map virtual addresses to linear addresses using identity map. + // Cannot share a CODE descriptor for both kernel and user + // because it would have to have DPL_USR, but the CPU forbids + // an interrupt from CPL=0 to DPL=3. + c = &cpus[cpunum()]; + c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0); + c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0); + c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER); + c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER); + + // Map cpu, and curproc + c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 8, 0); + + lgdt(c->gdt, sizeof(c->gdt)); + loadgs(SEG_KCPU << 3); + + // Initialize cpu-local storage. + cpu = c; + proc = 0; +} + +// Return the address of the PTE in page table pgdir +// that corresponds to linear address va. If create!=0, // create any required page table pages. static pte_t * walkpgdir(pde_t *pgdir, const void *va, int create) { - uint r; pde_t *pde; pte_t *pgtab; pde = &pgdir[PDX(va)]; - if (*pde & PTE_P) { - pgtab = (pte_t*) PTE_ADDR(*pde); - } else if (!create || !(r = (uint) kalloc())) - return 0; - else { - pgtab = (pte_t*) r; - + if(*pde & PTE_P){ + pgtab = (pte_t*)PTE_ADDR(*pde); + } else { + if(!create || (pgtab = (pte_t*)kalloc()) == 0) + return 0; // Make sure all those PTE_P bits are zero. memset(pgtab, 0, PGSIZE); - // The permissions here are overly generous, but they can // be further restricted by the permissions in the page table // entries, if necessary. - *pde = PADDR(r) | PTE_P | PTE_W | PTE_U; + *pde = PADDR(pgtab) | PTE_P | PTE_W | PTE_U; } return &pgtab[PTX(va)]; } -// create PTEs for linear addresses starting at la that refer to +// Create PTEs for linear addresses starting at la that refer to // physical addresses starting at pa. la and size might not // be page-aligned. static int mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm) { - char *first = PGROUNDDOWN(la); - char *last = PGROUNDDOWN(la + size - 1); - char *a = first; - while(1){ - pte_t *pte = walkpgdir(pgdir, a, 1); + char *a, *last; + pte_t *pte; + + a = PGROUNDDOWN(la); + last = PGROUNDDOWN(la + size - 1); + for(;;){ + pte = walkpgdir(pgdir, a, 1); if(pte == 0) - return 0; + return -1; if(*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; @@ -89,292 +94,274 @@ mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm) a += PGSIZE; pa += PGSIZE; } - return 1; + return 0; } -// Set up CPU's kernel segment descriptors. -// Run once at boot time on each CPU. -void -ksegment(void) +// The mappings from logical to linear are one to one (i.e., +// segmentation doesn't do anything). +// There is one page table per process, plus one that's used +// when a CPU is not running any process (kpgdir). +// A user process uses the same page table as the kernel; the +// page protection bits prevent it from using anything other +// than its memory. +// +// setupkvm() and exec() set up every page table like this: +// 0..640K : user memory (text, data, stack, heap) +// 640K..1M : mapped direct (for IO space) +// 1M..end : mapped direct (for the kernel's text and data) +// end..PHYSTOP : mapped direct (kernel heap and user pages) +// 0xfe000000..0 : mapped direct (devices such as ioapic) +// +// The kernel allocates memory for its heap and for user memory +// between kernend and the end of physical memory (PHYSTOP). +// The virtual address space of each user program includes the kernel +// (which is inaccessible in user mode). The user program addresses +// range from 0 till 640KB (USERTOP), which where the I/O hole starts +// (both in physical memory and in the kernel's virtual address +// space). +static struct kmap { + void *p; + void *e; + int perm; +} kmap[] = { + {(void*)USERTOP, (void*)0x100000, PTE_W}, // I/O space + {(void*)0x100000, data, 0 }, // kernel text, rodata + {data, (void*)PHYSTOP, PTE_W}, // kernel data, memory + {(void*)0xFE000000, 0, PTE_W}, // device mappings +}; + +// Set up kernel part of a page table. +pde_t* +setupkvm(void) { - struct cpu *c; + pde_t *pgdir; + struct kmap *k; - // Map virtual addresses to linear addresses using identity map. - // Cannot share a CODE descriptor for both kernel and user - // because it would have to have DPL_USR, but the CPU forbids - // an interrupt from CPL=0 to DPL=3. - c = &cpus[cpunum()]; - c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0); - c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0); - c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER); - c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER); + if((pgdir = (pde_t*)kalloc()) == 0) + return 0; + memset(pgdir, 0, PGSIZE); + k = kmap; + for(k = kmap; k < &kmap[NELEM(kmap)]; k++) + if(mappages(pgdir, k->p, k->e - k->p, (uint)k->p, k->perm) < 0) + return 0; - // map cpu, and curproc - c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 8, 0); + return pgdir; +} - lgdt(c->gdt, sizeof(c->gdt)); - loadgs(SEG_KCPU << 3); - - // Initialize cpu-local storage. - cpu = c; - proc = 0; +// Turn on paging. +void +vmenable(void) +{ + uint cr0; + + switchkvm(); // load kpgdir into cr3 + cr0 = rcr0(); + cr0 |= CR0_PG; + lcr0(cr0); } -// Switch h/w page table and TSS registers to point to process p. +// Switch h/w page table register to the kernel-only page table, +// for when no process is running. +void +switchkvm(void) +{ + lcr3(PADDR(kpgdir)); // switch to the kernel page table +} + +// Switch TSS and h/w page table to correspond to process p. void switchuvm(struct proc *p) { pushcli(); - - // Setup TSS cpu->gdt[SEG_TSS] = SEG16(STS_T32A, &cpu->ts, sizeof(cpu->ts)-1, 0); cpu->gdt[SEG_TSS].s = 0; cpu->ts.ss0 = SEG_KDATA << 3; cpu->ts.esp0 = (uint)proc->kstack + KSTACKSIZE; ltr(SEG_TSS << 3); - - if (p->pgdir == 0) - panic("switchuvm: no pgdir\n"); - + if(p->pgdir == 0) + panic("switchuvm: no pgdir"); lcr3(PADDR(p->pgdir)); // switch to new address space popcli(); } -// Switch h/w page table register to the kernel-only page table, for when -// no process is running. +// Load the initcode into address 0 of pgdir. +// sz must be less than a page. void -switchkvm() +inituvm(pde_t *pgdir, char *init, uint sz) { - lcr3(PADDR(kpgdir)); // Switch to the kernel page table + char *mem; + + if(sz >= PGSIZE) + panic("inituvm: more than a page"); + mem = kalloc(); + memset(mem, 0, PGSIZE); + mappages(pgdir, 0, PGSIZE, PADDR(mem), PTE_W|PTE_U); + memmove(mem, init, sz); } -// Set up kernel part of a page table. -pde_t* -setupkvm(void) +// Load a program segment into pgdir. addr must be page-aligned +// and the pages from addr to addr+sz must already be mapped. +int +loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) { - pde_t *pgdir; - - // Allocate page directory - if (!(pgdir = (pde_t *) kalloc())) - return 0; - memset(pgdir, 0, PGSIZE); - // Map IO space from 640K to 1Mbyte - if (!mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W)) - return 0; - // Map kernel text read-only - if (!mappages(pgdir, (void *) kerntext, kerntsz, kerntext, 0)) - return 0; - // Map kernel data read/write - if (!mappages(pgdir, (void *) kerndata, kerndsz, kerndata, PTE_W)) - return 0; - // Map dynamically-allocated memory read/write (kernel stacks, user mem) - if (!mappages(pgdir, (void *) kernend, freesz, PADDR(kernend), PTE_W)) - return 0; - // Map devices such as ioapic, lapic, ... - if (!mappages(pgdir, (void *)0xFE000000, 0x2000000, 0xFE000000, PTE_W)) - return 0; - return pgdir; -} + uint i, pa, n; + pte_t *pte; -// return the physical address that a given user address -// maps to. the result is also a kernel logical address, -// since the kernel maps the physical memory allocated to user -// processes directly. -char* -uva2ka(pde_t *pgdir, char *uva) -{ - pte_t *pte = walkpgdir(pgdir, uva, 0); - if (pte == 0) return 0; - uint pa = PTE_ADDR(*pte); - return (char *)pa; + if((uint)addr % PGSIZE != 0) + panic("loaduvm: addr must be page aligned"); + for(i = 0; i < sz; i += PGSIZE){ + if((pte = walkpgdir(pgdir, addr+i, 0)) == 0) + panic("loaduvm: address should exist"); + pa = PTE_ADDR(*pte); + if(sz - i < PGSIZE) + n = sz - i; + else + n = PGSIZE; + if(readi(ip, (char*)pa, offset+i, n) != n) + return -1; + } + return 0; } -// allocate sz bytes more memory for a process starting at the -// given user address; allocates physical memory and page -// table entries. addr and sz need not be page-aligned. -// it is a no-op for any parts of the requested memory -// that are already allocated. +// Allocate page tables and physical memory to grow process from oldsz to +// newsz, which need not be page aligned. Returns new size or 0 on error. int -allocuvm(pde_t *pgdir, char *addr, uint sz) +allocuvm(pde_t *pgdir, uint oldsz, uint newsz) { - if (addr + sz > (char*)USERTOP) + char *mem; + uint a; + + if(newsz > USERTOP) return 0; - char *first = PGROUNDDOWN(addr); - char *last = PGROUNDDOWN(addr + sz - 1); - char *a; - for(a = first; a <= last; a += PGSIZE){ - pte_t *pte = walkpgdir(pgdir, a, 0); - if(pte == 0 || (*pte & PTE_P) == 0){ - char *mem = kalloc(); - if(mem == 0){ - // XXX clean up? - return 0; - } - memset(mem, 0, PGSIZE); - mappages(pgdir, a, PGSIZE, PADDR(mem), PTE_W|PTE_U); + if(newsz < oldsz) + return oldsz; + + a = PGROUNDUP(oldsz); + for(; a < newsz; a += PGSIZE){ + mem = kalloc(); + if(mem == 0){ + cprintf("allocuvm out of memory\n"); + deallocuvm(pgdir, newsz, oldsz); + return 0; } + memset(mem, 0, PGSIZE); + mappages(pgdir, (char*)a, PGSIZE, PADDR(mem), PTE_W|PTE_U); } - return 1; + return newsz; } -// deallocate some of the user pages, in response to sbrk() -// with a negative argument. if addr is not page-aligned, -// then only deallocates starting at the next page boundary. +// Deallocate user pages to bring the process size from oldsz to +// newsz. oldsz and newsz need not be page-aligned, nor does newsz +// need to be less than oldsz. oldsz can be larger than the actual +// process size. Returns the new process size. int -deallocuvm(pde_t *pgdir, char *addr, uint sz) +deallocuvm(pde_t *pgdir, uint oldsz, uint newsz) { - if (addr + sz > (char*)USERTOP) - return 0; - char *first = (char*) PGROUNDUP((uint)addr); - char *last = PGROUNDDOWN(addr + sz - 1); - char *a; - for(a = first; a <= last; a += PGSIZE){ - pte_t *pte = walkpgdir(pgdir, a, 0); + pte_t *pte; + uint a, pa; + + if(newsz >= oldsz) + return oldsz; + + a = PGROUNDUP(newsz); + for(; a < oldsz; a += PGSIZE){ + pte = walkpgdir(pgdir, (char*)a, 0); if(pte && (*pte & PTE_P) != 0){ - uint pa = PTE_ADDR(*pte); + pa = PTE_ADDR(*pte); if(pa == 0) - panic("deallocuvm"); - kfree((void *) pa); + panic("kfree"); + kfree((char*)pa); *pte = 0; } } - return 1; + return newsz; } -// free a page table and all the physical memory pages +// Free a page table and all the physical memory pages // in the user part. void freevm(pde_t *pgdir) { - uint i, j, da; - - if (!pgdir) - panic("freevm: no pgdir\n"); - for (i = 0; i < NPDENTRIES; i++) { - da = PTE_ADDR(pgdir[i]); - if (da != 0) { - pte_t *pgtab = (pte_t*) da; - for (j = 0; j < NPTENTRIES; j++) { - if (pgtab[j] != 0) { - uint pa = PTE_ADDR(pgtab[j]); - uint va = PGADDR(i, j, 0); - if (va < USERTOP) // user memory - kfree((void *) pa); - pgtab[j] = 0; - } - } - kfree((void *) da); - pgdir[i] = 0; - } - } - kfree((void *) pgdir); -} - -int -loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) -{ - uint i, pa, n; - pte_t *pte; - - if ((uint)addr % PGSIZE != 0) - panic("loaduvm: addr must be page aligned\n"); - for (i = 0; i < sz; i += PGSIZE) { - if (!(pte = walkpgdir(pgdir, addr+i, 0))) - panic("loaduvm: address should exist\n"); - pa = PTE_ADDR(*pte); - if (sz - i < PGSIZE) n = sz - i; - else n = PGSIZE; - if(readi(ip, (char *)pa, offset+i, n) != n) - return 0; + uint i; + + if(pgdir == 0) + panic("freevm: no pgdir"); + deallocuvm(pgdir, USERTOP, 0); + for(i = 0; i < NPDENTRIES; i++){ + if(pgdir[i] & PTE_P) + kfree((char*)PTE_ADDR(pgdir[i])); } - return 1; + kfree((char*)pgdir); } -void -inituvm(pde_t *pgdir, char *addr, char *init, uint sz) -{ - uint i, pa, n, off; - pte_t *pte; - - for (i = 0; i < sz; i += PGSIZE) { - if (!(pte = walkpgdir(pgdir, (void *)(i+addr), 0))) - panic("inituvm: pte should exist\n"); - off = (i+(uint)addr) % PGSIZE; - pa = PTE_ADDR(*pte); - if (sz - i < PGSIZE) n = sz - i; - else n = PGSIZE; - memmove((char *)pa+off, init+i, n); - } -} - -// given a parent process's page table, create a copy +// Given a parent process's page table, create a copy // of it for a child. pde_t* copyuvm(pde_t *pgdir, uint sz) { - pde_t *d = setupkvm(); + pde_t *d; pte_t *pte; uint pa, i; char *mem; - if (!d) return 0; - for (i = 0; i < sz; i += PGSIZE) { - if (!(pte = walkpgdir(pgdir, (void *)i, 0))) - panic("copyuvm: pte should exist\n"); - if(*pte & PTE_P){ - pa = PTE_ADDR(*pte); - if (!(mem = kalloc())) - return 0; - memmove(mem, (char *)pa, PGSIZE); - if (!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U)) - return 0; - } + if((d = setupkvm()) == 0) + return 0; + for(i = 0; i < sz; i += PGSIZE){ + if((pte = walkpgdir(pgdir, (void*)i, 0)) == 0) + panic("copyuvm: pte should exist"); + if(!(*pte & PTE_P)) + panic("copyuvm: page not present"); + pa = PTE_ADDR(*pte); + if((mem = kalloc()) == 0) + goto bad; + memmove(mem, (char*)pa, PGSIZE); + if(mappages(d, (void*)i, PGSIZE, PADDR(mem), PTE_W|PTE_U) < 0) + goto bad; } return d; -} -// Gather information about physical memory layout. -// Called once during boot. -// Really should find out how much physical memory -// there is rather than assuming PHYSTOP. -void -pminit(void) -{ - extern char end[]; - struct proghdr *ph; - struct elfhdr *elf = (struct elfhdr*)0x10000; // scratch space - - if (elf->magic != ELF_MAGIC || elf->phnum != 2) - panic("pminit: need a text and data segment\n"); - - ph = (struct proghdr*)((uchar*)elf + elf->phoff); - kernend = ((uint)end + PGSIZE) & ~(PGSIZE-1); - kerntext = ph[0].va; - kerndata = ph[1].va; - kerntsz = ph[0].memsz; - kerndsz = ph[1].memsz; - freesz = PHYSTOP - kernend; - - kinit((char *)kernend, freesz); +bad: + freevm(d); + return 0; } -// Allocate one page table for the machine for the kernel address -// space for scheduler processes. -void -kvmalloc(void) +//PAGEBREAK! +// Map user virtual address to kernel physical address. +char* +uva2ka(pde_t *pgdir, char *uva) { - kpgdir = setupkvm(); + pte_t *pte; + + pte = walkpgdir(pgdir, uva, 0); + if((*pte & PTE_P) == 0) + return 0; + if((*pte & PTE_U) == 0) + return 0; + return (char*)PTE_ADDR(*pte); } -// Turn on paging. -void -vmenable(void) +// Copy len bytes from p to user address va in page table pgdir. +// Most useful when pgdir is not the current page table. +// uva2ka ensures this only works for PTE_U pages. +int +copyout(pde_t *pgdir, uint va, void *p, uint len) { - uint cr0; - - switchkvm(); // load kpgdir into cr3 - cr0 = rcr0(); - cr0 |= CR0_PG; - lcr0(cr0); + char *buf, *pa0; + uint n, va0; + + buf = (char*)p; + while(len > 0){ + va0 = (uint)PGROUNDDOWN(va); + pa0 = uva2ka(pgdir, (char*)va0); + if(pa0 == 0) + return -1; + n = PGSIZE - (va - va0); + if(n > len) + n = len; + memmove(pa0 + (va - va0), buf, n); + len -= n; + buf += n; + va = va0 + PGSIZE; + } + return 0; } - |