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