rustubs/mm/
vmm.rs

1//! a very simple virtual memory manager
2
3use alloc::string::String;
4use alloc::vec::Vec;
5use bitflags::bitflags;
6use core::fmt;
7use core::ops::Range;
8
9pub struct VMMan {
10	pub vmas: Vec<VMArea>,
11}
12
13impl VMMan {
14	pub fn new() -> Self { Self { vmas: Vec::<VMArea>::new() } }
15}
16
17bitflags! {
18	pub struct VMPerms: u8 {
19		const NONE = 0;
20		const R = 1 << 0;
21		const W = 1 << 1;
22		const X = 1 << 2;
23	}
24}
25
26impl fmt::Debug for VMPerms {
27	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28		write!(
29			f,
30			"{}{}{}",
31			if self.contains(Self::R) { "R" } else { "-" },
32			if self.contains(Self::W) { "W" } else { "-" },
33			if self.contains(Self::X) { "X" } else { "-" }
34		)
35	}
36}
37
38pub struct VMArea {
39	pub vm_range: Range<u64>,
40	pub tag: String,
41	pub user_perms: VMPerms,
42	pub backing: VMType,
43}
44
45impl fmt::Debug for VMArea {
46	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47		write!(
48			f,
49			"{:016X}-{:016X} {:?} - {:?} - {}",
50			self.vm_range.start,
51			self.vm_range.end,
52			self.user_perms,
53			self.backing,
54			self.tag
55		)
56	}
57}
58
59pub enum VMType {
60	ANOM,
61	FILE(&'static [u8]),
62	// NONE for device memory mappings
63	NONE,
64}
65
66impl fmt::Debug for VMType {
67	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68		write!(
69			f,
70			"{}",
71			match self {
72				Self::ANOM => "ANOM",
73				Self::FILE(_) => "FILE",
74				Self::NONE => "DEV",
75			},
76		)
77	}
78}