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>
61 #include <sys/pioctl.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
68 #include <sys/vmmeter.h>
70 #include <machine/cpu.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/smp.h>
74 #include <machine/specialreg.h>
75 #include <machine/tss.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_param.h>
84 _Static_assert(OFFSETOF_CURTHREAD == offsetof(struct pcpu, pc_curthread),
85 "OFFSETOF_CURTHREAD does not correspond with offset of pc_curthread.");
86 _Static_assert(OFFSETOF_CURPCB == offsetof(struct pcpu, pc_curpcb),
87 "OFFSETOF_CURPCB does not correspond with offset of pc_curpcb.");
88 _Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
89 "OFFSETOF_MONINORBUF does not correspond with offset of pc_monitorbuf.");
92 get_pcb_user_save_td(struct thread *td)
96 p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
97 roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
98 KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
99 return ((struct savefpu *)p);
103 get_pcb_user_save_pcb(struct pcb *pcb)
107 p = (vm_offset_t)(pcb + 1);
108 return ((struct savefpu *)p);
112 get_pcb_td(struct thread *td)
116 p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
117 roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
119 return ((struct pcb *)p);
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 * Finish a fork operation, with process p2 nearly set up.
139 * Copy and update the pcb, set up the stack so that the child
140 * ready to run and return to user mode.
143 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
147 struct mdproc *mdp1, *mdp2;
148 struct proc_ldt *pldt;
151 if ((flags & RFPROC) == 0) {
152 if ((flags & RFMEM) == 0) {
153 /* unshare user LDT */
156 if ((pldt = mdp1->md_ldt) != NULL &&
157 pldt->ldt_refcnt > 1 &&
158 user_ldt_alloc(p1, 1) == NULL)
159 panic("could not copy LDT");
160 mtx_unlock(&dt_lock);
165 /* Ensure that td1's pcb is up to date. */
167 update_pcb_bases(td1->td_pcb);
169 /* Point the pcb to the top of the stack */
170 pcb2 = get_pcb_td(td2);
174 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
176 /* Properly initialize pcb_save */
177 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
178 bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
179 cpu_max_ext_state_size);
181 /* Point mdproc and then copy over td1's contents */
183 bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
186 * Create a new fresh stack for the new process.
187 * Copy the trap frame for the return to user mode as if from a
188 * syscall. This copies most of the user mode register values.
190 td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
191 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
193 td2->td_frame->tf_rax = 0; /* Child returns zero */
194 td2->td_frame->tf_rflags &= ~PSL_C; /* success */
195 td2->td_frame->tf_rdx = 1;
198 * If the parent process has the trap bit set (i.e. a debugger had
199 * single stepped the process to the system call), we need to clear
200 * the trap flag from the new frame unless the debugger had set PF_FORK
201 * on the parent. Otherwise, the child will receive a (likely
202 * unexpected) SIGTRAP when it executes the first instruction after
203 * returning to userland.
205 if ((p1->p_pfsflags & PF_FORK) == 0)
206 td2->td_frame->tf_rflags &= ~PSL_T;
209 * Set registers for trampoline to user mode. Leave space for the
210 * return address on stack. These are the kernel mode register values.
212 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */
214 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
215 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */
216 pcb2->pcb_rip = (register_t)fork_trampoline;
218 * pcb2->pcb_dr*: cloned above.
219 * pcb2->pcb_savefpu: cloned above.
220 * pcb2->pcb_flags: cloned above.
221 * pcb2->pcb_onfault: cloned above (always NULL here?).
222 * pcb2->pcb_[fg]sbase: cloned above
225 /* Setup to release spin count in fork_exit(). */
226 td2->td_md.md_spinlock_count = 1;
227 td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
228 td2->td_md.md_invl_gen.gen = 0;
230 /* As an i386, do not copy io permission bitmap. */
231 pcb2->pcb_tssp = NULL;
233 /* New segment registers. */
234 set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
236 /* Copy the LDT, if necessary. */
237 mdp1 = &td1->td_proc->p_md;
239 if (mdp1->md_ldt == NULL) {
244 if (mdp1->md_ldt != NULL) {
246 mdp1->md_ldt->ldt_refcnt++;
247 mdp2->md_ldt = mdp1->md_ldt;
248 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
249 system_segment_descriptor));
252 mdp2->md_ldt = user_ldt_alloc(p2, 0);
253 if (mdp2->md_ldt == NULL)
254 panic("could not copy LDT");
255 amd64_set_ldt_data(td2, 0, max_ldt_segment,
256 (struct user_segment_descriptor *)
257 mdp1->md_ldt->ldt_base);
261 mtx_unlock(&dt_lock);
264 * Now, cpu_switch() can schedule the new process.
265 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
266 * containing the return address when exiting cpu_switch.
267 * This will normally be to fork_trampoline(), which will have
268 * %ebx loaded with the new proc's pointer. fork_trampoline()
269 * will set up a stack to call fork_return(p, frame); to complete
270 * the return to user-mode.
275 * Intercept the return address from a freshly forked process that has NOT
276 * been scheduled yet.
278 * This is needed to make kernel threads stay in kernel mode.
281 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
284 * Note that the trap frame follows the args, so the function
285 * is really called like this: func(arg, frame);
287 td->td_pcb->pcb_r12 = (long) func; /* function */
288 td->td_pcb->pcb_rbx = (long) arg; /* first arg */
292 cpu_exit(struct thread *td)
296 * If this process has a custom LDT, release it.
298 if (td->td_proc->p_md.md_ldt != NULL)
303 cpu_thread_exit(struct thread *td)
308 if (td == PCPU_GET(fpcurthread))
314 /* Disable any hardware breakpoints. */
315 if (pcb->pcb_flags & PCB_DBREGS) {
317 clear_pcb_flags(pcb, PCB_DBREGS);
322 cpu_thread_clean(struct thread *td)
331 if (pcb->pcb_tssp != NULL) {
332 pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
333 (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
334 kmem_free((vm_offset_t)pcb->pcb_tssp, ctob(IOPAGES + 1));
335 pcb->pcb_tssp = NULL;
340 cpu_thread_swapin(struct thread *td)
345 cpu_thread_swapout(struct thread *td)
350 cpu_thread_alloc(struct thread *td)
353 struct xstate_hdr *xhdr;
355 td->td_pcb = pcb = get_pcb_td(td);
356 td->td_frame = (struct trapframe *)pcb - 1;
357 pcb->pcb_save = get_pcb_user_save_pcb(pcb);
359 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
360 bzero(xhdr, sizeof(*xhdr));
361 xhdr->xstate_bv = xsave_mask;
366 cpu_thread_free(struct thread *td)
369 cpu_thread_clean(td);
373 cpu_set_syscall_retval(struct thread *td, int error)
378 td->td_frame->tf_rax = td->td_retval[0];
379 td->td_frame->tf_rdx = td->td_retval[1];
380 td->td_frame->tf_rflags &= ~PSL_C;
385 * Reconstruct pc, we know that 'syscall' is 2 bytes,
386 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
387 * We saved this in tf_err.
388 * %r10 (which was holding the value of %rcx) is restored
389 * for the next iteration.
390 * %r10 restore is only required for freebsd/amd64 processes,
391 * but shall be innocent for any ia32 ABI.
393 * Require full context restore to get the arguments
394 * in the registers reloaded at return to usermode.
396 td->td_frame->tf_rip -= td->td_frame->tf_err;
397 td->td_frame->tf_r10 = td->td_frame->tf_rcx;
398 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
405 td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error);
406 td->td_frame->tf_rflags |= PSL_C;
412 * Initialize machine state, mostly pcb and trap frame for a new
413 * thread, about to return to userspace. Put enough state in the new
414 * thread's PCB to get it to go back to the fork_return(), which
415 * finalizes the thread state and handles peculiarities of the first
416 * return to userspace for the new thread.
419 cpu_copy_thread(struct thread *td, struct thread *td0)
423 /* Point the pcb to the top of the stack. */
427 * Copy the upcall pcb. This loads kernel regs.
428 * Those not loaded individually below get their default
431 update_pcb_bases(td0->td_pcb);
432 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
433 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
435 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
436 bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
437 cpu_max_ext_state_size);
438 set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
441 * Create a new fresh stack for the new thread.
443 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
445 /* If the current thread has the trap bit set (i.e. a debugger had
446 * single stepped the process to the system call), we need to clear
447 * the trap flag from the new frame. Otherwise, the new thread will
448 * receive a (likely unexpected) SIGTRAP when it executes the first
449 * instruction after returning to userland.
451 td->td_frame->tf_rflags &= ~PSL_T;
454 * Set registers for trampoline to user mode. Leave space for the
455 * return address on stack. These are the kernel mode register values.
457 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */
459 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */
460 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */
461 pcb2->pcb_rip = (register_t)fork_trampoline;
463 * If we didn't copy the pcb, we'd need to do the following registers:
464 * pcb2->pcb_dr*: cloned above.
465 * pcb2->pcb_savefpu: cloned above.
466 * pcb2->pcb_onfault: cloned above (always NULL here?).
467 * pcb2->pcb_[fg]sbase: cloned above
470 /* Setup to release spin count in fork_exit(). */
471 td->td_md.md_spinlock_count = 1;
472 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
476 * Set that machine state for performing an upcall that starts
477 * the entry function with the given argument.
480 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
485 * Do any extra cleaning that needs to be done.
486 * The thread may have optional components
487 * that are not present in a fresh thread.
488 * This may be a recycled thread so make it look
489 * as though it's newly allocated.
491 cpu_thread_clean(td);
493 #ifdef COMPAT_FREEBSD32
494 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
496 * Set the trap frame to point at the beginning of the entry
499 td->td_frame->tf_rbp = 0;
500 td->td_frame->tf_rsp =
501 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
502 td->td_frame->tf_rip = (uintptr_t)entry;
504 /* Return address sentinel value to stop stack unwinding. */
505 suword32((void *)td->td_frame->tf_rsp, 0);
507 /* Pass the argument to the entry point. */
508 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
509 (uint32_t)(uintptr_t)arg);
516 * Set the trap frame to point at the beginning of the uts
519 td->td_frame->tf_rbp = 0;
520 td->td_frame->tf_rsp =
521 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
522 td->td_frame->tf_rsp -= 8;
523 td->td_frame->tf_rip = (register_t)entry;
524 td->td_frame->tf_ds = _udatasel;
525 td->td_frame->tf_es = _udatasel;
526 td->td_frame->tf_fs = _ufssel;
527 td->td_frame->tf_gs = _ugssel;
528 td->td_frame->tf_flags = TF_HASSEGS;
530 /* Return address sentinel value to stop stack unwinding. */
531 suword((void *)td->td_frame->tf_rsp, 0);
533 /* Pass the argument to the entry point. */
534 td->td_frame->tf_rdi = (register_t)arg;
538 cpu_set_user_tls(struct thread *td, void *tls_base)
542 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
546 set_pcb_flags(pcb, PCB_FULL_IRET);
547 #ifdef COMPAT_FREEBSD32
548 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
549 pcb->pcb_gsbase = (register_t)tls_base;
553 pcb->pcb_fsbase = (register_t)tls_base;
558 * Software interrupt handler for queued VM system processing.
563 if (busdma_swi_pending != 0)
568 * Tell whether this address is in some physical memory region.
569 * Currently used by the kernel coredump code in order to avoid
570 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
571 * or other unpredictable behaviour.
575 is_physical_memory(vm_paddr_t addr)
579 /* The ISA ``memory hole''. */
580 if (addr >= 0xa0000 && addr < 0x100000)
585 * stuff other tests for known memory-mapped devices (PCI?)