xref: /netbsd/sys/arch/arm/arm32/vm_machdep.c (revision 76488156)
1*76488156Sskrll /*	$NetBSD: vm_machdep.c,v 1.78 2021/03/28 10:29:05 skrll Exp $	*/
227f96e84Schris 
327f96e84Schris /*
427f96e84Schris  * Copyright (c) 1994-1998 Mark Brinicombe.
527f96e84Schris  * Copyright (c) 1994 Brini.
627f96e84Schris  * All rights reserved.
727f96e84Schris  *
827f96e84Schris  * This code is derived from software written for Brini by Mark Brinicombe
927f96e84Schris  *
1027f96e84Schris  * Redistribution and use in source and binary forms, with or without
1127f96e84Schris  * modification, are permitted provided that the following conditions
1227f96e84Schris  * are met:
1327f96e84Schris  * 1. Redistributions of source code must retain the above copyright
1427f96e84Schris  *    notice, this list of conditions and the following disclaimer.
1527f96e84Schris  * 2. Redistributions in binary form must reproduce the above copyright
1627f96e84Schris  *    notice, this list of conditions and the following disclaimer in the
1727f96e84Schris  *    documentation and/or other materials provided with the distribution.
1827f96e84Schris  * 3. All advertising materials mentioning features or use of this software
1927f96e84Schris  *    must display the following acknowledgement:
2027f96e84Schris  *	This product includes software developed by Brini.
2127f96e84Schris  * 4. The name of the company nor the name of the author may be used to
2227f96e84Schris  *    endorse or promote products derived from this software without specific
2327f96e84Schris  *    prior written permission.
2427f96e84Schris  *
2527f96e84Schris  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
2627f96e84Schris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2727f96e84Schris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2827f96e84Schris  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2927f96e84Schris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3027f96e84Schris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3127f96e84Schris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3227f96e84Schris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3327f96e84Schris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3427f96e84Schris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3527f96e84Schris  * SUCH DAMAGE.
3627f96e84Schris  *
3727f96e84Schris  * RiscBSD kernel project
3827f96e84Schris  *
3927f96e84Schris  * vm_machdep.h
4027f96e84Schris  *
4127f96e84Schris  * vm machine specific bits
4227f96e84Schris  *
4327f96e84Schris  * Created      : 08/10/94
4427f96e84Schris  */
4527f96e84Schris 
4608716eaeSlukem #include <sys/cdefs.h>
47*76488156Sskrll __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.78 2021/03/28 10:29:05 skrll Exp $");
4808716eaeSlukem 
4927f96e84Schris #include "opt_armfpe.h"
50b45167b6Srearnsha #include "opt_cputypes.h"
5127f96e84Schris 
5227f96e84Schris #include <sys/param.h>
5313d55194Sskrll 
5427f96e84Schris #include <sys/buf.h>
5513d55194Sskrll #include <sys/cpu.h>
5627f96e84Schris #include <sys/exec.h>
5713d55194Sskrll #include <sys/proc.h>
5813d55194Sskrll #include <sys/systm.h>
5913d55194Sskrll #include <sys/vnode.h>
6027f96e84Schris 
6127f96e84Schris #include <uvm/uvm_extern.h>
6227f96e84Schris 
636908b648Smatt #include <arm/vfpreg.h>
646908b648Smatt 
657efdc6dfSmatt #include <machine/pcb.h>
6627f96e84Schris #include <machine/pmap.h>
6727f96e84Schris #include <machine/reg.h>
6827f96e84Schris #include <machine/vmparam.h>
6927f96e84Schris 
70f0301095Syamt void lwp_trampoline(void);
7127f96e84Schris 
7227f96e84Schris /*
7327f96e84Schris  * Special compilation symbols:
7427f96e84Schris  *
7527f96e84Schris  * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
7627f96e84Schris  *		 on forking and check the pattern on exit, reporting
7727f96e84Schris  *		 the amount of stack used.
7827f96e84Schris  */
7927f96e84Schris 
8023bc2503Sthorpej void
cpu_proc_fork(struct proc * p1,struct proc * p2)8182357f6dSdsl cpu_proc_fork(struct proc *p1, struct proc *p2)
8223bc2503Sthorpej {
8395db636eSmatt 	/*
8495db636eSmatt 	 * Copy machine arch string (it's small so just memcpy it).
8595db636eSmatt 	 */
8695db636eSmatt 	memcpy(p2->p_md.md_march, p1->p_md.md_march, sizeof(p2->p_md.md_march));
8723bc2503Sthorpej }
8823bc2503Sthorpej 
8927f96e84Schris /*
90a5b2972aSskrll  * Finish a fork operation, with LWP l2 nearly set up.
91a5b2972aSskrll  *
9227f96e84Schris  * Copy and update the pcb and trapframe, making the child ready to run.
9327f96e84Schris  *
9427f96e84Schris  * Rig the child's kernel stack so that it will start out in
95a5b2972aSskrll  * lwp_trampoline() which will call the specified func with the argument arg.
9627f96e84Schris  *
9727f96e84Schris  * If an alternate user-level stack is requested (with non-zero values
9827f96e84Schris  * in both the stack and stacksize args), set up the user stack pointer
9927f96e84Schris  * accordingly.
10027f96e84Schris  */
10127f96e84Schris void
cpu_lwp_fork(struct lwp * l1,struct lwp * l2,void * stack,size_t stacksize,void (* func)(void *),void * arg)102f0301095Syamt cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
103f0301095Syamt     void (*func)(void *), void *arg)
10427f96e84Schris {
105154d3024Srmind 	struct switchframe *sf;
106ce313d4fSrmind 	vaddr_t uv;
10727f96e84Schris 
1086908b648Smatt 	const struct pcb * const pcb1 = lwp_getpcb(l1);
1096908b648Smatt 	struct pcb * const pcb2 = lwp_getpcb(l2);
11077214629Srmind 
111c6e78f35Sskrll #ifdef XXXDEBUG
11223bc2503Sthorpej 	printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
113c6e78f35Sskrll #endif	/* DEBUG */
11427f96e84Schris 
11527f96e84Schris 	/* Copy the pcb */
11677214629Srmind 	*pcb2 = *pcb1;
11727f96e84Schris 
1186908b648Smatt #ifdef FPU_VFP
1196908b648Smatt 	/*
1206908b648Smatt 	 * Disable the VFP for a newly created LWP but remember if the
1216908b648Smatt 	 * VFP state is valid.
1226908b648Smatt 	 */
1236908b648Smatt 	pcb2->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
1246908b648Smatt #endif
1256908b648Smatt 
12627f96e84Schris 	/*
127ce313d4fSrmind 	 * Set up the kernel stack for the process.
12827f96e84Schris 	 * Note: this stack is not in use if we are forking from p1
12927f96e84Schris 	 */
130ce313d4fSrmind 	uv = uvm_lwp_getuarea(l2);
1313c32e64fSmatt 	pcb2->pcb_ksp = uv + USPACE_SVC_STACK_TOP;
13227f96e84Schris 
13327f96e84Schris #ifdef STACKCHECKS
13427f96e84Schris 	/* Fill the kernel stack with a known pattern */
135ce313d4fSrmind 	memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
13627f96e84Schris 	    (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
13727f96e84Schris #endif	/* STACKCHECKS */
13827f96e84Schris 
139c6e78f35Sskrll #ifdef XXXDEBUG
14077214629Srmind 	printf("l1: pcb=%p pid=%d pmap=%p\n",
14177214629Srmind 	    pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
14277214629Srmind 	printf("l2: pcb=%p pid=%d pmap=%p\n",
14377214629Srmind 	    pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
144c6e78f35Sskrll #endif	/* DEBUG */
14527f96e84Schris 
1463c32e64fSmatt 	struct trapframe *tf = (struct trapframe *)pcb2->pcb_ksp - 1;
1477efdc6dfSmatt 	lwp_settrapframe(l2, tf);
1487efdc6dfSmatt 	*tf = *lwp_trapframe(l1);
14927f96e84Schris 
15027f96e84Schris 	/*
1511cbec577Smatt 	 * If specified, give the child a different stack (make sure
1521cbec577Smatt 	 * it's 8-byte aligned).
15327f96e84Schris 	 */
15427f96e84Schris 	if (stack != NULL)
1551cbec577Smatt 		tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
15627f96e84Schris 
157154d3024Srmind 	sf = (struct switchframe *)tf - 1;
158154d3024Srmind 	sf->sf_r4 = (u_int)func;
159154d3024Srmind 	sf->sf_r5 = (u_int)arg;
160983013fdSmatt 	sf->sf_r7 = PSR_USR32_MODE;		/* for returning to userspace */
161154d3024Srmind 	sf->sf_sp = (u_int)tf;
162154d3024Srmind 	sf->sf_pc = (u_int)lwp_trampoline;
1633c32e64fSmatt 	pcb2->pcb_ksp = (u_int)sf;
16423bc2503Sthorpej }
16523bc2503Sthorpej 
16627f96e84Schris /*
16727f96e84Schris  * cpu_exit is called as the last action during exit.
16827f96e84Schris  *
16927f96e84Schris  * We clean up a little and then call switch_exit() with the old proc as an
17011af2f9cSmatt  * argument.  switch_exit() first switches to lwp0's context, and finally
17127f96e84Schris  * jumps into switch() to wait for another process to wake up.
17227f96e84Schris  */
17327f96e84Schris 
17427f96e84Schris void
cpu_lwp_free(struct lwp * l,int proc)175089abdadSjdolecek cpu_lwp_free(struct lwp *l, int proc)
17627f96e84Schris {
17727f96e84Schris #ifdef STACKCHECKS
17827f96e84Schris 	/* Report how much stack has been used - debugging */
179ba1debe9Smatt 	struct pcb * const pcb = lwp_getpcb(l);
18027f96e84Schris 	u_char *ptr;
181ba1debe9Smatt 	u_int loop;
18227f96e84Schris 
183da876597Srmind 	ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
18427f96e84Schris 	for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
18527f96e84Schris 	    && *ptr == 0xdd; ++loop, ++ptr) ;
186ba1debe9Smatt 	log(LOG_INFO, "%u bytes of svc stack fill pattern\n", loop);
18727f96e84Schris #endif	/* STACKCHECKS */
18827f96e84Schris }
18927f96e84Schris 
190089abdadSjdolecek void
cpu_lwp_free2(struct lwp * l)191c51fe070Smatt cpu_lwp_free2(struct lwp *l)
192c51fe070Smatt {
193c51fe070Smatt }
194c51fe070Smatt 
19527f96e84Schris /*
19627f96e84Schris  * Map a user I/O request into kernel virtual address space.
19727f96e84Schris  * Note: the pages are already locked by uvm_vslock(), so we
19827f96e84Schris  * do not need to pass an access_type to pmap_enter().
19927f96e84Schris  */
20097912c92Spooka int
vmapbuf(struct buf * bp,vsize_t len)201454af1c0Sdsl vmapbuf(struct buf *bp, vsize_t len)
20227f96e84Schris {
203e327d1e7Smatt 	struct pmap * const pm = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
20427f96e84Schris 	vaddr_t faddr, taddr, off;
2056cca1c3fSchris 	paddr_t fpa;
2066cca1c3fSchris 
207e327d1e7Smatt 	KASSERT(pm != pmap_kernel());
20827f96e84Schris 
209c6e78f35Sskrll #ifdef XXXDEBUG
21027f96e84Schris 	printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
21127f96e84Schris 	    (u_int)bp->b_data, (u_int)len);
212c6e78f35Sskrll #endif	/* XXXDEBUG */
21327f96e84Schris 
21427f96e84Schris 	if ((bp->b_flags & B_PHYS) == 0)
21527f96e84Schris 		panic("vmapbuf");
21627f96e84Schris 
217a9f4c28aSskrll 	bp->b_saveaddr = bp->b_data;
218a9f4c28aSskrll 	faddr = trunc_page((vaddr_t)bp->b_data);
21927f96e84Schris 	off = (vaddr_t)bp->b_data - faddr;
22027f96e84Schris 	len = round_page(off + len);
221185ec971Smatt 	taddr = uvm_km_alloc(phys_map, len, atop(faddr) & uvmexp.colormask,
222185ec971Smatt 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
22353524e44Schristos 	bp->b_data = (void *)(taddr + off);
22427f96e84Schris 
22527f96e84Schris 	/*
226*76488156Sskrll 	 * The region is locked, so we expect that pmap_extract() will return
227*76488156Sskrll 	 * true.
22827f96e84Schris 	 */
2296cca1c3fSchris 	while (len) {
230e327d1e7Smatt 		(void) pmap_extract(pm, faddr, &fpa);
2318bf561efSflxd 		pmap_enter(pmap_kernel(), taddr, fpa, VM_PROT_READ|VM_PROT_WRITE,
232c26524d2Sskrll 		    VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
2336cca1c3fSchris 		faddr += PAGE_SIZE;
2346cca1c3fSchris 		taddr += PAGE_SIZE;
2356cca1c3fSchris 		len -= PAGE_SIZE;
2366cca1c3fSchris 	}
2370e7661f0Schris 	pmap_update(pmap_kernel());
23897912c92Spooka 
23997912c92Spooka 	return 0;
24027f96e84Schris }
24127f96e84Schris 
24227f96e84Schris /*
24327f96e84Schris  * Unmap a previously-mapped user I/O request.
24427f96e84Schris  */
24527f96e84Schris void
vunmapbuf(struct buf * bp,vsize_t len)246454af1c0Sdsl vunmapbuf(struct buf *bp, vsize_t len)
24727f96e84Schris {
24827f96e84Schris 	vaddr_t addr, off;
24927f96e84Schris 
250c6e78f35Sskrll #ifdef XXXDEBUG
25127f96e84Schris 	printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
25227f96e84Schris 	    (u_int)bp, (u_int)bp->b_data, (u_int)len);
253c6e78f35Sskrll #endif	/* XXXDEBUG */
25427f96e84Schris 
25527f96e84Schris 	if ((bp->b_flags & B_PHYS) == 0)
25627f96e84Schris 		panic("vunmapbuf");
25727f96e84Schris 
25827f96e84Schris 	/*
25927f96e84Schris 	 * Make sure the cache does not have dirty data for the
26027f96e84Schris 	 * pages we had mapped.
26127f96e84Schris 	 */
26227f96e84Schris 	addr = trunc_page((vaddr_t)bp->b_data);
26327f96e84Schris 	off = (vaddr_t)bp->b_data - addr;
26427f96e84Schris 	len = round_page(off + len);
265140252beSchris 
266c26524d2Sskrll 	pmap_remove(pmap_kernel(), addr, addr + len);
2670e7661f0Schris 	pmap_update(pmap_kernel());
2686b2d8b66Syamt 	uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
26927f96e84Schris 	bp->b_data = bp->b_saveaddr;
27027f96e84Schris 	bp->b_saveaddr = 0;
27127f96e84Schris }
27227f96e84Schris 
273eada09acSchs void
ktext_write(void * to,const void * from,size_t size)274eada09acSchs ktext_write(void *to, const void *from, size_t size)
275eada09acSchs {
276eada09acSchs 	struct pmap *pmap = pmap_kernel();
277eada09acSchs 	pd_entry_t *pde, oldpde, tmppde;
278eada09acSchs 	pt_entry_t *pte, oldpte, tmppte;
279eada09acSchs 	vaddr_t pgva;
280eada09acSchs 	size_t limit, savesize;
281eada09acSchs 	const char *src;
282eada09acSchs 	char *dst;
283eada09acSchs 
284eada09acSchs 	/* XXX: gcc */
285eada09acSchs 	oldpte = 0;
286eada09acSchs 
287eada09acSchs 	if ((savesize = size) == 0)
288eada09acSchs 		return;
289eada09acSchs 
290eada09acSchs 	dst = (char *) to;
291eada09acSchs 	src = (const char *) from;
292eada09acSchs 
293eada09acSchs 	do {
294eada09acSchs 		/* Get the PDE of the current VA. */
295eada09acSchs 		if (pmap_get_pde_pte(pmap, (vaddr_t) dst, &pde, &pte) == false)
296eada09acSchs 			goto no_mapping;
297eada09acSchs 		switch ((oldpde = *pde) & L1_TYPE_MASK) {
298eada09acSchs 		case L1_TYPE_S:
299eada09acSchs 			pgva = (vaddr_t)dst & L1_S_FRAME;
300eada09acSchs 			limit = L1_S_SIZE - ((vaddr_t)dst & L1_S_OFFSET);
301eada09acSchs 
302eada09acSchs 			tmppde = l1pte_set_writable(oldpde);
303eada09acSchs 			*pde = tmppde;
304eada09acSchs 			PTE_SYNC(pde);
305eada09acSchs 			break;
306eada09acSchs 
307eada09acSchs 		case L1_TYPE_C:
308eada09acSchs 			pgva = (vaddr_t)dst & L2_S_FRAME;
309eada09acSchs 			limit = L2_S_SIZE - ((vaddr_t)dst & L2_S_OFFSET);
310eada09acSchs 
311eada09acSchs 			if (pte == NULL)
312eada09acSchs 				goto no_mapping;
313eada09acSchs 			oldpte = *pte;
314eada09acSchs 			tmppte = l2pte_set_writable(oldpte);
315eada09acSchs 			*pte = tmppte;
316eada09acSchs 			PTE_SYNC(pte);
317eada09acSchs 			break;
318eada09acSchs 
319eada09acSchs 		default:
320eada09acSchs 		no_mapping:
321eada09acSchs 			printf(" address 0x%08lx not a valid page\n",
322eada09acSchs 			    (vaddr_t) dst);
323eada09acSchs 			return;
324eada09acSchs 		}
325eada09acSchs 		cpu_tlb_flushD_SE(pgva);
326eada09acSchs 		cpu_cpwait();
327eada09acSchs 
328eada09acSchs 		if (limit > size)
329eada09acSchs 			limit = size;
330eada09acSchs 		size -= limit;
331eada09acSchs 
332eada09acSchs 		/*
333eada09acSchs 		 * Page is now writable.  Do as much access as we
334eada09acSchs 		 * can in this page.
335eada09acSchs 		 */
336eada09acSchs 		for (; limit > 0; limit--)
337eada09acSchs 			*dst++ = *src++;
338eada09acSchs 
339eada09acSchs 		/*
340eada09acSchs 		 * Restore old mapping permissions.
341eada09acSchs 		 */
342eada09acSchs 		switch (oldpde & L1_TYPE_MASK) {
343eada09acSchs 		case L1_TYPE_S:
344eada09acSchs 			*pde = oldpde;
345eada09acSchs 			PTE_SYNC(pde);
346eada09acSchs 			break;
347eada09acSchs 
348eada09acSchs 		case L1_TYPE_C:
349eada09acSchs 			*pte = oldpte;
350eada09acSchs 			PTE_SYNC(pte);
351eada09acSchs 			break;
352eada09acSchs 		}
353eada09acSchs 		cpu_tlb_flushD_SE(pgva);
354eada09acSchs 		cpu_cpwait();
355eada09acSchs 	} while (size != 0);
356eada09acSchs 
357eada09acSchs 	/* Sync the I-cache. */
358eada09acSchs 	cpu_icache_sync_range((vaddr_t)to, savesize);
359eada09acSchs }
360eada09acSchs 
36127f96e84Schris /* End of vm_machdep.c */
362