|
| 1 | +/* |
| 2 | +Copyright 2024 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! This file contains architecture specific code for the x86_64 |
| 18 | +
|
| 19 | +use std::collections::HashMap; |
| 20 | + |
| 21 | +use super::VcpuStopReason; |
| 22 | + |
| 23 | +// Described in Table 6-1. Exceptions and Interrupts at Page 6-13 Vol. 1 |
| 24 | +// of Intel 64 and IA-32 Architectures Software Developer's Manual |
| 25 | +/// Exception id for #DB |
| 26 | +const DB_EX_ID: u32 = 1; |
| 27 | +/// Exception id for #BP - triggered by the INT3 instruction |
| 28 | +const BP_EX_ID: u32 = 3; |
| 29 | + |
| 30 | +/// Software Breakpoint size in memory |
| 31 | +pub(crate) const SW_BP_SIZE: usize = 1; |
| 32 | +/// Software Breakpoint opcode - INT3 |
| 33 | +/// Check page 7-28 Vol. 3A of Intel 64 and IA-32 |
| 34 | +/// Architectures Software Developer's Manual |
| 35 | +pub(crate) const SW_BP_OP: u8 = 0xCC; |
| 36 | +/// Software Breakpoint written to memory |
| 37 | +pub(crate) const SW_BP: [u8; SW_BP_SIZE] = [SW_BP_OP]; |
| 38 | +/// Maximum number of supported hardware breakpoints |
| 39 | +pub(crate) const MAX_NO_OF_HW_BP: usize = 4; |
| 40 | + |
| 41 | +/// Check page 19-4 Vol. 3B of Intel 64 and IA-32 |
| 42 | +/// Architectures Software Developer's Manual |
| 43 | +/// Bit position of BS flag in DR6 debug register |
| 44 | +pub(crate) const DR6_BS_FLAG_POS: usize = 14; |
| 45 | +/// Bit mask of BS flag in DR6 debug register |
| 46 | +pub(crate) const DR6_BS_FLAG_MASK: u64 = 1 << DR6_BS_FLAG_POS; |
| 47 | +/// Bit position of HW breakpoints status in DR6 debug register |
| 48 | +pub(crate) const DR6_HW_BP_FLAGS_POS: usize = 0; |
| 49 | +/// Bit mask of HW breakpoints status in DR6 debug register |
| 50 | +pub(crate) const DR6_HW_BP_FLAGS_MASK: u64 = 0x0F << DR6_HW_BP_FLAGS_POS; |
| 51 | + |
| 52 | +/// Determine the reason the vCPU stopped |
| 53 | +/// This is done by checking the DR6 register and the exception id |
| 54 | +/// NOTE: Additional checks are done for the entrypoint, stored hw_breakpoints |
| 55 | +/// and sw_breakpoints to ensure the stop reason is valid with internal state |
| 56 | +pub(crate) fn vcpu_stop_reason( |
| 57 | + single_step: bool, |
| 58 | + rip: u64, |
| 59 | + dr6: u64, |
| 60 | + entrypoint: u64, |
| 61 | + exception: u32, |
| 62 | + hw_breakpoints: &[u64], |
| 63 | + sw_breakpoints: &HashMap<u64, [u8; SW_BP_SIZE]>, |
| 64 | +) -> VcpuStopReason { |
| 65 | + if DB_EX_ID == exception { |
| 66 | + // If the BS flag in DR6 register is set, it means a single step |
| 67 | + // instruction triggered the exit |
| 68 | + // Check page 19-4 Vol. 3B of Intel 64 and IA-32 |
| 69 | + // Architectures Software Developer's Manual |
| 70 | + if dr6 & DR6_BS_FLAG_MASK != 0 && single_step { |
| 71 | + return VcpuStopReason::DoneStep; |
| 72 | + } |
| 73 | + |
| 74 | + // If any of the B0-B3 flags in DR6 register is set, it means a |
| 75 | + // hardware breakpoint triggered the exit |
| 76 | + // Check page 19-4 Vol. 3B of Intel 64 and IA-32 |
| 77 | + // Architectures Software Developer's Manual |
| 78 | + if DR6_HW_BP_FLAGS_MASK & dr6 != 0 && hw_breakpoints.contains(&rip) { |
| 79 | + if rip == entrypoint { |
| 80 | + return VcpuStopReason::EntryPointBp; |
| 81 | + } |
| 82 | + return VcpuStopReason::HwBp; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if BP_EX_ID == exception && sw_breakpoints.contains_key(&rip) { |
| 87 | + return VcpuStopReason::SwBp; |
| 88 | + } |
| 89 | + |
| 90 | + // Log an error and provide internal debugging info |
| 91 | + log::error!( |
| 92 | + r"The vCPU exited because of an unknown reason: |
| 93 | + single_step: {:?} |
| 94 | + rip: {:?} |
| 95 | + dr6: {:?} |
| 96 | + entrypoint: {:?} |
| 97 | + exception: {:?} |
| 98 | + hw_breakpoints: {:?} |
| 99 | + sw_breakpoints: {:?} |
| 100 | + ", |
| 101 | + single_step, |
| 102 | + rip, |
| 103 | + dr6, |
| 104 | + entrypoint, |
| 105 | + exception, |
| 106 | + hw_breakpoints, |
| 107 | + sw_breakpoints, |
| 108 | + ); |
| 109 | + |
| 110 | + VcpuStopReason::Unknown |
| 111 | +} |
0 commit comments