public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Andy Lutomirski <luto@kernel.org>,
	"H. Peter Anvin" <hpa@linux.intel.com>,
	Binutils <binutils@sourceware.org>,
	Borislav Petkov <bp@alien8.de>, "H.J. Lu" <hjl.tools@gmail.com>,
	Jan Beulich <JBeulich@suse.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>, Jiri Slaby <jslaby@suse.cz>
Subject: [PATCH 3.10 51/54] x86/asm/irq: Stop relying on magic JMP behavior for early_idt_handlers
Date: Tue, 23 Feb 2016 19:34:08 -0800	[thread overview]
Message-ID: <20160224033354.374825853@linuxfoundation.org> (raw)
In-Reply-To: <20160224033352.613205610@linuxfoundation.org>

3.10-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Andy Lutomirski <luto@kernel.org>

commit 425be5679fd292a3c36cb1fe423086708a99f11a upstream.

The early_idt_handlers asm code generates an array of entry
points spaced nine bytes apart.  It's not really clear from that
code or from the places that reference it what's going on, and
the code only works in the first place because GAS never
generates two-byte JMP instructions when jumping to global
labels.

Clean up the code to generate the correct array stride (member size)
explicitly. This should be considerably more robust against
screw-ups, as GAS will warn if a .fill directive has a negative
count.  Using '. =' to advance would have been even more robust
(it would generate an actual error if it tried to move
backwards), but it would pad with nulls, confusing anyone who
tries to disassemble the code.  The new scheme should be much
clearer to future readers.

While we're at it, improve the comments and rename the array and
common code.

Binutils may start relaxing jumps to non-weak labels.  If so,
this change will fix our build, and we may need to backport this
change.

Before, on x86_64:

  0000000000000000 <early_idt_handlers>:
     0:   6a 00                   pushq  $0x0
     2:   6a 00                   pushq  $0x0
     4:   e9 00 00 00 00          jmpq   9 <early_idt_handlers+0x9>
                          5: R_X86_64_PC32        early_idt_handler-0x4
  ...
    48:   66 90                   xchg   %ax,%ax
    4a:   6a 08                   pushq  $0x8
    4c:   e9 00 00 00 00          jmpq   51 <early_idt_handlers+0x51>
                          4d: R_X86_64_PC32       early_idt_handler-0x4
  ...
   117:   6a 00                   pushq  $0x0
   119:   6a 1f                   pushq  $0x1f
   11b:   e9 00 00 00 00          jmpq   120 <early_idt_handler>
                          11c: R_X86_64_PC32      early_idt_handler-0x4

After:

  0000000000000000 <early_idt_handler_array>:
     0:   6a 00                   pushq  $0x0
     2:   6a 00                   pushq  $0x0
     4:   e9 14 01 00 00          jmpq   11d <early_idt_handler_common>
  ...
    48:   6a 08                   pushq  $0x8
    4a:   e9 d1 00 00 00          jmpq   120 <early_idt_handler_common>
    4f:   cc                      int3
    50:   cc                      int3
  ...
   117:   6a 00                   pushq  $0x0
   119:   6a 1f                   pushq  $0x1f
   11b:   eb 03                   jmp    120 <early_idt_handler_common>
   11d:   cc                      int3
   11e:   cc                      int3
   11f:   cc                      int3

Signed-off-by: Andy Lutomirski <luto@kernel.org>
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Binutils <binutils@sourceware.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: H.J. Lu <hjl.tools@gmail.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/ac027962af343b0c599cbfcf50b945ad2ef3d7a8.1432336324.git.luto@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/x86/include/asm/segment.h |   13 ++++++++++++-
 arch/x86/kernel/head64.c       |    2 +-
 arch/x86/kernel/head_32.S      |   33 ++++++++++++++++++---------------
 arch/x86/kernel/head_64.S      |   20 +++++++++++---------
 4 files changed, 42 insertions(+), 26 deletions(-)

--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -212,8 +212,19 @@
 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
 
 #ifdef __KERNEL__
+
+/*
+ * early_idt_handler_array is an array of entry points referenced in the
+ * early IDT.  For simplicity, it's a real array with one entry point
+ * every nine bytes.  That leaves room for an optional 'push $0' if the
+ * vector has no error code (two bytes), a 'push $vector_number' (two
+ * bytes), and a jump to the common entry code (up to five bytes).
+ */
+#define EARLY_IDT_HANDLER_SIZE 9
+
 #ifndef __ASSEMBLY__
-extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
+
+extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
 
 /*
  * Load a segment. Fall back on loading the zero
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -162,7 +162,7 @@ void __init x86_64_start_kernel(char * r
 	clear_bss();
 
 	for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
-		set_intr_gate(i, &early_idt_handlers[i]);
+		set_intr_gate(i, &early_idt_handler_array[i]);
 	load_idt((const struct desc_ptr *)&idt_descr);
 
 	copy_bootdata(__va(real_mode_data));
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -499,21 +499,22 @@ check_x87:
 __INIT
 setup_once:
 	/*
-	 * Set up a idt with 256 entries pointing to ignore_int,
-	 * interrupt gates. It doesn't actually load idt - that needs
-	 * to be done on each CPU. Interrupts are enabled elsewhere,
-	 * when we can be relatively sure everything is ok.
+	 * Set up a idt with 256 interrupt gates that push zero if there
+	 * is no error code and then jump to early_idt_handler_common.
+	 * It doesn't actually load the idt - that needs to be done on
+	 * each CPU. Interrupts are enabled elsewhere, when we can be
+	 * relatively sure everything is ok.
 	 */
 
 	movl $idt_table,%edi
-	movl $early_idt_handlers,%eax
+	movl $early_idt_handler_array,%eax
 	movl $NUM_EXCEPTION_VECTORS,%ecx
 1:
 	movl %eax,(%edi)
 	movl %eax,4(%edi)
 	/* interrupt gate, dpl=0, present */
 	movl $(0x8E000000 + __KERNEL_CS),2(%edi)
-	addl $9,%eax
+	addl $EARLY_IDT_HANDLER_SIZE,%eax
 	addl $8,%edi
 	loop 1b
 
@@ -545,26 +546,28 @@ setup_once:
 	andl $0,setup_once_ref	/* Once is enough, thanks */
 	ret
 
-ENTRY(early_idt_handlers)
+ENTRY(early_idt_handler_array)
 	# 36(%esp) %eflags
 	# 32(%esp) %cs
 	# 28(%esp) %eip
 	# 24(%rsp) error code
 	i = 0
 	.rept NUM_EXCEPTION_VECTORS
-	.if (EXCEPTION_ERRCODE_MASK >> i) & 1
-	ASM_NOP2
-	.else
+	.ifeq (EXCEPTION_ERRCODE_MASK >> i) & 1
 	pushl $0		# Dummy error code, to make stack frame uniform
 	.endif
 	pushl $i		# 20(%esp) Vector number
-	jmp early_idt_handler
+	jmp early_idt_handler_common
 	i = i + 1
+	.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
 	.endr
-ENDPROC(early_idt_handlers)
+ENDPROC(early_idt_handler_array)
 	
-	/* This is global to keep gas from relaxing the jumps */
-ENTRY(early_idt_handler)
+early_idt_handler_common:
+	/*
+	 * The stack is the hardware frame, an error code or zero, and the
+	 * vector number.
+	 */
 	cld
 
 	cmpl $2,(%esp)		# X86_TRAP_NMI
@@ -624,7 +627,7 @@ ex_entry:
 is_nmi:
 	addl $8,%esp		/* drop vector number and error code */
 	iret
-ENDPROC(early_idt_handler)
+ENDPROC(early_idt_handler_common)
 
 /* This is the default interrupt "handler" :-) */
 	ALIGN
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -329,26 +329,28 @@ bad_address:
 	jmp bad_address
 
 	__INIT
-	.globl early_idt_handlers
-early_idt_handlers:
+ENTRY(early_idt_handler_array)
 	# 104(%rsp) %rflags
 	#  96(%rsp) %cs
 	#  88(%rsp) %rip
 	#  80(%rsp) error code
 	i = 0
 	.rept NUM_EXCEPTION_VECTORS
-	.if (EXCEPTION_ERRCODE_MASK >> i) & 1
-	ASM_NOP2
-	.else
+	.ifeq (EXCEPTION_ERRCODE_MASK >> i) & 1
 	pushq $0		# Dummy error code, to make stack frame uniform
 	.endif
 	pushq $i		# 72(%rsp) Vector number
-	jmp early_idt_handler
+	jmp early_idt_handler_common
 	i = i + 1
+	.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
 	.endr
+ENDPROC(early_idt_handler_array)
 
-/* This is global to keep gas from relaxing the jumps */
-ENTRY(early_idt_handler)
+early_idt_handler_common:
+	/*
+	 * The stack is the hardware frame, an error code or zero, and the
+	 * vector number.
+	 */
 	cld
 
 	cmpl $2,(%rsp)		# X86_TRAP_NMI
@@ -420,7 +422,7 @@ ENTRY(early_idt_handler)
 is_nmi:
 	addq $16,%rsp		# drop vector number and error code
 	INTERRUPT_RETURN
