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