]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/sun4v/sun4v/machdep.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / sun4v / sun4v / 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
45 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/bus.h>
52 #include <sys/cpu.h>
53 #include <sys/cons.h>
54 #include <sys/eventhandler.h>
55 #include <sys/exec.h>
56 #include <sys/imgact.h>
57 #include <sys/interrupt.h>
58 #include <sys/kdb.h>
59 #include <sys/kernel.h>
60 #include <sys/ktr.h>
61 #include <sys/linker.h>
62 #include <sys/lock.h>
63 #include <sys/msgbuf.h>
64 #include <sys/mutex.h>
65 #include <sys/pcpu.h>
66 #include <sys/ptrace.h>
67 #include <sys/reboot.h>
68 #include <sys/signalvar.h>
69 #include <sys/smp.h>
70 #include <sys/syscallsubr.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/clock.h>
91 #include <machine/cpu.h>
92 #include <machine/fp.h>
93 #include <machine/fsr.h>
94 #include <machine/intr_machdep.h>
95 #include <machine/md_var.h>
96 #include <machine/metadata.h>
97 #include <machine/ofw_machdep.h>
98 #include <machine/ofw_mem.h>
99 #include <machine/pcb.h>
100 #include <machine/pmap.h>
101 #include <machine/pstate.h>
102 #include <machine/reg.h>
103 #include <machine/sigframe.h>
104 #include <machine/smp.h>
105 #include <machine/tick.h>
106 #include <machine/tlb.h>
107 #include <machine/tstate.h>
108 #include <machine/asm.h>
109 #include <machine/hv_api.h>
110 #include <machine/wstate.h>
111
112 #include <machine/md_var.h>
113 #include <machine/hypervisorvar.h>
114 #include <dev/ofw/openfirm.h>
115
116 /* XXX move this to a header */
117 extern void mdesc_init(void);
118
119 typedef int ofw_vec_t(void *);
120
121 #ifdef DDB
122 extern vm_offset_t ksym_start, ksym_end;
123 #endif
124
125 struct tlb_entry *kernel_tlbs;
126 int kernel_tlb_slots;
127
128 int cold = 1;
129 long Maxmem;
130 long realmem;
131
132 void *dpcpu0;
133 char pcpu0[PCPU_PAGES * PAGE_SIZE];
134 struct trapframe frame0;
135 int trap_conversion[256];
136 vm_paddr_t mmu_fault_status_area;
137
138 vm_offset_t kstack0;
139 vm_paddr_t kstack0_phys;
140
141 struct kva_md_info kmi;
142
143 u_long ofw_vec;
144 u_long ofw_tba;
145
146 /*
147  * Note: timer quality for CPU's is set low to try and prevent them from
148  * being chosen as the primary timecounter.  The CPU counters are not
149  * synchronized among the CPU's so in MP machines this causes problems
150  * when calculating the time.  With this value the CPU's should only be
151  * chosen as the primary timecounter as a last resort.
152  */
153
154 #define UP_TICK_QUALITY 1000
155 #ifdef SUN4V
156 #define MP_TICK_QUALITY 1000
157 #else
158 #define MP_TICK_QUALITY -100
159 #endif
160
161
162
163
164
165 static struct timecounter tick_tc;
166
167 char sparc64_model[32];
168
169 cpu_block_copy_t *cpu_block_copy;
170 cpu_block_zero_t *cpu_block_zero;
171
172 static timecounter_get_t tick_get_timecount;
173 void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3,
174                   ofw_vec_t *vec);
175 void sparc64_shutdown_final(void *dummy, int howto);
176
177 static void cpu_startup(void *);
178 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
179
180 CTASSERT((1 << INT_SHIFT) == sizeof(int));
181 CTASSERT((1 << PTR_SHIFT) == sizeof(char *));
182
183 CTASSERT(sizeof(struct reg) == 256);
184 CTASSERT(sizeof(struct fpreg) == 272);
185 CTASSERT(sizeof(struct __mcontext) == 512);
186
187 CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0);
188 CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0);
189 CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0);
190 CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8));
191
192 CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2));
193 CTASSERT((sizeof(struct pcpu) & ((1<<6)-1)) == 0);
194
195
196 #define BVPRINTF(x) \
197         if (bootverbose) \
198                 printf(x);
199
200 static void
201 cpu_startup(void *arg)
202 {
203         vm_paddr_t physsz;
204         int i;
205
206         tick_tc.tc_get_timecount = tick_get_timecount;
207         tick_tc.tc_poll_pps = NULL;
208         tick_tc.tc_counter_mask = ~0u;
209         tick_tc.tc_frequency = tick_freq;
210         tick_tc.tc_name = "tick";
211         tick_tc.tc_quality = UP_TICK_QUALITY;
212 #ifdef SMP
213         /*
214          * We do not know if each CPU's tick counter is synchronized.
215          */
216         if (cpu_mp_probe())
217                 tick_tc.tc_quality = MP_TICK_QUALITY;
218 #endif
219
220         tc_init(&tick_tc);
221
222         physsz = 0;
223         for (i = 0; i < sparc64_nmemreg; i++)
224                 physsz += sparc64_memreg[i].mr_size;
225         printf("real memory  = %lu (%lu MB)\n", physsz,
226             physsz / (1024 * 1024));
227         realmem = (long)physsz;
228
229         vm_ksubmap_init(&kmi);
230
231         bufinit();
232         vm_pager_bufferinit();
233
234         EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
235             SHUTDOWN_PRI_LAST);
236
237         printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
238             cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
239
240         if (bootverbose)
241                 printf("machine: %s\n", sparc64_model);
242
243 #ifdef notyet
244         cpu_identify(rdpr(ver), tick_freq, PCPU_GET(cpuid));
245 #endif 
246 }
247
248 void
249 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
250 {
251         struct intr_request *ir;
252         int i;
253
254         pcpu->pc_irtail = &pcpu->pc_irhead;
255         for (i = 0; i < IR_FREE; i++) {
256                 ir = &pcpu->pc_irpool[i];
257                 ir->ir_next = pcpu->pc_irfree;
258                 pcpu->pc_irfree = ir;
259         }
260 }
261
262 void
263 spinlock_enter(void)
264 {
265         struct thread *td;
266         register_t pil;
267
268         td = curthread;
269         if (td->td_md.md_spinlock_count == 0) {
270                 pil = intr_disable();
271                 td->td_md.md_spinlock_count = 1;
272                 td->td_md.md_saved_pil = pil;
273         } else
274                 td->td_md.md_spinlock_count++;
275         critical_enter();
276 }
277
278 void
279 spinlock_exit(void)
280 {
281         struct thread *td;
282         register_t pil;
283
284         td = curthread;
285         critical_exit();
286         pil = td->td_md.md_saved_pil;
287         td->td_md.md_spinlock_count--;
288         if (td->td_md.md_spinlock_count == 0)
289                 intr_restore(pil);
290 }
291
292 unsigned
293 tick_get_timecount(struct timecounter *tc)
294 {
295         return ((unsigned)rd(tick));
296 }
297
298 void
299 sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
300 {
301         phandle_t child;
302         phandle_t root;
303         struct pcpu *pc;
304         vm_offset_t end;
305         caddr_t kmdp;
306         u_int clock;
307         char *env;
308         char type[8];
309         vm_paddr_t mmfsa;
310         int i;
311
312         end = 0;
313         kmdp = NULL;
314
315         /*
316          * XXX
317          */
318         bootverbose = 1;
319
320         /*
321          * Set up Open Firmware entry points
322          */
323         ofw_tba = rdpr(tba);
324         ofw_vec = (u_long)vec;
325
326         /*
327          * Parse metadata if present and fetch parameters.  Must be before the
328          * console is inited so cninit gets the right value of boothowto.
329          */
330         if (mdp != NULL) {
331                 preload_metadata = mdp;
332                 kmdp = preload_search_by_type("elf kernel");
333                 if (kmdp != NULL) {
334                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
335                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
336                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
337                         kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
338                             int);
339                         kernel_tlbs = (void *)preload_search_info(kmdp,
340                             MODINFO_METADATA | MODINFOMD_DTLB);
341                 }
342         }
343
344         if (boothowto & RB_VERBOSE)
345                 bootverbose = 1;
346
347         init_param1();
348
349         /*
350          * Initialize Open Firmware (needed for console).
351          */
352         OF_install(OFW_STD_DIRECT, 0);
353         OF_init(ofw_entry);
354
355         root = OF_peer(0);
356         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
357                 OF_getprop(child, "device_type", type, sizeof(type));
358                 if (strcmp(type, "cpu") == 0)
359                         break;
360         }
361
362         OF_getprop(child, "clock-frequency", &clock, sizeof(clock));
363
364         /*
365          * Initialize the console before printing anything.
366          * console uses the pcpu area for serialization 
367          */
368         pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
369         cpu_setregs(pc);
370
371         /*
372          * Initialize proc0 stuff (p_contested needs to be done early).
373          */
374
375         proc_linkup0(&proc0, &thread0);
376         proc0.p_md.md_sigtramp = NULL;
377         proc0.p_md.md_utrap = NULL;
378         frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
379         thread0.td_frame = &frame0;
380         if ((u_long)thread0.td_frame & 0x3f) {
381                 panic("unaligned frame0");
382         }
383
384         /*
385          * Prime our per-cpu data page for use.  Note, we are using it for our
386          * stack, so don't pass the real size (PAGE_SIZE) to pcpu_init or
387          * it'll zero it out from under us.
388          */
389         pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
390         pcpu_init(pc, 0, sizeof(struct pcpu));
391         pc->pc_curthread = &thread0;
392         pc->pc_addr = (vm_offset_t)pcpu0;
393
394         cninit();
395         tick_init(clock);
396
397         printf("cpu0: UltraSparc T1 Processor (%d.%02d MHz CPU)\n",
398             (clock + 4999) / 1000000, ((clock + 4999) / 10000) % 100);
399
400         /*
401          * Panic is there is no metadata.  Most likely the kernel was booted
402          * directly, instead of through loader(8).
403          */
404         if (mdp == NULL || kmdp == NULL) {
405                 printf("sparc64_init: no loader metadata.\n"
406                        "This probably means you are not using loader(8).\n");
407                 panic("sparc64_init");
408         }
409
410         /*
411          * Sanity check the kernel end, which is important.
412          */
413         if (end == 0) {
414                 printf("sparc64_init: warning, kernel end not specified.\n"
415                        "Attempting to continue anyway.\n");
416                 end = (vm_offset_t)_end;
417         }
418
419         cpu_block_copy = bcopy;
420         cpu_block_zero = bzero;
421
422 #ifdef SMP
423         mp_tramp = mp_tramp_alloc();
424 #endif
425
426         env = getenv("kernelname");
427         if (env != NULL) {
428                 strlcpy(kernelname, env, sizeof(kernelname));
429                 freeenv(env);
430         }
431
432         /*
433          * Initialize global registers.
434          * needed for curthread to work
435          */
436         cpu_setregs(pc);
437
438         /*
439          * Initialize virtual memory and calculate physmem.
440          */
441         pmap_bootstrap(end);
442
443         thread0.td_kstack = kstack0;
444         thread0.td_md.md_saved_pil = 0;
445         thread0.td_pcb = (struct pcb *)
446                 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
447         thread0.td_pcb->pcb_kstack = (uint64_t)(((char *)thread0.td_pcb) - (CCFSZ + SPOFF));
448         thread0.td_pcb = (struct pcb *)TLB_PHYS_TO_DIRECT(vtophys((vm_offset_t)thread0.td_pcb));
449         pc->pc_curpcb = thread0.td_pcb;
450
451         if (((thread0.td_pcb->pcb_kstack + SPOFF) & 0x3f) != 0) {
452                 printf("unaligned stack pcb_kstack & 0x3f == 0x%lx\n", 
453                        ((thread0.td_pcb->pcb_kstack + SPOFF) & 0x3f));
454         }
455
456         /*
457          * Update PCPU_REG to point to direct address
458          * to support easy phys <-> virt translation in trap handler
459          */
460         pc = (struct pcpu *)TLB_PHYS_TO_DIRECT(vtophys(pc));
461
462         BVPRINTF("initializing cpu regs\n");
463         cpu_setregs(pc);
464         
465         /*
466          * Initialize tunables.
467          */
468         BVPRINTF("initialize tunables\n");
469         init_param2(physmem);
470
471         /*
472          * setup trap table and fault status area
473          */
474         BVPRINTF("initialize trap tables\n");
475
476         mmfsa = mmu_fault_status_area + MMFSA_SIZE;
477         BVPRINTF("setwstate\n");
478         set_wstate(WSTATE_KERN);
479         BVPRINTF("set_mmfsa_scratchpad\n");
480         set_mmfsa_scratchpad(mmfsa);
481
482         BVPRINTF("init_mondo_queue\n");
483         init_mondo_queue();
484         BVPRINTF("set_mmfsa_traptable\n");
485         set_mmfsa_traptable(&tl0_base, mmfsa);
486         BVPRINTF("trap conversion\n");
487         for (i = 0; i < 256; i++)
488                 trap_conversion[i] = 0;
489         trap_conversion[TT_INSTRUCTION_EXCEPTION] = T_INSTRUCTION_EXCEPTION;
490         trap_conversion[TT_INSTRUCTION_MISS]      = T_INSTRUCTION_MISS;
491         trap_conversion[TT_ILLEGAL_INSTRUCTION]   = T_ILLEGAL_INSTRUCTION;
492         trap_conversion[TT_PRIVILEGED_OPCODE]     = T_PRIVILEGED_OPCODE;
493         trap_conversion[TT_FP_EXCEPTION_IEEE_754] = T_FP_EXCEPTION_IEEE_754; 
494         trap_conversion[TT_TAG_OVERFLOW]          = T_TAG_OVERFLOW;
495         trap_conversion[TT_DIVISION_BY_ZERO]      = T_DIVISION_BY_ZERO;
496         trap_conversion[TT_DATA_EXCEPTION]        = T_DATA_EXCEPTION;
497         trap_conversion[TT_DATA_MISS]             = T_DATA_MISS;
498         trap_conversion[TT_ALIGNMENT]             = T_ALIGNMENT;
499         trap_conversion[TT_DATA_PROTECTION]       = T_DATA_PROTECTION;
500         
501         /*
502          * Initialize the message buffer (after setting trap table).
503          */
504         BVPRINTF("initialize msgbuf\n");
505         dpcpu_init(dpcpu0, 0);
506         msgbufinit(msgbufp, msgbufsize);
507
508         BVPRINTF("initialize mutexes\n");
509         mutex_init();
510         
511         BVPRINTF("initialize machine descriptor table\n");
512         mdesc_init();
513
514         BVPRINTF("initialize get model name\n");
515         OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
516
517         BVPRINTF("initialize kdb\n");
518         kdb_init();
519
520 #ifdef KDB
521         if (boothowto & RB_KDB)
522                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
523 #endif
524         BVPRINTF("sparc64_init done\n");
525 }
526
527 void
528 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
529 {
530         struct trapframe *tf;
531         struct sigframe *sfp;
532         struct sigacts *psp;
533         struct sigframe sf;
534         struct thread *td;
535         struct frame *fp;
536         struct proc *p;
537         int oonstack;
538         u_long sp;
539         int sig;
540         int code;
541
542         oonstack = 0;
543         td = curthread;
544         p = td->td_proc;
545         PROC_LOCK_ASSERT(p, MA_OWNED);
546         sig = ksi->ksi_signo;
547         code = ksi->ksi_code;
548         psp = p->p_sigacts;
549         mtx_assert(&psp->ps_mtx, MA_OWNED);
550         tf = td->td_frame;
551         sp = tf->tf_sp + SPOFF;
552         oonstack = sigonstack(sp);
553
554         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
555             catcher, sig);
556
557         /* Make sure we have a signal trampoline to return to. */
558         if (p->p_md.md_sigtramp == NULL) {
559                 /*
560                  * No signal tramoline... kill the process.
561                  */
562                 CTR0(KTR_SIG, "sendsig: no sigtramp");
563                 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
564                 sigexit(td, sig);
565                 /* NOTREACHED */
566         }
567
568         /* Save user context. */
569         bzero(&sf, sizeof(sf));
570         get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
571         sf.sf_uc.uc_sigmask = *mask;
572         sf.sf_uc.uc_stack = td->td_sigstk;
573         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
574             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
575
576         /* Allocate and validate space for the signal handler context. */
577         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
578             SIGISMEMBER(psp->ps_sigonstack, sig)) {
579                 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
580                     td->td_sigstk.ss_size - sizeof(struct sigframe));
581         } else
582                 sfp = (struct sigframe *)sp - 1;
583         mtx_unlock(&psp->ps_mtx);
584         PROC_UNLOCK(p);
585
586         fp = (struct frame *)sfp - 1;
587
588         /* Translate the signal if appropriate. */
589         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
590                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
591
592         /* Build the argument list for the signal handler. */
593         tf->tf_out[0] = sig;
594         tf->tf_out[2] = (register_t)&sfp->sf_uc;
595         tf->tf_out[4] = (register_t)catcher;
596
597         /* Fill siginfo structure. */
598         sf.sf_si = ksi->ksi_info;
599         sf.sf_si.si_addr = (void *)tf->tf_tpc;
600         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
601                 /* Signal handler installed with SA_SIGINFO. */
602                 tf->tf_out[1] = (register_t)&sfp->sf_si;
603
604                 /* Fill in POSIX parts. */
605                 sf.sf_si = ksi->ksi_info;
606                 sf.sf_si.si_signo = sig; /* maybe a translated signal */
607         } else {
608                 /* Old FreeBSD-style arguments. */
609                 tf->tf_out[1] = ksi->ksi_code;
610                 tf->tf_out[3] = (register_t)ksi->ksi_addr;
611         }
612
613         /* Copy the sigframe out to the user's stack. */
614         if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
615             suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
616                 /*
617                  * Something is wrong with the stack pointer.
618                  * ...Kill the process.
619                  */
620                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
621                 PROC_LOCK(p);
622                 sigexit(td, SIGILL);
623                 /* NOTREACHED */
624         }
625
626         tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
627         tf->tf_tnpc = tf->tf_tpc + 4;
628         tf->tf_sp = (u_long)fp - SPOFF;
629
630         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
631             tf->tf_sp);
632
633         PROC_LOCK(p);
634         mtx_lock(&psp->ps_mtx);
635 }
636
637 #ifndef _SYS_SYSPROTO_H_
638 struct sigreturn_args {
639         ucontext_t *ucp;
640 };
641 #endif
642
643 /*
644  * MPSAFE
645  */
646 int
647 sigreturn(struct thread *td, struct sigreturn_args *uap)
648 {
649         struct proc *p;
650         mcontext_t *mc;
651         ucontext_t uc;
652         int error;
653
654         p = td->td_proc;
655         if (rwindow_save(td)) {
656                 PROC_LOCK(p);
657                 sigexit(td, SIGILL);
658         }
659
660         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
661         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
662                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
663                 return (EFAULT);
664         }
665
666         mc = &uc.uc_mcontext;
667         error = set_mcontext(td, mc);
668         if (error != 0)
669                 return (error);
670
671         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
672
673         CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
674             td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
675         return (EJUSTRETURN);
676 }
677
678 #ifdef COMPAT_FREEBSD4
679 int
680 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
681 {
682
683         return sigreturn(td, (struct sigreturn_args *)uap);
684 }
685 #endif
686
687 /*
688  * Construct a PCB from a trapframe. This is called from kdb_trap() where
689  * we want to start a backtrace from the function that caused us to enter
690  * the debugger. We have the context in the trapframe, but base the trace
691  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
692  * enough for a backtrace.
693  */
694 void
695 makectx(struct trapframe *tf, struct pcb *pcb)
696 {
697
698         pcb->pcb_pc = tf->tf_tpc;
699         pcb->pcb_sp = tf->tf_sp;
700 }
701
702 int
703 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
704 {
705         struct trapframe *tf;
706         struct pcb *pcb;
707
708         tf = td->td_frame;
709         pcb = td->td_pcb;
710         bcopy(tf, mc, sizeof(*tf));
711         if (flags & GET_MC_CLEAR_RET) {
712                 mc->mc_out[0] = 0;
713                 mc->mc_out[1] = 0;
714         }
715         mc->mc_flags = _MC_VERSION;
716         critical_enter();
717         if ((tf->tf_fprs & FPRS_FEF) != 0) {
718                 savefpctx(pcb->pcb_ufp);
719                 pcb->pcb_flags |= PCB_FEF;
720                 tf->tf_fprs &= ~FPRS_FEF;
721         }
722         if ((pcb->pcb_flags & PCB_FEF) != 0) {
723                 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
724                 mc->mc_fprs |= FPRS_FEF;
725         }
726         critical_exit();
727         return (0);
728 }
729
730 int
731 set_mcontext(struct thread *td, const mcontext_t *mc)
732 {
733         struct trapframe *tf;
734         struct pcb *pcb;
735         uint64_t wstate;
736
737         if (!TSTATE_SECURE(mc->mc_tstate) ||
738             (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
739                 return (EINVAL);
740         tf = td->td_frame;
741         pcb = td->td_pcb;
742         /* Make sure the windows are spilled first. */
743         flushw();
744         wstate = tf->tf_wstate;
745         bcopy(mc, tf, sizeof(*tf));
746         tf->tf_wstate = wstate;
747         if ((mc->mc_fprs & FPRS_FEF) != 0) {
748                 tf->tf_fprs = 0;
749                 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
750                 pcb->pcb_flags |= PCB_FEF;
751         }
752         return (0);
753 }
754
755 /*
756  * Exit the kernel and execute a firmware call that will not return, as
757  * specified by the arguments.
758  */
759 void
760 cpu_shutdown(void *args)
761 {
762
763 #ifdef SMP
764         cpu_mp_shutdown();
765 #endif
766         hv_mach_exit(0);
767 }
768
769 /*
770  * Flush the D-cache for non-DMA I/O so that the I-cache can
771  * be made coherent later.
772  */
773 void
774 cpu_flush_dcache(void *ptr, size_t len)
775 {
776         /* TBD */
777 }
778
779 /* Get current clock frequency for the given cpu id. */
780 int
781 cpu_est_clockrate(int cpu_id, uint64_t *rate)
782 {
783
784         return (ENXIO);
785 }
786
787 /*
788  * Duplicate OF_exit() with a different firmware call function that restores
789  * the trap table, otherwise a RED state exception is triggered in at least
790  * some firmware versions.
791  */
792 void
793 cpu_halt(void)
794 {
795         static struct {
796                 cell_t name;
797                 cell_t nargs;
798                 cell_t nreturns;
799         } args = {
800                 (cell_t)"exit",
801                 0,
802                 0
803         };
804
805         cpu_shutdown(&args);
806 }
807
808 void
809 sparc64_shutdown_final(void *dummy, int howto)
810 {
811         static struct {
812                 cell_t name;
813                 cell_t nargs;
814                 cell_t nreturns;
815         } args = {
816                 (cell_t)"SUNW,power-off",
817                 0,
818                 0
819         };
820
821         /* Turn the power off? */
822         if ((howto & RB_POWEROFF) != 0)
823                 cpu_shutdown(&args);
824         /* In case of halt, return to the firmware */
825         if ((howto & RB_HALT) != 0)
826                 cpu_halt();
827 }
828
829 void
830 cpu_idle(int busy)
831 {
832
833         if (rdpr(pil) != 0) 
834                 panic("pil in cpu_idle not 0 - %ld", rdpr(pil));
835         if (rdpr(pstate) != 0x16)
836                 panic("interrupts disabled in cpu_idle 0x%lx", rdpr(pstate));
837                 /* XXX heinous hack begin*/
838         
839         cpu_yield();
840 }
841
842 int
843 cpu_idle_wakeup(int cpu)
844 {
845
846         return (0);
847 }
848
849 int
850 ptrace_set_pc(struct thread *td, u_long addr)
851 {
852
853         td->td_frame->tf_tpc = addr;
854         td->td_frame->tf_tnpc = addr + 4;
855         return (0);
856 }
857
858 int
859 ptrace_single_step(struct thread *td)
860 {
861         /* TODO; */
862         return (0);
863 }
864
865 int
866 ptrace_clear_single_step(struct thread *td)
867 {
868         /* TODO; */
869         return (0);
870 }
871
872 void
873 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
874 {
875         struct trapframe *tf;
876         struct pcb *pcb;
877         struct proc *p;
878         uint64_t kstack;
879         u_long sp;
880
881         /* XXX no cpu_exec */
882
883         p = td->td_proc;
884         p->p_md.md_sigtramp = NULL;
885         if (p->p_md.md_utrap != NULL) {
886                 utrap_free(p->p_md.md_utrap);
887                 p->p_md.md_utrap = NULL;
888         }
889         pcb = td->td_pcb;
890         kstack = pcb->pcb_kstack;
891         tf = td->td_frame;
892         sp = rounddown(stack, 16);
893         bzero(pcb, sizeof(*pcb));
894         bzero(tf, sizeof(*tf));
895         pcb->pcb_kstack = kstack;
896
897         tf->tf_out[0] = stack;
898         tf->tf_out[3] = p->p_sysent->sv_psstrings;
899         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
900
901         tf->tf_tnpc = entry + 4;
902         tf->tf_tpc = entry;
903         tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
904
905         td->td_retval[0] = tf->tf_out[0];
906         td->td_retval[1] = tf->tf_out[1];
907 }
908
909 int
910 fill_regs(struct thread *td, struct reg *regs)
911 {
912
913         bcopy(td->td_frame, regs, sizeof(*regs));
914         return (0);
915 }
916
917 int
918 set_regs(struct thread *td, struct reg *regs)
919 {
920         struct trapframe *tf;
921
922         if (!TSTATE_SECURE(regs->r_tstate))
923                 return (EINVAL);
924         tf = td->td_frame;
925         regs->r_wstate = tf->tf_wstate;
926         bcopy(regs, tf, sizeof(*regs));
927         return (0);
928 }
929
930 int
931 fill_dbregs(struct thread *td, struct dbreg *dbregs)
932 {
933
934         return (ENOSYS);
935 }
936
937 int
938 set_dbregs(struct thread *td, struct dbreg *dbregs)
939 {
940
941         return (ENOSYS);
942 }
943
944 int
945 fill_fpregs(struct thread *td, struct fpreg *fpregs)
946 {
947         struct trapframe *tf;
948         struct pcb *pcb;
949
950         pcb = td->td_pcb;
951         tf = td->td_frame;
952         tf->tf_fprs = ~FPRS_FEF;
953         bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
954         fpregs->fr_fsr = tf->tf_fsr;
955         fpregs->fr_gsr = tf->tf_gsr;
956         return (0);
957 }
958
959 int
960 set_fpregs(struct thread *td, struct fpreg *fpregs)
961 {
962         struct trapframe *tf;
963         struct pcb *pcb;
964
965         pcb = td->td_pcb;
966         tf = td->td_frame;
967         tf->tf_fprs &= ~FPRS_FEF;
968         bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
969         tf->tf_fsr = fpregs->fr_fsr;
970         tf->tf_gsr = fpregs->fr_gsr;
971         return (0);
972 }
973
974 struct md_utrap *
975 utrap_alloc(void)
976 {
977         struct md_utrap *ut;
978
979         ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
980         ut->ut_refcnt = 1;
981         return (ut);
982 }
983
984 void
985 utrap_free(struct md_utrap *ut)
986 {
987         int refcnt;
988
989         if (ut == NULL)
990                 return;
991         mtx_pool_lock(mtxpool_sleep, ut);
992         ut->ut_refcnt--;
993         refcnt = ut->ut_refcnt;
994         mtx_pool_unlock(mtxpool_sleep, ut);
995         if (refcnt == 0)
996                 free(ut, M_SUBPROC);
997 }
998
999 struct md_utrap *
1000 utrap_hold(struct md_utrap *ut)
1001 {
1002
1003         if (ut == NULL)
1004                 return (NULL);
1005         mtx_pool_lock(mtxpool_sleep, ut);
1006         ut->ut_refcnt++;
1007         mtx_pool_unlock(mtxpool_sleep, ut);
1008         return (ut);
1009 }
1010
1011 void
1012 cpu_yield(void)
1013 {
1014         if (rdpr(pil) < PIL_TICK)
1015                 hv_cpu_yield();
1016 }