]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/head64.c
x86/boot/64: Rename init_level4_pgt and early_level4_pgt
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / head64.c
CommitLineData
1da177e4 1/*
835c34a1 2 * prepare to run common code
1da177e4
LT
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
1da177e4
LT
5 */
6
be3606ff 7#define DISABLE_BRANCH_PROFILING
1da177e4
LT
8#include <linux/init.h>
9#include <linux/linkage.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/percpu.h>
eaf76e8b 14#include <linux/start_kernel.h>
8b664aa6 15#include <linux/io.h>
72d7c3b3 16#include <linux/memblock.h>
1da177e4
LT
17
18#include <asm/processor.h>
19#include <asm/proto.h>
20#include <asm/smp.h>
1da177e4
LT
21#include <asm/setup.h>
22#include <asm/desc.h>
f6c2e333 23#include <asm/pgtable.h>
cfd243d4 24#include <asm/tlbflush.h>
2bc0414e 25#include <asm/sections.h>
718fc13b 26#include <asm/kdebug.h>
66441bd3 27#include <asm/e820/api.h>
47a3d5da 28#include <asm/bios_ebda.h>
5dcd14ec 29#include <asm/bootparam_utils.h>
feddc9de 30#include <asm/microcode.h>
ef7f0d6a 31#include <asm/kasan.h>
1da177e4 32
8170e6be
PA
33/*
34 * Manage page tables very early on.
35 */
65ade2f8 36extern pgd_t early_top_pgt[PTRS_PER_PGD];
8170e6be 37extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
c88d7150 38static unsigned int __initdata next_early_pgt;
5e427ec2 39pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
8170e6be 40
c88d7150
KS
41static void __init *fixup_pointer(void *ptr, unsigned long physaddr)
42{
43 return ptr - (void *)_text + (void *)physaddr;
44}
45
46void __init __startup_64(unsigned long physaddr)
47{
48 unsigned long load_delta, *p;
49 pgdval_t *pgd;
50 pudval_t *pud;
51 pmdval_t *pmd, pmd_entry;
52 int i;
53
54 /* Is the address too large? */
55 if (physaddr >> MAX_PHYSMEM_BITS)
56 for (;;);
57
58 /*
59 * Compute the delta between the address I am compiled to run at
60 * and the address I am actually running at.
61 */
62 load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
63
64 /* Is the address not 2M aligned? */
65 if (load_delta & ~PMD_PAGE_MASK)
66 for (;;);
67
68 /* Fixup the physical addresses in the page table */
69
65ade2f8 70 pgd = fixup_pointer(&early_top_pgt, physaddr);
c88d7150
KS
71 pgd[pgd_index(__START_KERNEL_map)] += load_delta;
72
73 pud = fixup_pointer(&level3_kernel_pgt, physaddr);
74 pud[510] += load_delta;
75 pud[511] += load_delta;
76
77 pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
78 pmd[506] += load_delta;
79
80 /*
81 * Set up the identity mapping for the switchover. These
82 * entries should *NOT* have the global bit set! This also
83 * creates a bunch of nonsense entries but that is fine --
84 * it avoids problems around wraparound.
85 */
86
87 pud = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
88 pmd = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
89
90 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
91 pgd[i + 0] = (pgdval_t)pud + _KERNPG_TABLE;
92 pgd[i + 1] = (pgdval_t)pud + _KERNPG_TABLE;
93
94 i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
95 pud[i + 0] = (pudval_t)pmd + _KERNPG_TABLE;
96 pud[i + 1] = (pudval_t)pmd + _KERNPG_TABLE;
97
98 pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
99 pmd_entry += physaddr;
100
101 for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
102 int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD;
103 pmd[idx] = pmd_entry + i * PMD_SIZE;
104 }
105
106 /*
107 * Fixup the kernel text+data virtual addresses. Note that
108 * we might write invalid pmds, when the kernel is relocated
109 * cleanup_highmap() fixes this up along with the mappings
110 * beyond _end.
111 */
112
113 pmd = fixup_pointer(level2_kernel_pgt, physaddr);
114 for (i = 0; i < PTRS_PER_PMD; i++) {
115 if (pmd[i] & _PAGE_PRESENT)
116 pmd[i] += load_delta;
117 }
118
119 /* Fixup phys_base */
120 p = fixup_pointer(&phys_base, physaddr);
121 *p += load_delta;
122}
123
8170e6be
PA
124/* Wipe all early page tables except for the kernel symbol map */
125static void __init reset_early_page_tables(void)
cfd243d4 126{
65ade2f8 127 memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
8170e6be 128 next_early_pgt = 0;
65ade2f8 129 write_cr3(__pa_nodebug(early_top_pgt));
8170e6be
PA
130}
131
132/* Create a new PMD entry */
133int __init early_make_pgtable(unsigned long address)
134{
135 unsigned long physaddr = address - __PAGE_OFFSET;
8170e6be 136 pgdval_t pgd, *pgd_p;
6b9c75ac 137 pudval_t pud, *pud_p;
8170e6be
PA
138 pmdval_t pmd, *pmd_p;
139
140 /* Invalid address or early pgt is done ? */
65ade2f8 141 if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
8170e6be
PA
142 return -1;
143
6b9c75ac 144again:
65ade2f8 145 pgd_p = &early_top_pgt[pgd_index(address)].pgd;
8170e6be
PA
146 pgd = *pgd_p;
147
148 /*
149 * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
150 * critical -- __PAGE_OFFSET would point us back into the dynamic
151 * range and we might end up looping forever...
152 */
6b9c75ac 153 if (pgd)
8170e6be 154 pud_p = (pudval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
6b9c75ac
YL
155 else {
156 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
8170e6be 157 reset_early_page_tables();
6b9c75ac
YL
158 goto again;
159 }
8170e6be
PA
160
161 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
a91bbe01 162 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
8170e6be
PA
163 *pgd_p = (pgdval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
164 }
6b9c75ac
YL
165 pud_p += pud_index(address);
166 pud = *pud_p;
8170e6be 167
6b9c75ac
YL
168 if (pud)
169 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
170 else {
171 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
172 reset_early_page_tables();
173 goto again;
174 }
175
176 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
a91bbe01 177 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
6b9c75ac
YL
178 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
179 }
78d77df7 180 pmd = (physaddr & PMD_MASK) + early_pmd_flags;
6b9c75ac 181 pmd_p[pmd_index(address)] = pmd;
8170e6be
PA
182
183 return 0;
cfd243d4
VG
184}
185
1da177e4
LT
186/* Don't add a printk in there. printk relies on the PDA which is not initialized
187 yet. */
188static void __init clear_bss(void)
189{
1da177e4 190 memset(__bss_start, 0,
2bc0414e 191 (unsigned long) __bss_stop - (unsigned long) __bss_start);
1da177e4
LT
192}
193
f1da834c
YL
194static unsigned long get_cmd_line_ptr(void)
195{
196 unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
197
ee92d815
YL
198 cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
199
f1da834c
YL
200 return cmd_line_ptr;
201}
202
1da177e4
LT
203static void __init copy_bootdata(char *real_mode_data)
204{
1da177e4 205 char * command_line;
f1da834c 206 unsigned long cmd_line_ptr;
1da177e4 207
30c82645 208 memcpy(&boot_params, real_mode_data, sizeof boot_params);
5dcd14ec 209 sanitize_boot_params(&boot_params);
f1da834c
YL
210 cmd_line_ptr = get_cmd_line_ptr();
211 if (cmd_line_ptr) {
212 command_line = __va(cmd_line_ptr);
30c82645 213 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
1da177e4 214 }
1da177e4
LT
215}
216
2605fc21 217asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
1da177e4 218{
1da177e4
LT
219 int i;
220
b4e0409a
IM
221 /*
222 * Build-time sanity checks on the kernel image and module
223 * area mappings. (these are purely build-time and produce no code)
224 */
8e3c2a8c
BP
225 BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
226 BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
b4e0409a 227 BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
8e3c2a8c 228 BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
b4e0409a
IM
229 BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
230 BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
231 BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
232 (__START_KERNEL & PGDIR_MASK)));
66d4bdf2 233 BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
b4e0409a 234
1e02ce4c
AL
235 cr4_init_shadow();
236
8170e6be
PA
237 /* Kill off the identity-map trampoline */
238 reset_early_page_tables();
239
3df0af0e
YL
240 clear_bss();
241
65ade2f8 242 clear_page(init_top_pgt);
d0f77d4d 243
5d5aa3cf
AP
244 kasan_early_init();
245
ac630dd9 246 for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
425be567 247 set_intr_gate(i, early_idt_handler_array[i]);
9d1c6e7c 248 load_idt((const struct desc_ptr *)&idt_descr);
f6c2e333 249
fa2bbce9
YL
250 copy_bootdata(__va(real_mode_data));
251
feddc9de
FY
252 /*
253 * Load microcode early on BSP.
254 */
255 load_ucode_bsp();
256
65ade2f8
KS
257 /* set init_top_pgt kernel high mapping*/
258 init_top_pgt[511] = early_top_pgt[511];
8170e6be 259
f97013fd
JF
260 x86_64_start_reservations(real_mode_data);
261}
262
263void __init x86_64_start_reservations(char *real_mode_data)
264{
fa2bbce9
YL
265 /* version is always not zero if it is copied */
266 if (!boot_params.hdr.version)
267 copy_bootdata(__va(real_mode_data));
9de819fe 268
8d152e7a 269 x86_early_init_platform_quirks();
75175278 270
3fda5bb4
AS
271 switch (boot_params.hdr.hardware_subarch) {
272 case X86_SUBARCH_INTEL_MID:
273 x86_intel_mid_early_setup();
274 break;
275 default:
276 break;
277 }
278
1da177e4
LT
279 start_kernel();
280}