]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/sparc64/sparc64/machdep.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / sparc64 / sparc64 / machdep.c
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 / PAGE_SIZE;
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 void
238 spinlock_enter(void)
239 {
240         struct thread *td;
241         register_t pil;
242
243         td = curthread;
244         if (td->td_md.md_spinlock_count == 0) {
245                 pil = rdpr(pil);
246                 wrpr(pil, 0, PIL_TICK);
247                 td->td_md.md_saved_pil = pil;
248         }
249         td->td_md.md_spinlock_count++;
250         critical_enter();
251 }
252
253 void
254 spinlock_exit(void)
255 {
256         struct thread *td;
257
258         td = curthread;
259         critical_exit();
260         td->td_md.md_spinlock_count--;
261         if (td->td_md.md_spinlock_count == 0)
262                 wrpr(pil, td->td_md.md_saved_pil, 0);
263 }
264
265 unsigned
266 tick_get_timecount(struct timecounter *tc)
267 {
268         return ((unsigned)rd(tick));
269 }
270
271 void
272 sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
273 {
274         char type[8];
275         char *env;
276         struct pcpu *pc;
277         vm_offset_t end;
278         caddr_t kmdp;
279         phandle_t child;
280         phandle_t root;
281         u_int clock;
282
283         end = 0;
284         kmdp = NULL;
285
286         /*
287          * Find out what kind of CPU we have first, for anything that changes
288          * behaviour.
289          */
290         cpu_impl = VER_IMPL(rdpr(ver));
291
292         /*
293          * Initialize Open Firmware (needed for console).
294          */
295         OF_init(vec);
296
297         /*
298          * Parse metadata if present and fetch parameters.  Must be before the
299          * console is inited so cninit gets the right value of boothowto.
300          */
301         if (mdp != NULL) {
302                 preload_metadata = mdp;
303                 kmdp = preload_search_by_type("elf kernel");
304                 if (kmdp != NULL) {
305                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
306                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
307                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
308                         kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
309                             int);
310                         kernel_tlbs = (void *)preload_search_info(kmdp,
311                             MODINFO_METADATA | MODINFOMD_DTLB);
312                 }
313         }
314
315         init_param1();
316
317         root = OF_peer(0);
318         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
319                 OF_getprop(child, "device_type", type, sizeof(type));
320                 if (strcmp(type, "cpu") == 0)
321                         break;
322         }
323
324         /*
325          * Initialize the tick counter.  Must be before the console is inited
326          * in order to provide the low-level console drivers with a working
327          * DELAY().
328          */
329         OF_getprop(child, "clock-frequency", &clock, sizeof(clock));
330         tick_init(clock);
331
332         /*
333          * Initialize the console before printing anything.
334          */
335         cninit();
336
337         /*
338          * Panic if there is no metadata.  Most likely the kernel was booted
339          * directly, instead of through loader(8).
340          */
341         if (mdp == NULL || kmdp == NULL) {
342                 printf("sparc64_init: no loader metadata.\n"
343                     "This probably means you are not using loader(8).\n");
344                 panic("sparc64_init");
345         }
346
347         /*
348          * Sanity check the kernel end, which is important.
349          */
350         if (end == 0) {
351                 printf("sparc64_init: warning, kernel end not specified.\n"
352                     "Attempting to continue anyway.\n");
353                 end = (vm_offset_t)_end;
354         }
355
356         cache_init(child);
357
358         getenv_int("machdep.use_vis", &cpu_use_vis);
359         if (cpu_use_vis) {
360                 cpu_block_copy = spitfire_block_copy;
361                 cpu_block_zero = spitfire_block_zero;
362         } else {
363                 cpu_block_copy = bcopy;
364                 cpu_block_zero = bzero;
365         }
366
367 #ifdef SMP
368         mp_tramp = mp_tramp_alloc();
369 #endif
370
371         /*
372          * Initialize virtual memory and calculate physmem.
373          */
374         pmap_bootstrap(end);
375
376         /*
377          * Initialize tunables.
378          */
379         init_param2(physmem);
380         env = getenv("kernelname");
381         if (env != NULL) {
382                 strlcpy(kernelname, env, sizeof(kernelname));
383                 freeenv(env);
384         }
385
386         /*
387          * Initialize the interrupt tables.
388          */
389         intr_init1();
390
391         /*
392          * Initialize proc0 stuff (p_contested needs to be done early).
393          */
394         proc_linkup(&proc0, &ksegrp0, &thread0);
395         proc0.p_md.md_sigtramp = NULL;
396         proc0.p_md.md_utrap = NULL;
397         thread0.td_kstack = kstack0;
398         thread0.td_pcb = (struct pcb *)
399             (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
400         frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
401         thread0.td_frame = &frame0;
402
403         /*
404          * Prime our per-cpu data page for use.  Note, we are using it for our
405          * stack, so don't pass the real size (PAGE_SIZE) to pcpu_init or
406          * it'll zero it out from under us.
407          */
408         pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
409         pcpu_init(pc, 0, sizeof(struct pcpu));
410         pc->pc_curthread = &thread0;
411         pc->pc_curpcb = thread0.td_pcb;
412         pc->pc_mid = UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG));
413         pc->pc_addr = (vm_offset_t)pcpu0;
414         pc->pc_node = child;
415         pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
416         pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
417         pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
418
419         /*
420          * Initialize global registers.
421          */
422         cpu_setregs(pc);
423
424         /*
425          * Initialize the message buffer (after setting trap table).
426          */
427         msgbufinit(msgbufp, MSGBUF_SIZE);
428
429         mutex_init();
430         intr_init2();
431
432         /*
433          * Finish pmap initialization now that we're ready for mutexes.
434          */
435         PMAP_LOCK_INIT(kernel_pmap);
436
437         OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
438
439         kdb_init();
440
441 #ifdef KDB
442         if (boothowto & RB_KDB)
443                 kdb_enter("Boot flags requested debugger");
444 #endif
445 }
446
447 void
448 set_openfirm_callback(ofw_vec_t *vec)
449 {
450
451         ofw_tba = rdpr(tba);
452         ofw_vec = (u_long)vec;
453 }
454
455 void
456 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
457 {
458         struct trapframe *tf;
459         struct sigframe *sfp;
460         struct sigacts *psp;
461         struct sigframe sf;
462         struct thread *td;
463         struct frame *fp;
464         struct proc *p;
465         u_long sp;
466         int oonstack;
467         register_t addr;
468
469         oonstack = 0;
470         td = curthread;
471         p = td->td_proc;
472         PROC_LOCK_ASSERT(p, MA_OWNED);
473         psp = p->p_sigacts;
474         mtx_assert(&psp->ps_mtx, MA_OWNED);
475         tf = td->td_frame;
476         sp = tf->tf_sp + SPOFF;
477         oonstack = sigonstack(sp);
478
479         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
480             catcher, sig);
481
482         /* Make sure we have a signal trampoline to return to. */
483         if (p->p_md.md_sigtramp == NULL) {
484                 /*
485                  * No signal trampoline... kill the process.
486                  */
487                 CTR0(KTR_SIG, "sendsig: no sigtramp");
488                 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
489                 sigexit(td, sig);
490                 /* NOTREACHED */
491         }
492
493         /* Save user context. */
494         bzero(&sf, sizeof(sf));
495         get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
496         sf.sf_uc.uc_sigmask = *mask;
497         sf.sf_uc.uc_stack = td->td_sigstk;
498         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
499             ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
500
501         /* Allocate and validate space for the signal handler context. */
502         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
503             SIGISMEMBER(psp->ps_sigonstack, sig)) {
504                 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
505                     td->td_sigstk.ss_size - sizeof(struct sigframe));
506         } else
507                 sfp = (struct sigframe *)sp - 1;
508         mtx_unlock(&psp->ps_mtx);
509         PROC_UNLOCK(p);
510
511         fp = (struct frame *)sfp - 1;
512
513         /* Translate the signal if appropriate. */
514         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
515                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
516
517         /* Build the argument list for the signal handler. */
518         tf->tf_out[0] = sig;
519         tf->tf_out[2] = (register_t)&sfp->sf_uc;
520         tf->tf_out[4] = (register_t)catcher;
521         if (tf->tf_type == T_DATA_MISS || tf->tf_type == T_DATA_PROTECTION)
522                 addr = tf->tf_sfar;
523         else
524                 addr = tf->tf_tpc;
525         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
526                 /* Signal handler installed with SA_SIGINFO. */
527                 tf->tf_out[1] = (register_t)&sfp->sf_si;
528
529                 /* Fill in POSIX parts. */
530                 sf.sf_si.si_signo = sig; /* maybe a translated signal */
531                 sf.sf_si.si_code = code;
532                 sf.sf_si.si_addr = (void *)addr;
533         } else {
534                 /* Old FreeBSD-style arguments. */
535                 tf->tf_out[1] = code;
536                 tf->tf_out[3] = addr;
537         }
538
539         /* Copy the sigframe out to the user's stack. */
540         if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
541             suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
542                 /*
543                  * Something is wrong with the stack pointer.
544                  * ...Kill the process.
545                  */
546                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
547                 PROC_LOCK(p);
548                 sigexit(td, SIGILL);
549                 /* NOTREACHED */
550         }
551
552         tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
553         tf->tf_tnpc = tf->tf_tpc + 4;
554         tf->tf_sp = (u_long)fp - SPOFF;
555
556         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
557             tf->tf_sp);
558
559         PROC_LOCK(p);
560         mtx_lock(&psp->ps_mtx);
561 }
562
563 /*
564  * Build siginfo_t for SA thread
565  */
566 void
567 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
568 {
569         struct proc *p;
570         struct thread *td;
571         struct trapframe *tf;
572
573         td = curthread;
574         p = td->td_proc;
575         PROC_LOCK_ASSERT(p, MA_OWNED);
576         tf = td->td_frame;
577
578         bzero(si, sizeof(*si));
579         si->si_signo = sig;
580         si->si_code = code;
581         if (tf->tf_type == T_DATA_MISS || tf->tf_type == T_DATA_PROTECTION)
582                 si->si_addr = (void *)tf->tf_sfar;
583         else
584                 si->si_addr = (void *)tf->tf_tpc;
585         /* XXXKSE fill other fields */
586 }
587
588 #ifndef _SYS_SYSPROTO_H_
589 struct sigreturn_args {
590         ucontext_t *ucp;
591 };
592 #endif
593
594 /*
595  * MPSAFE
596  */
597 int
598 sigreturn(struct thread *td, struct sigreturn_args *uap)
599 {
600         struct proc *p;
601         mcontext_t *mc;
602         ucontext_t uc;
603         int error;
604
605         p = td->td_proc;
606         if (rwindow_save(td)) {
607                 PROC_LOCK(p);
608                 sigexit(td, SIGILL);
609         }
610
611         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
612         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
613                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
614                 return (EFAULT);
615         }
616
617         mc = &uc.uc_mcontext;
618         error = set_mcontext(td, mc);
619         if (error != 0)
620                 return (error);
621
622         PROC_LOCK(p);
623         td->td_sigmask = uc.uc_sigmask;
624         SIG_CANTMASK(td->td_sigmask);
625         signotify(td);
626         PROC_UNLOCK(p);
627
628         CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
629             td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
630         return (EJUSTRETURN);
631 }
632
633 #ifdef COMPAT_FREEBSD4
634 int
635 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
636 {
637
638         return sigreturn(td, (struct sigreturn_args *)uap);
639 }
640 #endif
641
642 /*
643  * Construct a PCB from a trapframe. This is called from kdb_trap() where
644  * we want to start a backtrace from the function that caused us to enter
645  * the debugger. We have the context in the trapframe, but base the trace
646  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
647  * enough for a backtrace.
648  */
649 void
650 makectx(struct trapframe *tf, struct pcb *pcb)
651 {
652
653         pcb->pcb_pc = tf->tf_tpc;
654         pcb->pcb_sp = tf->tf_sp;
655 }
656
657 int
658 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
659 {
660         struct trapframe *tf;
661         struct pcb *pcb;
662
663         tf = td->td_frame;
664         pcb = td->td_pcb;
665         bcopy(tf, mc, sizeof(*tf));
666         if (flags & GET_MC_CLEAR_RET) {
667                 mc->mc_out[0] = 0;
668                 mc->mc_out[1] = 0;
669         }
670         mc->mc_flags = _MC_VERSION;
671         critical_enter();
672         if ((tf->tf_fprs & FPRS_FEF) != 0) {
673                 savefpctx(pcb->pcb_ufp);
674                 tf->tf_fprs &= ~FPRS_FEF;
675                 pcb->pcb_flags |= PCB_FEF;
676         }
677         if ((pcb->pcb_flags & PCB_FEF) != 0) {
678                 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
679                 mc->mc_fprs |= FPRS_FEF;
680         }
681         critical_exit();
682         return (0);
683 }
684
685 int
686 set_mcontext(struct thread *td, const mcontext_t *mc)
687 {
688         struct trapframe *tf;
689         struct pcb *pcb;
690         uint64_t wstate;
691
692         if (!TSTATE_SECURE(mc->mc_tstate) ||
693             (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
694                 return (EINVAL);
695         tf = td->td_frame;
696         pcb = td->td_pcb;
697         /* Make sure the windows are spilled first. */
698         flushw();
699         wstate = tf->tf_wstate;
700         bcopy(mc, tf, sizeof(*tf));
701         tf->tf_wstate = wstate;
702         if ((mc->mc_fprs & FPRS_FEF) != 0) {
703                 tf->tf_fprs = 0;
704                 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
705                 pcb->pcb_flags |= PCB_FEF;
706         }
707         return (0);
708 }
709
710 /*
711  * Exit the kernel and execute a firmware call that will not return, as
712  * specified by the arguments.
713  */
714 void
715 cpu_shutdown(void *args)
716 {
717
718 #ifdef SMP
719         cpu_mp_shutdown();
720 #endif
721         openfirmware_exit(args);
722 }
723
724 /* Get current clock frequency for the given CPU ID. */
725 int
726 cpu_est_clockrate(int cpu_id, uint64_t *rate)
727 {
728
729         return (ENXIO);
730 }
731
732 /*
733  * Duplicate OF_exit() with a different firmware call function that restores
734  * the trap table, otherwise a RED state exception is triggered in at least
735  * some firmware versions.
736  */
737 void
738 cpu_halt(void)
739 {
740         static struct {
741                 cell_t name;
742                 cell_t nargs;
743                 cell_t nreturns;
744         } args = {
745                 (cell_t)"exit",
746                 0,
747                 0
748         };
749
750         cpu_shutdown(&args);
751 }
752
753 void
754 sparc64_shutdown_final(void *dummy, int howto)
755 {
756         static struct {
757                 cell_t name;
758                 cell_t nargs;
759                 cell_t nreturns;
760         } args = {
761                 (cell_t)"SUNW,power-off",
762                 0,
763                 0
764         };
765
766         /* Turn the power off? */
767         if ((howto & RB_POWEROFF) != 0)
768                 cpu_shutdown(&args);
769         /* In case of halt, return to the firmware */
770         if ((howto & RB_HALT) != 0)
771                 cpu_halt();
772 }
773
774 void
775 cpu_idle(void)
776 {
777
778         /* Insert code to halt (until next interrupt) for the idle loop. */
779 }
780
781 int
782 ptrace_set_pc(struct thread *td, u_long addr)
783 {
784
785         td->td_frame->tf_tpc = addr;
786         td->td_frame->tf_tnpc = addr + 4;
787         return (0);
788 }
789
790 int
791 ptrace_single_step(struct thread *td)
792 {
793
794         /* TODO; */
795         return (0);
796 }
797
798 int
799 ptrace_clear_single_step(struct thread *td)
800 {
801
802         /* TODO; */
803         return (0);
804 }
805
806 void
807 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
808 {
809         struct trapframe *tf;
810         struct pcb *pcb;
811         struct proc *p;
812         u_long sp;
813
814         /* XXX no cpu_exec */
815         p = td->td_proc;
816         p->p_md.md_sigtramp = NULL;
817         if (p->p_md.md_utrap != NULL) {
818                 utrap_free(p->p_md.md_utrap);
819                 p->p_md.md_utrap = NULL;
820         }
821
822         pcb = td->td_pcb;
823         tf = td->td_frame;
824         sp = rounddown(stack, 16);
825         bzero(pcb, sizeof(*pcb));
826         bzero(tf, sizeof(*tf));
827         tf->tf_out[0] = stack;
828         tf->tf_out[3] = p->p_sysent->sv_psstrings;
829         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
830         tf->tf_tnpc = entry + 4;
831         tf->tf_tpc = entry;
832         tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
833
834         td->td_retval[0] = tf->tf_out[0];
835         td->td_retval[1] = tf->tf_out[1];
836 }
837
838 int
839 fill_regs(struct thread *td, struct reg *regs)
840 {
841
842         bcopy(td->td_frame, regs, sizeof(*regs));
843         return (0);
844 }
845
846 int
847 set_regs(struct thread *td, struct reg *regs)
848 {
849         struct trapframe *tf;
850
851         if (!TSTATE_SECURE(regs->r_tstate))
852                 return (EINVAL);
853         tf = td->td_frame;
854         regs->r_wstate = tf->tf_wstate;
855         bcopy(regs, tf, sizeof(*regs));
856         return (0);
857 }
858
859 int
860 fill_dbregs(struct thread *td, struct dbreg *dbregs)
861 {
862
863         return (ENOSYS);
864 }
865
866 int
867 set_dbregs(struct thread *td, struct dbreg *dbregs)
868 {
869
870         return (ENOSYS);
871 }
872
873 int
874 fill_fpregs(struct thread *td, struct fpreg *fpregs)
875 {
876         struct trapframe *tf;
877         struct pcb *pcb;
878
879         pcb = td->td_pcb;
880         tf = td->td_frame;
881         bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
882         fpregs->fr_fsr = tf->tf_fsr;
883         fpregs->fr_gsr = tf->tf_gsr;
884         return (0);
885 }
886
887 int
888 set_fpregs(struct thread *td, struct fpreg *fpregs)
889 {
890         struct trapframe *tf;
891         struct pcb *pcb;
892
893         pcb = td->td_pcb;
894         tf = td->td_frame;
895         tf->tf_fprs &= ~FPRS_FEF;
896         bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
897         tf->tf_fsr = fpregs->fr_fsr;
898         tf->tf_gsr = fpregs->fr_gsr;
899         return (0);
900 }
901
902 struct md_utrap *
903 utrap_alloc(void)
904 {
905         struct md_utrap *ut;
906
907         ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
908         ut->ut_refcnt = 1;
909         return (ut);
910 }
911
912 void
913 utrap_free(struct md_utrap *ut)
914 {
915         int refcnt;
916
917         if (ut == NULL)
918                 return;
919         mtx_pool_lock(mtxpool_sleep, ut);
920         ut->ut_refcnt--;
921         refcnt = ut->ut_refcnt;
922         mtx_pool_unlock(mtxpool_sleep, ut);
923         if (refcnt == 0)
924                 free(ut, M_SUBPROC);
925 }
926
927 struct md_utrap *
928 utrap_hold(struct md_utrap *ut)
929 {
930
931         if (ut == NULL)
932                 return (NULL);
933         mtx_pool_lock(mtxpool_sleep, ut);
934         ut->ut_refcnt++;
935         mtx_pool_unlock(mtxpool_sleep, ut);
936         return (ut);
937 }