All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@amd64.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH 4/5] x86-64: Handle exception table entries during early boot
Date: Wed, 18 Apr 2012 17:16:49 -0700	[thread overview]
Message-ID: <1334794610-5546-5-git-send-email-hpa@zytor.com> (raw)
In-Reply-To: <1334794610-5546-1-git-send-email-hpa@zytor.com>

From: "H. Peter Anvin" <hpa@zytor.com>

If we get an exception during early boot, walk the exception table to
see if we should intercept it.  The main use case for this is to allow
rdmsr_safe()/wrmsr_safe() during CPU initialization.

Since the exception table is currently sorted at runtime, and fairly
late in startup, this code walks the exception table linearly.  We
obviously don't need to worry about modules, however: none have been
loaded at this point.

In the future it would be better to have the table sorted at compile
time, or even better, turned into a perfect hash.  At that point this
code should be changed out from doing a linear search.
---
 arch/x86/include/asm/segment.h |    2 +-
 arch/x86/kernel/head_64.S      |   72 +++++++++++++++++++++++++++++----------
 2 files changed, 54 insertions(+), 20 deletions(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 58c1e6c..c48a950 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -213,7 +213,7 @@
 
 #ifdef __KERNEL__
 #ifndef __ASSEMBLY__
-extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][10];
+extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
 
 /*
  * Load a segment. Fall back on loading the zero
diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index d1e112c..8b89239 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -19,6 +19,7 @@
 #include <asm/cache.h>
 #include <asm/processor-flags.h>
 #include <asm/percpu.h>
+#include <asm/nops.h>
 
 #ifdef CONFIG_PARAVIRT
 #include <asm/asm-offsets.h>
@@ -26,6 +27,7 @@
 #define GET_CR2_INTO(reg) GET_CR2_INTO_RAX ; movq %rax, reg
 #else
 #define GET_CR2_INTO(reg) movq %cr2, reg
+#define INTERRUPT_RETURN iretq
 #endif
 
 /* we are not able to switch in one step to the final KERNEL ADDRESS SPACE
@@ -271,35 +273,56 @@ bad_address:
 	jmp bad_address
 
 	.section ".init.text","ax"
-#ifdef CONFIG_EARLY_PRINTK
 	.globl early_idt_handlers
 early_idt_handlers:
+	# 56(%rsp) %rflags
+	# 48(%rsp) %cs
+	# 40(%rsp) %rip
+	# 32(%rsp) error code
 	i = 0
 	.rept NUM_EXCEPTION_VECTORS
-	movl $i, %esi
+	.if (EXCEPTION_ERRCODE_MASK >> i) & 1
+	ASM_NOP2
+	.else
+	pushq $0		# Dummy error code, to make stack frame uniform
+	.endif
+	pushq $i		# 24(%rsp) Vector number
 	jmp early_idt_handler
 	i = i + 1
 	.endr
-#endif
 
 ENTRY(early_idt_handler)
-#ifdef CONFIG_EARLY_PRINTK
+	cld
+
 	cmpl $2,early_recursion_flag(%rip)
 	jz  1f
 	incl early_recursion_flag(%rip)
-	GET_CR2_INTO(%r9)
-	xorl %r8d,%r8d		# zero for error code
-	movl %esi,%ecx		# get vector number
-	# Test %ecx against mask of vectors that push error code.
-	cmpl $31,%ecx
-	ja 0f
-	movl $1,%eax
-	salq %cl,%rax
-	testl $EXCEPTION_ERRCODE_MASK,%eax
-	je 0f
-	popq %r8		# get error code
-0:	movq 0(%rsp),%rcx	# get ip
-	movq 8(%rsp),%rdx	# get cs
+
+	pushq %rax		# 16(%rsp)
+	pushq %rcx		#  8(%rsp)
+	pushq %rdx		#  0(%rsp)
+
+	cmpl $__KERNEL_CS,48(%rsp)
+	jne 10f
+
+	movq 40(%rsp),%rax	# %rip
+	leaq __start___ex_table(%rip),%rcx
+	leaq __stop___ex_table(%rip),%rdx
+
+11:
+	cmpq (%rcx),%rax
+	je 20f			# Found an exception entry
+	addq $16,%rcx
+	cmpq %rdx,%rcx
+	jb 11b
+
+10:
+#ifdef CONFIG_EARLY_PRINTK
+	GET_CR2_INTO(%r9)	# can clobber any volatile register if pv
+	movl 32(%rsp),%r8d	# error code
+	movl 24(%rsp),%esi	# vector number
+	movl 48(%rsp),%edx	# %cs
+	movq 40(%rsp),%rcx	# %rip
 	xorl %eax,%eax
 	leaq early_idt_msg(%rip),%rdi
 	call early_printk
@@ -308,17 +331,28 @@ ENTRY(early_idt_handler)
 	call dump_stack
 #ifdef CONFIG_KALLSYMS	
 	leaq early_idt_ripmsg(%rip),%rdi
-	movq 0(%rsp),%rsi	# get rip again
+	movq 40(%rsp),%rsi	# %rip again
 	call __print_symbol
 #endif
 #endif /* EARLY_PRINTK */
 1:	hlt
 	jmp 1b
 
