Skip to contentSkip to conversation

broken ebba638ae723d8a8fc2f7abce5ec18b688b791d7

BUG matthieu castet 7 participants 28 messages Jan 27, 2011 – Feb 5, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi,

ebba638ae723d8a8fc2f7abce5ec18b688b791d7 x86, cpu: Call verify_cpu during 32bit CPU startup look buggy.

It add a call to verify_cpu, but we never set the stack before (I check with qemu + gdbserver that sp is random when doing cpu hotplug).
This mean do randomly corrupt the memory.

Matthieu

#1 lore raw

Hi matthieu,

Quoted 5 lines · matthieu castet

ebba638ae723d8a8fc2f7abce5ec18b688b791d7 x86, cpu: Call verify_cpu during 32bit CPU startup look buggy.

It add a call to verify_cpu, but we never set the stack before (I check with qemu + gdbserver that sp is random when doing cpu hotplug).
This mean do randomly corrupt the memory.

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

        movw    $(trampoline_stack_end - r_base), %sp

arch/x86/boot/compressed/head_64.S uses:

        movl    $boot_stack_end, %eax
        addl    %ebp, %eax
        movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

arch/x86/kernel/head_32.S +2 -2
index fc293dc..8ddd0e4 100644
@@ -284,6 +284,8 @@ ENTRY(startup_32_smp)
 	movl %eax,%gs
 #endif /* CONFIG_SMP */
 default_entry:
+	/* Set up the stack pointer */
+	lss stack_start,%esp
 
 /*
  *	New page tables may be in 4Mbyte page mode and may
@@ -347,8 +349,6 @@ default_entry:
 	movl %eax,%cr0		/* ..and set paging (PG) bit */
 	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
 1:
-	/* Set up the stack pointer */
-	lss stack_start,%esp
 
 /*
  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#2 lore raw
Quoted 6 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

         movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

         movl    $boot_stack_end, %eax
         addl    %ebp, %eax
         movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +5 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
        movl %eax,%es
        movl %eax,%fs
        movl %eax,%gs
+       movl %eax,%ss
 2:
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
 
 /*
  * Clear BSS first so that there are no surprises...

-hpa

#3 lore raw

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main user of skipping segment register initialization.

-hpa

Quoted 43 lines · H. Peter Anvin
Quoted 6 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:
movw $(trampoline_stack_end - r_base), %sp

Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:
movl $boot_stack_end, %eax
addl %ebp, %eax
movl %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +0 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)

movl %eax,%es
movl %eax,%fs
movl %eax,%gs
+ movl %eax,%ss
2:
+/*
+ * Set up an initial stack
+ */
+ movl $pa(init_thread_union+THREAD_SIZE), %esp

/*
* Clear BSS first so that there are no surprises...

-hpa

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

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main user of skipping segment register initialization.

Xen doesn't go through head_*.S at all.

J

Quoted 50 lines

-hpa

Quoted 44 lines · H. Peter Anvin
Quoted 6 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:
movw $(trampoline_stack_end - r_base), %sp

Quoted 9 lines

arch/x86/boot/compressed/head_64.S uses:
movl $boot_stack_end, %eax
addl %ebp, %eax
movl %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start",
but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +0 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)

movl %eax,%es
movl %eax,%fs
movl %eax,%gs
+ movl %eax,%ss
2:
+/*
+ * Set up an initial stack
+ */
+ movl $pa(init_thread_union+THREAD_SIZE), %esp

/*
* Clear BSS first so that there are no surprises...

-hpa

#11 lore raw
In reply to H. Peter Anvin

Hi,

Quoted 33 lines · H. Peter Anvin
Quoted 6 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

        movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

        movl    $boot_stack_end, %eax
        addl    %ebp, %eax
        movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +0 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
        movl %eax,%es
        movl %eax,%fs

arch/x86/kernel/head_32.S ENTRY(startup_32) · line 110

Quoted 10 lines
arch/x86/kernel/head_32.S +5 -0
        movl %eax,%gs
+       movl %eax,%ss
 2:
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
 
 /*
  * Clear BSS first so that there are no surprises...

This doesn't appear to work for me. While I can boot fine, doing CPU hotplugging hangs the system. :(

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#12 lore raw
Quoted 42 lines · Kees Cook

Hi,

Quoted 39 lines · H. Peter Anvin
Quoted 5 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

        movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

        movl    $boot_stack_end, %eax
        addl    %ebp, %eax
        movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +5 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
        movl %eax,%es
        movl %eax,%fs
        movl %eax,%gs
+       movl %eax,%ss
 2:
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
 
Quoted 6 lines

arch/x86/kernel/head_32.S ENTRY(startup_32) · line 110

Quoted 2 lines
arch/x86/kernel/head_32.S +0 -0
 /*
  * Clear BSS first so that there are no surprises...

This doesn't appear to work for me. While I can boot fine, doing CPU hotplugging hangs the system. :(

This is weird because the patch only touch first cpu (startup_32 entry) and cpu hotplug go to startup_32_smp.

Here a untested patch that move the stack setup in the common path.

Matthieu

arch/x86/kernel/head_32.S +6 -0
index fc293dc..5df3432 100644
@@ -284,6 +284,12 @@ ENTRY(startup_32_smp)
 	movl %eax,%gs
 #endif /* CONFIG_SMP */
 default_entry:
