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