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/sparc64/sparc64/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) 2001 Jake Burkholder.
    3  * Copyright (c) 1992 Terrence R. Lambert.
    4  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to Berkeley by
    8  * William Jolitz.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      from: @(#)machdep.c     7.4 (Berkeley) 6/3/91
   35  *      from: FreeBSD: src/sys/i386/i386/machdep.c,v 1.477 2001/08/27
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD$");
   40 
   41 #include "opt_compat.h"
   42 #include "opt_ddb.h"
   43 #include "opt_kstack_pages.h"
   44 #include "opt_msgbuf.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/malloc.h>
   48 #include <sys/proc.h>
   49 #include <sys/systm.h>
   50 #include <sys/bio.h>
   51 #include <sys/buf.h>
   52 #include <sys/bus.h>
   53 #include <sys/cpu.h>
   54 #include <sys/cons.h>
   55 #include <sys/eventhandler.h>
   56 #include <sys/exec.h>
   57 #include <sys/imgact.h>
   58 #include <sys/interrupt.h>
   59 #include <sys/kdb.h>
   60 #include <sys/kernel.h>
   61 #include <sys/ktr.h>
   62 #include <sys/linker.h>
   63 #include <sys/lock.h>
   64 #include <sys/msgbuf.h>
   65 #include <sys/mutex.h>
   66 #include <sys/pcpu.h>
   67 #include <sys/ptrace.h>
   68 #include <sys/reboot.h>
   69 #include <sys/signalvar.h>
   70 #include <sys/smp.h>
   71 #include <sys/sysent.h>
   72 #include <sys/sysproto.h>
   73 #include <sys/timetc.h>
   74 #include <sys/ucontext.h>
   75 
   76 #include <dev/ofw/openfirm.h>
   77 
   78 #include <vm/vm.h>
   79 #include <vm/vm_extern.h>
   80 #include <vm/vm_kern.h>
   81 #include <vm/vm_page.h>
   82 #include <vm/vm_map.h>
   83 #include <vm/vm_object.h>
   84 #include <vm/vm_pager.h>
   85 #include <vm/vm_param.h>
   86 
   87 #include <ddb/ddb.h>
   88 
   89 #include <machine/bus.h>
   90 #include <machine/cache.h>
   91 #include <machine/clock.h>
   92 #include <machine/cpu.h>
   93 #include <machine/fp.h>
   94 #include <machine/fsr.h>
   95 #include <machine/intr_machdep.h>
   96 #include <machine/md_var.h>
   97 #include <machine/metadata.h>
   98 #include <machine/ofw_machdep.h>
   99 #include <machine/ofw_mem.h>
  100 #include <machine/pcb.h>
  101 #include <machine/pmap.h>
  102 #include <machine/pstate.h>
  103 #include <machine/reg.h>
  104 #include <machine/sigframe.h>
  105 #include <machine/smp.h>
  106 #include <machine/tick.h>
  107 #include <machine/tlb.h>
  108 #include <machine/tstate.h>
  109 #include <machine/upa.h>
  110 #include <machine/ver.h>
  111 
  112 typedef int ofw_vec_t(void *);
  113 
  114 #ifdef DDB
  115 extern vm_offset_t ksym_start, ksym_end;
  116 #endif
  117 
  118 struct tlb_entry *kernel_tlbs;
  119 int kernel_tlb_slots;
  120 
  121 int cold = 1;
  122 long Maxmem;
  123 long realmem;
  124 
  125 char pcpu0[PCPU_PAGES * PAGE_SIZE];
  126 struct trapframe frame0;
  127 
  128 vm_offset_t kstack0;
  129 vm_paddr_t kstack0_phys;
  130 
  131 struct kva_md_info kmi;
  132 
  133 u_long ofw_vec;
  134 u_long ofw_tba;
  135 
  136 /*
  137  * Note: timer quality for CPU's is set low to try and prevent them from
  138  * being chosen as the primary timecounter.  The CPU counters are not
  139  * synchronized among the CPU's so in MP machines this causes problems
  140  * when calculating the time.  With this value the CPU's should only be
  141  * chosen as the primary timecounter as a last resort.
  142  */
  143 
  144 #define UP_TICK_QUALITY 1000
  145 #define MP_TICK_QUALITY -100
  146 static struct timecounter tick_tc;
  147 
  148 char sparc64_model[32];
  149 
  150 static int cpu_use_vis = 1;
  151 
  152 cpu_block_copy_t *cpu_block_copy;
  153 cpu_block_zero_t *cpu_block_zero;
  154 
  155 static timecounter_get_t tick_get_timecount;
  156 void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3,
  157                   ofw_vec_t *vec);
  158 void sparc64_shutdown_final(void *dummy, int howto);
  159 
  160 static void cpu_startup(void *);
  161 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
  162 
  163 CTASSERT((1 << INT_SHIFT) == sizeof(int));
  164 CTASSERT((1 << PTR_SHIFT) == sizeof(char *));
  165 
  166 CTASSERT(sizeof(struct reg) == 256);
  167 CTASSERT(sizeof(struct fpreg) == 272);
  168 CTASSERT(sizeof(struct __mcontext) == 512);
  169 
  170 CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0);
  171 CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0);
  172 CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0);
  173 CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8));
  174 
  175 CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2));
  176 
  177 static void
  178 cpu_startup(void *arg)
  179 {
  180         vm_paddr_t physsz;
  181         int i;
  182 
  183         tick_tc.tc_get_timecount = tick_get_timecount;
  184         tick_tc.tc_poll_pps = NULL;
  185         tick_tc.tc_counter_mask = ~0u;
  186         tick_tc.tc_frequency = tick_freq;
  187         tick_tc.tc_name = "tick";
  188         tick_tc.tc_quality = UP_TICK_QUALITY;
  189 #ifdef SMP
  190         /*
  191          * We do not know if each CPU's tick counter is synchronized.
  192          */
  193         if (cpu_mp_probe())
  194                 tick_tc.tc_quality = MP_TICK_QUALITY;
  195 #endif
  196 
  197         tc_init(&tick_tc);
  198 
  199         physsz = 0;
  200         for (i = 0; i < sparc64_nmemreg; i++)
  201                 physsz += sparc64_memreg[i].mr_size;
  202         printf("real memory  = %lu (%lu MB)\n", physsz,
  203             physsz / (1024 * 1024));
  204         realmem = (long)physsz;
  205 
  206         vm_ksubmap_init(&kmi);
  207 
  208         bufinit();
  209         vm_pager_bufferinit();
  210 
  211         EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
  212             SHUTDOWN_PRI_LAST);
  213 
  214         printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
  215             cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
  216 
  217         if (bootverbose)
  218                 printf("machine: %s\n", sparc64_model);
  219 
  220         cpu_identify(rdpr(ver), tick_freq, PCPU_GET(cpuid));
  221 }
  222 
  223 void
  224 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
  225 {
  226         struct intr_request *ir;
  227         int i;
  228 
  229         pcpu->pc_irtail = &pcpu->pc_irhead;
  230         for (i = 0; i < IR_FREE; i++) {
  231                 ir = &pcpu->pc_irpool[i];
  232                 ir->ir_next = pcpu->pc_irfree;
  233                 pcpu->pc_irfree = ir;
  234         }
  235 }
  236 
  237 unsigned
  238 tick_get_timecount(struct timecounter *tc)
  239 {
  240         return ((unsigned)rd(tick));
  241 }
  242 
  243 void
  244 sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
  245 {
  246         phandle_t child;
  247         phandle_t root;
  248         struct pcpu *pc;
  249         vm_offset_t end;
  250         caddr_t kmdp;
  251         u_int clock;
  252         char *env;
  253         char type[8];
  254 
  255         end = 0;
  256         kmdp = NULL;
  257 
  258         /*
  259          * Find out what kind of cpu we have first, for anything that changes
  260          * behaviour.
  261          */
  262         cpu_impl = VER_IMPL(rdpr(ver));
  263 
  264         /*
  265          * Initialize Open Firmware (needed for console).
  266          */
  267         OF_init(vec);
  268 
  269         /*
  270          * Parse metadata if present and fetch parameters.  Must be before the
  271          * console is inited so cninit gets the right value of boothowto.
  272          */
  273         if (mdp != NULL) {
  274                 preload_metadata = mdp;
  275                 kmdp = preload_search_by_type("elf kernel");
  276                 if (kmdp != NULL) {
  277                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
  278                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
  279                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
  280                         kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
  281                             int);
  282                         kernel_tlbs = (void *)preload_search_info(kmdp,
  283                             MODINFO_METADATA | MODINFOMD_DTLB);
  284                 }
  285         }
  286 
  287         init_param1();
  288 
  289         root = OF_peer(0);
  290         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
  291                 OF_getprop(child, "device_type", type, sizeof(type));
  292                 if (strcmp(type, "cpu") == 0)
  293                         break;
  294         }
  295 
  296         OF_getprop(child, "clock-frequency", &clock, sizeof(clock));
  297         tick_init(clock);
  298 
  299         /*
  300          * Initialize the console before printing anything.
  301          */
  302         cninit();
  303 
  304         /*
  305          * Panic is there is no metadata.  Most likely the kernel was booted
  306          * directly, instead of through loader(8).
  307          */
  308         if (mdp == NULL || kmdp == NULL) {
  309                 printf("sparc64_init: no loader metadata.\n"
  310                        "This probably means you are not using loader(8).\n");
  311                 panic("sparc64_init");
  312         }
  313 
  314         /*
  315          * Sanity check the kernel end, which is important.
  316          */
  317         if (end == 0) {
  318                 printf("sparc64_init: warning, kernel end not specified.\n"
  319                        "Attempting to continue anyway.\n");
  320                 end = (vm_offset_t)_end;
  321         }
  322 
  323         cache_init(child);
  324 
  325         getenv_int("machdep.use_vis", &cpu_use_vis);
  326         if (cpu_use_vis) {
  327                 cpu_block_copy = spitfire_block_copy;
  328                 cpu_block_zero = spitfire_block_zero;
  329         } else {
  330                 cpu_block_copy = bcopy;
  331                 cpu_block_zero = bzero;
  332         }
  333 
  334 #ifdef SMP
  335         mp_tramp = mp_tramp_alloc();
  336 #endif
  337 
  338         /*
  339          * Initialize virtual memory and calculate physmem.
  340          */
  341         pmap_bootstrap(end);
  342 
  343         /*
  344          * Initialize tunables.
  345          */
  346         init_param2(physmem);
  347         env = getenv("kernelname");
  348         if (env != NULL) {
  349                 strlcpy(kernelname, env, sizeof(kernelname));
  350                 freeenv(env);
  351         }
  352 
  353         /*
  354          * Initialize the interrupt tables.
  355          */
  356         intr_init1();
  357 
  358         /*
  359          * Initialize proc0 stuff (p_contested needs to be done early).
  360          */
  361         proc_linkup(&proc0, &ksegrp0, &thread0);
  362         proc0.p_md.md_sigtramp = NULL;
  363         proc0.p_md.md_utrap = NULL;
  364         thread0.td_kstack = kstack0;
  365         thread0.td_pcb = (struct pcb *)
  366             (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
  367         frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
  368         thread0.td_frame = &frame0;
  369 
  370         /*
  371          * Prime our per-cpu data page for use.  Note, we are using it for our
  372          * stack, so don't pass the real size (PAGE_SIZE) to pcpu_init or
  373          * it'll zero it out from under us.
  374          */
  375         pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
  376         pcpu_init(pc, 0, sizeof(struct pcpu));
  377         pc->pc_curthread = &thread0;
  378         pc->pc_curpcb = thread0.td_pcb;
  379         pc->pc_mid = UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG));
  380         pc->pc_addr = (vm_offset_t)pcpu0;
  381         pc->pc_node = child;
  382         pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
  383         pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
  384         pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
  385 
  386         /*
  387          * Initialize global registers.
  388          */
  389         cpu_setregs(pc);
  390 
  391         /*
  392          * Initialize the message buffer (after setting trap table).
  393          */
  394         msgbufinit(msgbufp, MSGBUF_SIZE);
  395 
  396         mutex_init();
  397         intr_init2();
  398 
  399         /*
  400          * Finish pmap initialization now that we're ready for mutexes.
  401          */
  402         PMAP_LOCK_INIT(kernel_pmap);
  403 
  404         OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
  405 
  406         kdb_init();
  407 
  408 #ifdef KDB
  409         if (boothowto & RB_KDB)
  410                 kdb_enter("Boot flags requested debugger");
  411 #endif
  412 }
  413 
  414 void
  415 set_openfirm_callback(ofw_vec_t *vec)
  416 {
  417         ofw_tba = rdpr(tba);
  418         ofw_vec = (u_long)vec;
  419 }
  420 
  421 void
  422 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
  423 {
  424         struct trapframe *tf;
  425         struct sigframe *sfp;
  426         struct sigacts *psp;
  427         struct sigframe sf;
  428         struct thread *td;
  429         struct frame *fp;
  430         struct proc *p;
  431         int oonstack;
  432         u_long sp;
  433 
  434         oonstack = 0;
  435         td = curthread;
  436         p = td->td_proc;
  437         PROC_LOCK_ASSERT(p, MA_OWNED);
  438         psp = p->p_sigacts;
  439         mtx_assert(&psp->ps_mtx, MA_OWNED);
  440         tf = td->td_frame;
  441         sp = tf->tf_sp + SPOFF;
  442         oonstack = sigonstack(sp);
  443 
  444         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
  445             catcher, sig);
  446 
  447         /* Make sure we have a signal trampoline to return to. */
  448         if (p->p_md.md_sigtramp == NULL) {
  449                 /*
  450                  * No signal tramoline... kill the process.
  451                  */
  452                 CTR0(KTR_SIG, "sendsig: no sigtramp");
  453                 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
  454                 sigexit(td, sig);
  455                 /* NOTREACHED */
  456         }
  457 
  458         /* Save user context. */
  459         bzero(&sf, sizeof(sf));
  460         get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
  461         sf.sf_uc.uc_sigmask = *mask;
  462         sf.sf_uc.uc_stack = td->td_sigstk;
  463         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
  464             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
  465 
  466         /* Allocate and validate space for the signal handler context. */
  467         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
  468             SIGISMEMBER(psp->ps_sigonstack, sig)) {
  469                 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
  470                     td->td_sigstk.ss_size - sizeof(struct sigframe));
  471         } else
  472                 sfp = (struct sigframe *)sp - 1;
  473         mtx_unlock(&psp->ps_mtx);
  474         PROC_UNLOCK(p);
  475 
  476         fp = (struct frame *)sfp - 1;
  477 
  478         /* Translate the signal if appropriate. */
  479         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
  480                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
  481 
  482         /* Build the argument list for the signal handler. */
  483         tf->tf_out[0] = sig;
  484         tf->tf_out[1] = (register_t)&sfp->sf_si;
  485         tf->tf_out[2] = (register_t)&sfp->sf_uc;
  486         tf->tf_out[4] = (register_t)catcher;
  487         /* Fill siginfo structure. */
  488         sf.sf_si.si_signo = sig;
  489         sf.sf_si.si_code = code;
  490         sf.sf_si.si_addr = (void *)tf->tf_sfar;
  491 
  492         /* Copy the sigframe out to the user's stack. */
  493         if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
  494             suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
  495                 /*
  496                  * Something is wrong with the stack pointer.
  497                  * ...Kill the process.
  498                  */
  499                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
  500                 PROC_LOCK(p);
  501                 sigexit(td, SIGILL);
  502                 /* NOTREACHED */
  503         }
  504 
  505         tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
  506         tf->tf_tnpc = tf->tf_tpc + 4;
  507         tf->tf_sp = (u_long)fp - SPOFF;
  508 
  509         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
  510             tf->tf_sp);
  511 
  512         PROC_LOCK(p);
  513         mtx_lock(&psp->ps_mtx);
  514 }
  515 
  516 /*
  517  * Build siginfo_t for SA thread
  518  */
  519 void
  520 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
  521 {
  522         struct proc *p;
  523         struct thread *td;
  524 
  525         td = curthread;
  526         p = td->td_proc;
  527         PROC_LOCK_ASSERT(p, MA_OWNED);
  528 
  529         bzero(si, sizeof(*si));
  530         si->si_signo = sig;
  531         si->si_code = code;
  532         /* XXXKSE fill other fields */
  533 }
  534 
  535 #ifndef _SYS_SYSPROTO_H_
  536 struct sigreturn_args {
  537         ucontext_t *ucp;
  538 };
  539 #endif
  540 
  541 /*
  542  * MPSAFE
  543  */
  544 int
  545 sigreturn(struct thread *td, struct sigreturn_args *uap)
  546 {
  547         struct proc *p;
  548         mcontext_t *mc;
  549         ucontext_t uc;
  550         int error;
  551 
  552         p = td->td_proc;
  553         if (rwindow_save(td)) {
  554                 PROC_LOCK(p);
  555                 sigexit(td, SIGILL);
  556         }
  557 
  558         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
  559         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
  560                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
  561                 return (EFAULT);
  562         }
  563 
  564         mc = &uc.uc_mcontext;
  565         error = set_mcontext(td, mc);
  566         if (error != 0)
  567                 return (error);
  568 
  569         PROC_LOCK(p);
  570         td->td_sigmask = uc.uc_sigmask;
  571         SIG_CANTMASK(td->td_sigmask);
  572         signotify(td);
  573         PROC_UNLOCK(p);
  574 
  575         CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
  576             td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
  577         return (EJUSTRETURN);
  578 }
  579 
  580 #ifdef COMPAT_FREEBSD4
  581 int
  582 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
  583 {
  584 
  585         return sigreturn(td, (struct sigreturn_args *)uap);
  586 }
  587 #endif
  588 
  589 /*
  590  * Construct a PCB from a trapframe. This is called from kdb_trap() where
  591  * we want to start a backtrace from the function that caused us to enter
  592  * the debugger. We have the context in the trapframe, but base the trace
  593  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
  594  * enough for a backtrace.
  595  */
  596 void
  597 makectx(struct trapframe *tf, struct pcb *pcb)
  598 {
  599 
  600         pcb->pcb_pc = tf->tf_tpc;
  601         pcb->pcb_sp = tf->tf_sp;
  602 }
  603 
  604 int
  605 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
  606 {
  607         struct trapframe *tf;
  608         struct pcb *pcb;
  609 
  610         tf = td->td_frame;
  611         pcb = td->td_pcb;
  612         bcopy(tf, mc, sizeof(*tf));
  613         if (flags & GET_MC_CLEAR_RET) {
  614                 mc->mc_out[0] = 0;
  615                 mc->mc_out[1] = 0;
  616         }
  617         mc->mc_flags = _MC_VERSION;
  618         critical_enter();
  619         if ((tf->tf_fprs & FPRS_FEF) != 0) {
  620                 savefpctx(pcb->pcb_ufp);
  621                 tf->tf_fprs &= ~FPRS_FEF;
  622                 pcb->pcb_flags |= PCB_FEF;
  623         }
  624         if ((pcb->pcb_flags & PCB_FEF) != 0) {
  625                 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
  626                 mc->mc_fprs |= FPRS_FEF;
  627         }
  628         critical_exit();
  629         return (0);
  630 }
  631 
  632 int
  633 set_mcontext(struct thread *td, const mcontext_t *mc)
  634 {
  635         struct trapframe *tf;
  636         struct pcb *pcb;
  637         uint64_t wstate;
  638 
  639         if (!TSTATE_SECURE(mc->mc_tstate) ||
  640             (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
  641                 return (EINVAL);
  642         tf = td->td_frame;
  643         pcb = td->td_pcb;
  644         /* Make sure the windows are spilled first. */
  645         flushw();
  646         wstate = tf->tf_wstate;
  647         bcopy(mc, tf, sizeof(*tf));
  648         tf->tf_wstate = wstate;
  649         if ((mc->mc_fprs & FPRS_FEF) != 0) {
  650                 tf->tf_fprs = 0;
  651                 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
  652                 pcb->pcb_flags |= PCB_FEF;
  653         }
  654         return (0);
  655 }
  656 
  657 /*
  658  * Exit the kernel and execute a firmware call that will not return, as
  659  * specified by the arguments.
  660  */
  661 void
  662 cpu_shutdown(void *args)
  663 {
  664 
  665 #ifdef SMP
  666         cpu_mp_shutdown();
  667 #endif
  668         openfirmware_exit(args);
  669 }
  670 
  671 /* Get current clock frequency for the given cpu id. */
  672 int
  673 cpu_est_clockrate(int cpu_id, uint64_t *rate)
  674 {
  675 
  676         return (ENXIO);
  677 }
  678 
  679 /*
  680  * Duplicate OF_exit() with a different firmware call function that restores
  681  * the trap table, otherwise a RED state exception is triggered in at least
  682  * some firmware versions.
  683  */
  684 void
  685 cpu_halt(void)
  686 {
  687         static struct {
  688                 cell_t name;
  689                 cell_t nargs;
  690                 cell_t nreturns;
  691         } args = {
  692                 (cell_t)"exit",
  693                 0,
  694                 0
  695         };
  696 
  697         cpu_shutdown(&args);
  698 }
  699 
  700 void
  701 sparc64_shutdown_final(void *dummy, int howto)
  702 {
  703         static struct {
  704                 cell_t name;
  705                 cell_t nargs;
  706                 cell_t nreturns;
  707         } args = {
  708                 (cell_t)"SUNW,power-off",
  709                 0,
  710                 0
  711         };
  712 
  713         /* Turn the power off? */
  714         if ((howto & RB_POWEROFF) != 0)
  715                 cpu_shutdown(&args);
  716         /* In case of halt, return to the firmware */
  717         if ((howto & RB_HALT) != 0)
  718                 cpu_halt();
  719 }
  720 
  721 void
  722 cpu_idle(void)
  723 {
  724         /* Insert code to halt (until next interrupt) for the idle loop */
  725 }
  726 
  727 int
  728 ptrace_set_pc(struct thread *td, u_long addr)
  729 {
  730 
  731         td->td_frame->tf_tpc = addr;
  732         td->td_frame->tf_tnpc = addr + 4;
  733         return (0);
  734 }
  735 
  736 int
  737 ptrace_single_step(struct thread *td)
  738 {
  739         /* TODO; */
  740         return (0);
  741 }
  742 
  743 int
  744 ptrace_clear_single_step(struct thread *td)
  745 {
  746         /* TODO; */
  747         return (0);
  748 }
  749 
  750 void
  751 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
  752 {
  753         struct trapframe *tf;
  754         struct pcb *pcb;
  755         struct proc *p;
  756         u_long sp;
  757 
  758         /* XXX no cpu_exec */
  759         p = td->td_proc;
  760         p->p_md.md_sigtramp = NULL;
  761         if (p->p_md.md_utrap != NULL) {
  762                 utrap_free(p->p_md.md_utrap);
  763                 p->p_md.md_utrap = NULL;
  764         }
  765 
  766         pcb = td->td_pcb;
  767         tf = td->td_frame;
  768         sp = rounddown(stack, 16);
  769         bzero(pcb, sizeof(*pcb));
  770         bzero(tf, sizeof(*tf));
  771         tf->tf_out[0] = stack;
  772         tf->tf_out[3] = p->p_sysent->sv_psstrings;
  773         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
  774         tf->tf_tnpc = entry + 4;
  775         tf->tf_tpc = entry;
  776         tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
  777 
  778         td->td_retval[0] = tf->tf_out[0];
  779         td->td_retval[1] = tf->tf_out[1];
  780 }
  781 
  782 int
  783 fill_regs(struct thread *td, struct reg *regs)
  784 {
  785 
  786         bcopy(td->td_frame, regs, sizeof(*regs));
  787         return (0);
  788 }
  789 
  790 int
  791 set_regs(struct thread *td, struct reg *regs)
  792 {
  793         struct trapframe *tf;
  794 
  795         if (!TSTATE_SECURE(regs->r_tstate))
  796                 return (EINVAL);
  797         tf = td->td_frame;
  798         regs->r_wstate = tf->tf_wstate;
  799         bcopy(regs, tf, sizeof(*regs));
  800         return (0);
  801 }
  802 
  803 int
  804 fill_dbregs(struct thread *td, struct dbreg *dbregs)
  805 {
  806 
  807         return (ENOSYS);
  808 }
  809 
  810 int
  811 set_dbregs(struct thread *td, struct dbreg *dbregs)
  812 {
  813 
  814         return (ENOSYS);
  815 }
  816 
  817 int
  818 fill_fpregs(struct thread *td, struct fpreg *fpregs)
  819 {
  820         struct trapframe *tf;
  821         struct pcb *pcb;
  822 
  823         pcb = td->td_pcb;
  824         tf = td->td_frame;
  825         bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
  826         fpregs->fr_fsr = tf->tf_fsr;
  827         fpregs->fr_gsr = tf->tf_gsr;
  828         return (0);
  829 }
  830 
  831 int
  832 set_fpregs(struct thread *td, struct fpreg *fpregs)
  833 {
  834         struct trapframe *tf;
  835         struct pcb *pcb;
  836 
  837         pcb = td->td_pcb;
  838         tf = td->td_frame;
  839         tf->tf_fprs &= ~FPRS_FEF;
  840         bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
  841         tf->tf_fsr = fpregs->fr_fsr;
  842         tf->tf_gsr = fpregs->fr_gsr;
  843         return (0);
  844 }
  845 
  846 struct md_utrap *
  847 utrap_alloc(void)
  848 {
  849         struct md_utrap *ut;
  850 
  851         ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
  852         ut->ut_refcnt = 1;
  853         return (ut);
  854 }
  855 
  856 void
  857 utrap_free(struct md_utrap *ut)
  858 {
  859         int refcnt;
  860 
  861         if (ut == NULL)
  862                 return;
  863         mtx_pool_lock(mtxpool_sleep, ut);
  864         ut->ut_refcnt--;
  865         refcnt = ut->ut_refcnt;
  866         mtx_pool_unlock(mtxpool_sleep, ut);
  867         if (refcnt == 0)
  868                 free(ut, M_SUBPROC);
  869 }
  870 
  871 struct md_utrap *
  872 utrap_hold(struct md_utrap *ut)
  873 {
  874 
  875         if (ut == NULL)
  876                 return (NULL);
  877         mtx_pool_lock(mtxpool_sleep, ut);
  878         ut->ut_refcnt++;
  879         mtx_pool_unlock(mtxpool_sleep, ut);
  880         return (ut);
  881 }

Cache object: 56f27f4792b1b8beebde6aba86061ba1


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