ppc32: handle Book E debug exceptions on kernel stack
PATCH 1/1 ppc32: handle Book E debug exceptions on kernel stack
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls smp_processor_id() and spin_lock(), which reference current_thread_info(), which only works when we are on the kernel stack.
We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
---
This is low level code and needs careful review.
I've only tested it on mpc8548cds.
I didn't find the corresponding code in arch/powerpc, so I assume Book E support hasn't been merged yet.
arch/ppc/kernel/head_booke.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/ppc/kernel/head_booke.h +9 -6
@@ -92,7 +92,8 @@* registers as the normal prolog above. Instead we use a portion of the * critical/machine check exception stack at low physical addresses. */ -#define EXC_LEVEL_EXCEPTION_PROLOG(exc_level, exc_level_srr0, exc_level_srr1) \ +#define EXC_LEVEL_EXCEPTION_PROLOG (exc_level, exc_level_srr0, \ + exc_level_srr1, kernel_sp_reg) \ mtspr exc_level##_SPRG,r8; \ BOOKE_LOAD_EXC_LEVEL_STACK(exc_level);/* r8 points to the exc_level stack*/ \ stw r10,GPR10-INT_FRAME_SIZE(r8); \
@@ -100,7 +101,7 @@mfcr r10;/* save CR in r10 for now */ \mfspr r11,exc_level_srr1;/* check whether user or kernel */ \ andi. r11,r11,MSR_PR ; \ -mr r11,r8; \ +mr r11,kernel_sp_reg; \ mfspr r8,exc_level##_SPRG; \ beq 1f; \/* COMING FROM USER MODE */ \
@@ -128,11 +129,13 @@ SAVE_2GPRS(7, r11)#define CRITICAL_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r8) +#define CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG \ + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r1)#define DEBUG_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1, r1)#define MCHECK_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1, r8)/* * Exception vectors.
@@ -268,7 +271,7 @@ label:#else #define DEBUG_EXCEPTION \ START_EXCEPTION(Debug); \ - CRITICAL_EXCEPTION_PROLOG; \ + CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG; \ \/* \ * If there is a single step or branch-taken exception in an \
Let me look at this a little, however, I'm amused by the smp_processor_id() usage on 8548. Its a single core SoC, so any reason SMP is turned on in your kernel?
Not, that your patch, isn't needed for a SMP Book-E, just wondering.
- k
Quoted 76 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Signed-off-by Dale Farnsworth <[email protected]>---
This is low level code and needs careful review.
I've only tested it on mpc8548cds.I didn't find the corresponding code in
arch/powerpc, so I assume Book E support hasn't been merged yet.arch/ppc/kernel/head_booke.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)arch/ppc/kernel/head_booke.h +9
-6@@ -92,7 +92,8 @@* registers as the normal prolog above. Instead we use a portion of the * critical/machine check exception stack at low physical addresses. */ -#define EXC_LEVEL_EXCEPTION_PROLOG(exc_level, exc_level_srr0, exc_level_srr1) \ +#define EXC_LEVEL_EXCEPTION_PROLOG (exc_level, exc_level_srr0, \ + exc_level_srr1, kernel_sp_reg) \ mtspr exc_level##_SPRG,r8; \ BOOKE_LOAD_EXC_LEVEL_STACK(exc_level);/* r8 points to the exc_level stack*/ \ stw r10,GPR10-INT_FRAME_SIZE(r8); \@@ -100,7 +101,7 @@mfcr r10;/* save CR in r10 for now */ \mfspr r11,exc_level_srr1;/* check whether user or kernel */ \ andi. r11,r11,MSR_PR ; \ -mr r11,r8; \ +mr r11,kernel_sp_reg; \ mfspr r8,exc_level##_SPRG; \ beq 1f; \/* COMING FROM USER MODE */ \@@ -128,11 +129,13 @@ SAVE_2GPRS(7, r11)#define CRITICAL_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r8) +#define CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG \ + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r1)#define DEBUG_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1, r1)#define MCHECK_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1, r8)/* * Exception vectors. @@ -268,7 +271,7 @@ label:#else #define DEBUG_EXCEPTION \ START_EXCEPTION(Debug); \ - CRITICAL_EXCEPTION_PROLOG; \ + CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG; \ \/* \ * If there is a single step or branch-taken exception in an \
Quoted 1 line · Kumar Gala
Let me look at this a little, however, I'm amused by the
Thanks.
Quoted 2 lines
smp_processor_id()usage on 8548. Its a single core SoC, so any reason SMP is turned on in your kernel?
I haven't turned on SMP in my 8548 kernel.
The kgdb singlestep code calls smp_processor_id() even on UP. It doesn't really need to call it on UP, but smp_processor_id() still should work. I would have preferred seeing #ifdef CONFIG_SMP scattered throughout that code, but it's not my code. Anyway, the smp_processor_id() calls happen to return the correct value on UP, even on the critical exception stack, since that stack area is initialized to 0.
The issue I actually hit is that for CONFIG_PREEMPT, the calls to spin_lock() and spin_unlock() increment and decrement current_thread_info()->preempt_count. This breaks things when the preempt_count goes negative.
Quoted 1 line
Not, that your patch, isn't needed for a SMP Book-E, just wondering.
Yes, I'm looking forward to trying out a dual-core processor.
-Dale
Quoted 16 lines · Dale Farnsworth
Quoted 15 lines
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Signed-off-by Dale Farnsworth <[email protected]>
Quoted 9 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.
Quoted 6 lines
We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Signed-off-by Dale Farnsworth <[email protected]>
Paul,
We were wondering if you or David remember why a specific critical exception stack was added in the 40x port from 2.4 to 2.6?
- kumar
Quoted 61 lines
---
This is low level code and needs careful review.
I've only tested it on mpc8548cds.I didn't find the corresponding code in
arch/powerpc, so I assume Book E support hasn't been merged yet.arch/ppc/kernel/head_booke.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)arch/ppc/kernel/head_booke.h +9
-6@@ -92,7 +92,8 @@* registers as the normal prolog above. Instead we use a portion of the * critical/machine check exception stack at low physical addresses. */ -#define EXC_LEVEL_EXCEPTION_PROLOG(exc_level, exc_level_srr0, exc_level_srr1) \ +#define EXC_LEVEL_EXCEPTION_PROLOG (exc_level, exc_level_srr0, \ + exc_level_srr1, kernel_sp_reg) \ mtspr exc_level##_SPRG,r8; \ BOOKE_LOAD_EXC_LEVEL_STACK(exc_level);/* r8 points to the exc_level stack*/ \ stw r10,GPR10-INT_FRAME_SIZE(r8); \@@ -100,7 +101,7 @@mfcr r10;/* save CR in r10 for now */ \mfspr r11,exc_level_srr1;/* check whether user or kernel */ \ andi. r11,r11,MSR_PR ; \ -mr r11,r8; \ +mr r11,kernel_sp_reg; \ mfspr r8,exc_level##_SPRG; \ beq 1f; \/* COMING FROM USER MODE */ \@@ -128,11 +129,13 @@ SAVE_2GPRS(7, r11)#define CRITICAL_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r8) +#define CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG \ + EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1, r1)#define DEBUG_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(DEBUG, SPRN_DSRR0, SPRN_DSRR1, r1)#define MCHECK_EXCEPTION_PROLOG \ - EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1) + EXC_LEVEL_EXCEPTION_PROLOG(MCHECK, SPRN_MCSRR0, SPRN_MCSRR1, r8)/* * Exception vectors. @@ -268,7 +271,7 @@ label:#else #define DEBUG_EXCEPTION \ START_EXCEPTION(Debug); \ - CRITICAL_EXCEPTION_PROLOG; \ + CRITICAL_EXCEPTION_ON_KERNEL_STACK_PROLOG; \ \/* \ * If there is a single step or branch-taken exception in an \
Quoted 15 lines · Kumar Gala
Quoted 13 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Dale Farnsworth <[email protected]>Paul,
We were wondering if you or David remember why a specific critical exception stack was added in the 40x port from 2.4 to 2.6?
I think Matt did that. And if I remember correctly, it was to avoid corruption if you were in the middle of handling a normal interrupt and a critical interrupt came in.
josh
Quoted 18 lines · Josh Boyer
Quoted 17 lines · Kumar Gala
Quoted 14 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Signed-off-by Dale Farnsworth <[email protected]>
Quoted 8 lines
Quoted 4 lines
Paul,
We were wondering if you or David remember why a specific critical exception stack was added in the 40x port from 2.4 to 2.6?
I think Matt did that. And if I remember correctly, it was to avoid corruption if you were in the middle of handling a normal interrupt and a critical interrupt came in.
Thanks Josh, Matt was one of the "we" who were wondering. If indeed you are remembering correctly, then I think we're safe to use the normal kernel stack rather than the critical exception stack.
-Dale
Quoted 15 lines · Kumar Gala
Quoted 13 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Dale Farnsworth <[email protected]>Paul,
We were wondering if you or David remember why a specific critical exception stack was added in the 40x port from 2.4 to 2.6?
I don't, in any detail. But did it have to do with a possible race with a critical exception in just the wrong part of the normal exception exit path. ISTR BenH was worried about something in that area.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 21 lines · David Gibson
Quoted 20 lines · Kumar Gala
Quoted 9 lines · Dale Farnsworth
From: Dale Farnsworth <[email protected]>
On PPC Book E processsors, we currently handle debug exceptions on the critical exception stack (debug stack for E200). This causes problems with the kgdb single step handler, which calls
smp_processor_id()andspin_lock(), which referencecurrent_thread_info(), which only works when we are on the kernel stack.Quoted 6 lines
We address this by switching to the kernel stack early while handling debug exceptions. Note that the entry values of r10 and r11 are still saved on the critical exception (or debug) stack.
Signed-off-by Dale Farnsworth <[email protected]>Paul,
Quoted 7 lines
Quoted 2 lines
We were wondering if you or David remember why a specific critical exception stack was added in the 40x port from 2.4 to 2.6?
I don't, in any detail. But did it have to do with a possible race with a critical exception in just the wrong part of the normal exception exit path. ISTR BenH was worried about something in that area.
There's a race between a critical exception and the normal exception entry path. If a normal exception occurs in user mode and then a critical exception occurs before the normal exception handler has set r1 to the kernel stack area, we end up with r1 containing the user sp, though the critical exception occurred in kernel mode.
I haven't yet come up with a way to detect this case and reliably use the kernel stack on a critical exception.
-Dale
Quoted 5 lines · Dale Farnsworth
There's a race between a critical exception and the normal exception entry path. If a normal exception occurs in user mode and then a critical exception occurs before the normal exception handler has set r1 to the kernel stack area, we end up with r1 containing the user sp, though the critical exception occurred in kernel mode.
That's a separate problem from what we were discussing, which is why program_check_exception is entered with interrupts disabled.
Quoted 2 lines
I haven't yet come up with a way to detect this case and reliably use the kernel stack on a critical exception.
One way to solve this is to put the kernel stack pointer in some SPRGn when exiting to usermode, and on critical exceptions, use that SPRGn for the kernel stack if it is non-zero. In the exception entry path (for both normal and critical exceptions) put 0 in it once r1 has been set up with a valid kernel stack pointer. This also works if you have to call firmware such as RTAS which runs in kernel mode but may put arbitrary values in r1.
Paul.