Skip to contentSkip to conversation

i386, entry_32: cleanup

PATCH New Alexander van Heukelum 1 participant 9 messages May 20, 2009 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386, entry_32: cleanup

Hi Ingo and others!

Here is a new batch of patches to the entry code. I started something similar a few months ago for x86_64, but I lack a 64-bit machine I can use to test at the moment, so I decided to make a start on i386 instead.

This series takes on the SAVE_ALL macro, which expanded to a pretty large number of instructions in every interrupt dispatcher in entry_32.S. Instead, it now expands to a call to a function (save_entry) that does the register saving and the preparation needed to call C functions.

This also gets rid of the indirect call used in the interrupt dispatcher. The old code does some interesting shuffle on the stack to pass the address of the C handler to a common piece of code. The new version creates a stub that for each interrupt, where each stub calls save_entry, does a direct call to the C handler and the jumps to a common return path.

I did some latency measurements and this code does not perform significantly better or worse than the old code on a "Sempron 2400+". The original aim was to shrink entry_32.o, but this goal was not reached. The size has not changes significantly, mostly due to the due to the check for %ss==__ESPFIX_SS in the exception stubs and the fact that the SAVE_ALL macro expanded to a lot of small instructions. On the other hand, entry_32.S shrunk by more than 10%:

 arch/x86/kernel/cpu/mcheck/mce_32.c |    6 +
 arch/x86/kernel/entry_32.S          |  513 +++++++++++------------------------
 2 files changed, 171 insertions(+), 348 deletions(-)

I'ld very much appreciate some more testing in the -x86 pool.

Greetings,
Alexander

#1 lore raw

PATCH 1/1 i386, entry_32: get rid of resume_userspace_sig define

resume_userspace_sig is defined to check_userspace or resume_userspace depending on CONFIG_VM86. It's used only two times, and once inside code conditionally compiled depending on CONFIG_VM86. Duplicating two trivial lines leaves a single #ifdef/#else block and removes the need for redefining labels. Inverting a branch and getting rid of some real code duplication removes a few bytes from the object file.

before, CONFIG_VM86=y
---------------------
c1008749 <work_notifysig>:

c1008749:       f7 44 24 38 00 00 02    testl  $0x20000,0x38(%esp)
c1008750:       00
c1008751:       89 e0                   mov    %esp,%eax
c1008753:       75 0f                   jne    c1008764 <work_notifysig_v86>
c1008755:       31 d2                   xor    %edx,%edx
c1008757:       e8 34 f0 ff ff          call   c1007790 <do_notify_resume>
c100875c:       e9 89 fd ff ff          jmp    c10084ea <check_userspace>
c1008761:       8d 76 00                lea    0x0(%esi),%esi
c1008764 <work_notifysig_v86>:
c1008764:       51                      push   %ecx
c1008765:       e8 76 69 01 00          call   c101f0e0 <save_v86_state>
c100876a:       59                      pop    %ecx
c100876b:       89 c4                   mov    %eax,%esp
c100876d:       31 d2                   xor    %edx,%edx
c100876f:       e8 1c f0 ff ff          call   c1007790 <do_notify_resume>
c1008774:       e9 71 fd ff ff          jmp    c10084ea <check_userspace>

after, CONFIG_VM86=y
--------------------
c1008749 <work_notifysig>:

c1008749:       f7 44 24 38 00 00 02    testl  $0x20000,0x38(%esp)
c1008750:       00
c1008751:       89 e0                   mov    %esp,%eax
c1008753:       74 09                   je     c100875e <no_work_notifysig_v86>
c1008755:       51                      push   %ecx
c1008756:       e8 65 69 01 00          call   c101f0c0 <save_v86_state>
c100875b:       59                      pop    %ecx
c100875c:       89 c4                   mov    %eax,%esp
c100875e <no_work_notifysig_v86>:
c100875e:       31 d2                   xor    %edx,%edx
c1008760:       e8 2b f0 ff ff          call   c1007790 <do_notify_resume>
c1008765:       e9 80 fd ff ff          jmp    c10084ea <check_userspace>

before, CONFIG_VM86=n
---------------------
c1008729 <work_notifysig>:

