pte.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  25. }
  26. pub trait RawAttribute: Copy {
  27. /// Create a new attribute representing a non-present page.
  28. fn null() -> Self;
  29. /// Interpret the attribute as a page table attribute. Return `None` if it is
  30. /// not an attribute for a page table.
  31. ///
  32. /// # Panic
  33. /// The implementor should panic if invalid combinations of flags are present.
  34. fn as_table_attr(self) -> Option<TableAttribute>;
  35. /// Interpret the attribute as a page attribute. Return `None` if it is not
  36. /// an attribute for a page.
  37. ///
  38. /// # Panic
  39. /// The implementor should panic if invalid combinations of flags are present.
  40. fn as_page_attr(self) -> Option<PageAttribute>;
  41. /// Convert the attribute to a raw value.
  42. ///
  43. /// # Panic
  44. /// The implementor should panic if invalid combinations of flags are present.
  45. fn from_table_attr(table_attr: TableAttribute) -> Self;
  46. /// Convert the attribute to a raw value.
  47. ///
  48. /// # Panic
  49. /// The implementor should panic if invalid combinations of flags are present.
  50. fn from_page_attr(page_attr: PageAttribute) -> Self;
  51. }
  52. pub trait PTE: Sized {
  53. type Attr: RawAttribute;
  54. fn set(&mut self, pfn: PFN, attr: Self::Attr);
  55. fn get(&self) -> (PFN, Self::Attr);
  56. fn take(&mut self) -> (PFN, Self::Attr) {
  57. let pfn_attr = self.get();
  58. self.set(PFN::from_val(0), Self::Attr::null());
  59. pfn_attr
  60. }
  61. fn set_pfn(&mut self, pfn: PFN) {
  62. self.set(pfn, self.get_attr());
  63. }
  64. fn set_attr(&mut self, attr: Self::Attr) {
  65. self.set(self.get_pfn(), attr);
  66. }
  67. fn get_pfn(&self) -> PFN {
  68. self.get().0
  69. }
  70. fn get_attr(&self) -> Self::Attr {
  71. self.get().1
  72. }
  73. }