Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 1 | #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 Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 8 | struct real_mode_header *real_mode_header; |
Jarkko Sakkinen | cda846f | 2012-05-08 21:22:46 +0300 | [diff] [blame] | 9 | u32 *trampoline_cr4_features; |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 10 | |
Yinghai Lu | 4f7b9226 | 2013-01-24 12:19:51 -0800 | [diff] [blame] | 11 | void __init reserve_real_mode(void) |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 12 | { |
| 13 | phys_addr_t mem; |
Yinghai Lu | 4f7b9226 | 2013-01-24 12:19:51 -0800 | [diff] [blame] | 14 | 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 | |
| 29 | void __init setup_real_mode(void) |
| 30 | { |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 31 | u16 real_mode_seg; |
H. Peter Anvin | 7306006 | 2013-12-18 15:52:13 -0800 | [diff] [blame] | 32 | const u32 *rel; |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 33 | u32 count; |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 34 | unsigned char *base; |
H. Peter Anvin | 7306006 | 2013-12-18 15:52:13 -0800 | [diff] [blame] | 35 | unsigned long phys_base; |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 36 | struct trampoline_header *trampoline_header; |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 37 | size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 38 | #ifdef CONFIG_X86_64 |
| 39 | u64 *trampoline_pgd; |
H. Peter Anvin | 638d957 | 2012-05-16 14:02:05 -0700 | [diff] [blame] | 40 | u64 efer; |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 41 | #endif |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 42 | |
Yinghai Lu | 4f7b9226 | 2013-01-24 12:19:51 -0800 | [diff] [blame] | 43 | base = (unsigned char *)real_mode_header; |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 44 | |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 45 | memcpy(base, real_mode_blob, size); |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 46 | |
H. Peter Anvin | 7306006 | 2013-12-18 15:52:13 -0800 | [diff] [blame] | 47 | phys_base = __pa(base); |
| 48 | real_mode_seg = phys_base >> 4; |
| 49 | |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 50 | rel = (u32 *) real_mode_relocs; |
| 51 | |
| 52 | /* 16-bit segment relocations. */ |
H. Peter Anvin | 7306006 | 2013-12-18 15:52:13 -0800 | [diff] [blame] | 53 | count = *rel++; |
| 54 | while (count--) { |
| 55 | u16 *seg = (u16 *) (base + *rel++); |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 56 | *seg = real_mode_seg; |
| 57 | } |
| 58 | |
| 59 | /* 32-bit linear relocations. */ |
H. Peter Anvin | 7306006 | 2013-12-18 15:52:13 -0800 | [diff] [blame] | 60 | count = *rel++; |
| 61 | while (count--) { |
| 62 | u32 *ptr = (u32 *) (base + *rel++); |
| 63 | *ptr += phys_base; |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 64 | } |
| 65 | |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 66 | /* Must be perfomed *after* relocation. */ |
| 67 | trampoline_header = (struct trampoline_header *) |
| 68 | __va(real_mode_header->trampoline_header); |
| 69 | |
Jarkko Sakkinen | 48927bb | 2012-05-08 21:22:28 +0300 | [diff] [blame] | 70 | #ifdef CONFIG_X86_32 |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 71 | trampoline_header->start = __pa_symbol(startup_32_smp); |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 72 | trampoline_header->gdt_limit = __BOOT_DS + 7; |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 73 | trampoline_header->gdt_base = __pa_symbol(boot_gdt); |
Jarkko Sakkinen | 48927bb | 2012-05-08 21:22:28 +0300 | [diff] [blame] | 74 | #else |
H. Peter Anvin | 7960387 | 2012-05-16 13:22:41 -0700 | [diff] [blame] | 75 | /* |
| 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 Anvin | 638d957 | 2012-05-16 14:02:05 -0700 | [diff] [blame] | 79 | rdmsrl(MSR_EFER, efer); |
| 80 | trampoline_header->efer = efer & ~EFER_LMA; |
Jarkko Sakkinen | cda846f | 2012-05-08 21:22:46 +0300 | [diff] [blame] | 81 | |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 82 | trampoline_header->start = (u64) secondary_startup_64; |
Jarkko Sakkinen | cda846f | 2012-05-08 21:22:46 +0300 | [diff] [blame] | 83 | trampoline_cr4_features = &trampoline_header->cr4; |
| 84 | *trampoline_cr4_features = read_cr4(); |
| 85 | |
Jarkko Sakkinen | f37240f | 2012-05-08 21:22:43 +0300 | [diff] [blame] | 86 | trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd); |
Yinghai Lu | 9735e91 | 2013-01-24 12:19:50 -0800 | [diff] [blame] | 87 | trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd; |
| 88 | trampoline_pgd[511] = init_level4_pgt[511].pgd; |
Jarkko Sakkinen | 48927bb | 2012-05-08 21:22:28 +0300 | [diff] [blame] | 89 | #endif |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
Yinghai Lu | 4f7b9226 | 2013-01-24 12:19:51 -0800 | [diff] [blame] | 93 | * reserve_real_mode() gets called very early, to guarantee the |
Yinghai Lu | 231b364 | 2013-01-24 12:19:47 -0800 | [diff] [blame] | 94 | * availability of low memory. This is before the proper kernel page |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 95 | * tables are set up, so we cannot set page permissions in that |
Yinghai Lu | 231b364 | 2013-01-24 12:19:47 -0800 | [diff] [blame] | 96 | * 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 Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 99 | */ |
| 100 | static int __init set_real_mode_permissions(void) |
| 101 | { |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 102 | unsigned char *base = (unsigned char *) real_mode_header; |
| 103 | size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 104 | |
Jarkko Sakkinen | f156ffc | 2012-05-08 21:22:30 +0300 | [diff] [blame] | 105 | size_t ro_size = |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 106 | PAGE_ALIGN(real_mode_header->ro_end) - |
| 107 | __pa(base); |
Jarkko Sakkinen | f156ffc | 2012-05-08 21:22:30 +0300 | [diff] [blame] | 108 | |
| 109 | size_t text_size = |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 110 | PAGE_ALIGN(real_mode_header->ro_end) - |
| 111 | real_mode_header->text_start; |
Jarkko Sakkinen | f156ffc | 2012-05-08 21:22:30 +0300 | [diff] [blame] | 112 | |
| 113 | unsigned long text_start = |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 114 | (unsigned long) __va(real_mode_header->text_start); |
Jarkko Sakkinen | f156ffc | 2012-05-08 21:22:30 +0300 | [diff] [blame] | 115 | |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 116 | set_memory_nx((unsigned long) base, size >> PAGE_SHIFT); |
| 117 | set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT); |
Jarkko Sakkinen | f156ffc | 2012-05-08 21:22:30 +0300 | [diff] [blame] | 118 | set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT); |
| 119 | |
Jarkko Sakkinen | 084ee1c6 | 2012-05-08 21:22:26 +0300 | [diff] [blame] | 120 | return 0; |
| 121 | } |
Yinghai Lu | 231b364 | 2013-01-24 12:19:47 -0800 | [diff] [blame] | 122 | early_initcall(set_real_mode_permissions); |