xref: /freebsd/sys/arm/arm/vm_machdep.c (revision 5b9c547c)
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 <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/proc.h>
52 #include <sys/socketvar.h>
53 #include <sys/syscall.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysent.h>
56 #include <sys/unistd.h>
57 #include <machine/cpu.h>
58 #include <machine/frame.h>
59 #include <machine/pcb.h>
60 #include <machine/sysarch.h>
61 #include <sys/lock.h>
62 #include <sys/mutex.h>
63 
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_param.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/uma.h>
73 #include <vm/uma_int.h>
74 
75 #include <machine/md_var.h>
76 #include <machine/vfp.h>
77 
78 /*
79  * struct switchframe and trapframe must both be a multiple of 8
80  * for correct stack alignment.
81  */
82 CTASSERT(sizeof(struct switchframe) == 48);
83 CTASSERT(sizeof(struct trapframe) == 80);
84 
85 /*
86  * Finish a fork operation, with process p2 nearly set up.
87  * Copy and update the pcb, set up the stack so that the child
88  * ready to run and return to user mode.
89  */
90 void
91 cpu_fork(register struct thread *td1, register struct proc *p2,
92     struct thread *td2, int flags)
93 {
94 	struct pcb *pcb2;
95 	struct trapframe *tf;
96 	struct mdproc *mdp2;
97 
98 	if ((flags & RFPROC) == 0)
99 		return;
100 
101 	/* Point the pcb to the top of the stack */
102 	pcb2 = (struct pcb *)
103 	    (td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
104 #ifdef __XSCALE__
105 #ifndef CPU_XSCALE_CORE3
106 	pmap_use_minicache(td2->td_kstack, td2->td_kstack_pages * PAGE_SIZE);
107 #endif
108 #endif
109 	td2->td_pcb = pcb2;
110 
111 	/* Clone td1's pcb */
112 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
113 
114 	/* Point to mdproc and then copy over td1's contents */
115 	mdp2 = &p2->p_md;
116 	bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
117 
118 	/* Point the frame to the stack in front of pcb and copy td1's frame */
119 	td2->td_frame = (struct trapframe *)pcb2 - 1;
120 	*td2->td_frame = *td1->td_frame;
121 
122 	/*
123 	 * Create a new fresh stack for the new process.
124 	 * Copy the trap frame for the return to user mode as if from a
125 	 * syscall.  This copies most of the user mode register values.
126 	 */
127 	pmap_set_pcb_pagedir(vmspace_pmap(p2->p_vmspace), pcb2);
128 	pcb2->pcb_regs.sf_r4 = (register_t)fork_return;
129 	pcb2->pcb_regs.sf_r5 = (register_t)td2;
130 	pcb2->pcb_regs.sf_lr = (register_t)fork_trampoline;
131 	pcb2->pcb_regs.sf_sp = STACKALIGN(td2->td_frame);
132 
133 	pcb2->pcb_vfpcpu = -1;
134 	pcb2->pcb_vfpstate.fpscr = VFPSCR_DN | VFPSCR_FZ;
135 
136 	tf = td2->td_frame;
137 	tf->tf_spsr &= ~PSR_C;
138 	tf->tf_r0 = 0;
139 	tf->tf_r1 = 0;
140 
141 
142 	/* Setup to release spin count in fork_exit(). */
143 	td2->td_md.md_spinlock_count = 1;
144 	td2->td_md.md_saved_cspr = PSR_SVC32_MODE;;
145 #ifdef ARM_TP_ADDRESS
146 	td2->td_md.md_tp = *(register_t *)ARM_TP_ADDRESS;
147 #else
148 	td2->td_md.md_tp = td1->td_md.md_tp;
149 #endif
150 }
151 
152 void
153 cpu_thread_swapin(struct thread *td)
154 {
155 }
156 
157 void
158 cpu_thread_swapout(struct thread *td)
159 {
160 }
161 
162 void
163 cpu_set_syscall_retval(struct thread *td, int error)
164 {
165 	struct trapframe *frame;
166 	int fixup;
167 #ifdef __ARMEB__
168 	u_int call;
169 #endif
170 
171 	frame = td->td_frame;
172 	fixup = 0;
173 
174 #ifdef __ARMEB__
175 	/*
176 	 * __syscall returns an off_t while most other syscalls return an
177 	 * int. As an off_t is 64-bits and an int is 32-bits we need to
178 	 * place the returned data into r1. As the lseek and frerebsd6_lseek
179 	 * syscalls also return an off_t they do not need this fixup.
180 	 */
181 	call = frame->tf_r7;
182 	if (call == SYS___syscall) {
183 		register_t *ap = &frame->tf_r0;
184 		register_t code = ap[_QUAD_LOWWORD];
185 		if (td->td_proc->p_sysent->sv_mask)
186 			code &= td->td_proc->p_sysent->sv_mask;
187 		fixup = (code != SYS_freebsd6_lseek && code != SYS_lseek)
188 		    ? 1 : 0;
189 	}
190 #endif
191 
192 	switch (error) {
193 	case 0:
194 		if (fixup) {
195 			frame->tf_r0 = 0;
196 			frame->tf_r1 = td->td_retval[0];
197 		} else {
198 			frame->tf_r0 = td->td_retval[0];
199 			frame->tf_r1 = td->td_retval[1];
200 		}
201 		frame->tf_spsr &= ~PSR_C;   /* carry bit */
202 		break;
203 	case ERESTART:
204 		/*
205 		 * Reconstruct the pc to point at the swi.
206 		 */
207 		frame->tf_pc -= INSN_SIZE;
208 		break;
209 	case EJUSTRETURN:
210 		/* nothing to do */
211 		break;
212 	default:
213 		frame->tf_r0 = error;
214 		frame->tf_spsr |= PSR_C;    /* carry bit */
215 		break;
216 	}
217 }
218 
219 /*
220  * Initialize machine state (pcb and trap frame) for a new thread about to
221  * upcall. Put enough state in the new thread's PCB to get it to go back
222  * userret(), where we can intercept it again to set the return (upcall)
223  * Address and stack, along with those from upcals that are from other sources
224  * such as those generated in thread_userret() itself.
225  */
226 void
227 cpu_set_upcall(struct thread *td, struct thread *td0)
228 {
229 
230 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
231 	bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
232 
233 	td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
234 	td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
235 	td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
236 	td->td_pcb->pcb_regs.sf_sp = STACKALIGN(td->td_frame);
237 
238 	td->td_frame->tf_spsr &= ~PSR_C;
239 	td->td_frame->tf_r0 = 0;
240 
241 	/* Setup to release spin count in fork_exit(). */
242 	td->td_md.md_spinlock_count = 1;
243 	td->td_md.md_saved_cspr = PSR_SVC32_MODE;
244 }
245 
246 /*
247  * Set that machine state for performing an upcall that has to
248  * be done in thread_userret() so that those upcalls generated
249  * in thread_userret() itself can be done as well.
250  */
251 void
252 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
253 	stack_t *stack)
254 {
255 	struct trapframe *tf = td->td_frame;
256 
257 	tf->tf_usr_sp = STACKALIGN((int)stack->ss_sp + stack->ss_size);
258 	tf->tf_pc = (int)entry;
259 	tf->tf_r0 = (int)arg;
260 	tf->tf_spsr = PSR_USR32_MODE;
261 }
262 
263 int
264 cpu_set_user_tls(struct thread *td, void *tls_base)
265 {
266 
267 	td->td_md.md_tp = (register_t)tls_base;
268 	if (td == curthread) {
269 		critical_enter();
270 #ifdef ARM_TP_ADDRESS
271 		*(register_t *)ARM_TP_ADDRESS = (register_t)tls_base;
272 #else
273 		set_tls(tls_base);
274 #endif
275 		critical_exit();
276 	}
277 	return (0);
278 }
279 
280 void
281 cpu_thread_exit(struct thread *td)
282 {
283 }
284 
285 void
286 cpu_thread_alloc(struct thread *td)
287 {
288 	td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
289 	    PAGE_SIZE) - 1;
290 	/*
291 	 * Ensure td_frame is aligned to an 8 byte boundary as it will be
292 	 * placed into the stack pointer which must be 8 byte aligned in
293 	 * the ARM EABI.
294 	 */
295 	td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb) - 1;
296 
297 #ifdef __XSCALE__
298 #ifndef CPU_XSCALE_CORE3
299 	pmap_use_minicache(td->td_kstack, td->td_kstack_pages * PAGE_SIZE);
300 #endif
301 #endif
302 }
303 
304 void
305 cpu_thread_free(struct thread *td)
306 {
307 }
308 
309 void
310 cpu_thread_clean(struct thread *td)
311 {
312 }
313 
314 /*
315  * Intercept the return address from a freshly forked process that has NOT
316  * been scheduled yet.
317  *
318  * This is needed to make kernel threads stay in kernel mode.
319  */
320 void
321 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
322 {
323 	td->td_pcb->pcb_regs.sf_r4 = (register_t)func;	/* function */
324 	td->td_pcb->pcb_regs.sf_r5 = (register_t)arg;	/* first arg */
325 }
326 
327 /*
328  * Software interrupt handler for queued VM system processing.
329  */
330 void
331 swi_vm(void *dummy)
332 {
333 
334 	if (busdma_swi_pending)
335 		busdma_swi();
336 }
337 
338 void
339 cpu_exit(struct thread *td)
340 {
341 }
342 
343