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