xref: /illumos-gate/usr/src/uts/intel/os/arch_kdi.c (revision dd4eeefdb8e4583c47e28a7f315db6087931ef06)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Kernel/Debugger Interface (KDI) routines.  Called during debugger under
30  * various system states (boot, while running, while the debugger has control).
31  * Functions intended for use while the debugger has control may not grab any
32  * locks or perform any functions that assume the availability of other system
33  * services.
34  */
35 
36 #include <sys/systm.h>
37 #include <sys/x86_archext.h>
38 #include <sys/kdi_impl.h>
39 #include <sys/smp_impldefs.h>
40 #include <sys/psm_types.h>
41 #include <sys/segments.h>
42 #include <sys/archsystm.h>
43 #include <sys/controlregs.h>
44 #include <sys/trap.h>
45 #include <sys/kobj.h>
46 #include <sys/kobj_impl.h>
47 
48 static void
49 kdi_system_claim(void)
50 {
51 	psm_notifyf(PSM_DEBUG_ENTER);
52 }
53 
54 static void
55 kdi_system_release(void)
56 {
57 	psm_notifyf(PSM_DEBUG_EXIT);
58 }
59 
60 static cpu_t *
61 kdi_gdt2cpu(uintptr_t gdtbase)
62 {
63 	cpu_t *cp = cpu_list;
64 
65 	if (cp == NULL)
66 		return (NULL);
67 
68 	do {
69 		if (gdtbase == (uintptr_t)cp->cpu_gdt)
70 			return (cp);
71 	} while ((cp = cp->cpu_next) != cpu_list);
72 
73 	return (NULL);
74 }
75 
76 #if defined(__amd64)
77 uintptr_t
78 kdi_gdt2gsbase(uintptr_t gdtbase)
79 {
80 	return ((uintptr_t)kdi_gdt2cpu(gdtbase));
81 }
82 #endif
83 
84 static uintptr_t
85 kdi_get_userlimit(void)
86 {
87 	return (_userlimit);
88 }
89 
90 static int
91 kdi_get_cpuinfo(uint_t *vendorp, uint_t *familyp, uint_t *modelp)
92 {
93 	desctbr_t gdtr;
94 	cpu_t *cpu;
95 
96 	/*
97 	 * CPU doesn't work until the GDT and gs/GSBASE have been set up.
98 	 * Boot-loaded kmdb will call us well before then, so we have to
99 	 * find the current cpu_t the hard way.
100 	 */
101 	rd_gdtr(&gdtr);
102 	if ((cpu = kdi_gdt2cpu(gdtr.dtr_base)) == NULL ||
103 	    !cpuid_checkpass(cpu, 1))
104 		return (EAGAIN); /* cpuid isn't done yet */
105 
106 	*vendorp = cpuid_getvendor(cpu);
107 	*familyp = cpuid_getfamily(cpu);
108 	*modelp = cpuid_getmodel(cpu);
109 
110 	return (0);
111 }
112 
113 void
114 kdi_idtr_set(gate_desc_t *idt, size_t limit)
115 {
116 	desctbr_t idtr;
117 
118 	/*
119 	 * This rare case could happen if we entered kmdb whilst still on the
120 	 * fake CPU set up by boot_kdi_tmpinit().  We're trying to restore the
121 	 * kernel's IDT that we saved on entry, but it was from the fake cpu_t
122 	 * rather than the real IDT (which is still boot's).  It's unpleasant,
123 	 * but we just encode knowledge that it's idt0 we want to restore.
124 	 */
125 	if (idt == NULL)
126 		idt = idt0;
127 
128 	CPU->cpu_m.mcpu_idt = idt;
129 	idtr.dtr_base = (uintptr_t)idt;
130 	idtr.dtr_limit = limit;
131 	kdi_idtr_write(&idtr);
132 }
133 
134 static void
135 kdi_plat_call(void (*platfn)(void))
136 {
137 	if (platfn != NULL)
138 		platfn();
139 }
140 
141 /*
142  * On Intel, most of these are shared between i86*, so this is really an
143  * arch_kdi_init().
144  */
145 void
146 mach_kdi_init(kdi_t *kdi)
147 {
148 	kdi->kdi_plat_call = kdi_plat_call;
149 	kdi->kdi_kmdb_enter = kmdb_enter;
150 	kdi->mkdi_activate = kdi_activate;
151 	kdi->mkdi_deactivate = kdi_deactivate;
152 	kdi->mkdi_idt_switch = kdi_idt_switch;
153 	kdi->mkdi_update_drreg = kdi_update_drreg;
154 	kdi->mkdi_set_debug_msrs = kdi_set_debug_msrs;
155 	kdi->mkdi_get_userlimit = kdi_get_userlimit;
156 	kdi->mkdi_get_cpuinfo = kdi_get_cpuinfo;
157 	kdi->mkdi_stop_slaves = kdi_stop_slaves;
158 	kdi->mkdi_start_slaves = kdi_start_slaves;
159 	kdi->mkdi_slave_wait = kdi_slave_wait;
160 	kdi->mkdi_memrange_add = kdi_memrange_add;
161 	kdi->mkdi_reboot = kdi_reboot;
162 }
163 
164 void
165 plat_kdi_init(kdi_t *kdi)
166 {
167 	kdi->pkdi_system_claim = kdi_system_claim;
168 	kdi->pkdi_system_release = kdi_system_release;
169 }
170