1 /* $NetBSD: vm_machdep.c,v 1.96 2008/11/19 18:36:01 ad Exp $ */ 2 3 /* 4 * Copyright (c) 1996 5 * The President and Fellows of Harvard College. All rights reserved. 6 * Copyright (c) 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This software was developed by the Computer Systems Engineering group 10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 11 * contributed to Berkeley. 12 * 13 * All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Lawrence Berkeley Laboratory. 17 * This product includes software developed by Harvard University. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. All advertising materials mentioning features or use of this software 28 * must display the following acknowledgement: 29 * This product includes software developed by Harvard University. 30 * This product includes software developed by the University of 31 * California, Berkeley and its contributors. 32 * 4. Neither the name of the University nor the names of its contributors 33 * may be used to endorse or promote products derived from this software 34 * without specific prior written permission. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * @(#)vm_machdep.c 8.2 (Berkeley) 9/23/93 49 */ 50 51 #include <sys/cdefs.h> 52 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.96 2008/11/19 18:36:01 ad Exp $"); 53 54 #include "opt_multiprocessor.h" 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/proc.h> 59 #include <sys/user.h> 60 #include <sys/core.h> 61 #include <sys/malloc.h> 62 #include <sys/buf.h> 63 #include <sys/exec.h> 64 #include <sys/vnode.h> 65 #include <sys/simplelock.h> 66 67 #include <uvm/uvm_extern.h> 68 69 #include <machine/cpu.h> 70 #include <machine/frame.h> 71 #include <machine/trap.h> 72 73 #include <sparc/sparc/cpuvar.h> 74 75 /* 76 * Map a user I/O request into kernel virtual address space. 77 * Note: the pages are already locked by uvm_vslock(), so we 78 * do not need to pass an access_type to pmap_enter(). 79 */ 80 void 81 vmapbuf(struct buf *bp, vsize_t len) 82 { 83 struct pmap *upmap, *kpmap; 84 vaddr_t uva; /* User VA (map from) */ 85 vaddr_t kva; /* Kernel VA (new to) */ 86 paddr_t pa; /* physical address */ 87 vsize_t off; 88 89 if ((bp->b_flags & B_PHYS) == 0) 90 panic("vmapbuf"); 91 92 /* 93 * XXX: It might be better to round/trunc to a 94 * segment boundary to avoid VAC problems! 95 */ 96 bp->b_saveaddr = bp->b_data; 97 uva = trunc_page((vaddr_t)bp->b_data); 98 off = (vaddr_t)bp->b_data - uva; 99 len = round_page(off + len); 100 kva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); 101 bp->b_data = (void *)(kva + off); 102 103 /* 104 * We have to flush any write-back cache on the 105 * user-space mappings so our new mappings will 106 * have the correct contents. 107 */ 108 if (CACHEINFO.c_vactype != VAC_NONE) 109 cache_flush((void *)uva, len); 110 111 upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map); 112 kpmap = vm_map_pmap(kernel_map); 113 do { 114 if (pmap_extract(upmap, uva, &pa) == false) 115 panic("vmapbuf: null page frame"); 116 /* Now map the page into kernel space. */ 117 pmap_enter(kpmap, kva, pa, 118 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED); 119 uva += PAGE_SIZE; 120 kva += PAGE_SIZE; 121 len -= PAGE_SIZE; 122 } while (len); 123 pmap_update(kpmap); 124 } 125 126 /* 127 * Unmap a previously-mapped user I/O request. 128 */ 129 void 130 vunmapbuf(struct buf *bp, vsize_t len) 131 { 132 vaddr_t kva; 133 vsize_t off; 134 135 if ((bp->b_flags & B_PHYS) == 0) 136 panic("vunmapbuf"); 137 138 kva = trunc_page((vaddr_t)bp->b_data); 139 off = (vaddr_t)bp->b_data - kva; 140 len = round_page(off + len); 141 pmap_remove(vm_map_pmap(kernel_map), kva, kva + len); 142 pmap_update(vm_map_pmap(kernel_map)); 143 uvm_km_free(kernel_map, kva, len, UVM_KMF_VAONLY); 144 bp->b_data = bp->b_saveaddr; 145 bp->b_saveaddr = NULL; 146 147 #if 0 /* XXX: The flush above is sufficient, right? */ 148 if (CACHEINFO.c_vactype != VAC_NONE) 149 cpuinfo.cache_flush(bp->b_data, len); 150 #endif 151 } 152 153 154 void 155 cpu_proc_fork(struct proc *p1, struct proc *p2) 156 { 157 158 p2->p_md.md_flags = p1->p_md.md_flags; 159 } 160 161 162 /* 163 * The offset of the topmost frame in the kernel stack. 164 */ 165 #define TOPFRAMEOFF (USPACE-sizeof(struct trapframe)-sizeof(struct frame)) 166 167 /* 168 * Finish a fork operation, with process l2 nearly set up. 169 * Copy and update the pcb and trap frame, making the child ready to run. 170 * 171 * Rig the child's kernel stack so that it will start out in 172 * lwp_trampoline() and call child_return() with l2 as an 173 * argument. This causes the newly-created child process to go 174 * directly to user level with an apparent return value of 0 from 175 * fork(), while the parent process returns normally. 176 * 177 * l1 is the process being forked; if l1 == &lwp0, we are creating 178 * a kernel thread, and the return path and argument are specified with 179 * `func' and `arg'. 180 * 181 * If an alternate user-level stack is requested (with non-zero values 182 * in both the stack and stacksize args), set up the user stack pointer 183 * accordingly. 184 */ 185 void 186 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, 187 void *stack, size_t stacksize, 188 void (*func)(void *), void *arg) 189 { 190 struct pcb *opcb = &l1->l_addr->u_pcb; 191 struct pcb *npcb = &l2->l_addr->u_pcb; 192 struct trapframe *tf2; 193 struct rwindow *rp; 194 195 /* 196 * Save all user registers to l1's stack or, in the case of 197 * user registers and invalid stack pointers, to opcb. 198 * We then copy the whole pcb to l2; when switch() selects l2 199 * to run, it will run at the `lwp_trampoline' stub, rather 200 * than returning at the copying code below. 201 * 202 * If process l1 has an FPU state, we must copy it. If it is 203 * the FPU user, we must save the FPU state first. 204 */ 205 206 if (l1 == curlwp) { 207 write_user_windows(); 208 opcb->pcb_psr = getpsr(); 209 } 210 211 bcopy((void *)opcb, (void *)npcb, sizeof(struct pcb)); 212 if (l1->l_md.md_fpstate != NULL) { 213 struct cpu_info *cpi; 214 int s; 215 216 l2->l_md.md_fpstate = malloc(sizeof(struct fpstate), 217 M_SUBPROC, M_WAITOK); 218 219 FPU_LOCK(s); 220 if ((cpi = l1->l_md.md_fpu) != NULL) { 221 if (cpi->fplwp != l1) 222 panic("FPU(%d): fplwp %p", 223 cpi->ci_cpuid, cpi->fplwp); 224 if (l1 == cpuinfo.fplwp) 225 savefpstate(l1->l_md.md_fpstate); 226 #if defined(MULTIPROCESSOR) 227 else 228 XCALL1(savefpstate, l1->l_md.md_fpstate, 229 1 << cpi->ci_cpuid); 230 #endif 231 } 232 bcopy(l1->l_md.md_fpstate, l2->l_md.md_fpstate, 233 sizeof(struct fpstate)); 234 FPU_UNLOCK(s); 235 } else 236 l2->l_md.md_fpstate = NULL; 237 238 l2->l_md.md_fpu = NULL; 239 240 /* 241 * Setup (kernel) stack frame that will by-pass the child 242 * out of the kernel. (The trap frame invariably resides at 243 * the tippity-top of the u. area.) 244 */ 245 tf2 = l2->l_md.md_tf = (struct trapframe *) 246 ((int)npcb + USPACE - sizeof(*tf2)); 247 248 /* Copy parent's trapframe */ 249 *tf2 = *(struct trapframe *)((int)opcb + USPACE - sizeof(*tf2)); 250 251 /* 252 * If specified, give the child a different stack. 253 */ 254 if (stack != NULL) 255 tf2->tf_out[6] = (u_int)stack + stacksize; 256 257 /* 258 * The fork system call always uses the old system call 259 * convention; clear carry and skip trap instruction as 260 * in syscall(). 261 * note: lwp_trampoline() sets a fresh psr when returning 262 * to user mode. 263 */ 264 /*tf2->tf_psr &= ~PSR_C; -* success */ 265 tf2->tf_pc = tf2->tf_npc; 266 tf2->tf_npc = tf2->tf_pc + 4; 267 268 /* Set return values in child mode */ 269 tf2->tf_out[0] = 0; 270 tf2->tf_out[1] = 1; 271 272 /* Construct kernel frame to return to in cpu_switch() */ 273 rp = (struct rwindow *)((u_int)npcb + TOPFRAMEOFF); 274 rp->rw_local[0] = (int)func; /* Function to call */ 275 rp->rw_local[1] = (int)arg; /* and its argument */ 276 rp->rw_local[2] = (int)l2; /* the new LWP */ 277 278 npcb->pcb_pc = (int)lwp_trampoline - 8; 279 npcb->pcb_sp = (int)rp; 280 npcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 281 npcb->pcb_wim = 1; /* Fence at window #1 */ 282 } 283 284 /* 285 * Cleanup FPU state. 286 */ 287 void 288 cpu_lwp_free(struct lwp *l, int proc) 289 { 290 struct fpstate *fs; 291 292 if ((fs = l->l_md.md_fpstate) != NULL) { 293 struct cpu_info *cpi; 294 int s; 295 296 FPU_LOCK(s); 297 if ((cpi = l->l_md.md_fpu) != NULL) { 298 if (cpi->fplwp != l) 299 panic("FPU(%d): fplwp %p", 300 cpi->ci_cpuid, cpi->fplwp); 301 if (l == cpuinfo.fplwp) 302 savefpstate(fs); 303 #if defined(MULTIPROCESSOR) 304 else 305 XCALL1(savefpstate, fs, 1 << cpi->ci_cpuid); 306 #endif 307 cpi->fplwp = NULL; 308 } 309 l->l_md.md_fpu = NULL; 310 FPU_UNLOCK(s); 311 } 312 } 313 314 void 315 cpu_lwp_free2(struct lwp *l) 316 { 317 struct fpstate *fs; 318 319 if ((fs = l->l_md.md_fpstate) != NULL) 320 free((void *)fs, M_SUBPROC); 321 } 322 323 void 324 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg) 325 { 326 struct pcb *pcb = &l->l_addr->u_pcb; 327 /*struct trapframe *tf = l->l_md.md_tf;*/ 328 struct rwindow *rp; 329 330 /* Construct kernel frame to return to in cpu_switch() */ 331 rp = (struct rwindow *)((u_int)pcb + TOPFRAMEOFF); 332 rp->rw_local[0] = (int)func; /* Function to call */ 333 rp->rw_local[1] = (int)arg; /* and its argument */ 334 rp->rw_local[2] = (int)l; /* new lwp */ 335 336 pcb->pcb_pc = (int)lwp_trampoline - 8; 337 pcb->pcb_sp = (int)rp; 338 pcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 339 pcb->pcb_wim = 1; /* Fence at window #1 */ 340 } 341