1 /* $NetBSD: vm_machdep.c,v 1.28 2003/05/17 00:41:36 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1994-1998 Mark Brinicombe. 5 * Copyright (c) 1994 Brini. 6 * All rights reserved. 7 * 8 * This code is derived from software written for Brini by Mark Brinicombe 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by Brini. 21 * 4. The name of the company nor the name of the author may be used to 22 * endorse or promote products derived from this software without specific 23 * prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED 26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * RiscBSD kernel project 38 * 39 * vm_machdep.h 40 * 41 * vm machine specific bits 42 * 43 * Created : 08/10/94 44 */ 45 46 #include "opt_armfpe.h" 47 #include "opt_pmap_debug.h" 48 #include "opt_perfctrs.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/proc.h> 53 #include <sys/malloc.h> 54 #include <sys/vnode.h> 55 #include <sys/buf.h> 56 #include <sys/pmc.h> 57 #include <sys/user.h> 58 #include <sys/exec.h> 59 #include <sys/syslog.h> 60 61 #include <uvm/uvm_extern.h> 62 63 #include <machine/cpu.h> 64 #include <machine/pmap.h> 65 #include <machine/reg.h> 66 #include <machine/vmparam.h> 67 68 #ifdef ARMFPE 69 #include <arm/fpe-arm/armfpe.h> 70 #endif 71 72 extern pv_addr_t systempage; 73 74 int process_read_regs __P((struct proc *p, struct reg *regs)); 75 int process_read_fpregs __P((struct proc *p, struct fpreg *regs)); 76 77 void switch_exit __P((struct lwp *l, struct lwp *l0, 78 void (*)(struct lwp *))); 79 extern void proc_trampoline __P((void)); 80 81 /* 82 * Special compilation symbols: 83 * 84 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern 85 * on forking and check the pattern on exit, reporting 86 * the amount of stack used. 87 */ 88 89 void 90 cpu_proc_fork(p1, p2) 91 struct proc *p1, *p2; 92 { 93 94 #if defined(PERFCTRS) 95 if (PMC_ENABLED(p1)) 96 pmc_md_fork(p1, p2); 97 else { 98 p2->p_md.pmc_enabled = 0; 99 p2->p_md.pmc_state = NULL; 100 } 101 #endif 102 } 103 104 /* 105 * Finish a fork operation, with process p2 nearly set up. 106 * Copy and update the pcb and trap frame, making the child ready to run. 107 * 108 * Rig the child's kernel stack so that it will start out in 109 * proc_trampoline() and call child_return() with p2 as an 110 * argument. This causes the newly-created child process to go 111 * directly to user level with an apparent return value of 0 from 112 * fork(), while the parent process returns normally. 113 * 114 * p1 is the process being forked; if p1 == &proc0, we are creating 115 * a kernel thread, and the return path and argument are specified with 116 * `func' and `arg'. 117 * 118 * If an alternate user-level stack is requested (with non-zero values 119 * in both the stack and stacksize args), set up the user stack pointer 120 * accordingly. 121 */ 122 void 123 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg) 124 struct lwp *l1; 125 struct lwp *l2; 126 void *stack; 127 size_t stacksize; 128 void (*func) __P((void *)); 129 void *arg; 130 { 131 struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb; 132 struct trapframe *tf; 133 struct switchframe *sf; 134 135 #ifdef PMAP_DEBUG 136 if (pmap_debug_level >= 0) 137 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0); 138 #endif /* PMAP_DEBUG */ 139 140 #if 0 /* XXX */ 141 if (l1 == curlwp) { 142 /* Sync the PCB before we copy it. */ 143 savectx(curpcb); 144 } 145 #endif 146 147 /* Copy the pcb */ 148 *pcb = l1->l_addr->u_pcb; 149 150 /* 151 * Set up the undefined stack for the process. 152 * Note: this stack is not in use if we are forking from p1 153 */ 154 pcb->pcb_un.un_32.pcb32_und_sp = (u_int)l2->l_addr + 155 USPACE_UNDEF_STACK_TOP; 156 pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP; 157 158 #ifdef STACKCHECKS 159 /* Fill the undefined stack with a known pattern */ 160 memset(((u_char *)l2->l_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd, 161 (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)); 162 /* Fill the kernel stack with a known pattern */ 163 memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd, 164 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)); 165 #endif /* STACKCHECKS */ 166 167 #ifdef PMAP_DEBUG 168 if (pmap_debug_level >= 0) { 169 printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n", 170 l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid, 171 l1->l_proc->p_vmspace->vm_map.pmap); 172 printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n", 173 l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid, 174 l2->l_proc->p_vmspace->vm_map.pmap); 175 } 176 #endif /* PMAP_DEBUG */ 177 178 pmap_activate(l2); 179 180 #ifdef ARMFPE 181 /* Initialise a new FP context for p2 and copy the context from p1 */ 182 arm_fpe_core_initcontext(FP_CONTEXT(l2)); 183 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2)); 184 #endif /* ARMFPE */ 185 186 l2->l_addr->u_pcb.pcb_tf = tf = 187 (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1; 188 *tf = *l1->l_addr->u_pcb.pcb_tf; 189 190 /* 191 * If specified, give the child a different stack. 192 */ 193 if (stack != NULL) 194 tf->tf_usr_sp = (u_int)stack + stacksize; 195 196 sf = (struct switchframe *)tf - 1; 197 sf->sf_spl = 0; /* always equivalent to spl0() */ 198 sf->sf_r4 = (u_int)func; 199 sf->sf_r5 = (u_int)arg; 200 sf->sf_pc = (u_int)proc_trampoline; 201 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf; 202 } 203 204 void 205 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg) 206 { 207 struct pcb *pcb = &l->l_addr->u_pcb; 208 struct trapframe *tf = pcb->pcb_tf; 209 struct switchframe *sf = (struct switchframe *)tf - 1; 210 211 sf->sf_spl = 0; /* always equivalent to spl0() */ 212 sf->sf_r4 = (u_int)func; 213 sf->sf_r5 = (u_int)arg; 214 sf->sf_pc = (u_int)proc_trampoline; 215 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf; 216 } 217 218 /* 219 * cpu_exit is called as the last action during exit. 220 * 221 * We clean up a little and then call switch_exit() with the old proc as an 222 * argument. switch_exit() first switches to proc0's context, and finally 223 * jumps into switch() to wait for another process to wake up. 224 */ 225 226 void 227 cpu_exit(struct lwp *l, int proc) 228 { 229 #ifdef ARMFPE 230 /* Abort any active FP operation and deactivate the context */ 231 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL); 232 arm_fpe_core_changecontext(0); 233 #endif /* ARMFPE */ 234 235 #ifdef STACKCHECKS 236 /* Report how much stack has been used - debugging */ 237 if (l) { 238 u_char *ptr; 239 int loop; 240 241 ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM; 242 for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM) 243 && *ptr == 0xdd; ++loop, ++ptr) ; 244 log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop); 245 ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM; 246 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM) 247 && *ptr == 0xdd; ++loop, ++ptr) ; 248 log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop); 249 } 250 #endif /* STACKCHECKS */ 251 uvmexp.swtch++; 252 switch_exit(l, &lwp0, proc ? exit2 : lwp_exit2); 253 } 254 255 256 void 257 cpu_swapin(l) 258 struct lwp *l; 259 { 260 #if 0 261 struct proc *p = l->l_proc; 262 263 /* Don't do this. See the comment in cpu_swapout(). */ 264 #ifdef PMAP_DEBUG 265 if (pmap_debug_level >= 0) 266 printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid, 267 p->p_comm, p->p_vmspace->vm_map.pmap); 268 #endif /* PMAP_DEBUG */ 269 270 if (vector_page < KERNEL_BASE) { 271 /* Map the vector page */ 272 pmap_enter(p->p_vmspace->vm_map.pmap, vector_page, 273 systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED); 274 pmap_update(p->p_vmspace->vm_map.pmap); 275 } 276 #endif 277 } 278 279 280 void 281 cpu_swapout(l) 282 struct lwp *l; 283 { 284 #if 0 285 struct proc *p = l->l_proc; 286 287 /* 288 * Don't do this! If the pmap is shared with another process, 289 * it will loose it's page0 entry. That's bad news indeed. 290 */ 291 #ifdef PMAP_DEBUG 292 if (pmap_debug_level >= 0) 293 printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid, 294 p->p_comm, &p->p_vmspace->vm_map.pmap); 295 #endif /* PMAP_DEBUG */ 296 297 if (vector_page < KERNEL_BASE) { 298 /* Free the system page mapping */ 299 pmap_remove(p->p_vmspace->vm_map.pmap, vector_page, 300 vector_page + PAGE_SIZE); 301 pmap_update(p->p_vmspace->vm_map.pmap); 302 } 303 #endif 304 } 305 306 307 /* 308 * Move pages from one kernel virtual address to another. 309 * Both addresses are assumed to reside in the Sysmap, 310 * and size must be a multiple of PAGE_SIZE. 311 */ 312 313 void 314 pagemove(from, to, size) 315 caddr_t from, to; 316 size_t size; 317 { 318 paddr_t pa; 319 boolean_t rv; 320 321 if (size % PAGE_SIZE) 322 panic("pagemove: size=%08lx", (u_long) size); 323 324 while (size > 0) { 325 rv = pmap_extract(pmap_kernel(), (vaddr_t) from, &pa); 326 #ifdef DEBUG 327 if (rv == FALSE) 328 panic("pagemove 2"); 329 if (pmap_extract(pmap_kernel(), (vaddr_t) to, NULL) == TRUE) 330 panic("pagemove 3"); 331 #endif 332 pmap_kremove((vaddr_t) from, PAGE_SIZE); 333 pmap_kenter_pa((vaddr_t) to, pa, VM_PROT_READ|VM_PROT_WRITE); 334 from += PAGE_SIZE; 335 to += PAGE_SIZE; 336 size -= PAGE_SIZE; 337 } 338 pmap_update(pmap_kernel()); 339 } 340 341 /* 342 * Map a user I/O request into kernel virtual address space. 343 * Note: the pages are already locked by uvm_vslock(), so we 344 * do not need to pass an access_type to pmap_enter(). 345 */ 346 void 347 vmapbuf(bp, len) 348 struct buf *bp; 349 vsize_t len; 350 { 351 vaddr_t faddr, taddr, off; 352 paddr_t fpa; 353 354 355 #ifdef PMAP_DEBUG 356 if (pmap_debug_level >= 0) 357 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp, 358 (u_int)bp->b_data, (u_int)len); 359 #endif /* PMAP_DEBUG */ 360 361 if ((bp->b_flags & B_PHYS) == 0) 362 panic("vmapbuf"); 363 364 faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data); 365 off = (vaddr_t)bp->b_data - faddr; 366 len = round_page(off + len); 367 taddr = uvm_km_valloc_wait(phys_map, len); 368 bp->b_data = (caddr_t)(taddr + off); 369 370 /* 371 * The region is locked, so we expect that pmap_pte() will return 372 * non-NULL. 373 */ 374 while (len) { 375 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map), 376 faddr, &fpa); 377 pmap_enter(pmap_kernel(), taddr, fpa, 378 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED); 379 faddr += PAGE_SIZE; 380 taddr += PAGE_SIZE; 381 len -= PAGE_SIZE; 382 } 383 pmap_update(pmap_kernel()); 384 } 385 386 /* 387 * Unmap a previously-mapped user I/O request. 388 */ 389 void 390 vunmapbuf(bp, len) 391 struct buf *bp; 392 vsize_t len; 393 { 394 vaddr_t addr, off; 395 396 #ifdef PMAP_DEBUG 397 if (pmap_debug_level >= 0) 398 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n", 399 (u_int)bp, (u_int)bp->b_data, (u_int)len); 400 #endif /* PMAP_DEBUG */ 401 402 if ((bp->b_flags & B_PHYS) == 0) 403 panic("vunmapbuf"); 404 405 /* 406 * Make sure the cache does not have dirty data for the 407 * pages we had mapped. 408 */ 409 addr = trunc_page((vaddr_t)bp->b_data); 410 off = (vaddr_t)bp->b_data - addr; 411 len = round_page(off + len); 412 413 pmap_remove(pmap_kernel(), addr, addr + len); 414 pmap_update(pmap_kernel()); 415 uvm_km_free_wakeup(phys_map, addr, len); 416 bp->b_data = bp->b_saveaddr; 417 bp->b_saveaddr = 0; 418 } 419 420 /* End of vm_machdep.c */ 421