+	/*
+	 * Set up an initial stack
+	 */
+	movl $(__BOOT_DS),%eax
+	movl %eax,%ss
+	movl $pa(init_thread_union+THREAD_SIZE), %esp
 
 /*
  *	New page tables may be in 4Mbyte page mode and may
#20 lore raw
In reply to Kees Cook
Quoted 42 lines · Kees Cook

Hi,

Quoted 39 lines · H. Peter Anvin
Quoted 5 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

        movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

        movl    $boot_stack_end, %eax
        addl    %ebp, %eax
        movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +5 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
        movl %eax,%es
        movl %eax,%fs
        movl %eax,%gs
+       movl %eax,%ss
 2:
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
 
Quoted 6 lines

arch/x86/kernel/head_32.S ENTRY(startup_32) · line 110

Quoted 2 lines
arch/x86/kernel/head_32.S +0 -0
 /*
  * Clear BSS first so that there are no surprises...

This doesn't appear to work for me. While I can boot fine, doing CPU hotplugging hangs the system. :(

That's because of NX :)

The stack is marked NX,

#13 lore raw
In reply to matthieu castet
Quoted 49 lines · matthieu castet
Quoted 42 lines · Kees Cook

Hi,

Quoted 39 lines · H. Peter Anvin
Quoted 5 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

        movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

        movl    $boot_stack_end, %eax
        addl    %ebp, %eax
        movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +5 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
        movl %eax,%es
        movl %eax,%fs
        movl %eax,%gs
+       movl %eax,%ss
 2:
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
 
Quoted 6 lines

arch/x86/kernel/head_32.S ENTRY(startup_32) · line 110

Quoted 2 lines
arch/x86/kernel/head_32.S +0 -0
 /*
  * Clear BSS first so that there are no surprises...

This doesn't appear to work for me. While I can boot fine, doing CPU hotplugging hangs the system. :(

Quoted 4 lines

This is weird because the patch only touch first cpu (startup_32 entry) and cpu hotplug go to startup_32_smp.

Here a untested patch that move the stack setup in the common path.

Well, in my not so humble opinion the amount of random fixes required by the entire NX protection of kernel "data" pages is simply unacceptable.

Please revert the whole thing and do it once again from scratch and possibly without breaking stuff left and right. I guess you've learnt enough so far that this should be doable for 2.6.39 without introducing major issues like _boot_ problems (let alone the broken resume).

Thanks,
Rafael

#16 lore raw
In reply to matthieu castet

Hi Matthieu,

Quoted 59 lines · matthieu castet
Quoted 40 lines · Kees Cook
Quoted 39 lines · H. Peter Anvin
Quoted 5 lines · Kees Cook

Yikes, good catch.

arch/x86/kernel/trampoline_64.S uses:

       movw    $(trampoline_stack_end - r_base), %sp
Quoted 8 lines

arch/x86/boot/compressed/head_64.S uses:

       movl    $boot_stack_end, %eax
       addl    %ebp, %eax
       movl    %eax, %esp

what would be safe for arch/x86/kernel/head_32.S ? It uses "stack_start", but later after paging set-up. Is the following sane to solve this?

To run it before paging is set up, you can't use stack, start; you have to use a pointer based on physical address. You have two problems with using stack_start: you're using a linear address to access stack_start, and stack_start itself contains a linear address.

It's not entirely clear to me why we don't initialize %ss to __BOOT_DS with the other segment registers, but it would make most sense to me:

arch/x86/kernel/head_32.S +1 -0
index fc293dc..c10f9ba 100644
@@ -99,7 +99,12 @@ ENTRY(startup_32)
       movl %eax,%es
       movl %eax,%fs
       movl %eax,%gs
+       movl %eax,%ss

2:

arch/x86/kernel/head_32.S +4 -0
+/*
+ * Set up an initial stack
+ */
+       movl $pa(init_thread_union+THREAD_SIZE), %esp
Quoted 6 lines
Quoted 2 lines

/*
* Clear BSS first so that there are no surprises...

This doesn't appear to work for me. While I can boot fine, doing CPU hotplugging hangs the system. :(

This is weird because the patch only touch first cpu (startup_32 entry) and cpu hotplug go to startup_32_smp.

Here a untested patch that move the stack setup in the common path.

arch/x86/kernel/head_32.S +0 -0
index fc293dc..5df3432 100644
@@ -284,6 +284,12 @@ ENTRY(startup_32_smp)
 	movl %eax,%gs
 #endif /* CONFIG_SMP */

arch/x86/kernel/head_32.S ENTRY(startup_32_smp) · line 295

Quoted 10 lines
arch/x86/kernel/head_32.S +6 -0
 default_entry:
