xref: /illumos-gate/usr/src/uts/i86pc/os/mach_kdi.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Kernel/Debugger Interface (KDI) routines.  Called during debugger under
31*7c478bd9Sstevel@tonic-gate  * various system states (boot, while running, while the debugger has control).
32*7c478bd9Sstevel@tonic-gate  * Functions intended for use while the debugger has control may not grab any
33*7c478bd9Sstevel@tonic-gate  * locks or perform any functions that assume the availability of other system
34*7c478bd9Sstevel@tonic-gate  * services.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/psm_types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/segments.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static void
45*7c478bd9Sstevel@tonic-gate kdi_system_claim(void)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	psm_notifyf(PSM_DEBUG_ENTER);
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static void
51*7c478bd9Sstevel@tonic-gate kdi_system_release(void)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	psm_notifyf(PSM_DEBUG_EXIT);
54*7c478bd9Sstevel@tonic-gate }
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static cpu_t *
57*7c478bd9Sstevel@tonic-gate kdi_gdt2cpu(uintptr_t gdtbase)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	cpu_t *cp = cpu_list;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
62*7c478bd9Sstevel@tonic-gate 		return (NULL);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	do {
65*7c478bd9Sstevel@tonic-gate 		if (gdtbase == (uintptr_t)cp->cpu_gdt)
66*7c478bd9Sstevel@tonic-gate 			return (cp);
67*7c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	return (NULL);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
73*7c478bd9Sstevel@tonic-gate static uintptr_t
74*7c478bd9Sstevel@tonic-gate kdi_gdt2gsbase(uintptr_t gdtbase)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	return ((uintptr_t)kdi_gdt2cpu(gdtbase));
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate #endif
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static void
81*7c478bd9Sstevel@tonic-gate kdi_cpu_iter(void (*iter)(cpu_t *, uint_t), uint_t arg)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	cpu_t *cp;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	cp = cpu_list;
88*7c478bd9Sstevel@tonic-gate 	do {
89*7c478bd9Sstevel@tonic-gate 		iter(cp, arg);
90*7c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static gate_desc_t *
96*7c478bd9Sstevel@tonic-gate curidt(void)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	desctbr_t idtdesc;
99*7c478bd9Sstevel@tonic-gate 	rd_idtr(&idtdesc);
100*7c478bd9Sstevel@tonic-gate 	return ((gate_desc_t *)idtdesc.dtr_base);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static void
104*7c478bd9Sstevel@tonic-gate kdi_idt_init_gate(gate_desc_t *gate, void (*hdlr)(void), uint_t dpl,
105*7c478bd9Sstevel@tonic-gate     int useboot)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	bzero(gate, sizeof (gate_desc_t));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
110*7c478bd9Sstevel@tonic-gate 	set_gatesegd(gate, hdlr, (useboot ? B64CODE_SEL : KCS_SEL), 0,
111*7c478bd9Sstevel@tonic-gate 	    SDT_SYSIGT, dpl);
112*7c478bd9Sstevel@tonic-gate #else
113*7c478bd9Sstevel@tonic-gate 	set_gatesegd(gate, hdlr, (useboot ? BOOTCODE_SEL : KCS_SEL), 0,
114*7c478bd9Sstevel@tonic-gate 	    SDT_SYSIGT, dpl);
115*7c478bd9Sstevel@tonic-gate #endif
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate static void
119*7c478bd9Sstevel@tonic-gate kdi_idt_read(gate_desc_t *idt, gate_desc_t *gatep, uint_t vec)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	if (idt == NULL)
122*7c478bd9Sstevel@tonic-gate 		idt = curidt();
123*7c478bd9Sstevel@tonic-gate 	*gatep = idt[vec];
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate static void
127*7c478bd9Sstevel@tonic-gate kdi_idt_write(gate_desc_t *idt, gate_desc_t *gate, uint_t vec)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	if (idt == NULL)
130*7c478bd9Sstevel@tonic-gate 		idt = curidt();
131*7c478bd9Sstevel@tonic-gate 	idt[vec] = *gate;
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static gate_desc_t *
135*7c478bd9Sstevel@tonic-gate kdi_cpu2idt(cpu_t *cp)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
138*7c478bd9Sstevel@tonic-gate 		cp = CPU;
139*7c478bd9Sstevel@tonic-gate 	return (cp->cpu_idt);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate void
143*7c478bd9Sstevel@tonic-gate kdi_flush_caches(void)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate 	reload_cr3();
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate static int
149*7c478bd9Sstevel@tonic-gate kdi_get_cpuinfo(uint_t *vendorp, uint_t *familyp, uint_t *modelp)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	desctbr_t gdtr;
152*7c478bd9Sstevel@tonic-gate 	cpu_t *cpu;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	/*
155*7c478bd9Sstevel@tonic-gate 	 * CPU doesn't work until the GDT and gs/GSBASE have been set up.
156*7c478bd9Sstevel@tonic-gate 	 * Boot-loaded kmdb will call us well before then, so we have to
157*7c478bd9Sstevel@tonic-gate 	 * find the current cpu_t the hard way.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	rd_gdtr(&gdtr);
160*7c478bd9Sstevel@tonic-gate 	if ((cpu = kdi_gdt2cpu(gdtr.dtr_base)) == NULL ||
161*7c478bd9Sstevel@tonic-gate 	    !cpuid_checkpass(cpu, 1))
162*7c478bd9Sstevel@tonic-gate 		return (EAGAIN); /* cpuid isn't done yet */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	*vendorp = cpuid_getvendor(cpu);
165*7c478bd9Sstevel@tonic-gate 	*familyp = cpuid_getfamily(cpu);
166*7c478bd9Sstevel@tonic-gate 	*modelp = cpuid_getmodel(cpu);
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	return (0);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate static void
172*7c478bd9Sstevel@tonic-gate kdi_plat_call(void (*platfn)(void))
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	if (platfn != NULL)
175*7c478bd9Sstevel@tonic-gate 		platfn();
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate void
179*7c478bd9Sstevel@tonic-gate mach_kdi_init(kdi_t *kdi)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	kdi->kdi_plat_call = kdi_plat_call;
182*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_get_cpuinfo = kdi_get_cpuinfo;
183*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_xc_initialized = kdi_xc_initialized;
184*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_xc_others = kdi_xc_others;
185*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_get_userlimit = kdi_get_userlimit;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_idt_init_gate = kdi_idt_init_gate;
188*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_idt_read = kdi_idt_read;
189*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_idt_write = kdi_idt_write;
190*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_cpu2idt = kdi_cpu2idt;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_shutdownp = &psm_shutdownf;
193*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
194*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_gdt2gsbase = &kdi_gdt2gsbase;
195*7c478bd9Sstevel@tonic-gate #endif
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_cpu_iter = kdi_cpu_iter;
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
201*7c478bd9Sstevel@tonic-gate void
202*7c478bd9Sstevel@tonic-gate mach_kdi_fini(kdi_t *kdi)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	hat_kdi_fini();
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate void
208*7c478bd9Sstevel@tonic-gate plat_kdi_init(kdi_t *kdi)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	kdi->pkdi_system_claim = kdi_system_claim;
211*7c478bd9Sstevel@tonic-gate 	kdi->pkdi_system_release = kdi_system_release;
212*7c478bd9Sstevel@tonic-gate }
213