]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/boot/compressed/pagetable.c
x86/mm: Provide general kernel support for memory encryption
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / boot / compressed / pagetable.c
1 /*
2 * This code is used on x86_64 to create page table identity mappings on
3 * demand by building up a new set of page tables (or appending to the
4 * existing ones), and then switching over to them when ready.
5 *
6 * Copyright (C) 2015-2016 Yinghai Lu
7 * Copyright (C) 2016 Kees Cook
8 */
9
10 /*
11 * Since we're dealing with identity mappings, physical and virtual
12 * addresses are the same, so override these defines which are ultimately
13 * used by the headers in misc.h.
14 */
15 #define __pa(x) ((unsigned long)(x))
16 #define __va(x) ((void *)((unsigned long)(x)))
17
18 /*
19 * The pgtable.h and mm/ident_map.c includes make use of the SME related
20 * information which is not used in the compressed image support. Un-define
21 * the SME support to avoid any compile and link errors.
22 */
23 #undef CONFIG_AMD_MEM_ENCRYPT
24
25 #include "misc.h"
26
27 /* These actually do the work of building the kernel identity maps. */
28 #include <asm/init.h>
29 #include <asm/pgtable.h>
30 /* Use the static base for this part of the boot process */
31 #undef __PAGE_OFFSET
32 #define __PAGE_OFFSET __PAGE_OFFSET_BASE
33 #include "../../mm/ident_map.c"
34
35 /* Used by pgtable.h asm code to force instruction serialization. */
36 unsigned long __force_order;
37
38 /* Used to track our page table allocation area. */
39 struct alloc_pgt_data {
40 unsigned char *pgt_buf;
41 unsigned long pgt_buf_size;
42 unsigned long pgt_buf_offset;
43 };
44
45 /*
46 * Allocates space for a page table entry, using struct alloc_pgt_data
47 * above. Besides the local callers, this is used as the allocation
48 * callback in mapping_info below.
49 */
50 static void *alloc_pgt_page(void *context)
51 {
52 struct alloc_pgt_data *pages = (struct alloc_pgt_data *)context;
53 unsigned char *entry;
54
55 /* Validate there is space available for a new page. */
56 if (pages->pgt_buf_offset >= pages->pgt_buf_size) {
57 debug_putstr("out of pgt_buf in " __FILE__ "!?\n");
58 debug_putaddr(pages->pgt_buf_offset);
59 debug_putaddr(pages->pgt_buf_size);
60 return NULL;
61 }
62
63 entry = pages->pgt_buf + pages->pgt_buf_offset;
64 pages->pgt_buf_offset += PAGE_SIZE;
65
66 return entry;
67 }
68
69 /* Used to track our allocated page tables. */
70 static struct alloc_pgt_data pgt_data;
71
72 /* The top level page table entry pointer. */
73 static unsigned long top_level_pgt;
74
75 /*
76 * Mapping information structure passed to kernel_ident_mapping_init().
77 * Due to relocation, pointers must be assigned at run time not build time.
78 */
79 static struct x86_mapping_info mapping_info = {
80 .page_flag = __PAGE_KERNEL_LARGE_EXEC,
81 };
82
83 /* Locates and clears a region for a new top level page table. */
84 void initialize_identity_maps(void)
85 {
86 /* Init mapping_info with run-time function/buffer pointers. */
87 mapping_info.alloc_pgt_page = alloc_pgt_page;
88 mapping_info.context = &pgt_data;
89
90 /*
91 * It should be impossible for this not to already be true,
92 * but since calling this a second time would rewind the other
93 * counters, let's just make sure this is reset too.
94 */
95 pgt_data.pgt_buf_offset = 0;
96
97 /*
98 * If we came here via startup_32(), cr3 will be _pgtable already
99 * and we must append to the existing area instead of entirely
100 * overwriting it.
101 *
102 * With 5-level paging, we use '_pgtable' to allocate the p4d page table,
103 * the top-level page table is allocated separately.
104 *
105 * p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
106 * cases. On 4-level paging it's equal to 'top_level_pgt'.
107 */
108 top_level_pgt = read_cr3_pa();
109 if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
110 debug_putstr("booted via startup_32()\n");
111 pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
112 pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
113 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
114 } else {
115 debug_putstr("booted via startup_64()\n");
116 pgt_data.pgt_buf = _pgtable;
117 pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
118 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
119 top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
120 }
121 }
122
123 /*
124 * Adds the specified range to what will become the new identity mappings.
125 * Once all ranges have been added, the new mapping is activated by calling
126 * finalize_identity_maps() below.
127 */
128 void add_identity_map(unsigned long start, unsigned long size)
129 {
130 unsigned long end = start + size;
131
132 /* Align boundary to 2M. */
133 start = round_down(start, PMD_SIZE);
134 end = round_up(end, PMD_SIZE);
135 if (start >= end)
136 return;
137
138 /* Build the mapping. */
139 kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt,
140 start, end);
141 }
142
143 /*
144 * This switches the page tables to the new level4 that has been built
145 * via calls to add_identity_map() above. If booted via startup_32(),
146 * this is effectively a no-op.
147 */
148 void finalize_identity_maps(void)
149 {
150 write_cr3(top_level_pgt);
151 }