+	/*
+	 * Set up an initial stack
+	 */
+	movl $(__BOOT_DS),%eax
+	movl %eax,%ss
+	movl $pa(init_thread_union+THREAD_SIZE), %esp
 
 /*
  *	New page tables may be in 4Mbyte page mode and may

This worked, thanks! If this tests cleanly for you in qemu, we should get this committed.

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#17 lore raw
Quoted 4 lines · Kees Cook

This worked, thanks! If this tests cleanly for you in qemu, we should get this committed.

This is wrong for at least one reason; possibly two:

a) it ignores the control to not reload the segment registers (not sure if anything still uses them, but...)

b) I'm not sure that init_thread_union is safe for the non-BSP CPU here.

-hpa

#14 lore raw
In reply to Rafael J. Wysocki

Quoting "Rafael J. Wysocki" <[email protected]>:

Quoted 3 lines · matthieu castet

Well, in my not so humble opinion the amount of random fixes required by the entire NX protection of kernel "data" pages is simply unacceptable.

What's the relation between this bug and NX protection of kernel "data" ?

Matthieu

#15 lore raw
Quoted 7 lines · On Tuesday, February 01, 2011, [email protected] wrote:

Quoting "Rafael J. Wysocki" <[email protected]>:

Quoted 3 lines · matthieu castet

Well, in my not so humble opinion the amount of random fixes required by the entire NX protection of kernel "data" pages is simply unacceptable.

What's the relation between this bug and NX protection of kernel "data" ?

Sorry, I confused threads.

Rafael

#18 lore raw
In reply to H. Peter Anvin
Quoted 6 lines · H. Peter Anvin
Quoted 4 lines · Kees Cook

This worked, thanks! If this tests cleanly for you in qemu, we should get this committed.

Quoted 6 lines

This is wrong for at least one reason; possibly two:

a) it ignores the control to not reload the segment registers (not sure if anything still uses them, but...)

b) I'm not sure that init_thread_union is safe for the non-BSP CPU here.

What's the best way to move forward? Or, how can we find answers to these questions?

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#5 lore raw
In reply to Jeremy Fitzhardinge
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 4 lines · H. Peter Anvin

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main user of skipping segment register initialization.

Xen doesn't go through head_*.S at all.

OK, I guess it's actually lguest which needs this feature?

-hpa

#6 lore raw
Quoted 11 lines · H. Peter Anvin
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 4 lines · H. Peter Anvin

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main user of skipping segment register initialization.

Xen doesn't go through head_*.S at all.

OK, I guess it's actually lguest which needs this feature?

Well, we already move __BOOT_DS into every other segment reg, so I don't think %ss will break...

Checking...
Rusty.

#7 lore raw
Quoted 11 lines · Rusty Russell
Quoted 10 lines · H. Peter Anvin
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 4 lines · H. Peter Anvin

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main user of skipping segment register initialization.

Xen doesn't go through head_*.S at all.

Quoted 6 lines
Quoted 1 line

OK, I guess it's actually lguest which needs this feature?

Well, we already move __BOOT_DS into every other segment reg, so I don't think %ss will break...

Checking...

Fine here.

Thanks,
Rusty.
PS. In general, I don't want lguest to interfere with cleanups: I'd rather
fix up lguest as required until it gets too hard, then kill it.

#8 lore raw

Can we completely kill the "don't reload segments" flag, then?

Quoted 28 lines · Rusty Russell
Quoted 6 lines · Rusty Russell
Quoted 5 lines · H. Peter Anvin
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 3 lines · H. Peter Anvin

[Adding Jeremy]

Jeremy, would this break Xen? As far as I know, Xen is the main

user

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

of skipping segment register initialization.

Xen doesn't go through head_*.S at all.

OK, I guess it's actually lguest which needs this feature?

Well, we already move __BOOT_DS into every other segment reg, so I

don't think

Quoted 3 lines

%ss will break...

Checking...

Fine here.

Thanks,
Rusty.
PS. In general, I don't want lguest to interfere with cleanups: I'd rather
fix up lguest as required until it gets too hard, then kill it.

Signature · 1 line
Sent from my mobile phone.  Please pardon any lack of formatting.
#9 lore raw
Quoted 1 line · H. Peter Anvin

Can we completely kill the "don't reload segments" flag, then?

We can as far as *I* am concerned, but of course it's a separate issue...

Thanks,
Rusty.

#10 lore raw
Quoted 4 lines · Rusty Russell
Quoted 1 line · H. Peter Anvin

Can we completely kill the "don't reload segments" flag, then?

We can as far as *I* am concerned, but of course it's a separate issue...

I'm trying to figure out if there is any actual user of it. I thought it was Xen which needed it, but Jeremy says it doesn't; I don't know what else would possible need it. If it is is something that isn't used then it would be better to get rid of it.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#19 lore raw
In reply to Kees Cook
Quoted 12 lines · Kees Cook
Quoted 11 lines · H. Peter Anvin
Quoted 4 lines · Kees Cook

This worked, thanks! If this tests cleanly for you in qemu, we should get this committed.

This is wrong for at least one reason; possibly two:

a) it ignores the control to not reload the segment registers (not sure if anything still uses them, but...)

Quoted 7 lines
Quoted 1 line

b) I'm not sure that init_thread_union is safe for the non-BSP CPU here.

What's the best way to move forward? Or, how can we find answers to these questions?

-Kees

Can someone test out the attached patch and verify that it works?

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
arch/x86/kernel/head_32.S +12 -2
index fc293dc..2aee594 100644
@@ -26,6 +26,11 @@
 #define pa(X) ((X) - __PAGE_OFFSET)
 
 /*
+ * Initial stack pointer for the boot processor
+ */
+initial_stack_pointer = init_thread_union+THREAD_SIZE
+
+/*
  * References to members of the new_cpu_data structure.
  */
 
@@ -99,8 +104,10 @@ ENTRY(startup_32)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl %eax,%ss
 2:
-
+	movl $pa(initial_stack_pointer), %esp
+	
 /*
  * Clear BSS first so that there are no surprises...
  */
@@ -282,6 +289,9 @@ ENTRY(startup_32_smp)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl pa(stack_start),%ecx
+	movl %eax,%ss
+	leal -__PAGE_OFFSET(%ecx),%esp
 #endif /* CONFIG_SMP */
 default_entry:
 
@@ -671,7 +681,7 @@ ENTRY(initial_page_table)
 
 .data
 ENTRY(stack_start)
-	.long init_thread_union+THREAD_SIZE
+	.long initial_stack_pointer
 	.long __BOOT_DS
 
 ready:	.byte 0
#21 lore raw

[tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

In reply to matthieu castet
Commit-ID:  990957d4076354d449921b598cc8de28764cf7ed
Gitweb:     http://git.kernel.org/tip/990957d4076354d449921b598cc8de28764cf7ed
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Fri, 4 Feb 2011 16:14:11 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Fri, 4 Feb 2011 16:27:21 -0800

x86-32: Make sure the stack is set up before we use it

Since checkin ebba638ae723d8a8fc2f7abce5ec18b688b791d7 we call verify_cpu even in 32-bit mode. Unfortunately, calling a function means using the stack, and the stack pointer was not initialized in the 32-bit setup code! This code initializes the stack pointer, and simplifies the interface slightly since it is easier to rely on just a pointer value rather than a descriptor; we need to have different values for the segment register anyway.

This retains start_stack as a virtual address, even though a physical address would be more convenient for 32 bits; the 64-bit code wants the other way around...

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/smp.h   |    5 +----
 arch/x86/kernel/acpi/sleep.c |    2 +-
 arch/x86/kernel/head_32.S    |   20 ++++++++++++--------
 arch/x86/kernel/head_64.S    |    1 -
 arch/x86/kernel/smpboot.c    |    4 ++--
 5 files changed, 16 insertions(+), 16 deletions(-)
arch/x86/include/asm/smp.h +1 -4
index 4c2f63c..1f46951 100644
@@ -40,10 +40,7 @@ DECLARE_EARLY_PER_CPU(u16, x86_cpu_to_apicid);
 DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
 
 /* Static state in head.S used to set up a CPU */
-extern struct {
-	void *sp;
-	unsigned short ss;
-} stack_start;
+extern unsigned long stack_start; /* Initial stack pointer address */
 
 struct smp_ops {
 	void (*smp_prepare_boot_cpu)(void);
arch/x86/kernel/acpi/sleep.c +1 -1
index 69fd72a..4d9ebba 100644
@@ -100,7 +100,7 @@ int acpi_save_state_mem(void)
 #else /* CONFIG_64BIT */
 	header->trampoline_segment = setup_trampoline() >> 4;
 #ifdef CONFIG_SMP
-	stack_start.sp = temp_stack + sizeof(temp_stack);
+	stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
 	early_gdt_descr.address =
 			(unsigned long)get_cpu_gdt_table(smp_processor_id());
 	initial_gs = per_cpu_offset(smp_processor_id());
arch/x86/kernel/head_32.S +12 -8
index fc293dc..10a6381 100644
@@ -85,6 +85,8 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  */
 __HEAD
 ENTRY(startup_32)
+	movl pa(stack_start),%ecx
+	
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
 		us to not reload segments */
 	testb $(1<<6), BP_loadflags(%esi)
@@ -99,7 +101,9 @@ ENTRY(startup_32)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl %eax,%ss
 2:
+	leal -__PAGE_OFFSET(%ecx),%esp
 
 /*
  * Clear BSS first so that there are no surprises...
@@ -145,8 +149,6 @@ ENTRY(startup_32)
  * _brk_end is set up to point to the first "safe" location.
  * Mappings are created both at virtual address 0 (identity mapping)
  * and PAGE_OFFSET for up to _end.
- *
- * Note that the stack is not yet set up!
  */
 #ifdef CONFIG_X86_PAE
 
@@ -282,6 +284,9 @@ ENTRY(startup_32_smp)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl pa(stack_start),%ecx
+	movl %eax,%ss
+	leal -__PAGE_OFFSET(%ecx),%esp
 #endif /* CONFIG_SMP */
 default_entry:
 
@@ -347,8 +352,8 @@ default_entry:
 	movl %eax,%cr0		/* ..and set paging (PG) bit */
 	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
 1:
-	/* Set up the stack pointer */
-	lss stack_start,%esp
+	/* Shift the stack pointer to a virtual address */
+	addl $__PAGE_OFFSET, %esp
 
 /*
  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
@@ -475,7 +480,6 @@ is386:	movl $2,%ecx		# set MP
 	movb $1, ready
 	cmpb $0,%cl		# the first CPU calls start_kernel
 	je   1f
-	movl (stack_start), %esp
 1:
 #endif /* CONFIG_SMP */
 	jmp *(initial_code)
@@ -670,15 +674,15 @@ ENTRY(initial_page_table)
 #endif
 
 .data
+.balign 4
 ENTRY(stack_start)
 	.long init_thread_union+THREAD_SIZE
-	.long __BOOT_DS
-
-ready:	.byte 0
 
 early_recursion_flag:
 	.long 0
 
+ready:	.byte 0
+
 int_msg:
 	.asciz "Unknown interrupt or fault at: %p %p %p\n"
 
arch/x86/kernel/head_64.S +0 -1
index 239046b..1ee8493 100644
@@ -264,7 +264,6 @@ ENTRY(secondary_startup_64)
 
 	ENTRY(stack_start)
 	.quad  init_thread_union+THREAD_SIZE-8
-	.word  0
 	__FINITDATA
 
 bad_address:
arch/x86/kernel/smpboot.c +2 -2
index 0cbe8c0..03273b6 100644
@@ -638,7 +638,7 @@ wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
 	 * target processor state.
 	 */
 	startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
-			 (unsigned long)stack_start.sp);
+			 stack_start);
 
 	/*
 	 * Run STARTUP IPI loop.
@@ -785,7 +785,7 @@ do_rest:
 #endif
 	early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
 	initial_code = (unsigned long)start_secondary;
-	stack_start.sp = (void *) c_idle.idle->thread.sp;
+	stack_start  = c_idle.idle->thread.sp;
 
 	/* start_ip had better be page-aligned! */
 	start_ip = setup_trampoline();
