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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2018 Joyent, Inc.
29 * Management of KMDB's IDT, which is installed upon KMDB activation.
31 * Debugger activation has two flavors, which cover the cases where KMDB is
32 * loaded at boot, and when it is loaded after boot. In brief, in both cases,
33 * the KDI needs to interpose upon several handlers in the IDT. When
34 * mod-loaded KMDB is deactivated, we undo the IDT interposition, restoring the
35 * handlers to what they were before we started.
37 * We also take over the entirety of IDT (except the double-fault handler) on
38 * the active CPU when we're in kmdb so we can handle things like page faults
43 * When we're first activated, we're running on boot's IDT. We need to be able
44 * to function in this world, so we'll install our handlers into boot's IDT.
45 * This is a little complicated: we're using the fake cpu_t set up by
46 * boot_kdi_tmpinit(), so we can't access cpu_idt directly. Instead,
47 * kdi_idt_write() notices that cpu_idt is NULL, and works around this problem.
49 * Later, when we're about to switch to the kernel's IDT, it'll call us via
50 * kdi_idt_sync(), allowing us to add our handlers to the new IDT. While
51 * boot-loaded KMDB can't be unloaded, we still need to save the descriptors we
52 * replace so we can pass traps back to the kernel as necessary.
54 * The last phase of boot-loaded KMDB activation occurs at non-boot CPU
55 * startup. We will be called on each non-boot CPU, thus allowing us to set up
56 * any watchpoints that may have been configured on the boot CPU and interpose
57 * on the given CPU's IDT. We don't save the interposed descriptors in this
58 * case -- see kdi_cpu_init() for details.
62 * This style of activation is much simpler, as the CPUs are already running,
63 * and are using their own copy of the kernel's IDT. We simply interpose upon
64 * each CPU's IDT. We save the handlers we replace, both for deactivation and
65 * for passing traps back to the kernel. Note that for the hypervisors'
66 * benefit, we need to xcall to the other CPUs to do this, since we need to
67 * actively set the trap entries in its virtual IDT from that vcpu's context
68 * rather than just modifying the IDT table from the CPU running kdi_activate().
71 #include <sys/types.h>
72 #include <sys/segments.h>
74 #include <sys/cpuvar.h>
75 #include <sys/reboot.h>
76 #include <sys/sunddi.h>
77 #include <sys/archsystm.h>
78 #include <sys/kdi_impl.h>
79 #include <sys/x_call.h>
80 #include <ia32/sys/psw.h>
81 #include <vm/hat_i86.h>
83 #define KDI_GATE_NVECS 3
85 #define KDI_IDT_NOSAVE 0
86 #define KDI_IDT_SAVE 1
88 #define KDI_IDT_DTYPE_KERNEL 0
89 #define KDI_IDT_DTYPE_BOOT 1
91 kdi_cpusave_t
*kdi_cpusave
;
94 static kdi_main_t kdi_kmdb_main
;
96 kdi_drreg_t kdi_drreg
;
99 /* Used to track the current set of valid kernel selectors. */
106 uintptr_t kdi_kernel_handler
;
110 #define KDI_MEMRANGES_MAX 2
112 kdi_memrange_t kdi_memranges
[KDI_MEMRANGES_MAX
];
115 typedef void idt_hdlr_f(void);
117 extern idt_hdlr_f kdi_trap0
, kdi_trap1
, kdi_int2
, kdi_trap3
, kdi_trap4
;
118 extern idt_hdlr_f kdi_trap5
, kdi_trap6
, kdi_trap7
, kdi_trap9
;
119 extern idt_hdlr_f kdi_traperr10
, kdi_traperr11
, kdi_traperr12
;
120 extern idt_hdlr_f kdi_traperr13
, kdi_traperr14
, kdi_trap16
, kdi_traperr17
;
121 extern idt_hdlr_f kdi_trap18
, kdi_trap19
, kdi_trap20
, kdi_ivct32
;
122 extern idt_hdlr_f kdi_invaltrap
;
123 extern size_t kdi_ivct_size
;
125 typedef struct kdi_gate_spec
{
131 * Beware: kdi_pass_to_kernel() has unpleasant knowledge of this list.
133 static const kdi_gate_spec_t kdi_gate_specs
[KDI_GATE_NVECS
] = {
134 { T_SGLSTP
, TRP_KPL
},
135 { T_BPTFLT
, TRP_UPL
},
136 { T_DBGENTR
, TRP_KPL
}
139 static gate_desc_t kdi_kgates
[KDI_GATE_NVECS
];
141 extern gate_desc_t kdi_idt
[NIDT
];
143 struct idt_description
{
146 idt_hdlr_f
*id_basehdlr
;
148 } idt_description
[] = {
149 { T_ZERODIV
, 0, kdi_trap0
, NULL
},
150 { T_SGLSTP
, 0, kdi_trap1
, NULL
},
151 { T_NMIFLT
, 0, kdi_int2
, NULL
},
152 { T_BPTFLT
, 0, kdi_trap3
, NULL
},
153 { T_OVFLW
, 0, kdi_trap4
, NULL
},
154 { T_BOUNDFLT
, 0, kdi_trap5
, NULL
},
155 { T_ILLINST
, 0, kdi_trap6
, NULL
},
156 { T_NOEXTFLT
, 0, kdi_trap7
, NULL
},
157 { T_DBLFLT
, 0, syserrtrap
, NULL
},
158 { T_EXTOVRFLT
, 0, kdi_trap9
, NULL
},
159 { T_TSSFLT
, 0, kdi_traperr10
, NULL
},
160 { T_SEGFLT
, 0, kdi_traperr11
, NULL
},
161 { T_STKFLT
, 0, kdi_traperr12
, NULL
},
162 { T_GPFLT
, 0, kdi_traperr13
, NULL
},
163 { T_PGFLT
, 0, kdi_traperr14
, NULL
},
164 { 15, 0, kdi_invaltrap
, NULL
},
165 { T_EXTERRFLT
, 0, kdi_trap16
, NULL
},
166 { T_ALIGNMENT
, 0, kdi_traperr17
, NULL
},
167 { T_MCE
, 0, kdi_trap18
, NULL
},
168 { T_SIMDFPE
, 0, kdi_trap19
, NULL
},
169 { T_DBGENTR
, 0, kdi_trap20
, NULL
},
170 { 21, 31, kdi_invaltrap
, NULL
},
171 { 32, 255, kdi_ivct32
, &kdi_ivct_size
},
176 kdi_idt_init(selector_t sel
)
178 struct idt_description
*id
;
181 for (id
= idt_description
; id
->id_basehdlr
!= NULL
; id
++) {
182 uint_t high
= id
->id_high
!= 0 ? id
->id_high
: id
->id_low
;
183 size_t incr
= id
->id_incrp
!= NULL
? *id
->id_incrp
: 0;
186 if (kpti_enable
&& sel
== KCS_SEL
&& id
->id_low
== T_DBLFLT
)
187 id
->id_basehdlr
= tr_syserrtrap
;
190 for (i
= id
->id_low
; i
<= high
; i
++) {
191 caddr_t hdlr
= (caddr_t
)id
->id_basehdlr
+
192 incr
* (i
- id
->id_low
);
193 set_gatesegd(&kdi_idt
[i
], (void (*)())hdlr
, sel
,
194 SDT_SYSIGT
, TRP_KPL
, IST_DBG
);
200 kdi_idt_gates_install(selector_t sel
, int saveold
)
202 gate_desc_t gates
[KDI_GATE_NVECS
];
205 bzero(gates
, sizeof (*gates
));
207 for (i
= 0; i
< KDI_GATE_NVECS
; i
++) {
208 const kdi_gate_spec_t
*gs
= &kdi_gate_specs
[i
];
209 uintptr_t func
= GATESEG_GETOFFSET(&kdi_idt
[gs
->kgs_vec
]);
210 set_gatesegd(&gates
[i
], (void (*)())func
, sel
, SDT_SYSIGT
,
211 gs
->kgs_dpl
, IST_DBG
);
214 for (i
= 0; i
< KDI_GATE_NVECS
; i
++) {
215 uint_t vec
= kdi_gate_specs
[i
].kgs_vec
;
218 kdi_kgates
[i
] = CPU
->cpu_m
.mcpu_idt
[vec
];
220 kdi_idt_write(&gates
[i
], vec
);
225 kdi_idt_gates_restore(void)
229 for (i
= 0; i
< KDI_GATE_NVECS
; i
++)
230 kdi_idt_write(&kdi_kgates
[i
], kdi_gate_specs
[i
].kgs_vec
);
234 * Called when we switch to the kernel's IDT. We need to interpose on the
235 * kernel's IDT entries and stop using KMDBCODE_SEL.
240 kdi_idt_init(KCS_SEL
);
241 kdi_idt_gates_install(KCS_SEL
, KDI_IDT_SAVE
);
245 kdi_update_drreg(kdi_drreg_t
*drreg
)
251 kdi_memrange_add(caddr_t base
, size_t len
)
253 kdi_memrange_t
*mr
= &kdi_memranges
[kdi_nmemranges
];
255 ASSERT(kdi_nmemranges
!= KDI_MEMRANGES_MAX
);
258 mr
->mr_lim
= base
+ len
- 1;
263 kdi_idt_switch(kdi_cpusave_t
*cpusave
)
266 kdi_idtr_set(kdi_idt
, sizeof (kdi_idt
) - 1);
268 kdi_idtr_set(cpusave
->krs_idt
, (sizeof (*idt0
) * NIDT
) - 1);
272 * Activation for CPUs other than the boot CPU, called from that CPU's
273 * mp_startup(). We saved the kernel's descriptors when we initialized the
274 * boot CPU, so we don't want to do it again. Saving the handlers from this
275 * CPU's IDT would actually be dangerous with the CPU initialization method in
276 * use at the time of this writing. With that method, the startup code creates
277 * the IDTs for slave CPUs by copying the one used by the boot CPU, which has
278 * already been interposed upon by KMDB. Were we to interpose again, we'd
279 * replace the kernel's descriptors with our own in the save area. By not
280 * saving, but still overwriting, we'll work in the current world, and in any
281 * future world where the IDT is generated from scratch.
286 kdi_idt_gates_install(KCS_SEL
, KDI_IDT_NOSAVE
);
287 /* Load the debug registers. */
288 kdi_cpu_debug_init(&kdi_cpusave
[CPU
->cpu_id
]);
292 * Activation for all CPUs for mod-loaded kmdb, i.e. a kmdb that wasn't
296 kdi_cpu_activate(void)
298 kdi_idt_gates_install(KCS_SEL
, KDI_IDT_SAVE
);
303 kdi_activate(kdi_main_t main
, kdi_cpusave_t
*cpusave
, uint_t ncpusave
)
310 kdi_cpusave
= cpusave
;
311 kdi_ncpusave
= ncpusave
;
313 kdi_kmdb_main
= main
;
315 for (i
= 0; i
< kdi_ncpusave
; i
++) {
316 kdi_cpusave
[i
].krs_cpu_id
= i
;
318 kdi_cpusave
[i
].krs_curcrumb
=
319 &kdi_cpusave
[i
].krs_crumbs
[KDI_NCRUMBS
- 1];
320 kdi_cpusave
[i
].krs_curcrumbidx
= KDI_NCRUMBS
- 1;
323 if (boothowto
& RB_KMDB
)
324 kdi_idt_init(KMDBCODE_SEL
);
326 kdi_idt_init(KCS_SEL
);
328 /* The initial selector set. Updated by the debugger-entry code */
330 kdi_cs
= B32CODE_SEL
;
331 kdi_ds
= kdi_fs
= kdi_gs
= B32DATA_SEL
;
334 kdi_memranges
[0].mr_base
= kdi_segdebugbase
;
335 kdi_memranges
[0].mr_lim
= kdi_segdebugbase
+ kdi_segdebugsize
- 1;
338 kdi_drreg
.dr_ctl
= KDIREG_DRCTL_RESERVED
;
339 kdi_drreg
.dr_stat
= KDIREG_DRSTAT_RESERVED
;
341 if (boothowto
& RB_KMDB
) {
342 kdi_idt_gates_install(KMDBCODE_SEL
, KDI_IDT_NOSAVE
);
344 xc_call(0, 0, 0, CPUSET2BV(cpuset
),
345 (xc_func_t
)kdi_cpu_activate
);
350 kdi_cpu_deactivate(void)
352 kdi_idt_gates_restore();
362 xc_call(0, 0, 0, CPUSET2BV(cpuset
), (xc_func_t
)kdi_cpu_deactivate
);
367 * We receive all breakpoints and single step traps. Some of them,
368 * including those from userland and those induced by DTrace providers,
369 * are intended for the kernel, and must be processed there. We adopt
370 * this ours-until-proven-otherwise position due to the painful
371 * consequences of sending the kernel an unexpected breakpoint or
372 * single step. Unless someone can prove to us that the kernel is
373 * prepared to handle the trap, we'll assume there's a problem and will
374 * give the user a chance to debug it.
377 kdi_trap_pass(kdi_cpusave_t
*cpusave
)
379 greg_t tt
= cpusave
->krs_gregs
[KDIREG_TRAPNO
];
380 greg_t pc
= cpusave
->krs_gregs
[KDIREG_PC
];
381 greg_t cs
= cpusave
->krs_gregs
[KDIREG_CS
];
386 if (tt
!= T_BPTFLT
&& tt
!= T_SGLSTP
)
389 if (tt
== T_BPTFLT
&& kdi_dtrace_get_state() ==
390 KDI_DTSTATE_DTRACE_ACTIVE
)
394 * See the comments in the kernel's T_SGLSTP handler for why we need to
398 if (tt
== T_SGLSTP
&&
399 (pc
== (greg_t
)sys_sysenter
|| pc
== (greg_t
)brand_sys_sysenter
||
400 pc
== (greg_t
)tr_sys_sysenter
||
401 pc
== (greg_t
)tr_brand_sys_sysenter
)) {
403 if (tt
== T_SGLSTP
&&
404 (pc
== (greg_t
)sys_sysenter
|| pc
== (greg_t
)brand_sys_sysenter
)) {
413 * State has been saved, and all CPUs are on the CPU-specific stacks. All
414 * CPUs enter here, and head off into the debugger proper.
417 kdi_debugger_entry(kdi_cpusave_t
*cpusave
)
420 * BPTFLT gives us control with %eip set to the instruction *after*
421 * the int 3. Back it off, so we're looking at the instruction that
422 * triggered the fault.
424 if (cpusave
->krs_gregs
[KDIREG_TRAPNO
] == T_BPTFLT
)
425 cpusave
->krs_gregs
[KDIREG_PC
]--;
427 kdi_kmdb_main(cpusave
);