pte.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use crate::paging::PFN;
  2. use bitflags::bitflags;
  3. bitflags! {
  4. #[derive(Clone, Copy, PartialEq)]
  5. pub struct TableAttribute: usize {
  6. const PRESENT = 1;
  7. const USER = 2;
  8. const ACCESSED = 4;
  9. const GLOBAL = 8;
  10. }
  11. #[derive(Clone, Copy, PartialEq)]
  12. pub struct PageAttribute: usize {
  13. const PRESENT = 1;
  14. const READ = 2;
  15. const WRITE = 4;
  16. const EXECUTE = 8;
  17. const USER = 16;
  18. const ACCESSED = 32;
  19. const DIRTY = 64;
  20. const GLOBAL = 128;
  21. const COPY_ON_WRITE = 256;
  22. const MAPPED = 512;
  23. const ANONYMOUS = 1024;
  24. const HUGE = 2048;
  25. }
  26. }
  27. #[doc(notable_trait)]
  28. pub trait RawAttribute: Copy + From<PageAttribute> + From<TableAttribute> {
  29. /// Create a new attribute representing a non-present page.
  30. fn null() -> Self;
  31. /// Interpret the attribute as a page table attribute. Return `None` if it is
  32. /// not an attribute for a page table.
  33. ///
  34. /// # Panic
  35. /// The implementor should panic if invalid combinations of flags are present.
  36. fn as_table_attr(self) -> Option<TableAttribute>;
  37. /// Interpret the attribute as a page attribute. Return `None` if it is not
  38. /// an attribute for a page.
  39. ///
  40. /// # Panic
  41. /// The implementor should panic if invalid combinations of flags are present.
  42. fn as_page_attr(self) -> Option<PageAttribute>;
  43. }
  44. #[doc(notable_trait)]
  45. pub trait PTE: Sized {
  46. type Attr: RawAttribute;
  47. fn set(&mut self, pfn: PFN, attr: Self::Attr);
  48. fn get(&self) -> (PFN, Self::Attr);
  49. fn take(&mut self) -> (PFN, Self::Attr) {
  50. let pfn_attr = self.get();
  51. self.set(PFN::from_val(0), Self::Attr::null());
  52. pfn_attr
  53. }
  54. fn set_pfn(&mut self, pfn: PFN) {
  55. self.set(pfn, self.get_attr());
  56. }
  57. fn set_attr(&mut self, attr: Self::Attr) {
  58. self.set(self.get_pfn(), attr);
  59. }
  60. fn get_pfn(&self) -> PFN {
  61. self.get().0
  62. }
  63. fn get_attr(&self) -> Self::Attr {
  64. self.get().1
  65. }
  66. }