#22 lore raw

[tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

In reply to matthieu castet
Commit-ID:  43d79a250bdb10e30b08a79e906befc691c3f5ce
Gitweb:     http://git.kernel.org/tip/43d79a250bdb10e30b08a79e906befc691c3f5ce
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Fri, 4 Feb 2011 16:14:11 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Fri, 4 Feb 2011 16:38:35 -0800

x86-32: Make sure the stack is set up before we use it

Since checkin ebba638ae723d8a8fc2f7abce5ec18b688b791d7 we call verify_cpu even in 32-bit mode. Unfortunately, calling a function means using the stack, and the stack pointer was not initialized in the 32-bit setup code! This code initializes the stack pointer, and simplifies the interface slightly since it is easier to rely on just a pointer value rather than a descriptor; we need to have different values for the segment register anyway.

This retains start_stack as a virtual address, even though a physical address would be more convenient for 32 bits; the 64-bit code wants the other way around...

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/smp.h   |    5 +----
 arch/x86/kernel/acpi/sleep.c |    2 +-
 arch/x86/kernel/head_32.S    |   30 +++++++++++++-----------------
 arch/x86/kernel/head_64.S    |    1 -
 arch/x86/kernel/smpboot.c    |    4 ++--
 5 files changed, 17 insertions(+), 25 deletions(-)
arch/x86/include/asm/smp.h +1 -4
index 4c2f63c..1f46951 100644
@@ -40,10 +40,7 @@ DECLARE_EARLY_PER_CPU(u16, x86_cpu_to_apicid);
 DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
 
 /* Static state in head.S used to set up a CPU */
-extern struct {
-	void *sp;
-	unsigned short ss;
-} stack_start;
+extern unsigned long stack_start; /* Initial stack pointer address */
 
 struct smp_ops {
 	void (*smp_prepare_boot_cpu)(void);
arch/x86/kernel/acpi/sleep.c +1 -1
index 69fd72a..4d9ebba 100644
@@ -100,7 +100,7 @@ int acpi_save_state_mem(void)
 #else /* CONFIG_64BIT */
 	header->trampoline_segment = setup_trampoline() >> 4;
 #ifdef CONFIG_SMP
-	stack_start.sp = temp_stack + sizeof(temp_stack);
+	stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
 	early_gdt_descr.address =
 			(unsigned long)get_cpu_gdt_table(smp_processor_id());
 	initial_gs = per_cpu_offset(smp_processor_id());
arch/x86/kernel/head_32.S +13 -17
index fc293dc..767d6c4 100644
@@ -85,6 +85,8 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  */
 __HEAD
 ENTRY(startup_32)
+	movl pa(stack_start),%ecx
+	
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
 		us to not reload segments */
 	testb $(1<<6), BP_loadflags(%esi)
@@ -99,7 +101,9 @@ ENTRY(startup_32)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl %eax,%ss
 2:
+	leal -__PAGE_OFFSET(%ecx),%esp
 
 /*
  * Clear BSS first so that there are no surprises...
@@ -145,8 +149,6 @@ ENTRY(startup_32)
  * _brk_end is set up to point to the first "safe" location.
  * Mappings are created both at virtual address 0 (identity mapping)
  * and PAGE_OFFSET for up to _end.
- *
- * Note that the stack is not yet set up!
  */
 #ifdef CONFIG_X86_PAE
 
@@ -282,6 +284,9 @@ ENTRY(startup_32_smp)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl pa(stack_start),%ecx
+	movl %eax,%ss
+	leal -__PAGE_OFFSET(%ecx),%esp
 #endif /* CONFIG_SMP */
 default_entry:
 
@@ -347,8 +352,8 @@ default_entry:
 	movl %eax,%cr0		/* ..and set paging (PG) bit */
 	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
 1:
-	/* Set up the stack pointer */
-	lss stack_start,%esp
+	/* Shift the stack pointer to a virtual address */
+	addl $__PAGE_OFFSET, %esp
 
 /*
  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
@@ -360,9 +365,7 @@ default_entry:
 
 #ifdef CONFIG_SMP
 	cmpb $0, ready
-	jz  1f				/* Initial CPU cleans BSS */
-	jmp checkCPUtype
-1:
+	jnz checkCPUtype
 #endif /* CONFIG_SMP */
 
 /*
@@ -470,14 +473,7 @@ is386:	movl $2,%ecx		# set MP
 
 	cld			# gcc2 wants the direction flag cleared at all times
 	pushl $0		# fake return address for unwinder
-#ifdef CONFIG_SMP
-	movb ready, %cl
 	movb $1, ready
-	cmpb $0,%cl		# the first CPU calls start_kernel
-	je   1f
-	movl (stack_start), %esp
-1:
-#endif /* CONFIG_SMP */
 	jmp *(initial_code)
 
 /*
@@ -670,15 +666,15 @@ ENTRY(initial_page_table)
 #endif
 
 .data
+.balign 4
 ENTRY(stack_start)
 	.long init_thread_union+THREAD_SIZE
-	.long __BOOT_DS
-
-ready:	.byte 0
 
 early_recursion_flag:
 	.long 0
 
+ready:	.byte 0
+
 int_msg:
 	.asciz "Unknown interrupt or fault at: %p %p %p\n"
 
arch/x86/kernel/head_64.S +0 -1
index 239046b..1ee8493 100644
@@ -264,7 +264,6 @@ ENTRY(secondary_startup_64)
 
 	ENTRY(stack_start)
 	.quad  init_thread_union+THREAD_SIZE-8
-	.word  0
 	__FINITDATA
 
 bad_address:
arch/x86/kernel/smpboot.c +2 -2
index 0cbe8c0..03273b6 100644
@@ -638,7 +638,7 @@ wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
 	 * target processor state.
 	 */
 	startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
-			 (unsigned long)stack_start.sp);
+			 stack_start);
 
 	/*
 	 * Run STARTUP IPI loop.
@@ -785,7 +785,7 @@ do_rest:
 #endif
 	early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
 	initial_code = (unsigned long)start_secondary;
-	stack_start.sp = (void *) c_idle.idle->thread.sp;
+	stack_start  = c_idle.idle->thread.sp;
 
 	/* start_ip had better be page-aligned! */
 	start_ip = setup_trampoline();
