A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / scripts / gdb / lunadbg / arch / x86 / pte.py
1 class PageTableHelperBase:
2     @staticmethod
3     def null_mapping(pte):
4         return pte == 0
5     
6     @staticmethod
7     def translation_level(level=-1):
8         raise NotImplementedError()
9     
10     @staticmethod
11     def translation_shift_bits(level):
12         raise NotImplementedError()
13     
14     @staticmethod
15     def mapping_present(pte):
16         raise NotImplementedError()
17     
18     @staticmethod
19     def huge_page(pte):
20         raise NotImplementedError()
21     
22     @staticmethod
23     def protections(pte):
24         raise NotImplementedError()
25     
26     @staticmethod
27     def other_attributes(level, pte):
28         raise NotImplementedError()
29     
30     @staticmethod
31     def same_kind(pte1, pte2):
32         raise NotImplementedError()
33     
34     @staticmethod
35     def physical_pfn(pte):
36         raise NotImplementedError()
37     
38     @staticmethod
39     def vaddr_width():
40         raise NotImplementedError()
41     
42     @staticmethod
43     def pte_size():
44         raise NotImplementedError()
45
46 class PageTableHelper32(PageTableHelperBase):
47     @staticmethod
48     def translation_level(level = -1):
49         return [0, 1][level]
50     
51     @staticmethod
52     def pgtable_len():
53         return (1 << 10)
54     
55     @staticmethod
56     def translation_shift_bits(level):
57         return [10, 0][level] + 12
58     
59     @staticmethod
60     def mapping_present(pte):
61         return bool(pte & 1)
62     
63     @staticmethod
64     def huge_page(pte, po):
65         return bool(pte & (1 << 7)) and po 
66     
67     @staticmethod
68     def protections(pte):
69         prot = ['R'] # RWXUP
70         if (pte & (1 << 1)):
71             prot.append('W')
72         if (pte & -1):
73             prot.append('X')
74         if (pte & (1 << 2)):
75             prot.append('U')
76         if (pte & (1)):
77             prot.append('P')
78         return prot
79     
80     @staticmethod
81     def other_attributes(level, pte):
82         attrs = []
83         if pte & (1 << 5):
84             attrs.append("A")
85         if pte & (1 << 6):
86             attrs.append("D")
87         if pte & (1 << 3):
88             attrs.append("PWT")
89         if pte & (1 << 4):
90             attrs.append("PCD")
91         if PageTableHelper32.translation_level(level) == 1 and pte & (1 << 8):
92             attrs.append("G")
93         return attrs
94     
95     @staticmethod
96     def same_kind(pte1, pte2):
97         attr_mask = 0x19f  # P, R/W, U/S, PWT, PCD, PS, G
98         return (pte1 & attr_mask) == (pte2 & attr_mask)
99     
100     @staticmethod
101     def physical_pfn(pte):
102         return pte >> 12
103     
104     @staticmethod
105     def vaddr_width():
106         return 32
107     
108     @staticmethod
109     def pte_size():
110         return 4
111
112 class PageTableHelper64(PageTableHelperBase):
113     pass