c1008729:       89 e0                   mov    %esp,%eax
c100872b:       31 d2                   xor    %edx,%edx
c100872d:       e8 3e f0 ff ff          call   c1007770 <do_notify_resume>
c1008732:       e9 a9 fd ff ff          jmp    c10084e0 <resume_userspace>

after, CONFIG_VM86=n (i.e., no change)
--------------------------------------

c1008729:       89 e0                   mov    %esp,%eax
c100872b:       31 d2                   xor    %edx,%edx
c100872d:       e8 3e f0 ff ff          call   c1007770 <do_notify_resume>
c1008732:       e9 a9 fd ff ff          jmp    c10084e0 <resume_userspace>
---
 arch/x86/kernel/entry_32.S |   22 +++++++---------------
 1 files changed, 7 insertions(+), 15 deletions(-)
arch/x86/kernel/entry_32.S +7 -15
index c929add..314563e 100644
@@ -96,12 +96,6 @@
 #endif
 .endm
 
-#ifdef CONFIG_VM86
-#define resume_userspace_sig	check_userspace
-#else
-#define resume_userspace_sig	resume_userspace
-#endif
-
 /*
  * User gs save/restore
  *
@@ -634,26 +628,24 @@ work_notifysig:				# deal with pending signals and
 #ifdef CONFIG_VM86
 	testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
 	movl %esp, %eax
-	jne work_notifysig_v86		# returning to kernel-space or
+	je no_work_notifysig_v86	# returning to kernel-space or
 					# vm86-space
-	xorl %edx, %edx
-	call do_notify_resume
-	jmp resume_userspace_sig
-
-	ALIGN
-work_notifysig_v86:
 	pushl %ecx			# save ti_flags for do_notify_resume
 	CFI_ADJUST_CFA_OFFSET 4
 	call save_v86_state		# %eax contains pt_regs pointer
 	popl %ecx
 	CFI_ADJUST_CFA_OFFSET -4
 	movl %eax, %esp
+no_work_notifysig_v86:
+	xorl %edx, %edx			# %eax contains pt_regs pointer
+	call do_notify_resume
+	jmp check_userspace
 #else
 	movl %esp, %eax
-#endif
 	xorl %edx, %edx
 	call do_notify_resume
-	jmp resume_userspace_sig
+	jmp resume_userspace
+#endif
 END(work_pending)
 
 	# perform syscall exit tracing
Signature · 1 line
1.6.0.4
#2 lore raw

PATCH 1/1 i386, entry_32: remove page_fault microoptimization

In reply to Alexander van Heukelum

A distinct pattern exists in the handler stubs for traps. The pagefault handler has a microoptimization where the handler falls through to the common handler code 'error_code'. This adds a short jump to the pagefault handler and aligns the error_code code.

No functional change intended.

impact: prepare for introduction of zeroentry/errorentry macros

---
 arch/x86/kernel/entry_32.S |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_32.S +8 -3
index 0e8ac16..3c04063 100644
@@ -1178,8 +1178,13 @@ ENTRY(page_fault)
 	RING0_EC_FRAME
 	pushl $do_page_fault
 	CFI_ADJUST_CFA_OFFSET 4
-	ALIGN
-error_code:
+	jmp error_code
+	CFI_ENDPROC
+END(page_fault)
+
+ENTRY(error_code)
+	RING0_EC_FRAME
+	CFI_ADJUST_CFA_OFFSET 4
 	/* the function address is in %gs's slot on the stack */
 	pushl %fs
 	CFI_ADJUST_CFA_OFFSET 4
@@ -1229,7 +1234,7 @@ error_code:
 	call *%edi
 	jmp ret_from_exception
 	CFI_ENDPROC
-END(page_fault)
+END(error_code)
 
 /*
  * Debug traps and NMI can happen at the one SYSENTER instruction
Signature · 1 line
1.6.0.4
#3 lore raw

PATCH 1/1 i386, entry_32/mce: add do_machine_check

In reply to Alexander van Heukelum

A distinct pattern exists in the handler stubs for traps. The mce is slightly different and that is not necessary. This adds an extra indirection via a trivial C function and makes the assembly code equivalent to the other trap stubs.

No functional change intended.

impact: prepare for introduction of zeroentry/errorentry macros

---
 arch/x86/kernel/cpu/mcheck/mce_32.c |    6 ++++++
 arch/x86/kernel/entry_32.S          |    2 +-
 2 files changed, 7 insertions(+), 1 deletions(-)
arch/x86/kernel/cpu/mcheck/mce_32.c +6 -0
index 3552119..b134ae6 100644
@@ -30,6 +30,12 @@ static void unexpected_machine_check(struct pt_regs *regs, long error_code)
 /* Call the installed machine check handler for this CPU setup. */
 void (*machine_check_vector)(struct pt_regs *, long error_code) = unexpected_machine_check;
 
