1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$ 13 * 14 * @(#)vm_machdep.c 7.10 (Berkeley) 05/07/91 15 */ 16 17 #include "param.h" 18 #include "systm.h" 19 #include "proc.h" 20 #include "malloc.h" 21 #include "buf.h" 22 #include "user.h" 23 24 #include "../include/cpu.h" 25 26 #include "vm/vm.h" 27 #include "vm/vm_kern.h" 28 #include "pte.h" 29 30 /* 31 * Finish a fork operation, with process p2 nearly set up. 32 * Copy and update the kernel stack and pcb, making the child 33 * ready to run, and marking it so that it can return differently 34 * than the parent. Returns 1 in the child process, 0 in the parent. 35 * We currently double-map the user area so that the stack is at the same 36 * address in each process; in the future we will probably relocate 37 * the frame pointers on the stack after copying. 38 */ 39 cpu_fork(p1, p2) 40 register struct proc *p1, *p2; 41 { 42 register struct user *up = p2->p_addr; 43 int offset; 44 extern caddr_t getsp(); 45 extern char kstack[]; 46 47 /* 48 * Copy pcb and stack from proc p1 to p2. 49 * We do this as cheaply as possible, copying only the active 50 * part of the stack. The stack and pcb need to agree; 51 * this is tricky, as the final pcb is constructed by savectx, 52 * but its frame isn't yet on the stack when the stack is copied. 53 * swtch compensates for this when the child eventually runs. 54 * This should be done differently, with a single call 55 * that copies and updates the pcb+stack, 56 * replacing the bcopy and savectx. 57 */ 58 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 59 offset = getsp() - kstack; 60 bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset, 61 (unsigned) ctob(UPAGES) - offset); 62 63 PMAP_ACTIVATE(&p2->p_vmspace->vm_pmap, &up->u_pcb, 0); 64 65 /* 66 * Arrange for a non-local goto when the new process 67 * is started, to resume here, returning nonzero from setjmp. 68 */ 69 if (savectx(up, 1)) { 70 /* 71 * Return 1 in child. 72 */ 73 return (1); 74 } 75 return (0); 76 } 77 78 /* 79 * cpu_exit is called as the last action during exit. 80 * We release the address space and machine-dependent resources, 81 * including the memory for the user structure and kernel stack. 82 * Once finished, we call swtch_exit, which switches to a temporary 83 * pcb and stack and never returns. We block memory allocation 84 * until swtch_exit has made things safe again. 85 */ 86 cpu_exit(p) 87 struct proc *p; 88 { 89 90 vmspace_free(p->p_vmspace); 91 92 (void) splimp(); 93 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 94 swtch_exit(); 95 /* NOTREACHED */ 96 } 97 98 /* 99 * Move pages from one kernel virtual address to another. 100 * Both addresses are assumed to reside in the Sysmap, 101 * and size must be a multiple of CLSIZE. 102 */ 103 pagemove(from, to, size) 104 register caddr_t from, to; 105 int size; 106 { 107 register struct pte *fpte, *tpte; 108 109 if (size % CLBYTES) 110 panic("pagemove"); 111 fpte = kvtopte(from); 112 tpte = kvtopte(to); 113 while (size > 0) { 114 *tpte++ = *fpte; 115 *(int *)fpte++ = PG_NV; 116 TBIS(from); 117 TBIS(to); 118 from += NBPG; 119 to += NBPG; 120 size -= NBPG; 121 } 122 DCIS(); 123 } 124 125 /* 126 * Map `size' bytes of physical memory starting at `paddr' into 127 * kernel VA space at `vaddr'. Read/write and cache-inhibit status 128 * are specified by `prot'. 129 */ 130 physaccess(vaddr, paddr, size, prot) 131 caddr_t vaddr, paddr; 132 register int size, prot; 133 { 134 register struct pte *pte; 135 register u_int page; 136 137 pte = kvtopte(vaddr); 138 page = (u_int)paddr & PG_FRAME; 139 for (size = btoc(size); size; size--) { 140 *(int *)pte++ = PG_V | prot | page; 141 page += NBPG; 142 } 143 TBIAS(); 144 } 145 146 physunaccess(vaddr, size) 147 caddr_t vaddr; 148 register int size; 149 { 150 register struct pte *pte; 151 152 pte = kvtopte(vaddr); 153 for (size = btoc(size); size; size--) 154 *(int *)pte++ = PG_NV; 155 TBIAS(); 156 } 157 158 /* 159 * Set a red zone in the kernel stack after the u. area. 160 * We don't support a redzone right now. It really isn't clear 161 * that it is a good idea since, if the kernel stack were to roll 162 * into a write protected page, the processor would lock up (since 163 * it cannot create an exception frame) and we would get no useful 164 * post-mortem info. Currently, under the DEBUG option, we just 165 * check at every clock interrupt to see if the current k-stack has 166 * gone too far (i.e. into the "redzone" page) and if so, panic. 167 * Look at _lev6intr in locore.s for more details. 168 */ 169 /*ARGSUSED*/ 170 setredzone(pte, vaddr) 171 struct pte *pte; 172 caddr_t vaddr; 173 { 174 } 175 176 /* 177 * Convert kernel VA to physical address 178 */ 179 kvtop(addr) 180 register caddr_t addr; 181 { 182 vm_offset_t va; 183 184 va = pmap_extract(kernel_pmap, (vm_offset_t)addr); 185 if (va == 0) 186 panic("kvtop: zero page frame"); 187 return((int)va); 188 } 189 190 extern vm_map_t phys_map; 191 192 /* 193 * Map an IO request into kernel virtual address space. Requests fall into 194 * one of five catagories: 195 * 196 * B_PHYS|B_UAREA: User u-area swap. 197 * Address is relative to start of u-area (p_addr). 198 * B_PHYS|B_PAGET: User page table swap. 199 * Address is a kernel VA in usrpt (Usrptmap). 200 * B_PHYS|B_DIRTY: Dirty page push. 201 * Address is a VA in proc2's address space. 202 * B_PHYS|B_PGIN: Kernel pagein of user pages. 203 * Address is VA in user's address space. 204 * B_PHYS: User "raw" IO request. 205 * Address is VA in user's address space. 206 * 207 * All requests are (re)mapped into kernel VA space via the useriomap 208 * (a name with only slightly more meaning than "kernelmap") 209 */ 210 vmapbuf(bp) 211 register struct buf *bp; 212 { 213 register int npf; 214 register caddr_t addr; 215 register long flags = bp->b_flags; 216 struct proc *p; 217 int off; 218 vm_offset_t kva; 219 register vm_offset_t pa; 220 221 if ((flags & B_PHYS) == 0) 222 panic("vmapbuf"); 223 addr = bp->b_saveaddr = bp->b_un.b_addr; 224 off = (int)addr & PGOFSET; 225 p = bp->b_proc; 226 npf = btoc(round_page(bp->b_bcount + off)); 227 kva = kmem_alloc_wait(phys_map, ctob(npf)); 228 bp->b_un.b_addr = (caddr_t) (kva + off); 229 while (npf--) { 230 pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), 231 (vm_offset_t)addr); 232 if (pa == 0) 233 panic("vmapbuf: null page frame"); 234 pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa), 235 VM_PROT_READ|VM_PROT_WRITE, TRUE); 236 addr += PAGE_SIZE; 237 kva += PAGE_SIZE; 238 } 239 } 240 241 /* 242 * Free the io map PTEs associated with this IO operation. 243 * We also invalidate the TLB entries and restore the original b_addr. 244 */ 245 vunmapbuf(bp) 246 register struct buf *bp; 247 { 248 register int npf; 249 register caddr_t addr = bp->b_un.b_addr; 250 vm_offset_t kva; 251 252 if ((bp->b_flags & B_PHYS) == 0) 253 panic("vunmapbuf"); 254 npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET))); 255 kva = (vm_offset_t)((int)addr & ~PGOFSET); 256 kmem_free_wakeup(phys_map, kva, ctob(npf)); 257 bp->b_un.b_addr = bp->b_saveaddr; 258 bp->b_saveaddr = NULL; 259 } 260