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