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