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