]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/vm_machdep.c
MFC r342937:
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / vm_machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include "opt_platform.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/limits.h>
36 #include <sys/proc.h>
37 #include <sys/sf_buf.h>
38 #include <sys/signal.h>
39 #include <sys/sysent.h>
40 #include <sys/unistd.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_map.h>
45 #include <vm/uma.h>
46 #include <vm/uma_int.h>
47
48 #include <machine/armreg.h>
49 #include <machine/cpu.h>
50 #include <machine/md_var.h>
51 #include <machine/pcb.h>
52 #include <machine/frame.h>
53
54 #ifdef VFP
55 #include <machine/vfp.h>
56 #endif
57
58 #include <dev/psci/psci.h>
59
60 /*
61  * Finish a fork operation, with process p2 nearly set up.
62  * Copy and update the pcb, set up the stack so that the child
63  * ready to run and return to user mode.
64  */
65 void
66 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
67 {
68         struct pcb *pcb2;
69         struct trapframe *tf;
70
71         if ((flags & RFPROC) == 0)
72                 return;
73
74         if (td1 == curthread) {
75                 /*
76                  * Save the tpidr_el0 and the vfp state, these normally happen
77                  * in cpu_switch, but if userland changes these then forks
78                  * this may not have happened.
79                  */
80                 td1->td_pcb->pcb_tpidr_el0 = READ_SPECIALREG(tpidr_el0);
81 #ifdef VFP
82                 if ((td1->td_pcb->pcb_fpflags & PCB_FP_STARTED) != 0)
83                         vfp_save_state(td1, td1->td_pcb);
84 #endif
85         }
86
87         pcb2 = (struct pcb *)(td2->td_kstack +
88             td2->td_kstack_pages * PAGE_SIZE) - 1;
89
90         td2->td_pcb = pcb2;
91         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
92
93         td2->td_proc->p_md.md_l0addr =
94             vtophys(vmspace_pmap(td2->td_proc->p_vmspace)->pm_l0);
95
96         tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
97         bcopy(td1->td_frame, tf, sizeof(*tf));
98         tf->tf_x[0] = 0;
99         tf->tf_x[1] = 0;
100         tf->tf_spsr = 0;
101
102         td2->td_frame = tf;
103
104         /* Set the return value registers for fork() */
105         td2->td_pcb->pcb_x[8] = (uintptr_t)fork_return;
106         td2->td_pcb->pcb_x[9] = (uintptr_t)td2;
107         td2->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
108         td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame;
109         td2->td_pcb->pcb_fpusaved = &td2->td_pcb->pcb_fpustate;
110         td2->td_pcb->pcb_vfpcpu = UINT_MAX;
111
112         /* Setup to release spin count in fork_exit(). */
113         td2->td_md.md_spinlock_count = 1;
114         td2->td_md.md_saved_daif = td1->td_md.md_saved_daif & ~DAIF_I_MASKED;
115 }
116
117 void
118 cpu_reset(void)
119 {
120
121         psci_reset();
122
123         printf("cpu_reset failed");
124         while(1)
125                 __asm volatile("wfi" ::: "memory");
126 }
127
128 void
129 cpu_thread_swapin(struct thread *td)
130 {
131 }
132
133 void
134 cpu_thread_swapout(struct thread *td)
135 {
136 }
137
138 void
139 cpu_set_syscall_retval(struct thread *td, int error)
140 {
141         struct trapframe *frame;
142
143         frame = td->td_frame;
144
145         switch (error) {
146         case 0:
147                 frame->tf_x[0] = td->td_retval[0];
148                 frame->tf_x[1] = td->td_retval[1];
149                 frame->tf_spsr &= ~PSR_C;       /* carry bit */
150                 break;
151         case ERESTART:
152                 frame->tf_elr -= 4;
153                 break;
154         case EJUSTRETURN:
155                 break;
156         default:
157                 frame->tf_spsr |= PSR_C;        /* carry bit */
158                 frame->tf_x[0] = SV_ABI_ERRNO(td->td_proc, error);
159                 break;
160         }
161 }
162
163 /*
164  * Initialize machine state, mostly pcb and trap frame for a new
165  * thread, about to return to userspace.  Put enough state in the new
166  * thread's PCB to get it to go back to the fork_return(), which
167  * finalizes the thread state and handles peculiarities of the first
168  * return to userspace for the new thread.
169  */
170 void
171 cpu_copy_thread(struct thread *td, struct thread *td0)
172 {
173         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
174         bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
175
176         td->td_pcb->pcb_x[8] = (uintptr_t)fork_return;
177         td->td_pcb->pcb_x[9] = (uintptr_t)td;
178         td->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
179         td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
180         td->td_pcb->pcb_fpusaved = &td->td_pcb->pcb_fpustate;
181         td->td_pcb->pcb_vfpcpu = UINT_MAX;
182
183         /* Setup to release spin count in fork_exit(). */
184         td->td_md.md_spinlock_count = 1;
185         td->td_md.md_saved_daif = td0->td_md.md_saved_daif & ~DAIF_I_MASKED;
186 }
187
188 /*
189  * Set that machine state for performing an upcall that starts
190  * the entry function with the given argument.
191  */
192 void
193 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
194         stack_t *stack)
195 {
196         struct trapframe *tf = td->td_frame;
197
198         tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
199         tf->tf_elr = (register_t)entry;
200         tf->tf_x[0] = (register_t)arg;
201 }
202
203 int
204 cpu_set_user_tls(struct thread *td, void *tls_base)
205 {
206         struct pcb *pcb;
207
208         if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
209                 return (EINVAL);
210
211         pcb = td->td_pcb;
212         pcb->pcb_tpidr_el0 = (register_t)tls_base;
213         if (td == curthread)
214                 WRITE_SPECIALREG(tpidr_el0, tls_base);
215
216         return (0);
217 }
218
219 void
220 cpu_thread_exit(struct thread *td)
221 {
222 }
223
224 void
225 cpu_thread_alloc(struct thread *td)
226 {
227
228         td->td_pcb = (struct pcb *)(td->td_kstack +
229             td->td_kstack_pages * PAGE_SIZE) - 1;
230         td->td_frame = (struct trapframe *)STACKALIGN(
231             (struct trapframe *)td->td_pcb - 1);
232 }
233
234 void
235 cpu_thread_free(struct thread *td)
236 {
237 }
238
239 void
240 cpu_thread_clean(struct thread *td)
241 {
242 }
243
244 /*
245  * Intercept the return address from a freshly forked process that has NOT
246  * been scheduled yet.
247  *
248  * This is needed to make kernel threads stay in kernel mode.
249  */
250 void
251 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
252 {
253
254         td->td_pcb->pcb_x[8] = (uintptr_t)func;
255         td->td_pcb->pcb_x[9] = (uintptr_t)arg;
256         td->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
257         td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
258         td->td_pcb->pcb_fpusaved = &td->td_pcb->pcb_fpustate;
259         td->td_pcb->pcb_vfpcpu = UINT_MAX;
260 }
261
262 void
263 cpu_exit(struct thread *td)
264 {
265 }
266
267 bool
268 cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
269 {
270
271         return (true);
272 }
273
274 int
275 cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
276     int com __unused, void *data __unused)
277 {
278
279         return (EINVAL);
280 }
281
282 void
283 swi_vm(void *v)
284 {
285
286         if (busdma_swi_pending != 0)
287                 busdma_swi();
288 }