17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*ae115bc7Smrj * Common Development and Distribution License (the "License").
6*ae115bc7Smrj * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Kernel/Debugger Interface (KDI) routines. Called during debugger under
307c478bd9Sstevel@tonic-gate * various system states (boot, while running, while the debugger has control).
317c478bd9Sstevel@tonic-gate * Functions intended for use while the debugger has control may not grab any
327c478bd9Sstevel@tonic-gate * locks or perform any functions that assume the availability of other system
337c478bd9Sstevel@tonic-gate * services.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
387c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/psm_types.h>
417c478bd9Sstevel@tonic-gate #include <sys/segments.h>
42*ae115bc7Smrj #include <sys/archsystm.h>
43*ae115bc7Smrj #include <sys/controlregs.h>
44*ae115bc7Smrj #include <sys/trap.h>
45*ae115bc7Smrj #include <sys/kobj.h>
46*ae115bc7Smrj #include <sys/kobj_impl.h>
47*ae115bc7Smrj #include <sys/mach_mmu.h>
487c478bd9Sstevel@tonic-gate
49*ae115bc7Smrj void
kdi_idt_write(gate_desc_t * gate,uint_t vec)50*ae115bc7Smrj kdi_idt_write(gate_desc_t *gate, uint_t vec)
517c478bd9Sstevel@tonic-gate {
52*ae115bc7Smrj gate_desc_t *idt = CPU->cpu_m.mcpu_idt;
53*ae115bc7Smrj
54*ae115bc7Smrj /*
55*ae115bc7Smrj * See kdi_idtr_set().
56*ae115bc7Smrj */
57*ae115bc7Smrj if (idt == NULL) {
58*ae115bc7Smrj desctbr_t idtr;
59*ae115bc7Smrj rd_idtr(&idtr);
60*ae115bc7Smrj idt = (gate_desc_t *)idtr.dtr_base;
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate idt[vec] = *gate;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
66*ae115bc7Smrj ulong_t
kdi_dreg_get(int reg)67*ae115bc7Smrj kdi_dreg_get(int reg)
687c478bd9Sstevel@tonic-gate {
69*ae115bc7Smrj switch (reg) {
70*ae115bc7Smrj case 0:
71*ae115bc7Smrj return (kdi_getdr0());
72*ae115bc7Smrj case 1:
73*ae115bc7Smrj return (kdi_getdr1());
74*ae115bc7Smrj case 2:
75*ae115bc7Smrj return (kdi_getdr2());
76*ae115bc7Smrj case 3:
77*ae115bc7Smrj return (kdi_getdr3());
78*ae115bc7Smrj case 6:
79*ae115bc7Smrj return (kdi_getdr6());
80*ae115bc7Smrj case 7:
81*ae115bc7Smrj return (kdi_getdr7());
82*ae115bc7Smrj default:
83*ae115bc7Smrj panic("invalid debug register dr%d", reg);
84*ae115bc7Smrj /*NOTREACHED*/
85*ae115bc7Smrj }
86*ae115bc7Smrj }
87*ae115bc7Smrj
88*ae115bc7Smrj void
kdi_dreg_set(int reg,ulong_t value)89*ae115bc7Smrj kdi_dreg_set(int reg, ulong_t value)
90*ae115bc7Smrj {
91*ae115bc7Smrj switch (reg) {
92*ae115bc7Smrj case 0:
93*ae115bc7Smrj kdi_setdr0(value);
94*ae115bc7Smrj break;
95*ae115bc7Smrj case 1:
96*ae115bc7Smrj kdi_setdr1(value);
97*ae115bc7Smrj break;
98*ae115bc7Smrj case 2:
99*ae115bc7Smrj kdi_setdr2(value);
100*ae115bc7Smrj break;
101*ae115bc7Smrj case 3:
102*ae115bc7Smrj kdi_setdr3(value);
103*ae115bc7Smrj break;
104*ae115bc7Smrj case 6:
105*ae115bc7Smrj kdi_setdr6(value);
106*ae115bc7Smrj break;
107*ae115bc7Smrj case 7:
108*ae115bc7Smrj kdi_setdr7(value);
109*ae115bc7Smrj break;
110*ae115bc7Smrj default:
111*ae115bc7Smrj panic("invalid debug register dr%d", reg);
112*ae115bc7Smrj /*NOTREACHED*/
113*ae115bc7Smrj }
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate void
kdi_flush_caches(void)1177c478bd9Sstevel@tonic-gate kdi_flush_caches(void)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate reload_cr3();
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
122*ae115bc7Smrj extern void kdi_slave_entry(void);
123*ae115bc7Smrj
124*ae115bc7Smrj void
kdi_stop_slaves(int cpu,int doxc)125*ae115bc7Smrj kdi_stop_slaves(int cpu, int doxc)
1267c478bd9Sstevel@tonic-gate {
127*ae115bc7Smrj if (doxc)
128*ae115bc7Smrj kdi_xc_others(cpu, kdi_slave_entry);
129*ae115bc7Smrj }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
132*ae115bc7Smrj * On i86pc, slaves busy-loop, so we don't need to do anything here.
1337c478bd9Sstevel@tonic-gate */
134*ae115bc7Smrj void
kdi_start_slaves(void)135*ae115bc7Smrj kdi_start_slaves(void)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate void
kdi_slave_wait(void)140*ae115bc7Smrj kdi_slave_wait(void)
1417c478bd9Sstevel@tonic-gate {
142*ae115bc7Smrj }
1437c478bd9Sstevel@tonic-gate
144*ae115bc7Smrj /*
145*ae115bc7Smrj * Caution.
146*ae115bc7Smrj * These routines are called -extremely- early, during kmdb initialization.
147*ae115bc7Smrj *
148*ae115bc7Smrj * Many common kernel functions assume that %gs has been initialized,
149*ae115bc7Smrj * and fail horribly if it hasn't. At this point, the boot code has
150*ae115bc7Smrj * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
151*ae115bc7Smrj * to point at a dummy cpu_t, temporarily at least.
152*ae115bc7Smrj *
153*ae115bc7Smrj * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
154*ae115bc7Smrj */
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate #if defined(__amd64)
1577c478bd9Sstevel@tonic-gate
158*ae115bc7Smrj void *
boot_kdi_tmpinit(void)159*ae115bc7Smrj boot_kdi_tmpinit(void)
1607c478bd9Sstevel@tonic-gate {
161*ae115bc7Smrj cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
162*ae115bc7Smrj uintptr_t old;
163*ae115bc7Smrj
164*ae115bc7Smrj cpu->cpu_self = cpu;
165*ae115bc7Smrj
166*ae115bc7Smrj old = (uintptr_t)rdmsr(MSR_AMD_GSBASE);
167*ae115bc7Smrj wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu);
168*ae115bc7Smrj return ((void *)old);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate void
boot_kdi_tmpfini(void * old)172*ae115bc7Smrj boot_kdi_tmpfini(void *old)
1737c478bd9Sstevel@tonic-gate {
174*ae115bc7Smrj wrmsr(MSR_AMD_GSBASE, (uint64_t)old);
1757c478bd9Sstevel@tonic-gate }
176*ae115bc7Smrj
177*ae115bc7Smrj #elif defined(__i386)
178*ae115bc7Smrj
179*ae115bc7Smrj void *
boot_kdi_tmpinit(void)180*ae115bc7Smrj boot_kdi_tmpinit(void)
181*ae115bc7Smrj {
182*ae115bc7Smrj cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
183*ae115bc7Smrj uintptr_t old;
184*ae115bc7Smrj desctbr_t b_gdtr;
185*ae115bc7Smrj user_desc_t *bgdt;
186*ae115bc7Smrj
187*ae115bc7Smrj cpu->cpu_self = cpu;
188*ae115bc7Smrj
189*ae115bc7Smrj rd_gdtr(&b_gdtr);
190*ae115bc7Smrj bgdt = (user_desc_t *)(b_gdtr.dtr_base);
191*ae115bc7Smrj
192*ae115bc7Smrj set_usegd(&bgdt[GDT_BGSTMP],
193*ae115bc7Smrj cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32);
194*ae115bc7Smrj
195*ae115bc7Smrj /*
196*ae115bc7Smrj * Now switch %gs to point at it.
197*ae115bc7Smrj */
198*ae115bc7Smrj old = getgs();
199*ae115bc7Smrj setgs(KMDBGS_SEL);
200*ae115bc7Smrj
201*ae115bc7Smrj return ((void *)old);
202*ae115bc7Smrj }
203*ae115bc7Smrj
204*ae115bc7Smrj void
boot_kdi_tmpfini(void * old)205*ae115bc7Smrj boot_kdi_tmpfini(void *old)
206*ae115bc7Smrj {
207*ae115bc7Smrj setgs((uintptr_t)old);
208*ae115bc7Smrj }
209*ae115bc7Smrj
210*ae115bc7Smrj #endif /* __i386 */
211