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