kdmp-parser 0.8.2

A KISS, dependency free, Rust crate to parse Windows kernel crash-dumps created by Windows & its debugger.
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Axel '0vercl0k' Souchet - May 30 2023
//! This contains types that are useful to manipulate
//! Guest Virtual Addresses ([`Gva`]) and Guest Physical Addresses ([`Gpa`]).
//! Because ultimately they are both [`u64`] under the hood, a lot of operations
//! apply to both [`Gva`] & [`Gpa`] ([`Gxa::page_align`], etc.) and those are
//! implemented into the parent trait [`Gxa`].
//!
//! # Examples
//!
//! ```
//! use kdmp_parser::gxa::{Gxa, Gva};
//! let gva = Gva::new(1337);
//! let page_aligned_gva = gva.page_align();
//! let page_offset = gva.offset();
//! ```
use std::fmt::{self, Debug, Display};
use std::num::ParseIntError;
use std::ops::AddAssign;
use std::str::FromStr;

use crate::pxe::Pfn;
use crate::structs::PageKind;

/// A bunch of useful methods to manipulate 64-bit addresses of
/// any kind.
pub trait Gxa: Sized + Default + Copy + From<u64> {
    /// Get the underlying [`u64`] out of it.
    fn u64(&self) -> u64;

    /// Get the page offset.
    fn offset(&self) -> u64 {
        self.u64() & 0xf_ff
    }

    /// Is it page aligned?
    #[must_use]
    fn page_aligned(&self) -> bool {
        self.offset() == 0
    }

    /// Page-align it.
    #[must_use]
    fn page_align(&self) -> Self {
        Self::from(self.u64() & !0xf_ff)
    }

    /// Get the next aligned page.
    #[must_use]
    fn next_aligned_page(self) -> Self {
        Self::from(
            self.page_align()
                .u64()
                .checked_add(PageKind::Normal.size())
                .expect("Cannot overflow"),
        )
    }
}

/// Strong type for Guest Physical Addresses.
///
/// # Examples
///
/// ```
/// # use kdmp_parser::gxa::{Gxa, Gpa};
/// # fn main() {
/// let gpa = Gpa::new(0x1337_123);
/// assert_eq!(gpa.offset(), 0x123);
/// assert_eq!(gpa.page_aligned(), false);
/// let aligned_gpa = gpa.page_align();
/// assert_eq!(aligned_gpa.u64(), 0x1337_000);
/// assert_eq!(aligned_gpa.page_aligned(), true);
/// let next_gpa = gpa.next_aligned_page();
/// assert_eq!(next_gpa.u64(), 0x1338_000);
/// # }
/// ```
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Default, PartialOrd, Ord)]
pub struct Gpa(u64);

impl Gpa {
    /// Create a new [`Gpa`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::new(1337);
    /// # }
    /// ```
    #[must_use]
    pub const fn new(addr: u64) -> Self {
        Self(addr)
    }

    /// Create a new [`Gpa`] from a Page Frame Number or PFN.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::pxe::Pfn;
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::from_pfn(Pfn::new(0x1337));
    /// assert_eq!(gpa.u64(), 0x1337_000);
    /// # }
    /// ```
    #[must_use]
    pub const fn from_pfn(pfn: Pfn) -> Self {
        Self(pfn.u64() << (4 * 3))
    }

    /// Create a new [`Gpa`] from a Page Frame Number or PFN and an added
    /// offset.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::pxe::Pfn;
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::from_pfn_with_offset(Pfn::new(0x1337), 0x11);
    /// assert_eq!(gpa.u64(), 0x1337_011);
    /// # }
    /// ```
    #[must_use]
    pub const fn from_pfn_with_offset(pfn: Pfn, offset: u64) -> Self {
        let base = pfn.u64() << (4 * 3);

        Self(base + offset)
    }

    /// Get the Page Frame Number from a [`Gpa`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::new(0x1337_337);
    /// assert_eq!(gpa.pfn(), 0x1337);
    /// # }
    /// ```
    #[must_use]
    pub const fn pfn(&self) -> u64 {
        self.0 >> (4 * 3)
    }
}

/// Operator += for [`Gpa`].
impl AddAssign for Gpa {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl Gxa for Gpa {
    /// Get the underlying [`u64`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::new(1337);
    /// assert_eq!(gpa.u64(), 1337);
    /// # }
    /// ```
    fn u64(&self) -> u64 {
        self.0
    }
}

/// Convert a [`u64`] into a [`Gpa`].
impl From<u64> for Gpa {
    /// Create a [`Gpa`] from a [`u64`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::from(0xdeadbeef_baadc0de);
    /// assert_eq!(u64::from(gpa), 0xdeadbeef_baadc0de);
    /// # }
    /// ```
    fn from(value: u64) -> Self {
        Gpa(value)
    }
}

/// Convert a [`Gpa`] into a [`u64`].
impl From<Gpa> for u64 {
    /// Create a [`u64`] from a [`Gpa`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::new(0xdeadbeef_baadc0de);
    /// let gpa_u64: u64 = gpa.into();
    /// assert_eq!(gpa_u64, 0xdeadbeef_baadc0de);
    /// assert_eq!(u64::from(gpa), 0xdeadbeef_baadc0de);
    /// # }
    /// ```
    fn from(value: Gpa) -> Self {
        value.0
    }
}

/// Convert a [`&Gpa`][`Gpa`] into a [`u64`].
impl From<&Gpa> for u64 {
    /// Create a [`u64`] from a [`&Gpa`][`Gpa`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gpa = Gpa::new(0xdeadbeef_baadc0de);
    /// let gpa_p = &gpa;
    /// let gpa_u64: u64 = gpa_p.into();
    /// assert_eq!(gpa_u64, 0xdeadbeef_baadc0de);
    /// assert_eq!(u64::from(gpa_p), 0xdeadbeef_baadc0de);
    /// # }
    /// ```
    fn from(value: &Gpa) -> Self {
        value.0
    }
}

/// Format a [`Gpa`] as a string.
impl Display for Gpa {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "GPA:{:#x}", self.0)
    }
}

/// Parse a [`Gpa`] from a string.
impl FromStr for Gpa {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.replace('`', "");

        Ok(Gpa::new(u64::from_str_radix(
            s.trim_start_matches("0x"),
            16,
        )?))
    }
}

/// Strong type for Guest Virtual Addresses.
///
/// # Examples
///
/// ```
/// # use kdmp_parser::gxa::{Gxa, Gva};
/// # fn main() {
/// let gva = Gva::new(0x1337_fff);
/// assert_eq!(gva.offset(), 0xfff);
/// assert_eq!(gva.page_aligned(), false);
/// let aligned_gva = gva.page_align();
/// assert_eq!(aligned_gva.u64(), 0x1337_000);
/// assert_eq!(aligned_gva.page_aligned(), true);
/// let next_gva = gva.next_aligned_page();
/// assert_eq!(next_gva.u64(), 0x1338_000);
/// # }
/// ```
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Default, PartialOrd, Ord)]
pub struct Gva(u64);

impl Gva {
    /// Create a new [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let gva = Gva::new(0xdeadbeef);
    /// # }
    /// ```
    #[must_use]
    pub const fn new(addr: u64) -> Self {
        Self(addr)
    }

    /// Get the PTE index of the [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let first = Gva::new(0xff_ff_b9_dc_ee_77_31_37);
    /// assert_eq!(first.pte_idx(), 371);
    /// let second = Gva::new(0xff_ff_11_22_33_44_55_66);
    /// assert_eq!(second.pte_idx(), 0x45);
    /// # }
    /// ```
    #[allow(clippy::erasing_op, clippy::identity_op)]
    #[must_use]
    pub const fn pte_idx(&self) -> u64 {
        (self.0 >> (12 + (9 * 0))) & 0b1_1111_1111
    }

