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.1 (Berkeley) 01/07/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 "user.h" 23 24 #include "vm/vm.h" 25 #include "vm/vm_kern.h" 26 #include "vm/vm_page.h" 27 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 register pt_entry_t *pte; 44 register int i; 45 46 p2->p_regs = up->u_pcb.pcb_regs; 47 p2->p_md.md_flags = p1->p_md.md_flags & (MDP_FPUSED | MDP_ULTRIX); 48 49 /* 50 * Convert the user struct virtual address to a physical one 51 * and cache it in the proc struct. Note: if the phyical address 52 * can change (due to memory compaction in kmem_alloc?), 53 * we will have to update things. 54 */ 55 pte = kvtopte(up); 56 for (i = 0; i < UPAGES; i++) { 57 p2->p_md.md_upte[i] = pte->pt_entry & ~PG_G; 58 pte++; 59 } 60 61 /* 62 * Copy pcb and stack from proc p1 to p2. 63 * We do this as cheaply as possible, copying only the active 64 * part of the stack. The stack and pcb need to agree; 65 */ 66 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 67 68 /* 69 * Arrange for a non-local goto when the new process 70 * is started, to resume here, returning nonzero from setjmp. 71 */ 72 #ifdef DIAGNOSTIC 73 if (p1 != curproc) 74 panic("cpu_fork: curproc"); 75 #endif 76 if (copykstack(up)) { 77 /* 78 * Return 1 in child. 79 */ 80 return (1); 81 } 82 return (0); 83 } 84 85 /* 86 * cpu_exit is called as the last action during exit. 87 * We release the address space and machine-dependent resources, 88 * including the memory for the user structure and kernel stack. 89 * Once finished, we call swtch_exit, which switches to a temporary 90 * pcb and stack and never returns. We block memory allocation 91 * until swtch_exit has made things safe again. 92 */ 93 cpu_exit(p) 94 struct proc *p; 95 { 96 extern struct proc *machFPCurProcPtr; 97 98 if (machFPCurProcPtr == p) 99 machFPCurProcPtr = (struct proc *)0; 100 101 vmspace_free(p->p_vmspace); 102 103 (void) splhigh(); 104 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 105 swtch_exit(); 106 /* NOTREACHED */ 107 } 108 109 /* 110 * Move pages from one kernel virtual address to another. 111 * Both addresses are assumed to reside in the Sysmap, 112 * and size must be a multiple of CLSIZE. 113 */ 114 pagemove(from, to, size) 115 register caddr_t from, to; 116 int size; 117 { 118 register pt_entry_t *fpte, *tpte; 119 120 if (size % CLBYTES) 121 panic("pagemove"); 122 fpte = kvtopte(from); 123 tpte = kvtopte(to); 124 while (size > 0) { 125 MachTLBFlushAddr(from); 126 MachTLBUpdate(to, *fpte); 127 *tpte++ = *fpte; 128 fpte->pt_entry = 0; 129 fpte++; 130 size -= NBPG; 131 from += NBPG; 132 to += NBPG; 133 } 134 } 135 136 extern vm_map_t phys_map; 137 138 /* 139 * Map an IO request into kernel virtual address space. Requests fall into 140 * one of five catagories: 141 * 142 * B_PHYS|B_UAREA: User u-area swap. 143 * Address is relative to start of u-area (p_addr). 144 * B_PHYS|B_PAGET: User page table swap. 145 * Address is a kernel VA in usrpt (Usrptmap). 146 * B_PHYS|B_DIRTY: Dirty page push. 147 * Address is a VA in proc2's address space. 148 * B_PHYS|B_PGIN: Kernel pagein of user pages. 149 * Address is VA in user's address space. 150 * B_PHYS: User "raw" IO request. 151 * Address is VA in user's address space. 152 * 153 * All requests are (re)mapped into kernel VA space via the phys_map 154 */ 155 vmapbuf(bp) 156 register struct buf *bp; 157 { 158 register caddr_t addr; 159 register vm_size_t sz; 160 struct proc *p; 161 int off; 162 vm_offset_t kva; 163 register vm_offset_t pa; 164 165 if ((bp->b_flags & B_PHYS) == 0) 166 panic("vmapbuf"); 167 addr = bp->b_saveaddr = bp->b_un.b_addr; 168 off = (int)addr & PGOFSET; 169 p = bp->b_proc; 170 sz = round_page(bp->b_bcount + off); 171 kva = kmem_alloc_wait(phys_map, sz); 172 bp->b_un.b_addr = (caddr_t) (kva + off); 173 sz = atop(sz); 174 while (sz--) { 175 pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), 176 (vm_offset_t)addr); 177 if (pa == 0) 178 panic("vmapbuf: null page frame"); 179 pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa), 180 VM_PROT_READ|VM_PROT_WRITE, TRUE); 181 addr += PAGE_SIZE; 182 kva += PAGE_SIZE; 183 } 184 } 185 186 /* 187 * Free the io map PTEs associated with this IO operation. 188 * We also invalidate the TLB entries and restore the original b_addr. 189 */ 190 vunmapbuf(bp) 191 register struct buf *bp; 192 { 193 register caddr_t addr = bp->b_un.b_addr; 194 register vm_size_t sz; 195 vm_offset_t kva; 196 197 if ((bp->b_flags & B_PHYS) == 0) 198 panic("vunmapbuf"); 199 sz = round_page(bp->b_bcount + ((int)addr & PGOFSET)); 200 kva = (vm_offset_t)((int)addr & ~PGOFSET); 201 kmem_free_wakeup(phys_map, kva, sz); 202 bp->b_un.b_addr = bp->b_saveaddr; 203 bp->b_saveaddr = NULL; 204 } 205