2 * SPDX-License-Identifier: BSD-4-Clause
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
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
41 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
42 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/mutex.h>
63 #include <sys/procctl.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>
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>
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>
86 _Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
87 "OFFSETOF_MONITORBUF does not correspond with offset of pc_monitorbuf.");
90 set_top_of_stack_td(struct thread *td)
92 td->td_md.md_stack_base = td->td_kstack +
93 td->td_kstack_pages * PAGE_SIZE;
97 get_pcb_user_save_td(struct thread *td)
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);
107 get_pcb_td(struct thread *td)
110 return (&td->td_md.md_pcb);
114 get_pcb_user_save_pcb(struct pcb *pcb)
118 td = __containerof(pcb, struct thread, td_md.md_pcb);
119 return (get_pcb_user_save_td(td));
123 alloc_fpusave(int flags)
126 struct savefpu_ymm *sf;
128 res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
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;
138 * Common code shared between cpu_fork() and cpu_copy_thread() for
139 * initializing a thread.
142 copy_thread(struct thread *td1, struct thread *td2)
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);
152 update_pcb_bases(td1->td_pcb);
156 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
158 /* Properly initialize pcb_save */
159 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
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);
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);
174 * Set registers for trampoline to user mode. Leave space for the
175 * return address on stack. These are the kernel mode register values.
177 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */
179 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
180 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */
181 pcb2->pcb_rip = (register_t)fork_trampoline;
183 * pcb2->pcb_dr*: cloned above.
184 * pcb2->pcb_savefpu: cloned above.
185 * pcb2->pcb_flags: cloned above.
186 * pcb2->pcb_onfault: cloned above (always NULL here?).
187 * pcb2->pcb_[fg]sbase: cloned above
190 pcb2->pcb_tssp = NULL;
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);
198 * Copy the trap frame for the return to user mode as if from a syscall.
199 * This copies most of the user mode register values. Some of these
200 * registers are rewritten by cpu_set_upcall() and linux_set_upcall().
202 if ((td1->td_proc->p_flag & P_KPROC) == 0) {
203 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
206 * If the current thread has the trap bit set (i.e. a debugger
207 * had single stepped the process to the system call), we need
208 * to clear the trap flag from the new frame. Otherwise, the new
209 * thread will receive a (likely unexpected) SIGTRAP when it
210 * executes the first instruction after returning to userland.
212 td2->td_frame->tf_rflags &= ~PSL_T;
217 * Finish a fork operation, with process p2 nearly set up.
218 * Copy and update the pcb, set up the stack so that the child
219 * ready to run and return to user mode.
222 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
226 struct mdproc *mdp1, *mdp2;
227 struct proc_ldt *pldt;
230 if ((flags & RFPROC) == 0) {
231 if ((flags & RFMEM) == 0) {
232 /* unshare user LDT */
235 if ((pldt = mdp1->md_ldt) != NULL &&
236 pldt->ldt_refcnt > 1 &&
237 user_ldt_alloc(p1, 1) == NULL)
238 panic("could not copy LDT");
239 mtx_unlock(&dt_lock);
244 /* Point the stack and pcb to the actual location */
245 set_top_of_stack_td(td2);
246 td2->td_pcb = pcb2 = get_pcb_td(td2);
248 copy_thread(td1, td2);
250 /* Reset debug registers in the new process */
251 x86_clear_dbregs(pcb2);
253 /* Point mdproc and then copy over p1's contents */
255 bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
257 /* Set child return values. */
258 p2->p_sysent->sv_set_fork_retval(td2);
260 /* As on i386, do not copy io permission bitmap. */
261 pcb2->pcb_tssp = NULL;
263 /* New segment registers. */
264 set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
266 /* Copy the LDT, if necessary. */
267 mdp1 = &td1->td_proc->p_md;
269 if (mdp1->md_ldt == NULL) {
274 if (mdp1->md_ldt != NULL) {
276 mdp1->md_ldt->ldt_refcnt++;
277 mdp2->md_ldt = mdp1->md_ldt;
278 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
279 system_segment_descriptor));
282 mdp2->md_ldt = user_ldt_alloc(p2, 0);
283 if (mdp2->md_ldt == NULL)
284 panic("could not copy LDT");
285 amd64_set_ldt_data(td2, 0, max_ldt_segment,
286 (struct user_segment_descriptor *)
287 mdp1->md_ldt->ldt_base);
291 mtx_unlock(&dt_lock);
294 * Now, cpu_switch() can schedule the new process.
295 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
296 * containing the return address when exiting cpu_switch.
297 * This will normally be to fork_trampoline(), which will have
298 * %rbx loaded with the new proc's pointer. fork_trampoline()
299 * will set up a stack to call fork_return(p, frame); to complete
300 * the return to user-mode.
305 x86_set_fork_retval(struct thread *td)
307 struct trapframe *frame = td->td_frame;
309 frame->tf_rax = 0; /* Child returns zero */
310 frame->tf_rflags &= ~PSL_C; /* success */
311 frame->tf_rdx = 1; /* System V emulation */
315 * Intercept the return address from a freshly forked process that has NOT
316 * been scheduled yet.
318 * This is needed to make kernel threads stay in kernel mode.
321 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
324 * Note that the trap frame follows the args, so the function
325 * is really called like this: func(arg, frame);
327 td->td_pcb->pcb_r12 = (long) func; /* function */
328 td->td_pcb->pcb_rbx = (long) arg; /* first arg */
332 cpu_exit(struct thread *td)
336 * If this process has a custom LDT, release it.
338 if (td->td_proc->p_md.md_ldt != NULL)
343 cpu_thread_exit(struct thread *td)
348 if (td == PCPU_GET(fpcurthread))
354 /* Disable any hardware breakpoints. */
355 if (pcb->pcb_flags & PCB_DBREGS) {
357 clear_pcb_flags(pcb, PCB_DBREGS);
362 cpu_thread_clean(struct thread *td)
371 if (pcb->pcb_tssp != NULL) {
372 pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
373 (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
374 kmem_free((vm_offset_t)pcb->pcb_tssp, ctob(IOPAGES + 1));
375 pcb->pcb_tssp = NULL;
380 cpu_thread_swapin(struct thread *td)
385 cpu_thread_swapout(struct thread *td)
390 cpu_thread_alloc(struct thread *td)
393 struct xstate_hdr *xhdr;
395 set_top_of_stack_td(td);
396 td->td_pcb = pcb = get_pcb_td(td);
397 td->td_frame = (struct trapframe *)td->td_md.md_stack_base - 1;
398 td->td_md.md_usr_fpu_save = fpu_save_area_alloc();
399 pcb->pcb_save = get_pcb_user_save_pcb(pcb);
401 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
402 bzero(xhdr, sizeof(*xhdr));
403 xhdr->xstate_bv = xsave_mask;
408 cpu_thread_free(struct thread *td)
410 cpu_thread_clean(td);
412 fpu_save_area_free(td->td_md.md_usr_fpu_save);
413 td->td_md.md_usr_fpu_save = NULL;
417 cpu_exec_vmspace_reuse(struct proc *p, vm_map_t map)
420 return (((curproc->p_md.md_flags & P_MD_KPTI) != 0) ==
421 (vm_map_pmap(map)->pm_ucr3 != PMAP_NO_CR3));
425 cpu_procctl_kpti_ctl(struct proc *p, int val)
428 if (pti && val == PROC_KPTI_CTL_ENABLE_ON_EXEC)
429 p->p_md.md_flags |= P_MD_KPTI;
430 if (val == PROC_KPTI_CTL_DISABLE_ON_EXEC)
431 p->p_md.md_flags &= ~P_MD_KPTI;
435 cpu_procctl_kpti_status(struct proc *p, int *val)
437 *val = (p->p_md.md_flags & P_MD_KPTI) != 0 ?
438 PROC_KPTI_CTL_ENABLE_ON_EXEC:
439 PROC_KPTI_CTL_DISABLE_ON_EXEC;
440 if (vmspace_pmap(p->p_vmspace)->pm_ucr3 != PMAP_NO_CR3)
441 *val |= PROC_KPTI_STATUS_ACTIVE;
445 cpu_procctl_la_ctl(struct proc *p, int val)
451 case PROC_LA_CTL_LA48_ON_EXEC:
452 p->p_md.md_flags |= P_MD_LA48;
453 p->p_md.md_flags &= ~P_MD_LA57;
455 case PROC_LA_CTL_LA57_ON_EXEC:
457 p->p_md.md_flags &= ~P_MD_LA48;
458 p->p_md.md_flags |= P_MD_LA57;
463 case PROC_LA_CTL_DEFAULT_ON_EXEC:
464 p->p_md.md_flags &= ~(P_MD_LA48 | P_MD_LA57);
471 cpu_procctl_la_status(struct proc *p, int *val)
475 if ((p->p_md.md_flags & P_MD_LA48) != 0)
476 res = PROC_LA_CTL_LA48_ON_EXEC;
477 else if ((p->p_md.md_flags & P_MD_LA57) != 0)
478 res = PROC_LA_CTL_LA57_ON_EXEC;
480 res = PROC_LA_CTL_DEFAULT_ON_EXEC;
481 if (p->p_sysent->sv_maxuser == VM_MAXUSER_ADDRESS_LA48)
482 res |= PROC_LA_STATUS_LA48;
484 res |= PROC_LA_STATUS_LA57;
489 cpu_procctl(struct thread *td, int idtype, id_t id, int com, void *data)
496 case PROC_KPTI_STATUS:
499 if (idtype != P_PID) {
503 if (com == PROC_KPTI_CTL) {
504 /* sad but true and not a joke */
505 error = priv_check(td, PRIV_IO);
509 if (com == PROC_KPTI_CTL || com == PROC_LA_CTL) {
510 error = copyin(data, &val, sizeof(val));
514 if (com == PROC_KPTI_CTL &&
515 val != PROC_KPTI_CTL_ENABLE_ON_EXEC &&
516 val != PROC_KPTI_CTL_DISABLE_ON_EXEC) {
520 if (com == PROC_LA_CTL &&
521 val != PROC_LA_CTL_LA48_ON_EXEC &&
522 val != PROC_LA_CTL_LA57_ON_EXEC &&
523 val != PROC_LA_CTL_DEFAULT_ON_EXEC) {
527 error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
532 cpu_procctl_kpti_ctl(p, val);
534 case PROC_KPTI_STATUS:
535 cpu_procctl_kpti_status(p, &val);
538 error = cpu_procctl_la_ctl(p, val);
541 cpu_procctl_la_status(p, &val);
545 if (com == PROC_KPTI_STATUS || com == PROC_LA_STATUS)
546 error = copyout(&val, data, sizeof(val));
556 cpu_set_syscall_retval(struct thread *td, int error)
558 struct trapframe *frame;
560 frame = td->td_frame;
561 if (__predict_true(error == 0)) {
562 frame->tf_rax = td->td_retval[0];
563 frame->tf_rdx = td->td_retval[1];
564 frame->tf_rflags &= ~PSL_C;
571 * Reconstruct pc, we know that 'syscall' is 2 bytes,
572 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
573 * We saved this in tf_err.
574 * %r10 (which was holding the value of %rcx) is restored
575 * for the next iteration.
576 * %r10 restore is only required for freebsd/amd64 processes,
577 * but shall be innocent for any ia32 ABI.
579 * Require full context restore to get the arguments
580 * in the registers reloaded at return to usermode.
582 frame->tf_rip -= frame->tf_err;
583 frame->tf_r10 = frame->tf_rcx;
584 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
591 frame->tf_rax = error;
592 frame->tf_rflags |= PSL_C;
598 * Initialize machine state, mostly pcb and trap frame for a new
599 * thread, about to return to userspace. Put enough state in the new
600 * thread's PCB to get it to go back to the fork_return(), which
601 * finalizes the thread state and handles peculiarities of the first
602 * return to userspace for the new thread.
605 cpu_copy_thread(struct thread *td, struct thread *td0)
607 copy_thread(td0, td);
609 set_pcb_flags_raw(td->td_pcb, PCB_FULL_IRET);
613 * Set that machine state for performing an upcall that starts
614 * the entry function with the given argument.
617 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
622 * Do any extra cleaning that needs to be done.
623 * The thread may have optional components
624 * that are not present in a fresh thread.
625 * This may be a recycled thread so make it look
626 * as though it's newly allocated.
628 cpu_thread_clean(td);
630 #ifdef COMPAT_FREEBSD32
631 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
633 * Set the trap frame to point at the beginning of the entry
636 td->td_frame->tf_rbp = 0;
637 td->td_frame->tf_rsp =
638 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
639 td->td_frame->tf_rip = (uintptr_t)entry;
641 /* Return address sentinel value to stop stack unwinding. */
642 suword32((void *)td->td_frame->tf_rsp, 0);
644 /* Pass the argument to the entry point. */
645 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
646 (uint32_t)(uintptr_t)arg);
653 * Set the trap frame to point at the beginning of the uts
656 td->td_frame->tf_rbp = 0;
657 td->td_frame->tf_rsp =
658 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
659 td->td_frame->tf_rsp -= 8;
660 td->td_frame->tf_rip = (register_t)entry;
661 td->td_frame->tf_ds = _udatasel;
662 td->td_frame->tf_es = _udatasel;
663 td->td_frame->tf_fs = _ufssel;
664 td->td_frame->tf_gs = _ugssel;
665 td->td_frame->tf_flags = TF_HASSEGS;
667 /* Return address sentinel value to stop stack unwinding. */
668 suword((void *)td->td_frame->tf_rsp, 0);
670 /* Pass the argument to the entry point. */
671 td->td_frame->tf_rdi = (register_t)arg;
675 cpu_set_user_tls(struct thread *td, void *tls_base)
679 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
683 set_pcb_flags(pcb, PCB_FULL_IRET);
684 #ifdef COMPAT_FREEBSD32
685 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
686 pcb->pcb_gsbase = (register_t)tls_base;
690 pcb->pcb_fsbase = (register_t)tls_base;
695 * Software interrupt handler for queued VM system processing.
700 if (busdma_swi_pending != 0)
705 * Tell whether this address is in some physical memory region.
706 * Currently used by the kernel coredump code in order to avoid
707 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
708 * or other unpredictable behaviour.
712 is_physical_memory(vm_paddr_t addr)
716 /* The ISA ``memory hole''. */
717 if (addr >= 0xa0000 && addr < 0x100000)
722 * stuff other tests for known memory-mapped devices (PCI?)