xv6/vm.c

378 lines
9.3 KiB
C
Raw Normal View History

2010-07-23 16:52:35 +00:00
#include "param.h"
#include "types.h"
#include "defs.h"
#include "x86.h"
#include "memlayout.h"
2010-07-23 16:52:35 +00:00
#include "mmu.h"
#include "proc.h"
#include "elf.h"
extern char data[]; // defined by kernel.ld
pde_t *kpgdir; // for use in scheduler()
struct segdesc gdt[NSEGS];
2010-07-23 16:52:35 +00:00
// Set up CPU's kernel segment descriptors.
2011-08-16 00:11:13 +00:00
// Run once on entry on each CPU.
void
seginit(void)
{
struct cpu *c;
2011-08-16 00:21:14 +00:00
// Map "logical" addresses to virtual 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);
2010-09-02 20:36:38 +00:00
// 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;
}
2010-09-02 20:36:38 +00:00
// Return the address of the PTE in page table pgdir
2011-08-16 00:21:14 +00:00
// that corresponds to virtual address va. If alloc!=0,
// create any required page table pages.
2010-07-23 16:52:35 +00:00
static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int alloc)
2010-07-23 16:52:35 +00:00
{
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
2010-09-01 04:41:25 +00:00
if(*pde & PTE_P){
pgtab = (pte_t*)p2v(PTE_ADDR(*pde));
} else {
if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
return 0;
2010-07-23 16:52:35 +00:00
// 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 = v2p(pgtab) | PTE_P | PTE_W | PTE_U;
2010-07-23 16:52:35 +00:00
}
return &pgtab[PTX(va)];
}
// Create PTEs for virtual addresses starting at va that refer to
// physical addresses starting at pa. va and size might not
// be page-aligned.
2010-07-23 16:52:35 +00:00
static int
mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
2010-07-23 16:52:35 +00:00
{
char *a, *last;
pte_t *pte;
2011-09-02 18:28:44 +00:00
a = (char*)PGROUNDDOWN((uint)va);
last = (char*)PGROUNDDOWN(((uint)va) + size - 1);
for(;;){
if((pte = walkpgdir(pgdir, a, 1)) == 0)
return -1;
if(*pte & PTE_P)
panic("remap");
*pte = pa | perm | PTE_P;
if(a == last)
break;
a += PGSIZE;
pa += PGSIZE;
2010-07-23 16:52:35 +00:00
}
return 0;
2010-07-23 16:52:35 +00:00
}
2011-09-13 16:28:45 +00:00
// There is one page table per process, plus one that's used when
// a CPU is not running any process (kpgdir). The kernel uses the
// current process's page table during system calls and interrupts;
// page protection bits prevent user code from using the kernel's
// mappings.
//
// setupkvm() and exec() set up every page table like this:
2011-09-13 16:28:45 +00:00
//
// 0..KERNBASE: user memory (text+data+stack+heap), mapped to
// phys memory allocated by the kernel
2011-09-01 14:25:20 +00:00
// KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM (for I/O space)
2011-09-13 16:28:45 +00:00
// KERNBASE+EXTMEM..data: mapped to EXTMEM..V2P(data)
// for the kernel's instructions and r/o data
// data..KERNBASE+PHYSTOP: mapped to V2P(data)..PHYSTOP,
// rw data + free physical memory
2011-09-01 14:25:20 +00:00
// 0xfe000000..0: mapped direct (devices such as ioapic)
//
2011-09-13 16:28:45 +00:00
// The kernel allocates physical memory for its heap and for user memory
// between V2P(end) and the end of physical memory (PHYSTOP)
// (directly addressable from end..P2V(PHYSTOP)).
// This table defines the kernel's mappings, which are present in
// every process's page table.
static struct kmap {
2011-08-16 00:21:14 +00:00
void *virt;
uint phys_start;
uint phys_end;
int perm;
} kmap[] = {
2011-09-13 16:28:45 +00:00
{ (void*) KERNBASE, 0, EXTMEM, PTE_W}, // I/O space
{ (void*) KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kernel text+rodata
{ (void*) data, V2P(data), PHYSTOP, PTE_W}, // kernel data, memory
{ (void*) DEVSPACE, DEVSPACE, 0, PTE_W}, // more devices
};
2010-07-23 16:52:35 +00:00
// Set up kernel part of a page table.
pde_t*
setupkvm()
{
pde_t *pgdir;
struct kmap *k;
2010-07-23 16:52:35 +00:00
if((pgdir = (pde_t*)kalloc()) == 0)
return 0;
memset(pgdir, 0, PGSIZE);
2011-09-02 18:28:44 +00:00
if (p2v(PHYSTOP) > (void*)DEVSPACE)
panic("PHYSTOP too high");
for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
2011-09-01 14:25:20 +00:00
if(mappages(pgdir, k->virt, k->phys_end - k->phys_start,
(uint)k->phys_start, k->perm) < 0)
return 0;
return pgdir;
}
2010-07-23 16:52:35 +00:00
// Allocate one page table for the machine for the kernel address
// space for scheduler processes.
void
kvmalloc(void)
{
kpgdir = setupkvm();
switchkvm();
}
2010-09-02 20:36:38 +00:00
// Switch h/w page table register to the kernel-only page table,
// for when no process is running.
void
switchkvm(void)
{
lcr3(v2p(kpgdir)); // switch to the kernel page table
2010-07-23 16:52:35 +00:00
}
// Switch TSS and h/w page table to correspond to process p.
2010-07-23 16:52:35 +00:00
void
switchuvm(struct proc *p)
2010-07-23 16:52:35 +00:00
{
pushcli();
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);
2010-09-01 04:41:25 +00:00
if(p->pgdir == 0)
panic("switchuvm: no pgdir");
lcr3(v2p(p->pgdir)); // switch to new address space
2010-07-23 16:52:35 +00:00
popcli();
}
2010-09-02 20:39:55 +00:00
// Load the initcode into address 0 of pgdir.
// sz must be less than a page.
void
inituvm(pde_t *pgdir, char *init, uint sz)
{
char *mem;
if(sz >= PGSIZE)
panic("inituvm: more than a page");
mem = kalloc();
memset(mem, 0, PGSIZE);
mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U);
memmove(mem, init, sz);
}
2010-09-02 20:39:55 +00:00
// 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)
{
uint i, pa, n;
pte_t *pte;
2011-09-02 18:28:44 +00:00
if((uint) addr % PGSIZE != 0)
panic("loaduvm: addr must be page aligned");
for(i = 0; i < sz; i += PGSIZE){
2011-08-10 01:56:43 +00:00
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, p2v(pa), offset+i, n) != n)
return -1;
}
return 0;
}
// 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.
2010-07-23 16:52:35 +00:00
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
2010-07-23 16:52:35 +00:00
{
2011-01-11 18:51:40 +00:00
char *mem;
uint a;
2011-08-24 14:24:40 +00:00
if(newsz >= KERNBASE)
2010-07-23 16:52:35 +00:00
return 0;
if(newsz < oldsz)
return oldsz;
2011-01-11 18:51:40 +00:00
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;
2010-07-23 16:52:35 +00:00
}
memset(mem, 0, PGSIZE);
mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U);
2010-07-23 16:52:35 +00:00
}
return newsz;
2010-07-23 16:52:35 +00:00
}
// 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, uint oldsz, uint newsz)
{
pte_t *pte;
2011-01-11 18:51:40 +00:00
uint a, pa;
if(newsz >= oldsz)
return oldsz;
2011-01-11 18:51:40 +00:00
a = PGROUNDUP(newsz);
for(; a < oldsz; a += PGSIZE){
2011-08-10 01:56:43 +00:00
pte = walkpgdir(pgdir, (char*)a, 0);
if(!pte)
a += (NPTENTRIES - 1) * PGSIZE;
else if((*pte & PTE_P) != 0){
pa = PTE_ADDR(*pte);
if(pa == 0)
panic("kfree");
char *v = p2v(pa);
kfree(v);
*pte = 0;
}
}
return newsz;
}
2010-09-02 20:36:38 +00:00
// Free a page table and all the physical memory pages
// in the user part.
2010-07-23 16:52:35 +00:00
void
freevm(pde_t *pgdir)
{
2010-09-02 19:18:19 +00:00
uint i;
2010-07-23 16:52:35 +00:00
if(pgdir == 0)
2010-09-02 19:18:19 +00:00
panic("freevm: no pgdir");
2011-08-24 14:24:40 +00:00
deallocuvm(pgdir, KERNBASE, 0);
2010-09-01 04:41:25 +00:00
for(i = 0; i < NPDENTRIES; i++){
2011-09-02 18:28:44 +00:00
if(pgdir[i] & PTE_P){
char * v = p2v(PTE_ADDR(pgdir[i]));
kfree(v);
}
2010-07-23 16:52:35 +00:00
}
kfree((char*)pgdir);
2010-07-23 16:52:35 +00:00
}
// Clear PTE_U on a page. Used to create an inaccessible
// page beneath the user stack.
void
2011-09-02 18:37:04 +00:00
clearpteu(pde_t *pgdir, char *uva)
{
pte_t *pte;
pte = walkpgdir(pgdir, uva, 0);
if(pte == 0)
2011-09-02 18:37:04 +00:00
panic("clearpteu");
*pte &= ~PTE_U;
}
2010-09-02 20:36:38 +00:00
// Given a parent process's page table, create a copy
// of it for a child.
2010-07-23 16:52:35 +00:00
pde_t*
copyuvm(pde_t *pgdir, uint sz)
{
pde_t *d;
2010-07-23 16:52:35 +00:00
pte_t *pte;
uint pa, i;
char *mem;
if((d = setupkvm()) == 0)
return 0;
2010-09-01 04:41:25 +00:00
for(i = 0; i < sz; i += PGSIZE){
2011-09-02 18:28:44 +00:00
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*)p2v(pa), PGSIZE);
if(mappages(d, (void*)i, PGSIZE, v2p(mem), PTE_W|PTE_U) < 0)
goto bad;
2010-07-23 16:52:35 +00:00
}
return d;
2010-09-01 04:27:12 +00:00
bad:
freevm(d);
return 0;
2010-07-23 16:52:35 +00:00
}
//PAGEBREAK!
// Map user virtual address to kernel address.
char*
uva2ka(pde_t *pgdir, char *uva)
{
pte_t *pte;
2011-08-10 01:56:43 +00:00
pte = walkpgdir(pgdir, uva, 0);
if((*pte & PTE_P) == 0)
return 0;
if((*pte & PTE_U) == 0)
return 0;
return (char*)p2v(PTE_ADDR(*pte));
}
// 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)
{
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;
}