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