blob: 0b7a63d9844038a02492ae5ddf0847b51d51bdd7 [file] [log] [blame]
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03001#include <linux/io.h>
2#include <linux/memblock.h>
3
4#include <asm/cacheflush.h>
5#include <asm/pgtable.h>
6#include <asm/realmode.h>
7
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +03008struct real_mode_header *real_mode_header;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +03009u32 *trampoline_cr4_features;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030010
Yinghai Lu4f7b92262013-01-24 12:19:51 -080011void __init reserve_real_mode(void)
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030012{
13 phys_addr_t mem;
Yinghai Lu4f7b92262013-01-24 12:19:51 -080014 unsigned char *base;
15 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
16
17 /* Has to be under 1M so we can execute real-mode AP code. */
18 mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
19 if (!mem)
20 panic("Cannot allocate trampoline\n");
21
22 base = __va(mem);
23 memblock_reserve(mem, size);
24 real_mode_header = (struct real_mode_header *) base;
25 printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n",
26 base, (unsigned long long)mem, size);
27}
28
29void __init setup_real_mode(void)
30{
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030031 u16 real_mode_seg;
H. Peter Anvin73060062013-12-18 15:52:13 -080032 const u32 *rel;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030033 u32 count;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030034 unsigned char *base;
H. Peter Anvin73060062013-12-18 15:52:13 -080035 unsigned long phys_base;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030036 struct trampoline_header *trampoline_header;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030037 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030038#ifdef CONFIG_X86_64
39 u64 *trampoline_pgd;
H. Peter Anvin638d9572012-05-16 14:02:05 -070040 u64 efer;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030041#endif
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030042
Yinghai Lu4f7b92262013-01-24 12:19:51 -080043 base = (unsigned char *)real_mode_header;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030044
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030045 memcpy(base, real_mode_blob, size);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030046
H. Peter Anvin73060062013-12-18 15:52:13 -080047 phys_base = __pa(base);
48 real_mode_seg = phys_base >> 4;
49
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030050 rel = (u32 *) real_mode_relocs;
51
52 /* 16-bit segment relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -080053 count = *rel++;
54 while (count--) {
55 u16 *seg = (u16 *) (base + *rel++);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030056 *seg = real_mode_seg;
57 }
58
59 /* 32-bit linear relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -080060 count = *rel++;
61 while (count--) {
62 u32 *ptr = (u32 *) (base + *rel++);
63 *ptr += phys_base;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030064 }
65
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030066 /* Must be perfomed *after* relocation. */
67 trampoline_header = (struct trampoline_header *)
68 __va(real_mode_header->trampoline_header);
69
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030070#ifdef CONFIG_X86_32
Alexander Duyckfc8d7822012-11-16 13:57:13 -080071 trampoline_header->start = __pa_symbol(startup_32_smp);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030072 trampoline_header->gdt_limit = __BOOT_DS + 7;
Alexander Duyckfc8d7822012-11-16 13:57:13 -080073 trampoline_header->gdt_base = __pa_symbol(boot_gdt);
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030074#else
H. Peter Anvin79603872012-05-16 13:22:41 -070075 /*
76 * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
77 * so we need to mask it out.
78 */
H. Peter Anvin638d9572012-05-16 14:02:05 -070079 rdmsrl(MSR_EFER, efer);
80 trampoline_header->efer = efer & ~EFER_LMA;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030081
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030082 trampoline_header->start = (u64) secondary_startup_64;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030083 trampoline_cr4_features = &trampoline_header->cr4;
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070084 *trampoline_cr4_features = __read_cr4();
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030085
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030086 trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
Yinghai Lu9735e912013-01-24 12:19:50 -080087 trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
88 trampoline_pgd[511] = init_level4_pgt[511].pgd;
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030089#endif
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030090}
91
92/*
Yinghai Lu4f7b92262013-01-24 12:19:51 -080093 * reserve_real_mode() gets called very early, to guarantee the
Yinghai Lu231b3642013-01-24 12:19:47 -080094 * availability of low memory. This is before the proper kernel page
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030095 * tables are set up, so we cannot set page permissions in that
Yinghai Lu231b3642013-01-24 12:19:47 -080096 * function. Also trampoline code will be executed by APs so we
97 * need to mark it executable at do_pre_smp_initcalls() at least,
98 * thus run it as a early_initcall().
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030099 */
100static int __init set_real_mode_permissions(void)
101{
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300102 unsigned char *base = (unsigned char *) real_mode_header;
103 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300104
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300105 size_t ro_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300106 PAGE_ALIGN(real_mode_header->ro_end) -
107 __pa(base);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300108
109 size_t text_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300110 PAGE_ALIGN(real_mode_header->ro_end) -
111 real_mode_header->text_start;
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300112
113 unsigned long text_start =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300114 (unsigned long) __va(real_mode_header->text_start);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300115
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300116 set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
117 set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300118 set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
119
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300120 return 0;
121}
Yinghai Lu231b3642013-01-24 12:19:47 -0800122early_initcall(set_real_mode_permissions);