Skip to content
Snippets Groups Projects
vm.c 9.12 KiB
Newer Older
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "param.h"
#include "types.h"
#include "defs.h"
#include "x86.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "memlayout.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "mmu.h"
#include "proc.h"
#include "elf.h"

extern char data[];  // defined in data.S
pde_t *kpgdir;  // for use in scheduler()
Frans Kaashoek's avatar
Frans Kaashoek committed
struct segdesc gdt[NSEGS];
Frans Kaashoek's avatar
Frans Kaashoek committed

// Set up CPU's kernel segment descriptors.
Frans Kaashoek's avatar
Frans Kaashoek committed
// Run once on entry on each CPU.
Frans Kaashoek's avatar
Frans Kaashoek committed
  // 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);

  // 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
Frans Kaashoek's avatar
Frans Kaashoek committed
// that corresponds to virtual address va.  If alloc!=0,
// create any required page table pages.
Frans Kaashoek's avatar
Frans Kaashoek committed
static pte_t *
Frans Kaashoek's avatar
Frans Kaashoek committed
walkpgdir(pde_t *pgdir, const void *va, char* (*alloc)(void))
Frans Kaashoek's avatar
Frans Kaashoek committed
{
  pde_t *pde;
  pte_t *pgtab;

  pde = &pgdir[PDX(va)];
Austin Clements's avatar
Austin Clements committed
  if(*pde & PTE_P){
Frans Kaashoek's avatar
Frans Kaashoek committed
    pgtab = (pte_t*)p2v(PTE_ADDR(*pde));
Russ Cox's avatar
Russ Cox committed
  } else {
Frans Kaashoek's avatar
Frans Kaashoek committed
    if(!alloc || (pgtab = (pte_t*)alloc()) == 0)
Russ Cox's avatar
Russ Cox committed
      return 0;
Frans Kaashoek's avatar
Frans Kaashoek committed
    // 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.
Frans Kaashoek's avatar
Frans Kaashoek committed
    *pde = v2p(pgtab) | PTE_P | PTE_W | PTE_U;
Frans Kaashoek's avatar
Frans Kaashoek committed
  }
  return &pgtab[PTX(va)];
}

Frans Kaashoek's avatar
Frans Kaashoek committed
// Create PTEs for virtual addresses starting at va that refer to
// physical addresses starting at pa. va and size might not
// be page-aligned.
Frans Kaashoek's avatar
Frans Kaashoek committed
static int
Frans Kaashoek's avatar
Frans Kaashoek committed
mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm, char* (*alloc)(void))
Frans Kaashoek's avatar
Frans Kaashoek committed
{
Russ Cox's avatar
Russ Cox committed
  char *a, *last;
  pte_t *pte;
  
Frans Kaashoek's avatar
Frans Kaashoek committed
  a = (char *) PGROUNDDOWN((uint) va);
  last = (char *) PGROUNDDOWN(((uint) va) + size - 1);
Russ Cox's avatar
Russ Cox committed
  for(;;){
Frans Kaashoek's avatar
Frans Kaashoek committed
    pte = walkpgdir(pgdir, a, alloc);
    if(pte == 0)
Russ Cox's avatar
Russ Cox committed
      return -1;
    *pte = pa | perm | PTE_P;
    if(a == last)
      break;
    a += PGSIZE;
    pa += PGSIZE;
Frans Kaashoek's avatar
Frans Kaashoek committed
  }
Russ Cox's avatar
Russ Cox committed
  return 0;
Frans Kaashoek's avatar
Frans Kaashoek committed
}

