Skip to content

Commit 91df956

Browse files
authored
Merge pull request #462 from msft-jlange/gdbstub
debug: implement `GdbStubConnection` on top of the platform abstraction
2 parents 6064a62 + 5c42092 commit 91df956

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

kernel/src/debug/gdbstub.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub mod svsm_gdbstub {
2020
use crate::locking::{LockGuard, SpinLock};
2121
use crate::mm::guestmem::{read_u8, write_u8};
2222
use crate::mm::PerCPUPageMappingGuard;
23+
use crate::platform::SvsmPlatform;
2324
use crate::serial::{SerialPort, Terminal};
24-
use crate::svsm_console::SVSMIOPort;
2525
use crate::task::{is_current_task, TaskContext, INITIAL_TASK_ID, TASKLIST};
2626
use core::arch::asm;
2727
use core::fmt;
@@ -44,12 +44,11 @@ pub mod svsm_gdbstub {
4444
const INT3_INSTR: u8 = 0xcc;
4545
const MAX_BREAKPOINTS: usize = 32;
4646

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

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

219216
struct SvsmGdbStub<'a> {
220-
gdb: GdbStubStateMachine<'a, GdbStubTarget, GdbStubConnection>,
217+
gdb: GdbStubStateMachine<'a, GdbStubTarget, GdbStubConnection<'a>>,
221218
target: GdbStubTarget,
222219
}
223220

@@ -306,25 +303,27 @@ pub mod svsm_gdbstub {
306303
});
307304
}
308305

309-
struct GdbStubConnection;
306+
struct GdbStubConnection<'a> {
307+
serial_port: SerialPort<'a>,
308+
}
310309

311-
impl GdbStubConnection {
312-
const fn new() -> Self {
313-
Self {}
310+
impl<'a> GdbStubConnection<'a> {
311+
fn new(platform: &'a dyn SvsmPlatform) -> Self {
312+
let serial_port = SerialPort::new(platform.get_io_port(), 0x2f8);
313+
serial_port.init();
314+
Self { serial_port }
314315
}
315316

316317
fn read(&self) -> Result<u8, &'static str> {
317-
unsafe { Ok(GDB_SERIAL.get_byte()) }
318+
Ok(self.serial_port.get_byte())
318319
}
319320
}
320321

321-
impl Connection for GdbStubConnection {
322+
impl Connection for GdbStubConnection<'_> {
322323
type Error = usize;
323324

324325
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
325-
unsafe {
326-
GDB_SERIAL.put_byte(byte);
327-
}
326+
self.serial_port.put_byte(byte);
328327
Ok(())
329328
}
330329

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

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

kernel/src/svsm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub extern "C" fn svsm_main() {
388388

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

0 commit comments

Comments
 (0)