Skip to content

Commit fda57b2

Browse files
amlutoIngo Molnar
authored andcommitted
x86/entry: Improve system call entry comments
Ingo suggested that the comments should explain when the various entries are used. This adds these explanations and improves other parts of the comments. Signed-off-by: Andy Lutomirski <luto@kernel.org> Cc: Andrew Cooper <andrew.cooper3@citrix.com> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/9524ecef7a295347294300045d08354d6a57c6e7.1457578375.git.luto@kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 392a625 commit fda57b2

File tree

3 files changed

+128
-28
lines changed

3 files changed

+128
-28
lines changed

arch/x86/entry/entry_32.S

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,38 @@ ENTRY(xen_sysenter_target)
307307
jmp sysenter_past_esp
308308
#endif
309309

310+
/*
311+
* 32-bit SYSENTER entry.
312+
*
313+
* 32-bit system calls through the vDSO's __kernel_vsyscall enter here
314+
* if X86_FEATURE_SEP is available. This is the preferred system call
315+
* entry on 32-bit systems.
316+
*
317+
* The SYSENTER instruction, in principle, should *only* occur in the
318+
* vDSO. In practice, a small number of Android devices were shipped
319+
* with a copy of Bionic that inlined a SYSENTER instruction. This
320+
* never happened in any of Google's Bionic versions -- it only happened
321+
* in a narrow range of Intel-provided versions.
322+
*
323+
* SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
324+
* IF and VM in RFLAGS are cleared (IOW: interrupts are off).
325+
* SYSENTER does not save anything on the stack,
326+
* and does not save old EIP (!!!), ESP, or EFLAGS.
327+
*
328+
* To avoid losing track of EFLAGS.VM (and thus potentially corrupting
329+
* user and/or vm86 state), we explicitly disable the SYSENTER
330+
* instruction in vm86 mode by reprogramming the MSRs.
331+
*
332+
* Arguments:
333+
* eax system call number
334+
* ebx arg1
335+
* ecx arg2
336+
* edx arg3
337+
* esi arg4
338+
* edi arg5
339+
* ebp user stack
340+
* 0(%ebp) arg6
341+
*/
310342
ENTRY(entry_SYSENTER_32)
311343
movl TSS_sysenter_sp0(%esp), %esp
312344
sysenter_past_esp:
@@ -397,7 +429,34 @@ sysenter_past_esp:
397429
GLOBAL(__end_SYSENTER_singlestep_region)
398430
ENDPROC(entry_SYSENTER_32)
399431

400-
# system call handler stub
432+
/*
433+
* 32-bit legacy system call entry.
434+
*
435+
* 32-bit x86 Linux system calls traditionally used the INT $0x80
436+
* instruction. INT $0x80 lands here.
437+
*
438+
* This entry point can be used by any 32-bit perform system calls.
439+
* Instances of INT $0x80 can be found inline in various programs and
440+
* libraries. It is also used by the vDSO's __kernel_vsyscall
441+
* fallback for hardware that doesn't support a faster entry method.
442+
* Restarted 32-bit system calls also fall back to INT $0x80
443+
* regardless of what instruction was originally used to do the system
444+
* call. (64-bit programs can use INT $0x80 as well, but they can
445+
* only run on 64-bit kernels and therefore land in
446+
* entry_INT80_compat.)
447+
*
448+
* This is considered a slow path. It is not used by most libc
449+
* implementations on modern hardware except during process startup.
450+
*
451+
* Arguments:
452+
* eax system call number
453+
* ebx arg1
454+
* ecx arg2
455+
* edx arg3
456+
* esi arg4
457+
* edi arg5
458+
* ebp arg6
459+
*/
401460
ENTRY(entry_INT80_32)
402461
ASM_CLAC
403462
pushl %eax /* pt_regs->orig_ax */

arch/x86/entry/entry_64.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ ENDPROC(native_usergs_sysret64)
103103
/*
104104
* 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
105105
*
106+
* This is the only entry point used for 64-bit system calls. The
107+
* hardware interface is reasonably well designed and the register to
108+
* argument mapping Linux uses fits well with the registers that are
109+
* available when SYSCALL is used.
110+
*
111+
* SYSCALL instructions can be found inlined in libc implementations as
112+
* well as some other programs and libraries. There are also a handful
113+
* of SYSCALL instructions in the vDSO used, for example, as a
114+
* clock_gettimeofday fallback.
115+
*
106116
* 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
107117
* then loads new ss, cs, and rip from previously programmed MSRs.
108118
* rflags gets masked by a value from another MSR (so CLD and CLAC

arch/x86/entry/entry_64_compat.S

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
.section .entry.text, "ax"
2020

2121
/*
22-
* 32-bit SYSENTER instruction entry.
22+
* 32-bit SYSENTER entry.
2323
*
24-
* SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
25-
* IF and VM in rflags are cleared (IOW: interrupts are off).
24+
* 32-bit system calls through the vDSO's __kernel_vsyscall enter here
25+
* on 64-bit kernels running on Intel CPUs.
26+
*
27+
* The SYSENTER instruction, in principle, should *only* occur in the
28+
* vDSO. In practice, a small number of Android devices were shipped
29+
* with a copy of Bionic that inlined a SYSENTER instruction. This
30+
* never happened in any of Google's Bionic versions -- it only happened
31+
* in a narrow range of Intel-provided versions.
32+
*
33+
* SYSENTER loads SS, RSP, CS, and RIP from previously programmed MSRs.
34+
* IF and VM in RFLAGS are cleared (IOW: interrupts are off).
2635
* SYSENTER does not save anything on the stack,
27-
* and does not save old rip (!!!) and rflags.
36+
* and does not save old RIP (!!!), RSP, or RFLAGS.
2837
*
2938
* Arguments:
3039
* eax system call number
@@ -35,10 +44,6 @@
3544
* edi arg5
3645
* ebp user stack
3746
* 0(%ebp) arg6
38-
*
39-
* This is purely a fast path. For anything complicated we use the int 0x80
40-
* path below. We set up a complete hardware stack frame to share code
41-
* with the int 0x80 path.
4247
*/
4348
ENTRY(entry_SYSENTER_compat)
4449
/* Interrupts are off on entry. */
@@ -131,17 +136,38 @@ GLOBAL(__end_entry_SYSENTER_compat)
131136
ENDPROC(entry_SYSENTER_compat)
132137

