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

Cache object: 935674b817d70a7067ab9820879b5144


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