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