diff options
author | H. Peter Anvin <hpa@linux.intel.com> | 2012-05-16 16:49:10 -0400 |
---|---|---|
committer | H. Peter Anvin <hpa@linux.intel.com> | 2012-05-16 16:49:10 -0400 |
commit | 137127018812ec7fcccb9843156cfc0b5cfa31d5 (patch) | |
tree | 12718f2524a26c5bd26a53518068229d84c41c08 /arch/x86/realmode | |
parent | 51edbe6a2f47c78c6c6e529999ee0a044fe59a89 (diff) |
x86, realmode: Move kernel/realmode.c to realmode/init.c
Keep all the realmode code together, including initialization (only
the rm/ subdirectory is actually built as real-mode code, anyway.)
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Diffstat (limited to 'arch/x86/realmode')
-rw-r--r-- | arch/x86/realmode/Makefile | 1 | ||||
-rw-r--r-- | arch/x86/realmode/init.c | 116 |
2 files changed, 117 insertions, 0 deletions
diff --git a/arch/x86/realmode/Makefile b/arch/x86/realmode/Makefile index a05b3aca64ad..94f7fbe97b08 100644 --- a/arch/x86/realmode/Makefile +++ b/arch/x86/realmode/Makefile | |||
@@ -9,6 +9,7 @@ | |||
9 | 9 | ||
10 | subdir- := rm | 10 | subdir- := rm |
11 | 11 | ||
12 | obj-y += init.o | ||
12 | obj-y += rmpiggy.o | 13 | obj-y += rmpiggy.o |
13 | 14 | ||
14 | $(obj)/rmpiggy.o: $(obj)/rm/realmode.bin | 15 | $(obj)/rmpiggy.o: $(obj)/rm/realmode.bin |
diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c new file mode 100644 index 000000000000..099277984b80 --- /dev/null +++ b/arch/x86/realmode/init.c | |||
@@ -0,0 +1,116 @@ | |||
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 | |||
8 | struct real_mode_header *real_mode_header; | ||
9 | u32 *trampoline_cr4_features; | ||
10 | |||
11 | void __init setup_real_mode(void) | ||
12 | { | ||
13 | phys_addr_t mem; | ||
14 | u16 real_mode_seg; | ||
15 | u32 *rel; | ||
16 | u32 count; | ||
17 | u32 *ptr; | ||
18 | u16 *seg; | ||
19 | int i; | ||
20 | unsigned char *base; | ||
21 | struct trampoline_header *trampoline_header; | ||
22 | size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); | ||
23 | #ifdef CONFIG_X86_64 | ||
24 | u64 *trampoline_pgd; | ||
25 | u32 efer_low, efer_high; | ||
26 | #endif | ||
27 | |||
28 | /* Has to be in very low memory so we can execute real-mode AP code. */ | ||
29 | mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE); | ||
30 | if (!mem) | ||
31 | panic("Cannot allocate trampoline\n"); | ||
32 | |||
33 | base = __va(mem); | ||
34 | memblock_reserve(mem, size); | ||
35 | real_mode_header = (struct real_mode_header *) base; | ||
36 | printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n", | ||
37 | base, (unsigned long long)mem, size); | ||
38 | |||
39 | memcpy(base, real_mode_blob, size); | ||
40 | |||
41 | real_mode_seg = __pa(base) >> 4; | ||
42 | rel = (u32 *) real_mode_relocs; | ||
43 | |||
44 | /* 16-bit segment relocations. */ | ||
45 | count = rel[0]; | ||
46 | rel = &rel[1]; | ||
47 | for (i = 0; i < count; i++) { | ||
48 | seg = (u16 *) (base + rel[i]); | ||
49 | *seg = real_mode_seg; | ||
50 | } | ||
51 | |||
52 | /* 32-bit linear relocations. */ | ||
53 | count = rel[i]; | ||
54 | rel = &rel[i + 1]; | ||
55 | for (i = 0; i < count; i++) { | ||
56 | ptr = (u32 *) (base + rel[i]); | ||
57 | *ptr += __pa(base); | ||
58 | } | ||
59 | |||
60 | /* Must be perfomed *after* relocation. */ | ||
61 | trampoline_header = (struct trampoline_header *) | ||
62 | __va(real_mode_header->trampoline_header); | ||
63 | |||
64 | #ifdef CONFIG_X86_32 | ||
65 | trampoline_header->start = __pa(startup_32_smp); | ||
66 | trampoline_header->gdt_limit = __BOOT_DS + 7; | ||
67 | trampoline_header->gdt_base = __pa(boot_gdt); | ||
68 | #else | ||
69 | /* | ||
70 | * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR | ||
71 | * so we need to mask it out. | ||
72 | */ | ||
73 | rdmsr(MSR_EFER, efer_low, efer_high); | ||
74 | trampoline_header->efer_low = efer_low & ~EFER_LMA; | ||
75 | trampoline_header->efer_high = efer_high; | ||
76 | |||
77 | trampoline_header->start = (u64) secondary_startup_64; | ||
78 | trampoline_cr4_features = &trampoline_header->cr4; | ||
79 | *trampoline_cr4_features = read_cr4(); | ||
80 | |||
81 | trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd); | ||
82 | trampoline_pgd[0] = __pa(level3_ident_pgt) + _KERNPG_TABLE; | ||
83 | trampoline_pgd[511] = __pa(level3_kernel_pgt) + _KERNPG_TABLE; | ||
84 | #endif | ||
85 | } | ||
86 | |||
87 | /* | ||
88 | * set_real_mode_permissions() gets called very early, to guarantee the | ||
89 | * availability of low memory. This is before the proper kernel page | ||
90 | * tables are set up, so we cannot set page permissions in that | ||
91 | * function. Thus, we use an arch_initcall instead. | ||
92 | */ | ||
93 | static int __init set_real_mode_permissions(void) | ||
94 | { | ||
95 | unsigned char *base = (unsigned char *) real_mode_header; | ||
96 | size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); | ||
97 | |||
98 | size_t ro_size = | ||
99 | PAGE_ALIGN(real_mode_header->ro_end) - | ||
100 | __pa(base); | ||
101 | |||
102 | size_t text_size = | ||
103 | PAGE_ALIGN(real_mode_header->ro_end) - | ||
104 | real_mode_header->text_start; | ||
105 | |||
106 | unsigned long text_start = | ||
107 | (unsigned long) __va(real_mode_header->text_start); | ||
108 | |||
109 | set_memory_nx((unsigned long) base, size >> PAGE_SHIFT); | ||
110 | set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT); | ||
111 | set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT); | ||
112 | |||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | arch_initcall(set_real_mode_permissions); | ||