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