#23 lore raw

Re: [tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

Hi Peter,

This works for me doing CPU hotplugging on ia32, but on x86_64 it hangs. (Both boot, though.)

Thanks,

-Kees

Quoted 194 lines · tip-bot for H. Peter Anvin
Commit-ID:  43d79a250bdb10e30b08a79e906befc691c3f5ce
Gitweb:     http://git.kernel.org/tip/43d79a250bdb10e30b08a79e906befc691c3f5ce
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Fri, 4 Feb 2011 16:14:11 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Fri, 4 Feb 2011 16:38:35 -0800

x86-32: Make sure the stack is set up before we use it

Since checkin ebba638ae723d8a8fc2f7abce5ec18b688b791d7 we call verify_cpu even in 32-bit mode. Unfortunately, calling a function means using the stack, and the stack pointer was not initialized in the 32-bit setup code! This code initializes the stack pointer, and simplifies the interface slightly since it is easier to rely on just a pointer value rather than a descriptor; we need to have different values for the segment register anyway.

This retains start_stack as a virtual address, even though a physical address would be more convenient for 32 bits; the 64-bit code wants the other way around...

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/smp.h   |    5 +----
 arch/x86/kernel/acpi/sleep.c |    2 +-
 arch/x86/kernel/head_32.S    |   30 +++++++++++++-----------------
 arch/x86/kernel/head_64.S    |    1 -
 arch/x86/kernel/smpboot.c    |    4 ++--
 5 files changed, 17 insertions(+), 25 deletions(-)
arch/x86/include/asm/smp.h +1 -4
index 4c2f63c..1f46951 100644
@@ -40,10 +40,7 @@ DECLARE_EARLY_PER_CPU(u16, x86_cpu_to_apicid);
 DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
 
 /* Static state in head.S used to set up a CPU */
-extern struct {
-	void *sp;
-	unsigned short ss;
-} stack_start;
+extern unsigned long stack_start; /* Initial stack pointer address */
 
 struct smp_ops {
 	void (*smp_prepare_boot_cpu)(void);
arch/x86/kernel/acpi/sleep.c +1 -1
index 69fd72a..4d9ebba 100644
@@ -100,7 +100,7 @@ int acpi_save_state_mem(void)
 #else /* CONFIG_64BIT */
 	header->trampoline_segment = setup_trampoline() >> 4;
 #ifdef CONFIG_SMP
-	stack_start.sp = temp_stack + sizeof(temp_stack);
+	stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
 	early_gdt_descr.address =
 			(unsigned long)get_cpu_gdt_table(smp_processor_id());
 	initial_gs = per_cpu_offset(smp_processor_id());
arch/x86/kernel/head_32.S +13 -17
index fc293dc..767d6c4 100644
@@ -85,6 +85,8 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  */
 __HEAD
 ENTRY(startup_32)
+	movl pa(stack_start),%ecx
+	
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
 		us to not reload segments */
 	testb $(1<<6), BP_loadflags(%esi)
@@ -99,7 +101,9 @@ ENTRY(startup_32)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl %eax,%ss
 2:
+	leal -__PAGE_OFFSET(%ecx),%esp
 
 /*
  * Clear BSS first so that there are no surprises...
@@ -145,8 +149,6 @@ ENTRY(startup_32)
  * _brk_end is set up to point to the first "safe" location.
  * Mappings are created both at virtual address 0 (identity mapping)
  * and PAGE_OFFSET for up to _end.
- *
- * Note that the stack is not yet set up!
  */
 #ifdef CONFIG_X86_PAE
 
@@ -282,6 +284,9 @@ ENTRY(startup_32_smp)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl pa(stack_start),%ecx
+	movl %eax,%ss
+	leal -__PAGE_OFFSET(%ecx),%esp
 #endif /* CONFIG_SMP */
 default_entry:
 
@@ -347,8 +352,8 @@ default_entry:
 	movl %eax,%cr0		/* ..and set paging (PG) bit */
 	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
 1:
-	/* Set up the stack pointer */
-	lss stack_start,%esp
+	/* Shift the stack pointer to a virtual address */
+	addl $__PAGE_OFFSET, %esp
 
 /*
  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
@@ -360,9 +365,7 @@ default_entry:
 
 #ifdef CONFIG_SMP
 	cmpb $0, ready
-	jz  1f				/* Initial CPU cleans BSS */
-	jmp checkCPUtype
-1:
+	jnz checkCPUtype
 #endif /* CONFIG_SMP */
 
 /*
@@ -470,14 +473,7 @@ is386:	movl $2,%ecx		# set MP
 
 	cld			# gcc2 wants the direction flag cleared at all times
 	pushl $0		# fake return address for unwinder
-#ifdef CONFIG_SMP
-	movb ready, %cl
 	movb $1, ready
-	cmpb $0,%cl		# the first CPU calls start_kernel
-	je   1f
-	movl (stack_start), %esp
-1:
-#endif /* CONFIG_SMP */
 	jmp *(initial_code)
 
 /*
@@ -670,15 +666,15 @@ ENTRY(initial_page_table)
 #endif
 
 .data
+.balign 4
 ENTRY(stack_start)
 	.long init_thread_union+THREAD_SIZE
-	.long __BOOT_DS
-
-ready:	.byte 0
 
 early_recursion_flag:
 	.long 0
 
+ready:	.byte 0
+
 int_msg:
 	.asciz "Unknown interrupt or fault at: %p %p %p\n"
 
arch/x86/kernel/head_64.S +0 -1
index 239046b..1ee8493 100644
@@ -264,7 +264,6 @@ ENTRY(secondary_startup_64)
 
 	ENTRY(stack_start)
 	.quad  init_thread_union+THREAD_SIZE-8
-	.word  0
 	__FINITDATA
 
 bad_address:
arch/x86/kernel/smpboot.c +2 -2
index 0cbe8c0..03273b6 100644
@@ -638,7 +638,7 @@ wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
 	 * target processor state.
 	 */
 	startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
-			 (unsigned long)stack_start.sp);
+			 stack_start);
 
 	/*
 	 * Run STARTUP IPI loop.
@@ -785,7 +785,7 @@ do_rest:
 #endif
 	early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
 	initial_code = (unsigned long)start_secondary;
-	stack_start.sp = (void *) c_idle.idle->thread.sp;
+	stack_start  = c_idle.idle->thread.sp;
 
 	/* start_ip had better be page-aligned! */
 	start_ip = setup_trampoline();
