blob: 968d7005f4a72454ccf8678967f040fe06f36ad6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kees Cookcb18ef02016-05-09 13:22:05 -07002/*
3 * Helper routines for building identity mapping page tables. This is
4 * included by both the compressed kernel and the regular kernel.
5 */
Yinghai Lucf4fb152016-05-06 15:01:34 -07006
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +02007static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page,
Yinghai Lucf4fb152016-05-06 15:01:34 -07008 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. Wysockie4630fd2016-08-08 15:31:31 +020014 if (pmd_present(*pmd))
15 continue;
16
Xunlei Pang66aad4f2017-05-04 09:42:50 +080017 set_pmd(pmd, __pmd((addr - info->offset) | info->page_flag));
Yinghai Lucf4fb152016-05-06 15:01:34 -070018 }
19}
Kees Cookcb18ef02016-05-09 13:22:05 -070020
Yinghai Lucf4fb152016-05-06 15:01:34 -070021static 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 Pang66aad4f2017-05-04 09:42:50 +080034 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 Lucf4fb152016-05-06 15:01:34 -070046 if (pud_present(*pud)) {
47 pmd = pmd_offset(pud, 0);
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +020048 ident_pmd_init(info, pmd, addr, next);
Yinghai Lucf4fb152016-05-06 15:01:34 -070049 continue;
50 }
51 pmd = (pmd_t *)info->alloc_pgt_page(info->context);
52 if (!pmd)
53 return -ENOMEM;
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +020054 ident_pmd_init(info, pmd, addr, next);
Tom Lendackybba4ed02017-07-17 16:10:28 -050055 set_pud(pud, __pud(__pa(pmd) | info->kernpg_flag));
Yinghai Lucf4fb152016-05-06 15:01:34 -070056 }
57
58 return 0;
59}
60
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +030061static 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;
Arvind Sankar1fcd0092020-10-27 19:06:48 -040065 int result;
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +030066
67 for (; addr < end; addr = next) {
68 p4d_t *p4d = p4d_page + p4d_index(addr);
69 pud_t *pud;
70
71 next = (addr & P4D_MASK) + P4D_SIZE;
72 if (next > end)
73 next = end;
74
75 if (p4d_present(*p4d)) {
76 pud = pud_offset(p4d, 0);
Arvind Sankar1fcd0092020-10-27 19:06:48 -040077 result = ident_pud_init(info, pud, addr, next);
78 if (result)
79 return result;
80
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +030081 continue;
82 }
83 pud = (pud_t *)info->alloc_pgt_page(info->context);
84 if (!pud)
85 return -ENOMEM;
Arvind Sankar1fcd0092020-10-27 19:06:48 -040086
87 result = ident_pud_init(info, pud, addr, next);
88 if (result)
89 return result;
90
Tom Lendackybba4ed02017-07-17 16:10:28 -050091 set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag));
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +030092 }
93
94 return 0;
95}
96
Yinghai Lucf4fb152016-05-06 15:01:34 -070097int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +020098 unsigned long pstart, unsigned long pend)
Yinghai Lucf4fb152016-05-06 15:01:34 -070099{
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +0200100 unsigned long addr = pstart + info->offset;
101 unsigned long end = pend + info->offset;
Yinghai Lucf4fb152016-05-06 15:01:34 -0700102 unsigned long next;
103 int result;
Yinghai Lucf4fb152016-05-06 15:01:34 -0700104
Tom Lendackybba4ed02017-07-17 16:10:28 -0500105 /* Set the default pagetable flags if not supplied */
106 if (!info->kernpg_flag)
107 info->kernpg_flag = _KERNPG_TABLE;
108
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700109 /* Filter out unsupported __PAGE_KERNEL_* bits: */
110 info->kernpg_flag &= __default_kernel_pte_mask;
111
Yinghai Lucf4fb152016-05-06 15:01:34 -0700112 for (; addr < end; addr = next) {
Rafael J. Wysockie4630fd2016-08-08 15:31:31 +0200113 pgd_t *pgd = pgd_page + pgd_index(addr);
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300114 p4d_t *p4d;
Yinghai Lucf4fb152016-05-06 15:01:34 -0700115
116 next = (addr & PGDIR_MASK) + PGDIR_SIZE;
117 if (next > end)
118 next = end;
119
120 if (pgd_present(*pgd)) {
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300121 p4d = p4d_offset(pgd, 0);
122 result = ident_p4d_init(info, p4d, addr, next);
Yinghai Lucf4fb152016-05-06 15:01:34 -0700123 if (result)
124 return result;
125 continue;
126 }
127
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300128 p4d = (p4d_t *)info->alloc_pgt_page(info->context);
129 if (!p4d)
Yinghai Lucf4fb152016-05-06 15:01:34 -0700130 return -ENOMEM;
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300131 result = ident_p4d_init(info, p4d, addr, next);
Yinghai Lucf4fb152016-05-06 15:01:34 -0700132 if (result)
133 return result;
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300134 if (pgtable_l5_enabled()) {
Tom Lendackybba4ed02017-07-17 16:10:28 -0500135 set_pgd(pgd, __pgd(__pa(p4d) | info->kernpg_flag));
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300136 } else {
137 /*
138 * With p4d folded, pgd is equal to p4d.
139 * The pgd entry has to point to the pud page table in this case.
140 */
141 pud_t *pud = pud_offset(p4d, 0);
Tom Lendackybba4ed02017-07-17 16:10:28 -0500142 set_pgd(pgd, __pgd(__pa(pud) | info->kernpg_flag));
Kirill A. Shutemovea3b5e62017-03-13 17:33:07 +0300143 }
Yinghai Lucf4fb152016-05-06 15:01:34 -0700144 }
145
146 return 0;
147}