]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/vm_machdep.c
Merge llvm-project release/16.x llvmorg-16.0.4-0-gae42196bc493
[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 <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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/cpu.h>
61 #include <machine/frame.h>
62 #include <machine/pcb.h>
63 #include <machine/sysarch.h>
64 #include <sys/lock.h>
65 #include <sys/mutex.h>
66
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/uma.h>
76 #include <vm/uma_int.h>
77
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  */
85 _Static_assert((sizeof(struct switchframe) % 8) == 0, "Bad alignment");
86 _Static_assert((sizeof(struct trapframe) % 8) == 0, "Bad alignment");
87
88 uint32_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  */
95 void
96 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
97 {
98         struct pcb *pcb2;
99         struct trapframe *tf;
100         struct mdproc *mdp2;
101
102         if ((flags & RFPROC) == 0)
103                 return;
104
105         /* Point the pcb to the top of the stack */
106         pcb2 = (struct pcb *)
107             (td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
108 #ifdef VFP
109         /* Store actual state of VFP */
110         if (curthread == td1) {
111                 if ((td1->td_pcb->pcb_fpflags & PCB_FP_STARTED) != 0)
112                         vfp_save_state(td1, td1->td_pcb);
113         }
114 #endif
115         td2->td_pcb = pcb2;
116
117         /* Clone td1's pcb */
118         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
119
120         /* Point to mdproc and then copy over td1's contents */
121         mdp2 = &p2->p_md;
122         bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
123
124         /* Point the frame to the stack in front of pcb and copy td1's frame */
125         td2->td_frame = (struct trapframe *)pcb2 - 1;
126         *td2->td_frame = *td1->td_frame;
127
128         /*
129          * Create a new fresh stack for the new process.
130          * Copy the trap frame for the return to user mode as if from a
131          * syscall.  This copies most of the user mode register values.
132          */
133         pmap_set_pcb_pagedir(vmspace_pmap(p2->p_vmspace), pcb2);
134         pcb2->pcb_regs.sf_r4 = (register_t)fork_return;
135         pcb2->pcb_regs.sf_r5 = (register_t)td2;
136         pcb2->pcb_regs.sf_lr = (register_t)fork_trampoline;
137         pcb2->pcb_regs.sf_sp = STACKALIGN(td2->td_frame);
138         pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls();
139
140 #ifdef VFP
141         vfp_new_thread(td2, td1, true);
142 #endif
143
144         tf = td2->td_frame;
145         tf->tf_spsr &= ~PSR_C;
146         tf->tf_r0 = 0;
147         tf->tf_r1 = 0;
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 }
153
154 void
155 cpu_thread_swapin(struct thread *td)
156 {
157 }
158
159 void
160 cpu_thread_swapout(struct thread *td)
161 {
162 }
163
164 void
165 cpu_set_syscall_retval(struct thread *td, int error)
166 {
167         struct trapframe *frame;
168
169         frame = td->td_frame;
170         switch (error) {
171         case 0:
172                 frame->tf_r0 = td->td_retval[0];
173                 frame->tf_r1 = td->td_retval[1];
174                 frame->tf_spsr &= ~PSR_C;   /* carry bit */
175                 break;
176         case ERESTART:
177                 /*
178                  * Reconstruct the pc to point at the swi.
179                  */
180 #if __ARM_ARCH >= 7
181                 if ((frame->tf_spsr & PSR_T) != 0)
182                         frame->tf_pc -= THUMB_INSN_SIZE;
183                 else
184 #endif
185                         frame->tf_pc -= INSN_SIZE;
186                 break;
187         case EJUSTRETURN:
188                 /* nothing to do */
189                 break;
190         default:
191                 frame->tf_r0 = error;
192                 frame->tf_spsr |= PSR_C;    /* carry bit */
193                 break;
194         }
195 }
196
197 /*
198  * Initialize machine state, mostly pcb and trap frame for a new
199  * thread, about to return to userspace.  Put enough state in the new
200  * thread's PCB to get it to go back to the fork_return(), which
201  * finalizes the thread state and handles peculiarities of the first
202  * return to userspace for the new thread.
203  */
204 void
205 cpu_copy_thread(struct thread *td, struct thread *td0)
206 {
207
208         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
209         bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
210
211         td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
212         td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
213         td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
214         td->td_pcb->pcb_regs.sf_sp = STACKALIGN(td->td_frame);
215
216         td->td_frame->tf_spsr &= ~PSR_C;
217         td->td_frame->tf_r0 = 0;
218
219 #ifdef VFP
220         vfp_new_thread(td, td0, false);
221 #endif
222
223         /* Setup to release spin count in fork_exit(). */
224         td->td_md.md_spinlock_count = 1;
225         td->td_md.md_saved_cspr = PSR_SVC32_MODE;
226 }
227
228 /*
229  * Set that machine state for performing an upcall that starts
230  * the entry function with the given argument.
231  */
232 void
233 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
234         stack_t *stack)
235 {
236         struct trapframe *tf = td->td_frame;
237
238         tf->tf_usr_sp = STACKALIGN((int)stack->ss_sp + stack->ss_size);
239         tf->tf_pc = (int)entry;
240         tf->tf_r0 = (int)arg;
241         tf->tf_spsr = PSR_USR32_MODE;
242         if ((register_t)entry & 1)
243                 tf->tf_spsr |= PSR_T;
244 }
245
246 int
247 cpu_set_user_tls(struct thread *td, void *tls_base)
248 {
249
250         td->td_pcb->pcb_regs.sf_tpidrurw = (register_t)tls_base;
251         if (td == curthread)
252                 set_tls(tls_base);
253         return (0);
254 }
255
256 void
257 cpu_thread_exit(struct thread *td)
258 {
259 }
260
261 void
262 cpu_thread_alloc(struct thread *td)
263 {
264         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
265             PAGE_SIZE) - 1;
266         /*
267          * Ensure td_frame is aligned to an 8 byte boundary as it will be
268          * placed into the stack pointer which must be 8 byte aligned in
269          * the ARM EABI.
270          */
271         td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb) - 1;
272 }
273
274 void
275 cpu_thread_free(struct thread *td)
276 {
277 }
278
279 void
280 cpu_thread_clean(struct thread *td)
281 {
282 }
283
284 /*
285  * Intercept the return address from a freshly forked process that has NOT
286  * been scheduled yet.
287  *
288  * This is needed to make kernel threads stay in kernel mode.
289  */
290 void
291 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
292 {
293         td->td_pcb->pcb_regs.sf_r4 = (register_t)func;  /* function */
294         td->td_pcb->pcb_regs.sf_r5 = (register_t)arg;   /* first arg */
295 }
296
297 void
298 cpu_exit(struct thread *td)
299 {
300 }
301
302 bool
303 cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
304 {
305
306         return (true);
307 }
308
309 int
310 cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
311     int com __unused, void *data __unused)
312 {
313
314         return (EINVAL);
315 }