Signature · 2 lines
Kees Cook
Ubuntu Security Team
#24 lore raw

Re: [tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

Quoted 8 lines · Kees Cook

Hi Peter,

This works for me doing CPU hotplugging on ia32, but on x86_64 it hangs. (Both boot, though.)

Thanks,

-Kees

I can't reproduce the failure here, and it's hard to see how this patch would make a significant change on 64 bits. Could you try reverting *only* the head_64.S hunk and try it on your platform again?

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#25 lore raw

Re: [tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

Quoted 7 lines · H. Peter Anvin
Quoted 2 lines · Kees Cook

This works for me doing CPU hotplugging on ia32, but on x86_64 it hangs. (Both boot, though.)

I can't reproduce the failure here, and it's hard to see how this patch would make a significant change on 64 bits. Could you try reverting *only* the head_64.S hunk and try it on your platform again?

With the head_64.S hunk dropped, it runs fine. My highly fine-tuned test-case is:

while :; do ls -la / -R >/dev/null 2>/dev/null; done &
while :; do echo 0 > /sys/devices/system/cpu/cpu1/online ; echo 1 > /sys/devices/system/cpu/cpu1/online ; done &

Looks good from this end. Thanks!

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#26 lore raw

Re: [tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

Quoted 9 lines · Kees Cook
Quoted 7 lines · H. Peter Anvin
Quoted 2 lines · Kees Cook

This works for me doing CPU hotplugging on ia32, but on x86_64 it hangs. (Both boot, though.)

I can't reproduce the failure here, and it's hard to see how this patch would make a significant change on 64 bits. Could you try reverting *only* the head_64.S hunk and try it on your platform again?

Quoted 8 lines

With the head_64.S hunk dropped, it runs fine. My highly fine-tuned test-case is:

while :; do ls -la / -R >/dev/null 2>/dev/null; done &
while :; do echo 0 > /sys/devices/system/cpu/cpu1/online ; echo 1 > /sys/devices/system/cpu/cpu1/online ; done &

Looks good from this end. Thanks!

OK, that bothers me, because it would seem that something is accessing data it shouldn't, but ok, it fixes it for now. I'll update it in -tip.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#27 lore raw

[tip:x86/urgent] x86-32: Make sure the stack is set up before we use it

In reply to matthieu castet
Commit-ID:  11d4c3f9b671720e80353dd7e433ff2bf65e9500
Gitweb:     http://git.kernel.org/tip/11d4c3f9b671720e80353dd7e433ff2bf65e9500
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Fri, 4 Feb 2011 16:14:11 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Fri, 4 Feb 2011 22:27:28 -0800

x86-32: Make sure the stack is set up before we use it

Since checkin ebba638ae723d8a8fc2f7abce5ec18b688b791d7 we call verify_cpu even in 32-bit mode. Unfortunately, calling a function means using the stack, and the stack pointer was not initialized in the 32-bit setup code! This code initializes the stack pointer, and simplifies the interface slightly since it is easier to rely on just a pointer value rather than a descriptor; we need to have different values for the segment register anyway.

This retains start_stack as a virtual address, even though a physical address would be more convenient for 32 bits; the 64-bit code wants the other way around...

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/smp.h   |    5 +----
 arch/x86/kernel/acpi/sleep.c |    2 +-
 arch/x86/kernel/head_32.S    |   30 +++++++++++++-----------------
 arch/x86/kernel/smpboot.c    |    4 ++--
 4 files changed, 17 insertions(+), 24 deletions(-)
arch/x86/include/asm/smp.h +1 -4
index 4c2f63c..1f46951 100644
@@ -40,10 +40,7 @@ DECLARE_EARLY_PER_CPU(u16, x86_cpu_to_apicid);
 DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
 
 /* Static state in head.S used to set up a CPU */
-extern struct {
-	void *sp;
-	unsigned short ss;
-} stack_start;
+extern unsigned long stack_start; /* Initial stack pointer address */
 
 struct smp_ops {
 	void (*smp_prepare_boot_cpu)(void);
arch/x86/kernel/acpi/sleep.c +1 -1
index 69fd72a..4d9ebba 100644
@@ -100,7 +100,7 @@ int acpi_save_state_mem(void)
 #else /* CONFIG_64BIT */
 	header->trampoline_segment = setup_trampoline() >> 4;
 #ifdef CONFIG_SMP
-	stack_start.sp = temp_stack + sizeof(temp_stack);
+	stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
 	early_gdt_descr.address =
 			(unsigned long)get_cpu_gdt_table(smp_processor_id());
 	initial_gs = per_cpu_offset(smp_processor_id());
arch/x86/kernel/head_32.S +13 -17
index fc293dc..767d6c4 100644
@@ -85,6 +85,8 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  */
 __HEAD
 ENTRY(startup_32)
+	movl pa(stack_start),%ecx
+	
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
 		us to not reload segments */
 	testb $(1<<6), BP_loadflags(%esi)
@@ -99,7 +101,9 @@ ENTRY(startup_32)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl %eax,%ss
 2:
+	leal -__PAGE_OFFSET(%ecx),%esp
 
 /*
  * Clear BSS first so that there are no surprises...
@@ -145,8 +149,6 @@ ENTRY(startup_32)
  * _brk_end is set up to point to the first "safe" location.
  * Mappings are created both at virtual address 0 (identity mapping)
  * and PAGE_OFFSET for up to _end.
- *
- * Note that the stack is not yet set up!
  */
 #ifdef CONFIG_X86_PAE
 
@@ -282,6 +284,9 @@ ENTRY(startup_32_smp)
 	movl %eax,%es
 	movl %eax,%fs
 	movl %eax,%gs
+	movl pa(stack_start),%ecx
+	movl %eax,%ss
+	leal -__PAGE_OFFSET(%ecx),%esp
 #endif /* CONFIG_SMP */
 default_entry:
 
@@ -347,8 +352,8 @@ default_entry:
 	movl %eax,%cr0		/* ..and set paging (PG) bit */
 	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
 1:
-	/* Set up the stack pointer */
-	lss stack_start,%esp
+	/* Shift the stack pointer to a virtual address */
+	addl $__PAGE_OFFSET, %esp
 
 /*
  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
@@ -360,9 +365,7 @@ default_entry:
 
 #ifdef CONFIG_SMP
 	cmpb $0, ready
-	jz  1f				/* Initial CPU cleans BSS */
-	jmp checkCPUtype
-1:
+	jnz checkCPUtype
 #endif /* CONFIG_SMP */
 
 /*
@@ -470,14 +473,7 @@ is386:	movl $2,%ecx		# set MP
 
 	cld			# gcc2 wants the direction flag cleared at all times
 	pushl $0		# fake return address for unwinder
-#ifdef CONFIG_SMP
-	movb ready, %cl
 	movb $1, ready
-	cmpb $0,%cl		# the first CPU calls start_kernel
-	je   1f
-	movl (stack_start), %esp
-1:
-#endif /* CONFIG_SMP */
 	jmp *(initial_code)
 
 /*
@@ -670,15 +666,15 @@ ENTRY(initial_page_table)
 #endif
 
 .data
+.balign 4
 ENTRY(stack_start)
 	.long init_thread_union+THREAD_SIZE
-	.long __BOOT_DS
-
-ready:	.byte 0
 
 early_recursion_flag:
 	.long 0
 
+ready:	.byte 0
+
 int_msg:
 	.asciz "Unknown interrupt or fault at: %p %p %p\n"
 
arch/x86/kernel/smpboot.c +2 -2
index 0cbe8c0..03273b6 100644
@@ -638,7 +638,7 @@ wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
 	 * target processor state.
 	 */
 	startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
-			 (unsigned long)stack_start.sp);
+			 stack_start);
 
 	/*
 	 * Run STARTUP IPI loop.
@@ -785,7 +785,7 @@ do_rest:
 #endif
 	early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
 	initial_code = (unsigned long)start_secondary;
-	stack_start.sp = (void *) c_idle.idle->thread.sp;
+	stack_start  = c_idle.idle->thread.sp;
 
 	/* start_ip had better be page-aligned! */
 	start_ip = setup_trampoline();