Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.29
      1 /*	$NetBSD: vm_machdep.c,v 1.29 2003/07/15 00:24:42 lukem 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.29 2003/07/15 00:24:42 lukem 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	switch_exit	__P((struct lwp *l, struct lwp *l0,
     81 			     void (*)(struct lwp *)));
     82 extern void proc_trampoline	__P((void));
     83 
     84 /*
     85  * Special compilation symbols:
     86  *
     87  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
     88  *		 on forking and check the pattern on exit, reporting
     89  *		 the amount of stack used.
     90  */
     91 
     92 void
     93 cpu_proc_fork(p1, p2)
     94 	struct proc *p1, *p2;
     95 {
     96 
     97 #if defined(PERFCTRS)
     98 	if (PMC_ENABLED(p1))
     99 		pmc_md_fork(p1, p2);
    100 	else {
    101 		p2->p_md.pmc_enabled = 0;
    102 		p2->p_md.pmc_state = NULL;
    103 	}
    104 #endif
    105 }
    106 
    107 /*
    108  * Finish a fork operation, with process p2 nearly set up.
    109  * Copy and update the pcb and trap frame, making the child ready to run.
    110  *
    111  * Rig the child's kernel stack so that it will start out in
    112  * proc_trampoline() and call child_return() with p2 as an
    113  * argument. This causes the newly-created child process to go
    114  * directly to user level with an apparent return value of 0 from
    115  * fork(), while the parent process returns normally.
    116  *
    117  * p1 is the process being forked; if p1 == &proc0, we are creating
    118  * a kernel thread, and the return path and argument are specified with
    119  * `func' and `arg'.
    120  *
    121  * If an alternate user-level stack is requested (with non-zero values
    122  * in both the stack and stacksize args), set up the user stack pointer
    123  * accordingly.
    124  */
    125 void
    126 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg)
    127 	struct lwp *l1;
    128 	struct lwp *l2;
    129 	void *stack;
    130 	size_t stacksize;
    131 	void (*func) __P((void *));
    132 	void *arg;
    133 {
    134 	struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb;
    135 	struct trapframe *tf;
    136 	struct switchframe *sf;
    137 
    138 #ifdef PMAP_DEBUG
    139 	if (pmap_debug_level >= 0)
    140 		printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    141 #endif	/* PMAP_DEBUG */
    142 
    143 #if 0 /* XXX */
    144 	if (l1 == curlwp) {
    145 		/* Sync the PCB before we copy it. */
    146 		savectx(curpcb);
    147 	}
    148 #endif
    149 
    150 	/* Copy the pcb */
    151 	*pcb = l1->l_addr->u_pcb;
    152 
    153 	/*
    154 	 * Set up the undefined stack for the process.
    155 	 * Note: this stack is not in use if we are forking from p1
    156 	 */
    157 	pcb->pcb_un.un_32.pcb32_und_sp = (u_int)l2->l_addr +
    158 	    USPACE_UNDEF_STACK_TOP;
    159 	pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
    160 
    161 #ifdef STACKCHECKS
    162 	/* Fill the undefined stack with a known pattern */
    163 	memset(((u_char *)l2->l_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd,
    164 	    (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM));
    165 	/* Fill the kernel stack with a known pattern */
    166 	memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
    167 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    168 #endif	/* STACKCHECKS */
    169 
    170 #ifdef PMAP_DEBUG
    171 	if (pmap_debug_level >= 0) {
    172 		printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    173 		    l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
    174 		    l1->l_proc->p_vmspace->vm_map.pmap);
    175 		printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    176 		    l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
    177 		    l2->l_proc->p_vmspace->vm_map.pmap);
    178 	}
    179 #endif	/* PMAP_DEBUG */
    180 
    181 	pmap_activate(l2);
    182 
    183 #ifdef ARMFPE
    184 	/* Initialise a new FP context for p2 and copy the context from p1 */
    185 	arm_fpe_core_initcontext(FP_CONTEXT(l2));
    186 	arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
    187 #endif	/* ARMFPE */
    188 
    189 	l2->l_addr->u_pcb.pcb_tf = tf =
    190 	    (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
    191 	*tf = *l1->l_addr->u_pcb.pcb_tf;
    192 
    193 	/*
    194 	 * If specified, give the child a different stack.
    195 	 */
    196 	if (stack != NULL)
    197 		tf->tf_usr_sp = (u_int)stack + stacksize;
    198 
    199 	sf = (struct switchframe *)tf - 1;
    200 	sf->sf_spl = 0;		/* always equivalent to spl0() */
    201 	sf->sf_r4 = (u_int)func;
    202 	sf->sf_r5 = (u_int)arg;
    203 	sf->sf_pc = (u_int)proc_trampoline;
    204 	pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
    205 }
    206 
    207 void
    208 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
    209 {
    210 	struct pcb *pcb = &l->l_addr->u_pcb;
    211 	struct trapframe *tf = pcb->pcb_tf;
    212 	struct switchframe *sf = (struct switchframe *)tf - 1;
    213 
    214 	sf->sf_spl = 0;		/* always equivalent to spl0() */
    215 	sf->sf_r4 = (u_int)func;
    216 	sf->sf_r5 = (u_int)arg;
    217 	sf->sf_pc = (u_int)proc_trampoline;
    218 	pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
    219 }
    220 
    221 /*
    222  * cpu_exit is called as the last action during exit.
    223  *
    224  * We clean up a little and then call switch_exit() with the old proc as an
    225  * argument.  switch_exit() first switches to proc0's context, and finally
    226  * jumps into switch() to wait for another process to wake up.
    227  */
    228 
    229 void
    230 cpu_exit(struct lwp *l, int proc)
    231 {
    232 #ifdef ARMFPE
    233 	/* Abort any active FP operation and deactivate the context */
    234 	arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
    235 	arm_fpe_core_changecontext(0);
    236 #endif	/* ARMFPE */
    237 
    238 #ifdef STACKCHECKS
    239 	/* Report how much stack has been used - debugging */
    240 	if (l) {
    241 		u_char *ptr;
    242 		int loop;
    243 
    244 		ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM;
    245 		for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)
    246 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    247 		log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop);
    248 		ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
    249 		for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    250 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    251 		log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
    252 	}
    253 #endif	/* STACKCHECKS */
    254 	uvmexp.swtch++;
    255 	switch_exit(l, &lwp0, proc ? exit2 : lwp_exit2);
    256 }
    257 
    258 
    259 void
    260 cpu_swapin(l)
    261 	struct lwp *l;
    262 {
    263 #if 0
    264 	struct proc *p = l->l_proc;
    265 
    266 	/* Don't do this.  See the comment in cpu_swapout().  */
    267 #ifdef PMAP_DEBUG
    268 	if (pmap_debug_level >= 0)
    269 		printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
    270 		    p->p_comm, p->p_vmspace->vm_map.pmap);
    271 #endif	/* PMAP_DEBUG */
    272 
    273 	if (vector_page < KERNEL_BASE) {
    274 		/* Map the vector page */
    275 		pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
    276 		    systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    277 		pmap_update(p->p_vmspace->vm_map.pmap);
    278 	}
    279 #endif
    280 }
    281 
    282 
    283 void
    284 cpu_swapout(l)
    285 	struct lwp *l;
    286 {
    287 #if 0
    288 	struct proc *p = l->l_proc;
    289 
    290 	/*
    291 	 * Don't do this!  If the pmap is shared with another process,
    292 	 * it will loose it's page0 entry.  That's bad news indeed.
    293 	 */
    294 #ifdef PMAP_DEBUG
    295 	if (pmap_debug_level >= 0)
    296 		printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
    297 		    p->p_comm, &p->p_vmspace->vm_map.pmap);
    298 #endif	/* PMAP_DEBUG */
    299 
    300 	if (vector_page < KERNEL_BASE) {
    301 		/* Free the system page mapping */
    302 		pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
    303 		    vector_page + PAGE_SIZE);
    304 		pmap_update(p->p_vmspace->vm_map.pmap);
    305 	}
    306 #endif
    307 }
    308 
    309 
    310 /*
    311  * Move pages from one kernel virtual address to another.
    312  * Both addresses are assumed to reside in the Sysmap,
    313  * and size must be a multiple of PAGE_SIZE.
    314  */
    315 
    316 void
    317 pagemove(from, to, size)
    318 	caddr_t from, to;
    319 	size_t size;
    320 {
    321 	paddr_t pa;
    322 	boolean_t rv;
    323 
    324 	if (size % PAGE_SIZE)
    325 		panic("pagemove: size=%08lx", (u_long) size);
    326 
    327 	while (size > 0) {
    328 		rv = pmap_extract(pmap_kernel(), (vaddr_t) from, &pa);
    329 #ifdef DEBUG
    330 		if (rv == FALSE)
    331 			panic("pagemove 2");
    332 		if (pmap_extract(pmap_kernel(), (vaddr_t) to, NULL) == TRUE)
    333 			panic("pagemove 3");
    334 #endif
    335 		pmap_kremove((vaddr_t) from, PAGE_SIZE);
    336 		pmap_kenter_pa((vaddr_t) to, pa, VM_PROT_READ|VM_PROT_WRITE);
    337 		from += PAGE_SIZE;
    338 		to += PAGE_SIZE;
    339 		size -= PAGE_SIZE;
    340 	}
    341 	pmap_update(pmap_kernel());
    342 }
    343 
    344 /*
    345  * Map a user I/O request into kernel virtual address space.
    346  * Note: the pages are already locked by uvm_vslock(), so we
    347  * do not need to pass an access_type to pmap_enter().
    348  */
    349 void
    350 vmapbuf(bp, len)
    351 	struct buf *bp;
    352 	vsize_t len;
    353 {
    354 	vaddr_t faddr, taddr, off;
    355 	paddr_t fpa;
    356 
    357 
    358 #ifdef PMAP_DEBUG
    359 	if (pmap_debug_level >= 0)
    360 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    361 		    (u_int)bp->b_data, (u_int)len);
    362 #endif	/* PMAP_DEBUG */
    363 
    364 	if ((bp->b_flags & B_PHYS) == 0)
    365 		panic("vmapbuf");
    366 
    367 	faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data);
    368 	off = (vaddr_t)bp->b_data - faddr;
    369 	len = round_page(off + len);
    370 	taddr = uvm_km_valloc_wait(phys_map, len);
    371 	bp->b_data = (caddr_t)(taddr + off);
    372 
    373 	/*
    374 	 * The region is locked, so we expect that pmap_pte() will return
    375 	 * non-NULL.
    376 	 */
    377 	while (len) {
    378 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    379 		    faddr, &fpa);
    380 		pmap_enter(pmap_kernel(), taddr, fpa,
    381 			VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    382 		faddr += PAGE_SIZE;
    383 		taddr += PAGE_SIZE;
    384 		len -= PAGE_SIZE;
    385 	}
    386 	pmap_update(pmap_kernel());
    387 }
    388 
    389 /*
    390  * Unmap a previously-mapped user I/O request.
    391  */
    392 void
    393 vunmapbuf(bp, len)
    394 	struct buf *bp;
    395 	vsize_t len;
    396 {
    397 	vaddr_t addr, off;
    398 
    399 #ifdef PMAP_DEBUG
    400 	if (pmap_debug_level >= 0)
    401 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    402 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    403 #endif	/* PMAP_DEBUG */
    404 
    405 	if ((bp->b_flags & B_PHYS) == 0)
    406 		panic("vunmapbuf");
    407 
    408 	/*
    409 	 * Make sure the cache does not have dirty data for the
    410 	 * pages we had mapped.
    411 	 */
    412 	addr = trunc_page((vaddr_t)bp->b_data);
    413 	off = (vaddr_t)bp->b_data - addr;
    414 	len = round_page(off + len);
    415 
    416 	pmap_remove(pmap_kernel(), addr, addr + len);
    417 	pmap_update(pmap_kernel());
    418 	uvm_km_free_wakeup(phys_map, addr, len);
    419 	bp->b_data = bp->b_saveaddr;
    420 	bp->b_saveaddr = 0;
    421 }
    422 
    423 /* End of vm_machdep.c */
    424