The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/vm_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986 The Regents of the University of California.
    3  * Copyright (c) 1989, 1990 William Jolitz
    4  * Copyright (c) 1994 John Dyson
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to Berkeley by
    8  * the Systems Programming Group of the University of Utah Computer
    9  * Science Department, and William Jolitz.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the University of
   22  *      California, Berkeley and its contributors.
   23  * 4. Neither the name of the University nor the names of its contributors
   24  *    may be used to endorse or promote products derived from this software
   25  *    without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   37  * SUCH DAMAGE.
   38  *
   39  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
   40  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __FBSDID("$FreeBSD$");
   45 
   46 #include "opt_isa.h"
   47 #include "opt_cpu.h"
   48 #include "opt_compat.h"
   49 
   50 #include <sys/param.h>
   51 #include <sys/systm.h>
   52 #include <sys/bio.h>
   53 #include <sys/buf.h>
   54 #include <sys/kse.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/sf_buf.h>
   64 #include <sys/smp.h>
   65 #include <sys/sysctl.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/specialreg.h>
   74 
   75 #include <vm/vm.h>
   76 #include <vm/vm_extern.h>
   77 #include <vm/vm_kern.h>
   78 #include <vm/vm_page.h>
   79 #include <vm/vm_map.h>
   80 #include <vm/vm_param.h>
   81 
   82 #include <amd64/isa/isa.h>
   83 
   84 #ifdef COMPAT_IA32
   85 
   86 extern struct sysentvec ia32_freebsd_sysvec;
   87 
   88 #endif
   89 
   90 static void     cpu_reset_real(void);
   91 #ifdef SMP
   92 static void     cpu_reset_proxy(void);
   93 static u_int    cpu_reset_proxyid;
   94 static volatile u_int   cpu_reset_proxy_active;
   95 #endif
   96 
   97 /*
   98  * Finish a fork operation, with process p2 nearly set up.
   99  * Copy and update the pcb, set up the stack so that the child
  100  * ready to run and return to user mode.
  101  */
  102 void
  103 cpu_fork(td1, p2, td2, flags)
  104         register struct thread *td1;
  105         register struct proc *p2;
  106         struct thread *td2;
  107         int flags;
  108 {
  109         register struct proc *p1;
  110         struct pcb *pcb2;
  111         struct mdproc *mdp2;
  112 
  113         p1 = td1->td_proc;
  114         if ((flags & RFPROC) == 0)
  115                 return;
  116 
  117         /* Ensure that p1's pcb is up to date. */
  118         fpuexit(td1);
  119 
  120         /* Point the pcb to the top of the stack */
  121         pcb2 = (struct pcb *)(td2->td_kstack +
  122             td2->td_kstack_pages * PAGE_SIZE) - 1;
  123         td2->td_pcb = pcb2;
  124 
  125         /* Copy p1's pcb */
  126         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
  127 
  128         /* Point mdproc and then copy over td1's contents */
  129         mdp2 = &p2->p_md;
  130         bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
  131 
  132         /*
  133          * Create a new fresh stack for the new process.
  134          * Copy the trap frame for the return to user mode as if from a
  135          * syscall.  This copies most of the user mode register values.
  136          */
  137         td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
  138         bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
  139 
  140         td2->td_frame->tf_rax = 0;              /* Child returns zero */
  141         td2->td_frame->tf_rflags &= ~PSL_C;     /* success */
  142         td2->td_frame->tf_rdx = 1;
  143 
  144         /*
  145          * If the parent process has the trap bit set (i.e. a debugger had
  146          * single stepped the process to the system call), we need to clear
  147          * the trap flag from the new frame unless the debugger had set PF_FORK
  148          * on the parent.  Otherwise, the child will receive a (likely
  149          * unexpected) SIGTRAP when it executes the first instruction after
  150          * returning  to userland.
  151          */
  152         if ((p1->p_pfsflags & PF_FORK) == 0)
  153                 td2->td_frame->tf_rflags &= ~PSL_T;
  154 
  155         /*
  156          * Set registers for trampoline to user mode.  Leave space for the
  157          * return address on stack.  These are the kernel mode register values.
  158          */
  159         pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
  160         pcb2->pcb_r12 = (register_t)fork_return;        /* fork_trampoline argument */
  161         pcb2->pcb_rbp = 0;
  162         pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
  163         pcb2->pcb_rbx = (register_t)td2;                /* fork_trampoline argument */
  164         pcb2->pcb_rip = (register_t)fork_trampoline;
  165         /*-
  166          * pcb2->pcb_dr*:       cloned above.
  167          * pcb2->pcb_savefpu:   cloned above.
  168          * pcb2->pcb_flags:     cloned above.
  169          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  170          * pcb2->pcb_[fg]sbase: cloned above
  171          */
  172 
  173         /* Setup to release sched_lock in fork_exit(). */
  174         td2->td_md.md_spinlock_count = 1;
  175         td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
  176 
  177         /*
  178          * Now, cpu_switch() can schedule the new process.
  179          * pcb_rsp is loaded pointing to the cpu_switch() stack frame
  180          * containing the return address when exiting cpu_switch.
  181          * This will normally be to fork_trampoline(), which will have
  182          * %ebx loaded with the new proc's pointer.  fork_trampoline()
  183          * will set up a stack to call fork_return(p, frame); to complete
  184          * the return to user-mode.
  185          */
  186 }
  187 
  188 /*
  189  * Intercept the return address from a freshly forked process that has NOT
  190  * been scheduled yet.
  191  *
  192  * This is needed to make kernel threads stay in kernel mode.
  193  */
  194 void
  195 cpu_set_fork_handler(td, func, arg)
  196         struct thread *td;
  197         void (*func)(void *);
  198         void *arg;
  199 {
  200         /*
  201          * Note that the trap frame follows the args, so the function
  202          * is really called like this:  func(arg, frame);
  203          */
  204         td->td_pcb->pcb_r12 = (long) func;      /* function */
  205         td->td_pcb->pcb_rbx = (long) arg;       /* first arg */
  206 }
  207 
  208 void
  209 cpu_exit(struct thread *td)
  210 {
  211 }
  212 
  213 void
  214 cpu_thread_exit(struct thread *td)
  215 {
  216 
  217         if (td == PCPU_GET(fpcurthread))
  218                 fpudrop();
  219 
  220         /* Disable any hardware breakpoints. */
  221         if (td->td_pcb->pcb_flags & PCB_DBREGS) {
  222                 reset_dbregs();
  223                 td->td_pcb->pcb_flags &= ~PCB_DBREGS;
  224         }
  225 }
  226 
  227 void
  228 cpu_thread_clean(struct thread *td)
  229 {
  230 }
  231 
  232 void
  233 cpu_thread_swapin(struct thread *td)
  234 {
  235 }
  236 
  237 void
  238 cpu_thread_swapout(struct thread *td)
  239 {
  240 }
  241 
  242 void
  243 cpu_thread_setup(struct thread *td)
  244 {
  245 
  246         td->td_pcb = (struct pcb *)(td->td_kstack +
  247             td->td_kstack_pages * PAGE_SIZE) - 1;
  248         td->td_frame = (struct trapframe *)td->td_pcb - 1;
  249 }
  250 
  251 /*
  252  * Initialize machine state (pcb and trap frame) for a new thread about to
  253  * upcall. Put enough state in the new thread's PCB to get it to go back 
  254  * userret(), where we can intercept it again to set the return (upcall)
  255  * Address and stack, along with those from upcals that are from other sources
  256  * such as those generated in thread_userret() itself.
  257  */
  258 void
  259 cpu_set_upcall(struct thread *td, struct thread *td0)
  260 {
  261         struct pcb *pcb2;
  262 
  263         /* Point the pcb to the top of the stack. */
  264         pcb2 = td->td_pcb;
  265 
  266         /*
  267          * Copy the upcall pcb.  This loads kernel regs.
  268          * Those not loaded individually below get their default
  269          * values here.
  270          *
  271          * XXXKSE It might be a good idea to simply skip this as
  272          * the values of the other registers may be unimportant.
  273          * This would remove any requirement for knowing the KSE
  274          * at this time (see the matching comment below for
  275          * more analysis) (need a good safe default).
  276          */
  277         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
  278         pcb2->pcb_flags &= ~PCB_FPUINITDONE;
  279 
  280         /*
  281          * Create a new fresh stack for the new thread.
  282          * Don't forget to set this stack value into whatever supplies
  283          * the address for the fault handlers.
  284          * The contexts are filled in at the time we actually DO the
  285          * upcall as only then do we know which KSE we got.
  286          */
  287         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
  288 
  289         /*
  290          * Set registers for trampoline to user mode.  Leave space for the
  291          * return address on stack.  These are the kernel mode register values.
  292          */
  293         pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
  294         pcb2->pcb_r12 = (register_t)fork_return;            /* trampoline arg */
  295         pcb2->pcb_rbp = 0;
  296         pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);      /* trampoline arg */
  297         pcb2->pcb_rbx = (register_t)td;                     /* trampoline arg */
  298         pcb2->pcb_rip = (register_t)fork_trampoline;
  299         /*
  300          * If we didn't copy the pcb, we'd need to do the following registers:
  301          * pcb2->pcb_dr*:       cloned above.
  302          * pcb2->pcb_savefpu:   cloned above.
  303          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  304          * pcb2->pcb_[fg]sbase: cloned above
  305          */
  306 
  307         /* Setup to release sched_lock in fork_exit(). */
  308         td->td_md.md_spinlock_count = 1;
  309         td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
  310 }
  311 
  312 /*
  313  * Set that machine state for performing an upcall that has to
  314  * be done in thread_userret() so that those upcalls generated
  315  * in thread_userret() itself can be done as well.
  316  */
  317 void
  318 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
  319         stack_t *stack)
  320 {
  321 
  322         /* 
  323          * Do any extra cleaning that needs to be done.
  324          * The thread may have optional components
  325          * that are not present in a fresh thread.
  326          * This may be a recycled thread so make it look
  327          * as though it's newly allocated.
  328          */
  329         cpu_thread_clean(td);
  330 
  331 #ifdef COMPAT_IA32
  332         if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
  333                 /*
  334                  * Set the trap frame to point at the beginning of the uts
  335                  * function.
  336                  */
  337                 td->td_frame->tf_rbp = 0;
  338                 td->td_frame->tf_rsp =
  339                    (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
  340                 td->td_frame->tf_rip = (uintptr_t)entry;
  341 
  342                 /*
  343                  * Pass the address of the mailbox for this kse to the uts
  344                  * function as a parameter on the stack.
  345                  */
  346                 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
  347                     (uint32_t)(uintptr_t)arg);
  348 
  349                 return;
  350         }
  351 #endif
  352 
  353         /*
  354          * Set the trap frame to point at the beginning of the uts
  355          * function.
  356          */
  357         td->td_frame->tf_rbp = 0;
  358         td->td_frame->tf_rsp =
  359             ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
  360         td->td_frame->tf_rsp -= 8;
  361         td->td_frame->tf_rip = (register_t)entry;
  362 
  363         /*
  364          * Pass the address of the mailbox for this kse to the uts
  365          * function as a parameter on the stack.
  366          */
  367         td->td_frame->tf_rdi = (register_t)arg;
  368 }
  369 
  370 int
  371 cpu_set_user_tls(struct thread *td, void *tls_base)
  372 {
  373 
  374         if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
  375                 return (EINVAL);
  376 
  377 #ifdef COMPAT_IA32
  378         if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
  379                 if (td == curthread) {
  380                         critical_enter();
  381                         td->td_pcb->pcb_gsbase = (register_t)tls_base;
  382                         wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase);
  383                         critical_exit();
  384                 } else {
  385                         td->td_pcb->pcb_gsbase = (register_t)tls_base;
  386                 }
  387                 return (0);
  388         }
  389 #endif
  390         if (td == curthread) {
  391                 critical_enter();
  392                 td->td_pcb->pcb_fsbase = (register_t)tls_base;
  393                 wrmsr(MSR_FSBASE, td->td_pcb->pcb_fsbase);
  394                 critical_exit();
  395         } else {
  396                 td->td_pcb->pcb_fsbase = (register_t)tls_base;
  397         }
  398         return (0);
  399 }
  400 
  401 #ifdef SMP
  402 static void
  403 cpu_reset_proxy()
  404 {
  405 
  406         cpu_reset_proxy_active = 1;
  407         while (cpu_reset_proxy_active == 1)
  408                 ;       /* Wait for other cpu to see that we've started */
  409         stop_cpus((1<<cpu_reset_proxyid));
  410         printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
  411         DELAY(1000000);
  412         cpu_reset_real();
  413 }
  414 #endif
  415 
  416 void
  417 cpu_reset()
  418 {
  419 #ifdef SMP
  420         u_int cnt, map;
  421 
  422         if (smp_active) {
  423                 map = PCPU_GET(other_cpus) & ~stopped_cpus;
  424                 if (map != 0) {
  425                         printf("cpu_reset: Stopping other CPUs\n");
  426                         stop_cpus(map);
  427                 }
  428 
  429                 if (PCPU_GET(cpuid) != 0) {
  430                         cpu_reset_proxyid = PCPU_GET(cpuid);
  431                         cpustop_restartfunc = cpu_reset_proxy;
  432                         cpu_reset_proxy_active = 0;
  433                         printf("cpu_reset: Restarting BSP\n");
  434                         started_cpus = (1<<0);          /* Restart CPU #0 */
  435 
  436                         cnt = 0;
  437                         while (cpu_reset_proxy_active == 0 && cnt < 10000000)
  438                                 cnt++;  /* Wait for BSP to announce restart */
  439                         if (cpu_reset_proxy_active == 0)
  440                                 printf("cpu_reset: Failed to restart BSP\n");
  441                         enable_intr();
  442                         cpu_reset_proxy_active = 2;
  443 
  444                         while (1);
  445                         /* NOTREACHED */
  446                 }
  447 
  448                 DELAY(1000000);
  449         }
  450 #endif
  451         cpu_reset_real();
  452         /* NOTREACHED */
  453 }
  454 
  455 static void
  456 cpu_reset_real()
  457 {
  458         struct region_descriptor null_idt;
  459         int b;
  460 
  461         disable_intr();
  462 
  463         /*
  464          * Attempt to do a CPU reset via the keyboard controller,
  465          * do not turn off GateA20, as any machine that fails
  466          * to do the reset here would then end up in no man's land.
  467          */
  468         outb(IO_KBD + 4, 0xFE);
  469         DELAY(500000);  /* wait 0.5 sec to see if that did it */
  470 
  471         /*
  472          * Attempt to force a reset via the Reset Control register at
  473          * I/O port 0xcf9.  Bit 2 forces a system reset when it
  474          * transitions from 0 to 1.  Bit 1 selects the type of reset
  475          * to attempt: 0 selects a "soft" reset, and 1 selects a
  476          * "hard" reset.  We try a "hard" reset.  The first write sets
  477          * bit 1 to select a "hard" reset and clears bit 2.  The
  478          * second write forces a 0 -> 1 transition in bit 2 to trigger
  479          * a reset.
  480          */
  481         outb(0xcf9, 0x2);
  482         outb(0xcf9, 0x6);
  483         DELAY(500000);  /* wait 0.5 sec to see if that did it */
  484 
  485         /*
  486          * Attempt to force a reset via the Fast A20 and Init register
  487          * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
  488          * Bit 0 asserts INIT# when set to 1.  We are careful to only
  489          * preserve bit 1 while setting bit 0.  We also must clear bit
  490          * 0 before setting it if it isn't already clear.
  491          */
  492         b = inb(0x92);
  493         if (b != 0xff) {
  494                 if ((b & 0x1) != 0)
  495                         outb(0x92, b & 0xfe);
  496                 outb(0x92, b | 0x1);
  497                 DELAY(500000);  /* wait 0.5 sec to see if that did it */
  498         }
  499 
  500         printf("No known reset method worked, attempting CPU shutdown\n");
  501         DELAY(1000000); /* wait 1 sec for printf to complete */
  502 
  503         /* Wipe the IDT. */
  504         null_idt.rd_limit = 0;
  505         null_idt.rd_base = 0;
  506         lidt(&null_idt);
  507 
  508         /* "good night, sweet prince .... <THUNK!>" */
  509         breakpoint();
  510 
  511         /* NOTREACHED */
  512         while(1);
  513 }
  514 
  515 /*
  516  * Allocate an sf_buf for the given vm_page.  On this machine, however, there
  517  * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
  518  * returned.
  519  */
  520 struct sf_buf *
  521 sf_buf_alloc(struct vm_page *m, int pri)
  522 {
  523 
  524         return ((struct sf_buf *)m);
  525 }
  526 
  527 /*
  528  * Free the sf_buf.  In fact, do nothing because there are no resources
  529  * associated with the sf_buf.
  530  */
  531 void
  532 sf_buf_free(struct sf_buf *sf)
  533 {
  534 }
  535 
  536 /*
  537  * Software interrupt handler for queued VM system processing.
  538  */   
  539 void  
  540 swi_vm(void *dummy) 
  541 {     
  542         if (busdma_swi_pending != 0)
  543                 busdma_swi();
  544 }
  545 
  546 /*
  547  * Tell whether this address is in some physical memory region.
  548  * Currently used by the kernel coredump code in order to avoid
  549  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
  550  * or other unpredictable behaviour.
  551  */
  552 
  553 int
  554 is_physical_memory(vm_paddr_t addr)
  555 {
  556 
  557 #ifdef DEV_ISA
  558         /* The ISA ``memory hole''. */
  559         if (addr >= 0xa0000 && addr < 0x100000)
  560                 return 0;
  561 #endif
  562 
  563         /*
  564          * stuff other tests for known memory-mapped devices (PCI?)
  565          * here
  566          */
  567 
  568         return 1;
  569 }

Cache object: befe497e68488fdb29c945e811130777


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.