Skip to content

Commit df4c0b1

Browse files
committed
Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 asm updates from Ingo Molnar: - Add UMIP emulation/spoofing for 64-bit processes as well, because of Wine based gaming. - Clean up symbols/labels in low level asm code - Add an assembly optimized mul_u64_u32_div() implementation on x86-64. * 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/umip: Add emulation (spoofing) for UMIP covered instructions in 64-bit processes as well x86/asm: Make some functions local labels x86/asm/suspend: Get rid of bogus_64_magic x86/math64: Provide a sane mul_u64_u32_div() implementation for x86_64
2 parents 7e67a85 + e86c2c8 commit df4c0b1

File tree

9 files changed

+96
-70
lines changed

9 files changed

+96
-70
lines changed

arch/x86/boot/compressed/head_32.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ ENTRY(startup_32)
140140
/*
141141
* Jump to the relocated address.
142142
*/
143-
leal relocated(%ebx), %eax
143+
leal .Lrelocated(%ebx), %eax
144144
jmp *%eax
145145
ENDPROC(startup_32)
146146

@@ -209,7 +209,7 @@ ENDPROC(efi32_stub_entry)
209209
#endif
210210

211211
.text
212-
relocated:
212+
.Lrelocated:
213213

214214
/*
215215
* Clear BSS (stack is currently empty)

arch/x86/boot/compressed/head_64.S

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ENTRY(startup_32)
8787

8888
call verify_cpu
8989
testl %eax, %eax
90-
jnz no_longmode
90+
jnz .Lno_longmode
9191

9292
/*
9393
* Compute the delta between where we were compiled to run at
@@ -322,7 +322,7 @@ ENTRY(startup_64)
322322
1: popq %rdi
323323
subq $1b, %rdi
324324

325-
call adjust_got
325+
call .Ladjust_got
326326

327327
/*
328328
* At this point we are in long mode with 4-level paging enabled,
@@ -421,7 +421,7 @@ trampoline_return:
421421

422422
/* The new adjustment is the relocation address */
423423
movq %rbx, %rdi
424-
call adjust_got
424+
call .Ladjust_got
425425

426426
/*
427427
* Copy the compressed kernel to the end of our buffer
@@ -440,7 +440,7 @@ trampoline_return:
440440
/*
441441
* Jump to the relocated address.
442442
*/
443-
leaq relocated(%rbx), %rax
443+
leaq .Lrelocated(%rbx), %rax
444444
jmp *%rax
445445

446446
#ifdef CONFIG_EFI_STUB
@@ -511,7 +511,7 @@ ENDPROC(efi64_stub_entry)
511511
#endif
512512

513513
.text
514-
relocated:
514+
.Lrelocated:
515515

516516
/*
517517
* Clear BSS (stack is currently empty)
@@ -548,7 +548,7 @@ relocated:
548548
* first time we touch GOT).
549549
* RDI is the new adjustment to apply.
550550
*/
551-
adjust_got:
551+
.Ladjust_got:
552552
/* Walk through the GOT adding the address to the entries */
553553
leaq _got(%rip), %rdx
554554
leaq _egot(%rip), %rcx
@@ -622,7 +622,7 @@ ENTRY(trampoline_32bit_src)
622622
movl %eax, %cr4
623623

624624
/* Calculate address of paging_enabled() once we are executing in the trampoline */
625-
leal paging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%ecx), %eax
625+
leal .Lpaging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%ecx), %eax
626626

627627
/* Prepare the stack for far return to Long Mode */
628628
pushl $__KERNEL_CS
@@ -635,7 +635,7 @@ ENTRY(trampoline_32bit_src)
635635
lret
636636

637637
.code64
638-
paging_enabled:
638+
.Lpaging_enabled:
639639
/* Return from the trampoline */
640640
jmp *%rdi
641641

@@ -647,7 +647,7 @@ paging_enabled:
647647
.org trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_SIZE
648648

649649
.code32
650-
no_longmode:
650+
.Lno_longmode:
651651
/* This isn't an x86-64 CPU, so hang intentionally, we cannot continue */
652652
1:
653653
hlt

arch/x86/entry/entry_64.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,10 @@ ENTRY(native_load_gs_index)
10581058
ENDPROC(native_load_gs_index)
10591059
EXPORT_SYMBOL(native_load_gs_index)
10601060

