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