Skip to content

Commit aee8c67

Browse files
KAGA-KOKOsuryasaimadhu
authored andcommitted
x86/fpu: Return proper error codes from user access functions
When *RSTOR from user memory raises an exception, there is no way to differentiate them. That's bad because it forces the slow path even when the failure was not a fault. If the operation raised eg. #GP then going through the slow path is pointless. Use _ASM_EXTABLE_FAULT() which stores the trap number and let the exception fixup return the negated trap number as error. This allows to separate the fast path and let it handle faults directly and avoid the slow path for all other exceptions. Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0a6c2e9 commit aee8c67

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

arch/x86/include/asm/fpu/internal.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,22 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {}
8888
#endif
8989
extern void save_fpregs_to_fpstate(struct fpu *fpu);
9090

91+
/* Returns 0 or the negated trap number, which results in -EFAULT for #PF */
9192
#define user_insn(insn, output, input...) \
9293
({ \
9394
int err; \
9495
\
9596
might_fault(); \
9697
\
9798
asm volatile(ASM_STAC "\n" \
98-
"1:" #insn "\n\t" \
99+
"1: " #insn "\n" \
99100
"2: " ASM_CLAC "\n" \
100101
".section .fixup,\"ax\"\n" \
101-
"3: movl $-1,%[err]\n" \
102+
"3: negl %%eax\n" \
102103
" jmp 2b\n" \
103104
".previous\n" \
104-
_ASM_EXTABLE(1b, 3b) \
105-
: [err] "=r" (err), output \
105+
_ASM_EXTABLE_FAULT(1b, 3b) \
106+
: [err] "=a" (err), output \
106107
: "0"(0), input); \
107108
err; \
108109
})
@@ -196,16 +197,20 @@ static inline void fxsave(struct fxregs_state *fx)
196197
#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
197198
#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
198199

200+
/*
201+
* After this @err contains 0 on success or the negated trap number when
202+
* the operation raises an exception. For faults this results in -EFAULT.
203+
*/
199204
#define XSTATE_OP(op, st, lmask, hmask, err) \
200205
asm volatile("1:" op "\n\t" \
201206
"xor %[err], %[err]\n" \
202207
"2:\n\t" \
203208
".pushsection .fixup,\"ax\"\n\t" \
204-
"3: movl $-2,%[err]\n\t" \
209+
"3: negl %%eax\n\t" \
205210
"jmp 2b\n\t" \
206211
".popsection\n\t" \
207-
_ASM_EXTABLE(1b, 3b) \
208-
: [err] "=r" (err) \
212+
_ASM_EXTABLE_FAULT(1b, 3b) \
213+
: [err] "=a" (err) \
209214
: "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
210215
: "memory")
211216

0 commit comments

Comments
 (0)