    /// Get the PDE index of the [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let first = Gva::new(0xff_ff_b9_dc_ee_77_31_37);
    /// assert_eq!(first.pde_idx(), 371);
    /// let second = Gva::new(0xff_ff_11_22_33_44_55_66);
    /// assert_eq!(second.pde_idx(), 0x19a);
    /// # }
    /// ```
    #[allow(clippy::identity_op)]
    #[must_use]
    pub const fn pde_idx(&self) -> u64 {
        (self.0 >> (12 + (9 * 1))) & 0b1_1111_1111
    }

    /// Get the PDPE offset of the [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let first = Gva::new(0xff_ff_b9_dc_ee_77_31_37);
    /// assert_eq!(first.pdpe_idx(), 371);
    /// let second = Gva::new(0xff_ff_11_22_33_44_55_66);
    /// assert_eq!(second.pdpe_idx(), 0x88);
    /// # }
    /// ```
    #[must_use]
    pub const fn pdpe_idx(&self) -> u64 {
        (self.0 >> (12 + (9 * 2))) & 0b1_1111_1111
    }

    /// Get the PML4 index of the [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let first = Gva::new(0xff_ff_b9_dc_ee_77_31_37);
    /// assert_eq!(first.pml4e_idx(), 371);
    /// let second = Gva::new(0xff_ff_11_22_33_44_55_66);
    /// assert_eq!(second.pml4e_idx(), 0x22);
    /// # }
    /// ```
    #[must_use]
    pub fn pml4e_idx(&self) -> u64 {
        (self.0 >> (12 + (9 * 3))) & 0b1_1111_1111
    }
}

/// Operator += for [`Gva`].
impl AddAssign for Gva {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl Gxa for Gva {
    /// Get the underlying `u64`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let gva = Gva::new(0xdeadbeef);
    /// assert_eq!(gva.u64(), 0xdeadbeef);
    /// # }
    /// ```
    fn u64(&self) -> u64 {
        self.0
    }
}

/// Convert a [`Gva`] into a [`u64`].
impl From<u64> for Gva {
    /// Create a [`Gva`] from a [`u64`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let gva = Gva::from(0xbaadc0de_deadbeef);
    /// assert_eq!(u64::from(gva), 0xbaadc0de_deadbeef);
    /// # }
    /// ```
    fn from(value: u64) -> Self {
        Gva(value)
    }
}

/// Convert a [`Gva`] into a [`u64`].
impl From<Gva> for u64 {
    /// Create a [`u64`] from a [`Gva`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gva};
    /// # fn main() {
    /// let gva = Gva::new(0xbaadc0de_deadbeef);
    /// let gva_u64: u64 = gva.into();
    /// assert_eq!(gva_u64, 0xbaadc0de_deadbeef);
    /// assert_eq!(u64::from(gva), 0xbaadc0de_deadbeef);
    /// # }
    /// ```
    fn from(value: Gva) -> Self {
        value.0
    }
}

/// Convert a [`&Gva`][Gva] into a [`u64`].
impl From<&Gva> for u64 {
    /// Create a [`u64`] from a [&Gpa][`Gpa`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kdmp_parser::gxa::{Gxa, Gpa};
    /// # fn main() {
    /// let gva = Gpa::new(0xbaadc0de_deadbeef);
    /// let gva_p = &gva;
    /// let gva_u64: u64 = gva_p.into();
    /// assert_eq!(gva_u64, 0xbaadc0de_deadbeef);
    /// assert_eq!(u64::from(gva_p), 0xbaadc0de_deadbeef);
    /// # }
    /// ```
    fn from(value: &Gva) -> Self {
        value.0
    }
}

/// Format [`Gva`] as a string.
impl Display for Gva {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Gva:{:#x}", self.0)
    }
}