Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.59
      1 /*	$NetBSD: vm_machdep.c,v 1.59 2012/08/16 17:35:01 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.59 2012/08/16 17:35:01 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 <machine/pcb.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(struct proc *p1, struct proc *p2)
     93 {
     94 
     95 #if defined(PERFCTRS)
     96 	if (PMC_ENABLED(p1))
     97 		pmc_md_fork(p1, p2);
     98 	else {
     99 		p2->p_md.pmc_enabled = 0;
    100 		p2->p_md.pmc_state = NULL;
    101 	}
    102 #endif
    103 }
    104 
    105 /*
    106  * Finish a fork operation, with LWP l2 nearly set up.
    107  *
    108  * Copy and update the pcb and trapframe, making the child ready to run.
    109  *
    110  * Rig the child's kernel stack so that it will start out in
    111  * lwp_trampoline() which will call the specified func with the argument arg.
    112  *
    113  * If an alternate user-level stack is requested (with non-zero values
    114  * in both the stack and stacksize args), set up the user stack pointer
    115  * accordingly.
    116  */
    117 void
    118 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    119     void (*func)(void *), void *arg)
    120 {
    121 	struct pcb *pcb1, *pcb2;
    122 	struct switchframe *sf;
    123 	vaddr_t uv;
    124 
    125 	pcb1 = lwp_getpcb(l1);
    126 	pcb2 = lwp_getpcb(l2);
    127 
    128 #ifdef PMAP_DEBUG
    129 	if (pmap_debug_level >= 0)
    130 		printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    131 #endif	/* PMAP_DEBUG */
    132 
    133 #if 0 /* XXX */
    134 	if (l1 == curlwp) {
    135 		/* Sync the PCB before we copy it. */
    136 		savectx(curpcb);
    137 	}
    138 #endif
    139 
    140 	l2->l_md.md_flags = l1->l_md.md_flags & MDLWP_VFPUSED;
    141 
    142 	/* Copy the pcb */
    143 	*pcb2 = *pcb1;
    144 
    145 	/*
    146 	 * Set up the kernel stack for the process.
    147 	 * Note: this stack is not in use if we are forking from p1
    148 	 */
    149 	uv = uvm_lwp_getuarea(l2);
    150 	pcb2->pcb_sp = uv + USPACE_SVC_STACK_TOP;
    151 
    152 #ifdef STACKCHECKS
    153 	/* Fill the kernel stack with a known pattern */
    154 	memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
    155 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    156 #endif	/* STACKCHECKS */
    157 
    158 #ifdef PMAP_DEBUG
    159 	if (pmap_debug_level >= 0) {
    160 		printf("l1: pcb=%p pid=%d pmap=%p\n",
    161 		    pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
    162 		printf("l2: pcb=%p pid=%d pmap=%p\n",
    163 		    pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
    164 	}
    165 #endif	/* PMAP_DEBUG */
    166 
    167 #ifdef ARMFPE
    168 	/* Initialise a new FP context for p2 and copy the context from p1 */
    169 	arm_fpe_core_initcontext(FP_CONTEXT(l2));
    170 	arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
    171 #endif	/* ARMFPE */
    172 
    173 	struct trapframe *tf = (struct trapframe *)pcb2->pcb_sp - 1;
    174 	lwp_settrapframe(l2, tf);
    175 	*tf = *lwp_trapframe(l1);
    176 
    177 	/*
    178 	 * If specified, give the child a different stack (make sure
    179 	 * it's 8-byte aligned).
    180 	 */
    181 	if (stack != NULL)
    182 		tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
    183 
    184 	sf = (struct switchframe *)tf - 1;
    185 	sf->sf_r4 = (u_int)func;
    186 	sf->sf_r5 = (u_int)arg;
    187 	sf->sf_sp = (u_int)tf;
    188 	sf->sf_pc = (u_int)lwp_trampoline;
    189 	pcb2->pcb_sp = (u_int)sf;
    190 }
    191 
    192 /*
    193  * cpu_exit is called as the last action during exit.
    194  *
    195  * We clean up a little and then call switch_exit() with the old proc as an
    196  * argument.  switch_exit() first switches to lwp0's context, and finally
    197  * jumps into switch() to wait for another process to wake up.
    198  */
    199 
    200 void
    201 cpu_lwp_free(struct lwp *l, int proc)
    202 {
    203 #ifdef ARMFPE
    204 	/* Abort any active FP operation and deactivate the context */
    205 	arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
    206 	arm_fpe_core_changecontext(0);
    207 #endif	/* ARMFPE */
    208 
    209 #ifdef STACKCHECKS
    210 	/* Report how much stack has been used - debugging */
    211 	if (l) {
    212 		u_char *ptr;
    213 		int loop;
    214 
    215 		ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
    216 		for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    217 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    218 		log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
    219 	}
    220 #endif	/* STACKCHECKS */
    221 }
    222 
    223 void
    224 cpu_lwp_free2(struct lwp *l)
    225 {
    226 }
    227 
    228 /*
    229  * Map a user I/O request into kernel virtual address space.
    230  * Note: the pages are already locked by uvm_vslock(), so we
    231  * do not need to pass an access_type to pmap_enter().
    232  */
    233 int
    234 vmapbuf(struct buf *bp, vsize_t len)
    235 {
    236 	vaddr_t faddr, taddr, off;
    237 	paddr_t fpa;
    238 
    239 
    240 #ifdef PMAP_DEBUG
    241 	if (pmap_debug_level >= 0)
    242 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    243 		    (u_int)bp->b_data, (u_int)len);
    244 #endif	/* PMAP_DEBUG */
    245 
    246 	if ((bp->b_flags & B_PHYS) == 0)
    247 		panic("vmapbuf");
    248 
    249 	bp->b_saveaddr = bp->b_data;
    250 	faddr = trunc_page((vaddr_t)bp->b_data);
    251 	off = (vaddr_t)bp->b_data - faddr;
    252 	len = round_page(off + len);
    253 	taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    254 	bp->b_data = (void *)(taddr + off);
    255 
    256 	/*
    257 	 * The region is locked, so we expect that pmap_pte() will return
    258 	 * non-NULL.
    259 	 */
    260 	while (len) {
    261 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    262 		    faddr, &fpa);
    263 		pmap_enter(pmap_kernel(), taddr, fpa,
    264 			VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    265 		faddr += PAGE_SIZE;
    266 		taddr += PAGE_SIZE;
    267 		len -= PAGE_SIZE;
    268 	}
    269 	pmap_update(pmap_kernel());
    270 
    271 	return 0;
    272 }
    273 
    274 /*
    275  * Unmap a previously-mapped user I/O request.
    276  */
    277 void
    278 vunmapbuf(struct buf *bp, vsize_t len)
    279 {
    280 	vaddr_t addr, off;
    281 
    282 #ifdef PMAP_DEBUG
    283 	if (pmap_debug_level >= 0)
    284 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    285 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    286 #endif	/* PMAP_DEBUG */
    287 
    288 	if ((bp->b_flags & B_PHYS) == 0)
    289 		panic("vunmapbuf");
    290 
    291 	/*
    292 	 * Make sure the cache does not have dirty data for the
    293 	 * pages we had mapped.
    294 	 */
    295 	addr = trunc_page((vaddr_t)bp->b_data);
    296 	off = (vaddr_t)bp->b_data - addr;
    297 	len = round_page(off + len);
    298 
    299 	pmap_remove(pmap_kernel(), addr, addr + len);
    300 	pmap_update(pmap_kernel());
    301 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
    302 	bp->b_data = bp->b_saveaddr;
    303 	bp->b_saveaddr = 0;
    304 }
    305 
    306 /* End of vm_machdep.c */
    307