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.4/sys/sparc64/sparc64/machdep.c 235442 2012-05-14 15:05:18Z jhb $");
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/cmt.h>
92 #include <machine/cpu.h>
93 #include <machine/fireplane.h>
94 #include <machine/fp.h>
95 #include <machine/fsr.h>
96 #include <machine/intr_machdep.h>
97 #include <machine/jbus.h>
98 #include <machine/md_var.h>
99 #include <machine/metadata.h>
100 #include <machine/ofw_machdep.h>
101 #include <machine/ofw_mem.h>
102 #include <machine/pcb.h>
103 #include <machine/pmap.h>
104 #include <machine/pstate.h>
105 #include <machine/reg.h>
106 #include <machine/sigframe.h>
107 #include <machine/smp.h>
108 #include <machine/tick.h>
109 #include <machine/tlb.h>
110 #include <machine/tstate.h>
111 #include <machine/upa.h>
112 #include <machine/ver.h>
113
114 typedef int ofw_vec_t(void *);
115
116 #ifdef DDB
117 extern vm_offset_t ksym_start, ksym_end;
118 #endif
119
120 int dtlb_slots;
121 int itlb_slots;
122 struct tlb_entry *kernel_tlbs;
123 int kernel_tlb_slots;
124
125 int cold = 1;
126 long Maxmem;
127 long realmem;
128
129 void *dpcpu0;
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 const 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 * Set up Open Firmware entry points.
372 */
373 ofw_tba = rdpr(tba);
374 ofw_vec = (u_long)vec;
375
376 /*
377 * Parse metadata if present and fetch parameters. Must be before the
378 * console is inited so cninit() gets the right value of boothowto.
379 */
380 if (mdp != NULL) {
381 preload_metadata = mdp;
382 kmdp = preload_search_by_type("elf kernel");
383 if (kmdp != NULL) {
384 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
385 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
386 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
387 kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
388 int);
389 kernel_tlbs = (void *)preload_search_info(kmdp,
390 MODINFO_METADATA | MODINFOMD_DTLB);
391 }
392 }
393
394 init_param1();
395
396 /*
397 * Initialize Open Firmware (needed for console).
398 */
399 OF_install(OFW_STD_DIRECT, 0);
400 OF_init(ofw_entry);
401
402 /*
403 * Prime our per-CPU data page for use. Note, we are using it for
404 * our stack, so don't pass the real size (PAGE_SIZE) to pcpu_init
405 * or it'll zero it out from under us.
406 */
407 pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
408 pcpu_init(pc, 0, sizeof(struct pcpu));
409 pc->pc_addr = (vm_offset_t)pcpu0;
410 pc->pc_impl = cpu_impl;
411 pc->pc_mid = cpu_get_mid(cpu_impl);
412 pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
413 pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
414 pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
415
416 /*
417 * Determine the OFW node and frequency of the BSP (and ensure the
418 * BSP is in the device tree in the first place).
419 */
420 root = OF_peer(0);
421 pc->pc_node = find_bsp(root, pc->pc_mid, cpu_impl);
422 if (pc->pc_node == 0)
423 OF_panic("%s: cannot find boot CPU node", __func__);
424 if (OF_getprop(pc->pc_node, "clock-frequency", &pc->pc_clock,
425 sizeof(pc->pc_clock)) <= 0)
426 OF_panic("%s: cannot determine boot CPU clock", __func__);
427
428 /*
429 * Panic if there is no metadata. Most likely the kernel was booted
430 * directly, instead of through loader(8).
431 */
432 if (mdp == NULL || kmdp == NULL || end == 0 ||
433 kernel_tlb_slots == 0 || kernel_tlbs == NULL)
434 OF_panic("%s: missing loader metadata.\nThis probably means "
435 "you are not using loader(8).", __func__);
436
437 /*
438 * Work around the broken loader behavior of not demapping no
439 * longer used kernel TLB slots when unloading the kernel or
440 * modules.
441 */
442 for (va = KERNBASE + (kernel_tlb_slots - 1) * PAGE_SIZE_4M;
443 va >= roundup2(end, PAGE_SIZE_4M); va -= PAGE_SIZE_4M) {
444 if (bootverbose)
445 OF_printf("demapping unused kernel TLB slot "
446 "(va %#lx - %#lx)\n", va, va + PAGE_SIZE_4M - 1);
447 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
448 ASI_DMMU_DEMAP, 0);
449 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
450 ASI_IMMU_DEMAP, 0);
451 flush(KERNBASE);
452 kernel_tlb_slots--;
453 }
454
455 /*
456 * Determine the TLB slot maxima, which are expected to be
457 * equal across all CPUs.
458 * NB: for cheetah-class CPUs, these properties only refer
459 * to the t16s.
460 */
461 if (OF_getprop(pc->pc_node, "#dtlb-entries", &dtlb_slots,
462 sizeof(dtlb_slots)) == -1)
463 OF_panic("%s: cannot determine number of dTLB slots",
464 __func__);
465 if (OF_getprop(pc->pc_node, "#itlb-entries", &itlb_slots,
466 sizeof(itlb_slots)) == -1)
467 OF_panic("%s: cannot determine number of iTLB slots",
468 __func__);
469
470 /*
471 * Initialize and enable the caches. Note that this may include
472 * applying workarounds.
473 */
474 cache_init(pc);
475 cache_enable(cpu_impl);
476 uma_set_align(pc->pc_cache.dc_linesize - 1);
477
478 cpu_block_copy = bcopy;
479 cpu_block_zero = bzero;
480 getenv_int("machdep.use_vis", &cpu_use_vis);
481 if (cpu_use_vis) {
482 switch (cpu_impl) {
483 case CPU_IMPL_SPARC64:
484 case CPU_IMPL_ULTRASPARCI:
485 case CPU_IMPL_ULTRASPARCII:
486 case CPU_IMPL_ULTRASPARCIIi:
487 case CPU_IMPL_ULTRASPARCIIe:
488 case CPU_IMPL_ULTRASPARCIII: /* NB: we've disabled P$. */
489 case CPU_IMPL_ULTRASPARCIIIp:
490 case CPU_IMPL_ULTRASPARCIIIi:
491 case CPU_IMPL_ULTRASPARCIV:
492 case CPU_IMPL_ULTRASPARCIVp:
493 case CPU_IMPL_ULTRASPARCIIIip:
494 cpu_block_copy = spitfire_block_copy;
495 cpu_block_zero = spitfire_block_zero;
496 break;
497 case CPU_IMPL_SPARC64V:
498 cpu_block_copy = zeus_block_copy;
499 cpu_block_zero = zeus_block_zero;
500 break;
501 }
502 }
503
504 #ifdef SMP
505 mp_init(cpu_impl);
506 #endif
507
508 /*
509 * Initialize virtual memory and calculate physmem.
510 */
511 pmap_bootstrap(cpu_impl);
512
513 /*
514 * Initialize tunables.
515 */
516 init_param2(physmem);
517 env = getenv("kernelname");
518 if (env != NULL) {
519 strlcpy(kernelname, env, sizeof(kernelname));
520 freeenv(env);
521 }
522
523 /*
524 * Initialize the interrupt tables.
525 */
526 intr_init1();
527
528 /*
529 * Initialize proc0, set kstack0, frame0, curthread and curpcb.
530 */
531 proc_linkup0(&proc0, &thread0);
532 proc0.p_md.md_sigtramp = NULL;
533 proc0.p_md.md_utrap = NULL;
534 thread0.td_kstack = kstack0;
535 thread0.td_kstack_pages = KSTACK_PAGES;
536 thread0.td_pcb = (struct pcb *)
537 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
538 frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
539 thread0.td_frame = &frame0;
540 pc->pc_curthread = &thread0;
541 pc->pc_curpcb = thread0.td_pcb;
542
543 /*
544 * Initialize global registers.
545 */
546 cpu_setregs(pc);
547
548 /*
549 * Take over the trap table via the PROM. Using the PROM for this
550 * is necessary in order to set obp-control-relinquished to true
551 * within the PROM so obtaining /virtual-memory/translations doesn't
552 * trigger a fatal reset error or worse things further down the road.
553 * XXX it should be possible to use this solely instead of writing
554 * %tba in cpu_setregs(). Doing so causes a hang however.
555 */
556 sun4u_set_traptable(tl0_base);
557
558 /*
559 * Initialize the console.
560 * NB: the low-level console drivers require a working DELAY() and
561 * some compiler optimizations may cause the curthread accesses of
562 * mutex(9) to be factored out even if the latter aren't actually
563 * called, both requiring PCPU_REG to be set.
564 */
565 cninit();
566
567 /*
568 * Initialize the dynamic per-CPU area for the BSP and the message
569 * buffer (after setting the trap table).
570 */
571 dpcpu_init(dpcpu0, 0);
572 msgbufinit(msgbufp, msgbufsize);
573
574 /*
575 * Initialize mutexes.
576 */
577 mutex_init();
578
579 /*
580 * Finish the interrupt initialization now that mutexes work and
581 * enable them.
582 */
583 intr_init2();
584 wrpr(pil, 0, 0);
585 wrpr(pstate, 0, PSTATE_KERNEL);
586
587 OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
588
589 kdb_init();
590
591 #ifdef KDB
592 if (boothowto & RB_KDB)
593 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
594 #endif
595 }
596
597 void
598 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
599 {
600 struct trapframe *tf;
601 struct sigframe *sfp;
602 struct sigacts *psp;
603 struct sigframe sf;
604 struct thread *td;
605 struct frame *fp;
606 struct proc *p;
607 u_long sp;
608 int oonstack;
609 int sig;
610
611 oonstack = 0;
612 td = curthread;
613 p = td->td_proc;
614 PROC_LOCK_ASSERT(p, MA_OWNED);
615 sig = ksi->ksi_signo;
616 psp = p->p_sigacts;
617 mtx_assert(&psp->ps_mtx, MA_OWNED);
618 tf = td->td_frame;
619 sp = tf->tf_sp + SPOFF;
620 oonstack = sigonstack(sp);
621
622 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
623 catcher, sig);
624
625 /* Make sure we have a signal trampoline to return to. */
626 if (p->p_md.md_sigtramp == NULL) {
627 /*
628 * No signal trampoline... kill the process.
629 */
630 CTR0(KTR_SIG, "sendsig: no sigtramp");
631 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
632 sigexit(td, sig);
633 /* NOTREACHED */
634 }
635
636 /* Save user context. */
637 bzero(&sf, sizeof(sf));
638 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
639 sf.sf_uc.uc_sigmask = *mask;
640 sf.sf_uc.uc_stack = td->td_sigstk;
641 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
642 ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
643
644 /* Allocate and validate space for the signal handler context. */
645 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
646 SIGISMEMBER(psp->ps_sigonstack, sig)) {
647 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
648 td->td_sigstk.ss_size - sizeof(struct sigframe));
649 } else
650 sfp = (struct sigframe *)sp - 1;
651 mtx_unlock(&psp->ps_mtx);
652 PROC_UNLOCK(p);
653
654 fp = (struct frame *)sfp - 1;
655
656 /* Translate the signal if appropriate. */
657 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
658 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
659
660 /* Build the argument list for the signal handler. */
661 tf->tf_out[0] = sig;
662 tf->tf_out[2] = (register_t)&sfp->sf_uc;
663 tf->tf_out[4] = (register_t)catcher;
664 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
665 /* Signal handler installed with SA_SIGINFO. */
666 tf->tf_out[1] = (register_t)&sfp->sf_si;
667
668 /* Fill in POSIX parts. */
669 sf.sf_si = ksi->ksi_info;
670 sf.sf_si.si_signo = sig; /* maybe a translated signal */
671 } else {
672 /* Old FreeBSD-style arguments. */
673 tf->tf_out[1] = ksi->ksi_code;
674 tf->tf_out[3] = (register_t)ksi->ksi_addr;
675 }
676
677 /* Copy the sigframe out to the user's stack. */
678 if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
679 suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
680 /*
681 * Something is wrong with the stack pointer.
682 * ...Kill the process.
683 */
684 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
685 PROC_LOCK(p);
686 sigexit(td, SIGILL);
687 /* NOTREACHED */
688 }
689
690 tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
691 tf->tf_tnpc = tf->tf_tpc + 4;
692 tf->tf_sp = (u_long)fp - SPOFF;
693
694 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
695 tf->tf_sp);
696
697 PROC_LOCK(p);
698 mtx_lock(&psp->ps_mtx);
699 }
700
701 #ifndef _SYS_SYSPROTO_H_
702 struct sigreturn_args {
703 ucontext_t *ucp;
704 };
705 #endif
706
707 /*
708 * MPSAFE
709 */
710 int
711 sigreturn(struct thread *td, struct sigreturn_args *uap)
712 {
713 struct proc *p;
714 mcontext_t *mc;
715 ucontext_t uc;
716 int error;
717
718 p = td->td_proc;
719 if (rwindow_save(td)) {
720 PROC_LOCK(p);
721 sigexit(td, SIGILL);
722 }
723
724 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
725 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
726 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
727 return (EFAULT);
728 }
729
730 mc = &uc.uc_mcontext;
731 error = set_mcontext(td, mc);
732 if (error != 0)
733 return (error);
734
735 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
736
737 CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
738 td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
739 return (EJUSTRETURN);
740 }
741
742 /*
743 * Construct a PCB from a trapframe. This is called from kdb_trap() where
744 * we want to start a backtrace from the function that caused us to enter
745 * the debugger. We have the context in the trapframe, but base the trace
746 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
747 * enough for a backtrace.
748 */
749 void
750 makectx(struct trapframe *tf, struct pcb *pcb)
751 {
752
753 pcb->pcb_pc = tf->tf_tpc;
754 pcb->pcb_sp = tf->tf_sp;
755 }
756
757 int
758 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
759 {
760 struct trapframe *tf;
761 struct pcb *pcb;
762
763 tf = td->td_frame;
764 pcb = td->td_pcb;
765 /*
766 * Copy the registers which will be restored by tl0_ret() from the
767 * trapframe.
768 * Note that we skip %g7 which is used as the userland TLS register
769 * and %wstate.
770 */
771 mc->mc_flags = _MC_VERSION;
772 mc->mc_global[1] = tf->tf_global[1];
773 mc->mc_global[2] = tf->tf_global[2];
774 mc->mc_global[3] = tf->tf_global[3];
775 mc->mc_global[4] = tf->tf_global[4];
776 mc->mc_global[5] = tf->tf_global[5];
777 mc->mc_global[6] = tf->tf_global[6];
778 if (flags & GET_MC_CLEAR_RET) {
779 mc->mc_out[0] = 0;
780 mc->mc_out[1] = 0;
781 } else {
782 mc->mc_out[0] = tf->tf_out[0];
783 mc->mc_out[1] = tf->tf_out[1];
784 }
785 mc->mc_out[2] = tf->tf_out[2];
786 mc->mc_out[3] = tf->tf_out[3];
787 mc->mc_out[4] = tf->tf_out[4];
788 mc->mc_out[5] = tf->tf_out[5];
789 mc->mc_out[6] = tf->tf_out[6];
790 mc->mc_out[7] = tf->tf_out[7];
791 mc->mc_fprs = tf->tf_fprs;
792 mc->mc_fsr = tf->tf_fsr;
793 mc->mc_gsr = tf->tf_gsr;
794 mc->mc_tnpc = tf->tf_tnpc;
795 mc->mc_tpc = tf->tf_tpc;
796 mc->mc_tstate = tf->tf_tstate;
797 mc->mc_y = tf->tf_y;
798 critical_enter();
799 if ((tf->tf_fprs & FPRS_FEF) != 0) {
800 savefpctx(pcb->pcb_ufp);
801 tf->tf_fprs &= ~FPRS_FEF;
802 pcb->pcb_flags |= PCB_FEF;
803 }
804 if ((pcb->pcb_flags & PCB_FEF) != 0) {
805 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
806 mc->mc_fprs |= FPRS_FEF;
807 }
808 critical_exit();
809 return (0);
810 }
811
812 int
813 set_mcontext(struct thread *td, const mcontext_t *mc)
814 {
815 struct trapframe *tf;
816 struct pcb *pcb;
817
818 if (!TSTATE_SECURE(mc->mc_tstate) ||
819 (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
820 return (EINVAL);
821 tf = td->td_frame;
822 pcb = td->td_pcb;
823 /* Make sure the windows are spilled first. */
824 flushw();
825 /*
826 * Copy the registers which will be restored by tl0_ret() to the
827 * trapframe.
828 * Note that we skip %g7 which is used as the userland TLS register
829 * and %wstate.
830 */
831 tf->tf_global[1] = mc->mc_global[1];
832 tf->tf_global[2] = mc->mc_global[2];
833 tf->tf_global[3] = mc->mc_global[3];
834 tf->tf_global[4] = mc->mc_global[4];
835 tf->tf_global[5] = mc->mc_global[5];
836 tf->tf_global[6] = mc->mc_global[6];
837 tf->tf_out[0] = mc->mc_out[0];
838 tf->tf_out[1] = mc->mc_out[1];
839 tf->tf_out[2] = mc->mc_out[2];
840 tf->tf_out[3] = mc->mc_out[3];
841 tf->tf_out[4] = mc->mc_out[4];
842 tf->tf_out[5] = mc->mc_out[5];
843 tf->tf_out[6] = mc->mc_out[6];
844 tf->tf_out[7] = mc->mc_out[7];
845 tf->tf_fprs = mc->mc_fprs;
846 tf->tf_fsr = mc->mc_fsr;
847 tf->tf_gsr = mc->mc_gsr;
848 tf->tf_tnpc = mc->mc_tnpc;
849 tf->tf_tpc = mc->mc_tpc;
850 tf->tf_tstate = mc->mc_tstate;
851 tf->tf_y = mc->mc_y;
852 if ((mc->mc_fprs & FPRS_FEF) != 0) {
853 tf->tf_fprs = 0;
854 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
855 pcb->pcb_flags |= PCB_FEF;
856 }
857 return (0);
858 }
859
860 /*
861 * Exit the kernel and execute a firmware call that will not return, as
862 * specified by the arguments.
863 */
864 void
865 cpu_shutdown(void *args)
866 {
867
868 #ifdef SMP
869 cpu_mp_shutdown();
870 #endif
871 ofw_exit(args);
872 }
873
874 /*
875 * Flush the D-cache for non-DMA I/O so that the I-cache can
876 * be made coherent later.
877 */
878 void
879 cpu_flush_dcache(void *ptr, size_t len)
880 {
881
882 /* TBD */
883 }
884
885 /* Get current clock frequency for the given CPU ID. */
886 int
887 cpu_est_clockrate(int cpu_id, uint64_t *rate)
888 {
889 struct pcpu *pc;
890
891 pc = pcpu_find(cpu_id);
892 if (pc == NULL || rate == NULL)
893 return (EINVAL);
894 *rate = pc->pc_clock;
895 return (0);
896 }
897
898 /*
899 * Duplicate OF_exit() with a different firmware call function that restores
900 * the trap table, otherwise a RED state exception is triggered in at least
901 * some firmware versions.
902 */
903 void
904 cpu_halt(void)
905 {
906 static struct {
907 cell_t name;
908 cell_t nargs;
909 cell_t nreturns;
910 } args = {
911 (cell_t)"exit",
912 0,
913 0
914 };
915
916 cpu_shutdown(&args);
917 }
918
919 static void
920 sparc64_shutdown_final(void *dummy, int howto)
921 {
922 static struct {
923 cell_t name;
924 cell_t nargs;
925 cell_t nreturns;
926 } args = {
927 (cell_t)"SUNW,power-off",
928 0,
929 0
930 };
931
932 /* Turn the power off? */
933 if ((howto & RB_POWEROFF) != 0)
934 cpu_shutdown(&args);
935 /* In case of halt, return to the firmware. */
936 if ((howto & RB_HALT) != 0)
937 cpu_halt();
938 }
939
940 void
941 cpu_idle(int busy)
942 {
943
944 /* Insert code to halt (until next interrupt) for the idle loop. */
945 }
946
947 int
948 cpu_idle_wakeup(int cpu)
949 {
950
951 return (1);
952 }
953
954 int
955 ptrace_set_pc(struct thread *td, u_long addr)
956 {
957
958 td->td_frame->tf_tpc = addr;
959 td->td_frame->tf_tnpc = addr + 4;
960 return (0);
961 }
962
963 int
964 ptrace_single_step(struct thread *td)
965 {
966
967 /* TODO; */
968 return (0);
969 }
970
971 int
972 ptrace_clear_single_step(struct thread *td)
973 {
974
975 /* TODO; */
976 return (0);
977 }
978
979 void
980 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
981 {
982 struct trapframe *tf;
983 struct pcb *pcb;
984 struct proc *p;
985 u_long sp;
986
987 /* XXX no cpu_exec */
988 p = td->td_proc;
989 p->p_md.md_sigtramp = NULL;
990 if (p->p_md.md_utrap != NULL) {
991 utrap_free(p->p_md.md_utrap);
992 p->p_md.md_utrap = NULL;
993 }
994
995 pcb = td->td_pcb;
996 tf = td->td_frame;
997 sp = rounddown(stack, 16);
998 bzero(pcb, sizeof(*pcb));
999 bzero(tf, sizeof(*tf));
1000 tf->tf_out[0] = stack;
1001 tf->tf_out[3] = p->p_sysent->sv_psstrings;
1002 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
1003 tf->tf_tnpc = entry + 4;
1004 tf->tf_tpc = entry;
1005 /*
1006 * While we could adhere to the memory model indicated in the ELF
1007 * header, it turns out that just always using TSO performs best.
1008 */
1009 tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
1010
1011 td->td_retval[0] = tf->tf_out[0];
1012 td->td_retval[1] = tf->tf_out[1];
1013 }
1014
1015 int
1016 fill_regs(struct thread *td, struct reg *regs)
1017 {
1018
1019 bcopy(td->td_frame, regs, sizeof(*regs));
1020 return (0);
1021 }
1022
1023 int
1024 set_regs(struct thread *td, struct reg *regs)
1025 {
1026 struct trapframe *tf;
1027
1028 if (!TSTATE_SECURE(regs->r_tstate))
1029 return (EINVAL);
1030 tf = td->td_frame;
1031 regs->r_wstate = tf->tf_wstate;
1032 bcopy(regs, tf, sizeof(*regs));
1033 return (0);
1034 }
1035
1036 int
1037 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1038 {
1039
1040 return (ENOSYS);
1041 }
1042
1043 int
1044 set_dbregs(struct thread *td, struct dbreg *dbregs)
1045 {
1046
1047 return (ENOSYS);
1048 }
1049
1050 int
1051 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1052 {
1053 struct trapframe *tf;
1054 struct pcb *pcb;
1055
1056 pcb = td->td_pcb;
1057 tf = td->td_frame;
1058 bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
1059 fpregs->fr_fsr = tf->tf_fsr;
1060 fpregs->fr_gsr = tf->tf_gsr;
1061 return (0);
1062 }
1063
1064 int
1065 set_fpregs(struct thread *td, struct fpreg *fpregs)
1066 {
1067 struct trapframe *tf;
1068 struct pcb *pcb;
1069
1070 pcb = td->td_pcb;
1071 tf = td->td_frame;
1072 tf->tf_fprs &= ~FPRS_FEF;
1073 bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
1074 tf->tf_fsr = fpregs->fr_fsr;
1075 tf->tf_gsr = fpregs->fr_gsr;
1076 return (0);
1077 }
1078
1079 struct md_utrap *
1080 utrap_alloc(void)
1081 {
1082 struct md_utrap *ut;
1083
1084 ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
1085 ut->ut_refcnt = 1;
1086 return (ut);
1087 }
1088
1089 void
1090 utrap_free(struct md_utrap *ut)
1091 {
1092 int refcnt;
1093
1094 if (ut == NULL)
1095 return;
1096 mtx_pool_lock(mtxpool_sleep, ut);
1097 ut->ut_refcnt--;
1098 refcnt = ut->ut_refcnt;
1099 mtx_pool_unlock(mtxpool_sleep, ut);
1100 if (refcnt == 0)
1101 free(ut, M_SUBPROC);
1102 }
1103
1104 struct md_utrap *
1105 utrap_hold(struct md_utrap *ut)
1106 {
1107
1108 if (ut == NULL)
1109 return (NULL);
1110 mtx_pool_lock(mtxpool_sleep, ut);
1111 ut->ut_refcnt++;
1112 mtx_pool_unlock(mtxpool_sleep, ut);
1113 return (ut);
1114 }
Cache object: 68e931364e415161d593182587e57b5b
|