Skip to content

Commit 5fbe4c2

Browse files
committed
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull misc x86 fixes from Ingo Molnar: "This contains: - EFI fixes - a boot printout fix - ASLR/kASLR fixes - intel microcode driver fixes - other misc fixes Most of the linecount comes from an EFI revert" * 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/mm/ASLR: Avoid PAGE_SIZE redefinition for UML subarch x86/microcode/intel: Handle truncated microcode images more robustly x86/microcode/intel: Guard against stack overflow in the loader x86, mm/ASLR: Fix stack randomization on 64-bit systems x86/mm/init: Fix incorrect page size in init_memory_mapping() printks x86/mm/ASLR: Propagate base load address calculation Documentation/x86: Fix path in zero-page.txt x86/apic: Fix the devicetree build in certain configs Revert "efi/libstub: Call get_memory_map() to obtain map and desc sizes" x86/efi: Avoid triple faults during EFI mixed mode calls
2 parents e2defd0 + 570e1aa commit 5fbe4c2

20 files changed

Lines changed: 419 additions & 239 deletions

File tree

‎Documentation/x86/zero-page.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ protocol of kernel. These should be filled by bootloader or 16-bit
33
real-mode setup code of the kernel. References/settings to it mainly
44
are in:
55

6-
arch/x86/include/asm/bootparam.h
6+
arch/x86/include/uapi/asm/bootparam.h
77

88

99
Offset Proto Name Meaning

‎arch/x86/boot/compressed/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
5151

5252
vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
5353
$(objtree)/drivers/firmware/efi/libstub/lib.a
54+
vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
5455

5556
$(obj)/vmlinux: $(vmlinux-objs-y) FORCE
5657
$(call if_changed,ld)

‎arch/x86/boot/compressed/aslr.c‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
1515
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
1616

17+
struct kaslr_setup_data {
18+
__u64 next;
19+
__u32 type;
20+
__u32 len;
21+
__u8 data[1];
22+
} kaslr_setup_data;
23+
1724
#define I8254_PORT_CONTROL 0x43
1825
#define I8254_PORT_COUNTER0 0x40
1926
#define I8254_CMD_READBACK 0xC0
@@ -295,7 +302,29 @@ static unsigned long find_random_addr(unsigned long minimum,
295302
return slots_fetch_random();
296303
}
297304

298-
unsigned char *choose_kernel_location(unsigned char *input,
305+
static void add_kaslr_setup_data(struct boot_params *params, __u8 enabled)
306+
{
307+
struct setup_data *data;
308+
309+
kaslr_setup_data.type = SETUP_KASLR;
310+
kaslr_setup_data.len = 1;
311+
kaslr_setup_data.next = 0;
312+
kaslr_setup_data.data[0] = enabled;
313+
314+
data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
315+
316+
while (data && data->next)
317+
data = (struct setup_data *)(unsigned long)data->next;
318+
319+
if (data)
320+
data->next = (unsigned long)&kaslr_setup_data;
321+
else
322+
params->hdr.setup_data = (unsigned long)&kaslr_setup_data;
323+
324+
}
325+
326+
unsigned char *choose_kernel_location(struct boot_params *params,
327+
unsigned char *input,
299328
unsigned long input_size,
300329
unsigned char *output,
301330
unsigned long output_size)
@@ -306,14 +335,17 @@ unsigned char *choose_kernel_location(unsigned char *input,
306335
#ifdef CONFIG_HIBERNATION
307336
if (!cmdline_find_option_bool("kaslr")) {
308337
debug_putstr("KASLR disabled by default...\n");
338+
add_kaslr_setup_data(params, 0);
309339
goto out;
310340
}
311341
#else
312342
if (cmdline_find_option_bool("nokaslr")) {
313343
debug_putstr("KASLR disabled by cmdline...\n");
344+
add_kaslr_setup_data(params, 0);
314345
goto out;
315346
}
316347
#endif
348+
add_kaslr_setup_data(params, 1);
317349

318350
/* Record the various known unsafe memory ranges. */
319351
mem_avoid_init((unsigned long)input, input_size,

‎arch/x86/boot/compressed/efi_stub_64.S‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,3 @@
33
#include <asm/processor-flags.h>
44

55
#include "../../platform/efi/efi_stub_64.S"
6-
7-
#ifdef CONFIG_EFI_MIXED
8-
.code64
9-
.text
10-
ENTRY(efi64_thunk)
11-
push %rbp
12-
push %rbx
13-
14-
subq $16, %rsp
15-
leaq efi_exit32(%rip), %rax
16-
movl %eax, 8(%rsp)
17-
leaq efi_gdt64(%rip), %rax
18-
movl %eax, 4(%rsp)
19-
movl %eax, 2(%rax) /* Fixup the gdt base address */
20-
leaq efi32_boot_gdt(%rip), %rax
21-
movl %eax, (%rsp)
22-
23-
call __efi64_thunk
24-
25-
addq $16, %rsp
26-
pop %rbx
27-
pop %rbp
28-
ret
29-
ENDPROC(efi64_thunk)
30-
#endif /* CONFIG_EFI_MIXED */
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (C) 2014, 2015 Intel Corporation; author Matt Fleming
3+
*
4+
* Early support for invoking 32-bit EFI services from a 64-bit kernel.
5+
*
6+
* Because this thunking occurs before ExitBootServices() we have to
7+
* restore the firmware's 32-bit GDT before we make EFI serivce calls,
8+
* since the firmware's 32-bit IDT is still currently installed and it
9+
* needs to be able to service interrupts.
10+
*
11+
* On the plus side, we don't have to worry about mangling 64-bit
12+
* addresses into 32-bits because we're executing with an identify
13+
* mapped pagetable and haven't transitioned to 64-bit virtual addresses
14+
* yet.
15+
*/
16+
17+
#include <linux/linkage.h>
18+
#include <asm/msr.h>
19+
#include <asm/page_types.h>
20+
#include <asm/processor-flags.h>
21+
#include <asm/segment.h>
22+
23+
.code64
24+
.text
25+
ENTRY(efi64_thunk)
26+
push %rbp
27+
push %rbx
28+
29+
subq $8, %rsp
30+
leaq efi_exit32(%rip), %rax
31+
movl %eax, 4(%rsp)
32+
leaq efi_gdt64(%rip), %rax
33+
movl %eax, (%rsp)
34+
movl %eax, 2(%rax) /* Fixup the gdt base address */
35+
36+
movl %ds, %eax
37+
push %rax
38+
movl %es, %eax
39+
push %rax
40+
movl %ss, %eax
41+
push %rax
42+
43+
/*
44+
* Convert x86-64 ABI params to i386 ABI
45+
*/
46+
subq $32, %rsp
47+
movl %esi, 0x0(%rsp)
48+
movl %edx, 0x4(%rsp)
49+
movl %ecx, 0x8(%rsp)
50+
movq %r8, %rsi
51+
movl %esi, 0xc(%rsp)
52+
movq %r9, %rsi
53+
movl %esi, 0x10(%rsp)
54+
55+
sgdt save_gdt(%rip)
56+
57+
leaq 1f(%rip), %rbx
58+
movq %rbx, func_rt_ptr(%rip)
59+
60+
/*
61+
* Switch to gdt with 32-bit segments. This is the firmware GDT
62+
* that was installed when the kernel started executing. This
63+
* pointer was saved at the EFI stub entry point in head_64.S.
64+
*/
65+
leaq efi32_boot_gdt(%rip), %rax
66+
lgdt (%rax)
67+
68+
pushq $__KERNEL_CS
69+
leaq efi_enter32(%rip), %rax
70+
pushq %rax
71+
lretq
72+
73+
1: addq $32, %rsp
74+
75+
lgdt save_gdt(%rip)
76+
77+
pop %rbx
78+
movl %ebx, %ss
79+
pop %rbx
80+
movl %ebx, %es
81+
pop %rbx
82+
movl %ebx, %ds
83+
84+
/*
85+
* Convert 32-bit status code into 64-bit.
86+
*/
87+
test %rax, %rax
88+
jz 1f
89+
movl %eax, %ecx
90+
andl $0x0fffffff, %ecx
91+
andl $0xf0000000, %eax
92+
shl $32, %rax
93+
or %rcx, %rax
94+
1:
95+
addq $8, %rsp
96+
pop %rbx
97+
pop %rbp
98+
ret
99+
ENDPROC(efi64_thunk)
100+
101+
ENTRY(efi_exit32)
102+
movq func_rt_ptr(%rip), %rax
103+
push %rax
104+
mov %rdi, %rax
105+
ret
106+
ENDPROC(efi_exit32)
107+
108+
.code32
109+
/*
110+
* EFI service pointer must be in %edi.
111+
*
112+
* The stack should represent the 32-bit calling convention.
113+
*/
114+
ENTRY(efi_enter32)
115+
movl $__KERNEL_DS, %eax
116+
movl %eax, %ds
117+
movl %eax, %es
118+
movl %eax, %ss
119+
120+
/* Reload pgtables */
121+
movl %cr3, %eax
122+
movl %eax, %cr3
123+
124+
/* Disable paging */
125+
movl %cr0, %eax
126+
btrl $X86_CR0_PG_BIT, %eax
127+
movl %eax, %cr0
128+
129+
/* Disable long mode via EFER */
130+
movl $MSR_EFER, %ecx
131+
rdmsr
132+
btrl $_EFER_LME, %eax
133+
wrmsr
134+
135+
call *%edi
136+
137+
/* We must preserve return value */
138+
movl %eax, %edi
139+
140+
/*
141+
* Some firmware will return with interrupts enabled. Be sure to
142+
* disable them before we switch GDTs.
143+
*/
144+
cli
145+
146+
movl 56(%esp), %eax
147+
movl %eax, 2(%eax)
148+
lgdtl (%eax)
149+
150+
movl %cr4, %eax
151+
btsl $(X86_CR4_PAE_BIT), %eax
152+
movl %eax, %cr4
153+
154+
movl %cr3, %eax
155+
movl %eax, %cr3
156+
157+
movl $MSR_EFER, %ecx
158+
rdmsr
159+
btsl $_EFER_LME, %eax
160+
wrmsr
161+
162+
xorl %eax, %eax
163+
lldt %ax
164+
165+
movl 60(%esp), %eax
166+
pushl $__KERNEL_CS
167+
pushl %eax
168+
169+
/* Enable paging */
170+
movl %cr0, %eax
171+
btsl $X86_CR0_PG_BIT, %eax
172+
movl %eax, %cr0
173+
lret
174+
ENDPROC(efi_enter32)
175+
176+
.data
177+
.balign 8
178+
.global efi32_boot_gdt
179+
efi32_boot_gdt: .word 0
180+
.quad 0
181+
182+
save_gdt: .word 0
183+
.quad 0
184+
func_rt_ptr: .quad 0
185+
186+
.global efi_gdt64
187+
efi_gdt64:
188+
.word efi_gdt64_end - efi_gdt64
189+
.long 0 /* Filled out by user */
190+
.word 0
191+
.quad 0x0000000000000000 /* NULL descriptor */
192+
.quad 0x00af9a000000ffff /* __KERNEL_CS */
193+
.quad 0x00cf92000000ffff /* __KERNEL_DS */
194+
.quad 0x0080890000000000 /* TS descriptor */
195+
.quad 0x0000000000000000 /* TS continued */
196+
efi_gdt64_end:

‎arch/x86/boot/compressed/misc.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
401401
* the entire decompressed kernel plus relocation table, or the
402402
* entire decompressed kernel plus .bss and .brk sections.
403403
*/
404-
output = choose_kernel_location(input_data, input_len, output,
404+
output = choose_kernel_location(real_mode, input_data, input_len,
405+
output,
405406
output_len > run_size ? output_len
406407
: run_size);
407408

‎arch/x86/boot/compressed/misc.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ int cmdline_find_option_bool(const char *option);
5757

5858
#if CONFIG_RANDOMIZE_BASE
5959
/* aslr.c */
60-
unsigned char *choose_kernel_location(unsigned char *input,
60+
unsigned char *choose_kernel_location(struct boot_params *params,
61+
unsigned char *input,
6162
unsigned long input_size,
6263
unsigned char *output,
6364
unsigned long output_size);
6465
/* cpuflags.c */
6566
bool has_cpuflag(int flag);
6667
#else
6768
static inline
68-
unsigned char *choose_kernel_location(unsigned char *input,
69+
unsigned char *choose_kernel_location(struct boot_params *params,
70+
unsigned char *input,
6971
unsigned long input_size,
7072
unsigned char *output,
7173
unsigned long output_size)

‎arch/x86/include/asm/apic.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ void register_lapic_address(unsigned long address);
213213
extern void setup_boot_APIC_clock(void);
214214
extern void setup_secondary_APIC_clock(void);
215215
extern int APIC_init_uniprocessor(void);
216+
217+
#ifdef CONFIG_X86_64
218+
static inline int apic_force_enable(unsigned long addr)
219+
{
220+
return -1;
221+
}
222+
#else
216223
extern int apic_force_enable(unsigned long addr);
224+
#endif
217225

218226
extern int apic_bsp_setup(bool upmode);
219227
extern void apic_ap_setup(void);

‎arch/x86/include/asm/page_types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ extern int devmem_is_allowed(unsigned long pagenr);
5151
extern unsigned long max_low_pfn_mapped;
5252
extern unsigned long max_pfn_mapped;
5353

54+
extern bool kaslr_enabled;
55+
5456
static inline phys_addr_t get_max_mapped(void)
5557
{
5658
return (phys_addr_t)max_pfn_mapped << PAGE_SHIFT;

‎arch/x86/include/uapi/asm/bootparam.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SETUP_DTB 2
88
#define SETUP_PCI 3
99
#define SETUP_EFI 4
10+
#define SETUP_KASLR 5
1011

1112
/* ram_size flags */
1213
#define RAMDISK_IMAGE_START_MASK 0x07FF

0 commit comments

Comments
 (0)