1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1992 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 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 7.5 (Berkeley) 03/15/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 "vm/vm.h" 26 #include "vm/vm_kern.h" 27 #include "vm/vm_page.h" 28 29 #include "../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 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 77 /* 78 * Arrange for a non-local goto when the new process 79 * is started, to resume here, returning nonzero from setjmp. 80 */ 81 #ifdef DIAGNOSTIC 82 if (p1 != curproc) 83 panic("cpu_fork: curproc"); 84 #endif 85 if (copykstack(up)) { 86 /* 87 * Return 1 in child. 88 */ 89 return (1); 90 } 91 return (0); 92 } 93 94 /* 95 * cpu_exit is called as the last action during exit. 96 * We release the address space and machine-dependent resources, 97 * including the memory for the user structure and kernel stack. 98 * Once finished, we call swtch_exit, which switches to a temporary 99 * pcb and stack and never returns. We block memory allocation 100 * until swtch_exit has made things safe again. 101 */ 102 cpu_exit(p) 103 struct proc *p; 104 { 105 extern struct proc *machFPCurProcPtr; 106 107 if (machFPCurProcPtr == p) 108 machFPCurProcPtr = (struct proc *)0; 109 110 vmspace_free(p->p_vmspace); 111 112 (void) splhigh(); 113 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 114 swtch_exit(); 115 /* NOTREACHED */ 116 } 117 118 /* 119 * Dump the machine specific header information at the start of a core dump. 120 */ 121 cpu_coredump(p, vp, cred) 122 struct proc *p; 123 struct vnode *vp; 124 struct ucred *cred; 125 { 126 extern struct proc *machFPCurProcPtr; 127 128 /* 129 * Copy floating point state from the FP chip if this process 130 * has state stored there. 131 */ 132 if (p == machFPCurProcPtr) 133 MachSaveCurFPState(p); 134 135 return (vn_rdwr(UIO_WRITE, vp, (caddr_t)p->p_addr, ctob(UPAGES), 136 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 137 p)); 138 } 139 140 /* 141 * Move pages from one kernel virtual address to another. 142 * Both addresses are assumed to reside in the Sysmap, 143 * and size must be a multiple of CLSIZE. 144 */ 145 pagemove(from, to, size) 146 register caddr_t from, to; 147 int size; 148 { 149 register pt_entry_t *fpte, *tpte; 150 151 if (size % CLBYTES) 152 panic("pagemove"); 153 fpte = kvtopte(from); 154 tpte = kvtopte(to); 155 while (size > 0) { 156 MachTLBFlushAddr(from); 157 MachTLBUpdate(to, *fpte); 158 *tpte++ = *fpte; 159 fpte->pt_entry = 0; 160 fpte++; 161 size -= NBPG; 162 from += NBPG; 163 to += NBPG; 164 } 165 } 166 167 extern vm_map_t phys_map; 168 169 /* 170 * Map an IO request into kernel virtual address space. Requests fall into 171 * one of five catagories: 172 * 173 * B_PHYS|B_UAREA: User u-area swap. 174 * Address is relative to start of u-area (p_addr). 175 * B_PHYS|B_PAGET: User page table swap. 176 * Address is a kernel VA in usrpt (Usrptmap). 177 * B_PHYS|B_DIRTY: Dirty page push. 178 * Address is a VA in proc2's address space. 179 * B_PHYS|B_PGIN: Kernel pagein of user pages. 180 * Address is VA in user's address space. 181 * B_PHYS: User "raw" IO request. 182 * Address is VA in user's address space. 183 * 184 * All requests are (re)mapped into kernel VA space via the phys_map 185 */ 186 vmapbuf(bp) 187 register struct buf *bp; 188 { 189 register caddr_t addr; 190 register vm_size_t sz; 191 struct proc *p; 192 int off; 193 vm_offset_t kva; 194 register vm_offset_t pa; 195 196 if ((bp->b_flags & B_PHYS) == 0) 197 panic("vmapbuf"); 198 addr = bp->b_saveaddr = bp->b_un.b_addr; 199 off = (int)addr & PGOFSET; 200 p = bp->b_proc; 201 sz = round_page(bp->b_bcount + off); 202 kva = kmem_alloc_wait(phys_map, sz); 203 bp->b_un.b_addr = (caddr_t) (kva + off); 204 sz = atop(sz); 205 while (sz--) { 206 pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), 207 (vm_offset_t)addr); 208 if (pa == 0) 209 panic("vmapbuf: null page frame"); 210 pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa), 211 VM_PROT_READ|VM_PROT_WRITE, TRUE); 212 addr += PAGE_SIZE; 213 kva += PAGE_SIZE; 214 } 215 } 216 217 /* 218 * Free the io map PTEs associated with this IO operation. 219 * We also invalidate the TLB entries and restore the original b_addr. 220 */ 221 vunmapbuf(bp) 222 register struct buf *bp; 223 { 224 register caddr_t addr = bp->b_un.b_addr; 225 register vm_size_t sz; 226 vm_offset_t kva; 227 228 if ((bp->b_flags & B_PHYS) == 0) 229 panic("vunmapbuf"); 230 sz = round_page(bp->b_bcount + ((int)addr & PGOFSET)); 231 kva = (vm_offset_t)((int)addr & ~PGOFSET); 232 kmem_free_wakeup(phys_map, kva, sz); 233 bp->b_un.b_addr = bp->b_saveaddr; 234 bp->b_saveaddr = NULL; 235 } 236