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