Merge commit '74ecdb5171c9f3673b9393b1a3dc6f3a65e93895'
[unleashed.git] / arch / x86 / kernel / platform / i86pc / os / mach_kdi.c
blob60ca8c9fca7b5b10eaeecd0959a4a694cd27e99f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2018 Joyent, Inc.
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.
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 #include <sys/mach_mmu.h>
49 void
50 kdi_idt_write(gate_desc_t *gate, uint_t vec)
52 gate_desc_t *idt = CPU->cpu_m.mcpu_idt;
55 * See kdi_idtr_set().
57 if (idt == NULL) {
58 desctbr_t idtr;
59 rd_idtr(&idtr);
60 idt = (gate_desc_t *)idtr.dtr_base;
63 idt[vec] = *gate;
66 ulong_t
67 kdi_dreg_get(int reg)
69 switch (reg) {
70 case 0:
71 return (kdi_getdr0());
72 case 1:
73 return (kdi_getdr1());
74 case 2:
75 return (kdi_getdr2());
76 case 3:
77 return (kdi_getdr3());
78 case 6:
79 return (kdi_getdr6());
80 case 7:
81 return (kdi_getdr7());
82 default:
83 panic("invalid debug register dr%d", reg);
84 /*NOTREACHED*/
88 void
89 kdi_dreg_set(int reg, ulong_t value)
91 switch (reg) {
92 case 0:
93 kdi_setdr0(value);
94 break;
95 case 1:
96 kdi_setdr1(value);
97 break;
98 case 2:
99 kdi_setdr2(value);
100 break;
101 case 3:
102 kdi_setdr3(value);
103 break;
104 case 6:
105 kdi_setdr6(value);
106 break;
107 case 7:
108 kdi_setdr7(value);
109 break;
110 default:
111 panic("invalid debug register dr%d", reg);
112 /*NOTREACHED*/
116 extern void kdi_slave_entry(void);
118 void
119 kdi_stop_slaves(int cpu, int doxc)
121 if (doxc)
122 kdi_xc_others(cpu, kdi_slave_entry);
126 * On i86pc, slaves busy-loop, so we don't need to do anything here.
128 void
129 kdi_start_slaves(void)
133 void
134 kdi_slave_wait(void)
139 * Caution.
140 * These routines are called -extremely- early, during kmdb initialization.
142 * Many common kernel functions assume that %gs has been initialized,
143 * and fail horribly if it hasn't. At this point, the boot code has
144 * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
145 * to point at a dummy cpu_t, temporarily at least.
147 * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
150 #if defined(__amd64)
152 void *
153 boot_kdi_tmpinit(void)
155 cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
156 uintptr_t old;
158 cpu->cpu_self = cpu;
160 old = (uintptr_t)rdmsr(MSR_AMD_GSBASE);
161 wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu);
162 return ((void *)old);
165 void
166 boot_kdi_tmpfini(void *old)
168 wrmsr(MSR_AMD_GSBASE, (uint64_t)old);
171 #elif defined(__i386)
173 void *
174 boot_kdi_tmpinit(void)
176 cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
177 uintptr_t old;
178 desctbr_t b_gdtr;
179 user_desc_t *bgdt;
181 cpu->cpu_self = cpu;
183 rd_gdtr(&b_gdtr);
184 bgdt = (user_desc_t *)(b_gdtr.dtr_base);
186 set_usegd(&bgdt[GDT_BGSTMP],
187 cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32);
190 * Now switch %gs to point at it.
192 old = getgs();
193 setgs(KMDBGS_SEL);
195 return ((void *)old);
198 void
199 boot_kdi_tmpfini(void *old)
201 setgs((uintptr_t)old);
204 #endif /* __i386 */