]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/sparc64/sparc64/machdep.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / 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
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/cache.h>
91 #include <machine/cmt.h>
92 #include <machine/cpu.h>
93 #include <machine/fireplane.h>
94 #include <machine/fp.h>
95 #include <machine/fsr.h>
96 #include <machine/intr_machdep.h>
97 #include <machine/jbus.h>
98 #include <machine/md_var.h>
99 #include <machine/metadata.h>
100 #include <machine/ofw_machdep.h>
101 #include <machine/ofw_mem.h>
102 #include <machine/pcb.h>
103 #include <machine/pmap.h>
104 #include <machine/pstate.h>
105 #include <machine/reg.h>
106 #include <machine/sigframe.h>
107 #include <machine/smp.h>
108 #include <machine/tick.h>
109 #include <machine/tlb.h>
110 #include <machine/tstate.h>
111 #include <machine/upa.h>
112 #include <machine/ver.h>
113
114 typedef int ofw_vec_t(void *);
115
116 #ifdef DDB
117 extern vm_offset_t ksym_start, ksym_end;
118 #endif
119
120 int dtlb_slots;
121 int itlb_slots;
122 struct tlb_entry *kernel_tlbs;
123 int kernel_tlb_slots;
124
125 int cold = 1;
126 long Maxmem;
127 long realmem;
128
129 void *dpcpu0;
130 char pcpu0[PCPU_PAGES * PAGE_SIZE];
131 struct trapframe frame0;
132
133 vm_offset_t kstack0;
134 vm_paddr_t kstack0_phys;
135
136 struct kva_md_info kmi;
137
138 u_long ofw_vec;
139 u_long ofw_tba;
140 u_int tba_taken_over;
141
142 char sparc64_model[32];
143
144 static int cpu_use_vis = 1;
145
146 cpu_block_copy_t *cpu_block_copy;
147 cpu_block_zero_t *cpu_block_zero;
148
149 static phandle_t find_bsp(phandle_t node, uint32_t bspid, u_int cpu_impl);
150 void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3,
151     ofw_vec_t *vec);
152 static void sparc64_shutdown_final(void *dummy, int howto);
153
154 static void cpu_startup(void *arg);
155 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
156
157 CTASSERT((1 << INT_SHIFT) == sizeof(int));
158 CTASSERT((1 << PTR_SHIFT) == sizeof(char *));
159
160 CTASSERT(sizeof(struct reg) == 256);
161 CTASSERT(sizeof(struct fpreg) == 272);
162 CTASSERT(sizeof(struct __mcontext) == 512);
163
164 CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0);
165 CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0);
166 CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0);
167 CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8));
168
169 CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2));
170
171 static void
172 cpu_startup(void *arg)
173 {
174         vm_paddr_t physsz;
175         int i;
176
177         physsz = 0;
178         for (i = 0; i < sparc64_nmemreg; i++)
179                 physsz += sparc64_memreg[i].mr_size;
180         printf("real memory  = %lu (%lu MB)\n", physsz,
181             physsz / (1024 * 1024));
182         realmem = (long)physsz / PAGE_SIZE;
183
184         vm_ksubmap_init(&kmi);
185
186         bufinit();
187         vm_pager_bufferinit();
188
189         EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
190             SHUTDOWN_PRI_LAST);
191
192         printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
193             cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
194
195         if (bootverbose)
196                 printf("machine: %s\n", sparc64_model);
197
198         cpu_identify(rdpr(ver), PCPU_GET(clock), curcpu);
199
200         /*
201          * Add BSP as an interrupt target.
202          */
203         intr_add_cpu(0);
204 }
205
206 void
207 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
208 {
209         struct intr_request *ir;
210         int i;
211
212         pcpu->pc_irtail = &pcpu->pc_irhead;
213         for (i = 0; i < IR_FREE; i++) {
214                 ir = &pcpu->pc_irpool[i];
215                 ir->ir_next = pcpu->pc_irfree;
216                 pcpu->pc_irfree = ir;
217         }
218 }
219
220 void
221 spinlock_enter(void)
222 {
223         struct thread *td;
224         register_t pil;
225
226         td = curthread;
227         if (td->td_md.md_spinlock_count == 0) {
228                 pil = rdpr(pil);
229                 wrpr(pil, 0, PIL_TICK);
230                 td->td_md.md_spinlock_count = 1;
231                 td->td_md.md_saved_pil = pil;
232         } else
233                 td->td_md.md_spinlock_count++;
234         critical_enter();
235 }
236
237 void
238 spinlock_exit(void)
239 {
240         struct thread *td;
241         register_t pil;
242
243         td = curthread;
244         critical_exit();
245         pil = td->td_md.md_saved_pil;
246         td->td_md.md_spinlock_count--;
247         if (td->td_md.md_spinlock_count == 0)
248                 wrpr(pil, pil, 0);
249 }
250
251 static phandle_t
252 find_bsp(phandle_t node, uint32_t bspid, u_int cpu_impl)
253 {
254         char type[sizeof("cpu")];
255         phandle_t child;
256         uint32_t cpuid;
257
258         for (; node != 0; node = OF_peer(node)) {
259                 child = OF_child(node);
260                 if (child > 0) {
261                         child = find_bsp(child, bspid, cpu_impl);
262                         if (child > 0)
263                                 return (child);
264                 } else {
265                         if (OF_getprop(node, "device_type", type,
266                             sizeof(type)) <= 0)
267                                 continue;
268                         if (strcmp(type, "cpu") != 0)
269                                 continue;
270                         if (OF_getprop(node, cpu_cpuid_prop(cpu_impl), &cpuid,
271                             sizeof(cpuid)) <= 0)
272                                 continue;
273                         if (cpuid == bspid)
274                                 return (node);
275                 }
276         }
277         return (0);
278 }
279
280 const char *
281 cpu_cpuid_prop(u_int cpu_impl)
282 {
283
284         switch (cpu_impl) {
285         case CPU_IMPL_SPARC64:
286         case CPU_IMPL_SPARC64V:
287         case CPU_IMPL_ULTRASPARCI:
288         case CPU_IMPL_ULTRASPARCII:
289         case CPU_IMPL_ULTRASPARCIIi:
290         case CPU_IMPL_ULTRASPARCIIe:
291                 return ("upa-portid");
292         case CPU_IMPL_ULTRASPARCIII:
293         case CPU_IMPL_ULTRASPARCIIIp:
294         case CPU_IMPL_ULTRASPARCIIIi:
295         case CPU_IMPL_ULTRASPARCIIIip:
296                 return ("portid");
297         case CPU_IMPL_ULTRASPARCIV:
298         case CPU_IMPL_ULTRASPARCIVp:
299                 return ("cpuid");
300         default:
301                 return ("");
302         }
303 }
304
305 uint32_t
306 cpu_get_mid(u_int cpu_impl)
307 {
308
309         switch (cpu_impl) {
310         case CPU_IMPL_SPARC64:
311         case CPU_IMPL_SPARC64V:
312         case CPU_IMPL_ULTRASPARCI:
313         case CPU_IMPL_ULTRASPARCII:
314         case CPU_IMPL_ULTRASPARCIIi:
315         case CPU_IMPL_ULTRASPARCIIe:
316                 return (UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG)));
317         case CPU_IMPL_ULTRASPARCIII:
318         case CPU_IMPL_ULTRASPARCIIIp:
319                 return (FIREPLANE_CR_GET_AID(ldxa(AA_FIREPLANE_CONFIG,
320                     ASI_FIREPLANE_CONFIG_REG)));
321         case CPU_IMPL_ULTRASPARCIIIi:
322         case CPU_IMPL_ULTRASPARCIIIip:
323                 return (JBUS_CR_GET_JID(ldxa(0, ASI_JBUS_CONFIG_REG)));
324         case CPU_IMPL_ULTRASPARCIV:
325         case CPU_IMPL_ULTRASPARCIVp:
326                 return (INTR_ID_GET_ID(ldxa(AA_INTR_ID, ASI_INTR_ID)));
327         default:
328                 return (0);
329         }
330 }
331
332 void
333 sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
334 {
335         char *env;
336         struct pcpu *pc;
337         vm_offset_t end;
338         vm_offset_t va;
339         caddr_t kmdp;
340         phandle_t root;
341         u_int cpu_impl;
342
343         end = 0;
344         kmdp = NULL;
345
346         /*
347          * Find out what kind of CPU we have first, for anything that changes
348          * behaviour.
349          */
350         cpu_impl = VER_IMPL(rdpr(ver));
351
352         /*
353          * Do CPU-specific initialization.
354          */
355         if (cpu_impl >= CPU_IMPL_ULTRASPARCIII)
356                 cheetah_init(cpu_impl);
357         else if (cpu_impl == CPU_IMPL_SPARC64V)
358                 zeus_init(cpu_impl);
359
360         /*
361          * Clear (S)TICK timer (including NPT).
362          */
363         tick_clear(cpu_impl);
364
365         /*
366          * UltraSparc II[e,i] based systems come up with the tick interrupt
367          * enabled and a handler that resets the tick counter, causing DELAY()
368          * to not work properly when used early in boot.
369          * UltraSPARC III based systems come up with the system tick interrupt
370          * enabled, causing an interrupt storm on startup since they are not
371          * handled.
372          */
373         tick_stop(cpu_impl);
374
375         /*
376          * Set up Open Firmware entry points.
377          */
378         ofw_tba = rdpr(tba);
379         ofw_vec = (u_long)vec;
380
381         /*
382          * Parse metadata if present and fetch parameters.  Must be before the
383          * console is inited so cninit() gets the right value of boothowto.
384          */
385         if (mdp != NULL) {
386                 preload_metadata = mdp;
387                 kmdp = preload_search_by_type("elf kernel");
388                 if (kmdp != NULL) {
389                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
390                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
391                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
392                         kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
393                             int);
394                         kernel_tlbs = (void *)preload_search_info(kmdp,
395                             MODINFO_METADATA | MODINFOMD_DTLB);
396                 }
397         }
398
399         init_param1();
400
401         /*
402          * Initialize Open Firmware (needed for console).
403          */
404         OF_install(OFW_STD_DIRECT, 0);
405         OF_init(ofw_entry);
406
407         /*
408          * Prime our per-CPU data page for use.  Note, we are using it for
409          * our stack, so don't pass the real size (PAGE_SIZE) to pcpu_init
410          * or it'll zero it out from under us.
411          */
412         pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
413         pcpu_init(pc, 0, sizeof(struct pcpu));
414         pc->pc_addr = (vm_offset_t)pcpu0;
415         pc->pc_impl = cpu_impl;
416         pc->pc_mid = cpu_get_mid(cpu_impl);
417         pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
418         pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
419         pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
420
421         /*
422          * Determine the OFW node and frequency of the BSP (and ensure the
423          * BSP is in the device tree in the first place).
424          */
425         root = OF_peer(0);
426         pc->pc_node = find_bsp(root, pc->pc_mid, cpu_impl);
427         if (pc->pc_node == 0)
428                 OF_panic("%s: cannot find boot CPU node", __func__);
429         if (OF_getprop(pc->pc_node, "clock-frequency", &pc->pc_clock,
430             sizeof(pc->pc_clock)) <= 0)
431                 OF_panic("%s: cannot determine boot CPU clock", __func__);
432
433         /*
434          * Panic if there is no metadata.  Most likely the kernel was booted
435          * directly, instead of through loader(8).
436          */
437         if (mdp == NULL || kmdp == NULL || end == 0 ||
438             kernel_tlb_slots == 0 || kernel_tlbs == NULL)
439                 OF_panic("%s: missing loader metadata.\nThis probably means "
440                     "you are not using loader(8).", __func__);
441
442         /*
443          * Work around the broken loader behavior of not demapping no
444          * longer used kernel TLB slots when unloading the kernel or
445          * modules.
446          */
447         for (va = KERNBASE + (kernel_tlb_slots - 1) * PAGE_SIZE_4M;
448             va >= roundup2(end, PAGE_SIZE_4M); va -= PAGE_SIZE_4M) {
449                 if (bootverbose)
450                         OF_printf("demapping unused kernel TLB slot "
451                             "(va %#lx - %#lx)\n", va, va + PAGE_SIZE_4M - 1);
452                 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
453                     ASI_DMMU_DEMAP, 0);
454                 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
455                     ASI_IMMU_DEMAP, 0);
456                 flush(KERNBASE);
457                 kernel_tlb_slots--;
458         }
459
460         /*
461          * Determine the TLB slot maxima, which are expected to be
462          * equal across all CPUs.
463          * NB: for cheetah-class CPUs, these properties only refer
464          * to the t16s.
465          */
466         if (OF_getprop(pc->pc_node, "#dtlb-entries", &dtlb_slots,
467             sizeof(dtlb_slots)) == -1)
468                 OF_panic("%s: cannot determine number of dTLB slots",
469                     __func__);
470         if (OF_getprop(pc->pc_node, "#itlb-entries", &itlb_slots,
471             sizeof(itlb_slots)) == -1)
472                 OF_panic("%s: cannot determine number of iTLB slots",
473                     __func__);
474
475         /*
476          * Initialize and enable the caches.  Note that this may include
477          * applying workarounds.
478          */
479         cache_init(pc);
480         cache_enable(cpu_impl);
481         uma_set_align(pc->pc_cache.dc_linesize - 1);
482
483         cpu_block_copy = bcopy;
484         cpu_block_zero = bzero;
485         getenv_int("machdep.use_vis", &cpu_use_vis);
486         if (cpu_use_vis) {
487                 switch (cpu_impl) {
488                 case CPU_IMPL_SPARC64:
489                 case CPU_IMPL_ULTRASPARCI:
490                 case CPU_IMPL_ULTRASPARCII:
491                 case CPU_IMPL_ULTRASPARCIIi:
492                 case CPU_IMPL_ULTRASPARCIIe:
493                 case CPU_IMPL_ULTRASPARCIII:    /* NB: we've disabled P$. */
494                 case CPU_IMPL_ULTRASPARCIIIp:
495                 case CPU_IMPL_ULTRASPARCIIIi:
496                 case CPU_IMPL_ULTRASPARCIV:
497                 case CPU_IMPL_ULTRASPARCIVp:
498                 case CPU_IMPL_ULTRASPARCIIIip:
499                         cpu_block_copy = spitfire_block_copy;
500                         cpu_block_zero = spitfire_block_zero;
501                         break;
502                 case CPU_IMPL_SPARC64V:
503                         cpu_block_copy = zeus_block_copy;
504                         cpu_block_zero = zeus_block_zero;
505                         break;
506                 }
507         }
508
509 #ifdef SMP
510         mp_init(cpu_impl);
511 #endif
512
513         /*
514          * Initialize virtual memory and calculate physmem.
515          */
516         pmap_bootstrap(cpu_impl);
517
518         /*
519          * Initialize tunables.
520          */
521         init_param2(physmem);
522         env = getenv("kernelname");
523         if (env != NULL) {
524                 strlcpy(kernelname, env, sizeof(kernelname));
525                 freeenv(env);
526         }
527
528         /*
529          * Initialize the interrupt tables.
530          */
531         intr_init1();
532
533         /*
534          * Initialize proc0, set kstack0, frame0, curthread and curpcb.
535          */
536         proc_linkup0(&proc0, &thread0);
537         proc0.p_md.md_sigtramp = NULL;
538         proc0.p_md.md_utrap = NULL;
539         thread0.td_kstack = kstack0;
540         thread0.td_kstack_pages = KSTACK_PAGES;
541         thread0.td_pcb = (struct pcb *)
542             (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
543         frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
544         thread0.td_frame = &frame0;
545         pc->pc_curthread = &thread0;
546         pc->pc_curpcb = thread0.td_pcb;
547
548         /*
549          * Initialize global registers.
550          */
551         cpu_setregs(pc);
552
553         /*
554          * Take over the trap table via the PROM.  Using the PROM for this
555          * is necessary in order to set obp-control-relinquished to true
556          * within the PROM so obtaining /virtual-memory/translations doesn't
557          * trigger a fatal reset error or worse things further down the road.
558          * XXX it should be possible to use this solely instead of writing
559          * %tba in cpu_setregs().  Doing so causes a hang however.
560          */
561         sun4u_set_traptable(tl0_base);
562
563         /*
564          * Initialize the console.
565          * NB: the low-level console drivers require a working DELAY() and
566          * some compiler optimizations may cause the curthread accesses of
567          * mutex(9) to be factored out even if the latter aren't actually
568          * called, both requiring PCPU_REG to be set.
569          */
570         cninit();
571
572         /*
573          * Initialize the dynamic per-CPU area for the BSP and the message
574          * buffer (after setting the trap table).
575          */
576         dpcpu_init(dpcpu0, 0);
577         msgbufinit(msgbufp, msgbufsize);
578
579         /*
580          * Initialize mutexes.
581          */
582         mutex_init();
583
584         /*
585          * Finish the interrupt initialization now that mutexes work and
586          * enable them.
587          */
588         intr_init2();
589         wrpr(pil, 0, 0);
590         wrpr(pstate, 0, PSTATE_KERNEL);
591
592         OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
593
594         kdb_init();
595
596 #ifdef KDB
597         if (boothowto & RB_KDB)
598                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
599 #endif
600 }
601
602 void
603 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
604 {
605         struct trapframe *tf;
606         struct sigframe *sfp;
607         struct sigacts *psp;
608         struct sigframe sf;
609         struct thread *td;
610         struct frame *fp;
611         struct proc *p;
612         u_long sp;
613         int oonstack;
614         int sig;
615
616         oonstack = 0;
617         td = curthread;
618         p = td->td_proc;
619         PROC_LOCK_ASSERT(p, MA_OWNED);
620         sig = ksi->ksi_signo;
621         psp = p->p_sigacts;
622         mtx_assert(&psp->ps_mtx, MA_OWNED);
623         tf = td->td_frame;
624         sp = tf->tf_sp + SPOFF;
625         oonstack = sigonstack(sp);
626
627         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
628             catcher, sig);
629
630         /* Make sure we have a signal trampoline to return to. */
631         if (p->p_md.md_sigtramp == NULL) {
632                 /*
633                  * No signal trampoline... kill the process.
634                  */
635                 CTR0(KTR_SIG, "sendsig: no sigtramp");
636                 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
637                 sigexit(td, sig);
638                 /* NOTREACHED */
639         }
640
641         /* Save user context. */
642         bzero(&sf, sizeof(sf));
643         get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
644         sf.sf_uc.uc_sigmask = *mask;
645         sf.sf_uc.uc_stack = td->td_sigstk;
646         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
647             ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
648
649         /* Allocate and validate space for the signal handler context. */
650         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
651             SIGISMEMBER(psp->ps_sigonstack, sig)) {
652                 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
653                     td->td_sigstk.ss_size - sizeof(struct sigframe));
654         } else
655                 sfp = (struct sigframe *)sp - 1;
656         mtx_unlock(&psp->ps_mtx);
657         PROC_UNLOCK(p);
658
659         fp = (struct frame *)sfp - 1;
660
661         /* Translate the signal if appropriate. */
662         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
663                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
664
665         /* Build the argument list for the signal handler. */
666         tf->tf_out[0] = sig;
667         tf->tf_out[2] = (register_t)&sfp->sf_uc;
668         tf->tf_out[4] = (register_t)catcher;
669         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
670                 /* Signal handler installed with SA_SIGINFO. */
671                 tf->tf_out[1] = (register_t)&sfp->sf_si;
672
673                 /* Fill in POSIX parts. */
674                 sf.sf_si = ksi->ksi_info;
675                 sf.sf_si.si_signo = sig; /* maybe a translated signal */
676         } else {
677                 /* Old FreeBSD-style arguments. */
678                 tf->tf_out[1] = ksi->ksi_code;
679                 tf->tf_out[3] = (register_t)ksi->ksi_addr;
680         }
681
682         /* Copy the sigframe out to the user's stack. */
683         if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
684             suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
685                 /*
686                  * Something is wrong with the stack pointer.
687                  * ...Kill the process.
688                  */
689                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
690                 PROC_LOCK(p);
691                 sigexit(td, SIGILL);
692                 /* NOTREACHED */
693         }
694
695         tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
696         tf->tf_tnpc = tf->tf_tpc + 4;
697         tf->tf_sp = (u_long)fp - SPOFF;
698
699         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
700             tf->tf_sp);
701
702         PROC_LOCK(p);
703         mtx_lock(&psp->ps_mtx);
704 }
705
706 #ifndef _SYS_SYSPROTO_H_
707 struct sigreturn_args {
708         ucontext_t *ucp;
709 };
710 #endif
711
712 /*
713  * MPSAFE
714  */
715 int
716 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
717 {
718         struct proc *p;
719         mcontext_t *mc;
720         ucontext_t uc;
721         int error;
722
723         p = td->td_proc;
724         if (rwindow_save(td)) {
725                 PROC_LOCK(p);
726                 sigexit(td, SIGILL);
727         }
728
729         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
730         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
731                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
732                 return (EFAULT);
733         }
734
735         mc = &uc.uc_mcontext;
736         error = set_mcontext(td, mc);
737         if (error != 0)
738                 return (error);
739
740         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
741
742         CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
743             td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
744         return (EJUSTRETURN);
745 }
746
747 /*
748  * Construct a PCB from a trapframe. This is called from kdb_trap() where
749  * we want to start a backtrace from the function that caused us to enter
750  * the debugger. We have the context in the trapframe, but base the trace
751  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
752  * enough for a backtrace.
753  */
754 void
755 makectx(struct trapframe *tf, struct pcb *pcb)
756 {
757
758         pcb->pcb_pc = tf->tf_tpc;
759         pcb->pcb_sp = tf->tf_sp;
760 }
761
762 int
763 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
764 {
765         struct trapframe *tf;
766         struct pcb *pcb;
767
768         tf = td->td_frame;
769         pcb = td->td_pcb;
770         /*
771          * Copy the registers which will be restored by tl0_ret() from the
772          * trapframe.
773          * Note that we skip %g7 which is used as the userland TLS register
774          * and %wstate.
775          */
776         mc->mc_flags = _MC_VERSION;
777         mc->mc_global[1] = tf->tf_global[1];
778         mc->mc_global[2] = tf->tf_global[2];
779         mc->mc_global[3] = tf->tf_global[3];
780         mc->mc_global[4] = tf->tf_global[4];
781         mc->mc_global[5] = tf->tf_global[5];
782         mc->mc_global[6] = tf->tf_global[6];
783         if (flags & GET_MC_CLEAR_RET) {
784                 mc->mc_out[0] = 0;
785                 mc->mc_out[1] = 0;
786         } else {
787                 mc->mc_out[0] = tf->tf_out[0];
788                 mc->mc_out[1] = tf->tf_out[1];
789         }
790         mc->mc_out[2] = tf->tf_out[2];
791         mc->mc_out[3] = tf->tf_out[3];
792         mc->mc_out[4] = tf->tf_out[4];
793         mc->mc_out[5] = tf->tf_out[5];
794         mc->mc_out[6] = tf->tf_out[6];
795         mc->mc_out[7] = tf->tf_out[7];
796         mc->mc_fprs = tf->tf_fprs;
797         mc->mc_fsr = tf->tf_fsr;
798         mc->mc_gsr = tf->tf_gsr;
799         mc->mc_tnpc = tf->tf_tnpc;
800         mc->mc_tpc = tf->tf_tpc;
801         mc->mc_tstate = tf->tf_tstate;
802         mc->mc_y = tf->tf_y;
803         critical_enter();
804         if ((tf->tf_fprs & FPRS_FEF) != 0) {
805                 savefpctx(pcb->pcb_ufp);
806                 tf->tf_fprs &= ~FPRS_FEF;
807                 pcb->pcb_flags |= PCB_FEF;
808         }
809         if ((pcb->pcb_flags & PCB_FEF) != 0) {
810                 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
811                 mc->mc_fprs |= FPRS_FEF;
812         }
813         critical_exit();
814         return (0);
815 }
816
817 int
818 set_mcontext(struct thread *td, const mcontext_t *mc)
819 {
820         struct trapframe *tf;
821         struct pcb *pcb;
822
823         if (!TSTATE_SECURE(mc->mc_tstate) ||
824             (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
825                 return (EINVAL);
826         tf = td->td_frame;
827         pcb = td->td_pcb;
828         /* Make sure the windows are spilled first. */
829         flushw();
830         /*
831          * Copy the registers which will be restored by tl0_ret() to the
832          * trapframe.
833          * Note that we skip %g7 which is used as the userland TLS register
834          * and %wstate.
835          */
836         tf->tf_global[1] = mc->mc_global[1];
837         tf->tf_global[2] = mc->mc_global[2];
838         tf->tf_global[3] = mc->mc_global[3];
839         tf->tf_global[4] = mc->mc_global[4];
840         tf->tf_global[5] = mc->mc_global[5];
841         tf->tf_global[6] = mc->mc_global[6];
842         tf->tf_out[0] = mc->mc_out[0];
843         tf->tf_out[1] = mc->mc_out[1];
844         tf->tf_out[2] = mc->mc_out[2];
845         tf->tf_out[3] = mc->mc_out[3];
846         tf->tf_out[4] = mc->mc_out[4];
847         tf->tf_out[5] = mc->mc_out[5];
848         tf->tf_out[6] = mc->mc_out[6];
849         tf->tf_out[7] = mc->mc_out[7];
850         tf->tf_fprs = mc->mc_fprs;
851         tf->tf_fsr = mc->mc_fsr;
852         tf->tf_gsr = mc->mc_gsr;
853         tf->tf_tnpc = mc->mc_tnpc;
854         tf->tf_tpc = mc->mc_tpc;
855         tf->tf_tstate = mc->mc_tstate;
856         tf->tf_y = mc->mc_y;
857         if ((mc->mc_fprs & FPRS_FEF) != 0) {
858                 tf->tf_fprs = 0;
859                 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
860                 pcb->pcb_flags |= PCB_FEF;
861         }
862         return (0);
863 }
864
865 /*
866  * Exit the kernel and execute a firmware call that will not return, as
867  * specified by the arguments.
868  */
869 void
870 cpu_shutdown(void *args)
871 {
872
873 #ifdef SMP
874         cpu_mp_shutdown();
875 #endif
876         ofw_exit(args);
877 }
878
879 /*
880  * Flush the D-cache for non-DMA I/O so that the I-cache can
881  * be made coherent later.
882  */
883 void
884 cpu_flush_dcache(void *ptr, size_t len)
885 {
886
887         /* TBD */
888 }
889
890 /* Get current clock frequency for the given CPU ID. */
891 int
892 cpu_est_clockrate(int cpu_id, uint64_t *rate)
893 {
894         struct pcpu *pc;
895
896         pc = pcpu_find(cpu_id);
897         if (pc == NULL || rate == NULL)
898                 return (EINVAL);
899         *rate = pc->pc_clock;
900         return (0);
901 }
902
903 /*
904  * Duplicate OF_exit() with a different firmware call function that restores
905  * the trap table, otherwise a RED state exception is triggered in at least
906  * some firmware versions.
907  */
908 void
909 cpu_halt(void)
910 {
911         static struct {
912                 cell_t name;
913                 cell_t nargs;
914                 cell_t nreturns;
915         } args = {
916                 (cell_t)"exit",
917                 0,
918                 0
919         };
920
921         cpu_shutdown(&args);
922 }
923
924 static void
925 sparc64_shutdown_final(void *dummy, int howto)
926 {
927         static struct {
928                 cell_t name;
929                 cell_t nargs;
930                 cell_t nreturns;
931         } args = {
932                 (cell_t)"SUNW,power-off",
933                 0,
934                 0
935         };
936
937         /* Turn the power off? */
938         if ((howto & RB_POWEROFF) != 0)
939                 cpu_shutdown(&args);
940         /* In case of halt, return to the firmware. */
941         if ((howto & RB_HALT) != 0)
942                 cpu_halt();
943 }
944
945 void
946 cpu_idle(int busy)
947 {
948
949         /* Insert code to halt (until next interrupt) for the idle loop. */
950 }
951
952 int
953 cpu_idle_wakeup(int cpu)
954 {
955
956         return (1);
957 }
958
959 int
960 ptrace_set_pc(struct thread *td, u_long addr)
961 {
962
963         td->td_frame->tf_tpc = addr;
964         td->td_frame->tf_tnpc = addr + 4;
965         return (0);
966 }
967
968 int
969 ptrace_single_step(struct thread *td)
970 {
971
972         /* TODO; */
973         return (0);
974 }
975
976 int
977 ptrace_clear_single_step(struct thread *td)
978 {
979
980         /* TODO; */
981         return (0);
982 }
983
984 void
985 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
986 {
987         struct trapframe *tf;
988         struct pcb *pcb;
989         struct proc *p;
990         u_long sp;
991
992         /* XXX no cpu_exec */
993         p = td->td_proc;
994         p->p_md.md_sigtramp = NULL;
995         if (p->p_md.md_utrap != NULL) {
996                 utrap_free(p->p_md.md_utrap);
997                 p->p_md.md_utrap = NULL;
998         }
999
1000         pcb = td->td_pcb;
1001         tf = td->td_frame;
1002         sp = rounddown(stack, 16);
1003         bzero(pcb, sizeof(*pcb));
1004         bzero(tf, sizeof(*tf));
1005         tf->tf_out[0] = stack;
1006         tf->tf_out[3] = p->p_sysent->sv_psstrings;
1007         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
1008         tf->tf_tnpc = imgp->entry_addr + 4;
1009         tf->tf_tpc = imgp->entry_addr;
1010         /*
1011          * While we could adhere to the memory model indicated in the ELF
1012          * header, it turns out that just always using TSO performs best.
1013          */
1014         tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
1015
1016         td->td_retval[0] = tf->tf_out[0];
1017         td->td_retval[1] = tf->tf_out[1];
1018 }
1019
1020 int
1021 fill_regs(struct thread *td, struct reg *regs)
1022 {
1023
1024         bcopy(td->td_frame, regs, sizeof(*regs));
1025         return (0);
1026 }
1027
1028 int
1029 set_regs(struct thread *td, struct reg *regs)
1030 {
1031         struct trapframe *tf;
1032
1033         if (!TSTATE_SECURE(regs->r_tstate))
1034                 return (EINVAL);
1035         tf = td->td_frame;
1036         regs->r_wstate = tf->tf_wstate;
1037         bcopy(regs, tf, sizeof(*regs));
1038         return (0);
1039 }
1040
1041 int
1042 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1043 {
1044
1045         return (ENOSYS);
1046 }
1047
1048 int
1049 set_dbregs(struct thread *td, struct dbreg *dbregs)
1050 {
1051
1052         return (ENOSYS);
1053 }
1054
1055 int
1056 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1057 {
1058         struct trapframe *tf;
1059         struct pcb *pcb;
1060
1061         pcb = td->td_pcb;
1062         tf = td->td_frame;
1063         bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
1064         fpregs->fr_fsr = tf->tf_fsr;
1065         fpregs->fr_gsr = tf->tf_gsr;
1066         return (0);
1067 }
1068
1069 int
1070 set_fpregs(struct thread *td, struct fpreg *fpregs)
1071 {
1072         struct trapframe *tf;
1073         struct pcb *pcb;
1074
1075         pcb = td->td_pcb;
1076         tf = td->td_frame;
1077         tf->tf_fprs &= ~FPRS_FEF;
1078         bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
1079         tf->tf_fsr = fpregs->fr_fsr;
1080         tf->tf_gsr = fpregs->fr_gsr;
1081         return (0);
1082 }
1083
1084 struct md_utrap *
1085 utrap_alloc(void)
1086 {
1087         struct md_utrap *ut;
1088
1089         ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
1090         ut->ut_refcnt = 1;
1091         return (ut);
1092 }
1093
1094 void
1095 utrap_free(struct md_utrap *ut)
1096 {
1097         int refcnt;
1098
1099         if (ut == NULL)
1100                 return;
1101         mtx_pool_lock(mtxpool_sleep, ut);
1102         ut->ut_refcnt--;
1103         refcnt = ut->ut_refcnt;
1104         mtx_pool_unlock(mtxpool_sleep, ut);
1105         if (refcnt == 0)
1106                 free(ut, M_SUBPROC);
1107 }
1108
1109 struct md_utrap *
1110 utrap_hold(struct md_utrap *ut)
1111 {
1112
1113         if (ut == NULL)
1114                 return (NULL);
1115         mtx_pool_lock(mtxpool_sleep, ut);
1116         ut->ut_refcnt++;
1117         mtx_pool_unlock(mtxpool_sleep, ut);
1118         return (ut);
1119 }