iwlwifi: mvm: fix version check for GEO_TX_POWER_LIMIT support
[linux/fpc-iii.git] / arch / x86 / boot / compressed / kaslr_64.c
blob748456c365f4691af041753c63d6991f9bc8b4b8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This code is used on x86_64 to create page table identity mappings on
4 * demand by building up a new set of page tables (or appending to the
5 * existing ones), and then switching over to them when ready.
7 * Copyright (C) 2015-2016 Yinghai Lu
8 * Copyright (C) 2016 Kees Cook
9 */
12 * Since we're dealing with identity mappings, physical and virtual
13 * addresses are the same, so override these defines which are ultimately
14 * used by the headers in misc.h.
16 #define __pa(x) ((unsigned long)(x))
17 #define __va(x) ((void *)((unsigned long)(x)))
19 /* No PAGE_TABLE_ISOLATION support needed either: */
20 #undef CONFIG_PAGE_TABLE_ISOLATION
22 #include "misc.h"
24 /* These actually do the work of building the kernel identity maps. */
25 #include <asm/init.h>
26 #include <asm/pgtable.h>
27 /* Use the static base for this part of the boot process */
28 #undef __PAGE_OFFSET
29 #define __PAGE_OFFSET __PAGE_OFFSET_BASE
30 #include "../../mm/ident_map.c"
32 /* Used by pgtable.h asm code to force instruction serialization. */
33 unsigned long __force_order;
35 /* Used to track our page table allocation area. */
36 struct alloc_pgt_data {
37 unsigned char *pgt_buf;
38 unsigned long pgt_buf_size;
39 unsigned long pgt_buf_offset;
43 * Allocates space for a page table entry, using struct alloc_pgt_data
44 * above. Besides the local callers, this is used as the allocation
45 * callback in mapping_info below.
47 static void *alloc_pgt_page(void *context)
49 struct alloc_pgt_data *pages = (struct alloc_pgt_data *)context;
50 unsigned char *entry;
52 /* Validate there is space available for a new page. */
53 if (pages->pgt_buf_offset >= pages->pgt_buf_size) {
54 debug_putstr("out of pgt_buf in " __FILE__ "!?\n");
55 debug_putaddr(pages->pgt_buf_offset);
56 debug_putaddr(pages->pgt_buf_size);
57 return NULL;
60 entry = pages->pgt_buf + pages->pgt_buf_offset;
61 pages->pgt_buf_offset += PAGE_SIZE;
63 return entry;
66 /* Used to track our allocated page tables. */
67 static struct alloc_pgt_data pgt_data;
69 /* The top level page table entry pointer. */
70 static unsigned long top_level_pgt;
72 phys_addr_t physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
75 * Mapping information structure passed to kernel_ident_mapping_init().
76 * Due to relocation, pointers must be assigned at run time not build time.
78 static struct x86_mapping_info mapping_info;
80 /* Locates and clears a region for a new top level page table. */
81 void initialize_identity_maps(void)
83 /* If running as an SEV guest, the encryption mask is required. */
84 set_sev_encryption_mask();
86 /* Exclude the encryption mask from __PHYSICAL_MASK */
87 physical_mask &= ~sme_me_mask;
89 /* Init mapping_info with run-time function/buffer pointers. */
90 mapping_info.alloc_pgt_page = alloc_pgt_page;
91 mapping_info.context = &pgt_data;
92 mapping_info.page_flag = __PAGE_KERNEL_LARGE_EXEC | sme_me_mask;
93 mapping_info.kernpg_flag = _KERNPG_TABLE;
96 * It should be impossible for this not to already be true,
97 * but since calling this a second time would rewind the other
98 * counters, let's just make sure this is reset too.
100 pgt_data.pgt_buf_offset = 0;
103 * If we came here via startup_32(), cr3 will be _pgtable already
104 * and we must append to the existing area instead of entirely
105 * overwriting it.
107 * With 5-level paging, we use '_pgtable' to allocate the p4d page table,
108 * the top-level page table is allocated separately.
110 * p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
111 * cases. On 4-level paging it's equal to 'top_level_pgt'.
113 top_level_pgt = read_cr3_pa();
114 if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
115 debug_putstr("booted via startup_32()\n");
116 pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
117 pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
118 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
119 } else {
120 debug_putstr("booted via startup_64()\n");
121 pgt_data.pgt_buf = _pgtable;
122 pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
123 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
124 top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
129 * Adds the specified range to what will become the new identity mappings.
130 * Once all ranges have been added, the new mapping is activated by calling
131 * finalize_identity_maps() below.
133 void add_identity_map(unsigned long start, unsigned long size)
135 unsigned long end = start + size;
137 /* Align boundary to 2M. */
138 start = round_down(start, PMD_SIZE);
139 end = round_up(end, PMD_SIZE);
140 if (start >= end)
141 return;
143 /* Build the mapping. */
144 kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt,
145 start, end);
149 * This switches the page tables to the new level4 that has been built
150 * via calls to add_identity_map() above. If booted via startup_32(),
151 * this is effectively a no-op.
153 void finalize_identity_maps(void)
155 write_cr3(top_level_pgt);