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