forked from enarx/enarx
/
debug.rs
258 lines (215 loc) · 6.65 KB
/
debug.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// SPDX-License-Identifier: Apache-2.0
//! Debug functions
use core::arch::asm;
use x86_64::instructions::tables::lidt;
use x86_64::structures::DescriptorTablePointer;
use x86_64::VirtAddr;
#[cfg(feature = "dbg")]
use crate::addr::SHIM_VIRT_OFFSET;
use crate::snp::ghcb::{vmgexit_msr, GhcbMsr};
use crate::snp::snp_active;
/// Debug helper function for the early boot
///
/// # Safety
///
/// This function causes a triple fault!
#[inline(never)]
#[cfg_attr(coverage, no_coverage)]
pub unsafe fn _early_debug_panic(reason: u64, value: u64) -> ! {
let mut rbp: u64;
// Safe the contents of the rbp register containing the stack frame pointer
asm!("mov {}, rbp", out(reg) rbp);
if snp_active() {
_load_invalid_idt();
vmgexit_msr(
GhcbMsr::EXIT_REQ,
value.wrapping_shl(16) | (reason & 0x7).wrapping_shl(12),
0,
);
// a GHCB_MSR_EXIT_REQ should not return, but in case it does the
// `unreachable` `ud2` will cause a triple fault.
unreachable!();
}
let mut frames = backtrace(rbp);
frames[11] = reason;
frames[12] = value;
_inline_ud2_triple_fault(frames)
}
/// Provoke a triple fault to shutdown the machine
///
/// An illegal IDT is loaded with limit=0 and an #UD is produced
///
/// Fun read: http://www.rcollins.org/Productivity/TripleFault.html
///
/// # Safety
///
/// This function causes a triple fault!
#[inline(never)]
#[cfg_attr(coverage, no_coverage)]
pub unsafe fn _enarx_asm_triple_fault() -> ! {
let mut rbp: u64;
// Safe the contents of the rbp register containing the stack frame pointer
asm!("mov {}, rbp", out(reg) rbp);
if snp_active() {
_early_debug_panic(0x7, 0xFF);
}
let frames = backtrace(rbp);
_inline_ud2_triple_fault(frames)
}
/// Load an invalid DescriptorTablePointer with no base and limit and
/// provoke an #UD, which will lead to a triple fault
#[inline(always)]
#[cfg_attr(coverage, no_coverage)]
unsafe fn _inline_ud2_triple_fault(frames: [u64; 16]) -> ! {
_load_invalid_idt();
// ud2 with defined register contents
asm!(
"ud2",
in("rax") frames[0],
in("rcx") frames[1],
in("rdx") frames[2],
in("rsi") frames[3],
in("rdi") frames[4],
in("r8") frames[5],
in("r9") frames[6],
in("r10") frames[7],
in("r11") frames[8],
in("r12") frames[9],
in("r13") frames[10],
in("r14") frames[11],
in("r15") frames[12],
options(nomem, nostack, noreturn)
)
}
/// Load an invalid DescriptorTablePointer with no base and limit
#[inline(always)]
#[cfg_attr(coverage, no_coverage)]
unsafe fn _load_invalid_idt() {
let dtp = DescriptorTablePointer {
limit: 0,
base: VirtAddr::new(0),
};
// Load the invalid IDT
lidt(&dtp);
}
#[cfg(not(feature = "dbg"))]
unsafe fn backtrace(_rbp: u64) -> [u64; 16] {
[0u64; 16]
}
#[cfg(feature = "dbg")]
/// Produce a backtrace from a frame pointer
#[inline(always)]
#[cfg_attr(coverage, no_coverage)]
unsafe fn backtrace(mut rbp: u64) -> [u64; 16] {
use core::mem::size_of;
let mut frames = [0u64; 16];
for ele in frames.iter_mut() {
if let Some(rip_rbp) = rbp.checked_add(size_of::<usize>() as _) {
if rip_rbp < SHIM_VIRT_OFFSET {
break;
}
let rip = *(rip_rbp as *const u64);
if let Some(rip) = rip
.checked_sub(SHIM_VIRT_OFFSET)
.and_then(|v| v.checked_sub(1))
{
*ele = rip;
rbp = *(rbp as *const u64);
} else {
// Not a shim virtual address
break;
}
} else {
// RBP OVERFLOW
break;
}
}
frames
}
#[cfg(feature = "dbg")]
#[inline(never)]
/// print a stack trace from a stack frame pointer
#[cfg_attr(coverage, no_coverage)]
pub fn print_stack_trace() {
let mut rbp: usize;
unsafe {
asm!("mov {}, rbp", out(reg) rbp);
stack_trace_from_rbp(rbp);
}
}
#[cfg(feature = "dbg")]
#[cfg_attr(coverage, no_coverage)]
unsafe fn stack_trace_from_rbp(mut rbp: usize) {
use crate::exec::EXEC_VIRT_ADDR;
use crate::paging::SHIM_PAGETABLE;
use crate::print;
use core::mem::size_of;
use core::sync::atomic::Ordering;
use x86_64::structures::paging::Translate;
print::_eprint(format_args!("TRACE:\n"));
if SHIM_PAGETABLE.try_read().is_none() {
SHIM_PAGETABLE.force_write_unlock()
}
let shim_offset = SHIM_VIRT_OFFSET as usize;
let active_table = SHIM_PAGETABLE.read();
//Maximum 64 frames
for _frame in 0..64 {
if rbp == 0
|| VirtAddr::try_new(rbp as _).is_err()
|| active_table
.translate_addr(VirtAddr::new(rbp as _))
.is_none()
{
break;
}
if let Some(rip_rbp) = rbp.checked_add(size_of::<usize>() as _) {
if active_table
.translate_addr(VirtAddr::new(rip_rbp as _))
.is_none()
{
break;
}
let rip = *(rip_rbp as *const usize);
if let Some(rip) = rip.checked_sub(1) {
if rip == 0 {
break;
}
if let Some(rip) = rip.checked_sub(shim_offset) {
print::_eprint(format_args!("S 0x{rip:>016x}\n"));
rbp = *(rbp as *const usize);
} else if crate::exec::EXEC_READY.load(Ordering::Relaxed) {
if let Some(rip) = rip.checked_sub(EXEC_VIRT_ADDR.read().as_u64() as _) {
print::_eprint(format_args!("E 0x{rip:>016x}\n"));
rbp = *(rbp as *const usize);
} else {
break;
}
}
} else {
// RIP zero
break;
}
} else {
// RBP OVERFLOW
break;
}
}
}
#[cfg(feature = "dbg")]
#[cfg_attr(coverage, no_coverage)]
pub(crate) fn interrupt_trace(stack_frame: &crate::interrupts::ExtendedInterruptStackFrame) {
use crate::exec::EXEC_VIRT_ADDR;
let exec_virt = *EXEC_VIRT_ADDR.read();
let mut addr = stack_frame.instruction_pointer;
if addr.as_u64() > SHIM_VIRT_OFFSET {
addr -= SHIM_VIRT_OFFSET;
eprintln!("TRACE:\nS 0x{:>016x}", addr.as_u64());
} else if addr > exec_virt {
addr -= exec_virt.as_u64();
eprintln!("TRACE:\nE 0x{:>016x}", addr.as_u64());
};
unsafe {
stack_trace_from_rbp(stack_frame.rbp as _);
}
print_stack_trace();
}