xref: /haiku/src/system/kernel/arch/x86/paging/64bit/paging.h (revision 58353b3809402871992f09a75122c5f590487249)
1950b24e3SAlex Smith /*
2950b24e3SAlex Smith  * Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
3950b24e3SAlex Smith  * Distributed under the terms of the MIT License.
4950b24e3SAlex Smith  */
5e276cc04SAlex Smith #ifndef KERNEL_ARCH_X86_PAGING_64BIT_PAGING_H
6e276cc04SAlex Smith #define KERNEL_ARCH_X86_PAGING_64BIT_PAGING_H
7950b24e3SAlex Smith 
8950b24e3SAlex Smith 
9950b24e3SAlex Smith #include <OS.h>
10950b24e3SAlex Smith 
11950b24e3SAlex Smith 
12*58353b38SJérôme Duval // PML5 entry bits.
13*58353b38SJérôme Duval #define X86_64_PML5E_PRESENT			(1LL << 0)
14*58353b38SJérôme Duval #define X86_64_PML5E_WRITABLE			(1LL << 1)
15*58353b38SJérôme Duval #define X86_64_PML5E_USER				(1LL << 2)
16*58353b38SJérôme Duval #define X86_64_PML5E_WRITE_THROUGH		(1LL << 3)
17*58353b38SJérôme Duval #define X86_64_PML5E_CACHING_DISABLED	(1LL << 4)
18*58353b38SJérôme Duval #define X86_64_PML5E_ACCESSED			(1LL << 5)
19*58353b38SJérôme Duval #define X86_64_PML5E_NOT_EXECUTABLE		(1LL << 63)
20*58353b38SJérôme Duval #define X86_64_PML5E_ADDRESS_MASK		0x000ffffffffff000L
21*58353b38SJérôme Duval 
22e276cc04SAlex Smith // PML4 entry bits.
23e276cc04SAlex Smith #define X86_64_PML4E_PRESENT			(1LL << 0)
24e276cc04SAlex Smith #define X86_64_PML4E_WRITABLE			(1LL << 1)
25e276cc04SAlex Smith #define X86_64_PML4E_USER				(1LL << 2)
26e276cc04SAlex Smith #define X86_64_PML4E_WRITE_THROUGH		(1LL << 3)
27e276cc04SAlex Smith #define X86_64_PML4E_CACHING_DISABLED	(1LL << 4)
28e276cc04SAlex Smith #define X86_64_PML4E_ACCESSED			(1LL << 5)
29e276cc04SAlex Smith #define X86_64_PML4E_NOT_EXECUTABLE		(1LL << 63)
30e276cc04SAlex Smith #define X86_64_PML4E_ADDRESS_MASK		0x000ffffffffff000L
31e276cc04SAlex Smith 
32e276cc04SAlex Smith // PDPT entry bits.
33e276cc04SAlex Smith #define X86_64_PDPTE_PRESENT			(1LL << 0)
34e276cc04SAlex Smith #define X86_64_PDPTE_WRITABLE			(1LL << 1)
35e276cc04SAlex Smith #define X86_64_PDPTE_USER				(1LL << 2)
36e276cc04SAlex Smith #define X86_64_PDPTE_WRITE_THROUGH		(1LL << 3)
37e276cc04SAlex Smith #define X86_64_PDPTE_CACHING_DISABLED	(1LL << 4)
38e276cc04SAlex Smith #define X86_64_PDPTE_ACCESSED			(1LL << 5)
39e276cc04SAlex Smith #define X86_64_PDPTE_DIRTY				(1LL << 6)
40e276cc04SAlex Smith #define X86_64_PDPTE_LARGE_PAGE			(1LL << 7)
41e276cc04SAlex Smith #define X86_64_PDPTE_GLOBAL				(1LL << 8)
42e276cc04SAlex Smith #define X86_64_PDPTE_PAT				(1LL << 12)
43e276cc04SAlex Smith #define X86_64_PDPTE_NOT_EXECUTABLE		(1LL << 63)
44e276cc04SAlex Smith #define X86_64_PDPTE_ADDRESS_MASK		0x000ffffffffff000L
45e276cc04SAlex Smith 
46e276cc04SAlex Smith // Page directory entry bits.
47e276cc04SAlex Smith #define X86_64_PDE_PRESENT				(1LL << 0)
48e276cc04SAlex Smith #define X86_64_PDE_WRITABLE				(1LL << 1)
49e276cc04SAlex Smith #define X86_64_PDE_USER					(1LL << 2)
50e276cc04SAlex Smith #define X86_64_PDE_WRITE_THROUGH		(1LL << 3)
51e276cc04SAlex Smith #define X86_64_PDE_CACHING_DISABLED		(1LL << 4)
52e276cc04SAlex Smith #define X86_64_PDE_ACCESSED				(1LL << 5)
53e276cc04SAlex Smith #define X86_64_PDE_DIRTY				(1LL << 6)
54e276cc04SAlex Smith #define X86_64_PDE_LARGE_PAGE			(1LL << 7)
55e276cc04SAlex Smith #define X86_64_PDE_GLOBAL				(1LL << 8)
56e276cc04SAlex Smith #define X86_64_PDE_PAT					(1LL << 12)
57e276cc04SAlex Smith #define X86_64_PDE_NOT_EXECUTABLE		(1LL << 63)
58e276cc04SAlex Smith #define X86_64_PDE_ADDRESS_MASK			0x000ffffffffff000L
59e276cc04SAlex Smith 
60e276cc04SAlex Smith // Page table entry bits.
61e276cc04SAlex Smith #define X86_64_PTE_PRESENT				(1LL << 0)
62e276cc04SAlex Smith #define X86_64_PTE_WRITABLE				(1LL << 1)
63e276cc04SAlex Smith #define X86_64_PTE_USER					(1LL << 2)
64e276cc04SAlex Smith #define X86_64_PTE_WRITE_THROUGH		(1LL << 3)
65e276cc04SAlex Smith #define X86_64_PTE_CACHING_DISABLED		(1LL << 4)
66e276cc04SAlex Smith #define X86_64_PTE_ACCESSED				(1LL << 5)
67e276cc04SAlex Smith #define X86_64_PTE_DIRTY				(1LL << 6)
68e276cc04SAlex Smith #define X86_64_PTE_PAT					(1LL << 7)
69e276cc04SAlex Smith #define X86_64_PTE_GLOBAL				(1LL << 8)
70e276cc04SAlex Smith #define X86_64_PTE_NOT_EXECUTABLE		(1LL << 63)
71e276cc04SAlex Smith #define X86_64_PTE_ADDRESS_MASK			0x000ffffffffff000L
72966f2076SPawel Dziepak #define X86_64_PTE_PROTECTION_MASK		(X86_64_PTE_NOT_EXECUTABLE	\
73966f2076SPawel Dziepak 											| X86_64_PTE_WRITABLE	\
74966f2076SPawel Dziepak 											| X86_64_PTE_USER)
75e276cc04SAlex Smith #define X86_64_PTE_MEMORY_TYPE_MASK		(X86_64_PTE_WRITE_THROUGH \
76e276cc04SAlex Smith 											| X86_64_PTE_CACHING_DISABLED)
77e276cc04SAlex Smith 
78e276cc04SAlex Smith 
79e276cc04SAlex Smith static const size_t k64BitPageTableRange = 0x200000L;
80e276cc04SAlex Smith static const size_t k64BitPageDirectoryRange = 0x40000000L;
81e276cc04SAlex Smith static const size_t k64BitPDPTRange = 0x8000000000L;
82*58353b38SJérôme Duval static const size_t k64BitPML4TRange = 0x1000000000000L;
83e276cc04SAlex Smith 
84e276cc04SAlex Smith static const size_t k64BitTableEntryCount = 512;
85e276cc04SAlex Smith 
86e276cc04SAlex Smith 
87*58353b38SJérôme Duval #define VADDR_TO_PML5E(va)	(((va) & 0x01fffffffffff000L) / k64BitPML4TRange)
88*58353b38SJérôme Duval #define VADDR_TO_PML4E(va)	(((va) % k64BitPML4TRange) / k64BitPDPTRange)
89e276cc04SAlex Smith #define VADDR_TO_PDPTE(va)	(((va) % k64BitPDPTRange) / k64BitPageDirectoryRange)
90e276cc04SAlex Smith #define VADDR_TO_PDE(va)	(((va) % k64BitPageDirectoryRange) / k64BitPageTableRange)
91e276cc04SAlex Smith #define VADDR_TO_PTE(va)	(((va) % k64BitPageTableRange) / B_PAGE_SIZE)
92e276cc04SAlex Smith 
93e276cc04SAlex Smith 
94e276cc04SAlex Smith #endif	// KERNEL_ARCH_X86_PAGING_64BIT_PAGING_H
95