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