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/pioctl.h> 62 #include <sys/proc.h> 63 #include <sys/smp.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> 69 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> 76 77 #include <vm/vm.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> 83 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."); 90 91 struct savefpu * 92 get_pcb_user_save_td(struct thread *td) 93 { 94 vm_offset_t p; 95 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); 100 } 101 102 struct savefpu * 103 get_pcb_user_save_pcb(struct pcb *pcb) 104 { 105 vm_offset_t p; 106 107 p = (vm_offset_t)(pcb + 1); 108 return ((struct savefpu *)p); 109 } 110 111 struct pcb * 112 get_pcb_td(struct thread *td) 113 { 114 vm_offset_t p; 115 116 p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 117 roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) - 118 sizeof(struct pcb); 119 return ((struct pcb *)p); 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 * 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. 141 */ 142 void 143 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 144 { 145 struct proc *p1; 146 struct pcb *pcb2; 147 struct mdproc *mdp1, *mdp2; 148 struct proc_ldt *pldt; 149 150 p1 = td1->td_proc; 151 if ((flags & RFPROC) == 0) { 152 if ((flags & RFMEM) == 0) { 153 /* unshare user LDT */ 154 mdp1 = &p1->p_md; 155 mtx_lock(&dt_lock); 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); 161 } 162 return; 163 } 164 165 /* Ensure that td1's pcb is up to date. */ 166 fpuexit(td1); 167 update_pcb_bases(td1->td_pcb); 168 169 /* Point the pcb to the top of the stack */ 170 pcb2 = get_pcb_td(td2); 171 td2->td_pcb = pcb2; 172 173 /* Copy td1's pcb */ 174 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 175 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); 180 181 /* Point mdproc and then copy over td1's contents */ 182 mdp2 = &p2->p_md; 183 bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 184 185 /* 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. 189 */ 190 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 191 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 192 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; 196 197 /* 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. 204 */ 205 if ((p1->p_pfsflags & PF_FORK) == 0) 206 td2->td_frame->tf_rflags &= ~PSL_T; 207 208 /* 209 * Set registers for trampoline to user mode. Leave space for the 210 * return address on stack. These are the kernel mode register values. 211 */ 212 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 213 pcb2->pcb_rbp = 0; 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; 217 /*- 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 223 */ 224 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; 229 230 /* As an i386, do not copy io permission bitmap. */ 231 pcb2->pcb_tssp = NULL; 232 233 /* New segment registers. */ 234 set_pcb_flags_raw(pcb2, PCB_FULL_IRET); 235 236 /* Copy the LDT, if necessary. */ 237 mdp1 = &td1->td_proc->p_md; 238 mdp2 = &p2->p_md; 239 if (mdp1->md_ldt == NULL) { 240 mdp2->md_ldt = NULL; 241 return; 242 } 243 mtx_lock(&dt_lock); 244 if (mdp1->md_ldt != NULL) { 245 if (flags & RFMEM) { 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)); 250 } else { 251 mdp2->md_ldt = NULL; 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); 258 } 259 } else 260 mdp2->md_ldt = NULL; 261 mtx_unlock(&dt_lock); 262 263 /* 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. 271 */ 272 } 273 274 /* 275 * Intercept the return address from a freshly forked process that has NOT 276 * been scheduled yet. 277 * 278 * This is needed to make kernel threads stay in kernel mode. 279 */ 280 void 281 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg) 282 { 283 /* 284 * Note that the trap frame follows the args, so the function 285 * is really called like this: func(arg, frame); 286 */ 287 td->td_pcb->pcb_r12 = (long) func; /* function */ 288 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 289 } 290 291 void 292 cpu_exit(struct thread *td) 293 { 294 295 /* 296 * If this process has a custom LDT, release it. 297 */ 298 if (td->td_proc->p_md.md_ldt != NULL) 299 user_ldt_free(td); 300 } 301 302 void 303 cpu_thread_exit(struct thread *td) 304 { 305 struct pcb *pcb; 306 307 critical_enter(); 308 if (td == PCPU_GET(fpcurthread)) 309 fpudrop(); 310 critical_exit(); 311 312 pcb = td->td_pcb; 313 314 /* Disable any hardware breakpoints. */ 315 if (pcb->pcb_flags & PCB_DBREGS) { 316 reset_dbregs(); 317 clear_pcb_flags(pcb, PCB_DBREGS); 318 } 319 } 320 321 void 322 cpu_thread_clean(struct thread *td) 323 { 324 struct pcb *pcb; 325 326 pcb = td->td_pcb; 327 328 /* 329 * Clean TSS/iomap 330 */ 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(kernel_arena, (vm_offset_t)pcb->pcb_tssp, 335 ctob(IOPAGES + 1)); 336 pcb->pcb_tssp = NULL; 337 } 338 } 339 340 void 341 cpu_thread_swapin(struct thread *td) 342 { 343 } 344 345 void 346 cpu_thread_swapout(struct thread *td) 347 { 348 } 349 350 void 351 cpu_thread_alloc(struct thread *td) 352 { 353 struct pcb *pcb; 354 struct xstate_hdr *xhdr; 355 356 td->td_pcb = pcb = get_pcb_td(td); 357 td->td_frame = (struct trapframe *)pcb - 1; 358 pcb->pcb_save = get_pcb_user_save_pcb(pcb); 359 if (use_xsave) { 360 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1); 361 bzero(xhdr, sizeof(*xhdr)); 362 xhdr->xstate_bv = xsave_mask; 363 } 364 } 365 366 void 367 cpu_thread_free(struct thread *td) 368 { 369 370 cpu_thread_clean(td); 371 } 372 373 void 374 cpu_set_syscall_retval(struct thread *td, int error) 375 { 376 377 switch (error) { 378 case 0: 379 td->td_frame->tf_rax = td->td_retval[0]; 380 td->td_frame->tf_rdx = td->td_retval[1]; 381 td->td_frame->tf_rflags &= ~PSL_C; 382 break; 383 384 case ERESTART: 385 /* 386 * Reconstruct pc, we know that 'syscall' is 2 bytes, 387 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes. 388 * We saved this in tf_err. 389 * %r10 (which was holding the value of %rcx) is restored 390 * for the next iteration. 391 * %r10 restore is only required for freebsd/amd64 processes, 392 * but shall be innocent for any ia32 ABI. 393 * 394 * Require full context restore to get the arguments 395 * in the registers reloaded at return to usermode. 396 */ 397 td->td_frame->tf_rip -= td->td_frame->tf_err; 398 td->td_frame->tf_r10 = td->td_frame->tf_rcx; 399 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 400 break; 401 402 case EJUSTRETURN: 403 break; 404 405 default: 406 td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error); 407 td->td_frame->tf_rflags |= PSL_C; 408 break; 409 } 410 } 411 412 /* 413 * Initialize machine state, mostly pcb and trap frame for a new 414 * thread, about to return to userspace. Put enough state in the new 415 * thread's PCB to get it to go back to the fork_return(), which 416 * finalizes the thread state and handles peculiarities of the first 417 * return to userspace for the new thread. 418 */ 419 void 420 cpu_copy_thread(struct thread *td, struct thread *td0) 421 { 422 struct pcb *pcb2; 423 424 /* Point the pcb to the top of the stack. */ 425 pcb2 = td->td_pcb; 426 427 /* 428 * Copy the upcall pcb. This loads kernel regs. 429 * Those not loaded individually below get their default 430 * values here. 431 */ 432 update_pcb_bases(td0->td_pcb); 433 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 434 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE | 435 PCB_KERNFPU); 436 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2); 437 bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save, 438 cpu_max_ext_state_size); 439 set_pcb_flags_raw(pcb2, PCB_FULL_IRET); 440 441 /* 442 * Create a new fresh stack for the new thread. 443 */ 444 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 445 446 /* If the current thread has the trap bit set (i.e. a debugger had 447 * single stepped the process to the system call), we need to clear 448 * the trap flag from the new frame. Otherwise, the new thread will 449 * receive a (likely unexpected) SIGTRAP when it executes the first 450 * instruction after returning to userland. 451 */ 452 td->td_frame->tf_rflags &= ~PSL_T; 453 454 /* 455 * Set registers for trampoline to user mode. Leave space for the 456 * return address on stack. These are the kernel mode register values. 457 */ 458 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 459 pcb2->pcb_rbp = 0; 460 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 461 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 462 pcb2->pcb_rip = (register_t)fork_trampoline; 463 /* 464 * If we didn't copy the pcb, we'd need to do the following registers: 465 * pcb2->pcb_dr*: cloned above. 466 * pcb2->pcb_savefpu: cloned above. 467 * pcb2->pcb_onfault: cloned above (always NULL here?). 468 * pcb2->pcb_[fg]sbase: cloned above 469 */ 470 471 /* Setup to release spin count in fork_exit(). */ 472 td->td_md.md_spinlock_count = 1; 473 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I; 474 } 475 476 /* 477 * Set that machine state for performing an upcall that starts 478 * the entry function with the given argument. 479 */ 480 void 481 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg, 482 stack_t *stack) 483 { 484 485 /* 486 * Do any extra cleaning that needs to be done. 487 * The thread may have optional components 488 * that are not present in a fresh thread. 489 * This may be a recycled thread so make it look 490 * as though it's newly allocated. 491 */ 492 cpu_thread_clean(td); 493 494 #ifdef COMPAT_FREEBSD32 495 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 496 /* 497 * Set the trap frame to point at the beginning of the entry 498 * function. 499 */ 500 td->td_frame->tf_rbp = 0; 501 td->td_frame->tf_rsp = 502 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4; 503 td->td_frame->tf_rip = (uintptr_t)entry; 504 505 /* Return address sentinel value to stop stack unwinding. */ 506 suword32((void *)td->td_frame->tf_rsp, 0); 507 508 /* Pass the argument to the entry point. */ 509 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)), 510 (uint32_t)(uintptr_t)arg); 511 512 return; 513 } 514 #endif 515 516 /* 517 * Set the trap frame to point at the beginning of the uts 518 * function. 519 */ 520 td->td_frame->tf_rbp = 0; 521 td->td_frame->tf_rsp = 522 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f; 523 td->td_frame->tf_rsp -= 8; 524 td->td_frame->tf_rip = (register_t)entry; 525 td->td_frame->tf_ds = _udatasel; 526 td->td_frame->tf_es = _udatasel; 527 td->td_frame->tf_fs = _ufssel; 528 td->td_frame->tf_gs = _ugssel; 529 td->td_frame->tf_flags = TF_HASSEGS; 530 531 /* Return address sentinel value to stop stack unwinding. */ 532 suword((void *)td->td_frame->tf_rsp, 0); 533 534 /* Pass the argument to the entry point. */ 535 td->td_frame->tf_rdi = (register_t)arg; 536 } 537 538 int 539 cpu_set_user_tls(struct thread *td, void *tls_base) 540 { 541 struct pcb *pcb; 542 543 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS) 544 return (EINVAL); 545 546 pcb = td->td_pcb; 547 set_pcb_flags(pcb, PCB_FULL_IRET); 548 #ifdef COMPAT_FREEBSD32 549 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 550 pcb->pcb_gsbase = (register_t)tls_base; 551 return (0); 552 } 553 #endif 554 pcb->pcb_fsbase = (register_t)tls_base; 555 return (0); 556 } 557 558 /* 559 * Software interrupt handler for queued VM system processing. 560 */ 561 void 562 swi_vm(void *dummy) 563 { 564 if (busdma_swi_pending != 0) 565 busdma_swi(); 566 } 567 568 /* 569 * Tell whether this address is in some physical memory region. 570 * Currently used by the kernel coredump code in order to avoid 571 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 572 * or other unpredictable behaviour. 573 */ 574 575 int 576 is_physical_memory(vm_paddr_t addr) 577 { 578 579 #ifdef DEV_ISA 580 /* The ISA ``memory hole''. */ 581 if (addr >= 0xa0000 && addr < 0x100000) 582 return 0; 583 #endif 584 585 /* 586 * stuff other tests for known memory-mapped devices (PCI?) 587 * here 588 */ 589 590 return 1; 591 } 592