Frans Kaashoek's avatar
Frans Kaashoek committed
// The mappings from logical to virtual 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:
Frans Kaashoek's avatar
x  
Frans Kaashoek committed
//   0..USERTOP      : user memory (text, data, stack, heap), mapped to some unused phys mem
Frans Kaashoek's avatar
Frans Kaashoek committed
//   KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM  (below extended memory)
//   KERNBASE+EXTMEM..KERNBASE+end : mapped to EXTMEM..end (mapped without write permission)
//   KERNBASE+end..KERBASE+PHYSTOP     : mapped to end..PHYSTOP (rw data + free memory)
//   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
Frans Kaashoek's avatar
Frans Kaashoek committed
// (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.
static struct kmap {
Frans Kaashoek's avatar
Frans Kaashoek committed
  void *virt;
  uint phys_start;
  uint phys_end;
  int perm;
} kmap[] = {
Frans Kaashoek's avatar
x  
Frans Kaashoek committed
  { P2V(0), 0, 1024*1024, PTE_W},  // First 1Mbyte contains BIOS and some IO devices
Frans Kaashoek's avatar
Frans Kaashoek committed
  { (void *)KERNLINK, V2P(KERNLINK), V2P(data),  0},  // kernel text, rodata
  { data, V2P(data), PHYSTOP,  PTE_W},  // kernel data, memory
Frans Kaashoek's avatar
x  
Frans Kaashoek committed
  { (void*)DEVSPACE, DEVSPACE, 0, PTE_W},  // more devices
Frans Kaashoek's avatar
Frans Kaashoek committed

// Set up kernel part of a page table.
pde_t*
Russ Cox's avatar
Russ Cox committed
  pde_t *pgdir;
  struct kmap *k;
Frans Kaashoek's avatar
Frans Kaashoek committed

  k = kmap;
  if (p2v(PHYSTOP) > (void *) DEVSPACE)
    panic("PHYSTOP too high");
  for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
Frans Kaashoek's avatar
Frans Kaashoek committed
    if(mappages(pgdir, k->virt, k->phys_end - k->phys_start, (uint)k->phys_start, 
		k->perm, alloc) < 0)
Frans Kaashoek's avatar
Frans Kaashoek committed

Frans Kaashoek's avatar
Frans Kaashoek committed
// Allocate one page table for the machine for the kernel address
// space for scheduler processes.
void
kvmalloc(void)
{
Frans Kaashoek's avatar
Frans Kaashoek committed
  kpgdir = setupkvm(enter_alloc);
Frans Kaashoek's avatar
Frans Kaashoek committed
  switchkvm();
}

// Switch h/w page table register to the kernel-only page table,
// for when no process is running.
switchkvm(void)
Frans Kaashoek's avatar
Frans Kaashoek committed
  lcr3(v2p(kpgdir));   // switch to the kernel page table
Frans Kaashoek's avatar
Frans Kaashoek committed
}

// Switch TSS and h/w page table to correspond to process p.
Frans Kaashoek's avatar
Frans Kaashoek committed
void
switchuvm(struct proc *p)
Frans Kaashoek's avatar
Frans Kaashoek committed
{
  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);
Austin Clements's avatar
Austin Clements committed
  if(p->pgdir == 0)
    panic("switchuvm: no pgdir");
Frans Kaashoek's avatar
Frans Kaashoek committed
  lcr3(v2p(p->pgdir));  // switch to new address space
Frans Kaashoek's avatar
Frans Kaashoek committed
  popcli();
}

// Load the initcode into address 0 of pgdir.
// sz must be less than a page.
void
inituvm(pde_t *pgdir, char *init, uint sz)
{
Russ Cox's avatar
Russ Cox committed
  char *mem;
  
  if(sz >= PGSIZE)
Russ Cox's avatar
Russ Cox committed
  mem = kalloc();
  mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U, kalloc);
// 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;

  if((uint)addr % PGSIZE != 0)
    panic("loaduvm: addr must be page aligned");
Frans Kaashoek's avatar
Frans Kaashoek committed
    if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
      panic("loaduvm: address should exist");
Russ Cox's avatar
Russ Cox committed
    if(sz - i < PGSIZE)
      n = sz - i;
    else
      n = PGSIZE;
    if(readi(ip, p2v(pa), offset+i, n) != n)
Russ Cox's avatar
Russ Cox committed
      return -1;
Russ Cox's avatar
Russ Cox committed
  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.
Frans Kaashoek's avatar
Frans Kaashoek committed
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
Frans Kaashoek's avatar
Frans Kaashoek committed
{
Russ Cox's avatar
Russ Cox committed
  char *mem;
  uint a;
Frans Kaashoek's avatar
Frans Kaashoek committed
    return 0;
Russ Cox's avatar
Russ Cox committed
  if(newsz < oldsz)
    return oldsz;

Russ Cox's avatar
Russ Cox committed
  a = PGROUNDUP(oldsz);
  for(; a < newsz; a += PGSIZE){
Russ Cox's avatar
Russ Cox committed
    mem = kalloc();
    if(mem == 0){
      cprintf("allocuvm out of memory\n");
      deallocuvm(pgdir, newsz, oldsz);
      return 0;
Frans Kaashoek's avatar
Frans Kaashoek committed
    }
    mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U, kalloc);
Frans Kaashoek's avatar
Frans Kaashoek committed
  }
