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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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: releng/5.3/sys/amd64/amd64/vm_machdep.c 133902 2004-08-16 22:57:13Z peter $");
   45 
   46 #include "opt_isa.h"
   47 #include "opt_cpu.h"
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/bio.h>
   52 #include <sys/buf.h>
   53 #include <sys/kse.h>
   54 #include <sys/kernel.h>
   55 #include <sys/ktr.h>
   56 #include <sys/lock.h>
   57 #include <sys/malloc.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/mutex.h>
   60 #include <sys/proc.h>
   61 #include <sys/sf_buf.h>
   62 #include <sys/smp.h>
   63 #include <sys/sysctl.h>
   64 #include <sys/unistd.h>
   65 #include <sys/user.h>
   66 #include <sys/vnode.h>
   67 #include <sys/vmmeter.h>
   68 
   69 #include <machine/cpu.h>
   70 #include <machine/md_var.h>
   71 #include <machine/pcb.h>
   72 
   73 #include <vm/vm.h>
   74 #include <vm/vm_extern.h>
   75 #include <vm/vm_kern.h>
   76 #include <vm/vm_page.h>
   77 #include <vm/vm_map.h>
   78 #include <vm/vm_param.h>
   79 
   80 #include <amd64/isa/isa.h>
   81 
   82 static void     cpu_reset_real(void);
   83 #ifdef SMP
   84 static void     cpu_reset_proxy(void);
   85 static u_int    cpu_reset_proxyid;
   86 static volatile u_int   cpu_reset_proxy_active;
   87 #endif
   88 
   89 /*
   90  * Finish a fork operation, with process p2 nearly set up.
   91  * Copy and update the pcb, set up the stack so that the child
   92  * ready to run and return to user mode.
   93  */
   94 void
   95 cpu_fork(td1, p2, td2, flags)
   96         register struct thread *td1;
   97         register struct proc *p2;
   98         struct thread *td2;
   99         int flags;
  100 {
  101         register struct proc *p1;
  102         struct pcb *pcb2;
  103         struct mdproc *mdp2;
  104 
  105         p1 = td1->td_proc;
  106         if ((flags & RFPROC) == 0)
  107                 return;
  108 
  109         /* Ensure that p1's pcb is up to date. */
  110         fpuexit(td1);
  111 
  112         /* Point the pcb to the top of the stack */
  113         pcb2 = (struct pcb *)(td2->td_kstack +
  114             td2->td_kstack_pages * PAGE_SIZE) - 1;
  115         td2->td_pcb = pcb2;
  116 
  117         /* Copy p1's pcb */
  118         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
  119 
  120         /* Point mdproc and then copy over td1's contents */
  121         mdp2 = &p2->p_md;
  122         bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
  123 
  124         /*
  125          * Create a new fresh stack for the new process.
  126          * Copy the trap frame for the return to user mode as if from a
  127          * syscall.  This copies most of the user mode register values.
  128          */
  129         td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
  130         bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
  131 
  132         td2->td_frame->tf_rax = 0;              /* Child returns zero */
  133         td2->td_frame->tf_rflags &= ~PSL_C;     /* success */
  134         td2->td_frame->tf_rdx = 1;
  135 
  136         /*
  137          * Set registers for trampoline to user mode.  Leave space for the
  138          * return address on stack.  These are the kernel mode register values.
  139          */
  140         pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
  141         pcb2->pcb_r12 = (register_t)fork_return;        /* fork_trampoline argument */
  142         pcb2->pcb_rbp = 0;
  143         pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
  144         pcb2->pcb_rbx = (register_t)td2;                /* fork_trampoline argument */
  145         pcb2->pcb_rip = (register_t)fork_trampoline;
  146         pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */
  147         /*-
  148          * pcb2->pcb_dr*:       cloned above.
  149          * pcb2->pcb_savefpu:   cloned above.
  150          * pcb2->pcb_flags:     cloned above.
  151          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  152          * pcb2->pcb_[fg]sbase: cloned above
  153          */
  154 
  155         /*
  156          * Now, cpu_switch() can schedule the new process.
  157          * pcb_rsp is loaded pointing to the cpu_switch() stack frame
  158          * containing the return address when exiting cpu_switch.
  159          * This will normally be to fork_trampoline(), which will have
  160          * %ebx loaded with the new proc's pointer.  fork_trampoline()
  161          * will set up a stack to call fork_return(p, frame); to complete
  162          * the return to user-mode.
  163          */
  164 }
  165 
  166 /*
  167  * Intercept the return address from a freshly forked process that has NOT
  168  * been scheduled yet.
  169  *
  170  * This is needed to make kernel threads stay in kernel mode.
  171  */
  172 void
  173 cpu_set_fork_handler(td, func, arg)
  174         struct thread *td;
  175         void (*func)(void *);
  176         void *arg;
  177 {
  178         /*
  179          * Note that the trap frame follows the args, so the function
  180          * is really called like this:  func(arg, frame);
  181          */
  182         td->td_pcb->pcb_r12 = (long) func;      /* function */
  183         td->td_pcb->pcb_rbx = (long) arg;       /* first arg */
  184 }
  185 
  186 void
  187 cpu_exit(struct thread *td)
  188 {
  189         struct pcb *pcb = td->td_pcb;
  190 
  191         if (pcb->pcb_flags & PCB_DBREGS) {
  192                 /* disable all hardware breakpoints */
  193                 reset_dbregs();
  194                 pcb->pcb_flags &= ~PCB_DBREGS;
  195         }
  196 }
  197 
  198 void
  199 cpu_thread_exit(struct thread *td)
  200 {
  201         struct pcb *pcb = td->td_pcb;
  202 
  203         if (td == PCPU_GET(fpcurthread))
  204                 fpudrop();
  205         if (pcb->pcb_flags & PCB_DBREGS) {
  206                 /* disable all hardware breakpoints */
  207                 reset_dbregs();
  208                 pcb->pcb_flags &= ~PCB_DBREGS;
  209         }
  210 }
  211 
  212 void
  213 cpu_thread_clean(struct thread *td)
  214 {
  215 }
  216 
  217 void
  218 cpu_thread_swapin(struct thread *td)
  219 {
  220 }
  221 
  222 void
  223 cpu_thread_swapout(struct thread *td)
  224 {
  225 }
  226 
  227 void
  228 cpu_thread_setup(struct thread *td)
  229 {
  230 
  231         td->td_pcb = (struct pcb *)(td->td_kstack +
  232             td->td_kstack_pages * PAGE_SIZE) - 1;
  233         td->td_frame = (struct trapframe *)td->td_pcb - 1;
  234 }
  235 
  236 /*
  237  * Initialize machine state (pcb and trap frame) for a new thread about to
  238  * upcall. Pu t enough state in the new thread's PCB to get it to go back 
  239  * userret(), where we can intercept it again to set the return (upcall)
  240  * Address and stack, along with those from upcals that are from other sources
  241  * such as those generated in thread_userret() itself.
  242  */
  243 void
  244 cpu_set_upcall(struct thread *td, struct thread *td0)
  245 {
  246         struct pcb *pcb2;
  247 
  248         /* Point the pcb to the top of the stack. */
  249         pcb2 = td->td_pcb;
  250 
  251         /*
  252          * Copy the upcall pcb.  This loads kernel regs.
  253          * Those not loaded individually below get their default
  254          * values here.
  255          *
  256          * XXXKSE It might be a good idea to simply skip this as
  257          * the values of the other registers may be unimportant.
  258          * This would remove any requirement for knowing the KSE
  259          * at this time (see the matching comment below for
  260          * more analysis) (need a good safe default).
  261          */
  262         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
  263         pcb2->pcb_flags &= ~PCB_FPUINITDONE;
  264 
  265         /*
  266          * Create a new fresh stack for the new thread.
  267          * Don't forget to set this stack value into whatever supplies
  268          * the address for the fault handlers.
  269          * The contexts are filled in at the time we actually DO the
  270          * upcall as only then do we know which KSE we got.
  271          */
  272         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
  273 
  274         /*
  275          * Set registers for trampoline to user mode.  Leave space for the
  276          * return address on stack.  These are the kernel mode register values.
  277          */
  278         pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
  279         pcb2->pcb_r12 = (register_t)fork_return;            /* trampoline arg */
  280         pcb2->pcb_rbp = 0;
  281         pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);      /* trampoline arg */
  282         pcb2->pcb_rbx = (register_t)td;                     /* trampoline arg */
  283         pcb2->pcb_rip = (register_t)fork_trampoline;
  284         pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */
  285         /*
  286          * If we didn't copy the pcb, we'd need to do the following registers:
  287          * pcb2->pcb_dr*:       cloned above.
  288          * pcb2->pcb_savefpu:   cloned above.
  289          * pcb2->pcb_rflags:    cloned above.
  290          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  291          * pcb2->pcb_[fg]sbase: cloned above
  292          */
  293 }
  294 
  295 /*
  296  * Set that machine state for performing an upcall that has to
  297  * be done in thread_userret() so that those upcalls generated
  298  * in thread_userret() itself can be done as well.
  299  */
  300 void
  301 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
  302 {
  303 
  304         /* 
  305          * Do any extra cleaning that needs to be done.
  306          * The thread may have optional components
  307          * that are not present in a fresh thread.
  308          * This may be a recycled thread so make it look
  309          * as though it's newly allocated.
  310          */
  311         cpu_thread_clean(td);
  312 
  313         /*
  314          * Set the trap frame to point at the beginning of the uts
  315          * function.
  316          */
  317         td->td_frame->tf_rbp = 0; 
  318         td->td_frame->tf_rsp =
  319             ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f;
  320         td->td_frame->tf_rsp -= 8;
  321         td->td_frame->tf_rbp = 0;
  322         td->td_frame->tf_rip = (register_t)ku->ku_func;
  323 
  324         /*
  325          * Pass the address of the mailbox for this kse to the uts
  326          * function as a parameter on the stack.
  327          */
  328         td->td_frame->tf_rdi = (register_t)ku->ku_mailbox;
  329 }
  330 
  331 
  332 /*
  333  * Force reset the processor by invalidating the entire address space!
  334  */
  335 
  336 #ifdef SMP
  337 static void
  338 cpu_reset_proxy()
  339 {
  340 
  341         cpu_reset_proxy_active = 1;
  342         while (cpu_reset_proxy_active == 1)
  343                 ;        /* Wait for other cpu to see that we've started */
  344         stop_cpus((1<<cpu_reset_proxyid));
  345         printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
  346         DELAY(1000000);
  347         cpu_reset_real();
  348 }
  349 #endif
  350 
  351 void
  352 cpu_reset()
  353 {
  354 #ifdef SMP
  355         if (smp_active == 0) {
  356                 cpu_reset_real();
  357                 /* NOTREACHED */
  358         } else {
  359 
  360                 u_int map;
  361                 int cnt;
  362                 printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
  363 
  364                 map = PCPU_GET(other_cpus) & ~ stopped_cpus;
  365 
  366                 if (map != 0) {
  367                         printf("cpu_reset: Stopping other CPUs\n");
  368                         stop_cpus(map);         /* Stop all other CPUs */
  369                 }
  370 
  371                 if (PCPU_GET(cpuid) == 0) {
  372                         DELAY(1000000);
  373                         cpu_reset_real();
  374                         /* NOTREACHED */
  375                 } else {
  376                         /* We are not BSP (CPU #0) */
  377 
  378                         cpu_reset_proxyid = PCPU_GET(cpuid);
  379                         cpustop_restartfunc = cpu_reset_proxy;
  380                         cpu_reset_proxy_active = 0;
  381                         printf("cpu_reset: Restarting BSP\n");
  382                         started_cpus = (1<<0);          /* Restart CPU #0 */
  383 
  384                         cnt = 0;
  385                         while (cpu_reset_proxy_active == 0 && cnt < 10000000)
  386                                 cnt++;  /* Wait for BSP to announce restart */
  387                         if (cpu_reset_proxy_active == 0)
  388                                 printf("cpu_reset: Failed to restart BSP\n");
  389                         enable_intr();
  390                         cpu_reset_proxy_active = 2;
  391 
  392                         while (1);
  393                         /* NOTREACHED */
  394                 }
  395         }
  396 #else
  397         cpu_reset_real();
  398 #endif
  399 }
  400 
  401 static void
  402 cpu_reset_real()
  403 {
  404 
  405         /*
  406          * Attempt to do a CPU reset via the keyboard controller,
  407          * do not turn of the GateA20, as any machine that fails
  408          * to do the reset here would then end up in no man's land.
  409          */
  410 
  411         outb(IO_KBD + 4, 0xFE);
  412         DELAY(500000);  /* wait 0.5 sec to see if that did it */
  413         printf("Keyboard reset did not work, attempting CPU shutdown\n");
  414         DELAY(1000000); /* wait 1 sec for printf to complete */
  415         /* force a shutdown by unmapping entire address space ! */
  416         bzero((caddr_t)PML4map, PAGE_SIZE);
  417 
  418         /* "good night, sweet prince .... <THUNK!>" */
  419         invltlb();
  420         /* NOTREACHED */
  421         while(1);
  422 }
  423 
  424 /*
  425  * Allocate an sf_buf for the given vm_page.  On this machine, however, there
  426  * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
  427  * returned.
  428  */
  429 struct sf_buf *
  430 sf_buf_alloc(struct vm_page *m, int pri)
  431 {
  432 
  433         return ((struct sf_buf *)m);
  434 }
  435 
  436 /*
  437  * Free the sf_buf.  In fact, do nothing because there are no resources
  438  * associated with the sf_buf.
  439  */
  440 void
  441 sf_buf_free(struct sf_buf *sf)
  442 {
  443 }
  444 
  445 /*
  446  * Software interrupt handler for queued VM system processing.
  447  */   
  448 void  
  449 swi_vm(void *dummy) 
  450 {     
  451         if (busdma_swi_pending != 0)
  452                 busdma_swi();
  453 }
  454 
  455 /*
  456  * Tell whether this address is in some physical memory region.
  457  * Currently used by the kernel coredump code in order to avoid
  458  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
  459  * or other unpredictable behaviour.
  460  */
  461 
  462 int
  463 is_physical_memory(vm_paddr_t addr)
  464 {
  465 
  466 #ifdef DEV_ISA
  467         /* The ISA ``memory hole''. */
  468         if (addr >= 0xa0000 && addr < 0x100000)
  469                 return 0;
  470 #endif
  471 
  472         /*
  473          * stuff other tests for known memory-mapped devices (PCI?)
  474          * here
  475          */
  476 
  477         return 1;
  478 }

Cache object: 1043d1bc2bfbcd96adceb17dbb1e9cf3


[ 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.