Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper routines for building identity mapping page tables. This is |
| 3 | * included by both the compressed kernel and the regular kernel. |
| 4 | */ |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 5 | |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 6 | 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] | 7 | unsigned long addr, unsigned long end) |
| 8 | { |
| 9 | addr &= PMD_MASK; |
| 10 | for (; addr < end; addr += PMD_SIZE) { |
| 11 | pmd_t *pmd = pmd_page + pmd_index(addr); |
| 12 | |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 13 | if (pmd_present(*pmd)) |
| 14 | continue; |
| 15 | |
| 16 | set_pmd(pmd, __pmd((addr - info->offset) | info->pmd_flag)); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 17 | } |
| 18 | } |
Kees Cook | cb18ef0 | 2016-05-09 13:22:05 -0700 | [diff] [blame] | 19 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 20 | static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, |
| 21 | unsigned long addr, unsigned long end) |
| 22 | { |
| 23 | unsigned long next; |
| 24 | |
| 25 | for (; addr < end; addr = next) { |
| 26 | pud_t *pud = pud_page + pud_index(addr); |
| 27 | pmd_t *pmd; |
| 28 | |
| 29 | next = (addr & PUD_MASK) + PUD_SIZE; |
| 30 | if (next > end) |
| 31 | next = end; |
| 32 | |
| 33 | if (pud_present(*pud)) { |
| 34 | pmd = pmd_offset(pud, 0); |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 35 | ident_pmd_init(info, pmd, addr, next); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 36 | continue; |
| 37 | } |
| 38 | pmd = (pmd_t *)info->alloc_pgt_page(info->context); |
| 39 | if (!pmd) |
| 40 | return -ENOMEM; |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 41 | ident_pmd_init(info, pmd, addr, next); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 42 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | 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] | 49 | unsigned long pstart, unsigned long pend) |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 50 | { |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 51 | unsigned long addr = pstart + info->offset; |
| 52 | unsigned long end = pend + info->offset; |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 53 | unsigned long next; |
| 54 | int result; |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 55 | |
| 56 | for (; addr < end; addr = next) { |
Rafael J. Wysocki | e4630fd | 2016-08-08 15:31:31 +0200 | [diff] [blame] | 57 | pgd_t *pgd = pgd_page + pgd_index(addr); |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 58 | pud_t *pud; |
| 59 | |
| 60 | next = (addr & PGDIR_MASK) + PGDIR_SIZE; |
| 61 | if (next > end) |
| 62 | next = end; |
| 63 | |
| 64 | if (pgd_present(*pgd)) { |
| 65 | pud = pud_offset(pgd, 0); |
| 66 | result = ident_pud_init(info, pud, addr, next); |
| 67 | if (result) |
| 68 | return result; |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | pud = (pud_t *)info->alloc_pgt_page(info->context); |
| 73 | if (!pud) |
| 74 | return -ENOMEM; |
| 75 | result = ident_pud_init(info, pud, addr, next); |
| 76 | if (result) |
| 77 | return result; |
| 78 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE)); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |