/
mod.rs
155 lines (131 loc) · 4.37 KB
/
mod.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
//! `Register` structs for x86 architectures.
use core::convert::TryInto;
use gdbstub::arch::Registers;
/// `RegId` definitions for x86 architectures.
pub mod id;
mod core32;
mod core64;
pub use core32::X86CoreRegs;
pub use core64::X86_64CoreRegs;
/// 80-bit floating point value
pub type F80 = [u8; 10];
/// FPU registers
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct X87FpuInternalRegs {
/// Floating-point control register
pub fctrl: u32,
/// Floating-point status register
pub fstat: u32,
/// Tag word
pub ftag: u32,
/// FPU instruction pointer segment
pub fiseg: u32,
/// FPU instruction pointer offset
pub fioff: u32,
/// FPU operand segment
pub foseg: u32,
/// FPU operand offset
pub fooff: u32,
/// Floating-point opcode
pub fop: u32,
}
impl Registers for X87FpuInternalRegs {
type ProgramCounter = u32;
// HACK: this struct is never used as an architecture's main register file, so
// using a dummy value here is fine.
fn pc(&self) -> Self::ProgramCounter {
0
}
fn gdb_serialize(&self, mut write_byte: impl FnMut(Option<u8>)) {
macro_rules! write_bytes {
($bytes:expr) => {
for b in $bytes {
write_byte(Some(*b))
}
};
}
// Note: GDB section names don't make sense unless you read x87 FPU section 8.1:
// https://web.archive.org/web/20150123212110/http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf
write_bytes!(&self.fctrl.to_le_bytes());
write_bytes!(&self.fstat.to_le_bytes());
write_bytes!(&self.ftag.to_le_bytes());
write_bytes!(&self.fiseg.to_le_bytes());
write_bytes!(&self.fioff.to_le_bytes());
write_bytes!(&self.foseg.to_le_bytes());
write_bytes!(&self.fooff.to_le_bytes());
write_bytes!(&self.fop.to_le_bytes());
}
fn gdb_deserialize(&mut self, bytes: &[u8]) -> Result<(), ()> {
if bytes.len() != 0x20 {
return Err(());
}
let mut regs = bytes
.chunks_exact(4)
.map(|x| u32::from_le_bytes(x.try_into().unwrap()));
self.fctrl = regs.next().ok_or(())?;
self.fstat = regs.next().ok_or(())?;
self.ftag = regs.next().ok_or(())?;
self.fiseg = regs.next().ok_or(())?;
self.fioff = regs.next().ok_or(())?;
self.foseg = regs.next().ok_or(())?;
self.fooff = regs.next().ok_or(())?;
self.fop = regs.next().ok_or(())?;
Ok(())
}
}
/// x86 segment registers.
///
/// Source: <https://github.com/bminor/binutils-gdb/blob/master/gdb/features/i386/64bit-core.xml>
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct X86SegmentRegs {
/// Code Segment
pub cs: u32,
/// Stack Segment
pub ss: u32,
/// Data Segment
pub ds: u32,
/// Extra Segment
pub es: u32,
/// General Purpose Segment
pub fs: u32,
/// General Purpose Segment
pub gs: u32,
}
impl Registers for X86SegmentRegs {
type ProgramCounter = u32;
// HACK: this struct is never used as an architecture's main register file, so
// using a dummy value here is fine.
fn pc(&self) -> Self::ProgramCounter {
0
}
fn gdb_serialize(&self, mut write_byte: impl FnMut(Option<u8>)) {
macro_rules! write_bytes {
($bytes:expr) => {
for b in $bytes {
write_byte(Some(*b))
}
};
}
write_bytes!(&self.cs.to_le_bytes());
write_bytes!(&self.ss.to_le_bytes());
write_bytes!(&self.ds.to_le_bytes());
write_bytes!(&self.es.to_le_bytes());
write_bytes!(&self.fs.to_le_bytes());
write_bytes!(&self.gs.to_le_bytes());
}
fn gdb_deserialize(&mut self, bytes: &[u8]) -> Result<(), ()> {
if bytes.len() != core::mem::size_of::<u32>() * 6 {
return Err(());
}
let mut regs = bytes
.chunks_exact(4)
.map(|x| u32::from_le_bytes(x.try_into().unwrap()));
self.cs = regs.next().ok_or(())?;
self.ss = regs.next().ok_or(())?;
self.ds = regs.next().ok_or(())?;
self.es = regs.next().ok_or(())?;
self.fs = regs.next().ok_or(())?;
self.gs = regs.next().ok_or(())?;
Ok(())
}
}