Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.47
      1 /*	$NetBSD: vm_machdep.c,v 1.47 2009/03/14 14:45:55 dsl 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.47 2009/03/14 14:45:55 dsl 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/buf.h>
     60 #include <sys/pmc.h>
     61 #include <sys/user.h>
     62 #include <sys/exec.h>
     63 #include <sys/syslog.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #include <machine/cpu.h>
     68 #include <machine/pmap.h>
     69 #include <machine/reg.h>
     70 #include <machine/vmparam.h>
     71 
     72 #ifdef ARMFPE
     73 #include <arm/fpe-arm/armfpe.h>
     74 #endif
     75 
     76 extern pv_addr_t systempage;
     77 
     78 int process_read_regs(struct proc *p, struct reg *regs);
     79 int process_read_fpregs(struct proc *p, struct fpreg *regs);
     80 
     81 void lwp_trampoline(void);
     82 
     83 /*
     84  * Special compilation symbols:
     85  *
     86  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
     87  *		 on forking and check the pattern on exit, reporting
     88  *		 the amount of stack used.
     89  */
     90 
     91 void
     92 cpu_proc_fork(p1, p2)
     93 	struct proc *p1, *p2;
     94 {
     95 
     96 #if defined(PERFCTRS)
     97 	if (PMC_ENABLED(p1))
     98 		pmc_md_fork(p1, p2);
     99 	else {
    100 		p2->p_md.pmc_enabled = 0;
    101 		p2->p_md.pmc_state = NULL;
    102 	}
    103 #endif
    104 }
    105 
    106 void
    107 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
    108 {
    109 	struct pcb *pcb = &l->l_addr->u_pcb;
    110 	struct trapframe *tf = pcb->pcb_tf;
    111 	struct switchframe *sf = (struct switchframe *)tf - 1;
    112 
    113 	sf->sf_r4 = (u_int)func;
    114 	sf->sf_r5 = (u_int)arg;
    115 	sf->sf_sp = (u_int)tf;
    116 	sf->sf_pc = (u_int)lwp_trampoline;
    117 	pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
    118 }
    119 
    120 /*
    121  * Finish a fork operation, with LWP l2 nearly set up.
    122  *
    123  * Copy and update the pcb and trapframe, making the child ready to run.
    124  *
    125  * Rig the child's kernel stack so that it will start out in
    126  * lwp_trampoline() which will call the specified func with the argument arg.
    127  *
    128  * If an alternate user-level stack is requested (with non-zero values
    129  * in both the stack and stacksize args), set up the user stack pointer
    130  * accordingly.
    131  */
    132 void
    133 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    134     void (*func)(void *), void *arg)
    135 {
    136 	struct pcb *pcb = &l2->l_addr->u_pcb;
    137 	struct trapframe *tf;
    138 
    139 #ifdef PMAP_DEBUG
    140 	if (pmap_debug_level >= 0)
    141 		printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    142 #endif	/* PMAP_DEBUG */
    143 
    144 #if 0 /* XXX */
    145 	if (l1 == curlwp) {
    146 		/* Sync the PCB before we copy it. */
    147 		savectx(curpcb);
    148 	}
    149 #endif
    150 
    151 	l2->l_md.md_flags = l1->l_md.md_flags & MDP_VFPUSED;
    152 
    153 #ifdef FPU_VFP
    154 	/*
    155 	 * Copy the floating point state from the VFP to the PCB
    156 	 * if this process has state stored there.
    157 	 */
    158 	if (l1->l_addr->u_pcb.pcb_vfpcpu != NULL)
    159 		vfp_saveregs_lwp(l1, 1);
    160 #endif
    161 
    162 	/* Copy the pcb */
    163 	*pcb = l1->l_addr->u_pcb;
    164 
    165 	/*
    166 	 * Set up the stack for the process.
    167 	 * Note: this stack is not in use if we are forking from p1
    168 	 */
    169 	pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
    170 
    171 #ifdef STACKCHECKS
    172 	/* Fill the kernel stack with a known pattern */
    173 	memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
    174 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    175 #endif	/* STACKCHECKS */
    176 
    177 #ifdef PMAP_DEBUG
    178 	if (pmap_debug_level >= 0) {
    179 		printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    180 		    l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
    181 		    l1->l_proc->p_vmspace->vm_map.pmap);
    182 		printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    183 		    l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
    184 		    l2->l_proc->p_vmspace->vm_map.pmap);
    185 	}
    186 #endif	/* PMAP_DEBUG */
    187 
    188 #ifdef ARMFPE
    189 	/* Initialise a new FP context for p2 and copy the context from p1 */
    190 	arm_fpe_core_initcontext(FP_CONTEXT(l2));
    191 	arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
    192 #endif	/* ARMFPE */
    193 
    194 	l2->l_addr->u_pcb.pcb_tf = tf =
    195 	    (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
    196 	*tf = *l1->l_addr->u_pcb.pcb_tf;
    197 
    198 	/*
    199 	 * If specified, give the child a different stack.
    200 	 */
    201 	if (stack != NULL)
    202 		tf->tf_usr_sp = (u_int)stack + stacksize;
    203 
    204 	cpu_setfunc(l2, func, arg);
    205 }
    206 
    207 /*
    208  * cpu_exit is called as the last action during exit.
    209  *
    210  * We clean up a little and then call switch_exit() with the old proc as an
    211  * argument.  switch_exit() first switches to proc0's context, and finally
    212  * jumps into switch() to wait for another process to wake up.
    213  */
    214 
    215 void
    216 cpu_lwp_free(struct lwp *l, int proc)
    217 {
    218 #ifdef ARMFPE
    219 	/* Abort any active FP operation and deactivate the context */
    220 	arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
    221 	arm_fpe_core_changecontext(0);
    222 #endif	/* ARMFPE */
    223 
    224 #ifdef FPU_VFP
    225 	if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
    226 		vfp_saveregs_lwp(l, 0);
    227 #endif
    228 
    229 #ifdef STACKCHECKS
    230 	/* Report how much stack has been used - debugging */
    231 	if (l) {
    232 		u_char *ptr;
    233 		int loop;
    234 
    235 		ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
    236 		for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    237 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    238 		log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
    239 	}
    240 #endif	/* STACKCHECKS */
    241 }
    242 
    243 void
    244 cpu_lwp_free2(struct lwp *l)
    245 {
    246 }
    247 
    248 void
    249 cpu_swapin(l)
    250 	struct lwp *l;
    251 {
    252 #if 0
    253 	struct proc *p = l->l_proc;
    254 
    255 	/* Don't do this.  See the comment in cpu_swapout().  */
    256 #ifdef PMAP_DEBUG
    257 	if (pmap_debug_level >= 0)
    258 		printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
    259 		    p->p_comm, p->p_vmspace->vm_map.pmap);
    260 #endif	/* PMAP_DEBUG */
    261 
    262 	if (vector_page < KERNEL_BASE) {
    263 		/* Map the vector page */
    264 		pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
    265 		    systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    266 		pmap_update(p->p_vmspace->vm_map.pmap);
    267 	}
    268 #endif
    269 }
    270 
    271 
    272 void
    273 cpu_swapout(l)
    274 	struct lwp *l;
    275 {
    276 #ifdef FPU_VFP
    277 	if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
    278 		vfp_saveregs_lwp(l, 1);
    279 #endif
    280 
    281 #if 0
    282 	struct proc *p = l->l_proc;
    283 
    284 	/*
    285 	 * Don't do this!  If the pmap is shared with another process,
    286 	 * it will loose it's page0 entry.  That's bad news indeed.
    287 	 */
    288 #ifdef PMAP_DEBUG
    289 	if (pmap_debug_level >= 0)
    290 		printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
    291 		    p->p_comm, &p->p_vmspace->vm_map.pmap);
    292 #endif	/* PMAP_DEBUG */
    293 
    294 	if (vector_page < KERNEL_BASE) {
    295 		/* Free the system page mapping */
    296 		pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
    297 		    vector_page + PAGE_SIZE);
    298 		pmap_update(p->p_vmspace->vm_map.pmap);
    299 	}
    300 #endif
    301 }
    302 
    303 /*
    304  * Map a user I/O request into kernel virtual address space.
    305  * Note: the pages are already locked by uvm_vslock(), so we
    306  * do not need to pass an access_type to pmap_enter().
    307  */
    308 void
    309 vmapbuf(bp, len)
    310 	struct buf *bp;
    311 	vsize_t len;
    312 {
    313 	vaddr_t faddr, taddr, off;
    314 	paddr_t fpa;
    315 
    316 
    317 #ifdef PMAP_DEBUG
    318 	if (pmap_debug_level >= 0)
    319 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    320 		    (u_int)bp->b_data, (u_int)len);
    321 #endif	/* PMAP_DEBUG */
    322 
    323 	if ((bp->b_flags & B_PHYS) == 0)
    324 		panic("vmapbuf");
    325 
    326 	bp->b_saveaddr = bp->b_data;
    327 	faddr = trunc_page((vaddr_t)bp->b_data);
    328 	off = (vaddr_t)bp->b_data - faddr;
    329 	len = round_page(off + len);
    330 	taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    331 	bp->b_data = (void *)(taddr + off);
    332 
    333 	/*
    334 	 * The region is locked, so we expect that pmap_pte() will return
    335 	 * non-NULL.
    336 	 */
    337 	while (len) {
    338 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    339 		    faddr, &fpa);
    340 		pmap_enter(pmap_kernel(), taddr, fpa,
    341 			VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    342 		faddr += PAGE_SIZE;
    343 		taddr += PAGE_SIZE;
    344 		len -= PAGE_SIZE;
    345 	}
    346 	pmap_update(pmap_kernel());
    347 }
    348 
    349 /*
    350  * Unmap a previously-mapped user I/O request.
    351  */
    352 void
    353 vunmapbuf(bp, len)
    354 	struct buf *bp;
    355 	vsize_t len;
    356 {
    357 	vaddr_t addr, off;
    358 
    359 #ifdef PMAP_DEBUG
    360 	if (pmap_debug_level >= 0)
    361 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    362 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    363 #endif	/* PMAP_DEBUG */
    364 
    365 	if ((bp->b_flags & B_PHYS) == 0)
    366 		panic("vunmapbuf");
    367 
    368 	/*
    369 	 * Make sure the cache does not have dirty data for the
    370 	 * pages we had mapped.
    371 	 */
    372 	addr = trunc_page((vaddr_t)bp->b_data);
    373 	off = (vaddr_t)bp->b_data - addr;
    374 	len = round_page(off + len);
    375 
    376 	pmap_remove(pmap_kernel(), addr, addr + len);
    377 	pmap_update(pmap_kernel());
    378 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
    379 	bp->b_data = bp->b_saveaddr;
    380 	bp->b_saveaddr = 0;
    381 }
    382 
    383 /* End of vm_machdep.c */
    384