1061-
_ASM_EXTABLE(.Lgs_change, bad_gs)
1061+
_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
10621062
.section .fixup, "ax"
10631063
/* running with kernelgs */
1064-
bad_gs:
1064+
.Lbad_gs:
10651065
SWAPGS /* switch back to user gs */
10661066
.macro ZAP_GS
10671067
/* This can't be a string because the preprocessor needs to see it. */

arch/x86/include/asm/div64.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ static inline u64 mul_u32_u32(u32 a, u32 b)
7373

7474
#else
7575
# include <asm-generic/div64.h>
76+
77+
static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 div)
78+
{
79+
u64 q;
80+
81+
asm ("mulq %2; divq %3" : "=a" (q)
82+
: "a" (a), "rm" ((u64)mul), "rm" ((u64)div)
83+
: "rdx");
84+
85+
return q;
86+
}
87+
#define mul_u64_u32_div mul_u64_u32_div
88+
7689
#endif /* CONFIG_X86_32 */
7790

7891
#endif /* _ASM_X86_DIV64_H */

arch/x86/kernel/acpi/wakeup_64.S

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ ENTRY(wakeup_long64)
1818
movq saved_magic, %rax
1919
movq $0x123456789abcdef0, %rdx
2020
cmpq %rdx, %rax
21-
jne bogus_64_magic
21+
je 2f
2222

23+
/* stop here on a saved_magic mismatch */
24+
movq $0xbad6d61676963, %rcx
25+
1:
26+
jmp 1b
27+
2:
2328
movw $__KERNEL_DS, %ax
2429
movw %ax, %ss
2530
movw %ax, %ds
@@ -37,9 +42,6 @@ ENTRY(wakeup_long64)
3742
jmp *%rax
3843
ENDPROC(wakeup_long64)
3944

40-
bogus_64_magic:
41-
jmp bogus_64_magic
42-
4345
ENTRY(do_suspend_lowlevel)
4446
FRAME_BEGIN
4547
subq $8, %rsp

arch/x86/kernel/umip.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/** DOC: Emulation for User-Mode Instruction Prevention (UMIP)
2020
*
2121
* The feature User-Mode Instruction Prevention present in recent Intel
22-
* processor prevents a group of instructions (sgdt, sidt, sldt, smsw, and str)
22+
* processor prevents a group of instructions (SGDT, SIDT, SLDT, SMSW and STR)
2323
* from being executed with CPL > 0. Otherwise, a general protection fault is
2424
* issued.
2525
*
@@ -36,24 +36,22 @@
3636
* DOSEMU2) rely on this subset of instructions to function.
3737
*
3838
* The instructions protected by UMIP can be split in two groups. Those which
39-
* return a kernel memory address (sgdt and sidt) and those which return a
40-
* value (sldt, str and smsw).
39+
* return a kernel memory address (SGDT and SIDT) and those which return a
40+
* value (SLDT, STR and SMSW).
4141
*
4242
* For the instructions that return a kernel memory address, applications
4343
* such as WineHQ rely on the result being located in the kernel memory space,
4444
* not the actual location of the table. The result is emulated as a hard-coded
4545
* value that, lies close to the top of the kernel memory. The limit for the GDT
4646
* and the IDT are set to zero.
4747
*
48-
* Given that sldt and str are not commonly used in programs that run on WineHQ
48+
* Given that SLDT and STR are not commonly used in programs that run on WineHQ
4949
* or DOSEMU2, they are not emulated.
5050
*
5151
* The instruction smsw is emulated to return the value that the register CR0
5252
* has at boot time as set in the head_32.
5353
*
54-
* Also, emulation is provided only for 32-bit processes; 64-bit processes
55-
* that attempt to use the instructions that UMIP protects will receive the
56-
* SIGSEGV signal issued as a consequence of the general protection fault.
54+
* Emulation is provided for both 32-bit and 64-bit processes.
5755
*
5856
* Care is taken to appropriately emulate the results when segmentation is
5957
* used. That is, rather than relying on USER_DS and USER_CS, the function
@@ -63,17 +61,18 @@
6361
* application uses a local descriptor table.
6462
*/
6563

66-
#define UMIP_DUMMY_GDT_BASE 0xfffe0000
67-
#define UMIP_DUMMY_IDT_BASE 0xffff0000
64+
#define UMIP_DUMMY_GDT_BASE 0xfffffffffffe0000ULL
65+
#define UMIP_DUMMY_IDT_BASE 0xffffffffffff0000ULL
6866

