Home | History | Annotate | Line # | Download | only in arm32
      1  1.79    andvar /*	$NetBSD: vm_machdep.c,v 1.79 2024/01/15 20:35:22 andvar 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.79    andvar __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.79 2024/01/15 20:35:22 andvar Exp $");
     48   1.1     chris 
     49   1.1     chris #include "opt_armfpe.h"
     50  1.43  rearnsha #include "opt_cputypes.h"
     51   1.1     chris 
     52   1.1     chris #include <sys/param.h>
     53  1.77     skrll 
     54  1.77     skrll #include <sys/buf.h>
     55  1.77     skrll #include <sys/cpu.h>
     56  1.77     skrll #include <sys/exec.h>
     57  1.77     skrll #include <sys/proc.h>
     58  1.79    andvar #ifdef STACKCHECKS
     59  1.79    andvar #include <sys/syslog.h>
     60  1.79    andvar #endif
     61   1.1     chris #include <sys/systm.h>
     62   1.1     chris #include <sys/vnode.h>
     63   1.1     chris 
     64   1.1     chris #include <uvm/uvm_extern.h>
     65   1.1     chris 
     66  1.64      matt #include <arm/vfpreg.h>
     67  1.64      matt 
     68  1.59      matt #include <machine/pcb.h>
     69   1.1     chris #include <machine/pmap.h>
     70   1.1     chris #include <machine/reg.h>
     71   1.1     chris #include <machine/vmparam.h>
     72   1.1     chris 
     73  1.38      yamt void lwp_trampoline(void);
     74   1.1     chris 
     75   1.1     chris /*
     76   1.1     chris  * Special compilation symbols:
     77   1.1     chris  *
     78   1.1     chris  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
     79   1.1     chris  *		 on forking and check the pattern on exit, reporting
     80   1.1     chris  *		 the amount of stack used.
     81   1.1     chris  */
     82   1.1     chris 
     83  1.24   thorpej void
     84  1.49       dsl cpu_proc_fork(struct proc *p1, struct proc *p2)
     85  1.24   thorpej {
     86  1.68      matt 	/*
     87  1.68      matt 	 * Copy machine arch string (it's small so just memcpy it).
     88  1.68      matt 	 */
     89  1.68      matt 	memcpy(p2->p_md.md_march, p1->p_md.md_march, sizeof(p2->p_md.md_march));
     90  1.24   thorpej }
     91  1.24   thorpej 
     92   1.1     chris /*
     93  1.44     skrll  * Finish a fork operation, with LWP l2 nearly set up.
     94  1.44     skrll  *
     95  1.44     skrll  * Copy and update the pcb and trapframe, making the child ready to run.
     96  1.66     skrll  *
     97   1.1     chris  * Rig the child's kernel stack so that it will start out in
     98  1.44     skrll  * lwp_trampoline() which will call the specified func with the argument 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.38      yamt cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    106  1.38      yamt     void (*func)(void *), void *arg)
    107   1.1     chris {
    108  1.56     rmind 	struct switchframe *sf;
    109  1.53     rmind 	vaddr_t uv;
    110   1.1     chris 
    111  1.64      matt 	const struct pcb * const pcb1 = lwp_getpcb(l1);
    112  1.64      matt 	struct pcb * const pcb2 = lwp_getpcb(l2);
    113  1.51     rmind 
    114  1.76     skrll #ifdef XXXDEBUG
    115  1.76     skrll 	printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
    116  1.76     skrll #endif	/* DEBUG */
    117   1.1     chris 
    118  1.64      matt 	/* Copy the pcb */
    119  1.64      matt 	*pcb2 = *pcb1;
    120  1.18    briggs 
    121  1.64      matt #ifdef FPU_VFP
    122  1.64      matt 	/*
    123  1.64      matt 	 * Disable the VFP for a newly created LWP but remember if the
    124  1.64      matt 	 * VFP state is valid.
    125  1.64      matt 	 */
    126  1.64      matt 	pcb2->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
    127  1.64      matt #endif
    128   1.1     chris 
    129  1.66     skrll 	/*
    130  1.53     rmind 	 * Set up the kernel stack for the process.
    131   1.1     chris 	 * Note: this stack is not in use if we are forking from p1
    132   1.1     chris 	 */
    133  1.53     rmind 	uv = uvm_lwp_getuarea(l2);
    134  1.63      matt 	pcb2->pcb_ksp = uv + USPACE_SVC_STACK_TOP;
    135   1.1     chris 
    136   1.1     chris #ifdef STACKCHECKS
    137   1.1     chris 	/* Fill the kernel stack with a known pattern */
    138  1.53     rmind 	memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
    139   1.1     chris 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
    140   1.1     chris #endif	/* STACKCHECKS */
    141   1.1     chris 
    142  1.76     skrll #ifdef XXXDEBUG
    143  1.76     skrll 	printf("l1: pcb=%p pid=%d pmap=%p\n",
    144  1.76     skrll 	    pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
    145  1.76     skrll 	printf("l2: pcb=%p pid=%d pmap=%p\n",
    146  1.76     skrll 	    pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
    147  1.76     skrll #endif	/* DEBUG */
    148   1.1     chris 
    149  1.63      matt 	struct trapframe *tf = (struct trapframe *)pcb2->pcb_ksp - 1;
    150  1.59      matt 	lwp_settrapframe(l2, tf);
    151  1.59      matt 	*tf = *lwp_trapframe(l1);
    152   1.1     chris 
    153   1.1     chris 	/*
    154  1.57      matt 	 * If specified, give the child a different stack (make sure
    155  1.57      matt 	 * it's 8-byte aligned).
    156   1.1     chris 	 */
    157   1.1     chris 	if (stack != NULL)
    158  1.57      matt 		tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
    159   1.1     chris 
    160  1.56     rmind 	sf = (struct switchframe *)tf - 1;
    161  1.56     rmind 	sf->sf_r4 = (u_int)func;
    162  1.56     rmind 	sf->sf_r5 = (u_int)arg;
    163  1.60      matt 	sf->sf_r7 = PSR_USR32_MODE;		/* for returning to userspace */
    164  1.56     rmind 	sf->sf_sp = (u_int)tf;
    165  1.56     rmind 	sf->sf_pc = (u_int)lwp_trampoline;
    166  1.63      matt 	pcb2->pcb_ksp = (u_int)sf;
    167  1.24   thorpej }
    168  1.24   thorpej 
    169   1.1     chris /*
    170   1.1     chris  * cpu_exit is called as the last action during exit.
    171   1.1     chris  *
    172   1.1     chris  * We clean up a little and then call switch_exit() with the old proc as an
    173  1.52      matt  * argument.  switch_exit() first switches to lwp0's context, and finally
    174   1.1     chris  * jumps into switch() to wait for another process to wake up.
    175   1.1     chris  */
    176   1.1     chris 
    177   1.1     chris void
    178  1.31  jdolecek cpu_lwp_free(struct lwp *l, int proc)
    179   1.1     chris {
    180   1.1     chris #ifdef STACKCHECKS
    181   1.1     chris 	/* Report how much stack has been used - debugging */
    182  1.62      matt 	struct pcb * const pcb = lwp_getpcb(l);
    183  1.62      matt 	u_char *ptr;
    184  1.62      matt 	u_int loop;
    185  1.62      matt 
    186  1.62      matt 	ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
    187  1.62      matt 	for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
    188  1.62      matt 	    && *ptr == 0xdd; ++loop, ++ptr) ;
    189  1.62      matt 	log(LOG_INFO, "%u bytes of svc stack fill pattern\n", loop);
    190   1.1     chris #endif	/* STACKCHECKS */
    191   1.1     chris }
    192   1.1     chris 
    193  1.31  jdolecek void
    194  1.36      matt cpu_lwp_free2(struct lwp *l)
    195  1.36      matt {
    196  1.36      matt }
    197  1.36      matt 
    198   1.1     chris /*
    199   1.1     chris  * Map a user I/O request into kernel virtual address space.
    200   1.1     chris  * Note: the pages are already locked by uvm_vslock(), so we
    201   1.1     chris  * do not need to pass an access_type to pmap_enter().
    202   1.1     chris  */
    203  1.55     pooka int
    204  1.48       dsl vmapbuf(struct buf *bp, vsize_t len)
    205   1.1     chris {
    206  1.70      matt 	struct pmap * const pm = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
    207   1.1     chris 	vaddr_t faddr, taddr, off;
    208   1.3     chris 	paddr_t fpa;
    209   1.3     chris 
    210  1.70      matt 	KASSERT(pm != pmap_kernel());
    211   1.1     chris 
    212  1.76     skrll #ifdef XXXDEBUG
    213  1.76     skrll 	printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
    214  1.76     skrll 	    (u_int)bp->b_data, (u_int)len);
    215  1.76     skrll #endif	/* XXXDEBUG */
    216  1.66     skrll 
    217   1.1     chris 	if ((bp->b_flags & B_PHYS) == 0)
    218   1.1     chris 		panic("vmapbuf");
    219   1.1     chris 
    220  1.35     skrll 	bp->b_saveaddr = bp->b_data;
    221  1.35     skrll 	faddr = trunc_page((vaddr_t)bp->b_data);
    222   1.1     chris 	off = (vaddr_t)bp->b_data - faddr;
    223   1.1     chris 	len = round_page(off + len);
    224  1.61      matt 	taddr = uvm_km_alloc(phys_map, len, atop(faddr) & uvmexp.colormask,
    225  1.61      matt 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
    226  1.37  christos 	bp->b_data = (void *)(taddr + off);
    227   1.1     chris 
    228   1.1     chris 	/*
    229  1.78     skrll 	 * The region is locked, so we expect that pmap_extract() will return
    230  1.78     skrll 	 * true.
    231   1.1     chris 	 */
    232   1.3     chris 	while (len) {
    233  1.70      matt 		(void) pmap_extract(pm, faddr, &fpa);
    234  1.71      flxd 		pmap_enter(pmap_kernel(), taddr, fpa, VM_PROT_READ|VM_PROT_WRITE,
    235  1.72     skrll 		    VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    236   1.3     chris 		faddr += PAGE_SIZE;
    237   1.3     chris 		taddr += PAGE_SIZE;
    238   1.3     chris 		len -= PAGE_SIZE;
    239   1.3     chris 	}
    240   1.6     chris 	pmap_update(pmap_kernel());
    241  1.55     pooka 
    242  1.55     pooka 	return 0;
    243   1.1     chris }
    244   1.1     chris 
    245   1.1     chris /*
    246   1.1     chris  * Unmap a previously-mapped user I/O request.
    247   1.1     chris  */
    248   1.1     chris void
    249  1.48       dsl vunmapbuf(struct buf *bp, vsize_t len)
    250   1.1     chris {
    251   1.1     chris 	vaddr_t addr, off;
    252   1.1     chris 
    253  1.76     skrll #ifdef XXXDEBUG
    254  1.76     skrll 	printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
    255  1.76     skrll 	    (u_int)bp, (u_int)bp->b_data, (u_int)len);
    256  1.76     skrll #endif	/* XXXDEBUG */
    257   1.1     chris 
    258   1.1     chris 	if ((bp->b_flags & B_PHYS) == 0)
    259   1.1     chris 		panic("vunmapbuf");
    260   1.1     chris 
    261   1.1     chris 	/*
    262   1.1     chris 	 * Make sure the cache does not have dirty data for the
    263   1.1     chris 	 * pages we had mapped.
    264   1.1     chris 	 */
    265   1.1     chris 	addr = trunc_page((vaddr_t)bp->b_data);
    266   1.1     chris 	off = (vaddr_t)bp->b_data - addr;
    267   1.1     chris 	len = round_page(off + len);
    268  1.66     skrll 
    269  1.72     skrll 	pmap_remove(pmap_kernel(), addr, addr + len);
    270   1.6     chris 	pmap_update(pmap_kernel());
    271  1.33      yamt 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
    272   1.1     chris 	bp->b_data = bp->b_saveaddr;
    273   1.1     chris 	bp->b_saveaddr = 0;
    274   1.1     chris }
    275   1.1     chris 
    276  1.73       chs void
    277  1.73       chs ktext_write(void *to, const void *from, size_t size)
    278  1.73       chs {
    279  1.73       chs 	struct pmap *pmap = pmap_kernel();
    280  1.73       chs 	pd_entry_t *pde, oldpde, tmppde;
    281  1.73       chs 	pt_entry_t *pte, oldpte, tmppte;
    282  1.73       chs 	vaddr_t pgva;
    283  1.73       chs 	size_t limit, savesize;
    284  1.73       chs 	const char *src;
    285  1.73       chs 	char *dst;
    286  1.73       chs 
    287  1.73       chs 	/* XXX: gcc */
    288  1.73       chs 	oldpte = 0;
    289  1.73       chs 
    290  1.73       chs 	if ((savesize = size) == 0)
    291  1.73       chs 		return;
    292  1.73       chs 
    293  1.73       chs 	dst = (char *) to;
    294  1.73       chs 	src = (const char *) from;
    295  1.73       chs 
    296  1.73       chs 	do {
    297  1.73       chs 		/* Get the PDE of the current VA. */
    298  1.73       chs 		if (pmap_get_pde_pte(pmap, (vaddr_t) dst, &pde, &pte) == false)
    299  1.73       chs 			goto no_mapping;
    300  1.73       chs 		switch ((oldpde = *pde) & L1_TYPE_MASK) {
    301  1.73       chs 		case L1_TYPE_S:
    302  1.73       chs 			pgva = (vaddr_t)dst & L1_S_FRAME;
    303  1.73       chs 			limit = L1_S_SIZE - ((vaddr_t)dst & L1_S_OFFSET);
    304  1.73       chs 
    305  1.73       chs 			tmppde = l1pte_set_writable(oldpde);
    306  1.73       chs 			*pde = tmppde;
    307  1.73       chs 			PTE_SYNC(pde);
    308  1.73       chs 			break;
    309  1.73       chs 
    310  1.73       chs 		case L1_TYPE_C:
    311  1.73       chs 			pgva = (vaddr_t)dst & L2_S_FRAME;
    312  1.73       chs 			limit = L2_S_SIZE - ((vaddr_t)dst & L2_S_OFFSET);
    313  1.73       chs 
    314  1.73       chs 			if (pte == NULL)
    315  1.73       chs 				goto no_mapping;
    316  1.73       chs 			oldpte = *pte;
    317  1.73       chs 			tmppte = l2pte_set_writable(oldpte);
    318  1.73       chs 			*pte = tmppte;
    319  1.73       chs 			PTE_SYNC(pte);
    320  1.73       chs 			break;
    321  1.73       chs 
    322  1.73       chs 		default:
    323  1.73       chs 		no_mapping:
    324  1.73       chs 			printf(" address 0x%08lx not a valid page\n",
    325  1.73       chs 			    (vaddr_t) dst);
    326  1.73       chs 			return;
    327  1.73       chs 		}
    328  1.73       chs 		cpu_tlb_flushD_SE(pgva);
    329  1.73       chs 		cpu_cpwait();
    330  1.73       chs 
    331  1.73       chs 		if (limit > size)
    332  1.73       chs 			limit = size;
    333  1.73       chs 		size -= limit;
    334  1.73       chs 
    335  1.73       chs 		/*
    336  1.73       chs 		 * Page is now writable.  Do as much access as we
    337  1.73       chs 		 * can in this page.
    338  1.73       chs 		 */
    339  1.73       chs 		for (; limit > 0; limit--)
    340  1.73       chs 			*dst++ = *src++;
    341  1.73       chs 
    342  1.73       chs 		/*
    343  1.73       chs 		 * Restore old mapping permissions.
    344  1.73       chs 		 */
    345  1.73       chs 		switch (oldpde & L1_TYPE_MASK) {
    346  1.73       chs 		case L1_TYPE_S:
    347  1.73       chs 			*pde = oldpde;
    348  1.73       chs 			PTE_SYNC(pde);
    349  1.73       chs 			break;
    350  1.73       chs 
    351  1.73       chs 		case L1_TYPE_C:
    352  1.73       chs 			*pte = oldpte;
    353  1.73       chs 			PTE_SYNC(pte);
    354  1.73       chs 			break;
    355  1.73       chs 		}
    356  1.73       chs 		cpu_tlb_flushD_SE(pgva);
    357  1.73       chs 		cpu_cpwait();
    358  1.73       chs 	} while (size != 0);
    359  1.73       chs 
    360  1.73       chs 	/* Sync the I-cache. */
    361  1.73       chs 	cpu_icache_sync_range((vaddr_t)to, savesize);
    362  1.73       chs }
    363  1.73       chs 
    364   1.1     chris /* End of vm_machdep.c */
    365