xref: /onnv-gate/usr/src/uts/i86pc/os/mach_kdi.c (revision 3446:5903aece022d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3446Smrj  * Common Development and Distribution License (the "License").
6*3446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Kernel/Debugger Interface (KDI) routines.  Called during debugger under
300Sstevel@tonic-gate  * various system states (boot, while running, while the debugger has control).
310Sstevel@tonic-gate  * Functions intended for use while the debugger has control may not grab any
320Sstevel@tonic-gate  * locks or perform any functions that assume the availability of other system
330Sstevel@tonic-gate  * services.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/x86_archext.h>
380Sstevel@tonic-gate #include <sys/kdi_impl.h>
390Sstevel@tonic-gate #include <sys/smp_impldefs.h>
400Sstevel@tonic-gate #include <sys/psm_types.h>
410Sstevel@tonic-gate #include <sys/segments.h>
42*3446Smrj #include <sys/archsystm.h>
43*3446Smrj #include <sys/controlregs.h>
44*3446Smrj #include <sys/trap.h>
45*3446Smrj #include <sys/kobj.h>
46*3446Smrj #include <sys/kobj_impl.h>
47*3446Smrj #include <sys/mach_mmu.h>
480Sstevel@tonic-gate 
49*3446Smrj void
kdi_idt_write(gate_desc_t * gate,uint_t vec)50*3446Smrj kdi_idt_write(gate_desc_t *gate, uint_t vec)
510Sstevel@tonic-gate {
52*3446Smrj 	gate_desc_t *idt = CPU->cpu_m.mcpu_idt;
530Sstevel@tonic-gate 
54*3446Smrj 	/*
55*3446Smrj 	 * See kdi_idtr_set().
56*3446Smrj 	 */
57*3446Smrj 	if (idt == NULL) {
58*3446Smrj 		desctbr_t idtr;
59*3446Smrj 		rd_idtr(&idtr);
60*3446Smrj 		idt = (gate_desc_t *)idtr.dtr_base;
61*3446Smrj 	}
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	idt[vec] = *gate;
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
66*3446Smrj ulong_t
kdi_dreg_get(int reg)67*3446Smrj kdi_dreg_get(int reg)
680Sstevel@tonic-gate {
69*3446Smrj 	switch (reg) {
70*3446Smrj 	case 0:
71*3446Smrj 		return (kdi_getdr0());
72*3446Smrj 	case 1:
73*3446Smrj 		return (kdi_getdr1());
74*3446Smrj 	case 2:
75*3446Smrj 		return (kdi_getdr2());
76*3446Smrj 	case 3:
77*3446Smrj 		return (kdi_getdr3());
78*3446Smrj 	case 6:
79*3446Smrj 		return (kdi_getdr6());
80*3446Smrj 	case 7:
81*3446Smrj 		return (kdi_getdr7());
82*3446Smrj 	default:
83*3446Smrj 		panic("invalid debug register dr%d", reg);
84*3446Smrj 		/*NOTREACHED*/
85*3446Smrj 	}
86*3446Smrj }
87*3446Smrj 
88*3446Smrj void
kdi_dreg_set(int reg,ulong_t value)89*3446Smrj kdi_dreg_set(int reg, ulong_t value)
90*3446Smrj {
91*3446Smrj 	switch (reg) {
92*3446Smrj 	case 0:
93*3446Smrj 		kdi_setdr0(value);
94*3446Smrj 		break;
95*3446Smrj 	case 1:
96*3446Smrj 		kdi_setdr1(value);
97*3446Smrj 		break;
98*3446Smrj 	case 2:
99*3446Smrj 		kdi_setdr2(value);
100*3446Smrj 		break;
101*3446Smrj 	case 3:
102*3446Smrj 		kdi_setdr3(value);
103*3446Smrj 		break;
104*3446Smrj 	case 6:
105*3446Smrj 		kdi_setdr6(value);
106*3446Smrj 		break;
107*3446Smrj 	case 7:
108*3446Smrj 		kdi_setdr7(value);
109*3446Smrj 		break;
110*3446Smrj 	default:
111*3446Smrj 		panic("invalid debug register dr%d", reg);
112*3446Smrj 		/*NOTREACHED*/
113*3446Smrj 	}
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate void
kdi_flush_caches(void)1170Sstevel@tonic-gate kdi_flush_caches(void)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	reload_cr3();
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
122*3446Smrj extern void kdi_slave_entry(void);
1230Sstevel@tonic-gate 
124*3446Smrj void
kdi_stop_slaves(int cpu,int doxc)125*3446Smrj kdi_stop_slaves(int cpu, int doxc)
126*3446Smrj {
127*3446Smrj 	if (doxc)
128*3446Smrj 		kdi_xc_others(cpu, kdi_slave_entry);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
131*3446Smrj /*
132*3446Smrj  * On i86pc, slaves busy-loop, so we don't need to do anything here.
133*3446Smrj  */
134*3446Smrj void
kdi_start_slaves(void)135*3446Smrj kdi_start_slaves(void)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate void
kdi_slave_wait(void)140*3446Smrj kdi_slave_wait(void)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
144*3446Smrj /*
145*3446Smrj  * Caution.
146*3446Smrj  * These routines are called -extremely- early, during kmdb initialization.
147*3446Smrj  *
148*3446Smrj  * Many common kernel functions assume that %gs has been initialized,
149*3446Smrj  * and fail horribly if it hasn't.  At this point, the boot code has
150*3446Smrj  * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
151*3446Smrj  * to point at a dummy cpu_t, temporarily at least.
152*3446Smrj  *
153*3446Smrj  * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
154*3446Smrj  */
155*3446Smrj 
156*3446Smrj #if defined(__amd64)
157*3446Smrj 
158*3446Smrj void *
boot_kdi_tmpinit(void)159*3446Smrj boot_kdi_tmpinit(void)
1600Sstevel@tonic-gate {
161*3446Smrj 	cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
162*3446Smrj 	uintptr_t old;
163*3446Smrj 
164*3446Smrj 	cpu->cpu_self = cpu;
165*3446Smrj 
166*3446Smrj 	old = (uintptr_t)rdmsr(MSR_AMD_GSBASE);
167*3446Smrj 	wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu);
168*3446Smrj 	return ((void *)old);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate void
boot_kdi_tmpfini(void * old)172*3446Smrj boot_kdi_tmpfini(void *old)
173*3446Smrj {
174*3446Smrj 	wrmsr(MSR_AMD_GSBASE, (uint64_t)old);
175*3446Smrj }
176*3446Smrj 
177*3446Smrj #elif defined(__i386)
178*3446Smrj 
179*3446Smrj void *
boot_kdi_tmpinit(void)180*3446Smrj boot_kdi_tmpinit(void)
1810Sstevel@tonic-gate {
182*3446Smrj 	cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
183*3446Smrj 	uintptr_t old;
184*3446Smrj 	desctbr_t b_gdtr;
185*3446Smrj 	user_desc_t *bgdt;
186*3446Smrj 
187*3446Smrj 	cpu->cpu_self = cpu;
188*3446Smrj 
189*3446Smrj 	rd_gdtr(&b_gdtr);
190*3446Smrj 	bgdt = (user_desc_t *)(b_gdtr.dtr_base);
191*3446Smrj 
192*3446Smrj 	set_usegd(&bgdt[GDT_BGSTMP],
193*3446Smrj 	    cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32);
194*3446Smrj 
195*3446Smrj 	/*
196*3446Smrj 	 * Now switch %gs to point at it.
197*3446Smrj 	 */
198*3446Smrj 	old = getgs();
199*3446Smrj 	setgs(KMDBGS_SEL);
200*3446Smrj 
201*3446Smrj 	return ((void *)old);
2020Sstevel@tonic-gate }
203*3446Smrj 
204*3446Smrj void
boot_kdi_tmpfini(void * old)205*3446Smrj boot_kdi_tmpfini(void *old)
206*3446Smrj {
207*3446Smrj 	setgs((uintptr_t)old);
208*3446Smrj }
209*3446Smrj 
210*3446Smrj #endif	/* __i386 */
211