Skip to content

Commit

Permalink
debug: implement GdbStubConnection on top of the platform abstraction
Browse files Browse the repository at this point in the history
The GDB stub connection needs to communicate over the serial port.  Like
other serial port accesses, this should be done using the I/O port
driver implemented by the platform abstraction instead of assuming the
GHCB-based I/O driver.

Signed-off-by: Jon Lange <[email protected]>
  • Loading branch information
msft-jlange committed Sep 19, 2024
1 parent f23151f commit 9a4f368
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions kernel/src/debug/gdbstub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub mod svsm_gdbstub {
use crate::locking::{LockGuard, SpinLock};
use crate::mm::guestmem::{read_u8, write_u8};
use crate::mm::PerCPUPageMappingGuard;
use crate::platform::SvsmPlatform;
use crate::serial::{SerialPort, Terminal};
use crate::svsm_console::SVSMIOPort;
use crate::task::{is_current_task, TaskContext, INITIAL_TASK_ID, TASKLIST};
use core::arch::asm;
use core::fmt;
Expand All @@ -44,12 +44,11 @@ pub mod svsm_gdbstub {
const INT3_INSTR: u8 = 0xcc;
const MAX_BREAKPOINTS: usize = 32;

pub fn gdbstub_start() -> Result<(), u64> {
pub fn gdbstub_start(platform: &'static dyn SvsmPlatform) -> Result<(), u64> {
unsafe {
GDB_SERIAL.init();
let mut target = GdbStubTarget::new();
#[allow(static_mut_refs)]
let gdb = GdbStubBuilder::new(GdbStubConnection::new())
let gdb = GdbStubBuilder::new(GdbStubConnection::new(platform))
.with_packet_buffer(&mut PACKET_BUFFER)
.build()
.expect("Failed to initialise GDB stub")
Expand Down Expand Up @@ -173,8 +172,6 @@ pub mod svsm_gdbstub {

static GDB_INITIALISED: AtomicBool = AtomicBool::new(false);
static GDB_STATE: SpinLock<Option<SvsmGdbStub<'_>>> = SpinLock::new(None);
static GDB_IO: SVSMIOPort = SVSMIOPort::new();
static mut GDB_SERIAL: SerialPort<'_> = SerialPort::new(&GDB_IO, 0x2f8);
static mut PACKET_BUFFER: [u8; 4096] = [0; 4096];
// Allocate the GDB stack as an array of u64's to ensure 8 byte alignment of the stack.
static mut GDB_STACK: [u64; 8192] = [0; 8192];
Expand Down Expand Up @@ -217,7 +214,7 @@ pub mod svsm_gdbstub {
}

struct SvsmGdbStub<'a> {
gdb: GdbStubStateMachine<'a, GdbStubTarget, GdbStubConnection>,
gdb: GdbStubStateMachine<'a, GdbStubTarget, GdbStubConnection<'a>>,
target: GdbStubTarget,
}

Expand Down Expand Up @@ -306,25 +303,27 @@ pub mod svsm_gdbstub {
});
}

struct GdbStubConnection;
struct GdbStubConnection<'a> {
serial_port: SerialPort<'a>,
}

impl GdbStubConnection {
const fn new() -> Self {
Self {}
impl GdbStubConnection<'_> {
fn new(platform: &'static dyn SvsmPlatform) -> Self {
let serial_port = SerialPort::new(platform.get_io_port(), 0x2f8);
serial_port.init();
Self { serial_port }
}

fn read(&self) -> Result<u8, &'static str> {
unsafe { Ok(GDB_SERIAL.get_byte()) }
Ok(self.serial_port.get_byte())
}
}

impl Connection for GdbStubConnection {
impl Connection for GdbStubConnection<'_> {
type Error = usize;

fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
unsafe {
GDB_SERIAL.put_byte(byte);
}
self.serial_port.put_byte(byte);
Ok(())
}

Expand Down Expand Up @@ -742,8 +741,9 @@ pub mod svsm_gdbstub {
#[cfg(not(feature = "enable-gdb"))]
pub mod svsm_gdbstub {
use crate::cpu::X86ExceptionContext;
use crate::platform::SvsmPlatform;

pub fn gdbstub_start() -> Result<(), u64> {
pub fn gdbstub_start(_platform: &'static dyn SvsmPlatform) -> Result<(), u64> {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub extern "C" fn svsm_main() {

// If required, the GDB stub can be started earlier, just after the console
// is initialised in svsm_start() above.
gdbstub_start().expect("Could not start GDB stub");
gdbstub_start(platform).expect("Could not start GDB stub");
// Uncomment the line below if you want to wait for
// a remote GDB connection
//debug_break();
Expand Down

0 comments on commit 9a4f368

Please sign in to comment.