mod.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. use alloc::sync::Arc;
  2. use page::EntryFlags;
  3. use system_error::SystemError;
  4. use crate::arch::MMArch;
  5. use core::{
  6. cmp,
  7. fmt::Debug,
  8. intrinsics::unlikely,
  9. ops::{Add, AddAssign, Sub, SubAssign},
  10. ptr,
  11. sync::atomic::{AtomicBool, Ordering},
  12. };
  13. use self::{
  14. allocator::page_frame::{VirtPageFrame, VirtPageFrameIter},
  15. memblock::MemoryAreaAttr,
  16. page::round_up_to_page_size,
  17. ucontext::{AddressSpace, LockedVMA, UserMapper},
  18. };
  19. pub mod allocator;
  20. pub mod c_adapter;
  21. pub mod early_ioremap;
  22. pub mod fault;
  23. pub mod init;
  24. pub mod kernel_mapper;
  25. pub mod madvise;
  26. pub mod memblock;
  27. pub mod mmio_buddy;
  28. pub mod no_init;
  29. pub mod page;
  30. pub mod percpu;
  31. pub mod syscall;
  32. pub mod ucontext;
  33. /// 内核INIT进程的用户地址空间结构体(仅在process_init中初始化)
  34. static mut __IDLE_PROCESS_ADDRESS_SPACE: Option<Arc<AddressSpace>> = None;
  35. bitflags! {
  36. /// Virtual memory flags
  37. #[allow(clippy::bad_bit_mask)]
  38. pub struct VmFlags:usize{
  39. const VM_NONE = 0x00000000;
  40. const VM_READ = 0x00000001;
  41. const VM_WRITE = 0x00000002;
  42. const VM_EXEC = 0x00000004;
  43. const VM_SHARED = 0x00000008;
  44. const VM_MAYREAD = 0x00000010;
  45. const VM_MAYWRITE = 0x00000020;
  46. const VM_MAYEXEC = 0x00000040;
  47. const VM_MAYSHARE = 0x00000080;
  48. const VM_GROWSDOWN = 0x00000100;
  49. const VM_UFFD_MISSING = 0x00000200;
  50. const VM_PFNMAP = 0x00000400;
  51. const VM_UFFD_WP = 0x00001000;
  52. const VM_LOCKED = 0x00002000;
  53. const VM_IO = 0x00004000;
  54. const VM_SEQ_READ = 0x00008000;
  55. const VM_RAND_READ = 0x00010000;
  56. const VM_DONTCOPY = 0x00020000;
  57. const VM_DONTEXPAND = 0x00040000;
  58. const VM_LOCKONFAULT = 0x00080000;
  59. const VM_ACCOUNT = 0x00100000;
  60. const VM_NORESERVE = 0x00200000;
  61. const VM_HUGETLB = 0x00400000;
  62. const VM_SYNC = 0x00800000;
  63. const VM_ARCH_1 = 0x01000000;
  64. const VM_WIPEONFORK = 0x02000000;
  65. const VM_DONTDUMP = 0x04000000;
  66. }
  67. /// 描述页面错误处理过程中发生的不同情况或结果
  68. pub struct VmFaultReason:u32 {
  69. const VM_FAULT_OOM = 0x000001;
  70. const VM_FAULT_SIGBUS = 0x000002;
  71. const VM_FAULT_MAJOR = 0x000004;
  72. const VM_FAULT_WRITE = 0x000008;
  73. const VM_FAULT_HWPOISON = 0x000010;
  74. const VM_FAULT_HWPOISON_LARGE = 0x000020;
  75. const VM_FAULT_SIGSEGV = 0x000040;
  76. const VM_FAULT_NOPAGE = 0x000100;
  77. const VM_FAULT_LOCKED = 0x000200;
  78. const VM_FAULT_RETRY = 0x000400;
  79. const VM_FAULT_FALLBACK = 0x000800;
  80. const VM_FAULT_DONE_COW = 0x001000;
  81. const VM_FAULT_NEEDDSYNC = 0x002000;
  82. const VM_FAULT_COMPLETED = 0x004000;
  83. const VM_FAULT_HINDEX_MASK = 0x0f0000;
  84. const VM_FAULT_ERROR = 0x000001 | 0x000002 | 0x000040 | 0x000010 | 0x000020 | 0x000800;
  85. }
  86. pub struct MsFlags:usize {
  87. const MS_ASYNC = 1;
  88. const MS_INVALIDATE = 2;
  89. const MS_SYNC = 4;
  90. }
  91. }
  92. impl core::ops::Index<VmFlags> for [usize] {
  93. type Output = usize;
  94. fn index(&self, index: VmFlags) -> &Self::Output {
  95. &self[index.bits]
  96. }
  97. }
  98. impl core::ops::IndexMut<VmFlags> for [usize] {
  99. fn index_mut(&mut self, index: VmFlags) -> &mut Self::Output {
  100. &mut self[index.bits]
  101. }
  102. }
  103. /// 获取内核IDLE进程的用户地址空间结构体
  104. #[allow(non_snake_case)]
  105. #[inline(always)]
  106. pub fn IDLE_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace> {
  107. unsafe {
  108. return __IDLE_PROCESS_ADDRESS_SPACE
  109. .as_ref()
  110. .expect("IDLE_PROCESS_ADDRESS_SPACE is null")
  111. .clone();
  112. }
  113. }
  114. /// 设置内核IDLE进程的用户地址空间结构体全局变量
  115. #[allow(non_snake_case)]
  116. pub unsafe fn set_IDLE_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>) {
  117. static INITIALIZED: AtomicBool = AtomicBool::new(false);
  118. if INITIALIZED
  119. .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
  120. .is_err()
  121. {
  122. panic!("IDLE_PROCESS_ADDRESS_SPACE is already initialized");
  123. }
  124. __IDLE_PROCESS_ADDRESS_SPACE = Some(address_space);
  125. }
  126. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
  127. pub enum PageTableKind {
  128. /// 用户可访问的页表
  129. User,
  130. /// 内核页表
  131. Kernel,
  132. /// x86内存虚拟化中使用的EPT
  133. #[cfg(target_arch = "x86_64")]
  134. EPT,
  135. }
  136. /// 物理内存地址
  137. #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
  138. #[repr(transparent)]
  139. pub struct PhysAddr(usize);
  140. impl PhysAddr {
  141. /// 最大物理地址
  142. pub const MAX: Self = PhysAddr(usize::MAX);
  143. #[inline(always)]
  144. pub const fn new(address: usize) -> Self {
  145. Self(address)
  146. }
  147. /// @brief 获取物理地址的值
  148. #[inline(always)]
  149. pub const fn data(&self) -> usize {
  150. self.0
  151. }
  152. /// @brief 将物理地址加上一个偏移量
  153. #[inline(always)]
  154. pub fn add(self, offset: usize) -> Self {
  155. Self(self.0 + offset)
  156. }
  157. /// @brief 判断物理地址是否按照指定要求对齐
  158. #[inline(always)]
  159. pub fn check_aligned(&self, align: usize) -> bool {
  160. return self.0 & (align - 1) == 0;
  161. }
  162. #[inline(always)]
  163. pub fn is_null(&self) -> bool {
  164. return self.0 == 0;
  165. }
  166. }
  167. impl Debug for PhysAddr {
  168. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  169. write!(f, "PhysAddr({:#x})", self.0)
  170. }
  171. }
  172. impl core::ops::Add<usize> for PhysAddr {
  173. type Output = Self;
  174. #[inline(always)]
  175. fn add(self, rhs: usize) -> Self::Output {
  176. return Self(self.0 + rhs);
  177. }
  178. }
  179. impl core::ops::AddAssign<usize> for PhysAddr {
  180. #[inline(always)]
  181. fn add_assign(&mut self, rhs: usize) {
  182. self.0 += rhs;
  183. }
  184. }
  185. impl core::ops::Add<PhysAddr> for PhysAddr {
  186. type Output = Self;
  187. #[inline(always)]
  188. fn add(self, rhs: PhysAddr) -> Self::Output {
  189. return Self(self.0 + rhs.0);
  190. }
  191. }
  192. impl core::ops::AddAssign<PhysAddr> for PhysAddr {
  193. #[inline(always)]
  194. fn add_assign(&mut self, rhs: PhysAddr) {
  195. self.0 += rhs.0;
  196. }
  197. }
  198. impl core::ops::BitOrAssign<usize> for PhysAddr {
  199. #[inline(always)]
  200. fn bitor_assign(&mut self, rhs: usize) {
  201. self.0 |= rhs;
  202. }
  203. }
  204. impl core::ops::BitOrAssign<PhysAddr> for PhysAddr {
  205. #[inline(always)]
  206. fn bitor_assign(&mut self, rhs: PhysAddr) {
  207. self.0 |= rhs.0;
  208. }
  209. }
  210. impl core::ops::Sub<usize> for PhysAddr {
  211. type Output = Self;
  212. #[inline(always)]
  213. fn sub(self, rhs: usize) -> Self::Output {
  214. return Self(self.0 - rhs);
  215. }
  216. }
  217. impl core::ops::SubAssign<usize> for PhysAddr {
  218. #[inline(always)]
  219. fn sub_assign(&mut self, rhs: usize) {
  220. self.0 -= rhs;
  221. }
  222. }
  223. impl core::ops::Sub<PhysAddr> for PhysAddr {
  224. type Output = usize;
  225. #[inline(always)]
  226. fn sub(self, rhs: PhysAddr) -> Self::Output {
  227. return self.0 - rhs.0;
  228. }
  229. }
  230. impl core::ops::SubAssign<PhysAddr> for PhysAddr {
  231. #[inline(always)]
  232. fn sub_assign(&mut self, rhs: PhysAddr) {
  233. self.0 -= rhs.0;
  234. }
  235. }
  236. /// 虚拟内存地址
  237. #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
  238. #[repr(transparent)]
  239. pub struct VirtAddr(usize);
  240. impl VirtAddr {
  241. #[inline(always)]
  242. pub const fn new(address: usize) -> Self {
  243. return Self(address);
  244. }
  245. /// @brief 获取虚拟地址的值
  246. #[inline(always)]
  247. pub const fn data(&self) -> usize {
  248. return self.0;
  249. }
  250. /// @brief 判断虚拟地址的类型
  251. #[inline(always)]
  252. pub fn kind(&self) -> PageTableKind {
  253. if self.check_user() {
  254. return PageTableKind::User;
  255. } else {
  256. return PageTableKind::Kernel;
  257. }
  258. }
  259. /// @brief 判断虚拟地址是否按照指定要求对齐
  260. #[inline(always)]
  261. pub fn check_aligned(&self, align: usize) -> bool {
  262. return self.0 & (align - 1) == 0;
  263. }
  264. /// @brief 判断虚拟地址是否在用户空间
  265. #[inline(always)]
  266. pub fn check_user(&self) -> bool {
  267. return self < &MMArch::USER_END_VADDR;
  268. }
  269. #[inline(always)]
  270. pub fn as_ptr<T>(self) -> *mut T {
  271. return self.0 as *mut T;
  272. }
  273. #[inline(always)]
  274. pub fn is_null(&self) -> bool {
  275. return self.0 == 0;
  276. }
  277. }
  278. impl Add<VirtAddr> for VirtAddr {
  279. type Output = Self;
  280. #[inline(always)]
  281. fn add(self, rhs: VirtAddr) -> Self::Output {
  282. return Self(self.0 + rhs.0);
  283. }
  284. }
  285. impl Add<usize> for VirtAddr {
  286. type Output = Self;
  287. #[inline(always)]
  288. fn add(self, rhs: usize) -> Self::Output {
  289. return Self(self.0 + rhs);
  290. }
  291. }
  292. impl Sub<VirtAddr> for VirtAddr {
  293. type Output = usize;
  294. #[inline(always)]
  295. fn sub(self, rhs: VirtAddr) -> Self::Output {
  296. return self.0 - rhs.0;
  297. }
  298. }
  299. impl Sub<usize> for VirtAddr {
  300. type Output = Self;
  301. #[inline(always)]
  302. fn sub(self, rhs: usize) -> Self::Output {
  303. return Self(self.0 - rhs);
  304. }
  305. }
  306. impl AddAssign<usize> for VirtAddr {
  307. #[inline(always)]
  308. fn add_assign(&mut self, rhs: usize) {
  309. self.0 += rhs;
  310. }
  311. }
  312. impl AddAssign<VirtAddr> for VirtAddr {
  313. #[inline(always)]
  314. fn add_assign(&mut self, rhs: VirtAddr) {
  315. self.0 += rhs.0;
  316. }
  317. }
  318. impl SubAssign<usize> for VirtAddr {
  319. #[inline(always)]
  320. fn sub_assign(&mut self, rhs: usize) {
  321. self.0 -= rhs;
  322. }
  323. }
  324. impl SubAssign<VirtAddr> for VirtAddr {
  325. #[inline(always)]
  326. fn sub_assign(&mut self, rhs: VirtAddr) {
  327. self.0 -= rhs.0;
  328. }
  329. }
  330. impl Debug for VirtAddr {
  331. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  332. write!(f, "VirtAddr({:#x})", self.0)
  333. }
  334. }
  335. /// @brief 物理内存区域
  336. #[derive(Clone, Copy, Debug)]
  337. pub struct PhysMemoryArea {
  338. /// 物理基地址
  339. pub base: PhysAddr,
  340. /// 该区域的物理内存大小
  341. pub size: usize,
  342. pub flags: MemoryAreaAttr,
  343. }
  344. impl PhysMemoryArea {
  345. pub const DEFAULT: Self = Self {
  346. base: PhysAddr::new(0),
  347. size: 0,
  348. flags: MemoryAreaAttr::empty(),
  349. };
  350. pub fn new(base: PhysAddr, size: usize, flags: MemoryAreaAttr) -> Self {
  351. Self { base, size, flags }
  352. }
  353. /// 返回向上页面对齐的区域起始物理地址
  354. pub fn area_base_aligned(&self) -> PhysAddr {
  355. return PhysAddr::new(
  356. (self.base.data() + (MMArch::PAGE_SIZE - 1)) & !(MMArch::PAGE_SIZE - 1),
  357. );
  358. }
  359. /// 返回向下页面对齐的区域截止物理地址
  360. pub fn area_end_aligned(&self) -> PhysAddr {
  361. return PhysAddr::new((self.base.data() + self.size) & !(MMArch::PAGE_SIZE - 1));
  362. }
  363. }
  364. impl Default for PhysMemoryArea {
  365. fn default() -> Self {
  366. return Self::DEFAULT;
  367. }
  368. }
  369. #[allow(dead_code)]
  370. pub trait MemoryManagementArch: Clone + Copy + Debug {
  371. /// 是否支持缺页中断
  372. const PAGE_FAULT_ENABLED: bool;
  373. /// 页面大小的shift(假如页面4K,那么这个值就是12,因为2^12=4096)
  374. const PAGE_SHIFT: usize;
  375. /// 每个页表的页表项数目。(以2^n次幂来表示)假如有512个页表项,那么这个值就是9
  376. const PAGE_ENTRY_SHIFT: usize;
  377. /// 页表层级数量
  378. const PAGE_LEVELS: usize;
  379. /// 页表项的有效位的index(假如页表项的第0-51位有效,那么这个值就是52)
  380. const ENTRY_ADDRESS_SHIFT: usize;
  381. /// 页面的页表项的默认值
  382. const ENTRY_FLAG_DEFAULT_PAGE: usize;
  383. /// 页表的页表项的默认值
  384. const ENTRY_FLAG_DEFAULT_TABLE: usize;
  385. /// 页表项的present位被置位之后的值
  386. const ENTRY_FLAG_PRESENT: usize;
  387. /// 页表项为read only时的值
  388. const ENTRY_FLAG_READONLY: usize;
  389. /// 页表项的write bit
  390. const ENTRY_FLAG_WRITEABLE: usize;
  391. /// 页表项为可读写状态的值
  392. const ENTRY_FLAG_READWRITE: usize;
  393. /// 页面项标记页面为user page的值
  394. const ENTRY_FLAG_USER: usize;
  395. /// 页面项标记页面为write through的值
  396. const ENTRY_FLAG_WRITE_THROUGH: usize;
  397. /// 页面项标记页面为cache disable的值
  398. const ENTRY_FLAG_CACHE_DISABLE: usize;
  399. /// 标记当前页面不可执行的标志位(Execute disable)(也就是说,不能从这段内存里面获取处理器指令)
  400. const ENTRY_FLAG_NO_EXEC: usize;
  401. /// 标记当前页面可执行的标志位(Execute enable)
  402. const ENTRY_FLAG_EXEC: usize;
  403. /// 当该位为1时,标明这是一个脏页
  404. const ENTRY_FLAG_DIRTY: usize;
  405. /// 当该位为1时,代表这个页面被处理器访问过
  406. const ENTRY_FLAG_ACCESSED: usize;
  407. /// 标记该页表项指向的页是否为大页
  408. const ENTRY_FLAG_HUGE_PAGE: usize;
  409. /// 当该位为1时,代表该页表项是全局的
  410. const ENTRY_FLAG_GLOBAL: usize;
  411. /// 虚拟地址与物理地址的偏移量
  412. const PHYS_OFFSET: usize;
  413. /// 内核在链接时被链接到的偏移量
  414. const KERNEL_LINK_OFFSET: usize;
  415. const KERNEL_VIRT_START: usize = Self::PHYS_OFFSET + Self::KERNEL_LINK_OFFSET;
  416. /// 每个页面的大小
  417. const PAGE_SIZE: usize = 1 << Self::PAGE_SHIFT;
  418. /// 通过这个mask,获取地址的页内偏移量
  419. const PAGE_OFFSET_MASK: usize = Self::PAGE_SIZE - 1;
  420. /// 通过这个mask,获取页的首地址
  421. const PAGE_MASK: usize = !(Self::PAGE_OFFSET_MASK);
  422. /// 页表项的地址、数据部分的shift。
  423. /// 打个比方,如果这个值为52,那么意味着页表项的[0, 52)位,用于表示地址以及其他的标志位
  424. const PAGE_ADDRESS_SHIFT: usize = Self::PAGE_LEVELS * Self::PAGE_ENTRY_SHIFT + Self::PAGE_SHIFT;
  425. /// 最大的虚拟地址(对于不同的架构,由于上述PAGE_ADDRESS_SHIFT可能包括了reserved bits, 事实上能表示的虚拟地址应该比这个值要小)
  426. const PAGE_ADDRESS_SIZE: usize = 1 << Self::PAGE_ADDRESS_SHIFT;
  427. /// 页表项的值与这个常量进行与运算,得到的结果是所填写的物理地址
  428. const PAGE_ADDRESS_MASK: usize = Self::PAGE_ADDRESS_SIZE - Self::PAGE_SIZE;
  429. /// 每个页表项的大小
  430. const PAGE_ENTRY_SIZE: usize = 1 << (Self::PAGE_SHIFT - Self::PAGE_ENTRY_SHIFT);
  431. /// 每个页表的页表项数目
  432. const PAGE_ENTRY_NUM: usize = 1 << Self::PAGE_ENTRY_SHIFT;
  433. /// 该字段用于根据虚拟地址,获取该虚拟地址在对应的页表中是第几个页表项
  434. const PAGE_ENTRY_MASK: usize = Self::PAGE_ENTRY_NUM - 1;
  435. /// 内核页表在顶级页表的第一个页表项的索引
  436. const PAGE_KERNEL_INDEX: usize = (Self::PHYS_OFFSET & Self::PAGE_ADDRESS_MASK)
  437. >> (Self::PAGE_ADDRESS_SHIFT - Self::PAGE_ENTRY_SHIFT);
  438. const PAGE_NEGATIVE_MASK: usize = !((Self::PAGE_ADDRESS_SIZE) - 1);
  439. const ENTRY_ADDRESS_SIZE: usize = 1 << Self::ENTRY_ADDRESS_SHIFT;
  440. /// 该mask用于获取页表项中地址字段
  441. const ENTRY_ADDRESS_MASK: usize = Self::ENTRY_ADDRESS_SIZE - Self::PAGE_SIZE;
  442. /// 这个mask用于获取页表项中的flags
  443. const ENTRY_FLAGS_MASK: usize = !Self::ENTRY_ADDRESS_MASK;
  444. /// 用户空间的最高地址
  445. const USER_END_VADDR: VirtAddr;
  446. /// 用户堆的起始地址
  447. const USER_BRK_START: VirtAddr;
  448. /// 用户栈起始地址(向下生长,不包含该值)
  449. const USER_STACK_START: VirtAddr;
  450. /// 内核的固定映射区的起始地址
  451. const FIXMAP_START_VADDR: VirtAddr;
  452. /// 内核的固定映射区的大小
  453. const FIXMAP_SIZE: usize;
  454. /// 内核的固定映射区的结束地址
  455. const FIXMAP_END_VADDR: VirtAddr =
  456. VirtAddr::new(Self::FIXMAP_START_VADDR.data() + Self::FIXMAP_SIZE);
  457. /// MMIO虚拟空间的基地址
  458. const MMIO_BASE: VirtAddr;
  459. /// MMIO虚拟空间的大小
  460. const MMIO_SIZE: usize;
  461. /// MMIO虚拟空间的顶端地址(不包含)
  462. const MMIO_TOP: VirtAddr = VirtAddr::new(Self::MMIO_BASE.data() + Self::MMIO_SIZE);
  463. /// @brief 用于初始化内存管理模块与架构相关的信息。
  464. /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用
  465. unsafe fn init();
  466. /// 内存管理初始化完成后,调用该函数
  467. unsafe fn arch_post_init() {}
  468. /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针
  469. #[inline(always)]
  470. unsafe fn read<T>(address: VirtAddr) -> T {
  471. return ptr::read(address.data() as *const T);
  472. }
  473. /// @brief 将value写入到指定的虚拟地址
  474. #[inline(always)]
  475. unsafe fn write<T>(address: VirtAddr, value: T) {
  476. ptr::write(address.data() as *mut T, value);
  477. }
  478. #[inline(always)]
  479. unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) {
  480. ptr::write_bytes(address.data() as *mut u8, value, count);
  481. }
  482. /// @brief 刷新TLB中,关于指定虚拟地址的条目
  483. unsafe fn invalidate_page(address: VirtAddr);
  484. /// @brief 刷新TLB中,所有的条目
  485. unsafe fn invalidate_all();
  486. /// @brief 获取顶级页表的物理地址
  487. unsafe fn table(table_kind: PageTableKind) -> PhysAddr;
  488. /// @brief 设置顶级页表的物理地址到处理器中
  489. unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr);
  490. /// @brief 将物理地址转换为虚拟地址.
  491. ///
  492. /// @param phys 物理地址
  493. ///
  494. /// @return 转换后的虚拟地址。如果转换失败,返回None
  495. #[inline(always)]
  496. unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> {
  497. if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) {
  498. return Some(VirtAddr::new(vaddr));
  499. } else {
  500. return None;
  501. }
  502. }
  503. /// 将虚拟地址转换为物理地址
  504. ///
  505. /// ## 参数
  506. ///
  507. /// - `virt` 虚拟地址
  508. ///
  509. /// ## 返回值
  510. ///
  511. /// 转换后的物理地址。如果转换失败,返回None
  512. #[inline(always)]
  513. unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> {
  514. if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) {
  515. return Some(PhysAddr::new(paddr));
  516. } else {
  517. return None;
  518. }
  519. }
  520. /// @brief 判断指定的虚拟地址是否正确(符合规范)
  521. fn virt_is_valid(virt: VirtAddr) -> bool;
  522. /// 获取内存管理初始化时,创建的第一个内核页表的地址
  523. fn initial_page_table() -> PhysAddr;
  524. /// 初始化新的usermapper,为用户进程创建页表
  525. fn setup_new_usermapper() -> Result<UserMapper, SystemError>;
  526. /// 创建页表项
  527. ///
  528. /// 这是一个低阶api,用于根据物理地址以及指定好的EntryFlags,创建页表项
  529. ///
  530. /// ## 参数
  531. ///
  532. /// - `paddr` 物理地址
  533. /// - `page_flags` 页表项的flags
  534. ///
  535. /// ## 返回值
  536. ///
  537. /// 页表项的值
  538. fn make_entry(paddr: PhysAddr, page_flags: usize) -> usize;
  539. /// 判断一个VMA是否允许访问
  540. ///
  541. /// ## 参数
  542. ///
  543. /// - `vma`: 进行判断的VMA
  544. /// - `write`: 是否需要写入权限(true 表示需要写权限)
  545. /// - `execute`: 是否需要执行权限(true 表示需要执行权限)
  546. /// - `foreign`: 是否是外部的(即非当前进程的)VMA
  547. ///
  548. /// ## 返回值
  549. /// - `true`: VMA允许访问
  550. /// - `false`: 错误的说明
  551. fn vma_access_permitted(
  552. _vma: Arc<LockedVMA>,
  553. _write: bool,
  554. _execute: bool,
  555. _foreign: bool,
  556. ) -> bool {
  557. true
  558. }
  559. const PAGE_NONE: usize;
  560. const PAGE_SHARED: usize;
  561. const PAGE_SHARED_EXEC: usize;
  562. const PAGE_COPY_NOEXEC: usize;
  563. const PAGE_COPY_EXEC: usize;
  564. const PAGE_COPY: usize;
  565. const PAGE_READONLY: usize;
  566. const PAGE_READONLY_EXEC: usize;
  567. const PAGE_READ: usize;
  568. const PAGE_READ_EXEC: usize;
  569. const PAGE_WRITE: usize;
  570. const PAGE_WRITE_EXEC: usize;
  571. const PAGE_EXEC: usize;
  572. const PROTECTION_MAP: [EntryFlags<Self>; 16];
  573. /// 页面保护标志转换函数
  574. /// ## 参数
  575. ///
  576. /// - `vm_flags`: VmFlags标志
  577. ///
  578. /// ## 返回值
  579. /// - EntryFlags: 页面的保护位
  580. fn vm_get_page_prot(vm_flags: VmFlags) -> EntryFlags<Self> {
  581. let map = Self::PROTECTION_MAP;
  582. let mut ret = map[vm_flags
  583. .intersection(
  584. VmFlags::VM_READ | VmFlags::VM_WRITE | VmFlags::VM_EXEC | VmFlags::VM_SHARED,
  585. )
  586. .bits()];
  587. #[cfg(target_arch = "x86_64")]
  588. {
  589. // 如果xd位被保留,那么将可执行性设置为true
  590. if crate::arch::mm::X86_64MMArch::is_xd_reserved() {
  591. ret = ret.set_execute(true);
  592. }
  593. }
  594. ret
  595. }
  596. }
  597. /// @brief 虚拟地址范围
  598. /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小
  599. ///
  600. /// 请注意与VMA进行区分,该结构体被VMA所包含
  601. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  602. pub struct VirtRegion {
  603. start: VirtAddr,
  604. size: usize,
  605. }
  606. #[allow(dead_code)]
  607. impl VirtRegion {
  608. /// # 创建一个新的虚拟地址范围
  609. pub fn new(start: VirtAddr, size: usize) -> Self {
  610. VirtRegion { start, size }
  611. }
  612. /// 获取虚拟地址范围的起始地址
  613. #[inline(always)]
  614. pub fn start(&self) -> VirtAddr {
  615. self.start
  616. }
  617. /// 获取虚拟地址范围的截止地址(不包括返回的地址)
  618. #[inline(always)]
  619. pub fn end(&self) -> VirtAddr {
  620. return self.start().add(self.size);
  621. }
  622. /// # Create a new VirtRegion from a range [start, end)
  623. ///
  624. /// If end <= start, return None
  625. pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> {
  626. if unlikely(end.data() <= start.data()) {
  627. return None;
  628. }
  629. let size = end.data() - start.data();
  630. return Some(VirtRegion::new(start, size));
  631. }
  632. /// # 取两个虚拟地址范围的交集
  633. ///
  634. /// 如果两个虚拟地址范围没有交集,返回None
  635. pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> {
  636. let start = self.start.max(other.start);
  637. let end = self.end().min(other.end());
  638. return VirtRegion::between(start, end);
  639. }
  640. /// 设置虚拟地址范围的起始地址
  641. #[inline(always)]
  642. pub fn set_start(&mut self, start: VirtAddr) {
  643. self.start = start;
  644. }
  645. #[inline(always)]
  646. pub fn size(&self) -> usize {
  647. self.size
  648. }
  649. /// 设置虚拟地址范围的大小
  650. #[inline(always)]
  651. pub fn set_size(&mut self, size: usize) {
  652. self.size = size;
  653. }
  654. /// 判断虚拟地址范围是否为空
  655. #[inline(always)]
  656. pub fn is_empty(&self) -> bool {
  657. self.size == 0
  658. }
  659. /// 将虚拟地址区域的大小向上对齐到页大小
  660. #[inline(always)]
  661. pub fn round_up_size_to_page(self) -> Self {
  662. return VirtRegion::new(self.start, round_up_to_page_size(self.size));
  663. }
  664. /// 判断两个虚拟地址范围是否由于具有交集而导致冲突
  665. #[inline(always)]
  666. pub fn collide(&self, other: &VirtRegion) -> bool {
  667. return self.intersect(other).is_some();
  668. }
  669. pub fn iter_pages(&self) -> VirtPageFrameIter {
  670. return VirtPageFrame::iter_range(
  671. VirtPageFrame::new(self.start),
  672. VirtPageFrame::new(self.end()),
  673. );
  674. }
  675. /// 获取[self.start(), region.start())的虚拟地址范围
  676. ///
  677. /// 如果self.start() >= region.start(),返回None
  678. pub fn before(self, region: &VirtRegion) -> Option<Self> {
  679. return Self::between(self.start(), region.start());
  680. }
  681. /// 获取[region.end(),self.end())的虚拟地址范围
  682. ///
  683. /// 如果 self.end() >= region.end() ,返回None
  684. pub fn after(self, region: &VirtRegion) -> Option<Self> {
  685. // if self.end() > region.end() none
  686. return Self::between(region.end(), self.end());
  687. }
  688. /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址
  689. ///
  690. /// 如果vaddr不在当前虚拟地址范围内,返回None
  691. ///
  692. /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址
  693. pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> {
  694. if !self.contains(vaddr) {
  695. return None;
  696. }
  697. let offset = vaddr.data() - self.start().data();
  698. let new_start = new_base.start().data() + offset;
  699. return Some(VirtAddr::new(new_start));
  700. }
  701. /// 判断虚拟地址范围是否包含指定的虚拟地址
  702. pub fn contains(&self, addr: VirtAddr) -> bool {
  703. return self.start() <= addr && addr < self.end();
  704. }
  705. /// 创建当前虚拟地址范围的页面迭代器
  706. pub fn pages(&self) -> VirtPageFrameIter {
  707. return VirtPageFrame::iter_range(
  708. VirtPageFrame::new(self.start()),
  709. VirtPageFrame::new(self.end()),
  710. );
  711. }
  712. }
  713. impl PartialOrd for VirtRegion {
  714. fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
  715. Some(self.cmp(other))
  716. }
  717. }
  718. impl Ord for VirtRegion {
  719. fn cmp(&self, other: &Self) -> cmp::Ordering {
  720. return self.start.cmp(&other.start);
  721. }
  722. }
  723. /// ## 判断虚拟地址是否超出了用户空间
  724. ///
  725. /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT).
  726. /// 如果end < start,返回Err(SystemError::EOVERFLOW)
  727. ///
  728. /// 否则返回Ok(())
  729. pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> {
  730. let end = addr.add(size);
  731. if unlikely(end.data() < addr.data()) {
  732. return Err(SystemError::EOVERFLOW);
  733. }
  734. if !addr.check_user() || !end.check_user() {
  735. return Err(SystemError::EFAULT);
  736. }
  737. return Ok(());
  738. }