linux-kernel.vger.kernel.org archive mirror
 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 5/5] x86-32: Handle exception table entries during early boot
Date: Wed, 18 Apr 2012 17:16:50 -0700	[thread overview]
Message-ID: <1334794610-5546-6-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.

This patch changes the early IDT setup to look a lot more like x86-64:
we now install handlers for all 32 exception vectors.  The output of
the early exception handler has changed somewhat as it directly
reflects the stack frame of the exception handler, and the stack frame
has been somewhat restructured.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
---
 arch/x86/kernel/head_32.S |  142 +++++++++++++++++++++++++++++----------------
 1 files changed, 91 insertions(+), 51 deletions(-)

diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index ce0be7c..3a3cd0d 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -21,6 +21,7 @@
 #include <asm/msr-index.h>
 #include <asm/cpufeature.h>
 #include <asm/percpu.h>
+#include <asm/nops.h>
 
 /* Physical address */
 #define pa(X) ((X) - __PAGE_OFFSET)
@@ -508,68 +509,94 @@ check_x87:
  *  Warning: %esi is live across this function.
  */
 setup_idt:
-	lea ignore_int,%edx
+	movl $idt_table,%edi
+	movl $early_idt_handlers,%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 $8,%edi
+	loop 1b
+
+	movl $256 - NUM_EXCEPTION_VECTORS,%ecx
+	movl $ignore_int,%edx
 	movl $(__KERNEL_CS << 16),%eax
 	movw %dx,%ax		/* selector = 0x0010 = cs */
 	movw $0x8E00,%dx	/* interrupt gate - dpl=0, present */
-
-	lea idt_table,%edi
-	mov $256,%ecx
-rp_sidt:
+2:
 	movl %eax,(%edi)
 	movl %edx,4(%edi)
 	addl $8,%edi
-	dec %ecx
-	jne rp_sidt
-
-.macro	set_early_handler handler,trapno
-	lea \handler,%edx
-	movl $(__KERNEL_CS << 16),%eax
-	movw %dx,%ax
-	movw $0x8E00,%dx	/* interrupt gate - dpl=0, present */
-	lea idt_table,%edi
-	movl %eax,8*\trapno(%edi)
-	movl %edx,8*\trapno+4(%edi)
-.endm
-
-	set_early_handler handler=early_divide_err,trapno=0
-	set_early_handler handler=early_illegal_opcode,trapno=6
-	set_early_handler handler=early_protection_fault,trapno=13
-	set_early_handler handler=early_page_fault,trapno=14
-
+	loop 2b
+	
 	ret
 
-early_divide_err:
-	xor %edx,%edx
-	pushl $0	/* fake errcode */
-	jmp early_fault
+	.globl early_idt_handlers
+early_idt_handlers:
+	# 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
+	pushl $0		# Dummy error code, to make stack frame uniform
+	.endif
+	pushl $i		# 20(%esp) Vector number
+	jmp early_idt_handler
+	i = i + 1
+	.endr
+
+	/* This is global to keep gas from relaxing the jumps */
+	.globl	early_idt_handler
+early_idt_handler:
+	cld
+	cmpl $2,%ss:early_recursion_flag
+	je hlt_loop
+	incl %ss:early_recursion_flag
 
-early_illegal_opcode:
-	movl $6,%edx
-	pushl $0	/* fake errcode */
-	jmp early_fault
+	push %eax		# 16(%esp)
+	push %ecx		# 12(%esp)
+	push %edx		#  8(%esp)
+	push %ds		#  4(%esp)
+	push %es		#  0(%esp)
+	movl $(__KERNEL_DS),%eax
+	movl %eax,%ds
+	movl %eax,%es
 
-early_protection_fault:
-	movl $13,%edx
-	jmp early_fault
+	cmpl $(__KERNEL_CS),32(%esp)
+	jne 10f
 
-early_page_fault:
-	movl $14,%edx
-	jmp early_fault
+	movl 28(%esp),%eax	# %eip
+	movl $__start___ex_table,%ecx
+	movl $__stop___ex_table,%edx
+11:
+	cmpl (%ecx),%eax
+	je ex_entry		/* found an exception entry */
+	addl $8,%ecx
+	cmpl %edx,%ecx
+	jb 11b
 
-early_fault:
-	cld
+10:
 #ifdef CONFIG_PRINTK
-	pusha
-	movl $(__KERNEL_DS),%eax
-	movl %eax,%ds
-	movl %eax,%es
-	cmpl $2,early_recursion_flag
-	je hlt_loop
-	incl early_recursion_flag
+	xorl %eax,%eax
+	movw %ax,2(%esp)	/* clean up the segment values on some cpus */
+	movw %ax,6(%esp)
+	movw %ax,34(%esp)
+	leal  40(%esp),%eax
+	pushl %eax		/* %esp before the exception */
+	pushl %ebx
+	pushl %ebp
+	pushl %esi
+	pushl %edi
 	movl %cr2,%eax
 	pushl %eax
-	pushl %edx		/* trapno */
+	pushl (20+6*4)(%esp)	/* trapno */
 	pushl $fault_msg
 	call printk
 #endif
@@ -578,6 +605,18 @@ hlt_loop:
 	hlt
 	jmp hlt_loop
 
+ex_entry:
+	movl 4(%ecx),%eax
+	movl %eax,28(%esp)	/* change %eip on the stack */
+	pop %es
+	pop %ds
+	pop %edx
+	pop %ecx
+	pop %eax
+	addl $8,%esp		/* drop vector number and error code */
+	decl %ss:early_recursion_flag
+	iret
+
 /* This is the default interrupt "handler" :-) */
 	ALIGN
 ignore_int:
@@ -681,11 +720,12 @@ int_msg:
 fault_msg:
 /* fault info: */
 	.ascii "BUG: Int %d: CR2 %p\n"
-/* pusha regs: */
-	.ascii "     EDI %p  ESI %p  EBP %p  ESP %p\n"
-	.ascii "     EBX %p  EDX %p  ECX %p  EAX %p\n"
+/* regs pushed in early_idt_handler: */
+	.ascii "     EDI %p  ESI %p  EBP %p  EBX %p\n"
+	.ascii "     ESP %p   ES %p   DS %p\n"
+	.ascii "     EDX %p  ECX %p  EAX %p\n"
 /* fault frame: */
-	.ascii "     err %p  EIP %p   CS %p  flg %p\n"
+	.ascii "     vec %p  err %p  EIP %p   CS %p  flg %p\n"
 	.ascii "Stack: %p %p %p %p %p %p %p %p\n"
 	.ascii "       %p %p %p %p %p %p %p %p\n"
 	.asciz "       %p %p %p %p %p %p %p %p\n"
-- 
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 ` [PATCH 4/5] x86-64: Handle exception table entries during early boot H. Peter Anvin
2012-04-19 13:02   ` 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 ` H. Peter Anvin [this message]
2012-04-20  0:28   ` [tip:x86/extable] x86-32: " 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-6-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).