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 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 */
42
43 #include <sys/cdefs.h>
44 #include "opt_isa.h"
45 #include "opt_cpu.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/kernel.h>
52 #include <sys/ktr.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/mutex.h>
57 #include <sys/priv.h>
58 #include <sys/proc.h>
59 #include <sys/procctl.h>
60 #include <sys/smp.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/unistd.h>
64 #include <sys/vnode.h>
65 #include <sys/vmmeter.h>
66 #include <sys/wait.h>
67
68 #include <machine/cpu.h>
69 #include <machine/md_var.h>
70 #include <machine/pcb.h>
71 #include <machine/smp.h>
72 #include <machine/specialreg.h>
73 #include <machine/tss.h>
74
75 #include <vm/vm.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_param.h>
81
82 _Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
83 "OFFSETOF_MONITORBUF does not correspond with offset of pc_monitorbuf.");
84
85 void
set_top_of_stack_td(struct thread * td)86 set_top_of_stack_td(struct thread *td)
87 {
88 td->td_md.md_stack_base = td->td_kstack +
89 td->td_kstack_pages * PAGE_SIZE;
90 }
91
92 struct savefpu *
get_pcb_user_save_td(struct thread * td)93 get_pcb_user_save_td(struct thread *td)
94 {
95 KASSERT(((vm_offset_t)td->td_md.md_usr_fpu_save %
96 XSAVE_AREA_ALIGN) == 0,
97 ("Unaligned pcb_user_save area ptr %p td %p",
98 td->td_md.md_usr_fpu_save, td));
99 return (td->td_md.md_usr_fpu_save);
100 }
101
102 struct pcb *
get_pcb_td(struct thread * td)103 get_pcb_td(struct thread *td)
104 {
105
106 return (&td->td_md.md_pcb);
107 }
108
109 struct savefpu *
get_pcb_user_save_pcb(struct pcb * pcb)110 get_pcb_user_save_pcb(struct pcb *pcb)
111 {
112 struct thread *td;
113
114 td = __containerof(pcb, struct thread, td_md.md_pcb);
115 return (get_pcb_user_save_td(td));
116 }
117
118 void *
alloc_fpusave(int flags)119 alloc_fpusave(int flags)
120 {
121 void *res;
122 struct savefpu_ymm *sf;
123
124 res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
125 if (use_xsave) {
126 sf = (struct savefpu_ymm *)res;
127 bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
128 sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
129 }
130 return (res);
131 }
132
133 /*
134 * Common code shared between cpu_fork() and cpu_copy_thread() for
135 * initializing a thread.
136 */
137 static void
copy_thread(struct thread * td1,struct thread * td2)138 copy_thread(struct thread *td1, struct thread *td2)
139 {
140 struct pcb *pcb2;
141
142 pcb2 = td2->td_pcb;
143
144 /* Ensure that td1's pcb is up to date for user threads. */
145 if ((td2->td_pflags & TDP_KTHREAD) == 0) {
146 MPASS(td1 == curthread);
147 fpuexit(td1);
148 update_pcb_bases(td1->td_pcb);
149 }
150
151 /* Copy td1's pcb */
152 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
153
154 /* Properly initialize pcb_save */
155 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
156
157 /* Kernel threads start with clean FPU and segment bases. */
158 if ((td2->td_pflags & TDP_KTHREAD) != 0) {
159 pcb2->pcb_fsbase = 0;
160 pcb2->pcb_gsbase = 0;
161 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
162 PCB_KERNFPU | PCB_KERNFPU_THR);
163 } else {
164 MPASS((pcb2->pcb_flags & (PCB_KERNFPU | PCB_KERNFPU_THR)) == 0);
165 bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
166 cpu_max_ext_state_size);
167 }
168
169 td2->td_frame = (struct trapframe *)td2->td_md.md_stack_base - 1;
170
171 /*
172 * Set registers for trampoline to user mode. Leave space for the
173 * return address on stack. These are the kernel mode register values.
174 */
175 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */
176 pcb2->pcb_rbp = 0;
177 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
178 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */
179 pcb2->pcb_rip = (register_t)fork_trampoline;
180 /*-
181 * pcb2->pcb_dr*: cloned above.
182 * pcb2->pcb_savefpu: cloned above.
183 * pcb2->pcb_flags: cloned above.
184 * pcb2->pcb_onfault: cloned above (always NULL here?).
185 * pcb2->pcb_[fg]sbase: cloned above
186 */
187
188 pcb2->pcb_tssp = NULL;
189
190 /* Setup to release spin count in fork_exit(). */
191 td2->td_md.md_spinlock_count = 1;
192 td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
193 pmap_thread_init_invl_gen(td2);
194
195 /*
196 * Copy the trap frame for the return to user mode as if from a syscall.
197 * This copies most of the user mode register values. Some of these
198 * registers are rewritten by cpu_set_upcall() and linux_set_upcall().
199 */
200 if ((td1->td_proc->p_flag & P_KPROC) == 0) {
201 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
202
203 /*
204 * If the current thread has the trap bit set (i.e. a debugger
205 * had single stepped the process to the system call), we need
206 * to clear the trap flag from the new frame. Otherwise, the new
207 * thread will receive a (likely unexpected) SIGTRAP when it
208 * executes the first instruction after returning to userland.
209 */
210 td2->td_frame->tf_rflags &= ~PSL_T;
211 }
212 }
213
214 /*
215 * Finish a fork operation, with process p2 nearly set up.
216 * Copy and update the pcb, set up the stack so that the child
217 * ready to run and return to user mode.
218 */
219 void
cpu_fork(struct thread * td1,struct proc * p2,struct thread * td2,int flags)220 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
221 {
222 struct proc *p1;
223 struct pcb *pcb2;
224 struct mdproc *mdp1, *mdp2;
225 struct proc_ldt *pldt;
226
227 p1 = td1->td_proc;
228 if ((flags & RFPROC) == 0) {
229 if ((flags & RFMEM) == 0) {
230 /* unshare user LDT */
231 mdp1 = &p1->p_md;
232 mtx_lock(&dt_lock);
233 if ((pldt = mdp1->md_ldt) != NULL &&
234 pldt->ldt_refcnt > 1 &&
235 user_ldt_alloc(p1, 1) == NULL)
236 panic("could not copy LDT");
237 mtx_unlock(&dt_lock);
238 }
239 return;
240 }
241
242 /* Point the stack and pcb to the actual location */
243 set_top_of_stack_td(td2);
244 td2->td_pcb = pcb2 = get_pcb_td(td2);
245
246 copy_thread(td1, td2);
247
248 /* Reset debug registers in the new process */
249 x86_clear_dbregs(pcb2);
250
251 /* Point mdproc and then copy over p1's contents */
252 mdp2 = &p2->p_md;
253 bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
254
255 /* Set child return values. */
256 p2->p_sysent->sv_set_fork_retval(td2);
257
258 /* As on i386, do not copy io permission bitmap. */
259 pcb2->pcb_tssp = NULL;
260
261 /* New segment registers. */
262 set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
263
264 /* Copy the LDT, if necessary. */
265 mdp1 = &td1->td_proc->p_md;
266 mdp2 = &p2->p_md;
267 if (mdp1->md_ldt == NULL) {
268 mdp2->md_ldt = NULL;
269 return;
270 }
271 mtx_lock(&dt_lock);
272 if (mdp1->md_ldt != NULL) {
273 if (flags & RFMEM) {
274 mdp1->md_ldt->ldt_refcnt++;
275 mdp2->md_ldt = mdp1->md_ldt;
276 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
277 system_segment_descriptor));
278 } else {
279 mdp2->md_ldt = NULL;
280 mdp2->md_ldt = user_ldt_alloc(p2, 0);
281 if (mdp2->md_ldt == NULL)
282 panic("could not copy LDT");
283 amd64_set_ldt_data(td2, 0, max_ldt_segment,
284 (struct user_segment_descriptor *)
285 mdp1->md_ldt->ldt_base);
286 }
287 } else
288 mdp2->md_ldt = NULL;
289 mtx_unlock(&dt_lock);
290
291 /*
292 * Now, cpu_switch() can schedule the new process.
293 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
294 * containing the return address when exiting cpu_switch.
295 * This will normally be to fork_trampoline(), which will have
296 * %rbx loaded with the new proc's pointer. fork_trampoline()
297 * will set up a stack to call fork_return(p, frame); to complete
298 * the return to user-mode.
299 */
300 }
301
302 void
x86_set_fork_retval(struct thread * td)303 x86_set_fork_retval(struct thread *td)
304 {
305 struct trapframe *frame = td->td_frame;
306
307 frame->tf_rax = 0; /* Child returns zero */
308 frame->tf_rflags &= ~PSL_C; /* success */
309 frame->tf_rdx = 1; /* System V emulation */
310 }
311
312 /*
313 * Intercept the return address from a freshly forked process that has NOT
314 * been scheduled yet.
315 *
316 * This is needed to make kernel threads stay in kernel mode.
317 */
318 void
cpu_fork_kthread_handler(struct thread * td,void (* func)(void *),void * arg)319 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
320 {
321 /*
322 * Note that the trap frame follows the args, so the function
323 * is really called like this: func(arg, frame);
324 */
325 td->td_pcb->pcb_r12 = (long) func; /* function */
326 td->td_pcb->pcb_rbx = (long) arg; /* first arg */
327 }
328
329 void
cpu_exit(struct thread * td)330 cpu_exit(struct thread *td)
331 {
332
333 /*
334 * If this process has a custom LDT, release it.
335 */
336 if (td->td_proc->p_md.md_ldt != NULL)
337 user_ldt_free(td);
338 }
339
340 void
cpu_thread_exit(struct thread * td)341 cpu_thread_exit(struct thread *td)
342 {
343 struct pcb *pcb;
344
345 critical_enter();
346 if (td == PCPU_GET(fpcurthread))
347 fpudrop();
348 critical_exit();
349
350 pcb = td->td_pcb;
351
352 /* Disable any hardware breakpoints. */
353 if (pcb->pcb_flags & PCB_DBREGS) {
354 reset_dbregs();
355 clear_pcb_flags(pcb, PCB_DBREGS);
356 }
357 }
358
359 void
cpu_thread_clean(struct thread * td)360 cpu_thread_clean(struct thread *td)
361 {
362 struct pcb *pcb;
363
364 pcb = td->td_pcb;
365
366 /*
367 * Clean TSS/iomap
368 */
369 if (pcb->pcb_tssp != NULL) {
370 pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
371 (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
372 kmem_free(pcb->pcb_tssp, ctob(IOPAGES + 1));
373 pcb->pcb_tssp = NULL;
374 }
375 }
376
377 void
cpu_thread_alloc(struct thread * td)378 cpu_thread_alloc(struct thread *td)
379 {
380 struct pcb *pcb;
381 struct xstate_hdr *xhdr;
382
383 set_top_of_stack_td(td);
384 td->td_pcb = pcb = get_pcb_td(td);
385 td->td_frame = (struct trapframe *)td->td_md.md_stack_base - 1;
386 td->td_md.md_usr_fpu_save = fpu_save_area_alloc();
387 pcb->pcb_save = get_pcb_user_save_pcb(pcb);
388 if (use_xsave) {
389 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
390 bzero(xhdr, sizeof(*xhdr));
391 xhdr->xstate_bv = xsave_mask;
392 }
393 }
394
395 void
cpu_thread_free(struct thread * td)396 cpu_thread_free(struct thread *td)
397 {
398 cpu_thread_clean(td);
399
400 fpu_save_area_free(td->td_md.md_usr_fpu_save);
401 td->td_md.md_usr_fpu_save = NULL;
402 }
403
404 bool
cpu_exec_vmspace_reuse(struct proc * p,vm_map_t map)405 cpu_exec_vmspace_reuse(struct proc *p, vm_map_t map)
406 {
407
408 return (((curproc->p_md.md_flags & P_MD_KPTI) != 0) ==
409 (vm_map_pmap(map)->pm_ucr3 != PMAP_NO_CR3));
410 }
411
412 static void
cpu_procctl_kpti_ctl(struct proc * p,int val)413 cpu_procctl_kpti_ctl(struct proc *p, int val)
414 {
415
416 if (pti && val == PROC_KPTI_CTL_ENABLE_ON_EXEC)
417 p->p_md.md_flags |= P_MD_KPTI;
418 if (val == PROC_KPTI_CTL_DISABLE_ON_EXEC)
419 p->p_md.md_flags &= ~P_MD_KPTI;
420 }
421
422 static void
cpu_procctl_kpti_status(struct proc * p,int * val)423 cpu_procctl_kpti_status(struct proc *p, int *val)
424 {
425 *val = (p->p_md.md_flags & P_MD_KPTI) != 0 ?
426 PROC_KPTI_CTL_ENABLE_ON_EXEC:
427 PROC_KPTI_CTL_DISABLE_ON_EXEC;
428 if (vmspace_pmap(p->p_vmspace)->pm_ucr3 != PMAP_NO_CR3)
429 *val |= PROC_KPTI_STATUS_ACTIVE;
430 }
431
432 static int
cpu_procctl_la_ctl(struct proc * p,int val)433 cpu_procctl_la_ctl(struct proc *p, int val)
434 {
435 int error;
436
437 error = 0;
438 switch (val) {
439 case PROC_LA_CTL_LA48_ON_EXEC:
440 p->p_md.md_flags |= P_MD_LA48;
441 p->p_md.md_flags &= ~P_MD_LA57;
442 break;
443 case PROC_LA_CTL_LA57_ON_EXEC:
444 if (la57) {
445 p->p_md.md_flags &= ~P_MD_LA48;
446 p->p_md.md_flags |= P_MD_LA57;
447 } else {
448 error = ENOTSUP;
449 }
450 break;
451 case PROC_LA_CTL_DEFAULT_ON_EXEC:
452 p->p_md.md_flags &= ~(P_MD_LA48 | P_MD_LA57);
453 break;
454 }
455 return (error);
456 }
457
458 static void
cpu_procctl_la_status(struct proc * p,int * val)459 cpu_procctl_la_status(struct proc *p, int *val)
460 {
461 int res;
462
463 if ((p->p_md.md_flags & P_MD_LA48) != 0)
464 res = PROC_LA_CTL_LA48_ON_EXEC;
465 else if ((p->p_md.md_flags & P_MD_LA57) != 0)
466 res = PROC_LA_CTL_LA57_ON_EXEC;
467 else
468 res = PROC_LA_CTL_DEFAULT_ON_EXEC;
469 if (p->p_sysent->sv_maxuser == VM_MAXUSER_ADDRESS_LA48)
470 res |= PROC_LA_STATUS_LA48;
471 else
472 res |= PROC_LA_STATUS_LA57;
473 *val = res;
474 }
475
476 int
cpu_procctl(struct thread * td,int idtype,id_t id,int com,void * data)477 cpu_procctl(struct thread *td, int idtype, id_t id, int com, void *data)
478 {
479 struct proc *p;
480 int error, val;
481
482 switch (com) {
483 case PROC_KPTI_CTL:
484 case PROC_KPTI_STATUS:
485 case PROC_LA_CTL:
486 case PROC_LA_STATUS:
487 if (idtype != P_PID) {
488 error = EINVAL;
489 break;
490 }
491 if (com == PROC_KPTI_CTL) {
492 /* sad but true and not a joke */
493 error = priv_check(td, PRIV_IO);
494 if (error != 0)
495 break;
496 }
497 if (com == PROC_KPTI_CTL || com == PROC_LA_CTL) {
498 error = copyin(data, &val, sizeof(val));
499 if (error != 0)
500 break;
501 }
502 if (com == PROC_KPTI_CTL &&
503 val != PROC_KPTI_CTL_ENABLE_ON_EXEC &&
504 val != PROC_KPTI_CTL_DISABLE_ON_EXEC) {
505 error = EINVAL;
506 break;
507 }
508 if (com == PROC_LA_CTL &&
509 val != PROC_LA_CTL_LA48_ON_EXEC &&
510 val != PROC_LA_CTL_LA57_ON_EXEC &&
511 val != PROC_LA_CTL_DEFAULT_ON_EXEC) {
512 error = EINVAL;
513 break;
514 }
515 error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
516 if (error != 0)
517 break;
518 switch (com) {
519 case PROC_KPTI_CTL:
520 cpu_procctl_kpti_ctl(p, val);
521 break;
522 case PROC_KPTI_STATUS:
523 cpu_procctl_kpti_status(p, &val);
524 break;
525 case PROC_LA_CTL:
526 error = cpu_procctl_la_ctl(p, val);
527 break;
528 case PROC_LA_STATUS:
529 cpu_procctl_la_status(p, &val);
530 break;
531 }
532 PROC_UNLOCK(p);
533 if (com == PROC_KPTI_STATUS || com == PROC_LA_STATUS)
534 error = copyout(&val, data, sizeof(val));
535 break;
536 default:
537 error = EINVAL;
538 break;
539 }
540 return (error);
541 }
542
543 void
cpu_set_syscall_retval(struct thread * td,int error)544 cpu_set_syscall_retval(struct thread *td, int error)
545 {
546 struct trapframe *frame;
547
548 frame = td->td_frame;
549 if (__predict_true(error == 0)) {
550 frame->tf_rax = td->td_retval[0];
551 frame->tf_rdx = td->td_retval[1];
552 frame->tf_rflags &= ~PSL_C;
553 return;
554 }
555
556 switch (error) {
557 case ERESTART:
558 /*
559 * Reconstruct pc, we know that 'syscall' is 2 bytes,
560 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
561 * We saved this in tf_err.
562 * %r10 (which was holding the value of %rcx) is restored
563 * for the next iteration.
564 * %r10 restore is only required for freebsd/amd64 processes,
565 * but shall be innocent for any ia32 ABI.
566 *
567 * Require full context restore to get the arguments
568 * in the registers reloaded at return to usermode.
569 */
570 frame->tf_rip -= frame->tf_err;
571 frame->tf_r10 = frame->tf_rcx;
572 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
573 break;
574
575 case EJUSTRETURN:
576 break;
577
578 default:
579 frame->tf_rax = error;
580 frame->tf_rflags |= PSL_C;
581 break;
582 }
583 }
584
585 /*
586 * Initialize machine state, mostly pcb and trap frame for a new
587 * thread, about to return to userspace. Put enough state in the new
588 * thread's PCB to get it to go back to the fork_return(), which
589 * finalizes the thread state and handles peculiarities of the first
590 * return to userspace for the new thread.
591 */
592 void
cpu_copy_thread(struct thread * td,struct thread * td0)593 cpu_copy_thread(struct thread *td, struct thread *td0)
594 {
595 copy_thread(td0, td);
596
597 set_pcb_flags_raw(td->td_pcb, PCB_FULL_IRET);
598 }
599
600 /*
601 * Set that machine state for performing an upcall that starts
602 * the entry function with the given argument.
603 */
604 int
cpu_set_upcall(struct thread * td,void (* entry)(void *),void * arg,stack_t * stack)605 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
606 stack_t *stack)
607 {
608 #ifdef COMPAT_FREEBSD32
609 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
610 /*
611 * Set the trap frame to point at the beginning of the entry
612 * function.
613 */
614 td->td_frame->tf_rbp = 0;
615 td->td_frame->tf_rsp =
616 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
617 td->td_frame->tf_rip = (uintptr_t)entry;
618
619 /* Return address sentinel value to stop stack unwinding. */
620 if (suword32((void *)td->td_frame->tf_rsp, 0) != 0)
621 return (EFAULT);
622
623 /* Pass the argument to the entry point. */
624 if (suword32(
625 (void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
626 (uint32_t)(uintptr_t)arg) != 0)
627 return (EFAULT);
628 return (0);
629 }
630 #endif
631
632 /*
633 * Set the trap frame to point at the beginning of the uts
634 * function.
635 */
636 td->td_frame->tf_rbp = 0;
637 td->td_frame->tf_rsp =
638 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
639 td->td_frame->tf_rsp -= 8;
640 td->td_frame->tf_rip = (register_t)entry;
641 td->td_frame->tf_ds = _udatasel;
642 td->td_frame->tf_es = _udatasel;
643 td->td_frame->tf_fs = _ufssel;
644 td->td_frame->tf_gs = _ugssel;
645 td->td_frame->tf_flags = TF_HASSEGS;
646
647 /* Return address sentinel value to stop stack unwinding. */
648 if (suword((void *)td->td_frame->tf_rsp, 0) != 0)
649 return (EFAULT);
650
651 /* Pass the argument to the entry point. */
652 td->td_frame->tf_rdi = (register_t)arg;
653
654 return (0);
655 }
656
657 int
cpu_set_user_tls(struct thread * td,void * tls_base)658 cpu_set_user_tls(struct thread *td, void *tls_base)
659 {
660 struct pcb *pcb;
661
662 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
663 return (EINVAL);
664
665 pcb = td->td_pcb;
666 set_pcb_flags(pcb, PCB_FULL_IRET);
667 #ifdef COMPAT_FREEBSD32
668 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
669 pcb->pcb_gsbase = (register_t)tls_base;
670 return (0);
671 }
672 #endif
673 pcb->pcb_fsbase = (register_t)tls_base;
674 return (0);
675 }
676
677 void
cpu_update_pcb(struct thread * td)678 cpu_update_pcb(struct thread *td)
679 {
680 MPASS(td == curthread);
681 update_pcb_bases(td->td_pcb);
682 }
683