Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __XLAT_TABLES_PRIVATE_H__ |
| 8 | #define __XLAT_TABLES_PRIVATE_H__ |
| 9 | |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 10 | #include <platform_def.h> |
Sandrine Bailleux | 8933c34 | 2017-05-19 09:59:37 +0100 | [diff] [blame^] | 11 | #include <xlat_tables_defs.h> |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 12 | |
| 13 | /* Struct that holds all information about the translation tables. */ |
| 14 | typedef struct { |
| 15 | |
| 16 | /* |
| 17 | * Max allowed Virtual and Physical Addresses. |
| 18 | */ |
| 19 | unsigned long long pa_max_address; |
| 20 | uintptr_t va_max_address; |
| 21 | |
| 22 | /* |
| 23 | * Array of all memory regions stored in order of ascending end address |
| 24 | * and ascending size to simplify the code that allows overlapping |
| 25 | * regions. The list is terminated by the first entry with size == 0. |
Antonio Nino Diaz | e769db3 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 26 | * The max size of the list is stored in `mmap_num`. `mmap` points to an |
| 27 | * array of mmap_num + 1 elements, so that there is space for the final |
| 28 | * null entry. |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 29 | */ |
Antonio Nino Diaz | e769db3 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 30 | mmap_region_t *mmap; |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 31 | unsigned int mmap_num; |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Array of finer-grain translation tables. |
| 35 | * For example, if the initial lookup level is 1 then this array would |
| 36 | * contain both level-2 and level-3 entries. |
| 37 | */ |
| 38 | uint64_t (*tables)[XLAT_TABLE_ENTRIES]; |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 39 | unsigned int tables_num; |
Antonio Nino Diaz | 0b64f4e | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 40 | /* |
| 41 | * Keep track of how many regions are mapped in each table. The base |
| 42 | * table can't be unmapped so it isn't needed to keep track of it. |
| 43 | */ |
| 44 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 45 | int *tables_mapped_regions; |
| 46 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 47 | |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 48 | unsigned int next_table; |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Base translation table. It doesn't need to have the same amount of |
| 52 | * entries as the ones used for other levels. |
| 53 | */ |
| 54 | uint64_t *base_table; |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 55 | unsigned int base_table_entries; |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 56 | |
Antonio Nino Diaz | e769db3 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 57 | /* |
| 58 | * Max Physical and Virtual addresses currently in use by the |
| 59 | * translation tables. These might get updated as we map/unmap memory |
| 60 | * regions but they will never go beyond pa/va_max_address. |
| 61 | */ |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 62 | unsigned long long max_pa; |
| 63 | uintptr_t max_va; |
| 64 | |
| 65 | /* Level of the base translation table. */ |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 66 | unsigned int base_level; |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 67 | |
| 68 | /* Set to 1 when the translation tables are initialized. */ |
Varun Wadekar | 6311f63 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 69 | unsigned int initialized; |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 70 | |
Antonio Nino Diaz | a564025 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 71 | /* |
| 72 | * Bit mask that has to be ORed to the rest of a translation table |
| 73 | * descriptor in order to prohibit execution of code at the exception |
| 74 | * level of this translation context. |
| 75 | */ |
| 76 | uint64_t execute_never_mask; |
| 77 | |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 78 | } xlat_ctx_t; |
| 79 | |
Antonio Nino Diaz | 0b64f4e | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 80 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 81 | /* |
| 82 | * Shifts and masks to access fields of an mmap_attr_t |
| 83 | */ |
| 84 | /* Dynamic or static */ |
| 85 | #define MT_DYN_SHIFT 30 /* 31 would cause undefined behaviours */ |
| 86 | |
| 87 | /* |
| 88 | * Memory mapping private attributes |
| 89 | * |
| 90 | * Private attributes not exposed in the mmap_attr_t enum. |
| 91 | */ |
| 92 | typedef enum { |
| 93 | /* |
| 94 | * Regions mapped before the MMU can't be unmapped dynamically (they are |
| 95 | * static) and regions mapped with MMU enabled can be unmapped. This |
| 96 | * behaviour can't be overridden. |
| 97 | * |
| 98 | * Static regions can overlap each other, dynamic regions can't. |
| 99 | */ |
| 100 | MT_STATIC = 0 << MT_DYN_SHIFT, |
| 101 | MT_DYNAMIC = 1 << MT_DYN_SHIFT |
| 102 | } mmap_priv_attr_t; |
| 103 | |
| 104 | /* |
| 105 | * Function used to invalidate all levels of the translation walk for a given |
| 106 | * virtual address. It must be called for every translation table entry that is |
| 107 | * modified. |
| 108 | */ |
| 109 | void xlat_arch_tlbi_va(uintptr_t va); |
| 110 | |
| 111 | /* |
| 112 | * This function has to be called at the end of any code that uses the function |
| 113 | * xlat_arch_tlbi_va(). |
| 114 | */ |
| 115 | void xlat_arch_tlbi_va_sync(void); |
| 116 | |
| 117 | /* Add a dynamic region to the specified context. */ |
| 118 | int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); |
| 119 | |
| 120 | /* Remove a dynamic region from the specified context. */ |
| 121 | int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va, |
| 122 | size_t size); |
| 123 | |
| 124 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 125 | |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 126 | /* Print VA, PA, size and attributes of all regions in the mmap array. */ |
| 127 | void print_mmap(mmap_region_t *const mmap); |
| 128 | |
| 129 | /* |
| 130 | * Print the current state of the translation tables by reading them from |
| 131 | * memory. |
| 132 | */ |
| 133 | void xlat_tables_print(xlat_ctx_t *ctx); |
| 134 | |
| 135 | /* |
| 136 | * Initialize the translation tables by mapping all regions added to the |
| 137 | * specified context. |
| 138 | */ |
| 139 | void init_xlation_table(xlat_ctx_t *ctx); |
| 140 | |
| 141 | /* Add a static region to the specified context. */ |
| 142 | void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); |
| 143 | |
| 144 | /* |
| 145 | * Architecture-specific initialization code. |
| 146 | */ |
| 147 | |
Antonio Nino Diaz | a564025 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 148 | /* Returns the current Exception Level. The returned EL must be 1 or higher. */ |
| 149 | int xlat_arch_current_el(void); |
| 150 | |
| 151 | /* |
| 152 | * Returns the bit mask that has to be ORed to the rest of a translation table |
| 153 | * descriptor so that execution of code is prohibited at the given Exception |
| 154 | * Level. |
| 155 | */ |
| 156 | uint64_t xlat_arch_get_xn_desc(int el); |
| 157 | |
Antonio Nino Diaz | 7bb01fb | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 158 | /* Execute architecture-specific translation table initialization code. */ |
| 159 | void init_xlat_tables_arch(unsigned long long max_pa); |
| 160 | |
| 161 | /* Enable MMU and configure it to use the specified translation tables. */ |
| 162 | void enable_mmu_arch(unsigned int flags, uint64_t *base_table); |
| 163 | |
| 164 | /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */ |
| 165 | int is_mmu_enabled(void); |
| 166 | |
| 167 | #endif /* __XLAT_TABLES_PRIVATE_H__ */ |