]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/vm_machdep.c
MFV r333779: xz 5.2.4.
[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/unistd.h>
40
41 #include <vm/vm.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_map.h>
44 #include <vm/uma.h>
45 #include <vm/uma_int.h>
46
47 #include <machine/armreg.h>
48 #include <machine/cpu.h>
49 #include <machine/md_var.h>
50 #include <machine/pcb.h>
51 #include <machine/frame.h>
52
53 #ifdef VFP
54 #include <machine/vfp.h>
55 #endif
56
57 #ifdef DEV_PSCI
58 #include <dev/psci/psci.h>
59 #endif
60
61 /*
62  * Finish a fork operation, with process p2 nearly set up.
63  * Copy and update the pcb, set up the stack so that the child
64  * ready to run and return to user mode.
65  */
66 void
67 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
68 {
69         struct pcb *pcb2;
70         struct trapframe *tf;
71
72         if ((flags & RFPROC) == 0)
73                 return;
74
75         if (td1 == curthread) {
76                 /*
77                  * Save the tpidr_el0 and the vfp state, these normally happen
78                  * in cpu_switch, but if userland changes these then forks
79                  * this may not have happened.
80                  */
81                 td1->td_pcb->pcb_tpidr_el0 = READ_SPECIALREG(tpidr_el0);
82 #ifdef VFP
83                 if ((td1->td_pcb->pcb_fpflags & PCB_FP_STARTED) != 0)
84                         vfp_save_state(td1, td1->td_pcb);
85 #endif
86         }
87
88         pcb2 = (struct pcb *)(td2->td_kstack +
89             td2->td_kstack_pages * PAGE_SIZE) - 1;
90
91         td2->td_pcb = pcb2;
92         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
93
94         td2->td_proc->p_md.md_l0addr =
95             vtophys(vmspace_pmap(td2->td_proc->p_vmspace)->pm_l0);
96
97         tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
98         bcopy(td1->td_frame, tf, sizeof(*tf));
99         tf->tf_x[0] = 0;
100         tf->tf_x[1] = 0;
101         tf->tf_spsr = 0;
102
103         td2->td_frame = tf;
104
105         /* Set the return value registers for fork() */
106         td2->td_pcb->pcb_x[8] = (uintptr_t)fork_return;
107         td2->td_pcb->pcb_x[9] = (uintptr_t)td2;
108         td2->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
109         td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame;
110         td2->td_pcb->pcb_fpusaved = &td2->td_pcb->pcb_fpustate;
111         td2->td_pcb->pcb_vfpcpu = UINT_MAX;
112
113         /* Setup to release spin count in fork_exit(). */
114         td2->td_md.md_spinlock_count = 1;
115         td2->td_md.md_saved_daif = 0;
116 }
117
118 void
119 cpu_reset(void)
120 {
121
122 #ifdef DEV_PSCI
123         psci_reset();
124 #endif
125
126         printf("cpu_reset failed");
127         while(1)
128                 __asm volatile("wfi" ::: "memory");
129 }
130
131 void
132 cpu_thread_swapin(struct thread *td)
133 {
134 }
135
136 void
137 cpu_thread_swapout(struct thread *td)
138 {
139 }
140
141 void
142 cpu_set_syscall_retval(struct thread *td, int error)
143 {
144         struct trapframe *frame;
145
146         frame = td->td_frame;
147
148         switch (error) {
149         case 0:
150                 frame->tf_x[0] = td->td_retval[0];
151                 frame->tf_x[1] = td->td_retval[1];
152                 frame->tf_spsr &= ~PSR_C;       /* carry bit */
153                 break;
154         case ERESTART:
155                 frame->tf_elr -= 4;
156                 break;
157         case EJUSTRETURN:
158                 break;
159         default:
160                 frame->tf_spsr |= PSR_C;        /* carry bit */
161                 frame->tf_x[0] = error;
162                 break;
163         }
164 }
165
166 /*
167  * Initialize machine state, mostly pcb and trap frame for a new
168  * thread, about to return to userspace.  Put enough state in the new
169  * thread's PCB to get it to go back to the fork_return(), which
170  * finalizes the thread state and handles peculiarities of the first
171  * return to userspace for the new thread.
172  */
173 void
174 cpu_copy_thread(struct thread *td, struct thread *td0)
175 {
176         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
177         bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
178
179         td->td_pcb->pcb_x[8] = (uintptr_t)fork_return;
180         td->td_pcb->pcb_x[9] = (uintptr_t)td;
181         td->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
182         td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
183         td->td_pcb->pcb_fpusaved = &td->td_pcb->pcb_fpustate;
184         td->td_pcb->pcb_vfpcpu = UINT_MAX;
185
186         /* Setup to release spin count in fork_exit(). */
187         td->td_md.md_spinlock_count = 1;
188         td->td_md.md_saved_daif = 0;
189 }
190
191 /*
192  * Set that machine state for performing an upcall that starts
193  * the entry function with the given argument.
194  */
195 void
196 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
197         stack_t *stack)
198 {
199         struct trapframe *tf = td->td_frame;
200
201         tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
202         tf->tf_elr = (register_t)entry;
203         tf->tf_x[0] = (register_t)arg;
204 }
205
206 int
207 cpu_set_user_tls(struct thread *td, void *tls_base)
208 {
209         struct pcb *pcb;
210
211         if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
212                 return (EINVAL);
213
214         pcb = td->td_pcb;
215         pcb->pcb_tpidr_el0 = (register_t)tls_base;
216         if (td == curthread)
217                 WRITE_SPECIALREG(tpidr_el0, tls_base);
218
219         return (0);
220 }
221
222 void
223 cpu_thread_exit(struct thread *td)
224 {
225 }
226
227 void
228 cpu_thread_alloc(struct thread *td)
229 {
230
231         td->td_pcb = (struct pcb *)(td->td_kstack +
232             td->td_kstack_pages * PAGE_SIZE) - 1;
233         td->td_frame = (struct trapframe *)STACKALIGN(
234             td->td_pcb - 1);
235 }
236
237 void
238 cpu_thread_free(struct thread *td)
239 {
240 }
241
242 void
243 cpu_thread_clean(struct thread *td)
244 {
245 }
246
247 /*
248  * Intercept the return address from a freshly forked process that has NOT
249  * been scheduled yet.
250  *
251  * This is needed to make kernel threads stay in kernel mode.
252  */
253 void
254 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
255 {
256
257         td->td_pcb->pcb_x[8] = (uintptr_t)func;
258         td->td_pcb->pcb_x[9] = (uintptr_t)arg;
259         td->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
260         td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
261         td->td_pcb->pcb_fpusaved = &td->td_pcb->pcb_fpustate;
262         td->td_pcb->pcb_vfpcpu = UINT_MAX;
263 }
264
265 void
266 cpu_exit(struct thread *td)
267 {
268 }
269
270 void
271 swi_vm(void *v)
272 {
273
274         if (busdma_swi_pending != 0)
275                 busdma_swi();
276 }