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