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