1 /* $NetBSD: vm_machdep.c,v 1.103 2011/01/14 02:06:32 rmind 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.103 2011/01/14 02:06:32 rmind 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/core.h> 60 #include <sys/malloc.h> 61 #include <sys/buf.h> 62 #include <sys/exec.h> 63 #include <sys/vnode.h> 64 #include <sys/simplelock.h> 65 66 #include <uvm/uvm_extern.h> 67 68 #include <machine/cpu.h> 69 #include <machine/frame.h> 70 #include <machine/pcb.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 = lwp_getpcb(l1); 191 struct pcb *npcb = lwp_getpcb(l2); 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 #ifdef DIAGNOSTIC 211 else if (l1 != &lwp0) /* XXX is this valid? */ 212 panic("cpu_lwp_fork: curlwp"); 213 #endif 214 215 memcpy((void *)npcb, (void *)opcb, sizeof(struct pcb)); 216 if (l1->l_md.md_fpstate != NULL) { 217 struct cpu_info *cpi; 218 int s; 219 220 l2->l_md.md_fpstate = malloc(sizeof(struct fpstate), 221 M_SUBPROC, M_WAITOK); 222 223 FPU_LOCK(s); 224 if ((cpi = l1->l_md.md_fpu) != NULL) { 225 if (cpi->fplwp != l1) 226 panic("FPU(%d): fplwp %p", 227 cpi->ci_cpuid, cpi->fplwp); 228 if (l1 == cpuinfo.fplwp) 229 savefpstate(l1->l_md.md_fpstate); 230 #if defined(MULTIPROCESSOR) 231 else 232 XCALL1(ipi_savefpstate, l1->l_md.md_fpstate, 233 1 << cpi->ci_cpuid); 234 #endif 235 } 236 memcpy(l2->l_md.md_fpstate, l1->l_md.md_fpstate, 237 sizeof(struct fpstate)); 238 FPU_UNLOCK(s); 239 } else 240 l2->l_md.md_fpstate = NULL; 241 242 l2->l_md.md_fpu = NULL; 243 244 /* 245 * Setup (kernel) stack frame that will by-pass the child 246 * out of the kernel. (The trap frame invariably resides at 247 * the tippity-top of the u. area.) 248 */ 249 tf2 = l2->l_md.md_tf = (struct trapframe *) 250 ((int)npcb + USPACE - sizeof(*tf2)); 251 252 /* Copy parent's trapframe */ 253 *tf2 = *(struct trapframe *)((int)opcb + USPACE - sizeof(*tf2)); 254 255 /* 256 * If specified, give the child a different stack. 257 */ 258 if (stack != NULL) 259 tf2->tf_out[6] = (u_int)stack + stacksize; 260 261 /* 262 * The fork system call always uses the old system call 263 * convention; clear carry and skip trap instruction as 264 * in syscall(). 265 * note: lwp_trampoline() sets a fresh psr when returning 266 * to user mode. 267 */ 268 /*tf2->tf_psr &= ~PSR_C; -* success */ 269 tf2->tf_pc = tf2->tf_npc; 270 tf2->tf_npc = tf2->tf_pc + 4; 271 272 /* Set return values in child mode */ 273 tf2->tf_out[0] = 0; 274 tf2->tf_out[1] = 1; 275 276 /* Construct kernel frame to return to in cpu_switch() */ 277 rp = (struct rwindow *)((u_int)npcb + TOPFRAMEOFF); 278 /**rp = *(struct rwindow *)((u_int)opcb + TOPFRAMEOFF);*/ 279 rp->rw_local[0] = (int)func; /* Function to call */ 280 rp->rw_local[1] = (int)arg; /* and its argument */ 281 rp->rw_local[2] = (int)l2; /* new LWP */ 282 283 npcb->pcb_pc = (int)lwp_trampoline - 8; 284 npcb->pcb_sp = (int)rp; 285 npcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 286 npcb->pcb_wim = 1; /* Fence at window #1 */ 287 } 288 289 /* 290 * Cleanup FPU state. 291 */ 292 void 293 cpu_lwp_free(struct lwp *l, int proc) 294 { 295 struct fpstate *fs; 296 297 if ((fs = l->l_md.md_fpstate) != NULL) { 298 struct cpu_info *cpi; 299 int s; 300 301 FPU_LOCK(s); 302 if ((cpi = l->l_md.md_fpu) != NULL) { 303 if (cpi->fplwp != l) 304 panic("FPU(%d): fplwp %p", 305 cpi->ci_cpuid, cpi->fplwp); 306 if (l == cpuinfo.fplwp) 307 savefpstate(fs); 308 #if defined(MULTIPROCESSOR) 309 else 310 XCALL1(ipi_savefpstate, fs, 1 << cpi->ci_cpuid); 311 #endif 312 cpi->fplwp = NULL; 313 } 314 l->l_md.md_fpu = NULL; 315 FPU_UNLOCK(s); 316 } 317 } 318 319 void 320 cpu_lwp_free2(struct lwp *l) 321 { 322 struct fpstate *fs; 323 324 if ((fs = l->l_md.md_fpstate) != NULL) 325 free((void *)fs, M_SUBPROC); 326 } 327 328 void 329 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg) 330 { 331 struct pcb *pcb = lwp_getpcb(l); 332 /*struct trapframe *tf = l->l_md.md_tf;*/ 333 struct rwindow *rp; 334 335 /* Construct kernel frame to return to in cpu_switch() */ 336 rp = (struct rwindow *)((u_int)pcb + TOPFRAMEOFF); 337 rp->rw_local[0] = (int)func; /* Function to call */ 338 rp->rw_local[1] = (int)arg; /* and its argument */ 339 340 pcb->pcb_pc = (int)lwp_setfunc_trampoline - 8; 341 pcb->pcb_sp = (int)rp; 342 pcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 343 pcb->pcb_wim = 1; /* Fence at window #1 */ 344 } 345