Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Helper routines for building identity mapping page tables. This is |
| 4 | * included by both the compressed kernel and the regular kernel. |
| 5 | */ |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 6 | |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 7 | static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page, |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 8 | unsigned long addr, unsigned long end) |
| 9 | { |
| 10 | addr &= PMD_MASK; |
| 11 | for (; addr < end; addr += PMD_SIZE) { |
| 12 | pmd_t *pmd = pmd_page + pmd_index(addr); |
| 13 | |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 14 | if (pmd_present(*pmd)) |
| 15 | continue; |
| 16 | |
Xunlei Pang | 66aad4f | 2017-05-04 09:42:50 +0800 | [diff] [blame] | 17 | set_pmd(pmd, __pmd((addr - info->offset) | info->page_flag)); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 18 | } |
| 19 | } |
Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 20 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 21 | static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, |
| 22 | unsigned long addr, unsigned long end) |
| 23 | { |
| 24 | unsigned long next; |
| 25 | |
| 26 | for (; addr < end; addr = next) { |
| 27 | pud_t *pud = pud_page + pud_index(addr); |
| 28 | pmd_t *pmd; |
| 29 | |
| 30 | next = (addr & PUD_MASK) + PUD_SIZE; |
| 31 | if (next > end) |
| 32 | next = end; |
| 33 | |
Xunlei Pang | 66aad4f | 2017-05-04 09:42:50 +0800 | [diff] [blame] | 34 | if (info->direct_gbpages) { |
| 35 | pud_t pudval; |
| 36 | |
| 37 | if (pud_present(*pud)) |
| 38 | continue; |
| 39 | |
| 40 | addr &= PUD_MASK; |
| 41 | pudval = __pud((addr - info->offset) | info->page_flag); |
| 42 | set_pud(pud, pudval); |
| 43 | continue; |
| 44 | } |
| 45 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 46 | if (pud_present(*pud)) { |
| 47 | pmd = pmd_offset(pud, 0); |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 48 | ident_pmd_init(info, pmd, addr, next); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 49 | continue; |
| 50 | } |
| 51 | pmd = (pmd_t *)info->alloc_pgt_page(info->context); |
| 52 | if (!pmd) |
| 53 | return -ENOMEM; |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 54 | ident_pmd_init(info, pmd, addr, next); |
Tom Lendacky | bba4ed0 | 2017-07-17 16:10:28 -0500 | [diff] [blame] | 55 | set_pud(pud, __pud(__pa(pmd) | info->kernpg_flag)); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 61 | static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page, |
| 62 | unsigned long addr, unsigned long end) |
| 63 | { |
| 64 | unsigned long next; |
| 65 | |
| 66 | for (; addr < end; addr = next) { |
| 67 | p4d_t *p4d = p4d_page + p4d_index(addr); |
| 68 | pud_t *pud; |
| 69 | |
| 70 | next = (addr & P4D_MASK) + P4D_SIZE; |
| 71 | if (next > end) |
| 72 | next = end; |
| 73 | |
| 74 | if (p4d_present(*p4d)) { |
| 75 | pud = pud_offset(p4d, 0); |
| 76 | ident_pud_init(info, pud, addr, next); |
| 77 | continue; |
| 78 | } |
| 79 | pud = (pud_t *)info->alloc_pgt_page(info->context); |
| 80 | if (!pud) |
| 81 | return -ENOMEM; |
| 82 | ident_pud_init(info, pud, addr, next); |
Tom Lendacky | bba4ed0 | 2017-07-17 16:10:28 -0500 | [diff] [blame] | 83 | set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag)); |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 89 | int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 90 | unsigned long pstart, unsigned long pend) |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 91 | { |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 92 | unsigned long addr = pstart + info->offset; |
| 93 | unsigned long end = pend + info->offset; |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 94 | unsigned long next; |
| 95 | int result; |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 96 | |
Tom Lendacky | bba4ed0 | 2017-07-17 16:10:28 -0500 | [diff] [blame] | 97 | /* Set the default pagetable flags if not supplied */ |
| 98 | if (!info->kernpg_flag) |
| 99 | info->kernpg_flag = _KERNPG_TABLE; |
| 100 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 101 | for (; addr < end; addr = next) { |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 102 | pgd_t *pgd = pgd_page + pgd_index(addr); |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 103 | p4d_t *p4d; |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 104 | |
| 105 | next = (addr & PGDIR_MASK) + PGDIR_SIZE; |
| 106 | if (next > end) |
| 107 | next = end; |
| 108 | |
| 109 | if (pgd_present(*pgd)) { |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 110 | p4d = p4d_offset(pgd, 0); |
| 111 | result = ident_p4d_init(info, p4d, addr, next); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 112 | if (result) |
| 113 | return result; |
| 114 | continue; |
| 115 | } |
| 116 | |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 117 | p4d = (p4d_t *)info->alloc_pgt_page(info->context); |
| 118 | if (!p4d) |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 119 | return -ENOMEM; |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 120 | result = ident_p4d_init(info, p4d, addr, next); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 121 | if (result) |
| 122 | return result; |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 123 | if (IS_ENABLED(CONFIG_X86_5LEVEL)) { |
Tom Lendacky | bba4ed0 | 2017-07-17 16:10:28 -0500 | [diff] [blame] | 124 | set_pgd(pgd, __pgd(__pa(p4d) | info->kernpg_flag)); |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 125 | } else { |
| 126 | /* |
| 127 | * With p4d folded, pgd is equal to p4d. |
| 128 | * The pgd entry has to point to the pud page table in this case. |
| 129 | */ |
| 130 | pud_t *pud = pud_offset(p4d, 0); |
Tom Lendacky | bba4ed0 | 2017-07-17 16:10:28 -0500 | [diff] [blame] | 131 | set_pgd(pgd, __pgd(__pa(pud) | info->kernpg_flag)); |
Kirill A. Shutemov | ea3b5e6 | 2017-03-13 17:33:07 +0300 | [diff] [blame] | 132 | } |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |