1 /*
2  * Copyright (c) 2011-2014 Wind River Systems, Inc.
3  * Copyright (c) 2020 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_INCLUDE_ARCH_X86_MMU_H
9 #define ZEPHYR_INCLUDE_ARCH_X86_MMU_H
10 
11 #include <zephyr/sys/util.h>
12 
13 /*
14  * K_MEM_PARTITION_* defines
15  *
16  * Slated for removal when virtual memory is implemented, memory
17  * mapping APIs will replace memory domains.
18  */
19 #define Z_X86_MMU_RW		BIT64(1)	/** Read-Write */
20 #define Z_X86_MMU_US		BIT64(2)	/** User-Supervisor */
21 #if defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64)
22 #define Z_X86_MMU_XD		BIT64(63)	/** Execute Disable */
23 #else
24 #define Z_X86_MMU_XD		0
25 #endif
26 
27 /* For these we'll just use the same bits in the PTE */
28 #define ARCH_DATA_PAGE_DIRTY		((uintptr_t)BIT(6))
29 #define ARCH_DATA_PAGE_LOADED		((uintptr_t)BIT(0))
30 #define ARCH_DATA_PAGE_ACCESSED		((uintptr_t)BIT(5))
31 
32 /* Use an PAT bit for this one since it's never set in a mapped PTE */
33 #define ARCH_DATA_PAGE_NOT_MAPPED	((uintptr_t)BIT(7))
34 
35 /* Always true with 32-bit page tables, don't enable
36  * CONFIG_EXECUTE_XOR_WRITE and expect it to work for you
37  */
38 #define K_MEM_PARTITION_IS_EXECUTABLE(attr)	(((attr) & Z_X86_MMU_XD) == 0)
39 #define K_MEM_PARTITION_IS_WRITABLE(attr)	(((attr) & Z_X86_MMU_RW) != 0)
40 
41 /* memory partition arch/soc independent attribute */
42 #define K_MEM_PARTITION_P_RW_U_RW	(Z_X86_MMU_RW | Z_X86_MMU_US | \
43 					 Z_X86_MMU_XD)
44 #define K_MEM_PARTITION_P_RW_U_NA	(Z_X86_MMU_RW | Z_X86_MMU_XD)
45 #define K_MEM_PARTITION_P_RO_U_RO	(Z_X86_MMU_US | Z_X86_MMU_XD)
46 #define K_MEM_PARTITION_P_RO_U_NA	Z_X86_MMU_XD
47 /* Execution-allowed attributes */
48 #define K_MEM_PARTITION_P_RWX_U_RWX	(Z_X86_MMU_RW | Z_X86_MMU_US)
49 #define K_MEM_PARTITION_P_RWX_U_NA	Z_X86_MMU_RW
50 #define K_MEM_PARTITION_P_RX_U_RX	Z_X86_MMU_US
51 #define K_MEM_PARTITION_P_RX_U_NA	(0)
52  /* memory partition access permission mask */
53 #define K_MEM_PARTITION_PERM_MASK	(Z_X86_MMU_RW | Z_X86_MMU_US | \
54 					 Z_X86_MMU_XD)
55 
56 #ifndef _ASMLANGUAGE
57 #include <zephyr/sys/slist.h>
58 
59 /* Page table entry data type at all levels. Defined here due to
60  * k_mem_partition_attr_t, eventually move to private x86_mmu.h
61  */
62 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
63 typedef uint64_t pentry_t;
64 #else
65 typedef uint32_t pentry_t;
66 #endif
67 typedef pentry_t k_mem_partition_attr_t;
68 
69 struct arch_mem_domain {
70 #ifdef CONFIG_X86_PAE
71 	/* 4-entry, 32-byte top-level PDPT */
72 	pentry_t pdpt[4];
73 #endif
74 	/* Pointer to top-level structure, either a PML4, PDPT, PD */
75 	pentry_t *ptables;
76 
77 	/* Linked list of all active memory domains */
78 	sys_snode_t node;
79 #ifdef CONFIG_X86_PAE
80 } __aligned(32);
81 #else
82 };
83 #endif /* CONFIG_X86_PAE */
84 #endif /* _ASMLANGUAGE */
85 #endif /* ZEPHYR_INCLUDE_ARCH_X86_MMU_H */
86