vmi_os_linux/comps/
vm_area_struct.rs1use once_cell::unsync::OnceCell;
2use vmi_core::{
3 MemoryAccess, Va, VmiError, VmiState, VmiVa,
4 driver::VmiRead,
5 os::{VmiOsRegion, VmiOsRegionKind},
6};
7
8use super::macros::impl_offsets;
9use crate::{ArchAdapter, LinuxOs};
10
11pub struct LinuxVmAreaStruct<'a, Driver>
20where
21 Driver: VmiRead,
22 Driver::Architecture: ArchAdapter<Driver>,
23{
24 vmi: VmiState<'a, LinuxOs<Driver>>,
26
27 va: Va,
29
30 flags: OnceCell<u64>,
32}
33
34impl<Driver> VmiVa for LinuxVmAreaStruct<'_, Driver>
35where
36 Driver: VmiRead,
37 Driver::Architecture: ArchAdapter<Driver>,
38{
39 fn va(&self) -> Va {
40 self.va
41 }
42}
43
44impl<Driver> std::fmt::Debug for LinuxVmAreaStruct<'_, Driver>
45where
46 Driver: VmiRead,
47 Driver::Architecture: ArchAdapter<Driver>,
48{
49 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
50 let start = self.start();
51 let end = self.end();
52 let protection = self.protection();
53 f.debug_struct("LinuxVmAreaStruct")
56 .field("start", &start)
57 .field("end", &end)
58 .field("protection", &protection)
59 .finish()
61 }
62}
63
64impl<'a, Driver> LinuxVmAreaStruct<'a, Driver>
65where
66 Driver: VmiRead,
67 Driver::Architecture: ArchAdapter<Driver>,
68{
69 impl_offsets!();
70
71 pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, vad: Va) -> Self {
73 Self {
74 vmi,
75 va: vad,
76 flags: OnceCell::new(),
77 }
78 }
79
80 pub fn flags(&self) -> Result<u64, VmiError> {
93 self.flags
94 .get_or_try_init(|| {
95 let offsets = self.offsets();
96 let __vm_area_struct = &offsets.vm_area_struct;
97
98 self.vmi.read_field(self.va, &__vm_area_struct.vm_flags)
99 })
100 .copied()
101 }
102}
103
104impl<'a, Driver> VmiOsRegion<'a, Driver> for LinuxVmAreaStruct<'a, Driver>
105where
106 Driver: VmiRead,
107 Driver::Architecture: ArchAdapter<Driver>,
108{
109 type Os = LinuxOs<Driver>;
110
111 fn start(&self) -> Result<Va, VmiError> {
117 let offsets = self.offsets();
118 let __vm_area_struct = &offsets.vm_area_struct;
119
120 self.vmi
121 .read_va_native(self.va + __vm_area_struct.vm_start.offset())
122 }
123
124 fn end(&self) -> Result<Va, VmiError> {
130 let offsets = self.offsets();
131 let __vm_area_struct = &offsets.vm_area_struct;
132
133 self.vmi
134 .read_va_native(self.va + __vm_area_struct.vm_end.offset())
135 }
136
137 fn protection(&self) -> Result<MemoryAccess, VmiError> {
143 const VM_READ: u64 = 0x00000001;
144 const VM_WRITE: u64 = 0x00000002;
145 const VM_EXEC: u64 = 0x00000004;
146 let flags = self.flags()?;
149 let mut protection = MemoryAccess::default();
150 if flags & VM_READ != 0 {
151 protection |= MemoryAccess::R;
152 }
153 if flags & VM_WRITE != 0 {
154 protection |= MemoryAccess::W;
155 }
156 if flags & VM_EXEC != 0 {
157 protection |= MemoryAccess::X;
158 }
159
160 Ok(protection)
161 }
162
163 fn kind(&self) -> Result<VmiOsRegionKind<'a, Self::Os>, VmiError> {
165 let offsets = self.offsets();
166 let __vm_area_struct = &offsets.vm_area_struct;
167
168 let file = self
169 .vmi
170 .read_va_native(self.va + __vm_area_struct.vm_file.offset())?;
171
172 if file.is_null() {
173 return Ok(VmiOsRegionKind::Private);
174 }
175
176 unimplemented!()
177 }
178}