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