blob: 747b71e8f5479da20da86d2952ea9884ad18f088 [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>
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -07007#include <asm/tlbflush.h>
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03008
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +03009struct real_mode_header *real_mode_header;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030010u32 *trampoline_cr4_features;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030011
Thomas Garnierb234e8a2016-06-21 17:47:01 -070012/* Hold the pgd entry used on booting additional CPUs */
13pgd_t trampoline_pgd_entry;
14
Yinghai Lu4f7b92262013-01-24 12:19:51 -080015void __init reserve_real_mode(void)
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030016{
17 phys_addr_t mem;
Yinghai Lu4f7b92262013-01-24 12:19:51 -080018 unsigned char *base;
19 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
20
21 /* Has to be under 1M so we can execute real-mode AP code. */
22 mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
23 if (!mem)
24 panic("Cannot allocate trampoline\n");
25
26 base = __va(mem);
27 memblock_reserve(mem, size);
28 real_mode_header = (struct real_mode_header *) base;
29 printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n",
30 base, (unsigned long long)mem, size);
31}
32
Andy Lutomirskid0de0f62016-08-10 02:29:15 -070033static void __init setup_real_mode(void)
Yinghai Lu4f7b92262013-01-24 12:19:51 -080034{
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030035 u16 real_mode_seg;
H. Peter Anvin73060062013-12-18 15:52:13 -080036 const u32 *rel;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030037 u32 count;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030038 unsigned char *base;
H. Peter Anvin73060062013-12-18 15:52:13 -080039 unsigned long phys_base;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030040 struct trampoline_header *trampoline_header;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030041 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030042#ifdef CONFIG_X86_64
43 u64 *trampoline_pgd;
H. Peter Anvin638d9572012-05-16 14:02:05 -070044 u64 efer;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030045#endif
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030046
Yinghai Lu4f7b92262013-01-24 12:19:51 -080047 base = (unsigned char *)real_mode_header;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030048
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030049 memcpy(base, real_mode_blob, size);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030050
H. Peter Anvin73060062013-12-18 15:52:13 -080051 phys_base = __pa(base);
52 real_mode_seg = phys_base >> 4;
53
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030054 rel = (u32 *) real_mode_relocs;
55
56 /* 16-bit segment relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -080057 count = *rel++;
58 while (count--) {
59 u16 *seg = (u16 *) (base + *rel++);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030060 *seg = real_mode_seg;
61 }
62
63 /* 32-bit linear relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -080064 count = *rel++;
65 while (count--) {
66 u32 *ptr = (u32 *) (base + *rel++);
67 *ptr += phys_base;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030068 }
69
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030070 /* Must be perfomed *after* relocation. */
71 trampoline_header = (struct trampoline_header *)
72 __va(real_mode_header->trampoline_header);
73
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030074#ifdef CONFIG_X86_32
Alexander Duyckfc8d7822012-11-16 13:57:13 -080075 trampoline_header->start = __pa_symbol(startup_32_smp);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030076 trampoline_header->gdt_limit = __BOOT_DS + 7;
Alexander Duyckfc8d7822012-11-16 13:57:13 -080077 trampoline_header->gdt_base = __pa_symbol(boot_gdt);
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030078#else
H. Peter Anvin79603872012-05-16 13:22:41 -070079 /*
80 * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
81 * so we need to mask it out.
82 */
H. Peter Anvin638d9572012-05-16 14:02:05 -070083 rdmsrl(MSR_EFER, efer);
84 trampoline_header->efer = efer & ~EFER_LMA;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030085
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030086 trampoline_header->start = (u64) secondary_startup_64;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030087 trampoline_cr4_features = &trampoline_header->cr4;
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -070088 *trampoline_cr4_features = mmu_cr4_features;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030089
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030090 trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
Thomas Garnierb234e8a2016-06-21 17:47:01 -070091 trampoline_pgd[0] = trampoline_pgd_entry.pgd;
Yinghai Lu9735e912013-01-24 12:19:50 -080092 trampoline_pgd[511] = init_level4_pgt[511].pgd;
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030093#endif
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030094}
95
96/*
Yinghai Lu4f7b92262013-01-24 12:19:51 -080097 * reserve_real_mode() gets called very early, to guarantee the
Yinghai Lu231b3642013-01-24 12:19:47 -080098 * availability of low memory. This is before the proper kernel page
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030099 * tables are set up, so we cannot set page permissions in that
Yinghai Lu231b3642013-01-24 12:19:47 -0800100 * function. Also trampoline code will be executed by APs so we
101 * need to mark it executable at do_pre_smp_initcalls() at least,
102 * thus run it as a early_initcall().
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300103 */
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700104static void __init set_real_mode_permissions(void)
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300105{
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300106 unsigned char *base = (unsigned char *) real_mode_header;
107 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300108
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300109 size_t ro_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300110 PAGE_ALIGN(real_mode_header->ro_end) -
111 __pa(base);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300112
113 size_t text_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300114 PAGE_ALIGN(real_mode_header->ro_end) -
115 real_mode_header->text_start;
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300116
117 unsigned long text_start =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300118 (unsigned long) __va(real_mode_header->text_start);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300119
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300120 set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
121 set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300122 set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700123}
124
125static int __init init_real_mode(void)
126{
127 if (!real_mode_header)
128 panic("Real mode trampoline was not allocated");
129
130 setup_real_mode();
131 set_real_mode_permissions();
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300132
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300133 return 0;
134}
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700135early_initcall(init_real_mode);