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