]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/vm_machdep.c
Upgrade to OpenSSH 7.8p1.
[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                 critical_enter();
112                 vfp_store(&td1->td_pcb->pcb_vfpstate, false);
113                 critical_exit();
114         }
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 #if __ARM_ARCH >= 6
140         pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls();
141 #endif
142
143         pcb2->pcb_vfpcpu = -1;
144         pcb2->pcb_vfpstate.fpscr = initial_fpscr;
145
146         tf = td2->td_frame;
147         tf->tf_spsr &= ~PSR_C;
148         tf->tf_r0 = 0;
149         tf->tf_r1 = 0;
150
151
152         /* Setup to release spin count in fork_exit(). */
153         td2->td_md.md_spinlock_count = 1;
154         td2->td_md.md_saved_cspr = PSR_SVC32_MODE;
155 #if __ARM_ARCH < 6
156         td2->td_md.md_tp = *(register_t *)ARM_TP_ADDRESS;
157 #endif
158 }
159
160 void
161 cpu_thread_swapin(struct thread *td)
162 {
163 }
164
165 void
166 cpu_thread_swapout(struct thread *td)
167 {
168 }
169
170 void
171 cpu_set_syscall_retval(struct thread *td, int error)
172 {
173         struct trapframe *frame;
174         int fixup;
175 #ifdef __ARMEB__
176         u_int call;
177 #endif
178
179         frame = td->td_frame;
180         fixup = 0;
181
182 #ifdef __ARMEB__
183         /*
184          * __syscall returns an off_t while most other syscalls return an
185          * int. As an off_t is 64-bits and an int is 32-bits we need to
186          * place the returned data into r1. As the lseek and freebsd6_lseek
187          * syscalls also return an off_t they do not need this fixup.
188          */
189         call = frame->tf_r7;
190         if (call == SYS___syscall) {
191                 register_t *ap = &frame->tf_r0;
192                 register_t code = ap[_QUAD_LOWWORD];
193                 if (td->td_proc->p_sysent->sv_mask)
194                         code &= td->td_proc->p_sysent->sv_mask;
195                 fixup = (code != SYS_lseek);
196         }
197 #endif
198
199         switch (error) {
200         case 0:
201                 if (fixup) {
202                         frame->tf_r0 = 0;
203                         frame->tf_r1 = td->td_retval[0];
204                 } else {
205                         frame->tf_r0 = td->td_retval[0];
206                         frame->tf_r1 = td->td_retval[1];
207                 }
208                 frame->tf_spsr &= ~PSR_C;   /* carry bit */
209                 break;
210         case ERESTART:
211                 /*
212                  * Reconstruct the pc to point at the swi.
213                  */
214 #if __ARM_ARCH >= 7
215                 if ((frame->tf_spsr & PSR_T) != 0)
216                         frame->tf_pc -= THUMB_INSN_SIZE;
217                 else
218 #endif
219                         frame->tf_pc -= INSN_SIZE;
220                 break;
221         case EJUSTRETURN:
222                 /* nothing to do */
223                 break;
224         default:
225                 frame->tf_r0 = SV_ABI_ERRNO(td->td_proc, error);
226                 frame->tf_spsr |= PSR_C;    /* carry bit */
227                 break;
228         }
229 }
230
231 /*
232  * Initialize machine state, mostly pcb and trap frame for a new
233  * thread, about to return to userspace.  Put enough state in the new
234  * thread's PCB to get it to go back to the fork_return(), which
235  * finalizes the thread state and handles peculiarities of the first
236  * return to userspace for the new thread.
237  */
238 void
239 cpu_copy_thread(struct thread *td, struct thread *td0)
240 {
241
242         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
243         bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
244
245         td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
246         td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
247         td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
248         td->td_pcb->pcb_regs.sf_sp = STACKALIGN(td->td_frame);
249
250         td->td_frame->tf_spsr &= ~PSR_C;
251         td->td_frame->tf_r0 = 0;
252
253         /* Setup to release spin count in fork_exit(). */
254         td->td_md.md_spinlock_count = 1;
255         td->td_md.md_saved_cspr = PSR_SVC32_MODE;
256 }
257
258 /*
259  * Set that machine state for performing an upcall that starts
260  * the entry function with the given argument.
261  */
262 void
263 cpu_set_upcall(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
274 int
275 cpu_set_user_tls(struct thread *td, void *tls_base)
276 {
277
278 #if __ARM_ARCH >= 6
279         td->td_pcb->pcb_regs.sf_tpidrurw = (register_t)tls_base;
280         if (td == curthread)
281                 set_tls(tls_base);
282 #else
283         td->td_md.md_tp = (register_t)tls_base;
284         if (td == curthread) {
285                 critical_enter();
286                 *(register_t *)ARM_TP_ADDRESS = (register_t)tls_base;
287                 critical_exit();
288         }
289 #endif
290         return (0);
291 }
292
293 void
294 cpu_thread_exit(struct thread *td)
295 {
296 }
297
298 void
299 cpu_thread_alloc(struct thread *td)
300 {
301         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
302             PAGE_SIZE) - 1;
303         /*
304          * Ensure td_frame is aligned to an 8 byte boundary as it will be
305          * placed into the stack pointer which must be 8 byte aligned in
306          * the ARM EABI.
307          */
308         td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb) - 1;
309 }
310
311 void
312 cpu_thread_free(struct thread *td)
313 {
314 }
315
316 void
317 cpu_thread_clean(struct thread *td)
318 {
319 }
320
321 /*
322  * Intercept the return address from a freshly forked process that has NOT
323  * been scheduled yet.
324  *
325  * This is needed to make kernel threads stay in kernel mode.
326  */
327 void
328 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
329 {
330         td->td_pcb->pcb_regs.sf_r4 = (register_t)func;  /* function */
331         td->td_pcb->pcb_regs.sf_r5 = (register_t)arg;   /* first arg */
332 }
333
334 /*
335  * Software interrupt handler for queued VM system processing.
336  */
337 void
338 swi_vm(void *dummy)
339 {
340
341         if (busdma_swi_pending)
342                 busdma_swi();
343 }
344
345 void
346 cpu_exit(struct thread *td)
347 {
348 }
349