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