-#ifdef CONFIG_EARLY_PRINTK
+20:	# Exception table entry found
+	movq 8(%rcx),%rax	# exception table target
+	movq %rax,40(%rsp)	# change %rip on stack
+	popq %rdx
+	popq %rcx
+	popq %rax
+	addq $16,%rsp		# drop vector number and error code
+	decl early_recursion_flag(%rip)
+	INTERRUPT_RETURN
+
+	.balign 4
 early_recursion_flag:
 	.long 0
 
+#ifdef CONFIG_EARLY_PRINTK
 early_idt_msg:
 	.asciz "PANIC: early exception %02lx rip %lx:%lx error %lx cr2 %lx\n"
 early_idt_ripmsg:
-- 
1.7.6.5


  parent reply	other threads:[~2012-04-19  0:17 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-19  0:16 [PATCH 0/5] RFC: x86: Early exception table support H. Peter Anvin
2012-04-19  0:16 ` [PATCH 1/5] x86, nop: Make the ASM_NOP* macros work from assembly H. Peter Anvin
2012-04-19  9:29   ` Borislav Petkov
2012-04-20  0:24   ` [tip:x86/extable] " tip-bot for H. Peter Anvin
2012-04-19  0:16 ` [PATCH 2/5] x86: Add symbolic constant for exceptions with error code H. Peter Anvin
2012-04-19  9:30   ` Borislav Petkov
2012-04-20  0:25   ` [tip:x86/extable] " tip-bot for H. Peter Anvin
2012-04-19  0:16 ` [PATCH 3/5] x86, paravirt: Replace GET_CR2_INTO_RCX with GET_CR2_INTO_RAX H. Peter Anvin
2012-04-20  0:26   ` [tip:x86/extable] " tip-bot for H. Peter Anvin
2012-04-19  0:16 ` H. Peter Anvin [this message]
2012-04-19 13:02   ` [PATCH 4/5] x86-64: Handle exception table entries during early boot Borislav Petkov
2012-04-19 16:59     ` H. Peter Anvin
2012-04-19 17:16       ` Borislav Petkov
2012-04-20  0:29         ` [tip:x86/extable] x86, doc: Revert "x86: Document rdmsr_safe restrictions" tip-bot for H. Peter Anvin
2012-04-20  0:28   ` [tip:x86/extable] x86-64: Handle exception table entries during early boot tip-bot for H. Peter Anvin
2012-04-19  0:16 ` [PATCH 5/5] x86-32: " H. Peter Anvin
2012-04-20  0:28   ` [tip:x86/extable] " tip-bot for H. Peter Anvin
2012-04-19  9:22 ` [PATCH 0/5] RFC: x86: Early exception table support Borislav Petkov
2012-04-19  9:24   ` [PATCH 1/3] x86, extable: Cleanup fixup_exception Borislav Petkov
2012-04-19  9:25   ` [PATCH 2/3] x86, extable: Carve out the main extable searching routine Borislav Petkov
2012-04-19  9:26   ` [PATCH 3/3] x86, extable: Handle early exceptions Borislav Petkov
2012-04-19 17:02     ` H. Peter Anvin
2012-04-19 17:27       ` Linus Torvalds
2012-04-19 17:38         ` Borislav Petkov
2012-04-19 17:59           ` H. Peter Anvin
2012-04-19 18:25             ` Linus Torvalds
2012-04-19 18:55               ` H. Peter Anvin
2012-04-19 20:17                 ` David Daney
2012-04-19 20:20                   ` H. Peter Anvin
2012-04-19 20:26                     ` H. Peter Anvin
2012-04-19 20:40                       ` David Daney
2012-04-19 21:47                       ` Linus Torvalds
2012-04-19 22:16                   ` H. Peter Anvin
2012-04-19 22:47                     ` Tony Luck
2012-04-19 22:58                       ` Linus Torvalds
2012-04-19 23:10                         ` H. Peter Anvin
2012-04-19 23:26                           ` Tony Luck
2012-04-19 23:35                             ` H. Peter Anvin
2012-04-20  8:26                             ` Andreas Schwab
2012-04-19 18:11           ` Linus Torvalds
2012-04-20  8:46             ` Borislav Petkov
2012-04-19 17:54       ` H. Peter Anvin
2012-04-20  0:27 ` [tip:x86/extable] x86, extable: Add early_fixup_exception() tip-bot for H. Peter Anvin

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=1334794610-5546-5-git-send-email-hpa@zytor.com \
    --to=hpa@zytor.com \
    --cc=bp@amd64.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.