Home | History | Annotate | Line # | Download | only in arm32
vm_machdep.c revision 1.62
      1  1.62      matt /*	$NetBSD: vm_machdep.c,v 1.62 2012/12/05 19:05:45 matt 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.29     lukem 
     46  1.29     lukem #include <sys/cdefs.h>
     47  1.62      matt __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.62 2012/12/05 19:05:45 matt Exp $");
     48   1.1     chris 
     49   1.1     chris #include "opt_armfpe.h"
     50   1.1     chris #include "opt_pmap_debug.h"
     51  1.18    briggs #include "opt_perfctrs.h"
     52  1.43  rearnsha #include "opt_cputypes.h"
     53   1.1     chris 
     54   1.1     chris #include <sys/param.h>
     55   1.1     chris #include <sys/systm.h>
     56   1.1     chris #include <sys/proc.h>
     57   1.1     chris #include <sys/malloc.h>
     58   1.1     chris #include <sys/vnode.h>
     59  1.59      matt #include <sys/cpu.h>
     60   1.1     chris #include <sys/buf.h>
     61  1.18    briggs #include <sys/pmc.h>
     62   1.1     chris #include <sys/exec.h>
     63   1.1     chris #include <sys/syslog.h>
     64   1.1     chris 
     65   1.1     chris #include <uvm/uvm_extern.h>
     66   1.1     chris 
     67  1.59      matt #include <machine/pcb.h>
     68   1.1     chris #include <machine/pmap.h>
     69   1.1     chris #include <machine/reg.h>
     70   1.1     chris #include <machine/vmparam.h>
     71   1.1     chris 
     72   1.1     chris extern pv_addr_t systempage;
     73   1.1     chris 
     74  1.47       dsl int process_read_regs(struct proc *p, struct reg *regs);
     75  1.47       dsl int process_read_fpregs(struct proc *p, struct fpreg *regs);
     76   1.1     chris 
     77  1.38      yamt void lwp_trampoline(void);
     78   1.1     chris 
     79   1.1     chris /*
     80   1.1     chris  * Special compilation symbols:
     81   1.1     chris  *
     82   1.1     chris  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
     83   1.1     chris  *		 on forking and check the pattern on exit, reporting
     84   1.1     chris  *		 the amount of stack used.
     85   1.1     chris  */
     86   1.1     chris 
     87  1.24   thorpej void
     88  1.49       dsl cpu_proc_fork(struct proc *p1, struct proc *p2)
     89  1.24   thorpej {
     90  1.24   thorpej 
     91  1.24   thorpej #if defined(PERFCTRS)
     92  1.24   thorpej 	if (PMC_ENABLED(p1))
     93  1.24   thorpej 		pmc_md_fork(p1, p2);
     94  1.24   thorpej 	else {
     95  1.24   thorpej 		p2->p_md.pmc_enabled = 0;
     96  1.24   thorpej 		p2->p_md.pmc_state = NULL;
     97  1.24   thorpej 	}
     98  1.24   thorpej #endif
     99  1.24   thorpej }
    100  1.24   thorpej 
    101   1.1     chris /*
    102  1.44     skrll  * Finish a fork operation, with LWP l2 nearly set up.
    103  1.44     skrll  *
    104  1.44     skrll  * Copy and update the pcb and trapframe, making the child ready to run.
    105   1.1     chris  *
    106   1.1     chris  * Rig the child's kernel stack so that it will start out in
    107  1.44     skrll  * lwp_trampoline() which will call the specified func with the argument arg.
    108   1.1     chris  *
    109   1.1     chris  * If an alternate user-level stack is requested (with non-zero values
    110   1.1     chris  * in both the stack and stacksize args), set up the user stack pointer
    111   1.1     chris  * accordingly.
    112   1.1     chris  */
    113   1.1     chris void
    114  1.38      yamt cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    115  1.38      yamt     void (*func)(void *), void *arg)
    116   1.1     chris {
    117  1.51     rmind 	struct pcb *pcb1, *pcb2;
    118  1.56     rmind 	struct switchframe *sf;
    119  1.53     rmind 	vaddr_t uv;
    120   1.1     chris 
    121  1.51     rmind 	pcb1 = lwp_getpcb(l1);
    122  1.51     rmind 	pcb2 = lwp_getpcb(l2);
    123  1.51     rmind 
    124   1.1     chris #ifdef PMAP_DEBUG
    125   1.1     chris 	if (pmap_debug_level >= 0)
    126  1.24   thorpej 		printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    127   1.1     chris #endif	/* PMAP_DEBUG */
    128   1.1     chris 
    129   1.1     chris #if 0 /* XXX */
    130  1.24   thorpej 	if (l1 == curlwp) {
    131   1.1     chris 		/* Sync the PCB before we copy it. */
    132   1.1     chris 		savectx(curpcb);
    133   1.1     chris 	}
    134  1.18    briggs #endif
    135  1.18    briggs 
    136  1.58      matt 	l2->l_md.md_flags = l1->l_md.md_flags & MDLWP_VFPUSED;
    137  1.43  rearnsha 
    138   1.1     chris 	/* Copy the pcb */
    139  1.51     rmind 	*pcb2 = *pcb1;
    140   1.1     chris 
    141   1.1     chris 	/*
    142  1.53     rmind 	 * Set up the kernel stack for the process.
    143   1.1     chris 	 * Note: this stack is not in use if we are forking from p1
    144   1.1     chris 	 */
    145  1.53     rmind 	uv = uvm_lwp_getuarea(l2);
    146  1.59      matt 	pcb2->pcb_sp = uv + USPACE_SVC_STACK_TOP;
    147   1.1     chris 
    148   1.1     chris #ifdef STACKCHECKS
    149   1.1     chris 	/* Fill the kernel stack with a known pattern */
    150  1.53     rmind 	memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
    151   1.1     chris 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    152   1.1     chris #endif	/* STACKCHECKS */
    153   1.1     chris 
    154   1.1     chris #ifdef PMAP_DEBUG
    155   1.1     chris 	if (pmap_debug_level >= 0) {
    156  1.51     rmind 		printf("l1: pcb=%p pid=%d pmap=%p\n",
    157  1.51     rmind 		    pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
    158  1.51     rmind 		printf("l2: pcb=%p pid=%d pmap=%p\n",
    159  1.51     rmind 		    pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
    160   1.1     chris 	}
    161   1.1     chris #endif	/* PMAP_DEBUG */
    162   1.1     chris 
    163  1.59      matt 	struct trapframe *tf = (struct trapframe *)pcb2->pcb_sp - 1;
    164  1.59      matt 	lwp_settrapframe(l2, tf);
    165  1.59      matt 	*tf = *lwp_trapframe(l1);
    166   1.1     chris 
    167   1.1     chris 	/*
    168  1.57      matt 	 * If specified, give the child a different stack (make sure
    169  1.57      matt 	 * it's 8-byte aligned).
    170   1.1     chris 	 */
    171   1.1     chris 	if (stack != NULL)
    172  1.57      matt 		tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
    173   1.1     chris 
    174  1.56     rmind 	sf = (struct switchframe *)tf - 1;
    175  1.56     rmind 	sf->sf_r4 = (u_int)func;
    176  1.56     rmind 	sf->sf_r5 = (u_int)arg;
    177  1.60      matt 	sf->sf_r7 = PSR_USR32_MODE;		/* for returning to userspace */
    178  1.56     rmind 	sf->sf_sp = (u_int)tf;
    179  1.56     rmind 	sf->sf_pc = (u_int)lwp_trampoline;
    180  1.59      matt 	pcb2->pcb_sp = (u_int)sf;
    181  1.24   thorpej }
    182  1.24   thorpej 
    183   1.1     chris /*
    184   1.1     chris  * cpu_exit is called as the last action during exit.
    185   1.1     chris  *
    186   1.1     chris  * We clean up a little and then call switch_exit() with the old proc as an
    187  1.52      matt  * argument.  switch_exit() first switches to lwp0's context, and finally
    188   1.1     chris  * jumps into switch() to wait for another process to wake up.
    189   1.1     chris  */
    190   1.1     chris 
    191   1.1     chris void
    192  1.31  jdolecek cpu_lwp_free(struct lwp *l, int proc)
    193   1.1     chris {
    194   1.1     chris #ifdef STACKCHECKS
    195   1.1     chris 	/* Report how much stack has been used - debugging */
    196  1.62      matt 	struct pcb * const pcb = lwp_getpcb(l);
    197  1.62      matt 	u_char *ptr;
    198  1.62      matt 	u_int loop;
    199  1.62      matt 
    200  1.62      matt 	ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
    201  1.62      matt 	for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    202  1.62      matt 	    && *ptr == 0xdd; ++loop, ++ptr) ;
    203  1.62      matt 	log(LOG_INFO, "%u bytes of svc stack fill pattern\n", loop);
    204   1.1     chris #endif	/* STACKCHECKS */
    205   1.1     chris }
    206   1.1     chris 
    207  1.31  jdolecek void
    208  1.36      matt cpu_lwp_free2(struct lwp *l)
    209  1.36      matt {
    210  1.36      matt }
    211  1.36      matt 
    212   1.1     chris /*
    213   1.1     chris  * Map a user I/O request into kernel virtual address space.
    214   1.1     chris  * Note: the pages are already locked by uvm_vslock(), so we
    215   1.1     chris  * do not need to pass an access_type to pmap_enter().
    216   1.1     chris  */
    217  1.55     pooka int
    218  1.48       dsl vmapbuf(struct buf *bp, vsize_t len)
    219   1.1     chris {
    220   1.1     chris 	vaddr_t faddr, taddr, off;
    221   1.3     chris 	paddr_t fpa;
    222   1.3     chris 
    223   1.1     chris 
    224   1.1     chris #ifdef PMAP_DEBUG
    225   1.1     chris 	if (pmap_debug_level >= 0)
    226   1.1     chris 		printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    227   1.1     chris 		    (u_int)bp->b_data, (u_int)len);
    228   1.1     chris #endif	/* PMAP_DEBUG */
    229   1.1     chris 
    230   1.1     chris 	if ((bp->b_flags & B_PHYS) == 0)
    231   1.1     chris 		panic("vmapbuf");
    232   1.1     chris 
    233  1.35     skrll 	bp->b_saveaddr = bp->b_data;
    234  1.35     skrll 	faddr = trunc_page((vaddr_t)bp->b_data);
    235   1.1     chris 	off = (vaddr_t)bp->b_data - faddr;
    236   1.1     chris 	len = round_page(off + len);
    237  1.61      matt 	taddr = uvm_km_alloc(phys_map, len, atop(faddr) & uvmexp.colormask,
    238  1.61      matt 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
    239  1.37  christos 	bp->b_data = (void *)(taddr + off);
    240   1.1     chris 
    241   1.1     chris 	/*
    242   1.1     chris 	 * The region is locked, so we expect that pmap_pte() will return
    243   1.1     chris 	 * non-NULL.
    244   1.1     chris 	 */
    245   1.3     chris 	while (len) {
    246   1.3     chris 		(void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
    247   1.3     chris 		    faddr, &fpa);
    248   1.4     chris 		pmap_enter(pmap_kernel(), taddr, fpa,
    249  1.23     chris 			VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    250   1.3     chris 		faddr += PAGE_SIZE;
    251   1.3     chris 		taddr += PAGE_SIZE;
    252   1.3     chris 		len -= PAGE_SIZE;
    253   1.3     chris 	}
    254   1.6     chris 	pmap_update(pmap_kernel());
    255  1.55     pooka 
    256  1.55     pooka 	return 0;
    257   1.1     chris }
    258   1.1     chris 
    259   1.1     chris /*
    260   1.1     chris  * Unmap a previously-mapped user I/O request.
    261   1.1     chris  */
    262   1.1     chris void
    263  1.48       dsl vunmapbuf(struct buf *bp, vsize_t len)
    264   1.1     chris {
    265   1.1     chris 	vaddr_t addr, off;
    266   1.1     chris 
    267   1.1     chris #ifdef PMAP_DEBUG
    268   1.1     chris 	if (pmap_debug_level >= 0)
    269   1.1     chris 		printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    270   1.1     chris 		    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    271   1.1     chris #endif	/* PMAP_DEBUG */
    272   1.1     chris 
    273   1.1     chris 	if ((bp->b_flags & B_PHYS) == 0)
    274   1.1     chris 		panic("vunmapbuf");
    275   1.1     chris 
    276   1.1     chris 	/*
    277   1.1     chris 	 * Make sure the cache does not have dirty data for the
    278   1.1     chris 	 * pages we had mapped.
    279   1.1     chris 	 */
    280   1.1     chris 	addr = trunc_page((vaddr_t)bp->b_data);
    281   1.1     chris 	off = (vaddr_t)bp->b_data - addr;
    282   1.1     chris 	len = round_page(off + len);
    283   1.4     chris 
    284   1.4     chris 	pmap_remove(pmap_kernel(), addr, addr + len);
    285   1.6     chris 	pmap_update(pmap_kernel());
    286  1.33      yamt 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
    287   1.1     chris 	bp->b_data = bp->b_saveaddr;
    288   1.1     chris 	bp->b_saveaddr = 0;
    289   1.1     chris }
    290   1.1     chris 
    291   1.1     chris /* End of vm_machdep.c */
    292