1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. 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 and Ralph Campbell. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$ 13 * 14 * @(#)vm_machdep.c 8.1 (Berkeley) 06/10/93 15 */ 16 17 #include <sys/param.h> 18 #include <sys/systm.h> 19 #include <sys/proc.h> 20 #include <sys/malloc.h> 21 #include <sys/buf.h> 22 #include <sys/vnode.h> 23 #include <sys/user.h> 24 25 #include <vm/vm.h> 26 #include <vm/vm_kern.h> 27 #include <vm/vm_page.h> 28 29 #include <machine/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 register pt_entry_t *pte; 45 register int i; 46 extern struct proc *machFPCurProcPtr; 47 48 p2->p_md.md_regs = up->u_pcb.pcb_regs; 49 p2->p_md.md_flags = p1->p_md.md_flags & (MDP_FPUSED | MDP_ULTRIX); 50 51 /* 52 * Convert the user struct virtual address to a physical one 53 * and cache it in the proc struct. Note: if the phyical address 54 * can change (due to memory compaction in kmem_alloc?), 55 * we will have to update things. 56 */ 57 pte = kvtopte(up); 58 for (i = 0; i < UPAGES; i++) { 59 p2->p_md.md_upte[i] = pte->pt_entry & ~PG_G; 60 pte++; 61 } 62 63 /* 64 * Copy floating point state from the FP chip if this process 65 * has state stored there. 66 */ 67 if (p1 == machFPCurProcPtr) 68 MachSaveCurFPState(p1); 69 70 /* 71 * Copy pcb and stack from proc p1 to p2. 72 * We do this as cheaply as possible, copying only the active 73 * part of the stack. The stack and pcb need to agree; 74 */ 75 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 76 /* cache segtab for ULTBMiss() */ 77 p2->p_addr->u_pcb.pcb_segtab = (void *)p2->p_vmspace->vm_pmap.pm_segtab; 78 79 /* 80 * Arrange for a non-local goto when the new process 81 * is started, to resume here, returning nonzero from setjmp. 82 */ 83 #ifdef DIAGNOSTIC 84 if (p1 != curproc) 85 panic("cpu_fork: curproc"); 86 #endif 87 if (copykstack(up)) { 88 /* 89 * Return 1 in child. 90 */ 91 return (1); 92 } 93 return (0); 94 } 95 96 /* 97 * cpu_exit is called as the last action during exit. 98 * We release the address space and machine-dependent resources, 99 * including the memory for the user structure and kernel stack. 100 * Once finished, we call swtch_exit, which switches to a temporary 101 * pcb and stack and never returns. We block memory allocation 102 * until swtch_exit has made things safe again. 103 */ 104 cpu_exit(p) 105 struct proc *p; 106 { 107 extern struct proc *machFPCurProcPtr; 108 109 if (machFPCurProcPtr == p) 110 machFPCurProcPtr = (struct proc *)0; 111 112 vmspace_free(p->p_vmspace); 113 114 (void) splhigh(); 115 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 116 swtch_exit(); 117 /* NOTREACHED */ 118 } 119 120 /* 121 * Dump the machine specific header information at the start of a core dump. 122 */ 123 cpu_coredump(p, vp, cred) 124 struct proc *p; 125 struct vnode *vp; 126 struct ucred *cred; 127 { 128 extern struct proc *machFPCurProcPtr; 129 130 /* 131 * Copy floating point state from the FP chip if this process 132 * has state stored there. 133 */ 134 if (p == machFPCurProcPtr) 135 MachSaveCurFPState(p); 136 137 return (vn_rdwr(UIO_WRITE, vp, (caddr_t)p->p_addr, ctob(UPAGES), 138 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 139 p)); 140 } 141 142 /* 143 * Move pages from one kernel virtual address to another. 144 * Both addresses are assumed to reside in the Sysmap, 145 * and size must be a multiple of CLSIZE. 146 */ 147 pagemove(from, to, size) 148 register caddr_t from, to; 149 int size; 150 { 151 register pt_entry_t *fpte, *tpte; 152 153 if (size % CLBYTES) 154 panic("pagemove"); 155 fpte = kvtopte(from); 156 tpte = kvtopte(to); 157 while (size > 0) { 158 MachTLBFlushAddr(from); 159 MachTLBUpdate(to, *fpte); 160 *tpte++ = *fpte; 161 fpte->pt_entry = 0; 162 fpte++; 163 size -= NBPG; 164 from += NBPG; 165 to += NBPG; 166 } 167 } 168 169 extern vm_map_t phys_map; 170 171 /* 172 * Map an IO request into kernel virtual address space. Requests fall into 173 * one of five catagories: 174 * 175 * B_PHYS|B_UAREA: User u-area swap. 176 * Address is relative to start of u-area (p_addr). 177 * B_PHYS|B_PAGET: User page table swap. 178 * Address is a kernel VA in usrpt (Usrptmap). 179 * B_PHYS|B_DIRTY: Dirty page push. 180 * Address is a VA in proc2's address space. 181 * B_PHYS|B_PGIN: Kernel pagein of user pages. 182 * Address is VA in user's address space. 183 * B_PHYS: User "raw" IO request. 184 * Address is VA in user's address space. 185 * 186 * All requests are (re)mapped into kernel VA space via the phys_map 187 */ 188 vmapbuf(bp) 189 register struct buf *bp; 190 { 191 register caddr_t addr; 192 register vm_size_t sz; 193 struct proc *p; 194 int off; 195 vm_offset_t kva; 196 register vm_offset_t pa; 197 198 if ((bp->b_flags & B_PHYS) == 0) 199 panic("vmapbuf"); 200 addr = bp->b_saveaddr = bp->b_un.b_addr; 201 off = (int)addr & PGOFSET; 202 p = bp->b_proc; 203 sz = round_page(bp->b_bcount + off); 204 kva = kmem_alloc_wait(phys_map, sz); 205 bp->b_un.b_addr = (caddr_t) (kva + off); 206 sz = atop(sz); 207 while (sz--) { 208 pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), 209 (vm_offset_t)addr); 210 if (pa == 0) 211 panic("vmapbuf: null page frame"); 212 pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa), 213 VM_PROT_READ|VM_PROT_WRITE, TRUE); 214 addr += PAGE_SIZE; 215 kva += PAGE_SIZE; 216 } 217 } 218 219 /* 220 * Free the io map PTEs associated with this IO operation. 221 * We also invalidate the TLB entries and restore the original b_addr. 222 */ 223 vunmapbuf(bp) 224 register struct buf *bp; 225 { 226 register caddr_t addr = bp->b_un.b_addr; 227 register vm_size_t sz; 228 vm_offset_t kva; 229 230 if ((bp->b_flags & B_PHYS) == 0) 231 panic("vunmapbuf"); 232 sz = round_page(bp->b_bcount + ((int)addr & PGOFSET)); 233 kva = (vm_offset_t)((int)addr & ~PGOFSET); 234 kmem_free_wakeup(phys_map, kva, sz); 235 bp->b_un.b_addr = bp->b_saveaddr; 236 bp->b_saveaddr = NULL; 237 } 238