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

Cache object: c5acb6abcf28d47d20d78e2084b7a641


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