xref: /freebsd/sys/arm64/arm64/vm_machdep.c (revision 74ccb8ec)
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 #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/sysent.h>
38 #include <sys/unistd.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_page.h>
42 #include <vm/vm_map.h>
43 #include <vm/uma.h>
44 #include <vm/uma_int.h>
45 
46 #include <machine/armreg.h>
47 #include <machine/cpu.h>
48 #include <machine/md_var.h>
49 #include <machine/pcb.h>
50 #include <machine/frame.h>
51 
52 #ifdef VFP
53 #include <machine/vfp.h>
54 #endif
55 
56 #include <dev/psci/psci.h>
57 
58 /*
59  * Finish a fork operation, with process p2 nearly set up.
60  * Copy and update the pcb, set up the stack so that the child
61  * ready to run and return to user mode.
62  */
63 void
64 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
65 {
66 	struct pcb *pcb2;
67 	struct trapframe *tf;
68 
69 	if ((flags & RFPROC) == 0)
70 		return;
71 
72 	if (td1 == curthread) {
73 		/*
74 		 * Save the tpidr_el0 and the vfp state, these normally happen
75 		 * in cpu_switch, but if userland changes these then forks
76 		 * this may not have happened.
77 		 */
78 		td1->td_pcb->pcb_tpidr_el0 = READ_SPECIALREG(tpidr_el0);
79 		td1->td_pcb->pcb_tpidrro_el0 = READ_SPECIALREG(tpidrro_el0);
80 #ifdef VFP
81 		if ((td1->td_pcb->pcb_fpflags & PCB_FP_STARTED) != 0)
82 			vfp_save_state(td1, td1->td_pcb);
83 #endif
84 	}
85 
86 	pcb2 = (struct pcb *)(td2->td_kstack +
87 	    td2->td_kstack_pages * PAGE_SIZE) - 1;
88 
89 	td2->td_pcb = pcb2;
90 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
91 
92 	/* Clear the debug register state. */
93 	bzero(&pcb2->pcb_dbg_regs, sizeof(pcb2->pcb_dbg_regs));
94 
95 	ptrauth_fork(td2, td1);
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 = td1->td_frame->tf_spsr & (PSR_M_32 | PSR_DAIF);
102 
103 	td2->td_frame = tf;
104 
105 	/* Set the return value registers for fork() */
106 	td2->td_pcb->pcb_x[PCB_X19] = (uintptr_t)fork_return;
107 	td2->td_pcb->pcb_x[PCB_X20] = (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 
111 	vfp_new_thread(td2, td1, true);
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 = PSR_DAIF_DEFAULT;
116 
117 #if defined(PERTHREAD_SSP)
118 	/* Set the new canary */
119 	arc4random_buf(&td2->td_md.md_canary, sizeof(td2->td_md.md_canary));
120 #endif
121 }
122 
123 void
124 cpu_reset(void)
125 {
126 
127 	psci_reset();
128 
129 	printf("cpu_reset failed");
130 	while(1)
131 		__asm volatile("wfi" ::: "memory");
132 }
133 
134 void
135 cpu_thread_swapin(struct thread *td)
136 {
137 }
138 
139 void
140 cpu_thread_swapout(struct thread *td)
141 {
142 }
143 
144 void
145 cpu_set_syscall_retval(struct thread *td, int error)
146 {
147 	struct trapframe *frame;
148 
149 	frame = td->td_frame;
150 
151 	if (__predict_true(error == 0)) {
152 		frame->tf_x[0] = td->td_retval[0];
153 		frame->tf_x[1] = td->td_retval[1];
154 		frame->tf_spsr &= ~PSR_C;	/* carry bit */
155 		return;
156 	}
157 
158 	switch (error) {
159 	case ERESTART:
160 		frame->tf_elr -= 4;
161 		break;
162 	case EJUSTRETURN:
163 		break;
164 	default:
165 		frame->tf_spsr |= PSR_C;	/* carry bit */
166 		frame->tf_x[0] = error;
167 		break;
168 	}
169 }
170 
171 /*
172  * Initialize machine state, mostly pcb and trap frame for a new
173  * thread, about to return to userspace.  Put enough state in the new
174  * thread's PCB to get it to go back to the fork_return(), which
175  * finalizes the thread state and handles peculiarities of the first
176  * return to userspace for the new thread.
177  */
178 void
179 cpu_copy_thread(struct thread *td, struct thread *td0)
180 {
181 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
182 	bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
183 
184 	td->td_pcb->pcb_x[PCB_X19] = (uintptr_t)fork_return;
185 	td->td_pcb->pcb_x[PCB_X20] = (uintptr_t)td;
186 	td->td_pcb->pcb_x[PCB_LR] = (uintptr_t)fork_trampoline;
187 	td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
188 
189 	/* Update VFP state for the new thread */
190 	vfp_new_thread(td, td0, false);
191 
192 	/* Setup to release spin count in fork_exit(). */
193 	td->td_md.md_spinlock_count = 1;
194 	td->td_md.md_saved_daif = PSR_DAIF_DEFAULT;
195 
196 #if defined(PERTHREAD_SSP)
197 	/* Set the new canary */
198 	arc4random_buf(&td->td_md.md_canary, sizeof(td->td_md.md_canary));
199 #endif
200 
201 	/* Generate new pointer authentication keys. */
202 	ptrauth_copy_thread(td, td0);
203 }
204 
205 /*
206  * Set that machine state for performing an upcall that starts
207  * the entry function with the given argument.
208  */
209 void
210 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
211 	stack_t *stack)
212 {
213 	struct trapframe *tf = td->td_frame;
214 
215 	/* 32bits processes use r13 for sp */
216 	if (td->td_frame->tf_spsr & PSR_M_32) {
217 		tf->tf_x[13] = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
218 		if ((register_t)entry & 1)
219 			tf->tf_spsr |= PSR_T;
220 	} else
221 		tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
222 	tf->tf_elr = (register_t)entry;
223 	tf->tf_x[0] = (register_t)arg;
224 	tf->tf_x[29] = 0;
225 	tf->tf_lr = 0;
226 }
227 
228 int
229 cpu_set_user_tls(struct thread *td, void *tls_base)
230 {
231 	struct pcb *pcb;
232 
233 	if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
234 		return (EINVAL);
235 
236 	pcb = td->td_pcb;
237 	if (td->td_frame->tf_spsr & PSR_M_32) {
238 		/* 32bits arm stores the user TLS into tpidrro */
239 		pcb->pcb_tpidrro_el0 = (register_t)tls_base;
240 		pcb->pcb_tpidr_el0 = (register_t)tls_base;
241 		if (td == curthread) {
242 			WRITE_SPECIALREG(tpidrro_el0, tls_base);
243 			WRITE_SPECIALREG(tpidr_el0, tls_base);
244 		}
245 	} else {
246 		pcb->pcb_tpidr_el0 = (register_t)tls_base;
247 		if (td == curthread)
248 			WRITE_SPECIALREG(tpidr_el0, tls_base);
249 	}
250 
251 	return (0);
252 }
253 
254 void
255 cpu_thread_exit(struct thread *td)
256 {
257 }
258 
259 void
260 cpu_thread_alloc(struct thread *td)
261 {
262 
263 	td->td_pcb = (struct pcb *)(td->td_kstack +
264 	    td->td_kstack_pages * PAGE_SIZE) - 1;
265 	td->td_frame = (struct trapframe *)STACKALIGN(
266 	    (struct trapframe *)td->td_pcb - 1);
267 	ptrauth_thread_alloc(td);
268 }
269 
270 void
271 cpu_thread_free(struct thread *td)
272 {
273 }
274 
275 void
276 cpu_thread_clean(struct thread *td)
277 {
278 }
279 
280 /*
281  * Intercept the return address from a freshly forked process that has NOT
282  * been scheduled yet.
283  *
284  * This is needed to make kernel threads stay in kernel mode.
285  */
286 void
287 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
288 {
289 
290 	td->td_pcb->pcb_x[PCB_X19] = (uintptr_t)func;
291 	td->td_pcb->pcb_x[PCB_X20] = (uintptr_t)arg;
292 }
293 
294 void
295 cpu_exit(struct thread *td)
296 {
297 }
298 
299 bool
300 cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
301 {
302 
303 	return (true);
304 }
305 
306 int
307 cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
308     int com __unused, void *data __unused)
309 {
310 
311 	return (EINVAL);
312 }
313 
314 void
315 cpu_sync_core(void)
316 {
317 	/*
318 	 * Do nothing. According to ARM ARMv8 D1.11 Exception return
319 	 * If FEAT_ExS is not implemented, or if FEAT_ExS is
320 	 * implemented and the SCTLR_ELx.EOS field is set, exception
321 	 * return from ELx is a context synchronization event.
322 	 */
323 }
324