-ENDPROC(early_idt_handler)
+ENDPROC(early_idt_handler_common)
 
 	__INITDATA
 

  parent reply	other threads:[~2016-02-24  3:55 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-24  3:33 [PATCH 3.10 00/54] 3.10.98-stable review Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 01/54] ALSA: seq: Fix double port list deletion Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 02/54] wan/x25: Fix use-after-free in x25_asy_open_tty() Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 03/54] staging/speakup: Use tty_ldisc_ref() for paste kworker Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 04/54] pty: fix possible use after free of tty->driver_data Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 05/54] pty: make sure super_block is still valid in final /dev/tty close Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 06/54] AIO: properly check iovec sizes Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 07/54] ext4: fix potential integer overflow Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 08/54] Btrfs: fix hang on extent buffer lock caused by the inode_paths ioctl Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 10/54] ptrace: use fsuid, fsgid, effective creds for fs access checks Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 11/54] tools lib traceevent: Fix output of %llu for 64 bit values read on 32 bit machines Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 12/54] tracing: Fix freak link error caused by branch tracer Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 13/54] klist: fix starting point removed bug in klist iterators Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 14/54] scsi: restart list search after unlock in scsi_remove_target Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 15/54] scsi_sysfs: Fix queue_ramp_up_period return code Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 16/54] iscsi-target: Fix rx_login_comp hang after login failure Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 17/54] Fix a memory leak in scsi_host_dev_release() Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 18/54] SCSI: Fix NULL pointer dereference in runtime PM Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 19/54] iscsi-target: Fix potential dead-lock during node acl delete Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 20/54] SCSI: fix crashes in sd and sr runtime PM Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 21/54] drivers/scsi/sg.c: mark VMA as VM_IO to prevent migration Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 22/54] scsi_dh_rdac: always retry MODE SELECT on command lock violation Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 23/54] scsi: fix soft lockup in scsi_remove_target() on module removal Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 24/54] iio:ad7793: Fix ad7785 product ID Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 25/54] iio: lpc32xx_adc: fix warnings caused by enabling unprepared clock Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 26/54] iio:ad5064: Make sure ad5064_i2c_write() returns 0 on success Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 27/54] iio: adis_buffer: Fix out-of-bounds memory access Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 28/54] iio: dac: mcp4725: set iio name property in sysfs Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 29/54] cifs: fix erroneous return value Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 30/54] nfs: Fix race in __update_open_stateid() Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 31/54] udf: limit the maximum number of indirect extents in a row Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 32/54] udf: Prevent buffer overrun with multi-byte characters Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 33/54] udf: Check output buffer length when converting name to CS0 Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 34/54] ARM: 8519/1: ICST: try other dividends than 1 Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 35/54] ARM: 8517/1: ICST: avoid arithmetic overflow in icst_hz() Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 36/54] fuse: break infinite loop in fuse_fill_write_pages() Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 37/54] mm: soft-offline: check return value in second __get_any_page() call Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 38/54] Input: elantech - add Fujitsu Lifebook U745 to force crc_enabled Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 39/54] Input: elantech - mark protocols v2 and v3 as semi-mt Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 41/54] iommu/vt-d: Fix 64-bit accesses to 32-bit DMAR_GSTS_REG Greg Kroah-Hartman
2016-02-24  3:33 ` [PATCH 3.10 42/54] mm/memory_hotplug.c: check for missing sections in test_pages_in_a_zone() Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 43/54] xhci: Fix list corruption in urb dequeue at host removal Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 44/54] m32r: fix m32104ut_defconfig build fail Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 45/54] dma-debug: switch check from _text to _stext Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 46/54] scripts/bloat-o-meter: fix python3 syntax error Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 47/54] memcg: only free spare array when readers are done Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 48/54] radix-tree: fix race in gang lookup Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 49/54] radix-tree: fix oops after radix_tree_iter_retry Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 50/54] intel_scu_ipcutil: underflow in scu_reg_access() Greg Kroah-Hartman
2016-02-24  3:34 ` Greg Kroah-Hartman [this message]
2016-02-24  3:34 ` [PATCH 3.10 52/54] futex: Drop refcount if requeue_pi() acquired the rtmutex Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 53/54] ip6mr: call del_timer_sync() in ip6mr_free_table() Greg Kroah-Hartman
2016-02-24  3:34 ` [PATCH 3.10 54/54] module: wrapper for symbol name Greg Kroah-Hartman
2016-02-24 16:31 ` [PATCH 3.10 00/54] 3.10.98-stable review Willy Tarreau
2016-02-25 18:37   ` Greg Kroah-Hartman
2016-02-24 18:29 ` Shuah Khan
2016-02-25  4:57 ` Guenter Roeck
2016-02-25 16:33   ` Greg Kroah-Hartman
2016-02-25 17:01     ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160224033354.374825853@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=JBeulich@suse.com \
    --cc=binutils@sourceware.org \
    --cc=bp@alien8.de \
    --cc=hjl.tools@gmail.com \
    --cc=hpa@linux.intel.com \
    --cc=jslaby@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox