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