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