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