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