Skip to contentSkip to conversation

i386: early pagefault handler

PATCH In review Chuck Ebbert 7 participants 11 messages Jul 5, 2006 – Jul 7, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386: early pagefault handler

Page faults during kernel initialization can be hard to diagnose.

Add a handler that prints the fault address, EIP and top of stack when an early page fault happens.

 arch/i386/kernel/head.S |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+)
arch/i386/kernel/head.S +37 -0
@@ -378,8 +378,41 @@ rp_sidt:
 	addl $8,%edi
 	dec %ecx
 	jne rp_sidt
+
+	lea page_fault,%edi	/* early page fault handler */
+	movw %di,%ax
+	andl $0x0000ffff,%edx
+	andl $0xffff0000,%edi
+	orl %edi,%edx
+	lea idt_table,%edi
+	movl %eax,8*14(%edi)
+	movl %edx,8*14+4(%edi)
+
 	ret
 
+/* This is the early page fault handler */
+	ALIGN
+page_fault:
+	cld
+#ifdef CONFIG_PRINTK
+	movl $(__KERNEL_DS),%eax
+	movl %eax,%ds
+	movl %eax,%es
+	movl %cr2,%eax
+	pushl %eax
+	pushl $pf_msg
+#ifdef CONFIG_EARLY_PRINTK
+	call early_printk
+#else
+	call printk
+#endif
+#endif
+hlt_loop:
+	hlt
+1:
+	rep ; nop
+	jmp 1b
+
 /* This is the default interrupt "handler" :-) */
 	ALIGN
 ignore_int:
@@ -441,6 +474,10 @@ ready:	.byte 0
 int_msg:
 	.asciz "Unknown interrupt or fault at EIP %p %p %p\n"
 
+pf_msg:
+	.ascii "Pg flt: CR2 %p  err %p  EIP %p  CS %p  flags %p\n"
+	.asciz "   Stk: %p %p %p %p %p %p %p %p\n"
+
 /*
  * The IDT and GDT 'descriptors' are a strange 48-bit object
  * only used by the lidt and lgdt instructions. They are not
Signature · 2 lines
Chuck
 "You can't read a newspaper if you can't read."  --George W. Bush
#2 lore raw
In reply to Chuck Ebbert
Quoted 4 lines · Chuck Ebbert

Page faults during kernel initialization can be hard to diagnose.

Add a handler that prints the fault address, EIP and top of stack when an early page fault happens.

You should do it for all the exceptions then
(except perhaps NMI). Isn't much more work - see the x86-64 code.

arch/i386/kernel/head.S rp_sidt: · line 411

Quoted 2 lines
arch/i386/kernel/head.S +2 -0
+hlt_loop:
+	hlt

There are still supported i386 CPUs that don't support HLT and would recursively fault here.

arch/i386/kernel/head.S rp_sidt: · line 414

Quoted 2 lines
arch/i386/kernel/head.S +2 -0
+	rep ; nop
+	jmp 1b

Looks a bit weird to not jump to hlt back again but ok. The HLT is unlikely to come back anyways because interrupts are off.

-Andi

#3 lore raw
Quoted 7 lines · Andi Kleen

arch/i386/kernel/head.S rp_sidt: · line 411

Quoted 3 lines
arch/i386/kernel/head.S +2 -0
+hlt_loop:
+	hlt

There are still supported i386 CPUs that don't support HLT and would recursively fault here.

The HLT has been supported since 8086. However, it was broken in some early 486s (not 386s); that's what the test in the kernel is for.

I don't remember what the failure mode was, though; didn't think it was recursive faulting.

-hpa

#4 lore raw
Quoted 3 lines · H. Peter Anvin

I don't remember what the failure mode was, though; didn't think it was recursive faulting.

I think we should probably remove the test. The failure mode was simply that a machine with the "halt" idle loop simply didn't work, and would lock up. The most likely reason for that is probably just a bad CPU power VRM, and the potential high current fluctuations, not so much any CPU bug itself.

Anybody with that old a CPU will have learnt to to say "no-hlt" or whatever the kernel command line is, and we could probably retire the silly old hlt check (which I'm not even sure really ever worked).

Linus

#5 lore raw

Ar Mer, 2006-07-05 am 09:54 -0700, ysgrifennodd Linus Torvalds:

Quoted 3 lines

Anybody with that old a CPU will have learnt to to say "no-hlt" or whatever the kernel command line is, and we could probably retire the silly old hlt check (which I'm not even sure really ever worked).

The one specific case I know precisely details of was the Cyrix 5510. A hlt by the CPU on that chipset during an IDE DMA transfer hangs the system forever.

Its some years since I've even seen a 5510 and that check could be automated anyway

Alan

#6 lore raw
Quoted 9 lines · Alan Cox

Ar Mer, 2006-07-05 am 09:54 -0700, ysgrifennodd Linus Torvalds:

Quoted 1 line

Anybody with that old a CPU will have learnt to to say "no-hlt" or whatever the kernel command line is, and we could probably retire the silly old hlt check (which I'm not even sure really ever worked).

The one specific case I know precisely details of was the Cyrix 5510. A hlt by the CPU on that chipset during an IDE DMA transfer hangs the system forever.

Its some years since I've even seen a 5510 and that check could be automated anyway

I think HLT for a die loop should be safe :)

-hpa

#7 lore raw
In reply to Alan Cox
Quoted 9 lines · Alan Cox

Ar Mer, 2006-07-05 am 09:54 -0700, ysgrifennodd Linus Torvalds:

Quoted 3 lines

Anybody with that old a CPU will have learnt to to say "no-hlt" or whatever the kernel command line is, and we could probably retire the silly old hlt check (which I'm not even sure really ever worked).

The one specific case I know precisely details of was the Cyrix 5510. A hlt by the CPU on that chipset during an IDE DMA transfer hangs the system forever.

Yeah, now that you say it, another "halt" problem was some floppy DMA apparently being broken by halt on some machines.

The indirect point of that being that the boot-time hlt test wouldn't actually have triggered that anyway (no DMA taking place at that time).

Although I suspect back when this mattered (a long long time ago ;), the boot-time hlt test made a lot of people more _aware_ of the fact that halt could cause problems.

Sometimes the solutions are purely psychological ;)

Linus

#8 lore raw
In reply to Chuck Ebbert
Quoted 5 lines · Chuck Ebbert

Page faults during kernel initialization can be hard to diagnose.

Add a handler that prints the fault address, EIP and top of stack when an early page fault happens.

Is there really any reason to do this in assembler? The "start_kernel" call into C happens not that much later, and none of what the routine does seems to really be especially assembler-friendly.

Linus

#9 lore raw
In reply to Chuck Ebbert

arch/i386/kernel/head.S rp_sidt: · line 396

Quoted 2 lines · Chuck Ebbert
arch/i386/kernel/head.S +2 -0
+page_fault:
+	cld

My i386 lore is getting a little rusty, can the direction flag actually be random here?

Regards,

Daniel

#10 lore raw
Quoted 6 lines · Daniel Phillips

arch/i386/kernel/head.S rp_sidt: · line 396

Quoted 2 lines · Chuck Ebbert
arch/i386/kernel/head.S +2 -0
+page_fault:
+    cld

My i386 lore is getting a little rusty, can the direction flag actually be random here?

Yes.

-hpa