xref: /freebsd/sys/amd64/amd64/vm_machdep.c (revision 8223717c)
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 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
95 }
96 
97 struct savefpu *
98 get_pcb_user_save_td(struct thread *td)
99 {
100 	vm_offset_t p;
101 
102 	p = td->td_md.md_stack_base;
103 	KASSERT((p % XSAVE_AREA_ALIGN) == 0,
104 	    ("Unaligned pcb_user_save area ptr %#lx td %p", p, td));
105 	return ((struct savefpu *)p);
106 }
107 
108 struct pcb *
109 get_pcb_td(struct thread *td)
110 {
111 
112 	return (&td->td_md.md_pcb);
113 }
114 
115 struct savefpu *
116 get_pcb_user_save_pcb(struct pcb *pcb)
117 {
118 	struct thread *td;
119 
120 	td = __containerof(pcb, struct thread, td_md.md_pcb);
121 	return (get_pcb_user_save_td(td));
122 }
123 
124 void *
125 alloc_fpusave(int flags)
126 {
127 	void *res;
128 	struct savefpu_ymm *sf;
129 
130 	res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
131 	if (use_xsave) {
132 		sf = (struct savefpu_ymm *)res;
133 		bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
134 		sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
135 	}
136 	return (res);
137 }
138 
139 /*
140  * Common code shared between cpu_fork() and cpu_copy_thread() for
141  * initializing a thread.
142  */
143 static void
144 copy_thread(struct thread *td1, struct thread *td2)
145 {
146 	struct pcb *pcb2;
147 
148 	pcb2 = td2->td_pcb;
149 
150 	/* Ensure that td1's pcb is up to date for user threads. */
151 	if ((td2->td_pflags & TDP_KTHREAD) == 0) {
152 		MPASS(td1 == curthread);
153 		fpuexit(td1);
154 		update_pcb_bases(td1->td_pcb);
155 	}
156 
157 	/* Copy td1's pcb */
158 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
159 
160 	/* Properly initialize pcb_save */
161 	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
162 
163 	/* Kernel threads start with clean FPU and segment bases. */
164 	if ((td2->td_pflags & TDP_KTHREAD) != 0) {
165 		pcb2->pcb_fsbase = 0;
166 		pcb2->pcb_gsbase = 0;
167 		clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
168 		    PCB_KERNFPU | PCB_KERNFPU_THR);
169 	} else {
170 		MPASS((pcb2->pcb_flags & (PCB_KERNFPU | PCB_KERNFPU_THR)) == 0);
171 		bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
172 		    cpu_max_ext_state_size);
173 	}
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 	/* Setup to release spin count in fork_exit(). */
193 	td2->td_md.md_spinlock_count = 1;
194 	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
195 	pmap_thread_init_invl_gen(td2);
196 }
197 
198 /*
199  * Finish a fork operation, with process p2 nearly set up.
200  * Copy and update the pcb, set up the stack so that the child
201  * ready to run and return to user mode.
202  */
203 void
204 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
205 {
206 	struct proc *p1;
207 	struct pcb *pcb2;
208 	struct mdproc *mdp1, *mdp2;
209 	struct proc_ldt *pldt;
210 
211 	p1 = td1->td_proc;
212 	if ((flags & RFPROC) == 0) {
213 		if ((flags & RFMEM) == 0) {
214 			/* unshare user LDT */
215 			mdp1 = &p1->p_md;
216 			mtx_lock(&dt_lock);
217 			if ((pldt = mdp1->md_ldt) != NULL &&
218 			    pldt->ldt_refcnt > 1 &&
219 			    user_ldt_alloc(p1, 1) == NULL)
220 				panic("could not copy LDT");
221 			mtx_unlock(&dt_lock);
222 		}
223 		return;
224 	}
225 
226 	/* Point the stack and pcb to the actual location */
227 	set_top_of_stack_td(td2);
228 	td2->td_pcb = pcb2 = get_pcb_td(td2);
229 
230 	copy_thread(td1, td2);
231 
232 	/* Reset debug registers in the new process */
233 	pcb2->pcb_dr0 = 0;
234 	pcb2->pcb_dr1 = 0;
235 	pcb2->pcb_dr2 = 0;
236 	pcb2->pcb_dr3 = 0;
237 	pcb2->pcb_dr6 = 0;
238 	pcb2->pcb_dr7 = 0;
239 
240 	/* Point mdproc and then copy over p1's contents */
241 	mdp2 = &p2->p_md;
242 	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
243 
244 	/*
245 	 * Copy the trap frame for the return to user mode as if from a
246 	 * syscall.  This copies most of the user mode register values.
247 	 */
248 	td2->td_frame = (struct trapframe *)td2->td_md.md_stack_base - 1;
249 	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
250 
251 	td2->td_frame->tf_rax = 0;		/* Child returns zero */
252 	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
253 	td2->td_frame->tf_rdx = 1;
254 
255 	/*
256 	 * If the parent process has the trap bit set (i.e. a debugger
257 	 * had single stepped the process to the system call), we need
258 	 * to clear the trap flag from the new frame.
259 	 */
260 	td2->td_frame->tf_rflags &= ~PSL_T;
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 /*
307  * Intercept the return address from a freshly forked process that has NOT
308  * been scheduled yet.
309  *
310  * This is needed to make kernel threads stay in kernel mode.
311  */
312 void
313 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
314 {
315 	/*
316 	 * Note that the trap frame follows the args, so the function
317 	 * is really called like this:  func(arg, frame);
318 	 */
319 	td->td_pcb->pcb_r12 = (long) func;	/* function */
320 	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
321 }
322 
323 void
324 cpu_exit(struct thread *td)
325 {
326 
327 	/*
328 	 * If this process has a custom LDT, release it.
329 	 */
330 	if (td->td_proc->p_md.md_ldt != NULL)
331 		user_ldt_free(td);
332 }
333 
334 void
335 cpu_thread_exit(struct thread *td)
336 {
337 	struct pcb *pcb;
338 
339 	critical_enter();
340 	if (td == PCPU_GET(fpcurthread))
341 		fpudrop();
342 	critical_exit();
343 
344 	pcb = td->td_pcb;
345 
346 	/* Disable any hardware breakpoints. */
347 	if (pcb->pcb_flags & PCB_DBREGS) {
348 		reset_dbregs();
349 		clear_pcb_flags(pcb, PCB_DBREGS);
350 	}
351 }
352 
353 void
354 cpu_thread_clean(struct thread *td)
355 {
356 	struct pcb *pcb;
357 
358 	pcb = td->td_pcb;
359 
360 	/*
361 	 * Clean TSS/iomap
362 	 */
363 	if (pcb->pcb_tssp != NULL) {
364 		pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
365 		    (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
366 		kmem_free((vm_offset_t)pcb->pcb_tssp, ctob(IOPAGES + 1));
367 		pcb->pcb_tssp = NULL;
368 	}
369 }
370 
371 void
372 cpu_thread_swapin(struct thread *td)
373 {
374 }
375 
376 void
377 cpu_thread_swapout(struct thread *td)
378 {
379 }
380 
381 void
382 cpu_thread_alloc(struct thread *td)
383 {
384 	struct pcb *pcb;
385 	struct xstate_hdr *xhdr;
386 
387 	set_top_of_stack_td(td);
388 	td->td_pcb = pcb = get_pcb_td(td);
389 	td->td_frame = (struct trapframe *)td->td_md.md_stack_base - 1;
390 	pcb->pcb_save = get_pcb_user_save_pcb(pcb);
391 	if (use_xsave) {
392 		xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
393 		bzero(xhdr, sizeof(*xhdr));
394 		xhdr->xstate_bv = xsave_mask;
395 	}
396 }
397 
398 void
399 cpu_thread_free(struct thread *td)
400 {
401 
402 	cpu_thread_clean(td);
403 }
404 
405 bool
406 cpu_exec_vmspace_reuse(struct proc *p, vm_map_t map)
407 {
408 
409 	return (((curproc->p_md.md_flags & P_MD_KPTI) != 0) ==
410 	    (vm_map_pmap(map)->pm_ucr3 != PMAP_NO_CR3));
411 }
412 
413 static void
414 cpu_procctl_kpti_ctl(struct proc *p, int val)
415 {
416 
417 	if (pti && val == PROC_KPTI_CTL_ENABLE_ON_EXEC)
418 		p->p_md.md_flags |= P_MD_KPTI;
419 	if (val == PROC_KPTI_CTL_DISABLE_ON_EXEC)
420 		p->p_md.md_flags &= ~P_MD_KPTI;
421 }
422 
423 static void
424 cpu_procctl_kpti_status(struct proc *p, int *val)
425 {
426 	*val = (p->p_md.md_flags & P_MD_KPTI) != 0 ?
427 	    PROC_KPTI_CTL_ENABLE_ON_EXEC:
428 	    PROC_KPTI_CTL_DISABLE_ON_EXEC;
429 	if (vmspace_pmap(p->p_vmspace)->pm_ucr3 != PMAP_NO_CR3)
430 		*val |= PROC_KPTI_STATUS_ACTIVE;
431 }
432 
433 static int
434 cpu_procctl_la_ctl(struct proc *p, int val)
435 {
436 	int error;
437 
438 	error = 0;
439 	switch (val) {
440 	case PROC_LA_CTL_LA48_ON_EXEC:
441 		p->p_md.md_flags |= P_MD_LA48;
442 		p->p_md.md_flags &= ~P_MD_LA57;
443 		break;
444 	case PROC_LA_CTL_LA57_ON_EXEC:
445 		if (la57) {
446 			p->p_md.md_flags &= ~P_MD_LA48;
447 			p->p_md.md_flags |= P_MD_LA57;
448 		} else {
449 			error = ENOTSUP;
450 		}
451 		break;
452 	case PROC_LA_CTL_DEFAULT_ON_EXEC:
453 		p->p_md.md_flags &= ~(P_MD_LA48 | P_MD_LA57);
454 		break;
455 	}
456 	return (error);
457 }
458 
459 static void
460 cpu_procctl_la_status(struct proc *p, int *val)
461 {
462 	int res;
463 
464 	if ((p->p_md.md_flags & P_MD_LA48) != 0)
465 		res = PROC_LA_CTL_LA48_ON_EXEC;
466 	else if ((p->p_md.md_flags & P_MD_LA57) != 0)
467 		res = PROC_LA_CTL_LA57_ON_EXEC;
468 	else
469 		res = PROC_LA_CTL_DEFAULT_ON_EXEC;
470 	if (p->p_sysent->sv_maxuser == VM_MAXUSER_ADDRESS_LA48)
471 		res |= PROC_LA_STATUS_LA48;
472 	else
473 		res |= PROC_LA_STATUS_LA57;
474 	*val = res;
475 }
476 
477 int
478 cpu_procctl(struct thread *td, int idtype, id_t id, int com, void *data)
479 {
480 	struct proc *p;
481 	int error, val;
482 
483 	switch (com) {
484 	case PROC_KPTI_CTL:
485 	case PROC_KPTI_STATUS:
486 	case PROC_LA_CTL:
487 	case PROC_LA_STATUS:
488 		if (idtype != P_PID) {
489 			error = EINVAL;
490 			break;
491 		}
492 		if (com == PROC_KPTI_CTL) {
493 			/* sad but true and not a joke */
494 			error = priv_check(td, PRIV_IO);
495 			if (error != 0)
496 				break;
497 		}
498 		if (com == PROC_KPTI_CTL || com == PROC_LA_CTL) {
499 			error = copyin(data, &val, sizeof(val));
500 			if (error != 0)
501 				break;
502 		}
503 		if (com == PROC_KPTI_CTL &&
504 		    val != PROC_KPTI_CTL_ENABLE_ON_EXEC &&
505 		    val != PROC_KPTI_CTL_DISABLE_ON_EXEC) {
506 			error = EINVAL;
507 			break;
508 		}
509 		if (com == PROC_LA_CTL &&
510 		    val != PROC_LA_CTL_LA48_ON_EXEC &&
511 		    val != PROC_LA_CTL_LA57_ON_EXEC &&
512 		    val != PROC_LA_CTL_DEFAULT_ON_EXEC) {
513 			error = EINVAL;
514 			break;
515 		}
516 		error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
517 		if (error != 0)
518 			break;
519 		switch (com) {
520 		case PROC_KPTI_CTL:
521 			cpu_procctl_kpti_ctl(p, val);
522 			break;
523 		case PROC_KPTI_STATUS:
524 			cpu_procctl_kpti_status(p, &val);
525 			break;
526 		case PROC_LA_CTL:
527 			error = cpu_procctl_la_ctl(p, val);
528 			break;
529 		case PROC_LA_STATUS:
530 			cpu_procctl_la_status(p, &val);
531 			break;
532 		}
533 		PROC_UNLOCK(p);
534 		if (com == PROC_KPTI_STATUS || com == PROC_LA_STATUS)
535 			error = copyout(&val, data, sizeof(val));
536 		break;
537 	default:
538 		error = EINVAL;
539 		break;
540 	}
541 	return (error);
542 }
543 
544 void
545 cpu_set_syscall_retval(struct thread *td, int error)
546 {
547 	struct trapframe *frame;
548 
549 	frame = td->td_frame;
550 	if (__predict_true(error == 0)) {
551 		frame->tf_rax = td->td_retval[0];
552 		frame->tf_rdx = td->td_retval[1];
553 		frame->tf_rflags &= ~PSL_C;
554 		return;
555 	}
556 
557 	switch (error) {
558 	case ERESTART:
559 		/*
560 		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
561 		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
562 		 * We saved this in tf_err.
563 		 * %r10 (which was holding the value of %rcx) is restored
564 		 * for the next iteration.
565 		 * %r10 restore is only required for freebsd/amd64 processes,
566 		 * but shall be innocent for any ia32 ABI.
567 		 *
568 		 * Require full context restore to get the arguments
569 		 * in the registers reloaded at return to usermode.
570 		 */
571 		frame->tf_rip -= frame->tf_err;
572 		frame->tf_r10 = frame->tf_rcx;
573 		set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
574 		break;
575 
576 	case EJUSTRETURN:
577 		break;
578 
579 	default:
580 		frame->tf_rax = error;
581 		frame->tf_rflags |= PSL_C;
582 		break;
583 	}
584 }
585 
586 /*
587  * Initialize machine state, mostly pcb and trap frame for a new
588  * thread, about to return to userspace.  Put enough state in the new
589  * thread's PCB to get it to go back to the fork_return(), which
590  * finalizes the thread state and handles peculiarities of the first
591  * return to userspace for the new thread.
592  */
593 void
594 cpu_copy_thread(struct thread *td, struct thread *td0)
595 {
596 	copy_thread(td0, td);
597 
598 	/*
599 	 * Copy user general-purpose registers.
600 	 *
601 	 * Some of these registers are rewritten by cpu_set_upcall()
602 	 * and linux_set_upcall().
603 	 */
604 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
605 
606 	/* If the current thread has the trap bit set (i.e. a debugger had
607 	 * single stepped the process to the system call), we need to clear
608 	 * the trap flag from the new frame. Otherwise, the new thread will
609 	 * receive a (likely unexpected) SIGTRAP when it executes the first
610 	 * instruction after returning to userland.
611 	 */
612 	td->td_frame->tf_rflags &= ~PSL_T;
613 
614 	set_pcb_flags_raw(td->td_pcb, PCB_FULL_IRET);
615 }
616 
617 /*
618  * Set that machine state for performing an upcall that starts
619  * the entry function with the given argument.
620  */
621 void
622 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
623     stack_t *stack)
624 {
625 
626 	/*
627 	 * Do any extra cleaning that needs to be done.
628 	 * The thread may have optional components
629 	 * that are not present in a fresh thread.
630 	 * This may be a recycled thread so make it look
631 	 * as though it's newly allocated.
632 	 */
633 	cpu_thread_clean(td);
634 
635 #ifdef COMPAT_FREEBSD32
636 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
637 		/*
638 		 * Set the trap frame to point at the beginning of the entry
639 		 * function.
640 		 */
641 		td->td_frame->tf_rbp = 0;
642 		td->td_frame->tf_rsp =
643 		   (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
644 		td->td_frame->tf_rip = (uintptr_t)entry;
645 
646 		/* Return address sentinel value to stop stack unwinding. */
647 		suword32((void *)td->td_frame->tf_rsp, 0);
648 
649 		/* Pass the argument to the entry point. */
650 		suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
651 		    (uint32_t)(uintptr_t)arg);
652 
653 		return;
654 	}
655 #endif
656 
657 	/*
658 	 * Set the trap frame to point at the beginning of the uts
659 	 * function.
660 	 */
661 	td->td_frame->tf_rbp = 0;
662 	td->td_frame->tf_rsp =
663 	    ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
664 	td->td_frame->tf_rsp -= 8;
665 	td->td_frame->tf_rip = (register_t)entry;
666 	td->td_frame->tf_ds = _udatasel;
667 	td->td_frame->tf_es = _udatasel;
668 	td->td_frame->tf_fs = _ufssel;
669 	td->td_frame->tf_gs = _ugssel;
670 	td->td_frame->tf_flags = TF_HASSEGS;
671 
672 	/* Return address sentinel value to stop stack unwinding. */
673 	suword((void *)td->td_frame->tf_rsp, 0);
674 
675 	/* Pass the argument to the entry point. */
676 	td->td_frame->tf_rdi = (register_t)arg;
677 }
678 
679 int
680 cpu_set_user_tls(struct thread *td, void *tls_base)
681 {
682 	struct pcb *pcb;
683 
684 	if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
685 		return (EINVAL);
686 
687 	pcb = td->td_pcb;
688 	set_pcb_flags(pcb, PCB_FULL_IRET);
689 #ifdef COMPAT_FREEBSD32
690 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
691 		pcb->pcb_gsbase = (register_t)tls_base;
692 		return (0);
693 	}
694 #endif
695 	pcb->pcb_fsbase = (register_t)tls_base;
696 	return (0);
697 }
698 
699 /*
700  * Software interrupt handler for queued VM system processing.
701  */
702 void
703 swi_vm(void *dummy)
704 {
705 	if (busdma_swi_pending != 0)
706 		busdma_swi();
707 }
708 
709 /*
710  * Tell whether this address is in some physical memory region.
711  * Currently used by the kernel coredump code in order to avoid
712  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
713  * or other unpredictable behaviour.
714  */
715 
716 int
717 is_physical_memory(vm_paddr_t addr)
718 {
719 
720 #ifdef DEV_ISA
721 	/* The ISA ``memory hole''. */
722 	if (addr >= 0xa0000 && addr < 0x100000)
723 		return 0;
724 #endif
725 
726 	/*
727 	 * stuff other tests for known memory-mapped devices (PCI?)
728 	 * here
729 	 */
730 
731 	return 1;
732 }
733