machdep.c revision 170846
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: head/sys/sparc64/sparc64/machdep.c 170846 2007-06-16 23:26: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
112typedef int ofw_vec_t(void *);
113
114#ifdef DDB
115extern vm_offset_t ksym_start, ksym_end;
116#endif
117
118struct tlb_entry *kernel_tlbs;
119int kernel_tlb_slots;
120
121int cold = 1;
122long Maxmem;
123long realmem;
124
125char pcpu0[PCPU_PAGES * PAGE_SIZE];
126struct trapframe frame0;
127
128vm_offset_t kstack0;
129vm_paddr_t kstack0_phys;
130
131struct kva_md_info kmi;
132
133u_long ofw_vec;
134u_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
146static struct timecounter tick_tc;
147
148char sparc64_model[32];
149
150static int cpu_use_vis = 1;
151
152cpu_block_copy_t *cpu_block_copy;
153cpu_block_zero_t *cpu_block_zero;
154
155static timecounter_get_t tick_get_timecount;
156void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3,
157		  ofw_vec_t *vec);
158void sparc64_shutdown_final(void *dummy, int howto);
159
160static void cpu_startup(void *);
161SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
162
163CTASSERT((1 << INT_SHIFT) == sizeof(int));
164CTASSERT((1 << PTR_SHIFT) == sizeof(char *));
165
166CTASSERT(sizeof(struct reg) == 256);
167CTASSERT(sizeof(struct fpreg) == 272);
168CTASSERT(sizeof(struct __mcontext) == 512);
169
170CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0);
171CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0);
172CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0);
173CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8));
174
175CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2));
176
177static void
178cpu_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
223void
224cpu_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
237void
238spinlock_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
253void
254spinlock_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
265unsigned
266tick_get_timecount(struct timecounter *tc)
267{
268	return ((unsigned)rd(tick));
269}
270
271void
272sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec)
273{
274	phandle_t child;
275	phandle_t root;
276	struct pcpu *pc;
277	vm_offset_t end;
278	caddr_t kmdp;
279	u_int clock;
280	char *env;
281	char type[8];
282
283	end = 0;
284	kmdp = NULL;
285
286	/*
287	 * Find out what kind of cpu we have first, for anything that changes
288	 * behaviour.
289	 */
290	cpu_impl = VER_IMPL(rdpr(ver));
291
292	/*
293	 * Initialize Open Firmware (needed for console).
294	 */
295	OF_init(vec);
296
297	/*
298	 * Parse metadata if present and fetch parameters.  Must be before the
299	 * console is inited so cninit gets the right value of boothowto.
300	 */
301	if (mdp != NULL) {
302		preload_metadata = mdp;
303		kmdp = preload_search_by_type("elf kernel");
304		if (kmdp != NULL) {
305			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
306			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
307			end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
308			kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS,
309			    int);
310			kernel_tlbs = (void *)preload_search_info(kmdp,
311			    MODINFO_METADATA | MODINFOMD_DTLB);
312		}
313	}
314
315	init_param1();
316
317	root = OF_peer(0);
318	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
319		OF_getprop(child, "device_type", type, sizeof(type));
320		if (strcmp(type, "cpu") == 0)
321			break;
322	}
323
324	/*
325	 * Initialize the tick counter.  Must be before the console is inited
326	 * in order to provide the low-level console drivers with a working
327	 * DELAY().
328	 */
329	OF_getprop(child, "clock-frequency", &clock, sizeof(clock));
330	tick_init(clock);
331
332	/*
333	 * Initialize the console before printing anything.
334	 */
335	cninit();
336
337	/*
338	 * Panic if there is no metadata.  Most likely the kernel was booted
339	 * directly, instead of through loader(8).
340	 */
341	if (mdp == NULL || kmdp == NULL) {
342		printf("sparc64_init: no loader metadata.\n"
343		       "This probably means you are not using loader(8).\n");
344		panic("sparc64_init");
345	}
346
347	/*
348	 * Sanity check the kernel end, which is important.
349	 */
350	if (end == 0) {
351		printf("sparc64_init: warning, kernel end not specified.\n"
352		       "Attempting to continue anyway.\n");
353		end = (vm_offset_t)_end;
354	}
355
356	cache_init(child);
357	uma_set_align(cache.dc_linesize - 1);
358
359	cpu_block_copy = bcopy;
360	cpu_block_zero = bzero;
361	getenv_int("machdep.use_vis", &cpu_use_vis);
362	if (cpu_use_vis) {
363		switch (cpu_impl) {
364		case CPU_IMPL_SPARC64:
365		case CPU_IMPL_ULTRASPARCI:
366		case CPU_IMPL_ULTRASPARCII:
367		case CPU_IMPL_ULTRASPARCIIi:
368		case CPU_IMPL_ULTRASPARCIIe:
369			cpu_block_copy = spitfire_block_copy;
370			cpu_block_zero = spitfire_block_zero;
371			break;
372		}
373	}
374
375#ifdef SMP
376	mp_init();
377#endif
378
379	/*
380	 * Initialize virtual memory and calculate physmem.
381	 */
382	pmap_bootstrap(end);
383
384	/*
385	 * Initialize tunables.
386	 */
387	init_param2(physmem);
388	env = getenv("kernelname");
389	if (env != NULL) {
390		strlcpy(kernelname, env, sizeof(kernelname));
391		freeenv(env);
392	}
393
394	/*
395	 * Initialize the interrupt tables.
396	 */
397	intr_init1();
398
399	/*
400	 * Initialize proc0 stuff (p_contested needs to be done early).
401	 */
402	proc_linkup(&proc0, &thread0);
403	proc0.p_md.md_sigtramp = NULL;
404	proc0.p_md.md_utrap = NULL;
405	thread0.td_kstack = kstack0;
406	thread0.td_pcb = (struct pcb *)
407	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
408	frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV;
409	thread0.td_frame = &frame0;
410
411	/*
412	 * Prime our per-cpu data page for use.  Note, we are using it for our
413	 * stack, so don't pass the real size (PAGE_SIZE) to pcpu_init or
414	 * it'll zero it out from under us.
415	 */
416	pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1;
417	pcpu_init(pc, 0, sizeof(struct pcpu));
418	pc->pc_curthread = &thread0;
419	pc->pc_curpcb = thread0.td_pcb;
420	pc->pc_mid = UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG));
421	pc->pc_addr = (vm_offset_t)pcpu0;
422	pc->pc_node = child;
423	pc->pc_tlb_ctx = TLB_CTX_USER_MIN;
424	pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN;
425	pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX;
426
427	/*
428	 * Initialize global registers.
429	 */
430	cpu_setregs(pc);
431
432	/*
433	 * Initialize the message buffer (after setting trap table).
434	 */
435	msgbufinit(msgbufp, MSGBUF_SIZE);
436
437	mutex_init();
438	intr_init2();
439
440	/*
441	 * Finish pmap initialization now that we're ready for mutexes.
442	 */
443	PMAP_LOCK_INIT(kernel_pmap);
444
445	OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1);
446
447	kdb_init();
448
449#ifdef KDB
450	if (boothowto & RB_KDB)
451		kdb_enter("Boot flags requested debugger");
452#endif
453}
454
455void
456set_openfirm_callback(ofw_vec_t *vec)
457{
458	ofw_tba = rdpr(tba);
459	ofw_vec = (u_long)vec;
460}
461
462void
463sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
464{
465	struct trapframe *tf;
466	struct sigframe *sfp;
467	struct sigacts *psp;
468	struct sigframe sf;
469	struct thread *td;
470	struct frame *fp;
471	struct proc *p;
472	int oonstack;
473	u_long sp;
474	int sig;
475
476	oonstack = 0;
477	td = curthread;
478	p = td->td_proc;
479	PROC_LOCK_ASSERT(p, MA_OWNED);
480	sig = ksi->ksi_signo;
481	psp = p->p_sigacts;
482	mtx_assert(&psp->ps_mtx, MA_OWNED);
483	tf = td->td_frame;
484	sp = tf->tf_sp + SPOFF;
485	oonstack = sigonstack(sp);
486
487	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
488	    catcher, sig);
489
490	/* Make sure we have a signal trampoline to return to. */
491	if (p->p_md.md_sigtramp == NULL) {
492		/*
493		 * No signal trampoline... kill the process.
494		 */
495		CTR0(KTR_SIG, "sendsig: no sigtramp");
496		printf("sendsig: %s is too old, rebuild it\n", p->p_comm);
497		sigexit(td, sig);
498		/* NOTREACHED */
499	}
500
501	/* Save user context. */
502	bzero(&sf, sizeof(sf));
503	get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
504	sf.sf_uc.uc_sigmask = *mask;
505	sf.sf_uc.uc_stack = td->td_sigstk;
506	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
507	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
508
509	/* Allocate and validate space for the signal handler context. */
510	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
511	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
512		sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
513		    td->td_sigstk.ss_size - sizeof(struct sigframe));
514	} else
515		sfp = (struct sigframe *)sp - 1;
516	mtx_unlock(&psp->ps_mtx);
517	PROC_UNLOCK(p);
518
519	fp = (struct frame *)sfp - 1;
520
521	/* Translate the signal if appropriate. */
522	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
523		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
524
525	/* Build the argument list for the signal handler. */
526	tf->tf_out[0] = sig;
527	tf->tf_out[2] = (register_t)&sfp->sf_uc;
528	tf->tf_out[4] = (register_t)catcher;
529	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
530		/* Signal handler installed with SA_SIGINFO. */
531		tf->tf_out[1] = (register_t)&sfp->sf_si;
532
533		/* Fill in POSIX parts. */
534		sf.sf_si = ksi->ksi_info;
535		sf.sf_si.si_signo = sig; /* maybe a translated signal */
536	} else {
537		/* Old FreeBSD-style arguments. */
538		tf->tf_out[1] = ksi->ksi_code;
539		tf->tf_out[3] = (register_t)ksi->ksi_addr;
540	}
541
542	/* Copy the sigframe out to the user's stack. */
543	if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
544	    suword(&fp->fr_in[6], tf->tf_out[6]) != 0) {
545		/*
546		 * Something is wrong with the stack pointer.
547		 * ...Kill the process.
548		 */
549		CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
550		PROC_LOCK(p);
551		sigexit(td, SIGILL);
552		/* NOTREACHED */
553	}
554
555	tf->tf_tpc = (u_long)p->p_md.md_sigtramp;
556	tf->tf_tnpc = tf->tf_tpc + 4;
557	tf->tf_sp = (u_long)fp - SPOFF;
558
559	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc,
560	    tf->tf_sp);
561
562	PROC_LOCK(p);
563	mtx_lock(&psp->ps_mtx);
564}
565
566#ifndef	_SYS_SYSPROTO_H_
567struct sigreturn_args {
568	ucontext_t *ucp;
569};
570#endif
571
572/*
573 * MPSAFE
574 */
575int
576sigreturn(struct thread *td, struct sigreturn_args *uap)
577{
578	struct proc *p;
579	mcontext_t *mc;
580	ucontext_t uc;
581	int error;
582
583	p = td->td_proc;
584	if (rwindow_save(td)) {
585		PROC_LOCK(p);
586		sigexit(td, SIGILL);
587	}
588
589	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
590	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
591		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
592		return (EFAULT);
593	}
594
595	mc = &uc.uc_mcontext;
596	error = set_mcontext(td, mc);
597	if (error != 0)
598		return (error);
599
600	PROC_LOCK(p);
601	td->td_sigmask = uc.uc_sigmask;
602	SIG_CANTMASK(td->td_sigmask);
603	signotify(td);
604	PROC_UNLOCK(p);
605
606	CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx",
607	    td, mc->mc_tpc, mc->mc_sp, mc->mc_tstate);
608	return (EJUSTRETURN);
609}
610
611#ifdef COMPAT_FREEBSD4
612int
613freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
614{
615
616	return sigreturn(td, (struct sigreturn_args *)uap);
617}
618#endif
619
620/*
621 * Construct a PCB from a trapframe. This is called from kdb_trap() where
622 * we want to start a backtrace from the function that caused us to enter
623 * the debugger. We have the context in the trapframe, but base the trace
624 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
625 * enough for a backtrace.
626 */
627void
628makectx(struct trapframe *tf, struct pcb *pcb)
629{
630
631	pcb->pcb_pc = tf->tf_tpc;
632	pcb->pcb_sp = tf->tf_sp;
633}
634
635int
636get_mcontext(struct thread *td, mcontext_t *mc, int flags)
637{
638	struct trapframe *tf;
639	struct pcb *pcb;
640
641	tf = td->td_frame;
642	pcb = td->td_pcb;
643	bcopy(tf, mc, sizeof(*tf));
644	if (flags & GET_MC_CLEAR_RET) {
645		mc->mc_out[0] = 0;
646		mc->mc_out[1] = 0;
647	}
648	mc->mc_flags = _MC_VERSION;
649	critical_enter();
650	if ((tf->tf_fprs & FPRS_FEF) != 0) {
651		savefpctx(pcb->pcb_ufp);
652		tf->tf_fprs &= ~FPRS_FEF;
653		pcb->pcb_flags |= PCB_FEF;
654	}
655	if ((pcb->pcb_flags & PCB_FEF) != 0) {
656		bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp));
657		mc->mc_fprs |= FPRS_FEF;
658	}
659	critical_exit();
660	return (0);
661}
662
663int
664set_mcontext(struct thread *td, const mcontext_t *mc)
665{
666	struct trapframe *tf;
667	struct pcb *pcb;
668	uint64_t wstate;
669
670	if (!TSTATE_SECURE(mc->mc_tstate) ||
671	    (mc->mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION)
672		return (EINVAL);
673	tf = td->td_frame;
674	pcb = td->td_pcb;
675	/* Make sure the windows are spilled first. */
676	flushw();
677	wstate = tf->tf_wstate;
678	bcopy(mc, tf, sizeof(*tf));
679	tf->tf_wstate = wstate;
680	if ((mc->mc_fprs & FPRS_FEF) != 0) {
681		tf->tf_fprs = 0;
682		bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
683		pcb->pcb_flags |= PCB_FEF;
684	}
685	return (0);
686}
687
688/*
689 * Exit the kernel and execute a firmware call that will not return, as
690 * specified by the arguments.
691 */
692void
693cpu_shutdown(void *args)
694{
695
696#ifdef SMP
697	cpu_mp_shutdown();
698#endif
699	openfirmware_exit(args);
700}
701
702/* Get current clock frequency for the given cpu id. */
703int
704cpu_est_clockrate(int cpu_id, uint64_t *rate)
705{
706
707	return (ENXIO);
708}
709
710/*
711 * Duplicate OF_exit() with a different firmware call function that restores
712 * the trap table, otherwise a RED state exception is triggered in at least
713 * some firmware versions.
714 */
715void
716cpu_halt(void)
717{
718	static struct {
719		cell_t name;
720		cell_t nargs;
721		cell_t nreturns;
722	} args = {
723		(cell_t)"exit",
724		0,
725		0
726	};
727
728	cpu_shutdown(&args);
729}
730
731void
732sparc64_shutdown_final(void *dummy, int howto)
733{
734	static struct {
735		cell_t name;
736		cell_t nargs;
737		cell_t nreturns;
738	} args = {
739		(cell_t)"SUNW,power-off",
740		0,
741		0
742	};
743
744	/* Turn the power off? */
745	if ((howto & RB_POWEROFF) != 0)
746		cpu_shutdown(&args);
747	/* In case of halt, return to the firmware */
748	if ((howto & RB_HALT) != 0)
749		cpu_halt();
750}
751
752void
753cpu_idle(void)
754{
755	/* Insert code to halt (until next interrupt) for the idle loop */
756}
757
758int
759ptrace_set_pc(struct thread *td, u_long addr)
760{
761
762	td->td_frame->tf_tpc = addr;
763	td->td_frame->tf_tnpc = addr + 4;
764	return (0);
765}
766
767int
768ptrace_single_step(struct thread *td)
769{
770	/* TODO; */
771	return (0);
772}
773
774int
775ptrace_clear_single_step(struct thread *td)
776{
777	/* TODO; */
778	return (0);
779}
780
781void
782exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
783{
784	struct trapframe *tf;
785	struct pcb *pcb;
786	struct proc *p;
787	u_long sp;
788
789	/* XXX no cpu_exec */
790	p = td->td_proc;
791	p->p_md.md_sigtramp = NULL;
792	if (p->p_md.md_utrap != NULL) {
793		utrap_free(p->p_md.md_utrap);
794		p->p_md.md_utrap = NULL;
795	}
796
797	pcb = td->td_pcb;
798	tf = td->td_frame;
799	sp = rounddown(stack, 16);
800	bzero(pcb, sizeof(*pcb));
801	bzero(tf, sizeof(*tf));
802	tf->tf_out[0] = stack;
803	tf->tf_out[3] = p->p_sysent->sv_psstrings;
804	tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
805	tf->tf_tnpc = entry + 4;
806	tf->tf_tpc = entry;
807	tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO;
808
809	td->td_retval[0] = tf->tf_out[0];
810	td->td_retval[1] = tf->tf_out[1];
811}
812
813int
814fill_regs(struct thread *td, struct reg *regs)
815{
816
817	bcopy(td->td_frame, regs, sizeof(*regs));
818	return (0);
819}
820
821int
822set_regs(struct thread *td, struct reg *regs)
823{
824	struct trapframe *tf;
825
826	if (!TSTATE_SECURE(regs->r_tstate))
827		return (EINVAL);
828	tf = td->td_frame;
829	regs->r_wstate = tf->tf_wstate;
830	bcopy(regs, tf, sizeof(*regs));
831	return (0);
832}
833
834int
835fill_dbregs(struct thread *td, struct dbreg *dbregs)
836{
837
838	return (ENOSYS);
839}
840
841int
842set_dbregs(struct thread *td, struct dbreg *dbregs)
843{
844
845	return (ENOSYS);
846}
847
848int
849fill_fpregs(struct thread *td, struct fpreg *fpregs)
850{
851	struct trapframe *tf;
852	struct pcb *pcb;
853
854	pcb = td->td_pcb;
855	tf = td->td_frame;
856	bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs));
857	fpregs->fr_fsr = tf->tf_fsr;
858	fpregs->fr_gsr = tf->tf_gsr;
859	return (0);
860}
861
862int
863set_fpregs(struct thread *td, struct fpreg *fpregs)
864{
865	struct trapframe *tf;
866	struct pcb *pcb;
867
868	pcb = td->td_pcb;
869	tf = td->td_frame;
870	tf->tf_fprs &= ~FPRS_FEF;
871	bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp));
872	tf->tf_fsr = fpregs->fr_fsr;
873	tf->tf_gsr = fpregs->fr_gsr;
874	return (0);
875}
876
877struct md_utrap *
878utrap_alloc(void)
879{
880	struct md_utrap *ut;
881
882	ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
883	ut->ut_refcnt = 1;
884	return (ut);
885}
886
887void
888utrap_free(struct md_utrap *ut)
889{
890	int refcnt;
891
892	if (ut == NULL)
893		return;
894	mtx_pool_lock(mtxpool_sleep, ut);
895	ut->ut_refcnt--;
896	refcnt = ut->ut_refcnt;
897	mtx_pool_unlock(mtxpool_sleep, ut);
898	if (refcnt == 0)
899		free(ut, M_SUBPROC);
900}
901
902struct md_utrap *
903utrap_hold(struct md_utrap *ut)
904{
905
906	if (ut == NULL)
907		return (NULL);
908	mtx_pool_lock(mtxpool_sleep, ut);
909	ut->ut_refcnt++;
910	mtx_pool_unlock(mtxpool_sleep, ut);
911	return (ut);
912}
913