133138
/*
134-
* 32-bit SYSCALL instruction entry.
139+
* 32-bit SYSCALL entry.
140+
*
141+
* 32-bit system calls through the vDSO's __kernel_vsyscall enter here
142+
* on 64-bit kernels running on AMD CPUs.
143+
*
144+
* The SYSCALL instruction, in principle, should *only* occur in the
145+
* vDSO. In practice, it appears that this really is the case.
146+
* As evidence:
147+
*
148+
* - The calling convention for SYSCALL has changed several times without
149+
* anyone noticing.
150+
*
151+
* - Prior to the in-kernel X86_BUG_SYSRET_SS_ATTRS fixup, anything
152+
* user task that did SYSCALL without immediately reloading SS
153+
* would randomly crash.
135154
*
136-
* 32-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
137-
* then loads new ss, cs, and rip from previously programmed MSRs.
138-
* rflags gets masked by a value from another MSR (so CLD and CLAC
139-
* are not needed). SYSCALL does not save anything on the stack
140-
* and does not change rsp.
155+
* - Most programmers do not directly target AMD CPUs, and the 32-bit
156+
* SYSCALL instruction does not exist on Intel CPUs. Even on AMD
157+
* CPUs, Linux disables the SYSCALL instruction on 32-bit kernels
158+
* because the SYSCALL instruction in legacy/native 32-bit mode (as
159+
* opposed to compat mode) is sufficiently poorly designed as to be
160+
* essentially unusable.
141161
*
142-
* Note: rflags saving+masking-with-MSR happens only in Long mode
162+
* 32-bit SYSCALL saves RIP to RCX, clears RFLAGS.RF, then saves
163+
* RFLAGS to R11, then loads new SS, CS, and RIP from previously
164+
* programmed MSRs. RFLAGS gets masked by a value from another MSR
165+
* (so CLD and CLAC are not needed). SYSCALL does not save anything on
166+
* the stack and does not change RSP.
167+
*
168+
* Note: RFLAGS saving+masking-with-MSR happens only in Long mode
143169
* (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
144-
* Don't get confused: rflags saving+masking depends on Long Mode Active bit
170+
* Don't get confused: RFLAGS saving+masking depends on Long Mode Active bit
145171
* (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
146172
* or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
147173
*
@@ -241,7 +267,21 @@ sysret32_from_system_call:
241267
END(entry_SYSCALL_compat)
242268

243269
/*
244-
* Emulated IA32 system calls via int 0x80.
270+
* 32-bit legacy system call entry.
271+
*
272+
* 32-bit x86 Linux system calls traditionally used the INT $0x80
273+
* instruction. INT $0x80 lands here.
274+
*
275+
* This entry point can be used by 32-bit and 64-bit programs to perform
276+
* 32-bit system calls. Instances of INT $0x80 can be found inline in
277+
* various programs and libraries. It is also used by the vDSO's
278+
* __kernel_vsyscall fallback for hardware that doesn't support a faster
279+
* entry method. Restarted 32-bit system calls also fall back to INT
280+
* $0x80 regardless of what instruction was originally used to do the
281+
* system call.
282+
*
283+
* This is considered a slow path. It is not used by most libc
284+
* implementations on modern hardware except during process startup.
245285
*
246286
* Arguments:
247287
* eax system call number
@@ -250,17 +290,8 @@ END(entry_SYSCALL_compat)
250290
* edx arg3
251291
* esi arg4
252292
* edi arg5
253-
* ebp arg6 (note: not saved in the stack frame, should not be touched)
254-
*
255-
* Notes:
256-
* Uses the same stack frame as the x86-64 version.
257-
* All registers except eax must be saved (but ptrace may violate that).
258-
* Arguments are zero extended. For system calls that want sign extension and
259-
* take long arguments a wrapper is needed. Most calls can just be called
260-
* directly.
261-
* Assumes it is only called from user space and entered with interrupts off.
293+
* ebp arg6
262294
*/
263-
264295
ENTRY(entry_INT80_compat)
265296
/*
266297
* Interrupts are off on entry.

0 commit comments

Comments
 (0)