1 /* $NetBSD: vm_machdep.c,v 1.81 2004/09/17 14:11:22 skrll 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.81 2004/09/17 14:11:22 skrll 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 66 #include <uvm/uvm_extern.h> 67 68 #include <machine/cpu.h> 69 #include <machine/frame.h> 70 #include <machine/trap.h> 71 72 #include <sparc/sparc/cpuvar.h> 73 74 /* 75 * Map a user I/O request into kernel virtual address space. 76 * Note: the pages are already locked by uvm_vslock(), so we 77 * do not need to pass an access_type to pmap_enter(). 78 */ 79 void 80 vmapbuf(bp, len) 81 struct buf *bp; 82 vsize_t len; 83 { 84 struct pmap *upmap, *kpmap; 85 vaddr_t uva; /* User VA (map from) */ 86 vaddr_t kva; /* Kernel VA (new to) */ 87 paddr_t pa; /* physical address */ 88 vsize_t off; 89 90 if ((bp->b_flags & B_PHYS) == 0) 91 panic("vmapbuf"); 92 93 /* 94 * XXX: It might be better to round/trunc to a 95 * segment boundary to avoid VAC problems! 96 */ 97 bp->b_saveaddr = bp->b_data; 98 uva = trunc_page((vaddr_t)bp->b_data); 99 off = (vaddr_t)bp->b_data - uva; 100 len = round_page(off + len); 101 kva = uvm_km_valloc_wait(kernel_map, len); 102 bp->b_data = (caddr_t)(kva + off); 103 104 /* 105 * We have to flush any write-back cache on the 106 * user-space mappings so our new mappings will 107 * have the correct contents. 108 */ 109 if (CACHEINFO.c_vactype != VAC_NONE) 110 cache_flush((caddr_t)uva, len); 111 112 upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map); 113 kpmap = vm_map_pmap(kernel_map); 114 do { 115 if (pmap_extract(upmap, uva, &pa) == FALSE) 116 panic("vmapbuf: null page frame"); 117 /* Now map the page into kernel space. */ 118 pmap_enter(kpmap, kva, pa, 119 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED); 120 uva += PAGE_SIZE; 121 kva += PAGE_SIZE; 122 len -= PAGE_SIZE; 123 } while (len); 124 pmap_update(kpmap); 125 } 126 127 /* 128 * Unmap a previously-mapped user I/O request. 129 */ 130 void 131 vunmapbuf(bp, len) 132 struct buf *bp; 133 vsize_t len; 134 { 135 vaddr_t kva; 136 vsize_t off; 137 138 if ((bp->b_flags & B_PHYS) == 0) 139 panic("vunmapbuf"); 140 141 kva = trunc_page((vaddr_t)bp->b_data); 142 off = (vaddr_t)bp->b_data - kva; 143 len = round_page(off + len); 144 pmap_remove(vm_map_pmap(kernel_map), kva, kva + len); 145 pmap_update(vm_map_pmap(kernel_map)); 146 uvm_km_free_wakeup(kernel_map, kva, len); 147 bp->b_data = bp->b_saveaddr; 148 bp->b_saveaddr = NULL; 149 150 #if 0 /* XXX: The flush above is sufficient, right? */ 151 if (CACHEINFO.c_vactype != VAC_NONE) 152 cpuinfo.cache_flush(bp->b_data, len); 153 #endif 154 } 155 156 157 /* 158 * The offset of the topmost frame in the kernel stack. 159 */ 160 #define TOPFRAMEOFF (USPACE-sizeof(struct trapframe)-sizeof(struct frame)) 161 162 /* 163 * Finish a fork operation, with process l2 nearly set up. 164 * Copy and update the pcb and trap frame, making the child ready to run. 165 * 166 * Rig the child's kernel stack so that it will start out in 167 * proc_trampoline() and call child_return() with l2 as an 168 * argument. This causes the newly-created child process to go 169 * directly to user level with an apparent return value of 0 from 170 * fork(), while the parent process returns normally. 171 * 172 * l1 is the process being forked; if l1 == &lwp0, we are creating 173 * a kernel thread, and the return path and argument are specified with 174 * `func' and `arg'. 175 * 176 * If an alternate user-level stack is requested (with non-zero values 177 * in both the stack and stacksize args), set up the user stack pointer 178 * accordingly. 179 */ 180 void 181 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg) 182 struct lwp *l1, *l2; 183 void *stack; 184 size_t stacksize; 185 void (*func) __P((void *)); 186 void *arg; 187 { 188 struct pcb *opcb = &l1->l_addr->u_pcb; 189 struct pcb *npcb = &l2->l_addr->u_pcb; 190 struct trapframe *tf2; 191 struct rwindow *rp; 192 193 /* 194 * Save all user registers to l1's stack or, in the case of 195 * user registers and invalid stack pointers, to opcb. 196 * We then copy the whole pcb to p2; when switch() selects p2 197 * to run, it will run at the `proc_trampoline' stub, rather 198 * than returning at the copying code below. 199 * 200 * If process l1 has an FPU state, we must copy it. If it is 201 * the FPU user, we must save the FPU state first. 202 */ 203 204 if (l1 == curlwp) { 205 write_user_windows(); 206 opcb->pcb_psr = getpsr(); 207 } 208 #ifdef DIAGNOSTIC 209 else if (l1 != &lwp0) 210 panic("cpu_lwp_fork: curlwp"); 211 #endif 212 213 bcopy((caddr_t)opcb, (caddr_t)npcb, sizeof(struct pcb)); 214 if (l1->l_md.md_fpstate != NULL) { 215 struct cpu_info *cpi; 216 int s; 217 218 l2->l_md.md_fpstate = malloc(sizeof(struct fpstate), 219 M_SUBPROC, M_WAITOK); 220 221 FPU_LOCK(s); 222 if ((cpi = l1->l_md.md_fpu) != NULL) { 223 if (cpi->fplwp != l1) 224 panic("FPU(%d): fplwp %p", 225 cpi->ci_cpuid, cpi->fplwp); 226 if (l1 == cpuinfo.fplwp) 227 savefpstate(l1->l_md.md_fpstate); 228 #if defined(MULTIPROCESSOR) 229 else 230 XCALL1(savefpstate, l1->l_md.md_fpstate, 231 1 << cpi->ci_cpuid); 232 #endif 233 } 234 bcopy(l1->l_md.md_fpstate, l2->l_md.md_fpstate, 235 sizeof(struct fpstate)); 236 FPU_UNLOCK(s); 237 } else 238 l2->l_md.md_fpstate = NULL; 239 240 l2->l_md.md_fpu = NULL; 241 242 /* 243 * Setup (kernel) stack frame that will by-pass the child 244 * out of the kernel. (The trap frame invariably resides at 245 * the tippity-top of the u. area.) 246 */ 247 tf2 = l2->l_md.md_tf = (struct trapframe *) 248 ((int)npcb + USPACE - sizeof(*tf2)); 249 250 /* Copy parent's trapframe */ 251 *tf2 = *(struct trapframe *)((int)opcb + USPACE - sizeof(*tf2)); 252 253 /* 254 * If specified, give the child a different stack. 255 */ 256 if (stack != NULL) 257 tf2->tf_out[6] = (u_int)stack + stacksize; 258 259 /* 260 * The fork system call always uses the old system call 261 * convention; clear carry and skip trap instruction as 262 * in syscall(). 263 * note: proc_trampoline() sets a fresh psr when returning 264 * to user mode. 265 */ 266 /*tf2->tf_psr &= ~PSR_C; -* success */ 267 tf2->tf_pc = tf2->tf_npc; 268 tf2->tf_npc = tf2->tf_pc + 4; 269 270 /* Set return values in child mode */ 271 tf2->tf_out[0] = 0; 272 tf2->tf_out[1] = 1; 273 274 /* Construct kernel frame to return to in cpu_switch() */ 275 rp = (struct rwindow *)((u_int)npcb + TOPFRAMEOFF); 276 rp->rw_local[0] = (int)func; /* Function to call */ 277 rp->rw_local[1] = (int)arg; /* and its argument */ 278 279 npcb->pcb_pc = (int)proc_trampoline - 8; 280 npcb->pcb_sp = (int)rp; 281 npcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 282 npcb->pcb_wim = 1; /* Fence at window #1 */ 283 } 284 285 /* 286 * Cleanup FPU state. 287 */ 288 void 289 cpu_lwp_free(struct lwp *l, int proc) 290 { 291 struct fpstate *fs; 292 293 if ((fs = l->l_md.md_fpstate) != NULL) { 294 struct cpu_info *cpi; 295 int s; 296 297 FPU_LOCK(s); 298 if ((cpi = l->l_md.md_fpu) != NULL) { 299 if (cpi->fplwp != l) 300 panic("FPU(%d): fplwp %p", 301 cpi->ci_cpuid, cpi->fplwp); 302 if (l == cpuinfo.fplwp) 303 savefpstate(fs); 304 #if defined(MULTIPROCESSOR) 305 else 306 XCALL1(savefpstate, fs, 1 << cpi->ci_cpuid); 307 #endif 308 cpi->fplwp = NULL; 309 } 310 l->l_md.md_fpu = NULL; 311 FPU_UNLOCK(s); 312 l->l_md.md_fpstate = NULL; 313 free((void *)fs, M_SUBPROC); 314 } 315 } 316 317 void 318 cpu_setfunc(l, func, arg) 319 struct lwp *l; 320 void (*func) __P((void *)); 321 void *arg; 322 { 323 struct pcb *pcb = &l->l_addr->u_pcb; 324 /*struct trapframe *tf = l->l_md.md_tf;*/ 325 struct rwindow *rp; 326 327 /* Construct kernel frame to return to in cpu_switch() */ 328 rp = (struct rwindow *)((u_int)pcb + TOPFRAMEOFF); 329 rp->rw_local[0] = (int)func; /* Function to call */ 330 rp->rw_local[1] = (int)arg; /* and its argument */ 331 332 pcb->pcb_pc = (int)proc_trampoline - 8; 333 pcb->pcb_sp = (int)rp; 334 pcb->pcb_psr &= ~PSR_CWP; /* Run in window #0 */ 335 pcb->pcb_wim = 1; /* Fence at window #1 */ 336 } 337 338 /* 339 * cpu_coredump is called to write a core dump header. 340 * (should this be defined elsewhere? machdep.c?) 341 */ 342 int 343 cpu_coredump(l, vp, cred, chdr) 344 struct lwp *l; 345 struct vnode *vp; 346 struct ucred *cred; 347 struct core *chdr; 348 { 349 int error; 350 struct md_coredump md_core; 351 struct coreseg cseg; 352 struct proc *p; 353 354 p = l->l_proc; 355 356 CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0); 357 chdr->c_hdrsize = ALIGN(sizeof(*chdr)); 358 chdr->c_seghdrsize = ALIGN(sizeof(cseg)); 359 chdr->c_cpusize = sizeof(md_core); 360 361 md_core.md_tf = *l->l_md.md_tf; 362 if (l->l_md.md_fpstate) { 363 if (l == cpuinfo.fplwp) 364 savefpstate(l->l_md.md_fpstate); 365 md_core.md_fpstate = *l->l_md.md_fpstate; 366 } else 367 bzero((caddr_t)&md_core.md_fpstate, sizeof(struct fpstate)); 368 369 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU); 370 cseg.c_addr = 0; 371 cseg.c_size = chdr->c_cpusize; 372 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize, 373 (off_t)chdr->c_hdrsize, UIO_SYSSPACE, 374 IO_NODELOCKED|IO_UNIT, cred, NULL, NULL); 375 if (error) 376 return error; 377 378 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core), 379 (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE, 380 IO_NODELOCKED|IO_UNIT, cred, NULL, NULL); 381 if (!error) 382 chdr->c_nseg++; 383 384 return error; 385 } 386