+/* Call the machine check vector */
+void do_machine_check(struct pt_regs *regs, long error_code)
+{
+	machine_check_vector(regs, error_code);
+}
+
 /* This has to be run for each processor */
 void mcheck_init(struct cpuinfo_x86 *c)
 {
arch/x86/kernel/entry_32.S +1 -1
index 3b63eba..0e8ac16 100644
@@ -932,7 +932,7 @@ ENTRY(machine_check)
 	RING0_INT_FRAME
 	pushl $0
 	CFI_ADJUST_CFA_OFFSET 4
-	pushl machine_check_vector
+	pushl $do_machine_check
 	CFI_ADJUST_CFA_OFFSET 4
 	jmp error_code
 	CFI_ENDPROC
Signature · 1 line
1.6.0.4
#4 lore raw

PATCH 1/1 i386, entry_32: introduce zeroentry and errorentry macros

In reply to Alexander van Heukelum

Introduce zeroentry and errorentry macros en remove source code duplication.

Object code unchanged

impact: introduction of zeroentry/errorentry macros

---
 arch/x86/kernel/entry_32.S |  185 ++++++++++----------------------------------
 1 files changed, 41 insertions(+), 144 deletions(-)
arch/x86/kernel/entry_32.S +41 -144
index 3c04063..29dcd74 100644
@@ -307,6 +307,31 @@
 	CFI_OFFSET ebx, PT_EBX-PT_OLDESP
 .endm
 
+/*
+ * Exception entry points.
+ */
+.macro zeroentry sym do_sym errorcode=0
+ENTRY(\sym)
+	RING0_INT_FRAME
+	pushl $\errorcode
+	CFI_ADJUST_CFA_OFFSET 4
+	pushl $\do_sym
+	CFI_ADJUST_CFA_OFFSET 4
+	jmp error_code
+	CFI_ENDPROC
+END(\sym)
+.endm
+
+.macro errorentry sym do_sym
+ENTRY(\sym)
+	RING0_EC_FRAME
+	pushl $\do_sym
+	CFI_ADJUST_CFA_OFFSET 4
+	jmp error_code
+	CFI_ENDPROC
+END(\sym)
+.endm
+
 ENTRY(ret_from_fork)
 	CFI_STARTPROC
 	pushl %eax
@@ -800,35 +825,9 @@ ENDPROC(name)
 /* The include is where all of the SMP etc. interrupts come from */
 #include <asm/entry_arch.h>
 
-ENTRY(coprocessor_error)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_coprocessor_error
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(coprocessor_error)
-
-ENTRY(simd_coprocessor_error)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_simd_coprocessor_error
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(simd_coprocessor_error)
-
-ENTRY(device_not_available)
-	RING0_INT_FRAME
-	pushl $-1			# mark this as an int
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_device_not_available
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(device_not_available)
+zeroentry coprocessor_error do_coprocessor_error
+zeroentry simd_coprocessor_error do_simd_coprocessor_error
+zeroentry device_not_available do_device_not_available -1
 
 #ifdef CONFIG_PARAVIRT
 ENTRY(native_iret)
@@ -845,109 +844,19 @@ ENTRY(native_irq_enable_sysexit)
 END(native_irq_enable_sysexit)
 #endif
 
-ENTRY(overflow)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_overflow
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(overflow)
-
-ENTRY(bounds)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_bounds
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(bounds)
-
-ENTRY(invalid_op)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_invalid_op
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(invalid_op)
-
-ENTRY(coprocessor_segment_overrun)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_coprocessor_segment_overrun
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(coprocessor_segment_overrun)
-
-ENTRY(invalid_TSS)
-	RING0_EC_FRAME
-	pushl $do_invalid_TSS
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(invalid_TSS)
-
-ENTRY(segment_not_present)
-	RING0_EC_FRAME
-	pushl $do_segment_not_present
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(segment_not_present)
-
-ENTRY(stack_segment)
-	RING0_EC_FRAME
-	pushl $do_stack_segment
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(stack_segment)
-
-ENTRY(alignment_check)
-	RING0_EC_FRAME
-	pushl $do_alignment_check
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(alignment_check)
-
-ENTRY(divide_error)
-	RING0_INT_FRAME
-	pushl $0			# no error code
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_divide_error
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(divide_error)
-
+zeroentry overflow do_overflow
+zeroentry bounds do_bounds
+zeroentry invalid_op do_invalid_op
+zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
+errorentry invalid_TSS do_invalid_TSS
+errorentry segment_not_present do_segment_not_present
+errorentry stack_segment do_stack_segment
+errorentry alignment_check do_alignment_check
+zeroentry divide_error do_divide_error
 #ifdef CONFIG_X86_MCE
-ENTRY(machine_check)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_machine_check
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(machine_check)
+zeroentry machine_check do_machine_check
 #endif
-
-ENTRY(spurious_interrupt_bug)
-	RING0_INT_FRAME
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl $do_spurious_interrupt_bug
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(spurious_interrupt_bug)
+zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
 
 ENTRY(kernel_thread_helper)
 	pushl $0		# fake return address for unwinder
@@ -1174,13 +1083,7 @@ syscall_table_size=(.-sys_call_table)
  */
 	.pushsection .kprobes.text, "ax"
 
-ENTRY(page_fault)
-	RING0_EC_FRAME
-	pushl $do_page_fault
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(page_fault)
+errorentry page_fault do_page_fault
 
 ENTRY(error_code)
 	RING0_EC_FRAME
@@ -1382,13 +1285,7 @@ ENTRY(int3)
 	CFI_ENDPROC
 END(int3)
 
-ENTRY(general_protection)
-	RING0_EC_FRAME
-	pushl $do_general_protection
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
-	CFI_ENDPROC
-END(general_protection)
+errorentry general_protection do_general_protection
 
 /*
  * End of kprobes section
Signature · 1 line
1.6.0.4
#5 lore raw

PATCH 1/1 i386, entry_32: use zeroentry for iret_exc

In reply to Alexander van Heukelum

iret_exc uses the error_code path. Use the zeroentry macro here too. This adds some dwarf2 annotations to the object file, but the executable code is unchanged.

impact: prepare for removing indirect calls in error_code

---
 arch/x86/kernel/entry_32.S |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
arch/x86/kernel/entry_32.S +1 -4
index 29dcd74..407443c 100644
@@ -533,10 +533,7 @@ restore_nocheck_notrace:
 irq_return:
 	INTERRUPT_RETURN
 .section .fixup,"ax"
-ENTRY(iret_exc)
-	pushl $0			# no error code
-	pushl $do_iret_error
-	jmp error_code
+zeroentry iret_exc do_iret_error
 .previous
 .section __ex_table,"a"
 	.align 4
Signature · 1 line
1.6.0.4
#6 lore raw

PATCH 1/1 i386, entry_32: remove indirect calls to exception handlers

In reply to Alexander van Heukelum

This patch separates out register saving and setting up the C environment to a function "save_entry". This largely replaces error_entry, which was called with the address of the exception handler on the stack in the slot that was reserved for %gs. Also separated out is setting the correct stack if the current stack is the espfix stack, but for performance reasons the check itself is now inlined in the handler stub.

No functional change intended.

---
 arch/x86/kernel/entry_32.S |  131 ++++++++++++++++++++++++--------------------
 1 files changed, 72 insertions(+), 59 deletions(-)
arch/x86/kernel/entry_32.S +72 -59
index 407443c..d089897 100644
@@ -313,11 +313,22 @@
 .macro zeroentry sym do_sym errorcode=0
 ENTRY(\sym)
 	RING0_INT_FRAME
-	pushl $\errorcode
+	pushl $-1	/* no syscall to restart */
 	CFI_ADJUST_CFA_OFFSET 4
-	pushl $\do_sym
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
+	sub $11*4, %esp
+	call save_entry
+
+	/* see if on espfix stack */
+	mov %ss, %eax
+	cmp $__ESPFIX_SS, %eax
+	jne 1f
+	call fixup_espfix_stack		/* switch to normal stack */
+1:	TRACE_IRQS_OFF
+
+	movl $\errorcode, %edx		/* made-up error code */
+	movl %esp,%eax			/* pt_regs pointer */
+	call \do_sym
+	jmp ret_from_exception
 	CFI_ENDPROC
 END(\sym)
 .endm
@@ -325,9 +336,21 @@ END(\sym)
 .macro errorentry sym do_sym
 ENTRY(\sym)
 	RING0_EC_FRAME
-	pushl $\do_sym
-	CFI_ADJUST_CFA_OFFSET 4
-	jmp error_code
+	sub $11*4, %esp
+	call save_entry
+
+	/* see if on espfix stack */
+	mov %ss, %eax
+	cmp $__ESPFIX_SS, %eax
+	jne 1f
+	call fixup_espfix_stack		/* switch to normal stack */
+1:	TRACE_IRQS_OFF
+
+	movl PT_ORIG_EAX(%esp), %edx	/* get the error code */
+	movl $-1, PT_ORIG_EAX(%esp)	/* no syscall to restart */
+	movl %esp, %eax			/* pt_regs pointer */
+	call \do_sym
+	jmp ret_from_exception
 	CFI_ENDPROC
 END(\sym)
 .endm
@@ -1082,59 +1105,49 @@ syscall_table_size=(.-sys_call_table)
 
 errorentry page_fault do_page_fault
 
-ENTRY(error_code)
-	RING0_EC_FRAME
-	CFI_ADJUST_CFA_OFFSET 4
-	/* the function address is in %gs's slot on the stack */
-	pushl %fs
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET fs, 0*/
-	pushl %es
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET es, 0*/
-	pushl %ds
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET ds, 0*/
-	pushl %eax
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET eax, 0
-	pushl %ebp
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ebp, 0
-	pushl %edi
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET edi, 0
-	pushl %esi
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET esi, 0
-	pushl %edx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET edx, 0
-	pushl %ecx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ecx, 0
-	pushl %ebx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ebx, 0
+ENTRY(save_entry)
+	RING0_PTREGS_FRAME
+	CFI_ADJUST_CFA_OFFSET 4	/* return address */
+	/* save registers */
+	mov %ebx, PT_EBX+4(%esp)
+	mov %ecx, PT_ECX+4(%esp)
+	mov %edx, PT_EDX+4(%esp)
+	mov %esi, PT_ESI+4(%esp)
+	mov %edi, PT_EDI+4(%esp)
+	mov %ebp, PT_EBP+4(%esp)
+	mov %eax, PT_EAX+4(%esp)
+
+	/* save segment registers */
+	mov %ds, %edx
+	mov %edx, PT_DS+4(%esp)
+	mov %es, %edx
+	mov %edx, PT_ES+4(%esp)
+	mov %fs, %edx
+	mov %edx, PT_FS+4(%esp)
+#ifdef CONFIG_X86_32_LAZY_GS
+	movl $0, PT_GS+4(%esp)
+#else
+	mov %gs, %edx
+	mov %edx, PT_GS+4(%esp)
+#endif
+	/* set C environment */
 	cld
-	movl $(__KERNEL_PERCPU), %ecx
-	movl %ecx, %fs
-	UNWIND_ESPFIX_STACK
-	GS_TO_REG %ecx
-	movl PT_GS(%esp), %edi		# get the function address
-	movl PT_ORIG_EAX(%esp), %edx	# get the error code
-	movl $-1, PT_ORIG_EAX(%esp)	# no syscall to restart
-	REG_TO_PTGS %ecx
-	SET_KERNEL_GS %ecx
-	movl $(__USER_DS), %ecx
-	movl %ecx, %ds
-	movl %ecx, %es
-	TRACE_IRQS_OFF
-	movl %esp,%eax			# pt_regs pointer
-	call *%edi
-	jmp ret_from_exception
-	CFI_ENDPROC
-END(error_code)
+	movl $(__USER_DS), %edx
+	movl %edx, %ds
+	movl %edx, %es
+	movl $(__KERNEL_PERCPU), %edx
+	movl %edx, %fs
+#ifndef CONFIG_X86_32_LAZY_GS
+	movl $(__KERNEL_STACK_CANARY), %edx
+	movl %edx, %gs
+#endif
+	ret
+END(save_entry)
+
+ENTRY(fixup_espfix_stack)
+	FIXUP_ESPFIX_STACK
+	ret
+END(fixup_espfix_stack)
 
 /*
  * Debug traps and NMI can happen at the one SYSENTER instruction
Signature · 1 line
1.6.0.4
#7 lore raw

PATCH 1/1 i386, entry_32: get rid of sysenter_audit and sysexit_audit defines

In reply to Alexander van Heukelum

If CONFIG_AUDITSYSCALL=n, sysenter_audit and sysexit_audit are defines for other labels. Remove this by introducing sysenter_trace and sysexit_trace labels which are always present and move the audit entry code there.

No functional change intended.

---
 arch/x86/kernel/entry_32.S |   88 +++++++++++++++++++++-----------------------
 1 files changed, 42 insertions(+), 46 deletions(-)
arch/x86/kernel/entry_32.S +42 -46
index 314563e..3b63eba 100644
@@ -60,11 +60,6 @@
 #define AUDIT_ARCH_I386		(EM_386|__AUDIT_ARCH_LE)
 #define __AUDIT_ARCH_LE	   0x40000000
 
-#ifndef CONFIG_AUDITSYSCALL
-#define sysenter_audit	syscall_trace_entry
-#define sysexit_audit	syscall_exit_work
-#endif
-
 /*
  * We use macros for low-level operations which need to be overridden
  * for paravirtualization.  The following will never clobber any registers:
@@ -437,7 +432,7 @@ sysenter_past_esp:
 	GET_THREAD_INFO(%ebp)
 
 	testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
-	jnz sysenter_audit
+	jnz sysenter_trace
 sysenter_do_call:
 	cmpl $(nr_syscalls), %eax
 	jae syscall_badsys
@@ -448,7 +443,7 @@ sysenter_do_call:
 	TRACE_IRQS_OFF
 	movl TI_flags(%ebp), %ecx
 	testl $_TIF_ALLWORK_MASK, %ecx
-	jne sysexit_audit
+	jne sysexit_trace
 sysenter_exit:
 /* if something modifies registers it must also disable sysexit */
 	movl PT_EIP(%esp), %edx
@@ -458,45 +453,6 @@ sysenter_exit:
 1:	mov  PT_FS(%esp), %fs
 	PTGS_TO_GS
 	ENABLE_INTERRUPTS_SYSEXIT
-
-#ifdef CONFIG_AUDITSYSCALL
-sysenter_audit:
-	testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
-	jnz syscall_trace_entry
-	addl $4,%esp
-	CFI_ADJUST_CFA_OFFSET -4
-	/* %esi already in 8(%esp)	   6th arg: 4th syscall arg */
-	/* %edx already in 4(%esp)	   5th arg: 3rd syscall arg */
-	/* %ecx already in 0(%esp)	   4th arg: 2nd syscall arg */
-	movl %ebx,%ecx			/* 3rd arg: 1st syscall arg */
-	movl %eax,%edx			/* 2nd arg: syscall number */
-	movl $AUDIT_ARCH_I386,%eax	/* 1st arg: audit arch */
-	call audit_syscall_entry
-	pushl %ebx
-	CFI_ADJUST_CFA_OFFSET 4
-	movl PT_EAX(%esp),%eax		/* reload syscall number */
-	jmp sysenter_do_call
-
-sysexit_audit:
-	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
-	jne syscall_exit_work
-	TRACE_IRQS_ON
-	ENABLE_INTERRUPTS(CLBR_ANY)
-	movl %eax,%edx		/* second arg, syscall return value */
-	cmpl $0,%eax		/* is it < 0? */
-	setl %al		/* 1 if so, 0 if not */
-	movzbl %al,%eax		/* zero-extend that */
-	inc %eax /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
-	call audit_syscall_exit
-	DISABLE_INTERRUPTS(CLBR_ANY)
-	TRACE_IRQS_OFF
-	movl TI_flags(%ebp), %ecx
-	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
-	jne syscall_exit_work
-	movl PT_EAX(%esp),%eax	/* reload syscall return value */
-	jmp sysenter_exit
-#endif
-
 	CFI_ENDPROC
 .pushsection .fixup,"ax"
 2:	movl $0,PT_FS(%esp)
@@ -650,6 +606,25 @@ END(work_pending)
 
 	# perform syscall exit tracing
 	ALIGN
+sysenter_trace:
+#ifdef CONFIG_AUDITSYSCALL
+	testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
+	jnz syscall_trace_entry
+	addl $4,%esp
+	CFI_ADJUST_CFA_OFFSET -4
+	/* %esi already in 8(%esp)	   6th arg: 4th syscall arg */
+	/* %edx already in 4(%esp)	   5th arg: 3rd syscall arg */
+	/* %ecx already in 0(%esp)	   4th arg: 2nd syscall arg */
+	movl %ebx,%ecx			/* 3rd arg: 1st syscall arg */
+	movl %eax,%edx			/* 2nd arg: syscall number */
+	movl $AUDIT_ARCH_I386,%eax	/* 1st arg: audit arch */
+	call audit_syscall_entry
+	pushl %ebx
+	CFI_ADJUST_CFA_OFFSET 4
+	movl PT_EAX(%esp),%eax		/* reload syscall number */
+	jmp sysenter_do_call
+	ALIGN
+#endif
 syscall_trace_entry:
 	movl $-ENOSYS,PT_EAX(%esp)
 	movl %esp, %eax
@@ -662,6 +637,27 @@ END(syscall_trace_entry)
 
 	# perform syscall exit tracing
 	ALIGN
+sysexit_trace:
+#ifdef CONFIG_AUDITSYSCALL
+	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
+	jne syscall_exit_work
+	TRACE_IRQS_ON
+	ENABLE_INTERRUPTS(CLBR_ANY)
+	movl %eax,%edx		/* second arg, syscall return value */
+	cmpl $0,%eax		/* is it < 0? */
+	setl %al		/* 1 if so, 0 if not */
+	movzbl %al,%eax		/* zero-extend that */
+	inc %eax /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
+	call audit_syscall_exit
+	DISABLE_INTERRUPTS(CLBR_ANY)
+	TRACE_IRQS_OFF
+	movl TI_flags(%ebp), %ecx
+	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
+	jne syscall_exit_work
+	movl PT_EAX(%esp),%eax	/* reload syscall return value */
+	jmp sysenter_exit
+	ALIGN
+#endif
 syscall_exit_work:
 	testl $_TIF_WORK_SYSCALL_EXIT, %ecx
 	jz work_pending
Signature · 1 line
1.6.0.4
#8 lore raw

PATCH 1/1 i386, entry_32: cleanup macro usage

In reply to Alexander van Heukelum

Cleanup macro usage

The function fixup_espfix_stack is now called from nmi_espfix_stack instead of having the code duplicated inline.

---
 arch/x86/kernel/entry_32.S |  113 ++++++--------------------------------------
 1 files changed, 15 insertions(+), 98 deletions(-)
arch/x86/kernel/entry_32.S +15 -98
index d089897..1b6bbd5 100644
@@ -103,10 +103,6 @@
 #ifdef CONFIG_X86_32_LAZY_GS
 
  /* unfortunately push/pop can't be no-op */
-.macro PUSH_GS
-	pushl $0
-	CFI_ADJUST_CFA_OFFSET 4
-.endm
 .macro POP_GS pop=0
 	addl $(4 + \pop), %esp
 	CFI_ADJUST_CFA_OFFSET -(4 + \pop)
@@ -119,21 +115,9 @@
 .endm
 .macro PTGS_TO_GS_EX
 .endm
-.macro GS_TO_REG reg
-.endm
-.macro REG_TO_PTGS reg
-.endm
-.macro SET_KERNEL_GS reg
-.endm
 
 #else	/* CONFIG_X86_32_LAZY_GS */
 
-.macro PUSH_GS
-	pushl %gs
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET gs, 0*/
-.endm
-
 .macro POP_GS pop=0
 98:	popl %gs
 	CFI_ADJUST_CFA_OFFSET -4
@@ -166,60 +150,11 @@
 .popsection
 .endm
 
-.macro GS_TO_REG reg
-	movl %gs, \reg
-	/*CFI_REGISTER gs, \reg*/
-.endm
-.macro REG_TO_PTGS reg
-	movl \reg, PT_GS(%esp)
-	/*CFI_REL_OFFSET gs, PT_GS*/
-.endm
-.macro SET_KERNEL_GS reg
-	movl $(__KERNEL_STACK_CANARY), \reg
-	movl \reg, %gs
-.endm
-
 #endif	/* CONFIG_X86_32_LAZY_GS */
 
 .macro SAVE_ALL
-	cld
-	PUSH_GS
-	pushl %fs
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET fs, 0;*/
-	pushl %es
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET es, 0;*/
-	pushl %ds
-	CFI_ADJUST_CFA_OFFSET 4
-	/*CFI_REL_OFFSET ds, 0;*/
-	pushl %eax
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET eax, 0
-	pushl %ebp
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ebp, 0
-	pushl %edi
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET edi, 0
-	pushl %esi
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET esi, 0
-	pushl %edx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET edx, 0
-	pushl %ecx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ecx, 0
-	pushl %ebx
-	CFI_ADJUST_CFA_OFFSET 4
-	CFI_REL_OFFSET ebx, 0
-	movl $(__USER_DS), %edx
-	movl %edx, %ds
-	movl %edx, %es
-	movl $(__KERNEL_PERCPU), %edx
-	movl %edx, %fs
-	SET_KERNEL_GS %edx
+	sub $11*4, %esp
+	call save_entry
 .endm
 
 .macro RESTORE_INT_REGS
@@ -315,8 +250,7 @@ ENTRY(\sym)
 	RING0_INT_FRAME
 	pushl $-1	/* no syscall to restart */
 	CFI_ADJUST_CFA_OFFSET 4
-	sub $11*4, %esp
-	call save_entry
+	SAVE_ALL
 
 	/* see if on espfix stack */
 	mov %ss, %eax
@@ -336,8 +270,7 @@ END(\sym)
 .macro errorentry sym do_sym
 ENTRY(\sym)
 	RING0_EC_FRAME
-	sub $11*4, %esp
-	call save_entry
+	SAVE_ALL
 
 	/* see if on espfix stack */
 	mov %ss, %eax
@@ -748,31 +681,6 @@ PTREGSCALL(rt_sigreturn)
 PTREGSCALL(vm86)
 PTREGSCALL(vm86old)
 
-.macro FIXUP_ESPFIX_STACK
-	/* since we are on a wrong stack, we cant make it a C code :( */
-	PER_CPU(gdt_page, %ebx)
-	GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
-	addl %esp, %eax
-	pushl $__KERNEL_DS
-	CFI_ADJUST_CFA_OFFSET 4
-	pushl %eax
-	CFI_ADJUST_CFA_OFFSET 4
-	lss (%esp), %esp
-	CFI_ADJUST_CFA_OFFSET -8
-.endm
-.macro UNWIND_ESPFIX_STACK
-	movl %ss, %eax
-	/* see if on espfix stack */
-	cmpw $__ESPFIX_SS, %ax
-	jne 27f
-	movl $__KERNEL_DS, %eax
-	movl %eax, %ds
-	movl %eax, %es
-	/* switch to normal stack */
-	FIXUP_ESPFIX_STACK
-27:
-.endm
-
 /*
  * Build the entry stubs and pointer table with some assembler magic.
  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
@@ -1145,7 +1053,16 @@ ENTRY(save_entry)
 END(save_entry)
 
 ENTRY(fixup_espfix_stack)
-	FIXUP_ESPFIX_STACK
+	/* since we are on a wrong stack, we cant make it a C code :( */
+	PER_CPU(gdt_page, %ebx)
+	GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
+	addl %esp, %eax
+	pushl $__KERNEL_DS
+	CFI_ADJUST_CFA_OFFSET 4
+	pushl %eax
+	CFI_ADJUST_CFA_OFFSET 4
+	lss (%esp), %esp
+	CFI_ADJUST_CFA_OFFSET -8
 	ret
 END(fixup_espfix_stack)
 
@@ -1272,7 +1189,7 @@ nmi_espfix_stack:
 	pushl %eax
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
-	FIXUP_ESPFIX_STACK		# %eax == %esp
+	call fixup_espfix_stack		# %eax == %esp
 	xorl %edx,%edx			# zero error code
 	call do_nmi
 	RESTORE_REGS
Signature · 1 line
1.6.0.4