vm_machdep.c revision 302408
1139776Simp/*- 222521Sdyson * Copyright (c) 1982, 1986 The Regents of the University of California. 31541Srgrimes * Copyright (c) 1989, 1990 William Jolitz 41541Srgrimes * Copyright (c) 1994 John Dyson 51541Srgrimes * All rights reserved. 61541Srgrimes * 71541Srgrimes * This code is derived from software contributed to Berkeley by 81541Srgrimes * the Systems Programming Group of the University of Utah Computer 91541Srgrimes * Science Department, and William Jolitz. 101541Srgrimes * 111541Srgrimes * Redistribution and use in source and binary forms, with or without 121541Srgrimes * modification, are permitted provided that the following conditions 131541Srgrimes * are met: 141541Srgrimes * 1. Redistributions of source code must retain the above copyright 151541Srgrimes * notice, this list of conditions and the following disclaimer. 161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171541Srgrimes * notice, this list of conditions and the following disclaimer in the 181541Srgrimes * documentation and/or other materials provided with the distribution. 191541Srgrimes * 3. All advertising materials mentioning features or use of this software 201541Srgrimes * must display the following acknowledgement: 211541Srgrimes * This product includes software developed by the University of 221541Srgrimes * California, Berkeley and its contributors. 231541Srgrimes * 4. Neither the name of the University nor the names of its contributors 241541Srgrimes * may be used to endorse or promote products derived from this software 251541Srgrimes * without specific prior written permission. 261541Srgrimes * 271541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 281541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 291541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 301541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 311541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 321541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 331541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3450477Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 351541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 361541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 371541Srgrimes * SUCH DAMAGE. 381541Srgrimes * 391541Srgrimes * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 401541Srgrimes * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 411541Srgrimes */ 421541Srgrimes 4376166Smarkm#include <sys/cdefs.h> 442946Swollman__FBSDID("$FreeBSD: stable/11/sys/amd64/amd64/vm_machdep.c 301961 2016-06-16 12:05:44Z kib $"); 4576166Smarkm 4689316Salfred#include "opt_isa.h" 4776166Smarkm#include "opt_cpu.h" 4876166Smarkm#include "opt_compat.h" 491541Srgrimes 50220400Strasz#include <sys/param.h> 511541Srgrimes#include <sys/systm.h> 521541Srgrimes#include <sys/bio.h> 5376166Smarkm#include <sys/buf.h> 5477031Sru#include <sys/kernel.h> 551541Srgrimes#include <sys/ktr.h> 56151897Srwatson#include <sys/lock.h> 5730354Sphk#include <sys/malloc.h> 58158611Skbyanc#include <sys/mbuf.h> 59132902Sphk#include <sys/mutex.h> 60116271Sphk#include <sys/pioctl.h> 61116271Sphk#include <sys/proc.h> 62141622Sphk#include <sys/smp.h> 63116271Sphk#include <sys/sysctl.h> 641541Srgrimes#include <sys/sysent.h> 65158611Skbyanc#include <sys/unistd.h> 66158611Skbyanc#include <sys/vnode.h> 67158611Skbyanc#include <sys/vmmeter.h> 68230725Smckusick 69158611Skbyanc#include <machine/cpu.h> 70158611Skbyanc#include <machine/md_var.h> 71158611Skbyanc#include <machine/pcb.h> 72158611Skbyanc#include <machine/smp.h> 73158611Skbyanc#include <machine/specialreg.h> 741541Srgrimes#include <machine/tss.h> 751541Srgrimes 7612143Sphk#include <vm/vm.h> 77191990Sattilio#include <vm/vm_extern.h> 781541Srgrimes#include <vm/vm_kern.h> 791541Srgrimes#include <vm/vm_page.h> 801541Srgrimes#include <vm/vm_map.h> 811541Srgrimes#include <vm/vm_param.h> 821541Srgrimes 831541Srgrimes#include <isa/isareg.h> 841541Srgrimes 851541Srgrimesstatic void cpu_reset_real(void); 86137478Sphk#ifdef SMP 871541Srgrimesstatic void cpu_reset_proxy(void); 881541Srgrimesstatic u_int cpu_reset_proxyid; 89184205Sdesstatic volatile u_int cpu_reset_proxy_active; 90111119Simp#endif 91179288Slulf 92179288Slulf_Static_assert(OFFSETOF_CURTHREAD == offsetof(struct pcpu, pc_curthread), 93179288Slulf "OFFSETOF_CURTHREAD does not correspond with offset of pc_curthread."); 94179288Slulf_Static_assert(OFFSETOF_CURPCB == offsetof(struct pcpu, pc_curpcb), 95179288Slulf "OFFSETOF_CURPCB does not correspond with offset of pc_curpcb."); 96179288Slulf_Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf), 97179288Slulf "OFFSETOF_MONINORBUF does not correspond with offset of pc_monitorbuf."); 98191990Sattilio 99179288Slulfstruct savefpu * 100179288Slulfget_pcb_user_save_td(struct thread *td) 101179288Slulf{ 102179288Slulf vm_offset_t p; 103179288Slulf 1041541Srgrimes p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 105101308Sjeff roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN); 1061541Srgrimes KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area")); 107179288Slulf return ((struct savefpu *)p); 1081541Srgrimes} 1091541Srgrimes 110179288Slulfstruct savefpu * 111179288Slulfget_pcb_user_save_pcb(struct pcb *pcb) 112179288Slulf{ 11322521Sdyson vm_offset_t p; 1141541Srgrimes 115138483Sphk p = (vm_offset_t)(pcb + 1); 1161541Srgrimes return ((struct savefpu *)p); 1171541Srgrimes} 1181541Srgrimes 11912143Sphkstruct pcb * 120191990Sattilioget_pcb_td(struct thread *td) 1211541Srgrimes{ 1221541Srgrimes vm_offset_t p; 1231541Srgrimes 124179288Slulf p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 125179288Slulf roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) - 1261541Srgrimes sizeof(struct pcb); 1271541Srgrimes return ((struct pcb *)p); 1281541Srgrimes} 129179288Slulf 130179288Slulfvoid * 131179288Slulfalloc_fpusave(int flags) 132179288Slulf{ 133179288Slulf void *res; 134179288Slulf struct savefpu_ymm *sf; 1351541Srgrimes 136179288Slulf res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags); 1371541Srgrimes if (use_xsave) { 1381541Srgrimes sf = (struct savefpu_ymm *)res; 1391541Srgrimes bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd)); 1401541Srgrimes sf->sv_xstate.sx_hd.xstate_bv = xsave_mask; 1411541Srgrimes } 14276688Siedowse return (res); 14376688Siedowse} 14476688Siedowse 1451541Srgrimes/* 146191990Sattilio * Finish a fork operation, with process p2 nearly set up. 1471541Srgrimes * Copy and update the pcb, set up the stack so that the child 1481541Srgrimes * ready to run and return to user mode. 1491541Srgrimes */ 150179288Slulfvoid 151179288Slulfcpu_fork(td1, p2, td2, flags) 1521541Srgrimes register struct thread *td1; 153179288Slulf register struct proc *p2; 154179288Slulf struct thread *td2; 1551541Srgrimes int flags; 156179288Slulf{ 157179288Slulf register struct proc *p1; 1581541Srgrimes struct pcb *pcb2; 1591541Srgrimes struct mdproc *mdp1, *mdp2; 1601541Srgrimes struct proc_ldt *pldt; 1611541Srgrimes 162141622Sphk p1 = td1->td_proc; 163191990Sattilio if ((flags & RFPROC) == 0) { 1641541Srgrimes if ((flags & RFMEM) == 0) { 165144058Sjeff /* unshare user LDT */ 1661541Srgrimes mdp1 = &p1->p_md; 1671541Srgrimes mtx_lock(&dt_lock); 1681541Srgrimes if ((pldt = mdp1->md_ldt) != NULL && 1691541Srgrimes pldt->ldt_refcnt > 1 && 1701541Srgrimes user_ldt_alloc(p1, 1) == NULL) 1711541Srgrimes panic("could not copy LDT"); 1721541Srgrimes mtx_unlock(&dt_lock); 1731541Srgrimes } 174191990Sattilio return; 1751541Srgrimes } 1761541Srgrimes 1771541Srgrimes /* Ensure that td1's pcb is up to date. */ 1781541Srgrimes fpuexit(td1); 17912143Sphk 180191990Sattilio /* Point the pcb to the top of the stack */ 1811541Srgrimes pcb2 = get_pcb_td(td2); 1821541Srgrimes td2->td_pcb = pcb2; 183191990Sattilio 18483366Sjulian /* Copy td1's pcb */ 1851541Srgrimes bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 1861541Srgrimes 1871541Srgrimes /* Properly initialize pcb_save */ 1881541Srgrimes pcb2->pcb_save = get_pcb_user_save_pcb(pcb2); 1891541Srgrimes bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2), 190220400Strasz cpu_max_ext_state_size); 1911541Srgrimes 192191990Sattilio /* Point mdproc and then copy over td1's contents */ 193191990Sattilio mdp2 = &p2->p_md; 1941541Srgrimes bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 1951541Srgrimes 1961541Srgrimes /* 1971541Srgrimes * Create a new fresh stack for the new process. 1981541Srgrimes * Copy the trap frame for the return to user mode as if from a 1991541Srgrimes * syscall. This copies most of the user mode register values. 200125454Sjhb */ 201125454Sjhb td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 202125454Sjhb bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 20383366Sjulian 204168355Srwatson td2->td_frame->tf_rax = 0; /* Child returns zero */ 205220400Strasz td2->td_frame->tf_rflags &= ~PSL_C; /* success */ 206220400Strasz td2->td_frame->tf_rdx = 1; 207220400Strasz 2081541Srgrimes /* 2091541Srgrimes * If the parent process has the trap bit set (i.e. a debugger had 2101541Srgrimes * single stepped the process to the system call), we need to clear 2111541Srgrimes * the trap flag from the new frame unless the debugger had set PF_FORK 2121541Srgrimes * on the parent. Otherwise, the child will receive a (likely 2131541Srgrimes * unexpected) SIGTRAP when it executes the first instruction after 2141541Srgrimes * returning to userland. 2151541Srgrimes */ 2161541Srgrimes if ((p1->p_pfsflags & PF_FORK) == 0) 2171541Srgrimes td2->td_frame->tf_rflags &= ~PSL_T; 2181541Srgrimes 2191541Srgrimes /* 220168355Srwatson * Set registers for trampoline to user mode. Leave space for the 2211541Srgrimes * return address on stack. These are the kernel mode register values. 2221541Srgrimes */ 2231541Srgrimes pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 2241541Srgrimes pcb2->pcb_rbp = 0; 2251541Srgrimes pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *); 2261541Srgrimes pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */ 2271541Srgrimes pcb2->pcb_rip = (register_t)fork_trampoline; 2281541Srgrimes /*- 2291541Srgrimes * pcb2->pcb_dr*: cloned above. 2301541Srgrimes * pcb2->pcb_savefpu: cloned above. 2311541Srgrimes * pcb2->pcb_flags: cloned above. 2321541Srgrimes * pcb2->pcb_onfault: cloned above (always NULL here?). 23312143Sphk * pcb2->pcb_[fg]sbase: cloned above 234158611Skbyanc */ 235116271Sphk 236132902Sphk /* Setup to release spin count in fork_exit(). */ 237116271Sphk td2->td_md.md_spinlock_count = 1; 238116271Sphk td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I; 239179288Slulf td2->td_md.md_invl_gen.gen = 0; 240116271Sphk 2411541Srgrimes /* As an i386, do not copy io permission bitmap. */ 2422946Swollman pcb2->pcb_tssp = NULL; 24377133Sru 244 /* New segment registers. */ 245 set_pcb_flags(pcb2, PCB_FULL_IRET); 246 247 /* Copy the LDT, if necessary. */ 248 mdp1 = &td1->td_proc->p_md; 249 mdp2 = &p2->p_md; 250 mtx_lock(&dt_lock); 251 if (mdp1->md_ldt != NULL) { 252 if (flags & RFMEM) { 253 mdp1->md_ldt->ldt_refcnt++; 254 mdp2->md_ldt = mdp1->md_ldt; 255 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct 256 system_segment_descriptor)); 257 } else { 258 mdp2->md_ldt = NULL; 259 mdp2->md_ldt = user_ldt_alloc(p2, 0); 260 if (mdp2->md_ldt == NULL) 261 panic("could not copy LDT"); 262 amd64_set_ldt_data(td2, 0, max_ldt_segment, 263 (struct user_segment_descriptor *) 264 mdp1->md_ldt->ldt_base); 265 } 266 } else 267 mdp2->md_ldt = NULL; 268 mtx_unlock(&dt_lock); 269 270 /* 271 * Now, cpu_switch() can schedule the new process. 272 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 273 * containing the return address when exiting cpu_switch. 274 * This will normally be to fork_trampoline(), which will have 275 * %ebx loaded with the new proc's pointer. fork_trampoline() 276 * will set up a stack to call fork_return(p, frame); to complete 277 * the return to user-mode. 278 */ 279} 280 281/* 282 * Intercept the return address from a freshly forked process that has NOT 283 * been scheduled yet. 284 * 285 * This is needed to make kernel threads stay in kernel mode. 286 */ 287void 288cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg) 289{ 290 /* 291 * Note that the trap frame follows the args, so the function 292 * is really called like this: func(arg, frame); 293 */ 294 td->td_pcb->pcb_r12 = (long) func; /* function */ 295 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 296} 297 298void 299cpu_exit(struct thread *td) 300{ 301 302 /* 303 * If this process has a custom LDT, release it. 304 */ 305 mtx_lock(&dt_lock); 306 if (td->td_proc->p_md.md_ldt != 0) 307 user_ldt_free(td); 308 else 309 mtx_unlock(&dt_lock); 310} 311 312void 313cpu_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 331void 332cpu_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 kmem_free(kernel_arena, (vm_offset_t)pcb->pcb_tssp, 343 ctob(IOPAGES + 1)); 344 pcb->pcb_tssp = NULL; 345 } 346} 347 348void 349cpu_thread_swapin(struct thread *td) 350{ 351} 352 353void 354cpu_thread_swapout(struct thread *td) 355{ 356} 357 358void 359cpu_thread_alloc(struct thread *td) 360{ 361 struct pcb *pcb; 362 struct xstate_hdr *xhdr; 363 364 td->td_pcb = pcb = get_pcb_td(td); 365 td->td_frame = (struct trapframe *)pcb - 1; 366 pcb->pcb_save = get_pcb_user_save_pcb(pcb); 367 if (use_xsave) { 368 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1); 369 bzero(xhdr, sizeof(*xhdr)); 370 xhdr->xstate_bv = xsave_mask; 371 } 372} 373 374void 375cpu_thread_free(struct thread *td) 376{ 377 378 cpu_thread_clean(td); 379} 380 381void 382cpu_set_syscall_retval(struct thread *td, int error) 383{ 384 385 switch (error) { 386 case 0: 387 td->td_frame->tf_rax = td->td_retval[0]; 388 td->td_frame->tf_rdx = td->td_retval[1]; 389 td->td_frame->tf_rflags &= ~PSL_C; 390 break; 391 392 case ERESTART: 393 /* 394 * Reconstruct pc, we know that 'syscall' is 2 bytes, 395 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes. 396 * We saved this in tf_err. 397 * %r10 (which was holding the value of %rcx) is restored 398 * for the next iteration. 399 * %r10 restore is only required for freebsd/amd64 processes, 400 * but shall be innocent for any ia32 ABI. 401 * 402 * Require full context restore to get the arguments 403 * in the registers reloaded at return to usermode. 404 */ 405 td->td_frame->tf_rip -= td->td_frame->tf_err; 406 td->td_frame->tf_r10 = td->td_frame->tf_rcx; 407 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 408 break; 409 410 case EJUSTRETURN: 411 break; 412 413 default: 414 td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error); 415 td->td_frame->tf_rflags |= PSL_C; 416 break; 417 } 418} 419 420/* 421 * Initialize machine state, mostly pcb and trap frame for a new 422 * thread, about to return to userspace. Put enough state in the new 423 * thread's PCB to get it to go back to the fork_return(), which 424 * finalizes the thread state and handles peculiarities of the first 425 * return to userspace for the new thread. 426 */ 427void 428cpu_copy_thread(struct thread *td, struct thread *td0) 429{ 430 struct pcb *pcb2; 431 432 /* Point the pcb to the top of the stack. */ 433 pcb2 = td->td_pcb; 434 435 /* 436 * Copy the upcall pcb. This loads kernel regs. 437 * Those not loaded individually below get their default 438 * values here. 439 */ 440 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 441 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE | 442 PCB_KERNFPU); 443 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2); 444 bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save, 445 cpu_max_ext_state_size); 446 set_pcb_flags(pcb2, PCB_FULL_IRET); 447 448 /* 449 * Create a new fresh stack for the new thread. 450 */ 451 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 452 453 /* If the current thread has the trap bit set (i.e. a debugger had 454 * single stepped the process to the system call), we need to clear 455 * the trap flag from the new frame. Otherwise, the new thread will 456 * receive a (likely unexpected) SIGTRAP when it executes the first 457 * instruction after returning to userland. 458 */ 459 td->td_frame->tf_rflags &= ~PSL_T; 460 461 /* 462 * Set registers for trampoline to user mode. Leave space for the 463 * return address on stack. These are the kernel mode register values. 464 */ 465 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 466 pcb2->pcb_rbp = 0; 467 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 468 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 469 pcb2->pcb_rip = (register_t)fork_trampoline; 470 /* 471 * If we didn't copy the pcb, we'd need to do the following registers: 472 * pcb2->pcb_dr*: cloned above. 473 * pcb2->pcb_savefpu: cloned above. 474 * pcb2->pcb_onfault: cloned above (always NULL here?). 475 * pcb2->pcb_[fg]sbase: cloned above 476 */ 477 478 /* Setup to release spin count in fork_exit(). */ 479 td->td_md.md_spinlock_count = 1; 480 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I; 481} 482 483/* 484 * Set that machine state for performing an upcall that starts 485 * the entry function with the given argument. 486 */ 487void 488cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg, 489 stack_t *stack) 490{ 491 492 /* 493 * Do any extra cleaning that needs to be done. 494 * The thread may have optional components 495 * that are not present in a fresh thread. 496 * This may be a recycled thread so make it look 497 * as though it's newly allocated. 498 */ 499 cpu_thread_clean(td); 500 501#ifdef COMPAT_FREEBSD32 502 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 503 /* 504 * Set the trap frame to point at the beginning of the entry 505 * function. 506 */ 507 td->td_frame->tf_rbp = 0; 508 td->td_frame->tf_rsp = 509 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4; 510 td->td_frame->tf_rip = (uintptr_t)entry; 511 512 /* Pass the argument to the entry point. */ 513 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)), 514 (uint32_t)(uintptr_t)arg); 515 516 return; 517 } 518#endif 519 520 /* 521 * Set the trap frame to point at the beginning of the uts 522 * function. 523 */ 524 td->td_frame->tf_rbp = 0; 525 td->td_frame->tf_rsp = 526 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f; 527 td->td_frame->tf_rsp -= 8; 528 td->td_frame->tf_rip = (register_t)entry; 529 td->td_frame->tf_ds = _udatasel; 530 td->td_frame->tf_es = _udatasel; 531 td->td_frame->tf_fs = _ufssel; 532 td->td_frame->tf_gs = _ugssel; 533 td->td_frame->tf_flags = TF_HASSEGS; 534 535 /* Pass the argument to the entry point. */ 536 td->td_frame->tf_rdi = (register_t)arg; 537} 538 539int 540cpu_set_user_tls(struct thread *td, void *tls_base) 541{ 542 struct pcb *pcb; 543 544 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS) 545 return (EINVAL); 546 547 pcb = td->td_pcb; 548 set_pcb_flags(pcb, PCB_FULL_IRET); 549#ifdef COMPAT_FREEBSD32 550 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 551 pcb->pcb_gsbase = (register_t)tls_base; 552 return (0); 553 } 554#endif 555 pcb->pcb_fsbase = (register_t)tls_base; 556 return (0); 557} 558 559#ifdef SMP 560static void 561cpu_reset_proxy() 562{ 563 cpuset_t tcrp; 564 565 cpu_reset_proxy_active = 1; 566 while (cpu_reset_proxy_active == 1) 567 ia32_pause(); /* Wait for other cpu to see that we've started */ 568 569 CPU_SETOF(cpu_reset_proxyid, &tcrp); 570 stop_cpus(tcrp); 571 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 572 DELAY(1000000); 573 cpu_reset_real(); 574} 575#endif 576 577void 578cpu_reset() 579{ 580#ifdef SMP 581 cpuset_t map; 582 u_int cnt; 583 584 if (smp_started) { 585 map = all_cpus; 586 CPU_CLR(PCPU_GET(cpuid), &map); 587 CPU_NAND(&map, &stopped_cpus); 588 if (!CPU_EMPTY(&map)) { 589 printf("cpu_reset: Stopping other CPUs\n"); 590 stop_cpus(map); 591 } 592 593 if (PCPU_GET(cpuid) != 0) { 594 cpu_reset_proxyid = PCPU_GET(cpuid); 595 cpustop_restartfunc = cpu_reset_proxy; 596 cpu_reset_proxy_active = 0; 597 printf("cpu_reset: Restarting BSP\n"); 598 599 /* Restart CPU #0. */ 600 CPU_SETOF(0, &started_cpus); 601 wmb(); 602 603 cnt = 0; 604 while (cpu_reset_proxy_active == 0 && cnt < 10000000) { 605 ia32_pause(); 606 cnt++; /* Wait for BSP to announce restart */ 607 } 608 if (cpu_reset_proxy_active == 0) 609 printf("cpu_reset: Failed to restart BSP\n"); 610 enable_intr(); 611 cpu_reset_proxy_active = 2; 612 613 while (1) 614 ia32_pause(); 615 /* NOTREACHED */ 616 } 617 618 DELAY(1000000); 619 } 620#endif 621 cpu_reset_real(); 622 /* NOTREACHED */ 623} 624 625static void 626cpu_reset_real() 627{ 628 struct region_descriptor null_idt; 629 int b; 630 631 disable_intr(); 632 633 /* 634 * Attempt to do a CPU reset via the keyboard controller, 635 * do not turn off GateA20, as any machine that fails 636 * to do the reset here would then end up in no man's land. 637 */ 638 outb(IO_KBD + 4, 0xFE); 639 DELAY(500000); /* wait 0.5 sec to see if that did it */ 640 641 /* 642 * Attempt to force a reset via the Reset Control register at 643 * I/O port 0xcf9. Bit 2 forces a system reset when it 644 * transitions from 0 to 1. Bit 1 selects the type of reset 645 * to attempt: 0 selects a "soft" reset, and 1 selects a 646 * "hard" reset. We try a "hard" reset. The first write sets 647 * bit 1 to select a "hard" reset and clears bit 2. The 648 * second write forces a 0 -> 1 transition in bit 2 to trigger 649 * a reset. 650 */ 651 outb(0xcf9, 0x2); 652 outb(0xcf9, 0x6); 653 DELAY(500000); /* wait 0.5 sec to see if that did it */ 654 655 /* 656 * Attempt to force a reset via the Fast A20 and Init register 657 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate. 658 * Bit 0 asserts INIT# when set to 1. We are careful to only 659 * preserve bit 1 while setting bit 0. We also must clear bit 660 * 0 before setting it if it isn't already clear. 661 */ 662 b = inb(0x92); 663 if (b != 0xff) { 664 if ((b & 0x1) != 0) 665 outb(0x92, b & 0xfe); 666 outb(0x92, b | 0x1); 667 DELAY(500000); /* wait 0.5 sec to see if that did it */ 668 } 669 670 printf("No known reset method worked, attempting CPU shutdown\n"); 671 DELAY(1000000); /* wait 1 sec for printf to complete */ 672 673 /* Wipe the IDT. */ 674 null_idt.rd_limit = 0; 675 null_idt.rd_base = 0; 676 lidt(&null_idt); 677 678 /* "good night, sweet prince .... <THUNK!>" */ 679 breakpoint(); 680 681 /* NOTREACHED */ 682 while(1); 683} 684 685/* 686 * Software interrupt handler for queued VM system processing. 687 */ 688void 689swi_vm(void *dummy) 690{ 691 if (busdma_swi_pending != 0) 692 busdma_swi(); 693} 694 695/* 696 * Tell whether this address is in some physical memory region. 697 * Currently used by the kernel coredump code in order to avoid 698 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 699 * or other unpredictable behaviour. 700 */ 701 702int 703is_physical_memory(vm_paddr_t addr) 704{ 705 706#ifdef DEV_ISA 707 /* The ISA ``memory hole''. */ 708 if (addr >= 0xa0000 && addr < 0x100000) 709 return 0; 710#endif 711 712 /* 713 * stuff other tests for known memory-mapped devices (PCI?) 714 * here 715 */ 716 717 return 1; 718} 719