Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.41
      1 /*	$NetBSD: vm_machdep.c,v 1.41 2008/01/12 20:50:24 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.41 2008/01/12 20:50:24 skrll Exp $");
     48 
     49 #include "opt_armfpe.h"
     50 #include "opt_pmap_debug.h"
     51 #include "opt_perfctrs.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/proc.h>
     56 #include <sys/malloc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/buf.h>
     59 #include <sys/pmc.h>
     60 #include <sys/user.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	__P((struct proc *p, struct reg *regs));
     78 int process_read_fpregs	__P((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(p1, p2)
     92 	struct proc *p1, *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 process p2 nearly set up.
    107  * Copy and update the pcb and trap frame, making the child ready to run.
    108  *
    109  * Rig the child's kernel stack so that it will start out in
    110  * proc_trampoline() and call child_return() with p2 as an
    111  * argument. This causes the newly-created child process to go
    112  * directly to user level with an apparent return value of 0 from
    113  * fork(), while the parent process returns normally.
    114  *
    115  * p1 is the process being forked; if p1 == &proc0, we are creating
    116  * a kernel thread, and the return path and argument are specified with
    117  * `func' and `arg'.
    118  *
    119  * If an alternate user-level stack is requested (with non-zero values
    120  * in both the stack and stacksize args), set up the user stack pointer
    121  * accordingly.
    122  */
    123 void
    124 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    125     void (*func)(void *), void *arg)
    126 {
    127 	struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb;
    128 	struct trapframe *tf;
    129 	struct switchframe *sf;
    130 
    131 #ifdef PMAP_DEBUG
    132 	if (pmap_debug_level >= 0)
    133 		printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    134 #endif	/* PMAP_DEBUG */
    135 
    136 #if 0 /* XXX */
    137 	if (l1 == curlwp) {
    138 		/* Sync the PCB before we copy it. */
    139 		savectx(curpcb);
    140 	}
    141 #endif
    142 
    143 	/* Copy the pcb */
    144 	*pcb = l1->l_addr->u_pcb;
    145 
    146 	/*
    147 	 * Set up the undefined stack for the process.
    148 	 * Note: this stack is not in use if we are forking from p1
    149 	 */
    150 	pcb->pcb_un.un_32.pcb32_und_sp = (u_int)l2->l_addr +
    151 	    USPACE_UNDEF_STACK_TOP;
    152 	pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
    153 
    154 #ifdef STACKCHECKS
    155 	/* Fill the undefined stack with a known pattern */
    156 	memset(((u_char *)l2->l_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd,
    157 	    (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM));
    158 	/* Fill the kernel stack with a known pattern */
    159 	memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
    160 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    161 #endif	/* STACKCHECKS */
    162 
    163 #ifdef PMAP_DEBUG
    164 	if (pmap_debug_level >= 0) {
    165 		printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    166 		    l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
    167 		    l1->l_proc->p_vmspace->vm_map.pmap);
    168 		printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    169 		    l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
    170 		    l2->l_proc->p_vmspace->vm_map.pmap);
    171 	}
    172 #endif	/* PMAP_DEBUG */
    173 
    174 #ifdef ARMFPE
    175 	/* Initialise a new FP context for p2 and copy the context from p1 */
    176 	arm_fpe_core_initcontext(FP_CONTEXT(l2));
    177 	arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
    178 #endif	/* ARMFPE */
    179 
    180 	l2->l_addr->u_pcb.pcb_tf = tf =
    181 	    (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
    182 	*tf = *l1->l_addr->u_pcb.pcb_tf;
    183 
    184 	/*
    185 	 * If specified, give the child a different stack.
    186 	 */
    187 	if (stack != NULL)
    188 		tf->tf_usr_sp = (u_int)stack + stacksize;
    189 
    190 	sf = (struct switchframe *)tf - 1;
    191 	sf->sf_r4 = (u_int)func;
    192 	sf->sf_r5 = (u_int)arg;
    193 	sf->sf_sp = (u_int)tf;
    194 	sf->sf_pc = (u_int)lwp_trampoline;
    195 	pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
    196 }
    197 
    198 /*
    199  * cpu_exit is called as the last action during exit.
    200  *
    201  * We clean up a little and then call switch_exit() with the old proc as an
    202  * argument.  switch_exit() first switches to proc0's context, and finally
    203  * jumps into switch() to wait for another process to wake up.
    204  */
    205 
    206 void
    207 cpu_lwp_free(struct lwp *l, int proc)
    208 {
    209 #ifdef ARMFPE
    210 	/* Abort any active FP operation and deactivate the context */
    211 	arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
    212 	arm_fpe_core_changecontext(0);
    213 #endif	/* ARMFPE */
    214 
    215 #ifdef STACKCHECKS
    216 	/* Report how much stack has been used - debugging */
    217 	if (l) {
    218 		u_char *ptr;
    219 		int loop;
    220 
    221 		ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM;
    222 		for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)
    223 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    224 		log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop);
    225 		ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
    226 		for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    227 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    228 		log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
    229 	}
    230 #endif	/* STACKCHECKS */
    231 }
    232 
    233 void
    234 cpu_lwp_free2(struct lwp *l)
    235 {
    236 }
    237 
    238 void
    239 cpu_swapin(l)
    240 	struct lwp *l;
    241 {
    242 #if 0
    243 	struct proc *p = l->l_proc;
    244 
    245 	/* Don't do this.  See the comment in cpu_swapout().  */
    246 #ifdef PMAP_DEBUG
    247 	if (pmap_debug_level >= 0)
    248 		printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
    249 		    p->p_comm, p->p_vmspace->vm_map.pmap);
    250 #endif	/* PMAP_DEBUG */
    251 
    252 	if (vector_page < KERNEL_BASE) {
    253 		/* Map the vector page */
    254 		pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
    255 		    systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    256 		pmap_update(p->p_vmspace->vm_map.pmap);
    257 	}
    258 #endif
    259 }
    260 
    261 
    262 void
    263 cpu_swapout(l)
    264 	struct lwp *l;
    265 {
    266 #if 0
    267 	struct proc *p = l->l_proc;
    268 
    269 	/*
    270 	 * Don't do this!  If the pmap is shared with another process,
    271 	 * it will loose it's page0 entry.  That's bad news indeed.
    272 	 */
    273 #ifdef PMAP_DEBUG
    274 	if (pmap_debug_level >= 0)
    275 		printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
    276 		    p->p_comm, &p->p_vmspace->vm_map.pmap);
    277 #endif	/* PMAP_DEBUG */
    278 
    279 	if (vector_page < KERNEL_BASE) {
    280 		/* Free the system page mapping */
    281 		pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
    282 		    vector_page + PAGE_SIZE);
    283 		pmap_update(p->p_vmspace->vm_map.pmap);
    284 	}
    285 #endif
    286 }
    287 
    288 /*
    289  * Map a user I/O request into kernel virtual address space.
    290  * Note: the pages are already locked by uvm_vslock(), so we
    291  * do not need to pass an access_type to pmap_enter().
    292  */
    293 void
    294 vmapbuf(bp, len)
    295 	struct buf *bp;
    296 	vsize_t len;
    297 {
    298 	vaddr_t faddr, taddr, off;
    299 	paddr_t fpa;
    300 
    301 
    302 #ifdef PMAP_DEBUG
    303 	if (pmap_debug_level >= 0)
    304 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    305 		    (u_int)bp->b_data, (u_int)len);
    306 #endif	/* PMAP_DEBUG */
    307 
    308 	if ((bp->b_flags & B_PHYS) == 0)
    309 		panic("vmapbuf");
    310 
    311 	bp->b_saveaddr = bp->b_data;
    312 	faddr = trunc_page((vaddr_t)bp->b_data);
    313 	off = (vaddr_t)bp->b_data - faddr;
    314 	len = round_page(off + len);
    315 	taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    316 	bp->b_data = (void *)(taddr + off);
    317 
    318 	/*
    319 	 * The region is locked, so we expect that pmap_pte() will return
    320 	 * non-NULL.
    321 	 */
    322 	while (len) {
    323 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    324 		    faddr, &fpa);
    325 		pmap_enter(pmap_kernel(), taddr, fpa,
    326 			VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    327 		faddr += PAGE_SIZE;
    328 		taddr += PAGE_SIZE;
    329 		len -= PAGE_SIZE;
    330 	}
    331 	pmap_update(pmap_kernel());
    332 }
    333 
    334 /*
    335  * Unmap a previously-mapped user I/O request.
    336  */
    337 void
    338 vunmapbuf(bp, len)
    339 	struct buf *bp;
    340 	vsize_t len;
    341 {
    342 	vaddr_t addr, off;
    343 
    344 #ifdef PMAP_DEBUG
    345 	if (pmap_debug_level >= 0)
    346 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    347 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    348 #endif	/* PMAP_DEBUG */
    349 
    350 	if ((bp->b_flags & B_PHYS) == 0)
    351 		panic("vunmapbuf");
    352 
    353 	/*
    354 	 * Make sure the cache does not have dirty data for the
    355 	 * pages we had mapped.
    356 	 */
    357 	addr = trunc_page((vaddr_t)bp->b_data);
    358 	off = (vaddr_t)bp->b_data - addr;
    359 	len = round_page(off + len);
    360 
    361 	pmap_remove(pmap_kernel(), addr, addr + len);
    362 	pmap_update(pmap_kernel());
    363 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
    364 	bp->b_data = bp->b_saveaddr;
    365 	bp->b_saveaddr = 0;
    366 }
    367 
    368 /* End of vm_machdep.c */
    369