2010-07-23 18:52:35 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
2011-07-29 13:31:27 +02:00
|
|
|
#include "memlayout.h"
|
2010-07-23 18:52:35 +02:00
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "elf.h"
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
extern char data[]; // defined in data.S
|
2010-08-06 17:12:18 +02:00
|
|
|
static pde_t *kpgdir; // for use in scheduler()
|
2011-07-29 13:31:27 +02:00
|
|
|
struct segdesc gdt[NSEGS];
|
2010-07-23 18:52:35 +02:00
|
|
|
|
2011-07-29 13:31:27 +02:00
|
|
|
|
|
|
|
// page map for during boot
|
|
|
|
// XXX build a static page table in assembly
|
|
|
|
static void
|
|
|
|
pgmap(void *va, void *last, uint pa)
|
|
|
|
{
|
|
|
|
pde_t *pde;
|
|
|
|
pte_t *pgtab;
|
|
|
|
pte_t *pte;
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
pde = &kpgdir[PDX(va)];
|
|
|
|
pde_t pdev = *pde;
|
|
|
|
if (pdev == 0) {
|
|
|
|
pgtab = (pte_t *) pgalloc();
|
|
|
|
*pde = v2p(pgtab) | PTE_P | PTE_W;
|
|
|
|
} else {
|
|
|
|
pgtab = (pte_t*)p2v(PTE_ADDR(pdev));
|
|
|
|
}
|
|
|
|
pte = &pgtab[PTX(va)];
|
|
|
|
*pte = pa | PTE_W | PTE_P;
|
|
|
|
if(va == last)
|
|
|
|
break;
|
|
|
|
va += PGSIZE;
|
|
|
|
pa += PGSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up a page table to get off the ground
|
2011-02-20 03:17:55 +01:00
|
|
|
void
|
2011-07-29 13:31:27 +02:00
|
|
|
pginit(char* (*alloc)(void))
|
2011-02-20 03:17:55 +01:00
|
|
|
{
|
2011-07-29 13:31:27 +02:00
|
|
|
uint cr0;
|
|
|
|
|
|
|
|
kpgdir = (pde_t *) alloc();
|
|
|
|
pgmap((void *) 0, (void *) PHYSTOP, 0); // map pa 0 at va 0
|
|
|
|
pgmap((void *) KERNBASE, (void *) (KERNBASE+PHYSTOP), 0); // map pa 0 at va KERNBASE
|
|
|
|
pgmap((void*)0xFE000000, 0, 0xFE000000);
|
|
|
|
|
|
|
|
switchkvm(); // load kpgdir into cr3
|
|
|
|
|
|
|
|
cr0 = rcr0();
|
|
|
|
cr0 |= CR0_PG;
|
|
|
|
lcr0(cr0); // paging on
|
|
|
|
|
|
|
|
// new gdt
|
|
|
|
gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
|
|
|
|
gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
|
|
|
|
lgdt((void *)v2p(gdt), sizeof(gdt));
|
|
|
|
loadgs(SEG_KDATA << 3);
|
|
|
|
loadfs(SEG_KDATA << 3);
|
|
|
|
loades(SEG_KDATA << 3);
|
|
|
|
loadds(SEG_KDATA << 3);
|
|
|
|
loadss(SEG_KDATA << 3);
|
|
|
|
|
|
|
|
__asm volatile("ljmp %0,$1f\n 1:\n" :: "i" (SEG_KCODE << 3)); // reload cs
|
2011-02-20 03:17:55 +01:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:23:15 +02:00
|
|
|
// Set up CPU's kernel segment descriptors.
|
|
|
|
// Run once at boot time on each CPU.
|
|
|
|
void
|
2010-09-13 21:34:44 +02:00
|
|
|
seginit(void)
|
2010-09-02 22:23:15 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2010-09-02 22:36:38 +02:00
|
|
|
// Map cpu, and curproc
|
2010-09-02 22:23:15 +02:00
|
|
|
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 22:36:38 +02:00
|
|
|
// Return the address of the PTE in page table pgdir
|
|
|
|
// that corresponds to linear address va. If create!=0,
|
2010-08-05 18:10:54 +02:00
|
|
|
// create any required page table pages.
|
2010-07-23 18:52:35 +02:00
|
|
|
static pte_t *
|
|
|
|
walkpgdir(pde_t *pgdir, const void *va, int create)
|
|
|
|
{
|
|
|
|
pde_t *pde;
|
|
|
|
pte_t *pgtab;
|
|
|
|
|
|
|
|
pde = &pgdir[PDX(va)];
|
2010-09-01 06:41:25 +02:00
|
|
|
if(*pde & PTE_P){
|
2011-07-29 13:31:27 +02:00
|
|
|
pgtab = (pte_t*)p2v(PTE_ADDR(*pde));
|
2011-01-11 19:01:13 +01:00
|
|
|
} else {
|
|
|
|
if(!create || (pgtab = (pte_t*)kalloc()) == 0)
|
|
|
|
return 0;
|
2010-07-23 18:52:35 +02: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.
|
2011-07-29 13:31:27 +02:00
|
|
|
*pde = v2p(pgtab) | PTE_P | PTE_W | PTE_U;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
return &pgtab[PTX(va)];
|
|
|
|
}
|
|
|
|
|
2010-09-02 22:36:38 +02:00
|
|
|
// Create PTEs for linear addresses starting at la that refer to
|
2010-08-05 22:00:59 +02:00
|
|
|
// physical addresses starting at pa. la and size might not
|
|
|
|
// be page-aligned.
|
2010-07-23 18:52:35 +02:00
|
|
|
static int
|
2010-07-26 14:10:02 +02:00
|
|
|
mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm)
|
2010-07-23 18:52:35 +02:00
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
char *a, *last;
|
|
|
|
pte_t *pte;
|
|
|
|
|
|
|
|
a = PGROUNDDOWN(la);
|
|
|
|
last = PGROUNDDOWN(la + size - 1);
|
|
|
|
for(;;){
|
|
|
|
pte = walkpgdir(pgdir, a, 1);
|
2010-08-05 22:00:59 +02:00
|
|
|
if(pte == 0)
|
2011-01-11 19:01:13 +01:00
|
|
|
return -1;
|
2010-08-05 18:10:54 +02:00
|
|
|
if(*pte & PTE_P)
|
|
|
|
panic("remap");
|
2010-08-05 22:00:59 +02:00
|
|
|
*pte = pa | perm | PTE_P;
|
|
|
|
if(a == last)
|
|
|
|
break;
|
|
|
|
a += PGSIZE;
|
|
|
|
pa += PGSIZE;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
return 0;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:23:15 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2011-07-29 13:31:27 +02:00
|
|
|
//
|
2010-09-02 22:23:15 +02:00
|
|
|
// setupkvm() and exec() set up every page table like this:
|
2011-08-08 05:03:48 +02:00
|
|
|
// 0..USERTOP : user memory (text, data, stack, heap), mapped to some phys mem
|
2011-07-29 13:31:27 +02:00
|
|
|
// KERNBASE+640K..KERNBASE+1M: mapped to 640K..1M
|
|
|
|
// KERNBASE+1M..KERNBASE+end : mapped to 1M..end
|
|
|
|
// KERNBASE+end..KERBASE+PHYSTOP : mapped to end..PHYSTOP (free memory)
|
2010-09-02 22:23:15 +02:00
|
|
|
// 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
|
2011-07-29 13:31:27 +02:00
|
|
|
// (which is inaccessible in user mode). The user program sits in
|
|
|
|
// the bottom of the address space, and the kernel at the top at KERNBASE.
|
2011-02-20 03:17:55 +01:00
|
|
|
static struct kmap {
|
2011-07-29 13:31:27 +02:00
|
|
|
void *l;
|
|
|
|
uint p;
|
|
|
|
uint e;
|
2011-02-20 03:17:55 +01:00
|
|
|
int perm;
|
|
|
|
} kmap[] = {
|
2011-07-29 13:31:27 +02:00
|
|
|
{ P2V(IOSPACEB), IOSPACEB, IOSPACEE, PTE_W}, // I/O space
|
|
|
|
{ (void *)KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kernel text, rodata
|
|
|
|
{ data, V2P(data), PHYSTOP, PTE_W}, // kernel data, memory
|
|
|
|
{ (void*)0xFE000000, 0xFE000000, 0, PTE_W}, // device mappings
|
2011-02-20 03:17:55 +01:00
|
|
|
};
|
2010-07-23 18:52:35 +02:00
|
|
|
|
2010-09-02 22:23:15 +02:00
|
|
|
// Set up kernel part of a page table.
|
|
|
|
pde_t*
|
|
|
|
setupkvm(void)
|
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
pde_t *pgdir;
|
2011-02-20 03:17:55 +01:00
|
|
|
struct kmap *k;
|
2010-07-23 18:52:35 +02:00
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
if((pgdir = (pde_t*)kalloc()) == 0)
|
2010-09-02 22:23:15 +02:00
|
|
|
return 0;
|
|
|
|
memset(pgdir, 0, PGSIZE);
|
2011-02-20 03:17:55 +01:00
|
|
|
k = kmap;
|
|
|
|
for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
|
2011-07-29 13:31:27 +02:00
|
|
|
if(mappages(pgdir, k->l, k->e - k->p, (uint)k->p, k->perm) < 0)
|
2011-02-20 03:17:55 +01:00
|
|
|
return 0;
|
|
|
|
|
2010-09-02 22:23:15 +02:00
|
|
|
return pgdir;
|
|
|
|
}
|
2010-07-23 18:52:35 +02:00
|
|
|
|
2011-07-29 13:31:27 +02: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 22:23:15 +02:00
|
|
|
// Turn on paging.
|
|
|
|
void
|
|
|
|
vmenable(void)
|
|
|
|
{
|
|
|
|
uint cr0;
|
|
|
|
|
|
|
|
switchkvm(); // load kpgdir into cr3
|
|
|
|
cr0 = rcr0();
|
|
|
|
cr0 |= CR0_PG;
|
|
|
|
lcr0(cr0);
|
2011-07-29 13:31:27 +02:00
|
|
|
|
|
|
|
struct cpu *c = &cpus[0];
|
|
|
|
lgdt((void *)v2p((void *)(c->gdt)), sizeof(c->gdt));
|
|
|
|
loadgs(SEG_KCPU << 3);
|
|
|
|
loadfs(SEG_KDATA << 3);
|
|
|
|
loades(SEG_KDATA << 3);
|
|
|
|
loadds(SEG_KDATA << 3);
|
|
|
|
loadss(SEG_KDATA << 3);
|
|
|
|
|
|
|
|
__asm volatile("ljmp %0,$1f\n 1:\n" :: "i" (SEG_KCODE << 3)); // reload cs
|
2010-09-02 22:23:15 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:36:38 +02:00
|
|
|
// Switch h/w page table register to the kernel-only page table,
|
|
|
|
// for when no process is running.
|
2010-09-02 22:23:15 +02:00
|
|
|
void
|
2011-02-20 03:17:55 +01:00
|
|
|
switchkvm(void)
|
2010-09-02 22:23:15 +02:00
|
|
|
{
|
2011-07-29 13:31:27 +02:00
|
|
|
lcr3(v2p(kpgdir)); // switch to the kernel page table
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
// Switch TSS and h/w page table to correspond to process p.
|
2010-07-23 18:52:35 +02:00
|
|
|
void
|
2010-08-06 17:12:18 +02:00
|
|
|
switchuvm(struct proc *p)
|
2010-07-23 18:52:35 +02: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 06:41:25 +02:00
|
|
|
if(p->pgdir == 0)
|
2011-02-20 03:17:55 +01:00
|
|
|
panic("switchuvm: no pgdir");
|
2011-07-29 13:31:27 +02:00
|
|
|
lcr3(v2p(p->pgdir)); // switch to new address space
|
2010-07-23 18:52:35 +02:00
|
|
|
popcli();
|
|
|
|
}
|
|
|
|
|
2010-09-02 22:39:55 +02:00
|
|
|
// Load the initcode into address 0 of pgdir.
|
|
|
|
// sz must be less than a page.
|
2010-09-02 22:23:15 +02:00
|
|
|
void
|
|
|
|
inituvm(pde_t *pgdir, char *init, uint sz)
|
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
char *mem;
|
|
|
|
|
|
|
|
if(sz >= PGSIZE)
|
2010-09-02 22:23:15 +02:00
|
|
|
panic("inituvm: more than a page");
|
2011-01-11 19:01:13 +01:00
|
|
|
mem = kalloc();
|
2010-09-02 22:23:15 +02:00
|
|
|
memset(mem, 0, PGSIZE);
|
2011-07-29 13:31:27 +02:00
|
|
|
mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U);
|
2010-09-02 22:23:15 +02:00
|
|
|
memmove(mem, init, sz);
|
|
|
|
}
|
|
|
|
|
2010-09-02 22:39:55 +02:00
|
|
|
// Load a program segment into pgdir. addr must be page-aligned
|
|
|
|
// and the pages from addr to addr+sz must already be mapped.
|
2010-09-02 22:23:15 +02:00
|
|
|
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)
|
2011-02-20 03:17:55 +01:00
|
|
|
panic("loaduvm: addr must be page aligned");
|
2010-09-02 22:23:15 +02:00
|
|
|
for(i = 0; i < sz; i += PGSIZE){
|
2011-01-11 19:01:13 +01:00
|
|
|
if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
|
2011-02-20 03:17:55 +01:00
|
|
|
panic("loaduvm: address should exist");
|
2010-09-02 22:23:15 +02:00
|
|
|
pa = PTE_ADDR(*pte);
|
2011-01-11 19:01:13 +01:00
|
|
|
if(sz - i < PGSIZE)
|
|
|
|
n = sz - i;
|
|
|
|
else
|
|
|
|
n = PGSIZE;
|
2011-08-01 03:27:02 +02:00
|
|
|
if(readi(ip, p2v(pa), offset+i, n) != n)
|
2011-01-11 19:01:13 +01:00
|
|
|
return -1;
|
2010-09-02 22:23:15 +02:00
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
return 0;
|
2010-09-02 22:23:15 +02:00
|
|
|
}
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
// 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 18:52:35 +02:00
|
|
|
int
|
2010-09-03 00:28:36 +02:00
|
|
|
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
|
2010-07-23 18:52:35 +02:00
|
|
|
{
|
2011-01-11 19:51:40 +01:00
|
|
|
char *mem;
|
|
|
|
uint a;
|
2011-01-11 19:01:13 +01:00
|
|
|
|
2010-09-03 00:28:36 +02:00
|
|
|
if(newsz > USERTOP)
|
2010-07-23 18:52:35 +02:00
|
|
|
return 0;
|
2011-01-11 19:01:13 +01:00
|
|
|
if(newsz < oldsz)
|
|
|
|
return oldsz;
|
|
|
|
|
2011-01-11 19:51:40 +01:00
|
|
|
a = PGROUNDUP(oldsz);
|
|
|
|
for(; a < newsz; a += PGSIZE){
|
2011-01-11 19:01:13 +01:00
|
|
|
mem = kalloc();
|
2010-09-03 00:28:36 +02:00
|
|
|
if(mem == 0){
|
|
|
|
cprintf("allocuvm out of memory\n");
|
|
|
|
deallocuvm(pgdir, newsz, oldsz);
|
|
|
|
return 0;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
2010-09-03 00:28:36 +02:00
|
|
|
memset(mem, 0, PGSIZE);
|
2011-07-29 13:31:27 +02:00
|
|
|
mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U);
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
return newsz;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
2010-09-03 00:28:36 +02: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.
|
2010-08-10 23:08:41 +02:00
|
|
|
int
|
2010-09-03 00:28:36 +02:00
|
|
|
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
|
2010-08-10 23:08:41 +02:00
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
pte_t *pte;
|
2011-01-11 19:51:40 +01:00
|
|
|
uint a, pa;
|
2011-01-11 19:01:13 +01:00
|
|
|
|
|
|
|
if(newsz >= oldsz)
|
|
|
|
return oldsz;
|
|
|
|
|
2011-01-11 19:51:40 +01:00
|
|
|
a = PGROUNDUP(newsz);
|
|
|
|
for(; a < oldsz; a += PGSIZE){
|
|
|
|
pte = walkpgdir(pgdir, (char*)a, 0);
|
2010-08-10 23:08:41 +02:00
|
|
|
if(pte && (*pte & PTE_P) != 0){
|
2011-01-11 19:01:13 +01:00
|
|
|
pa = PTE_ADDR(*pte);
|
2010-08-10 23:08:41 +02:00
|
|
|
if(pa == 0)
|
2010-09-03 00:28:36 +02:00
|
|
|
panic("kfree");
|
2011-08-01 03:27:02 +02:00
|
|
|
char *v = p2v(pa);
|
|
|
|
kfree(v);
|
2010-08-10 23:08:41 +02:00
|
|
|
*pte = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
return newsz;
|
2010-08-10 23:08:41 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:36:38 +02:00
|
|
|
// Free a page table and all the physical memory pages
|
2010-08-05 22:00:59 +02:00
|
|
|
// in the user part.
|
2010-07-23 18:52:35 +02:00
|
|
|
void
|
|
|
|
freevm(pde_t *pgdir)
|
|
|
|
{
|
2010-09-02 21:18:19 +02:00
|
|
|
uint i;
|
2010-07-23 18:52:35 +02:00
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
if(pgdir == 0)
|
2010-09-02 21:18:19 +02:00
|
|
|
panic("freevm: no pgdir");
|
2010-09-03 00:28:36 +02:00
|
|
|
deallocuvm(pgdir, USERTOP, 0);
|
2010-09-01 06:41:25 +02:00
|
|
|
for(i = 0; i < NPDENTRIES; i++){
|
2011-08-01 03:27:02 +02:00
|
|
|
if(pgdir[i] & PTE_P) {
|
|
|
|
char * v = p2v(PTE_ADDR(pgdir[i]));
|
|
|
|
kfree(v);
|
|
|
|
}
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
kfree((char*)pgdir);
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:36:38 +02:00
|
|
|
// Given a parent process's page table, create a copy
|
2010-08-06 17:12:18 +02:00
|
|
|
// of it for a child.
|
2010-07-23 18:52:35 +02:00
|
|
|
pde_t*
|
|
|
|
copyuvm(pde_t *pgdir, uint sz)
|
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
pde_t *d;
|
2010-07-23 18:52:35 +02:00
|
|
|
pte_t *pte;
|
|
|
|
uint pa, i;
|
|
|
|
char *mem;
|
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
if((d = setupkvm()) == 0)
|
|
|
|
return 0;
|
2010-09-01 06:41:25 +02:00
|
|
|
for(i = 0; i < sz; i += PGSIZE){
|
2011-01-11 19:01:13 +01:00
|
|
|
if((pte = walkpgdir(pgdir, (void*)i, 0)) == 0)
|
2011-02-20 03:17:55 +01:00
|
|
|
panic("copyuvm: pte should exist");
|
2010-09-01 23:14:58 +02:00
|
|
|
if(!(*pte & PTE_P))
|
2011-02-20 03:17:55 +01:00
|
|
|
panic("copyuvm: page not present");
|
2010-09-01 23:14:58 +02:00
|
|
|
pa = PTE_ADDR(*pte);
|
2011-01-11 19:01:13 +01:00
|
|
|
if((mem = kalloc()) == 0)
|
2010-09-01 23:14:58 +02:00
|
|
|
goto bad;
|
2011-08-01 03:27:02 +02:00
|
|
|
memmove(mem, (char*)p2v(pa), PGSIZE);
|
2011-07-29 13:31:27 +02:00
|
|
|
if(mappages(d, (void*)i, PGSIZE, v2p(mem), PTE_W|PTE_U) < 0)
|
2010-09-01 23:14:58 +02:00
|
|
|
goto bad;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
return d;
|
2010-09-01 06:27:12 +02:00
|
|
|
|
|
|
|
bad:
|
|
|
|
freevm(d);
|
|
|
|
return 0;
|
2010-07-23 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
//PAGEBREAK!
|
2011-08-01 03:27:02 +02:00
|
|
|
// Map user virtual address to kernel address.
|
2011-02-20 03:17:55 +01:00
|
|
|
char*
|
|
|
|
uva2ka(pde_t *pgdir, char *uva)
|
|
|
|
{
|
|
|
|
pte_t *pte;
|
|
|
|
|
|
|
|
pte = walkpgdir(pgdir, uva, 0);
|
|
|
|
if((*pte & PTE_P) == 0)
|
|
|
|
return 0;
|
|
|
|
if((*pte & PTE_U) == 0)
|
|
|
|
return 0;
|
2011-08-01 03:27:02 +02:00
|
|
|
return (char*)p2v(PTE_ADDR(*pte));
|
2011-02-20 03:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy len bytes from p to user address va in page table pgdir.
|
|
|
|
// Most useful when pgdir is not the current page table.
|
2010-09-27 22:14:33 +02:00
|
|
|
// uva2ka ensures this only works for PTE_U pages.
|
|
|
|
int
|
2011-02-20 03:17:55 +01:00
|
|
|
copyout(pde_t *pgdir, uint va, void *p, uint len)
|
2010-09-27 22:14:33 +02:00
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
char *buf, *pa0;
|
|
|
|
uint n, va0;
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
buf = (char*)p;
|
2010-09-27 22:14:33 +02:00
|
|
|
while(len > 0){
|
2011-01-11 19:01:13 +01:00
|
|
|
va0 = (uint)PGROUNDDOWN(va);
|
|
|
|
pa0 = uva2ka(pgdir, (char*)va0);
|
2010-09-27 22:14:33 +02:00
|
|
|
if(pa0 == 0)
|
2011-01-11 19:01:13 +01:00
|
|
|
return -1;
|
|
|
|
n = PGSIZE - (va - va0);
|
2010-09-27 22:14:33 +02:00
|
|
|
if(n > len)
|
|
|
|
n = len;
|
|
|
|
memmove(pa0 + (va - va0), buf, n);
|
|
|
|
len -= n;
|
|
|
|
buf += n;
|
|
|
|
va = va0 + PGSIZE;
|
|
|
|
}
|
2011-01-11 19:01:13 +01:00
|
|
|
return 0;
|
2010-09-27 22:14:33 +02:00
|
|
|
}
|