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