xref: /freebsd/sys/arm/arm/vm_machdep.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 1982, 1986 The Regents of the University of California.
3  * Copyright (c) 1989, 1990 William Jolitz
4  * Copyright (c) 1994 John Dyson
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department, and William Jolitz.
10  *
11  * Redistribution and use in source and binary :forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41  */
42 
43 #include "opt_compat.h"
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/proc.h>
54 #include <sys/socketvar.h>
55 #include <sys/syscall.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/unistd.h>
59 
60 #include <machine/cpu.h>
61 #include <machine/frame.h>
62 #include <machine/pcb.h>
63 #include <machine/sysarch.h>
64 #include <sys/lock.h>
65 #include <sys/mutex.h>
66 
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/uma.h>
76 #include <vm/uma_int.h>
77 
78 #include <machine/md_var.h>
79 #include <machine/vfp.h>
80 
81 /*
82  * struct switchframe and trapframe must both be a multiple of 8
83  * for correct stack alignment.
84  */
85 _Static_assert((sizeof(struct switchframe) % 8) == 0, "Bad alignment");
86 _Static_assert((sizeof(struct trapframe) % 8) == 0, "Bad alignment");
87 
88 uint32_t initial_fpscr = VFPSCR_DN | VFPSCR_FZ;
89 
90 /*
91  * Finish a fork operation, with process p2 nearly set up.
92  * Copy and update the pcb, set up the stack so that the child
93  * ready to run and return to user mode.
94  */
95 void
96 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
97 {
98 	struct pcb *pcb2;
99 	struct trapframe *tf;
100 	struct mdproc *mdp2;
101 
102 	if ((flags & RFPROC) == 0)
103 		return;
104 
105 	/* Point the pcb to the top of the stack */
106 	pcb2 = (struct pcb *)
107 	    (td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
108 #ifdef __XSCALE__
109 #ifndef CPU_XSCALE_CORE3
110 	pmap_use_minicache(td2->td_kstack, td2->td_kstack_pages * PAGE_SIZE);
111 #endif
112 #endif
113 #ifdef VFP
114 	/* Store actual state of VFP */
115 	if (curthread == td1) {
116 		critical_enter();
117 		vfp_store(&td1->td_pcb->pcb_vfpstate, false);
118 		critical_exit();
119 	}
120 #endif
121 	td2->td_pcb = pcb2;
122 
123 	/* Clone td1's pcb */
124 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
125 
126 	/* Point to mdproc and then copy over td1's contents */
127 	mdp2 = &p2->p_md;
128 	bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
129 
130 	/* Point the frame to the stack in front of pcb and copy td1's frame */
131 	td2->td_frame = (struct trapframe *)pcb2 - 1;
132 	*td2->td_frame = *td1->td_frame;
133 
134 	/*
135 	 * Create a new fresh stack for the new process.
136 	 * Copy the trap frame for the return to user mode as if from a
137 	 * syscall.  This copies most of the user mode register values.
138 	 */
139 	pmap_set_pcb_pagedir(vmspace_pmap(p2->p_vmspace), pcb2);
140 	pcb2->pcb_regs.sf_r4 = (register_t)fork_return;
141 	pcb2->pcb_regs.sf_r5 = (register_t)td2;
142 	pcb2->pcb_regs.sf_lr = (register_t)fork_trampoline;
143 	pcb2->pcb_regs.sf_sp = STACKALIGN(td2->td_frame);
144 #if __ARM_ARCH >= 6
145 	pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls();
146 #endif
147 
148 	pcb2->pcb_vfpcpu = -1;
149 	pcb2->pcb_vfpstate.fpscr = initial_fpscr;
150 
151 	tf = td2->td_frame;
152 	tf->tf_spsr &= ~PSR_C;
153 	tf->tf_r0 = 0;
154 	tf->tf_r1 = 0;
155 
156 
157 	/* Setup to release spin count in fork_exit(). */
158 	td2->td_md.md_spinlock_count = 1;
159 	td2->td_md.md_saved_cspr = PSR_SVC32_MODE;
160 #if __ARM_ARCH < 6
161 	td2->td_md.md_tp = *(register_t *)ARM_TP_ADDRESS;
162 #endif
163 }
164 
165 void
166 cpu_thread_swapin(struct thread *td)
167 {
168 }
169 
170 void
171 cpu_thread_swapout(struct thread *td)
172 {
173 }
174 
175 void
176 cpu_set_syscall_retval(struct thread *td, int error)
177 {
178 	struct trapframe *frame;
179 	int fixup;
180 #ifdef __ARMEB__
181 	u_int call;
182 #endif
183 
184 	frame = td->td_frame;
185 	fixup = 0;
186 
187 #ifdef __ARMEB__
188 	/*
189 	 * __syscall returns an off_t while most other syscalls return an
190 	 * int. As an off_t is 64-bits and an int is 32-bits we need to
191 	 * place the returned data into r1. As the lseek and freebsd6_lseek
192 	 * syscalls also return an off_t they do not need this fixup.
193 	 */
194 	call = frame->tf_r7;
195 	if (call == SYS___syscall) {
196 		register_t *ap = &frame->tf_r0;
197 		register_t code = ap[_QUAD_LOWWORD];
198 		if (td->td_proc->p_sysent->sv_mask)
199 			code &= td->td_proc->p_sysent->sv_mask;
200 		fixup = (code != SYS_lseek);
201 	}
202 #endif
203 
204 	switch (error) {
205 	case 0:
206 		if (fixup) {
207 			frame->tf_r0 = 0;
208 			frame->tf_r1 = td->td_retval[0];
209 		} else {
210 			frame->tf_r0 = td->td_retval[0];
211 			frame->tf_r1 = td->td_retval[1];
212 		}
213 		frame->tf_spsr &= ~PSR_C;   /* carry bit */
214 		break;
215 	case ERESTART:
216 		/*
217 		 * Reconstruct the pc to point at the swi.
218 		 */
219 #if __ARM_ARCH >= 7
220 		if ((frame->tf_spsr & PSR_T) != 0)
221 			frame->tf_pc -= THUMB_INSN_SIZE;
222 		else
223 #endif
224 			frame->tf_pc -= INSN_SIZE;
225 		break;
226 	case EJUSTRETURN:
227 		/* nothing to do */
228 		break;
229 	default:
230 		frame->tf_r0 = SV_ABI_ERRNO(td->td_proc, error);
231 		frame->tf_spsr |= PSR_C;    /* carry bit */
232 		break;
233 	}
234 }
235 
236 /*
237  * Initialize machine state, mostly pcb and trap frame for a new
238  * thread, about to return to userspace.  Put enough state in the new
239  * thread's PCB to get it to go back to the fork_return(), which
240  * finalizes the thread state and handles peculiarities of the first
241  * return to userspace for the new thread.
242  */
243 void
244 cpu_copy_thread(struct thread *td, struct thread *td0)
245 {
246 
247 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
248 	bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
249 
250 	td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
251 	td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
252 	td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
253 	td->td_pcb->pcb_regs.sf_sp = STACKALIGN(td->td_frame);
254 
255 	td->td_frame->tf_spsr &= ~PSR_C;
256 	td->td_frame->tf_r0 = 0;
257 
258 	/* Setup to release spin count in fork_exit(). */
259 	td->td_md.md_spinlock_count = 1;
260 	td->td_md.md_saved_cspr = PSR_SVC32_MODE;
261 }
262 
263 /*
264  * Set that machine state for performing an upcall that starts
265  * the entry function with the given argument.
266  */
267 void
268 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
269 	stack_t *stack)
270 {
271 	struct trapframe *tf = td->td_frame;
272 
273 	tf->tf_usr_sp = STACKALIGN((int)stack->ss_sp + stack->ss_size);
274 	tf->tf_pc = (int)entry;
275 	tf->tf_r0 = (int)arg;
276 	tf->tf_spsr = PSR_USR32_MODE;
277 }
278 
279 int
280 cpu_set_user_tls(struct thread *td, void *tls_base)
281 {
282 
283 #if __ARM_ARCH >= 6
284 	td->td_pcb->pcb_regs.sf_tpidrurw = (register_t)tls_base;
285 	if (td == curthread)
286 		set_tls(tls_base);
287 #else
288 	td->td_md.md_tp = (register_t)tls_base;
289 	if (td == curthread) {
290 		critical_enter();
291 		*(register_t *)ARM_TP_ADDRESS = (register_t)tls_base;
292 		critical_exit();
293 	}
294 #endif
295 	return (0);
296 }
297 
298 void
299 cpu_thread_exit(struct thread *td)
300 {
301 }
302 
303 void
304 cpu_thread_alloc(struct thread *td)
305 {
306 	td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
307 	    PAGE_SIZE) - 1;
308 	/*
309 	 * Ensure td_frame is aligned to an 8 byte boundary as it will be
310 	 * placed into the stack pointer which must be 8 byte aligned in
311 	 * the ARM EABI.
312 	 */
313 	td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb) - 1;
314 
315 #ifdef __XSCALE__
316 #ifndef CPU_XSCALE_CORE3
317 	pmap_use_minicache(td->td_kstack, td->td_kstack_pages * PAGE_SIZE);
318 #endif
319 #endif
320 }
321 
322 void
323 cpu_thread_free(struct thread *td)
324 {
325 }
326 
327 void
328 cpu_thread_clean(struct thread *td)
329 {
330 }
331 
332 /*
333  * Intercept the return address from a freshly forked process that has NOT
334  * been scheduled yet.
335  *
336  * This is needed to make kernel threads stay in kernel mode.
337  */
338 void
339 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
340 {
341 	td->td_pcb->pcb_regs.sf_r4 = (register_t)func;	/* function */
342 	td->td_pcb->pcb_regs.sf_r5 = (register_t)arg;	/* first arg */
343 }
344 
345 /*
346  * Software interrupt handler for queued VM system processing.
347  */
348 void
349 swi_vm(void *dummy)
350 {
351 
352 	if (busdma_swi_pending)
353 		busdma_swi();
354 }
355 
356 void
357 cpu_exit(struct thread *td)
358 {
359 }
360 
361