6967
/*
7068
* The SGDT and SIDT instructions store the contents of the global descriptor
7169
* table and interrupt table registers, respectively. The destination is a
7270
* memory operand of X+2 bytes. X bytes are used to store the base address of
73-
* the table and 2 bytes are used to store the limit. In 32-bit processes, the
74-
* only processes for which emulation is provided, X has a value of 4.
71+
* the table and 2 bytes are used to store the limit. In 32-bit processes X
72+
* has a value of 4, in 64-bit processes X has a value of 8.
7573
*/
76-
#define UMIP_GDT_IDT_BASE_SIZE 4
74+
#define UMIP_GDT_IDT_BASE_SIZE_64BIT 8
75+
#define UMIP_GDT_IDT_BASE_SIZE_32BIT 4
7776
#define UMIP_GDT_IDT_LIMIT_SIZE 2
7877

7978
#define UMIP_INST_SGDT 0 /* 0F 01 /0 */
@@ -189,6 +188,7 @@ static int identify_insn(struct insn *insn)
189188
* @umip_inst: A constant indicating the instruction to emulate
190189
* @data: Buffer into which the dummy result is stored
191190
* @data_size: Size of the emulated result
191+
* @x86_64: true if process is 64-bit, false otherwise
192192
*
193193
* Emulate an instruction protected by UMIP and provide a dummy result. The
194194
* result of the emulation is saved in @data. The size of the results depends
@@ -202,11 +202,8 @@ static int identify_insn(struct insn *insn)
202202
* 0 on success, -EINVAL on error while emulating.
203203
*/
204204
static int emulate_umip_insn(struct insn *insn, int umip_inst,
205-
unsigned char *data, int *data_size)
205+
unsigned char *data, int *data_size, bool x86_64)
206206
{
207-
unsigned long dummy_base_addr, dummy_value;
208-
unsigned short dummy_limit = 0;
209-
210207
if (!data || !data_size || !insn)
211208
return -EINVAL;
212209
/*
@@ -219,6 +216,9 @@ static int emulate_umip_insn(struct insn *insn, int umip_inst,
219216
* is always returned irrespective of the operand size.
220217
*/
221218
if (umip_inst == UMIP_INST_SGDT || umip_inst == UMIP_INST_SIDT) {
219+
u64 dummy_base_addr;
220+
u16 dummy_limit = 0;
221+
222222
/* SGDT and SIDT do not use registers operands. */
223223
if (X86_MODRM_MOD(insn->modrm.value) == 3)
224224
return -EINVAL;
@@ -228,13 +228,24 @@ static int emulate_umip_insn(struct insn *insn, int umip_inst,
228228
else
229229
dummy_base_addr = UMIP_DUMMY_IDT_BASE;
230230

231-
*data_size = UMIP_GDT_IDT_LIMIT_SIZE + UMIP_GDT_IDT_BASE_SIZE;
231+
/*
232+
* 64-bit processes use the entire dummy base address.
233+
* 32-bit processes use the lower 32 bits of the base address.
234+
* dummy_base_addr is always 64 bits, but we memcpy the correct
235+
* number of bytes from it to the destination.
236+
*/
237+
if (x86_64)
238+
*data_size = UMIP_GDT_IDT_BASE_SIZE_64BIT;
239+
else
240+
*data_size = UMIP_GDT_IDT_BASE_SIZE_32BIT;
241+
242+
memcpy(data + 2, &dummy_base_addr, *data_size);
232243

233-
memcpy(data + 2, &dummy_base_addr, UMIP_GDT_IDT_BASE_SIZE);
244+
*data_size += UMIP_GDT_IDT_LIMIT_SIZE;
234245
memcpy(data, &dummy_limit, UMIP_GDT_IDT_LIMIT_SIZE);
235246

236247
} else if (umip_inst == UMIP_INST_SMSW) {
237-
dummy_value = CR0_STATE;
248+
unsigned long dummy_value = CR0_STATE;
238249

239250
/*
240251
* Even though the CR0 register has 4 bytes, the number
@@ -290,11 +301,10 @@ static void force_sig_info_umip_fault(void __user *addr, struct pt_regs *regs)
290301
* fixup_umip_exception() - Fixup a general protection fault caused by UMIP
291302
* @regs: Registers as saved when entering the #GP handler
292303
*
293-
* The instructions sgdt, sidt, str, smsw, sldt cause a general protection
294-
* fault if executed with CPL > 0 (i.e., from user space). If the offending
295-
* user-space process is not in long mode, this function fixes the exception
296-
* up and provides dummy results for sgdt, sidt and smsw; str and sldt are not
297-
* fixed up. Also long mode user-space processes are not fixed up.
304+
* The instructions SGDT, SIDT, STR, SMSW and SLDT cause a general protection
305+
* fault if executed with CPL > 0 (i.e., from user space). This function fixes
306+
* the exception up and provides dummy results for SGDT, SIDT and SMSW; STR
307+
* and SLDT are not fixed up.
298308
*
299309
* If operands are memory addresses, results are copied to user-space memory as
300310
* indicated by the instruction pointed by eIP using the registers indicated in
@@ -373,13 +383,14 @@ bool fixup_umip_exception(struct pt_regs *regs)
373383
umip_pr_warning(regs, "%s instruction cannot be used by applications.\n",
374384
umip_insns[umip_inst]);
375385

376-
/* Do not emulate SLDT, STR or user long mode processes. */
377-
if (umip_inst == UMIP_INST_STR || umip_inst == UMIP_INST_SLDT || user_64bit_mode(regs))
386+
/* Do not emulate (spoof) SLDT or STR. */
387+
if (umip_inst == UMIP_INST_STR || umip_inst == UMIP_INST_SLDT)
378388
return false;
379389

380390
umip_pr_warning(regs, "For now, expensive software emulation returns the result.\n");
381391

382-
if (emulate_umip_insn(&insn, umip_inst, dummy_data, &dummy_data_size))
392+
if (emulate_umip_insn(&insn, umip_inst, dummy_data, &dummy_data_size,
393+
user_64bit_mode(regs)))
383394
return false;
384395

385396
/*

arch/x86/lib/copy_user_64.S

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
102:
3434
.section .fixup,"ax"
3535
103: addl %ecx,%edx /* ecx is zerorest also */
36-
jmp copy_user_handle_tail
36+
jmp .Lcopy_user_handle_tail
3737
.previous
3838

3939
_ASM_EXTABLE_UA(100b, 103b)
@@ -113,7 +113,7 @@ ENTRY(copy_user_generic_unrolled)
113113
40: leal (%rdx,%rcx,8),%edx
114114
jmp 60f
115115
50: movl %ecx,%edx
116-
60: jmp copy_user_handle_tail /* ecx is zerorest also */
116+
60: jmp .Lcopy_user_handle_tail /* ecx is zerorest also */
117117
.previous
118118

119119
_ASM_EXTABLE_UA(1b, 30b)
@@ -177,7 +177,7 @@ ENTRY(copy_user_generic_string)
177177
.section .fixup,"ax"
178178
11: leal (%rdx,%rcx,8),%ecx
179179
12: movl %ecx,%edx /* ecx is zerorest also */
180-
jmp copy_user_handle_tail
180+
jmp .Lcopy_user_handle_tail
181181
.previous
182182

183183
_ASM_EXTABLE_UA(1b, 11b)
@@ -210,7 +210,7 @@ ENTRY(copy_user_enhanced_fast_string)
210210

211211
.section .fixup,"ax"
212212
12: movl %ecx,%edx /* ecx is zerorest also */
213-
jmp copy_user_handle_tail
213+
jmp .Lcopy_user_handle_tail
214214
.previous
215215

216216
_ASM_EXTABLE_UA(1b, 12b)
@@ -231,15 +231,15 @@ EXPORT_SYMBOL(copy_user_enhanced_fast_string)
231231
* eax uncopied bytes or 0 if successful.
232232
*/
233233
ALIGN;
234-
copy_user_handle_tail:
234+
.Lcopy_user_handle_tail:
235235
movl %edx,%ecx
236236
1: rep movsb
237237
2: mov %ecx,%eax
238238
ASM_CLAC
239239
ret
240240

241241
_ASM_EXTABLE_UA(1b, 2b)
242-
END(copy_user_handle_tail)
242+
END(.Lcopy_user_handle_tail)
243243

244244
/*
245245
* copy_user_nocache - Uncached memory copy with exception handling
@@ -364,7 +364,7 @@ ENTRY(__copy_user_nocache)
364364
movl %ecx,%edx
365365
.L_fixup_handle_tail:
366366
sfence
367-
jmp copy_user_handle_tail
367+
jmp .Lcopy_user_handle_tail
368368
.previous
369369

370370
_ASM_EXTABLE_UA(1b, .L_fixup_4x8b_copy)

0 commit comments

Comments
 (0)