Skip to contentSkip to conversation

sparc64 mcount misaligned stack

DISCUSSION Igor Kovalenko 2 participants 4 messages Dec 5, 2009 – Dec 6, 2009 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

Hi!

We continue to play with qemu, and it turned out that due to some unknow issue the kernel built with stack overflow detection traps inside error path out of mcount. The problem with that the following code in arch/sparc/lib/mcount.S if we happen to jump at line 68 we have %g3 = 0x4000 and then we assign that to stack pointer.

----
arch/sparc/lib/mcount.S:

66 ldx [%g3 + %g1], %g7

    67         cmp             %sp, %g7
    68         bleu,pt         %xcc, 2f
    69          sethi          %hi(THREAD_SIZE), %g3
    70         add             %g7, %g3, %g7
    71         cmp             %sp, %g7
    72         blu,pn          %xcc, 1f

73 nop
74 /* If we are already on ovstack, don't hop onto it
75 * again, we are already trying to output the stack overflow
76 * message.
77 */
78 sethi %hi(ovstack), %g7 ! cant
move to panic stack fast enough

    79          or             %g7, %lo(ovstack), %g7
    80         add             %g7, OVSTACKSIZE, %g3
    81         sub             %g3, STACK_BIAS + 192, %g3
    82         sub             %g7, STACK_BIAS, %g7
    83         cmp             %sp, %g7
    84         blu,pn          %xcc, 2f
    85          cmp            %sp, %g3
    86         bleu,pn         %xcc, 1f

87 nop
88 2: mov %g3, %sp
89 sethi %hi(panicstring), %g3
90 call prom_printf
91 or %g3, %lo(panicstring), %o0
92 call prom_halt
93 nop
----

A few steps later we take misaligned trap inside prom_printf while accessing stack frame, please see qemu trace below.

Here is qemu trace:

----
IN: _mcount
0x0000000000545b34: or %g3, 0xb0, %o0 ! 0x70f8b0
IN: prom_printf
0x000000000053fa80: save %sp, -192, %sp
IN: prom_printf
0x000000000053fa84: add %fp, 0x887, %g1
IN: prom_printf
0x000000000053fa88: sethi %hi(0x788c00), %l0
IN: prom_printf
0x000000000053fa8c: stx %i1, [ %fp + 0x887 ] 107007: Unaligned Memory Access (v34) pc0000000053fa8c npc0000000053fa90 SP00000000003f40
----

I wonder if mcount ever works on sparc64, if it does work indeed I'd like to know what I'm missing there.
This is running on qemu-system-sparc64 so there is an opportunity to have bug in emulation, but still misaligned stack pointer is evident.

Signature · 2 lines
Kind regards,
Igor V. Kovalenko
maintainer
#1 lore raw

From: Igor Kovalenko <[email protected]>
Date: Sun, 6 Dec 2009 01:27:08 +0300

Quoted 2 lines

I wonder if mcount ever works on sparc64, if it does work indeed I'd like to know what I'm missing there.

Yep, two distinct bugs there. Please try this patch, thanks for the report.

sparc64: Fix stack debugging IRQ stack regression.

Commit 4f70f7a91bffdcc39f088748dc678953eb9a3fbd
(sparc64: Implement IRQ stacks.) has two bugs.

First, the softirq range check forgets to subtract STACK_BIAS before comparing with %sp. Next, on failure the wrong label is jumped to, resulting in a bogus stack being loaded.

arch/sparc/lib/mcount.S +3 -2
index 7ce9c65..24b8b12 100644
@@ -64,8 +64,9 @@ mcount:
 2:	 sethi		%hi(softirq_stack), %g3
 	or		%g3, %lo(softirq_stack), %g3
 	ldx		[%g3 + %g1], %g7
+	sub		%g7, STACK_BIAS, %g7
 	cmp		%sp, %g7
-	bleu,pt		%xcc, 2f
+	bleu,pt		%xcc, 3f
 	 sethi		%hi(THREAD_SIZE), %g3
 	add		%g7, %g3, %g7
 	cmp		%sp, %g7
@@ -75,7 +76,7 @@ mcount:
 	 * again, we are already trying to output the stack overflow
 	 * message.
 	 */
-	sethi		%hi(ovstack), %g7		! cant move to panic stack fast enough
+3:	sethi		%hi(ovstack), %g7		! cant move to panic stack fast enough
 	 or		%g7, %lo(ovstack), %g7
 	add		%g7, OVSTACKSIZE, %g3
 	sub		%g3, STACK_BIAS + 192, %g3
#2 lore raw
In reply to Igor Kovalenko
Quoted 8 lines · David Miller

From: Igor Kovalenko <[email protected]>
Date: Sun, 6 Dec 2009 01:27:08 +0300

Quoted 2 lines

I wonder if mcount ever works on sparc64, if it does work indeed I'd like to know what I'm missing there.

Yep, two distinct bugs there.  Please try this patch, thanks for the report.

Works here, thanks!

- does not trap too early
- prints "Stack overflow" and exits to prom without fault (I tried removing STACK_BIAS adjustment to make it trap)

Signature · 2 lines
Kind regards,
Igor V. Kovalenko
maintainer
#3 lore raw
In reply to Igor Kovalenko

From: Igor Kovalenko <[email protected]>
Date: Sun, 6 Dec 2009 20:37:03 +0300

Quoted 7 lines · David Miller
Quoted 6 lines

From: Igor Kovalenko <[email protected]>
Date: Sun, 6 Dec 2009 01:27:08 +0300

Quoted 2 lines

I wonder if mcount ever works on sparc64, if it does work indeed I'd like to know what I'm missing there.

Quoted 8 lines
Quoted 2 lines

Yep, two distinct bugs there. ,A (BPlease try this patch, thanks for the report.

Works here, thanks!

- does not trap too early
- prints "Stack overflow" and exits to prom without fault (I tried removing STACK_BIAS adjustment to make it trap)

Great, thanks for testing. I'll push the fix around.