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