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