vmi_core/os/
thread.rs

1use serde::{Deserialize, Serialize};
2
3use super::VmiOs;
4use crate::{Va, VmiDriver, VmiError, VmiVa};
5
6/// A thread ID within a system.
7#[derive(
8    Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
9)]
10pub struct ThreadId(pub u32);
11
12impl From<u32> for ThreadId {
13    fn from(value: u32) -> Self {
14        Self(value)
15    }
16}
17
18impl From<ThreadId> for u32 {
19    fn from(value: ThreadId) -> Self {
20        value.0
21    }
22}
23
24impl std::fmt::Display for ThreadId {
25    fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
26        write!(f, "{}", self.0)
27    }
28}
29
30/// A thread object within a system.
31///
32/// Equivalent to `ETHREAD*` on Windows.
33#[derive(
34    Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
35)]
36pub struct ThreadObject(pub Va);
37
38impl ThreadObject {
39    /// Checks if the thread object is a null reference.
40    pub fn is_null(&self) -> bool {
41        self.0.0 == 0
42    }
43
44    /// Converts the thread object to a 64-bit unsigned integer.
45    pub fn to_u64(&self) -> u64 {
46        self.0.0
47    }
48}
49
50impl From<Va> for ThreadObject {
51    fn from(value: Va) -> Self {
52        Self(value)
53    }
54}
55
56impl From<ThreadObject> for Va {
57    fn from(value: ThreadObject) -> Self {
58        value.0
59    }
60}
61
62impl std::fmt::Display for ThreadObject {
63    fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
64        write!(f, "{}", self.0)
65    }
66}
67
68/// A trait for thread objects.
69///
70/// This trait provides an abstraction over threads within a guest OS.
71pub trait VmiOsThread<'a, Driver>: VmiVa + 'a
72where
73    Driver: VmiDriver,
74{
75    /// The VMI OS type.
76    type Os: VmiOs<Driver>;
77
78    /// Returns the thread ID.
79    fn id(&self) -> Result<ThreadId, VmiError>;
80
81    /// Returns the thread object.
82    fn object(&self) -> Result<ThreadObject, VmiError>;
83}