Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.5
      1  1.5  toshii /*	$NetBSD: vm_machdep.c,v 1.5 2001/09/09 10:33:43 toshii Exp $	*/
      2  1.1   chris 
      3  1.1   chris /*
      4  1.1   chris  * Copyright (c) 1994-1998 Mark Brinicombe.
      5  1.1   chris  * Copyright (c) 1994 Brini.
      6  1.1   chris  * All rights reserved.
      7  1.1   chris  *
      8  1.1   chris  * This code is derived from software written for Brini by Mark Brinicombe
      9  1.1   chris  *
     10  1.1   chris  * Redistribution and use in source and binary forms, with or without
     11  1.1   chris  * modification, are permitted provided that the following conditions
     12  1.1   chris  * are met:
     13  1.1   chris  * 1. Redistributions of source code must retain the above copyright
     14  1.1   chris  *    notice, this list of conditions and the following disclaimer.
     15  1.1   chris  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   chris  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   chris  *    documentation and/or other materials provided with the distribution.
     18  1.1   chris  * 3. All advertising materials mentioning features or use of this software
     19  1.1   chris  *    must display the following acknowledgement:
     20  1.1   chris  *	This product includes software developed by Brini.
     21  1.1   chris  * 4. The name of the company nor the name of the author may be used to
     22  1.1   chris  *    endorse or promote products derived from this software without specific
     23  1.1   chris  *    prior written permission.
     24  1.1   chris  *
     25  1.1   chris  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     26  1.1   chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     27  1.1   chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  1.1   chris  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     29  1.1   chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     30  1.1   chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     31  1.1   chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1   chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1   chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1   chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1   chris  * SUCH DAMAGE.
     36  1.1   chris  *
     37  1.1   chris  * RiscBSD kernel project
     38  1.1   chris  *
     39  1.1   chris  * vm_machdep.h
     40  1.1   chris  *
     41  1.1   chris  * vm machine specific bits
     42  1.1   chris  *
     43  1.1   chris  * Created      : 08/10/94
     44  1.1   chris  */
     45  1.1   chris 
     46  1.1   chris #include "opt_armfpe.h"
     47  1.1   chris #include "opt_pmap_debug.h"
     48  1.1   chris 
     49  1.1   chris #include <sys/param.h>
     50  1.1   chris #include <sys/systm.h>
     51  1.1   chris #include <sys/proc.h>
     52  1.1   chris #include <sys/malloc.h>
     53  1.1   chris #include <sys/vnode.h>
     54  1.1   chris #include <sys/buf.h>
     55  1.1   chris #include <sys/user.h>
     56  1.1   chris #include <sys/exec.h>
     57  1.1   chris #include <sys/syslog.h>
     58  1.1   chris 
     59  1.1   chris #include <uvm/uvm_extern.h>
     60  1.1   chris 
     61  1.1   chris #include <machine/cpu.h>
     62  1.1   chris #include <machine/pmap.h>
     63  1.1   chris #include <machine/reg.h>
     64  1.1   chris #include <machine/vmparam.h>
     65  1.1   chris 
     66  1.1   chris #ifdef ARMFPE
     67  1.1   chris #include <arm32/fpe-arm/armfpe.h>
     68  1.1   chris #endif
     69  1.1   chris 
     70  1.1   chris extern pv_addr_t systempage;
     71  1.1   chris 
     72  1.1   chris int process_read_regs	__P((struct proc *p, struct reg *regs));
     73  1.1   chris int process_read_fpregs	__P((struct proc *p, struct fpreg *regs));
     74  1.1   chris 
     75  1.1   chris void	switch_exit	__P((struct proc *p, struct proc *proc0));
     76  1.1   chris extern void proc_trampoline	__P((void));
     77  1.1   chris 
     78  1.1   chris /*
     79  1.1   chris  * Special compilation symbols:
     80  1.1   chris  *
     81  1.1   chris  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
     82  1.1   chris  *		 on forking and check the pattern on exit, reporting
     83  1.1   chris  *		 the amount of stack used.
     84  1.1   chris  */
     85  1.1   chris 
     86  1.1   chris /*
     87  1.1   chris  * Finish a fork operation, with process p2 nearly set up.
     88  1.1   chris  * Copy and update the pcb and trap frame, making the child ready to run.
     89  1.1   chris  *
     90  1.1   chris  * Rig the child's kernel stack so that it will start out in
     91  1.1   chris  * proc_trampoline() and call child_return() with p2 as an
     92  1.1   chris  * argument. This causes the newly-created child process to go
     93  1.1   chris  * directly to user level with an apparent return value of 0 from
     94  1.1   chris  * fork(), while the parent process returns normally.
     95  1.1   chris  *
     96  1.1   chris  * p1 is the process being forked; if p1 == &proc0, we are creating
     97  1.1   chris  * a kernel thread, and the return path and argument are specified with
     98  1.1   chris  * `func' and `arg'.
     99  1.1   chris  *
    100  1.1   chris  * If an alternate user-level stack is requested (with non-zero values
    101  1.1   chris  * in both the stack and stacksize args), set up the user stack pointer
    102  1.1   chris  * accordingly.
    103  1.1   chris  */
    104  1.1   chris void
    105  1.1   chris cpu_fork(p1, p2, stack, stacksize, func, arg)
    106  1.1   chris 	struct proc *p1;
    107  1.1   chris 	struct proc *p2;
    108  1.1   chris 	void *stack;
    109  1.1   chris 	size_t stacksize;
    110  1.1   chris 	void (*func) __P((void *));
    111  1.1   chris 	void *arg;
    112  1.1   chris {
    113  1.1   chris 	struct pcb *pcb = (struct pcb *)&p2->p_addr->u_pcb;
    114  1.1   chris 	struct trapframe *tf;
    115  1.1   chris 	struct switchframe *sf;
    116  1.1   chris 
    117  1.1   chris #ifdef PMAP_DEBUG
    118  1.1   chris 	if (pmap_debug_level >= 0)
    119  1.1   chris 		printf("cpu_fork: %p %p %p %p\n", p1, p2, curproc, &proc0);
    120  1.1   chris #endif	/* PMAP_DEBUG */
    121  1.1   chris 
    122  1.1   chris #if 0 /* XXX */
    123  1.1   chris 	if (p1 == curproc) {
    124  1.1   chris 		/* Sync the PCB before we copy it. */
    125  1.1   chris 		savectx(curpcb);
    126  1.1   chris 	}
    127  1.1   chris #endif
    128  1.1   chris 
    129  1.1   chris 	/* Copy the pcb */
    130  1.1   chris 	*pcb = p1->p_addr->u_pcb;
    131  1.1   chris 
    132  1.1   chris 	/*
    133  1.1   chris 	 * Set up the undefined stack for the process.
    134  1.1   chris 	 * Note: this stack is not in use if we are forking from p1
    135  1.1   chris 	 */
    136  1.5  toshii 	pcb->pcb_un.un_32.pcb32_und_sp = (u_int)p2->p_addr +
    137  1.5  toshii 	    USPACE_UNDEF_STACK_TOP;
    138  1.5  toshii 	pcb->pcb_un.un_32.pcb32_sp = (u_int)p2->p_addr + USPACE_SVC_STACK_TOP;
    139  1.1   chris 
    140  1.1   chris #ifdef STACKCHECKS
    141  1.1   chris 	/* Fill the undefined stack with a known pattern */
    142  1.1   chris 	memset(((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd,
    143  1.1   chris 	    (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM));
    144  1.1   chris 	/* Fill the kernel stack with a known pattern */
    145  1.1   chris 	memset(((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
    146  1.1   chris 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    147  1.1   chris #endif	/* STACKCHECKS */
    148  1.1   chris 
    149  1.1   chris #ifdef PMAP_DEBUG
    150  1.1   chris 	if (pmap_debug_level >= 0) {
    151  1.1   chris 		printf("p1->procaddr=%p p1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    152  1.1   chris 		    p1->p_addr, &p1->p_addr->u_pcb, p1->p_pid,
    153  1.1   chris 		    p1->p_vmspace->vm_map.pmap);
    154  1.1   chris 		printf("p2->procaddr=%p p2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
    155  1.1   chris 		    p2->p_addr, &p2->p_addr->u_pcb, p2->p_pid,
    156  1.1   chris 		    p2->p_vmspace->vm_map.pmap);
    157  1.1   chris 	}
    158  1.1   chris #endif	/* PMAP_DEBUG */
    159  1.1   chris 
    160  1.1   chris 	pmap_activate(p2);
    161  1.1   chris 
    162  1.1   chris #ifdef ARMFPE
    163  1.1   chris 	/* Initialise a new FP context for p2 and copy the context from p1 */
    164  1.1   chris 	arm_fpe_core_initcontext(FP_CONTEXT(p2));
    165  1.1   chris 	arm_fpe_copycontext(FP_CONTEXT(p1), FP_CONTEXT(p2));
    166  1.1   chris #endif	/* ARMFPE */
    167  1.1   chris 
    168  1.5  toshii 	p2->p_addr->u_pcb.pcb_tf = tf =
    169  1.5  toshii 	    (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
    170  1.1   chris 	*tf = *p1->p_addr->u_pcb.pcb_tf;
    171  1.1   chris 
    172  1.1   chris 	/*
    173  1.1   chris 	 * If specified, give the child a different stack.
    174  1.1   chris 	 */
    175  1.1   chris 	if (stack != NULL)
    176  1.1   chris 		tf->tf_usr_sp = (u_int)stack + stacksize;
    177  1.1   chris 
    178  1.1   chris 	sf = (struct switchframe *)tf - 1;
    179  1.1   chris 	sf->sf_spl = _SPL_0;
    180  1.1   chris 	sf->sf_r4 = (u_int)func;
    181  1.1   chris 	sf->sf_r5 = (u_int)arg;
    182  1.1   chris 	sf->sf_pc = (u_int)proc_trampoline;
    183  1.5  toshii 	pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
    184  1.1   chris }
    185  1.1   chris 
    186  1.1   chris /*
    187  1.1   chris  * cpu_exit is called as the last action during exit.
    188  1.1   chris  *
    189  1.1   chris  * We clean up a little and then call switch_exit() with the old proc as an
    190  1.1   chris  * argument.  switch_exit() first switches to proc0's context, and finally
    191  1.1   chris  * jumps into switch() to wait for another process to wake up.
    192  1.1   chris  */
    193  1.1   chris 
    194  1.1   chris void
    195  1.1   chris cpu_exit(p)
    196  1.1   chris 	register struct proc *p;
    197  1.1   chris {
    198  1.1   chris #ifdef ARMFPE
    199  1.1   chris 	/* Abort any active FP operation and deactivate the context */
    200  1.1   chris 	arm_fpe_core_abort(FP_CONTEXT(p), NULL, NULL);
    201  1.1   chris 	arm_fpe_core_changecontext(0);
    202  1.1   chris #endif	/* ARMFPE */
    203  1.1   chris 
    204  1.1   chris #ifdef STACKCHECKS
    205  1.1   chris 	/* Report how much stack has been used - debugging */
    206  1.1   chris 	if (p) {
    207  1.1   chris 		u_char *ptr;
    208  1.1   chris 		int loop;
    209  1.1   chris 
    210  1.1   chris 		ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM;
    211  1.1   chris 		for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)
    212  1.1   chris 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    213  1.1   chris 		log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop);
    214  1.1   chris 		ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
    215  1.1   chris 		for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    216  1.1   chris 		    && *ptr == 0xdd; ++loop, ++ptr) ;
    217  1.1   chris 		log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
    218  1.1   chris 	}
    219  1.1   chris #endif	/* STACKCHECKS */
    220  1.1   chris 	uvmexp.swtch++;
    221  1.1   chris 	switch_exit(p, &proc0);
    222  1.1   chris }
    223  1.1   chris 
    224  1.1   chris 
    225  1.1   chris void
    226  1.1   chris cpu_swapin(p)
    227  1.1   chris 	struct proc *p;
    228  1.1   chris {
    229  1.1   chris 
    230  1.1   chris #ifdef PMAP_DEBUG
    231  1.1   chris 	if (pmap_debug_level >= 0)
    232  1.1   chris 		printf("cpu_swapin(%p, %d, %s, %p)\n", p, p->p_pid,
    233  1.1   chris 		    p->p_comm, p->p_vmspace->vm_map.pmap);
    234  1.1   chris #endif	/* PMAP_DEBUG */
    235  1.1   chris 
    236  1.1   chris 	/* Map the system page */
    237  1.1   chris 	pmap_enter(p->p_vmspace->vm_map.pmap, 0x00000000, systempage.pv_pa,
    238  1.1   chris 	    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    239  1.1   chris 	pmap_update();
    240  1.1   chris }
    241  1.1   chris 
    242  1.1   chris 
    243  1.1   chris void
    244  1.1   chris cpu_swapout(p)
    245  1.1   chris 	struct proc *p;
    246  1.1   chris {
    247  1.1   chris 
    248  1.1   chris #ifdef PMAP_DEBUG
    249  1.1   chris 	if (pmap_debug_level >= 0)
    250  1.1   chris 		printf("cpu_swapout(%p, %d, %s, %p)\n", p, p->p_pid,
    251  1.1   chris 		    p->p_comm, &p->p_vmspace->vm_map.pmap);
    252  1.1   chris #endif	/* PMAP_DEBUG */
    253  1.1   chris 
    254  1.1   chris 	/* Free the system page mapping */
    255  1.1   chris 	pmap_remove(p->p_vmspace->vm_map.pmap, 0x00000000, 0x00000000 + NBPG);
    256  1.1   chris 	pmap_update();
    257  1.1   chris }
    258  1.1   chris 
    259  1.1   chris 
    260  1.1   chris /*
    261  1.1   chris  * Move pages from one kernel virtual address to another.
    262  1.1   chris  * Both addresses are assumed to reside in the Sysmap,
    263  1.1   chris  * and size must be a multiple of CLSIZE.
    264  1.1   chris  */
    265  1.1   chris 
    266  1.1   chris void
    267  1.1   chris pagemove(from, to, size)
    268  1.1   chris 	caddr_t from, to;
    269  1.1   chris 	size_t size;
    270  1.1   chris {
    271  1.1   chris 	register pt_entry_t *fpte, *tpte;
    272  1.1   chris 
    273  1.1   chris 	if (size % NBPG)
    274  1.2    matt 		panic("pagemove: size=%08lx", (u_long) size);
    275  1.1   chris 
    276  1.1   chris #ifdef PMAP_DEBUG
    277  1.1   chris 	if (pmap_debug_level >= 0)
    278  1.2    matt 		printf("pagemove: V%p to %p size %08lx\n",
    279  1.2    matt 		    from, to, (u_long) size);
    280  1.1   chris #endif	/* PMAP_DEBUG */
    281  1.1   chris 
    282  1.1   chris 	fpte = vtopte((vaddr_t)from);
    283  1.1   chris 	tpte = vtopte((vaddr_t)to);
    284  1.1   chris 
    285  1.1   chris 	/*
    286  1.1   chris 	 * Make sure the cache does not have dirty data for the
    287  1.1   chris 	 * pages we are moving. Pages in the buffers are only
    288  1.1   chris 	 * ever moved with pagemove, so we only need to clean
    289  1.1   chris 	 * the 'from' area.
    290  1.1   chris 	 */
    291  1.1   chris 
    292  1.1   chris 	cpu_cache_purgeD_rng((u_int)from, size);
    293  1.1   chris 
    294  1.1   chris 	while (size > 0) {
    295  1.1   chris 		*tpte++ = *fpte;
    296  1.1   chris 		*fpte++ = 0;
    297  1.1   chris 		size -= NBPG;
    298  1.1   chris 	}
    299  1.1   chris 	//cpu_tlb_flushD();
    300  1.1   chris }
    301  1.1   chris 
    302  1.1   chris extern struct vm_map *phys_map;
    303  1.1   chris 
    304  1.1   chris /*
    305  1.1   chris  * Map a user I/O request into kernel virtual address space.
    306  1.1   chris  * Note: the pages are already locked by uvm_vslock(), so we
    307  1.1   chris  * do not need to pass an access_type to pmap_enter().
    308  1.1   chris  */
    309  1.1   chris void
    310  1.1   chris vmapbuf(bp, len)
    311  1.1   chris 	struct buf *bp;
    312  1.1   chris 	vsize_t len;
    313  1.1   chris {
    314  1.1   chris 	vaddr_t faddr, taddr, off;
    315  1.3   chris 	paddr_t fpa;
    316  1.3   chris 
    317  1.1   chris 
    318  1.1   chris #ifdef PMAP_DEBUG
    319  1.1   chris 	if (pmap_debug_level >= 0)
    320  1.1   chris 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    321  1.1   chris 		    (u_int)bp->b_data, (u_int)len);
    322  1.1   chris #endif	/* PMAP_DEBUG */
    323  1.1   chris 
    324  1.1   chris 	if ((bp->b_flags & B_PHYS) == 0)
    325  1.1   chris 		panic("vmapbuf");
    326  1.1   chris 
    327  1.1   chris 	taddr = uvm_km_valloc_wait(phys_map, len);
    328  1.1   chris 
    329  1.3   chris 	faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data);
    330  1.1   chris 	off = (vaddr_t)bp->b_data - faddr;
    331  1.1   chris 	len = round_page(off + len);
    332  1.1   chris 	bp->b_data = (caddr_t)(taddr + off);
    333  1.1   chris 
    334  1.1   chris 	/*
    335  1.1   chris 	 * The region is locked, so we expect that pmap_pte() will return
    336  1.1   chris 	 * non-NULL.
    337  1.1   chris 	 */
    338  1.3   chris 	while (len) {
    339  1.3   chris 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    340  1.3   chris 		    faddr, &fpa);
    341  1.4   chris 		pmap_enter(pmap_kernel(), taddr, fpa,
    342  1.4   chris 			VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
    343  1.3   chris 		faddr += PAGE_SIZE;
    344  1.3   chris 		taddr += PAGE_SIZE;
    345  1.3   chris 		len -= PAGE_SIZE;
    346  1.3   chris 	}
    347  1.3   chris 	pmap_update();
    348  1.1   chris }
    349  1.1   chris 
    350  1.1   chris /*
    351  1.1   chris  * Unmap a previously-mapped user I/O request.
    352  1.1   chris  */
    353  1.1   chris void
    354  1.1   chris vunmapbuf(bp, len)
    355  1.1   chris 	struct buf *bp;
    356  1.1   chris 	vsize_t len;
    357  1.1   chris {
    358  1.1   chris 	vaddr_t addr, off;
    359  1.1   chris 
    360  1.1   chris #ifdef PMAP_DEBUG
    361  1.1   chris 	if (pmap_debug_level >= 0)
    362  1.1   chris 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    363  1.1   chris 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    364  1.1   chris #endif	/* PMAP_DEBUG */
    365  1.1   chris 
    366  1.1   chris 	if ((bp->b_flags & B_PHYS) == 0)
    367  1.1   chris 		panic("vunmapbuf");
    368  1.1   chris 
    369  1.1   chris 	/*
    370  1.1   chris 	 * Make sure the cache does not have dirty data for the
    371  1.1   chris 	 * pages we had mapped.
    372  1.1   chris 	 */
    373  1.1   chris 	addr = trunc_page((vaddr_t)bp->b_data);
    374  1.1   chris 	off = (vaddr_t)bp->b_data - addr;
    375  1.1   chris 	len = round_page(off + len);
    376  1.4   chris 
    377  1.4   chris 	pmap_remove(pmap_kernel(), addr, addr + len);
    378  1.3   chris 	pmap_update();
    379  1.3   chris 	uvm_km_free_wakeup(phys_map, addr, len);
    380  1.1   chris 	bp->b_data = bp->b_saveaddr;
    381  1.1   chris 	bp->b_saveaddr = 0;
    382  1.1   chris }
    383  1.1   chris 
    384  1.1   chris /* End of vm_machdep.c */
    385