Russ Cox's avatar
Russ Cox committed
  return newsz;
Frans Kaashoek's avatar
Frans Kaashoek committed
}

// 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.
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
Russ Cox's avatar
Russ Cox committed
  pte_t *pte;
Russ Cox's avatar
Russ Cox committed
  uint a, pa;
Russ Cox's avatar
Russ Cox committed

  if(newsz >= oldsz)
    return oldsz;

Russ Cox's avatar
Russ Cox committed
  a = PGROUNDUP(newsz);
  for(; a  < oldsz; a += PGSIZE){
Frans Kaashoek's avatar
Frans Kaashoek committed
    pte = walkpgdir(pgdir, (char*)a, 0);
    if(pte && (*pte & PTE_P) != 0){
Russ Cox's avatar
Russ Cox committed
      pa = PTE_ADDR(*pte);
Russ Cox's avatar
Russ Cox committed
  return newsz;
// Free a page table and all the physical memory pages
// in the user part.
Frans Kaashoek's avatar
Frans Kaashoek committed
void
freevm(pde_t *pgdir)
{
Frans Kaashoek's avatar
Frans Kaashoek committed

Russ Cox's avatar
Russ Cox committed
  if(pgdir == 0)
    panic("freevm: no pgdir");
Austin Clements's avatar
Austin Clements committed
  for(i = 0; i < NPDENTRIES; i++){
    if(pgdir[i] & PTE_P) {
      char * v = p2v(PTE_ADDR(pgdir[i]));
      kfree(v);
    }
Frans Kaashoek's avatar
Frans Kaashoek committed
  }
Russ Cox's avatar
Russ Cox committed
  kfree((char*)pgdir);
Frans Kaashoek's avatar
Frans Kaashoek committed
}

// Given a parent process's page table, create a copy
// of it for a child.
Frans Kaashoek's avatar
Frans Kaashoek committed
pde_t*
copyuvm(pde_t *pgdir, uint sz)
{
Russ Cox's avatar
Russ Cox committed
  pde_t *d;
Frans Kaashoek's avatar
Frans Kaashoek committed
  pte_t *pte;
  uint pa, i;
  char *mem;

Russ Cox's avatar
Russ Cox committed
    return 0;
Austin Clements's avatar
Austin Clements committed
  for(i = 0; i < sz; i += PGSIZE){
Frans Kaashoek's avatar
Frans Kaashoek committed
    if((pte = walkpgdir(pgdir, (void*)i, 0)) == 0)
      panic("copyuvm: pte should exist");
      panic("copyuvm: page not present");
Russ Cox's avatar
Russ Cox committed
    if((mem = kalloc()) == 0)
    memmove(mem, (char*)p2v(pa), PGSIZE);
    if(mappages(d, (void*)i, PGSIZE, v2p(mem), PTE_W|PTE_U, kalloc) < 0)
Frans Kaashoek's avatar
Frans Kaashoek committed
  }
  return d;

bad:
  freevm(d);
  return 0;
Frans Kaashoek's avatar
Frans Kaashoek committed
}

//PAGEBREAK!
// Map user virtual address to kernel address.
char*
uva2ka(pde_t *pgdir, char *uva)
{
  pte_t *pte;

Frans Kaashoek's avatar
Frans Kaashoek committed
  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)
Russ Cox's avatar
Russ Cox committed
  char *buf, *pa0;
  uint n, va0;
  
  buf = (char*)p;
Russ Cox's avatar
Russ Cox committed
    va0 = (uint)PGROUNDDOWN(va);
    pa0 = uva2ka(pgdir, (char*)va0);
Russ Cox's avatar
Russ Cox committed
      return -1;
    n = PGSIZE - (va - va0);
    if(n > len)
      n = len;
    memmove(pa0 + (va - va0), buf, n);
    len -= n;
    buf += n;
    va = va0 + PGSIZE;
  }
Russ Cox's avatar
Russ Cox committed
  return 0;