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/i386/i386/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: releng/5.2/sys/i386/i386/vm_machdep.c 122860 2003-11-17 18:22:24Z alc $");
   45 
   46 #include "opt_npx.h"
   47 #ifdef PC98
   48 #include "opt_pc98.h"
   49 #endif
   50 #include "opt_reset.h"
   51 #include "opt_isa.h"
   52 #include "opt_kstack_pages.h"
   53 
   54 #include <sys/param.h>
   55 #include <sys/systm.h>
   56 #include <sys/malloc.h>
   57 #include <sys/proc.h>
   58 #include <sys/kse.h>
   59 #include <sys/bio.h>
   60 #include <sys/buf.h>
   61 #include <sys/vnode.h>
   62 #include <sys/vmmeter.h>
   63 #include <sys/kernel.h>
   64 #include <sys/ktr.h>
   65 #include <sys/mbuf.h>
   66 #include <sys/mutex.h>
   67 #include <sys/smp.h>
   68 #include <sys/sf_buf.h>
   69 #include <sys/sysctl.h>
   70 #include <sys/unistd.h>
   71 
   72 #include <machine/cpu.h>
   73 #include <machine/md_var.h>
   74 #include <machine/pcb.h>
   75 #include <machine/pcb_ext.h>
   76 #include <machine/vm86.h>
   77 
   78 #include <vm/vm.h>
   79 #include <vm/vm_param.h>
   80 #include <sys/lock.h>
   81 #include <vm/vm_kern.h>
   82 #include <vm/vm_page.h>
   83 #include <vm/vm_map.h>
   84 #include <vm/vm_extern.h>
   85 
   86 #include <sys/user.h>
   87 
   88 #ifdef PC98
   89 #include <pc98/pc98/pc98.h>
   90 #else
   91 #include <i386/isa/isa.h>
   92 #endif
   93 
   94 static void     cpu_reset_real(void);
   95 #ifdef SMP
   96 static void     cpu_reset_proxy(void);
   97 static u_int    cpu_reset_proxyid;
   98 static volatile u_int   cpu_reset_proxy_active;
   99 #endif
  100 static void     sf_buf_init(void *arg);
  101 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
  102 
  103 LIST_HEAD(sf_head, sf_buf);
  104 
  105 /*
  106  * A hash table of active sendfile(2) buffers
  107  */
  108 static struct sf_head *sf_buf_active;
  109 static u_long sf_buf_hashmask;
  110 
  111 #define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
  112 
  113 static struct sf_head sf_buf_freelist;
  114 static u_int    sf_buf_alloc_want;
  115 
  116 /*
  117  * A lock used to synchronize access to the hash table and free list
  118  */
  119 static struct mtx sf_buf_lock;
  120 
  121 extern int      _ucodesel, _udatasel;
  122 
  123 /*
  124  * Finish a fork operation, with process p2 nearly set up.
  125  * Copy and update the pcb, set up the stack so that the child
  126  * ready to run and return to user mode.
  127  */
  128 void
  129 cpu_fork(td1, p2, td2, flags)
  130         register struct thread *td1;
  131         register struct proc *p2;
  132         struct thread *td2;
  133         int flags;
  134 {
  135         register struct proc *p1;
  136         struct pcb *pcb2;
  137         struct mdproc *mdp2;
  138 #ifdef DEV_NPX
  139         register_t savecrit;
  140 #endif
  141 
  142         p1 = td1->td_proc;
  143         if ((flags & RFPROC) == 0) {
  144                 if ((flags & RFMEM) == 0) {
  145                         /* unshare user LDT */
  146                         struct mdproc *mdp1 = &p1->p_md;
  147                         struct proc_ldt *pldt = mdp1->md_ldt;
  148                         if (pldt && pldt->ldt_refcnt > 1) {
  149                                 pldt = user_ldt_alloc(mdp1, pldt->ldt_len);
  150                                 if (pldt == NULL)
  151                                         panic("could not copy LDT");
  152                                 mdp1->md_ldt = pldt;
  153                                 set_user_ldt(mdp1);
  154                                 user_ldt_free(td1);
  155                         }
  156                 }
  157                 return;
  158         }
  159 
  160         /* Ensure that p1's pcb is up to date. */
  161 #ifdef DEV_NPX
  162         if (td1 == curthread)
  163                 td1->td_pcb->pcb_gs = rgs();
  164         savecrit = intr_disable();
  165         if (PCPU_GET(fpcurthread) == td1)
  166                 npxsave(&td1->td_pcb->pcb_save);
  167         intr_restore(savecrit);
  168 #endif
  169 
  170         /* Point the pcb to the top of the stack */
  171         pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
  172         td2->td_pcb = pcb2;
  173 
  174         /* Copy p1's pcb */
  175         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
  176 
  177         /* Point mdproc and then copy over td1's contents */
  178         mdp2 = &p2->p_md;
  179         bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
  180 
  181         /*
  182          * Create a new fresh stack for the new process.
  183          * Copy the trap frame for the return to user mode as if from a
  184          * syscall.  This copies most of the user mode register values.
  185          * The -16 is so we can expand the trapframe if we go to vm86.
  186          */
  187         td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb - 16) - 1;
  188         bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
  189 
  190         td2->td_frame->tf_eax = 0;              /* Child returns zero */
  191         td2->td_frame->tf_eflags &= ~PSL_C;     /* success */
  192         td2->td_frame->tf_edx = 1;
  193 
  194         /*
  195          * Set registers for trampoline to user mode.  Leave space for the
  196          * return address on stack.  These are the kernel mode register values.
  197          */
  198 #ifdef PAE
  199         pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt);
  200 #else
  201         pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
  202 #endif
  203         pcb2->pcb_edi = 0;
  204         pcb2->pcb_esi = (int)fork_return;       /* fork_trampoline argument */
  205         pcb2->pcb_ebp = 0;
  206         pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *);
  207         pcb2->pcb_ebx = (int)td2;               /* fork_trampoline argument */
  208         pcb2->pcb_eip = (int)fork_trampoline;
  209         pcb2->pcb_psl = PSL_KERNEL;             /* ints disabled */
  210         pcb2->pcb_gs = rgs();
  211         /*-
  212          * pcb2->pcb_dr*:       cloned above.
  213          * pcb2->pcb_savefpu:   cloned above.
  214          * pcb2->pcb_flags:     cloned above.
  215          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  216          * pcb2->pcb_gs:        cloned above.
  217          * pcb2->pcb_ext:       cleared below.
  218          */
  219 
  220         /*
  221          * XXX don't copy the i/o pages.  this should probably be fixed.
  222          */
  223         pcb2->pcb_ext = 0;
  224 
  225         /* Copy the LDT, if necessary. */
  226         mtx_lock_spin(&sched_lock);
  227         if (mdp2->md_ldt != 0) {
  228                 if (flags & RFMEM) {
  229                         mdp2->md_ldt->ldt_refcnt++;
  230                 } else {
  231                         mdp2->md_ldt = user_ldt_alloc(mdp2,
  232                             mdp2->md_ldt->ldt_len);
  233                         if (mdp2->md_ldt == NULL)
  234                                 panic("could not copy LDT");
  235                 }
  236         }
  237         mtx_unlock_spin(&sched_lock);
  238 
  239         /*
  240          * Now, cpu_switch() can schedule the new process.
  241          * pcb_esp is loaded pointing to the cpu_switch() stack frame
  242          * containing the return address when exiting cpu_switch.
  243          * This will normally be to fork_trampoline(), which will have
  244          * %ebx loaded with the new proc's pointer.  fork_trampoline()
  245          * will set up a stack to call fork_return(p, frame); to complete
  246          * the return to user-mode.
  247          */
  248 }
  249 
  250 /*
  251  * Intercept the return address from a freshly forked process that has NOT
  252  * been scheduled yet.
  253  *
  254  * This is needed to make kernel threads stay in kernel mode.
  255  */
  256 void
  257 cpu_set_fork_handler(td, func, arg)
  258         struct thread *td;
  259         void (*func)(void *);
  260         void *arg;
  261 {
  262         /*
  263          * Note that the trap frame follows the args, so the function
  264          * is really called like this:  func(arg, frame);
  265          */
  266         td->td_pcb->pcb_esi = (int) func;       /* function */
  267         td->td_pcb->pcb_ebx = (int) arg;        /* first arg */
  268 }
  269 
  270 void
  271 cpu_exit(struct thread *td)
  272 {
  273         struct mdproc *mdp;
  274         struct pcb *pcb = td->td_pcb; 
  275 
  276 
  277         /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
  278         mdp = &td->td_proc->p_md;
  279         if (mdp->md_ldt) {
  280                 td->td_pcb->pcb_gs = _udatasel;
  281                 load_gs(_udatasel);
  282                 user_ldt_free(td);
  283         }
  284         if (pcb->pcb_flags & PCB_DBREGS) {
  285                 /* disable all hardware breakpoints */
  286                 reset_dbregs();
  287                 pcb->pcb_flags &= ~PCB_DBREGS;
  288         }
  289 }
  290 
  291 void
  292 cpu_thread_exit(struct thread *td)
  293 {
  294         struct pcb *pcb = td->td_pcb; 
  295 #ifdef DEV_NPX
  296         if (td == PCPU_GET(fpcurthread))
  297                 npxdrop();
  298 #endif
  299         if (pcb->pcb_flags & PCB_DBREGS) {
  300                 /* disable all hardware breakpoints */
  301                 reset_dbregs();
  302                 pcb->pcb_flags &= ~PCB_DBREGS;
  303         }
  304 }
  305 
  306 void
  307 cpu_thread_clean(struct thread *td)
  308 {
  309         struct pcb *pcb;
  310 
  311         pcb = td->td_pcb; 
  312         if (pcb->pcb_ext != 0) {
  313                 /* XXXKSE  XXXSMP  not SMP SAFE.. what locks do we have? */
  314                 /* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
  315                 /*
  316                  * XXX do we need to move the TSS off the allocated pages
  317                  * before freeing them?  (not done here)
  318                  */
  319                 mtx_lock(&Giant);
  320                 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
  321                     ctob(IOPAGES + 1));
  322                 mtx_unlock(&Giant);
  323                 pcb->pcb_ext = 0;
  324         }
  325 }
  326 
  327 void
  328 cpu_thread_swapin(struct thread *td)
  329 {
  330 }
  331 
  332 void
  333 cpu_thread_swapout(struct thread *td)
  334 {
  335 }
  336 
  337 void
  338 cpu_sched_exit(td)
  339         register struct thread *td;
  340 {
  341 }
  342 
  343 void
  344 cpu_thread_setup(struct thread *td)
  345 {
  346 
  347         td->td_pcb =
  348              (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
  349         td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb - 16) - 1;
  350         td->td_pcb->pcb_ext = NULL; 
  351 }
  352 
  353 /*
  354  * Initialize machine state (pcb and trap frame) for a new thread about to
  355  * upcall. Pu t enough state in the new thread's PCB to get it to go back 
  356  * userret(), where we can intercept it again to set the return (upcall)
  357  * Address and stack, along with those from upcals that are from other sources
  358  * such as those generated in thread_userret() itself.
  359  */
  360 void
  361 cpu_set_upcall(struct thread *td, struct thread *td0)
  362 {
  363         struct pcb *pcb2;
  364 
  365         /* Point the pcb to the top of the stack. */
  366         pcb2 = td->td_pcb;
  367 
  368         /*
  369          * Copy the upcall pcb.  This loads kernel regs.
  370          * Those not loaded individually below get their default
  371          * values here.
  372          *
  373          * XXXKSE It might be a good idea to simply skip this as
  374          * the values of the other registers may be unimportant.
  375          * This would remove any requirement for knowing the KSE
  376          * at this time (see the matching comment below for
  377          * more analysis) (need a good safe default).
  378          */
  379         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
  380         pcb2->pcb_flags &= ~(PCB_NPXTRAP|PCB_NPXINITDONE);
  381 
  382         /*
  383          * Create a new fresh stack for the new thread.
  384          * The -16 is so we can expand the trapframe if we go to vm86.
  385          * Don't forget to set this stack value into whatever supplies
  386          * the address for the fault handlers.
  387          * The contexts are filled in at the time we actually DO the
  388          * upcall as only then do we know which KSE we got.
  389          */
  390         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
  391 
  392         /*
  393          * Set registers for trampoline to user mode.  Leave space for the
  394          * return address on stack.  These are the kernel mode register values.
  395          */
  396 #ifdef PAE
  397         pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdpt);
  398 #else
  399         pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdir);
  400 #endif
  401         pcb2->pcb_edi = 0;
  402         pcb2->pcb_esi = (int)fork_return;                   /* trampoline arg */
  403         pcb2->pcb_ebp = 0;
  404         pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
  405         pcb2->pcb_ebx = (int)td;                            /* trampoline arg */
  406         pcb2->pcb_eip = (int)fork_trampoline;
  407         pcb2->pcb_psl &= ~(PSL_I);      /* interrupts must be disabled */
  408         pcb2->pcb_gs = rgs();
  409         /*
  410          * If we didn't copy the pcb, we'd need to do the following registers:
  411          * pcb2->pcb_dr*:       cloned above.
  412          * pcb2->pcb_savefpu:   cloned above.
  413          * pcb2->pcb_flags:     cloned above.
  414          * pcb2->pcb_onfault:   cloned above (always NULL here?).
  415          * pcb2->pcb_gs:        cloned above.  XXXKSE ???
  416          * pcb2->pcb_ext:       cleared below.
  417          */
  418          pcb2->pcb_ext = NULL;
  419 }
  420 
  421 /*
  422  * Set that machine state for performing an upcall that has to
  423  * be done in thread_userret() so that those upcalls generated
  424  * in thread_userret() itself can be done as well.
  425  */
  426 void
  427 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
  428 {
  429 
  430         /* 
  431          * Do any extra cleaning that needs to be done.
  432          * The thread may have optional components
  433          * that are not present in a fresh thread.
  434          * This may be a recycled thread so make it look
  435          * as though it's newly allocated.
  436          */
  437         cpu_thread_clean(td);
  438 
  439         /*
  440          * Set the trap frame to point at the beginning of the uts
  441          * function.
  442          */
  443         td->td_frame->tf_esp =
  444             (int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size - 16;
  445         td->td_frame->tf_eip = (int)ku->ku_func;
  446 
  447         /*
  448          * Pass the address of the mailbox for this kse to the uts
  449          * function as a parameter on the stack.
  450          */
  451         suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
  452             (int)ku->ku_mailbox);
  453 }
  454 
  455 /*
  456  * Convert kernel VA to physical address
  457  */
  458 vm_paddr_t
  459 kvtop(void *addr)
  460 {
  461         vm_paddr_t pa;
  462 
  463         pa = pmap_kextract((vm_offset_t)addr);
  464         if (pa == 0)
  465                 panic("kvtop: zero page frame");
  466         return (pa);
  467 }
  468 
  469 /*
  470  * Force reset the processor by invalidating the entire address space!
  471  */
  472 
  473 #ifdef SMP
  474 static void
  475 cpu_reset_proxy()
  476 {
  477 
  478         cpu_reset_proxy_active = 1;
  479         while (cpu_reset_proxy_active == 1)
  480                 ;        /* Wait for other cpu to see that we've started */
  481         stop_cpus((1<<cpu_reset_proxyid));
  482         printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
  483         DELAY(1000000);
  484         cpu_reset_real();
  485 }
  486 #endif
  487 
  488 void
  489 cpu_reset()
  490 {
  491 #ifdef SMP
  492         if (smp_active == 0) {
  493                 cpu_reset_real();
  494                 /* NOTREACHED */
  495         } else {
  496 
  497                 u_int map;
  498                 int cnt;
  499                 printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
  500 
  501                 map = PCPU_GET(other_cpus) & ~ stopped_cpus;
  502 
  503                 if (map != 0) {
  504                         printf("cpu_reset: Stopping other CPUs\n");
  505                         stop_cpus(map);         /* Stop all other CPUs */
  506                 }
  507 
  508                 if (PCPU_GET(cpuid) == 0) {
  509                         DELAY(1000000);
  510                         cpu_reset_real();
  511                         /* NOTREACHED */
  512                 } else {
  513                         /* We are not BSP (CPU #0) */
  514 
  515                         cpu_reset_proxyid = PCPU_GET(cpuid);
  516                         cpustop_restartfunc = cpu_reset_proxy;
  517                         cpu_reset_proxy_active = 0;
  518                         printf("cpu_reset: Restarting BSP\n");
  519                         started_cpus = (1<<0);          /* Restart CPU #0 */
  520 
  521                         cnt = 0;
  522                         while (cpu_reset_proxy_active == 0 && cnt < 10000000)
  523                                 cnt++;  /* Wait for BSP to announce restart */
  524                         if (cpu_reset_proxy_active == 0)
  525                                 printf("cpu_reset: Failed to restart BSP\n");
  526                         enable_intr();
  527                         cpu_reset_proxy_active = 2;
  528 
  529                         while (1);
  530                         /* NOTREACHED */
  531                 }
  532         }
  533 #else
  534         cpu_reset_real();
  535 #endif
  536 }
  537 
  538 static void
  539 cpu_reset_real()
  540 {
  541 
  542 #ifdef PC98
  543         /*
  544          * Attempt to do a CPU reset via CPU reset port.
  545          */
  546         disable_intr();
  547         if ((inb(0x35) & 0xa0) != 0xa0) {
  548                 outb(0x37, 0x0f);               /* SHUT0 = 0. */
  549                 outb(0x37, 0x0b);               /* SHUT1 = 0. */
  550         }
  551         outb(0xf0, 0x00);               /* Reset. */
  552 #else
  553         /*
  554          * Attempt to do a CPU reset via the keyboard controller,
  555          * do not turn of the GateA20, as any machine that fails
  556          * to do the reset here would then end up in no man's land.
  557          */
  558 
  559 #if !defined(BROKEN_KEYBOARD_RESET)
  560         outb(IO_KBD + 4, 0xFE);
  561         DELAY(500000);  /* wait 0.5 sec to see if that did it */
  562         printf("Keyboard reset did not work, attempting CPU shutdown\n");
  563         DELAY(1000000); /* wait 1 sec for printf to complete */
  564 #endif
  565 #endif /* PC98 */
  566         /* force a shutdown by unmapping entire address space ! */
  567         bzero((caddr_t)PTD, NBPTD);
  568 
  569         /* "good night, sweet prince .... <THUNK!>" */
  570         invltlb();
  571         /* NOTREACHED */
  572         while(1);
  573 }
  574 
  575 /*
  576  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
  577  */
  578 static void
  579 sf_buf_init(void *arg)
  580 {
  581         struct sf_buf *sf_bufs;
  582         vm_offset_t sf_base;
  583         int i;
  584 
  585         sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
  586         LIST_INIT(&sf_buf_freelist);
  587         sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
  588         sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
  589             M_NOWAIT | M_ZERO);
  590         for (i = 0; i < nsfbufs; i++) {
  591                 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
  592                 LIST_INSERT_HEAD(&sf_buf_freelist, &sf_bufs[i], list_entry);
  593         }
  594         sf_buf_alloc_want = 0;
  595         mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
  596 }
  597 
  598 /*
  599  * Get an sf_buf from the freelist. Will block if none are available.
  600  */
  601 struct sf_buf *
  602 sf_buf_alloc(struct vm_page *m)
  603 {
  604         struct sf_head *hash_list;
  605         struct sf_buf *sf;
  606         int error;
  607 
  608         hash_list = &sf_buf_active[SF_BUF_HASH(m)];
  609         mtx_lock(&sf_buf_lock);
  610         LIST_FOREACH(sf, hash_list, list_entry) {
  611                 if (sf->m == m) {
  612                         sf->ref_count++;
  613                         goto done;
  614                 }
  615         }
  616         while ((sf = LIST_FIRST(&sf_buf_freelist)) == NULL) {
  617                 sf_buf_alloc_want++;
  618                 error = msleep(&sf_buf_freelist, &sf_buf_lock, PVM|PCATCH,
  619                     "sfbufa", 0);
  620                 sf_buf_alloc_want--;
  621 
  622                 /*
  623                  * If we got a signal, don't risk going back to sleep. 
  624                  */
  625                 if (error)
  626                         goto done;
  627         }
  628         LIST_REMOVE(sf, list_entry);
  629         LIST_INSERT_HEAD(hash_list, sf, list_entry);
  630         sf->ref_count = 1;
  631         sf->m = m;
  632         pmap_qenter(sf->kva, &sf->m, 1);
  633 done:
  634         mtx_unlock(&sf_buf_lock);
  635         return (sf);
  636 }
  637 
  638 /*
  639  * Detatch mapped page and release resources back to the system.
  640  */
  641 void
  642 sf_buf_free(void *addr, void *args)
  643 {
  644         struct sf_buf *sf;
  645         struct vm_page *m;
  646 
  647         sf = args;
  648         mtx_lock(&sf_buf_lock);
  649         m = sf->m;
  650         sf->ref_count--;
  651         if (sf->ref_count == 0) {
  652                 pmap_qremove((vm_offset_t)addr, 1);
  653                 sf->m = NULL;
  654                 LIST_REMOVE(sf, list_entry);
  655                 LIST_INSERT_HEAD(&sf_buf_freelist, sf, list_entry);
  656                 if (sf_buf_alloc_want > 0)
  657                         wakeup_one(&sf_buf_freelist);
  658         }
  659         mtx_unlock(&sf_buf_lock);
  660 
  661         vm_page_lock_queues();
  662         vm_page_unwire(m, 0);
  663         /*
  664          * Check for the object going away on us. This can
  665          * happen since we don't hold a reference to it.
  666          * If so, we're responsible for freeing the page.
  667          */
  668         if (m->wire_count == 0 && m->object == NULL)
  669                 vm_page_free(m);
  670         vm_page_unlock_queues();
  671 }
  672 
  673 /*
  674  * Software interrupt handler for queued VM system processing.
  675  */   
  676 void  
  677 swi_vm(void *dummy) 
  678 {     
  679         if (busdma_swi_pending != 0)
  680                 busdma_swi();
  681 }
  682 
  683 /*
  684  * Tell whether this address is in some physical memory region.
  685  * Currently used by the kernel coredump code in order to avoid
  686  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
  687  * or other unpredictable behaviour.
  688  */
  689 
  690 int
  691 is_physical_memory(addr)
  692         vm_offset_t addr;
  693 {
  694 
  695 #ifdef DEV_ISA
  696         /* The ISA ``memory hole''. */
  697         if (addr >= 0xa0000 && addr < 0x100000)
  698                 return 0;
  699 #endif
  700 
  701         /*
  702          * stuff other tests for known memory-mapped devices (PCI?)
  703          * here
  704          */
  705 
  706         return 1;
  707 }

Cache object: 98b11f35f88ba299e7a9d9d8b6e95e6e


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