1 /*-
2 * Copyright (c) 2001 Jake Burkholder.
3 * Copyright (c) 1992 Terrence R. Lambert.
4 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
35 * from: FreeBSD: src/sys/i386/i386/machdep.c,v 1.477 2001/08/27
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_compat.h"
42 #include "opt_ddb.h"
43 #include "opt_kstack_pages.h"
44 #include "opt_msgbuf.h"
45
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 #include <sys/systm.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/bus.h>
53 #include <sys/cpu.h>
54 #include <sys/cons.h>
55 #include <sys/eventhandler.h>
56 #include <sys/exec.h>
57 #include <sys/imgact.h>
58 #include <sys/interrupt.h>
59 #include <sys/kdb.h>
60 #include <sys/kernel.h>
61 #include <sys/ktr.h>
62 #include <sys/linker.h>
63 #include <sys/lock.h>
64 #include <sys/msgbuf.h>
65 #include <sys/mutex.h>
66 #include <sys/pcpu.h>
67 #include <sys/ptrace.h>
68 #include <sys/reboot.h>
69 #include <sys/signalvar.h>
70 #include <sys/smp.h>
71 #include <sys/sysent.h>
72 #include <sys/sysproto.h>
73 #include <sys/timetc.h>
74 #include <sys/ucontext.h>
75
76 #include <dev/ofw/openfirm.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_param.h>
86
87 #include <ddb/ddb.h>
88
89 #include <machine/bus.h>
90 #include <machine/cache.h>
91 #include <machine/clock.h>
92 #include <machine/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 struct tlb_entry *kernel_tlbs;
119 int kernel_tlb_slots;
120
121 int cold = 1;
122 long Maxmem;
123 long realmem;
124
125 char pcpu0[PCPU_PAGES * PAGE_SIZE];
126 struct trapframe frame0;
127
128 vm_offset_t kstack0;
129 vm_paddr_t kstack0_phys;
130
131 struct kva_md_info kmi;
132
133 u_long ofw_vec;
134 u_long ofw_tba;
135
136 /*
137 * Note: timer quality for CPU's is set low to try and prevent them from
138 * being chosen as the primary timecounter. The CPU counters are not
139 * synchronized among the CPU's so in MP machines this causes problems
140 * when calculating the time. With this value the CPU's should only be
141 * chosen as the primary timecounter as a last resort.
142 */
143
144 #define UP_TICK_QUALITY 1000
145 #define MP_TICK_QUALITY -100
146 static struct timecounter tick_tc;
147
148 char sparc64_model[32];
149
150 static int cpu_use_vis = 1;
151
152 cpu_block_copy_t *cpu_block_copy;
153 cpu_block_zero_t *cpu_block_zero;
154
155 static timecounter_get_t tick_get_timecount;
156 void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3,
157 ofw_vec_t *vec);
158 void sparc64_shutdown_final(void *dummy, int howto);
159
160 static void cpu_startup(void *);
161 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
162
163 CTASSERT((1 << INT_SHIFT) == sizeof(int));
164 CTASSERT((1 << PTR_SHIFT) == sizeof(char *));
165
166 CTASSERT(sizeof(struct reg) == 256);
167 CTASSERT(sizeof(struct fpreg) == 272);
168 CTASSERT(sizeof(struct __mcontext) == 512);
169
170 CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0);
171 CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0);
172 CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0);
173 CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8));
174
175 CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2));
176
177 static void
178 cpu_startup(void *arg)
179 {
180 vm_paddr_t physsz;
181 int i;
182
183 tick_tc.tc_get_timecount = tick_get_timecount;
184 tick_tc.tc_poll_pps = NULL;
185 tick_tc.tc_counter_mask = ~0u;
186 tick_tc.tc_frequency = tick_freq;
187 tick_tc.tc_name = "tick";
188 tick_tc.tc_quality = UP_TICK_QUALITY;
189 #ifdef SMP
190 /*
191 * We do not know if each CPU's tick counter is synchronized.
192 */
193 if (cpu_mp_probe())
194 tick_tc.tc_quality = MP_TICK_QUALITY;
195 #endif
196
197 tc_init(&tick_tc);
198
199 physsz = 0;
200 for (i = 0; i < sparc64_nmemreg; i++)
201 physsz += sparc64_memreg[i].mr_size;
202 printf("real memory = %lu (%lu MB)\n", physsz,
203 physsz / (1024 * 1024));
204 realmem = (long)physsz / PAGE_SIZE;
205
206 vm_ksubmap_init(&kmi);
207
208 bufinit();
209 vm_pager_bufferinit();
210
211 EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
212 SHUTDOWN_PRI_LAST);
213
214 printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
215 cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
216
217 if (bootverbose)
218 printf("machine: %s\n", sparc64_model);
219
220 cpu_identify(rdpr(ver), tick_freq, PCPU_GET(cpuid));
221 }
222
223 void
224 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
225 {
226 struct intr_request *ir;
227 int i;
228
229 pcpu->pc_irtail = &pcpu->pc_irhead;
230 for (i = 0; i < IR_FREE; i++) {
231 ir = &pcpu->pc_irpool[i];
232 ir->ir_next = pcpu->pc_irfree;
233 pcpu->pc_irfree = ir;
234 }
235 }
236
237 void
238 spinlock_enter(void)
239 {
240 struct thread *td;
241 register_t pil;
242
243 td = curthread;
244 if (td->td_md.md_spinlock_count == 0) {
245 pil = rdpr(pil);
246 wrpr(pil, 0, PIL_TICK);
247 td->td_md.md_saved_pil = pil;
248 }
249 td->td_md.md_spinlock_count++;
250 critical_enter();
251 }
252
253 void
254 spinlock_exit(void)
255 {
256 struct thread *td;
257
258 td = curthread;
259 critical_exit();
260 td->td_md.md_spinlock_count--;
261 if (td->td_md.md_spinlock_count == 0)
262 wrpr(pil, td->td_md.md_saved_pil, 0);
263 }
264
265 unsigned
266 tick_get_timecount(struct timecounter *tc)
267 {
268 return ((unsigned)rd(tick));
269 }
270
271 void
272 sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
273 {
274 char type[8];
275 char *env;
276 struct pcpu *pc;
277 vm_offset_t end;
278 vm_offset_t va;
279 caddr_t kmdp;
280 phandle_t child;
281 phandle_t root;
282 u_int clock;
283
284 end = 0;
285 kmdp = NULL;
286
287 /*
288 * Find out what kind of CPU we have first, for anything that changes
289 * behaviour.
290 */
291 cpu_impl = VER_IMPL(rdpr(ver));
292
293 /*
294 * Initialize Open Firmware (needed for console).
295 */
296 OF_init(vec);
297
298 /*
299 * Parse metadata if present and fetch parameters. Must be before the
300 * console is inited so cninit gets the right value of boothowto.
301 */
302 if (mdp != NULL) {
303 preload_metadata = mdp;
304 kmdp = preload_search_by_type("elf kernel");
305 if (kmdp != NULL) {
306 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
307 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
308 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
309 kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
310 int);
311 kernel_tlbs = (void *)preload_search_info(kmdp,
312 MODINFO_METADATA | MODINFOMD_DTLB);
313 }
314 }
315
316 init_param1();
317
318 root = OF_peer(0);
319 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
320 OF_getprop(child, "device_type", type, sizeof(type));
321 if (strcmp(type, "cpu") == 0)
322 break;
323 }
324
325 /*
326 * Initialize the tick counter. Must be before the console is inited
327 * in order to provide the low-level console drivers with a working
328 * DELAY().
329 */
330 OF_getprop(child, "clock-frequency", &clock, sizeof(clock));
331 tick_init(clock);
332
333 /*
334 * Initialize the console before printing anything.
335 */
336 cninit();
337
338 /*
339 * Panic if there is no metadata. Most likely the kernel was booted
340 * directly, instead of through loader(8).
341 */
342 if (mdp == NULL || kmdp == NULL || end == 0 ||
343 kernel_tlb_slots == 0 || kernel_tlbs == NULL) {
344 printf("sparc64_init: missing loader metadata.\n"
345 "This probably means you are not using loader(8).\n");
346 panic("sparc64_init");
347 }
348
349 /*
350 * Work around the broken loader behavior of not demapping no
351 * longer used kernel TLB slots when unloading the kernel or
352 * modules.
353 */
354 for (va = KERNBASE + (kernel_tlb_slots - 1) * PAGE_SIZE_4M;
355 va >= roundup2(end, PAGE_SIZE_4M); va -= PAGE_SIZE_4M) {
356 printf("demapping unused kernel TLB slot (va %#lx - %#lx)\n",
357 va, va + PAGE_SIZE_4M - 1);
358 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
359 ASI_DMMU_DEMAP, 0);
360 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE,
361 ASI_IMMU_DEMAP, 0);
362 flush(KERNBASE);
363 kernel_tlb_slots--;
364 }
365
366 cache_init(child);
367
368 getenv_int("machdep.use_vis", &cpu_use_vis);
369 if (cpu_use_vis) {
370 cpu_block_copy = spitfire_block_copy;
371 cpu_block_zero = spitfire_block_zero;
372 } else {
373 cpu_block_copy = bcopy;
374 cpu_block_zero = bzero;
375 }
376
377 #ifdef SMP
378 mp_tramp = mp_tramp_alloc();
379 #endif
380
381 /*
382 * Initialize virtual memory and calculate physmem.
383 */
384 pmap_bootstrap();
385
386 /*
387 * Initialize tunables.
388 */
389 init_param2(physmem);
390 env = getenv("kernelname");
391 if (env != NULL) {
392 strlcpy(kernelname, env, sizeof(kernelname));
393 freeenv(env);
394 }
395
396 /*
397 * Initialize the interrupt tables.
398 */
399 intr_init1();
400
401 /*
402 * Initialize proc0 stuff (p_contested needs to be done early).
403 */
404 proc_linkup(&proc0, &ksegrp0, &thread0);
405 proc0.p_md.md_sigtramp = NULL;
406 proc0.p_md.md_utrap = NULL;
407 thread0.td_kstack = kstack0;
408 thread0.td_pcb = (struct pcb *)
409 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
410 frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
411 thread0.td_frame = &frame0;
412
413 /*
414 * Prime our per-cpu data page for use. Note, we are using it for our
415 * stack, so don't pass the real size (PAGE_SIZE) to pcpu_init or
416 * it'll zero it out from under us.
417 */
418 pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
419 pcpu_init(pc, 0, sizeof(struct pcpu));
420 pc->pc_curthread = &thread0;
421 pc->pc_curpcb = thread0.td_pcb;
422 pc->pc_mid = UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG));
423 pc->pc_addr = (vm_offset_t)pcpu0;
424 pc->pc_node = child;
425 pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
426 pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
427 pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
428
429 /*
430 * Initialize global registers.
431 */
432 cpu_setregs(pc);
433
434 /*
435 * Initialize the message buffer (after setting trap table).
436 */
437 msgbufinit(msgbufp, MSGBUF_SIZE);
438
439 mutex_init();
440 intr_init2();
441
442 /*
443 * Finish pmap initialization now that we're ready for mutexes.
444 */
445 PMAP_LOCK_INIT(kernel_pmap);
446
447 OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
448
449 kdb_init();
450
451 #ifdef KDB
452 if (boothowto & RB_KDB)
453 kdb_enter("Boot flags requested debugger");
454 #endif
455 }
456
457 void
458 set_openfirm_callback(ofw_vec_t *vec)
459 {
460
461 ofw_tba = rdpr(tba);
462 ofw_vec = (u_long)vec;
463 }
464
465 void
466 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
467 {
468 struct trapframe *tf;
469 struct sigframe *sfp;
470 struct sigacts *psp;
471 struct sigframe sf;
472 struct thread *td;
473 struct frame *fp;
474 struct proc *p;
475 u_long sp;
476 int oonstack;
477 register_t addr;
478
479 oonstack = 0;
480 td = curthread;
481 p = td->td_proc;
482 PROC_LOCK_ASSERT(p, MA_OWNED);
483 psp = p->p_sigacts;
484 mtx_assert(&psp->ps_mtx, MA_OWNED);
485 tf = td->td_frame;
486 sp = tf->tf_sp + SPOFF;
487 oonstack = sigonstack(sp);
488
489 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
490 catcher, sig);
491
492 /* Make sure we have a signal trampoline to return to. */
493 if (p->p_md.md_sigtramp == NULL) {
494 /*
495 * No signal trampoline... kill the process.
496 */
497 CTR0(KTR_SIG, "sendsig: no sigtramp");
498 printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
499 sigexit(td, sig);
500 /* NOTREACHED */
501 }
502
503 /* Save user context. */
504 bzero(&sf, sizeof(sf));
505 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
506 sf.sf_uc.uc_sigmask = *mask;
507 sf.sf_uc.uc_stack = td->td_sigstk;
508 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
509 ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
510
511 /* Allocate and validate space for the signal handler context. */
512 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
513 SIGISMEMBER(psp->ps_sigonstack, sig)) {
514 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
515 td->td_sigstk.ss_size - sizeof(struct sigframe));
516 } else
517 sfp = (struct sigframe *)sp - 1;
518 mtx_unlock(&psp->ps_mtx);
519 PROC_UNLOCK(p);
520
521 fp = (struct frame *)sfp - 1;
522
523 /* Translate the signal if appropriate. */
524 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
525 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
526
527 /* Build the argument list for the signal handler. */
528 tf->tf_out[0] = sig;
529 tf->tf_out[2] = (register_t)&sfp->sf_uc;
530 tf->tf_out[4] = (register_t)catcher;
531 if (tf->tf_type == T_DATA_MISS || tf->tf_type == T_DATA_PROTECTION)
532 addr = tf->tf_sfar;
533 else
534 addr = tf->tf_tpc;
535 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
536 /* Signal handler installed with SA_SIGINFO. */
537 tf->tf_out[1] = (register_t)&sfp->sf_si;
538
539 /* Fill in POSIX parts. */
540 sf.sf_si.si_signo = sig; /* maybe a translated signal */
541 sf.sf_si.si_code = code;
542 sf.sf_si.si_addr = (void *)addr;
543 } else {
544 /* Old FreeBSD-style arguments. */
545 tf->tf_out[1] = code;
546 tf->tf_out[3] = addr;
547 }
548
549 /* Copy the sigframe out to the user's stack. */
550 if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
551 suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
552 /*
553 * Something is wrong with the stack pointer.
554 * ...Kill the process.
555 */
556 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
557 PROC_LOCK(p);
558 sigexit(td, SIGILL);
559 /* NOTREACHED */
560 }
561
562 tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
563 tf->tf_tnpc = tf->tf_tpc + 4;
564 tf->tf_sp = (u_long)fp - SPOFF;
565
566 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
567 tf->tf_sp);
568
569 PROC_LOCK(p);
570 mtx_lock(&psp->ps_mtx);
571 }
572
573 /*
574 * Build siginfo_t for SA thread
575 */
576 void
577 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
578 {
579 struct proc *p;
580 struct thread *td;
581 struct trapframe *tf;
582
583 td = curthread;
584 p = td->td_proc;
585 PROC_LOCK_ASSERT(p, MA_OWNED);
586 tf = td->td_frame;
587
588 bzero(si, sizeof(*si));
589 si->si_signo = sig;
590 si->si_code = code;
591 if (tf->tf_type == T_DATA_MISS || tf->tf_type == T_DATA_PROTECTION)
592 si->si_addr = (void *)tf->tf_sfar;
593 else
594 si->si_addr = (void *)tf->tf_tpc;
595 /* XXXKSE fill other fields */
596 }
597
598 #ifndef _SYS_SYSPROTO_H_
599 struct sigreturn_args {
600 ucontext_t *ucp;
601 };
602 #endif
603
604 /*
605 * MPSAFE
606 */
607 int
608 sigreturn(struct thread *td, struct sigreturn_args *uap)
609 {
610 struct proc *p;
611 mcontext_t *mc;
612 ucontext_t uc;
613 int error;
614
615 p = td->td_proc;
616 if (rwindow_save(td)) {
617 PROC_LOCK(p);
618 sigexit(td, SIGILL);
619 }
620
621 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
622 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
623 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
624 return (EFAULT);
625 }
626
627 mc = &uc.uc_mcontext;
628 error = set_mcontext(td, mc);
629 if (error != 0)
630 return (error);
631
632 PROC_LOCK(p);
633 td->td_sigmask = uc.uc_sigmask;
634 SIG_CANTMASK(td->td_sigmask);
635 signotify(td);
636 PROC_UNLOCK(p);
637
638 CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
639 td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
640 return (EJUSTRETURN);
641 }
642
643 #ifdef COMPAT_FREEBSD4
644 int
645 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
646 {
647
648 return sigreturn(td, (struct sigreturn_args *)uap);
649 }
650 #endif
651
652 /*
653 * Construct a PCB from a trapframe. This is called from kdb_trap() where
654 * we want to start a backtrace from the function that caused us to enter
655 * the debugger. We have the context in the trapframe, but base the trace
656 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
657 * enough for a backtrace.
658 */
659 void
660 makectx(struct trapframe *tf, struct pcb *pcb)
661 {
662
663 pcb->pcb_pc = tf->tf_tpc;
664 pcb->pcb_sp = tf->tf_sp;
665 }
666
667 int
668 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
669 {
670 struct trapframe *tf;
671 struct pcb *pcb;
672
673 tf = td->td_frame;
674 pcb = td->td_pcb;
675 bcopy(tf, mc, sizeof(*tf));
676 if (flags & GET_MC_CLEAR_RET) {
677 mc->mc_out[0] = 0;
678 mc->mc_out[1] = 0;
679 }
680 mc->mc_flags = _MC_VERSION;
681 critical_enter();
682 if ((tf->tf_fprs & FPRS_FEF) != 0) {
683 savefpctx(pcb->pcb_ufp);
684 tf->tf_fprs &= ~FPRS_FEF;
685 pcb->pcb_flags |= PCB_FEF;
686 }
687 if ((pcb->pcb_flags & PCB_FEF) != 0) {
688 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
689 mc->mc_fprs |= FPRS_FEF;
690 }
691 critical_exit();
692 return (0);
693 }
694
695 int
696 set_mcontext(struct thread *td, const mcontext_t *mc)
697 {
698 struct trapframe *tf;
699 struct pcb *pcb;
700 uint64_t wstate;
701
702 if (!TSTATE_SECURE(mc->mc_tstate) ||
703 (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
704 return (EINVAL);
705 tf = td->td_frame;
706 pcb = td->td_pcb;
707 /* Make sure the windows are spilled first. */
708 flushw();
709 wstate = tf->tf_wstate;
710 bcopy(mc, tf, sizeof(*tf));
711 tf->tf_wstate = wstate;
712 if ((mc->mc_fprs & FPRS_FEF) != 0) {
713 tf->tf_fprs = 0;
714 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
715 pcb->pcb_flags |= PCB_FEF;
716 }
717 return (0);
718 }
719
720 /*
721 * Exit the kernel and execute a firmware call that will not return, as
722 * specified by the arguments.
723 */
724 void
725 cpu_shutdown(void *args)
726 {
727
728 #ifdef SMP
729 cpu_mp_shutdown();
730 #endif
731 openfirmware_exit(args);
732 }
733
734 /* Get current clock frequency for the given CPU ID. */
735 int
736 cpu_est_clockrate(int cpu_id, uint64_t *rate)
737 {
738
739 return (ENXIO);
740 }
741
742 /*
743 * Duplicate OF_exit() with a different firmware call function that restores
744 * the trap table, otherwise a RED state exception is triggered in at least
745 * some firmware versions.
746 */
747 void
748 cpu_halt(void)
749 {
750 static struct {
751 cell_t name;
752 cell_t nargs;
753 cell_t nreturns;
754 } args = {
755 (cell_t)"exit",
756 0,
757 0
758 };
759
760 cpu_shutdown(&args);
761 }
762
763 void
764 sparc64_shutdown_final(void *dummy, int howto)
765 {
766 static struct {
767 cell_t name;
768 cell_t nargs;
769 cell_t nreturns;
770 } args = {
771 (cell_t)"SUNW,power-off",
772 0,
773 0
774 };
775
776 /* Turn the power off? */
777 if ((howto & RB_POWEROFF) != 0)
778 cpu_shutdown(&args);
779 /* In case of halt, return to the firmware */
780 if ((howto & RB_HALT) != 0)
781 cpu_halt();
782 }
783
784 void
785 cpu_idle(void)
786 {
787
788 /* Insert code to halt (until next interrupt) for the idle loop. */
789 }
790
791 int
792 ptrace_set_pc(struct thread *td, u_long addr)
793 {
794
795 td->td_frame->tf_tpc = addr;
796 td->td_frame->tf_tnpc = addr + 4;
797 return (0);
798 }
799
800 int
801 ptrace_single_step(struct thread *td)
802 {
803
804 /* TODO; */
805 return (0);
806 }
807
808 int
809 ptrace_clear_single_step(struct thread *td)
810 {
811
812 /* TODO; */
813 return (0);
814 }
815
816 void
817 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
818 {
819 struct trapframe *tf;
820 struct pcb *pcb;
821 struct proc *p;
822 u_long sp;
823
824 /* XXX no cpu_exec */
825 p = td->td_proc;
826 p->p_md.md_sigtramp = NULL;
827 if (p->p_md.md_utrap != NULL) {
828 utrap_free(p->p_md.md_utrap);
829 p->p_md.md_utrap = NULL;
830 }
831
832 pcb = td->td_pcb;
833 tf = td->td_frame;
834 sp = rounddown(stack, 16);
835 bzero(pcb, sizeof(*pcb));
836 bzero(tf, sizeof(*tf));
837 tf->tf_out[0] = stack;
838 tf->tf_out[3] = p->p_sysent->sv_psstrings;
839 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
840 tf->tf_tnpc = entry + 4;
841 tf->tf_tpc = entry;
842 tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
843
844 td->td_retval[0] = tf->tf_out[0];
845 td->td_retval[1] = tf->tf_out[1];
846 }
847
848 int
849 fill_regs(struct thread *td, struct reg *regs)
850 {
851
852 bcopy(td->td_frame, regs, sizeof(*regs));
853 return (0);
854 }
855
856 int
857 set_regs(struct thread *td, struct reg *regs)
858 {
859 struct trapframe *tf;
860
861 if (!TSTATE_SECURE(regs->r_tstate))
862 return (EINVAL);
863 tf = td->td_frame;
864 regs->r_wstate = tf->tf_wstate;
865 bcopy(regs, tf, sizeof(*regs));
866 return (0);
867 }
868
869 int
870 fill_dbregs(struct thread *td, struct dbreg *dbregs)
871 {
872
873 return (ENOSYS);
874 }
875
876 int
877 set_dbregs(struct thread *td, struct dbreg *dbregs)
878 {
879
880 return (ENOSYS);
881 }
882
883 int
884 fill_fpregs(struct thread *td, struct fpreg *fpregs)
885 {
886 struct trapframe *tf;
887 struct pcb *pcb;
888
889 pcb = td->td_pcb;
890 tf = td->td_frame;
891 bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
892 fpregs->fr_fsr = tf->tf_fsr;
893 fpregs->fr_gsr = tf->tf_gsr;
894 return (0);
895 }
896
897 int
898 set_fpregs(struct thread *td, struct fpreg *fpregs)
899 {
900 struct trapframe *tf;
901 struct pcb *pcb;
902
903 pcb = td->td_pcb;
904 tf = td->td_frame;
905 tf->tf_fprs &= ~FPRS_FEF;
906 bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
907 tf->tf_fsr = fpregs->fr_fsr;
908 tf->tf_gsr = fpregs->fr_gsr;
909 return (0);
910 }
911
912 struct md_utrap *
913 utrap_alloc(void)
914 {
915 struct md_utrap *ut;
916
917 ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
918 ut->ut_refcnt = 1;
919 return (ut);
920 }
921
922 void
923 utrap_free(struct md_utrap *ut)
924 {
925 int refcnt;
926
927 if (ut == NULL)
928 return;
929 mtx_pool_lock(mtxpool_sleep, ut);
930 ut->ut_refcnt--;
931 refcnt = ut->ut_refcnt;
932 mtx_pool_unlock(mtxpool_sleep, ut);
933 if (refcnt == 0)
934 free(ut, M_SUBPROC);
935 }
936
937 struct md_utrap *
938 utrap_hold(struct md_utrap *ut)
939 {
940
941 if (ut == NULL)
942 return (NULL);
943 mtx_pool_lock(mtxpool_sleep, ut);
944 ut->ut_refcnt++;
945 mtx_pool_unlock(mtxpool_sleep, ut);
946 return (ut);
947 }
Cache object: 5feb26fe9bf2d7a3822cab99219f1ad2
|