Skip to contentSkip to conversation

x86/asm/entry: clean up usage of TEST insns

PATCH New Denys Vlasenko 1 participant 1 message Mar 30, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/asm/entry: clean up usage of TEST insns

After TEST insn, JE actually performs "jump if zero", let's use JZ mnemonic instead. No code changes, but less confusion.

By the nature of TEST operation, it is often possible to test a narrower part of the operand:

    "testl $3, mem"  -> "testb $3, mem",
    "testq $3, %rcx" -> "testb $3, %cl"

This results in shorter insns, because TEST insn has no sign-entending byte-immediate forms unlike other ALU ops.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>

---

Code changes in assembly are:

    -f7 84 24 88 00 00 00 03 00 00 00   testl  $0x3,0x88(%rsp)
    +f6 84 24 88 00 00 00 03            testb  $0x3,0x88(%rsp)
    -f7 44 24 68 03 00 00 00            testl  $0x3,0x68(%rsp)
    +f6 44 24 68 03                     testb  $0x3,0x68(%rsp)
    -49 f7 c3 00 00 01 00               test   $0x10000,%r11
    +41 f7 c3 00 00 01 00               test   $0x10000,%r11d
    -f7 84 24 90 00 00 00 03 00 00 00   testl  $0x3,0x90(%rsp)
    +f6 84 24 90 00 00 00 03            testb  $0x3,0x90(%rsp)
 arch/x86/kernel/entry_32.S | 10 +++++-----
 arch/x86/kernel/entry_64.S | 20 ++++++++++----------
 2 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/kernel/entry_32.S +5 -5
index 438d193..57c204e 100644
@@ -436,7 +436,7 @@ sysenter_after_call:
 	TRACE_IRQS_OFF
 	movl TI_flags(%ebp), %ecx
 	testl $_TIF_ALLWORK_MASK, %ecx
-	jne sysexit_audit
+	jnz sysexit_audit
 sysenter_exit:
 /* if something modifies registers it must also disable sysexit */
 	movl PT_EIP(%esp), %edx
@@ -464,7 +464,7 @@ sysenter_audit:
 
 sysexit_audit:
 	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
-	jne syscall_exit_work
+	jnz syscall_exit_work
 	TRACE_IRQS_ON
 	ENABLE_INTERRUPTS(CLBR_ANY)
 	movl %eax,%edx		/* second arg, syscall return value */
@@ -476,7 +476,7 @@ sysexit_audit:
 	TRACE_IRQS_OFF
 	movl TI_flags(%ebp), %ecx
 	testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
-	jne syscall_exit_work
+	jnz syscall_exit_work
 	movl PT_EAX(%esp),%eax	/* reload syscall return value */
 	jmp sysenter_exit
 #endif
@@ -514,7 +514,7 @@ syscall_exit:
 	TRACE_IRQS_OFF
 	movl TI_flags(%ebp), %ecx
 	testl $_TIF_ALLWORK_MASK, %ecx	# current->work
-	jne syscall_exit_work
+	jnz syscall_exit_work
 
 restore_all:
 	TRACE_IRQS_IRET
@@ -616,7 +616,7 @@ 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
+	jnz work_notifysig_v86		# returning to kernel-space or
 					# vm86-space
 1:
 #else
arch/x86/kernel/entry_64.S +10 -10
index ca80adc..b79a935 100644
@@ -512,7 +512,7 @@ ENTRY(ret_from_fork)
 
 	RESTORE_EXTRA_REGS
 
-	testl $3,CS(%rsp)			# from kernel_thread?
+	testb $3,CS(%rsp)			# from kernel_thread?
 	jz   1f
 
 	/*
@@ -597,8 +597,8 @@ END(interrupt)
 
 	leaq -RBP(%rsp),%rdi	/* arg1 for \func (pointer to pt_regs) */
 
-	testl $3, CS-RBP(%rsp)
-	je 1f
+	testb $3, CS-RBP(%rsp)
+	jz 1f
 	SWAPGS
 1:
 	/*
@@ -654,8 +654,8 @@ ret_from_intr:
 
 exit_intr:
 	GET_THREAD_INFO(%rcx)
-	testl $3,CS(%rsp)
-	je retint_kernel
+	testb $3,CS(%rsp)
+	jz retint_kernel
 
 	/* Interrupt came from user space */
 	/*
@@ -711,7 +711,7 @@ retint_swapgs:		/* return to user-space */
 	cmpq %r11,EFLAGS(%rsp)		/* R11 == RFLAGS */
 	jne opportunistic_sysret_failed
 
-	testq $X86_EFLAGS_RF,%r11	/* sysret can't restore RF */
+	testl $X86_EFLAGS_RF,%r11d	/* sysret can't restore RF */
 	jnz opportunistic_sysret_failed
 
 	/* nothing to check for RSP */
@@ -951,7 +951,7 @@ ENTRY(\sym)
 	.if \paranoid
 	.if \paranoid == 1
 	CFI_REMEMBER_STATE
-	testl $3, CS(%rsp)		/* If coming from userspace, treat */
+	testb $3, CS(%rsp)		/* If coming from userspace, treat */
 	jnz 1f				/* as non-paranoid. */
 	.endif
 	call paranoid_entry
@@ -1294,8 +1294,8 @@ ENTRY(error_entry)
 	SAVE_C_REGS 8
 	SAVE_EXTRA_REGS 8
 	xorl %ebx,%ebx
-	testl $3,CS+8(%rsp)
-	je error_kernelspace
+	testb $3,CS+8(%rsp)
+	jz error_kernelspace
 error_swapgs:
 	SWAPGS
 error_sti:
@@ -1346,7 +1346,7 @@ ENTRY(error_exit)
 	TRACE_IRQS_OFF
 	GET_THREAD_INFO(%rcx)
 	testl %eax,%eax
-	jne retint_kernel
+	jnz retint_kernel
 	LOCKDEP_SYS_EXIT_IRQ
 	movl TI_flags(%rcx),%edx
 	movl $_TIF_WORK_MASK,%edi
Signature · 1 line
1.8.1.4