1 /* $NetBSD: vm_machdep.c,v 1.69 2014/03/10 05:18:34 matt 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 <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.69 2014/03/10 05:18:34 matt Exp $"); 48 49 #include "opt_armfpe.h" 50 #include "opt_pmap_debug.h" 51 #include "opt_perfctrs.h" 52 #include "opt_cputypes.h" 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/proc.h> 57 #include <sys/malloc.h> 58 #include <sys/vnode.h> 59 #include <sys/cpu.h> 60 #include <sys/buf.h> 61 #include <sys/pmc.h> 62 #include <sys/exec.h> 63 #include <sys/syslog.h> 64 65 #include <uvm/uvm_extern.h> 66 67 #include <arm/vfpreg.h> 68 69 #include <machine/pcb.h> 70 #include <machine/pmap.h> 71 #include <machine/reg.h> 72 #include <machine/vmparam.h> 73 74 void lwp_trampoline(void); 75 76 /* 77 * Special compilation symbols: 78 * 79 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern 80 * on forking and check the pattern on exit, reporting 81 * the amount of stack used. 82 */ 83 84 void 85 cpu_proc_fork(struct proc *p1, struct proc *p2) 86 { 87 88 #if defined(PERFCTRS) 89 if (PMC_ENABLED(p1)) 90 pmc_md_fork(p1, p2); 91 else { 92 p2->p_md.pmc_enabled = 0; 93 p2->p_md.pmc_state = NULL; 94 } 95 #endif 96 /* 97 * Copy machine arch string (it's small so just memcpy it). 98 */ 99 memcpy(p2->p_md.md_march, p1->p_md.md_march, sizeof(p2->p_md.md_march)); 100 } 101 102 /* 103 * Finish a fork operation, with LWP l2 nearly set up. 104 * 105 * Copy and update the pcb and trapframe, making the child ready to run. 106 * 107 * Rig the child's kernel stack so that it will start out in 108 * lwp_trampoline() which will call the specified func with the argument arg. 109 * 110 * If an alternate user-level stack is requested (with non-zero values 111 * in both the stack and stacksize args), set up the user stack pointer 112 * accordingly. 113 */ 114 void 115 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, 116 void (*func)(void *), void *arg) 117 { 118 struct switchframe *sf; 119 vaddr_t uv; 120 121 const struct pcb * const pcb1 = lwp_getpcb(l1); 122 struct pcb * const pcb2 = lwp_getpcb(l2); 123 124 #ifdef PMAP_DEBUG 125 if (pmap_debug_level > 0) 126 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0); 127 #endif /* PMAP_DEBUG */ 128 129 /* Copy the pcb */ 130 *pcb2 = *pcb1; 131 132 #ifdef FPU_VFP 133 /* 134 * Disable the VFP for a newly created LWP but remember if the 135 * VFP state is valid. 136 */ 137 pcb2->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN; 138 #endif 139 140 /* 141 * Set up the kernel stack for the process. 142 * Note: this stack is not in use if we are forking from p1 143 */ 144 uv = uvm_lwp_getuarea(l2); 145 pcb2->pcb_ksp = uv + USPACE_SVC_STACK_TOP; 146 147 #ifdef STACKCHECKS 148 /* Fill the kernel stack with a known pattern */ 149 memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd, 150 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)); 151 #endif /* STACKCHECKS */ 152 153 #ifdef PMAP_DEBUG 154 if (pmap_debug_level > 0) { 155 printf("l1: pcb=%p pid=%d pmap=%p\n", 156 pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap); 157 printf("l2: pcb=%p pid=%d pmap=%p\n", 158 pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap); 159 } 160 #endif /* PMAP_DEBUG */ 161 162 struct trapframe *tf = (struct trapframe *)pcb2->pcb_ksp - 1; 163 lwp_settrapframe(l2, tf); 164 *tf = *lwp_trapframe(l1); 165 166 /* 167 * If specified, give the child a different stack (make sure 168 * it's 8-byte aligned). 169 */ 170 if (stack != NULL) 171 tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8; 172 173 sf = (struct switchframe *)tf - 1; 174 sf->sf_r4 = (u_int)func; 175 sf->sf_r5 = (u_int)arg; 176 sf->sf_r7 = PSR_USR32_MODE; /* for returning to userspace */ 177 sf->sf_sp = (u_int)tf; 178 sf->sf_pc = (u_int)lwp_trampoline; 179 pcb2->pcb_ksp = (u_int)sf; 180 } 181 182 /* 183 * cpu_exit is called as the last action during exit. 184 * 185 * We clean up a little and then call switch_exit() with the old proc as an 186 * argument. switch_exit() first switches to lwp0's context, and finally 187 * jumps into switch() to wait for another process to wake up. 188 */ 189 190 void 191 cpu_lwp_free(struct lwp *l, int proc) 192 { 193 #ifdef STACKCHECKS 194 /* Report how much stack has been used - debugging */ 195 struct pcb * const pcb = lwp_getpcb(l); 196 u_char *ptr; 197 u_int loop; 198 199 ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM; 200 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM) 201 && *ptr == 0xdd; ++loop, ++ptr) ; 202 log(LOG_INFO, "%u bytes of svc stack fill pattern\n", loop); 203 #endif /* STACKCHECKS */ 204 } 205 206 void 207 cpu_lwp_free2(struct lwp *l) 208 { 209 } 210 211 /* 212 * Map a user I/O request into kernel virtual address space. 213 * Note: the pages are already locked by uvm_vslock(), so we 214 * do not need to pass an access_type to pmap_enter(). 215 */ 216 int 217 vmapbuf(struct buf *bp, vsize_t len) 218 { 219 vaddr_t faddr, taddr, off; 220 paddr_t fpa; 221 222 223 #ifdef PMAP_DEBUG 224 if (pmap_debug_level > 0) 225 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp, 226 (u_int)bp->b_data, (u_int)len); 227 #endif /* PMAP_DEBUG */ 228 229 if ((bp->b_flags & B_PHYS) == 0) 230 panic("vmapbuf"); 231 232 bp->b_saveaddr = bp->b_data; 233 faddr = trunc_page((vaddr_t)bp->b_data); 234 off = (vaddr_t)bp->b_data - faddr; 235 len = round_page(off + len); 236 taddr = uvm_km_alloc(phys_map, len, atop(faddr) & uvmexp.colormask, 237 UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH); 238 bp->b_data = (void *)(taddr + off); 239 240 /* 241 * The region is locked, so we expect that pmap_pte() will return 242 * non-NULL. 243 */ 244 while (len) { 245 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map), 246 faddr, &fpa); 247 pmap_kenter_pa(taddr, fpa, VM_PROT_READ|VM_PROT_WRITE, 248 PMAP_WIRED); 249 faddr += PAGE_SIZE; 250 taddr += PAGE_SIZE; 251 len -= PAGE_SIZE; 252 } 253 pmap_update(pmap_kernel()); 254 255 return 0; 256 } 257 258 /* 259 * Unmap a previously-mapped user I/O request. 260 */ 261 void 262 vunmapbuf(struct buf *bp, vsize_t len) 263 { 264 vaddr_t addr, off; 265 266 #ifdef PMAP_DEBUG 267 if (pmap_debug_level > 0) 268 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n", 269 (u_int)bp, (u_int)bp->b_data, (u_int)len); 270 #endif /* PMAP_DEBUG */ 271 272 if ((bp->b_flags & B_PHYS) == 0) 273 panic("vunmapbuf"); 274 275 /* 276 * Make sure the cache does not have dirty data for the 277 * pages we had mapped. 278 */ 279 addr = trunc_page((vaddr_t)bp->b_data); 280 off = (vaddr_t)bp->b_data - addr; 281 len = round_page(off + len); 282 283 pmap_kremove(addr, len); 284 pmap_update(pmap_kernel()); 285 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY); 286 bp->b_data = bp->b_saveaddr; 287 bp->b_saveaddr = 0; 288 } 289 290 /* End of vm_machdep.c */ 291