All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fixing stack alignment in x86-64 Xen
@ 2005-05-18  2:25 Nakajima, Jun
  2005-05-18  5:07 ` Scott Parish
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Nakajima, Jun @ 2005-05-18  2:25 UTC (permalink / raw)
  To: Xen Development List

[-- Attachment #1: Type: text/plain, Size: 239 bytes --]

Long mode needs to align the stack on a 16-byte boundary. Recent changes
to Xen broke the requirement, and x86-64 XenLinux stopped booting. The
attached fixes the problem. 

Signed-off-by: Jun Nakajima <jun.nakajima@intel.com>

Jun

[-- Attachment #2: stack_alignment.patch --]
[-- Type: application/octet-stream, Size: 1555 bytes --]

diff -ur xen-unstable.orig/xen/include/asm-x86/x86_64/current.h xen-unstable/xen/include/asm-x86/x86_64/current.h
--- xen-unstable.orig/xen/include/asm-x86/x86_64/current.h	2005-05-16 20:27:42.000000000 -0700
+++ xen-unstable/xen/include/asm-x86/x86_64/current.h	2005-05-17 18:53:25.264794104 -0700
@@ -34,15 +34,16 @@
 
 /*
  * Get the bottom-of-stack, as stored in the per-CPU TSS. This is actually
- * 40 bytes before the real bottom of the stack to allow space for:
- *  domain pointer, DS, ES, FS, GS
+ * 48 bytes before the real bottom of the stack to allow space for:
+ * domain pointer, padding, DS, ES, FS, GS. The padding is required to
+ * have the stack pointer 16-byte aligned.
  */
 static inline unsigned long get_stack_bottom(void)
 {
     unsigned long p;
     __asm__( "andq %%rsp,%0; addq %2,%0"
 	    : "=r" (p)
-	    : "0" (~(STACK_SIZE-1)), "i" (STACK_SIZE-40) );
+	    : "0" (~(STACK_SIZE-1)), "i" (STACK_SIZE-48) );
     return p;
 }
 
diff -ur xen-unstable.orig/xen/include/public/arch-x86_64.h xen-unstable/xen/include/public/arch-x86_64.h
--- xen-unstable.orig/xen/include/public/arch-x86_64.h	2005-05-16 20:27:43.000000000 -0700
+++ xen-unstable/xen/include/public/arch-x86_64.h	2005-05-17 18:51:32.350959616 -0700
@@ -169,6 +169,7 @@
     u64 ds;
     u64 fs;      /* Non-zero => takes precedence over fs_base.      */
     u64 gs;      /* Non-zero => takes precedence over gs_base_user. */
+    u64 padding; /* Get the stack bottom 16-byte aligned */
 } cpu_user_regs_t;
 
 typedef u64 tsc_timestamp_t; /* RDTSC timestamp */

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: [PATCH] Fixing stack alignment in x86-64 Xen
@ 2005-05-18  7:36 Ian Pratt
  0 siblings, 0 replies; 10+ messages in thread
From: Ian Pratt @ 2005-05-18  7:36 UTC (permalink / raw)
  To: Keir Fraser, Nakajima, Jun; +Cc: Xen Development List

> > Long mode needs to align the stack on a 16-byte boundary. Recent 
> > changes to Xen broke the requirement, and x86-64 XenLinux stopped 
> > booting. The attached fixes the problem.
> >
> > Signed-off-by: Jun Nakajima <jun.nakajima@intel.com>
> 
> Thanks Jun, that was a nasty bug to have to find. I keep 
> forgetting about that annoying automatic 16-byte alignment...

Let's put an assert in there.

Ian

^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: [PATCH] Fixing stack alignment in x86-64 Xen
@ 2005-05-18 15:09 Nakajima, Jun
  2005-05-18 15:32 ` Andi Kleen
  0 siblings, 1 reply; 10+ messages in thread
From: Nakajima, Jun @ 2005-05-18 15:09 UTC (permalink / raw)
  To: Andi Kleen; +Cc: xen-devel

Andi Kleen wrote:
> "Nakajima, Jun" <jun.nakajima@intel.com> writes:
> 
>> Long mode needs to align the stack on a 16-byte boundary. Recent
>> changes to Xen broke the requirement, and x86-64 XenLinux stopped
>> booting. The attached fixes the problem.
> 
> Normally it should be only needed in user space for saving FP
> registers. The kernel and Xen which should not do this probably don't
> need it. 

Normally they don't care. The problem was that Xen set rsp0 in TSS on a
8-byte boundary and upon interrupts in Ring3 (XenLinux or user
processes) the processor started pushing registers (ss, rsp, rflags, ..,
rip) on the 16-byte boundary in Xen.  

The recent optimization reset_stack_and_jump() code needs to know the
exact address of the interrupt stack (because it resets %rsp), and
calculates it based on the value that Xen set (i.e. 8-byte boundary).
Since the processor forces the rsp0 on a 16-byte boundary (i.e. moves it
down by 8 bytes), Xen sees a wrong stack when returning from the
interrupt.

Jun

> 
> Unless you save FP registers on the stack somewhere. Then I would
> rather fix that place only.
> 
> At least the main linux kernel does not try to keep the stack
> always 16byte aligned. There is even a gcc option to turn it off
> and it saves some code (and probably stack) size.
> 
> -Andi

^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: [PATCH] Fixing stack alignment in x86-64 Xen
@ 2005-05-18 15:57 Nakajima, Jun
  2005-05-18 16:14 ` Andi Kleen
  0 siblings, 1 reply; 10+ messages in thread
From: Nakajima, Jun @ 2005-05-18 15:57 UTC (permalink / raw)
  To: Andi Kleen; +Cc: xen-devel

Andi Kleen wrote:
> "Nakajima, Jun" <jun.nakajima@intel.com> writes:
>> 
>> The recent optimization reset_stack_and_jump() code needs to know the
>> exact address of the interrupt stack (because it resets %rsp), and
>> calculates it based on the value that Xen set (i.e. 8-byte boundary).
>> Since the processor forces the rsp0 on a 16-byte boundary (i.e.
>> moves it down by 8 bytes), Xen sees a wrong stack when returning
>> from the interrupt.
> 
> I would rather fix reset_stack_and_jump then to do the necessary
> rounding or better look at the original RSP the processor stored into
> the stack frame. Otherwise the 16 byte alignment will probably bite
> you later again.
> 
> -Andi

I think the right thing is to get rsp0 in TSS on a 16-byte boundary by
getting get_stack_bottom() and get_cpu_user_regs() see the correct
stack. That will fix the reset_stack_and_jump() as well. It's basically
what my patch does. 

Jun

^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: [PATCH] Fixing stack alignment in x86-64 Xen
@ 2005-05-18 16:38 Nakajima, Jun
  0 siblings, 0 replies; 10+ messages in thread
From: Nakajima, Jun @ 2005-05-18 16:38 UTC (permalink / raw)
  To: Andi Kleen; +Cc: xen-devel

Andi Kleen wrote:
>> I think the right thing is to get rsp0 in TSS on a 16-byte boundary
>> by getting get_stack_bottom() and get_cpu_user_regs() see the correct
>> stack. That will fix the reset_stack_and_jump() as well. It's
>> basically what my patch does.
> 
> This means you cannot disable the 16 byte stack alignment in gcc.
> Probably does not matter too much today (I guess Xen is not that
> bad a stack pig), but in the far future it might come in useful.
> Also it would generate smaller code.
> 
> -Andi

I don't think that's correct. If you look at how they calculate the
stack pointer, fortunately they depend only on STACK_RESERVED and the
magic number 48 (see below). It does not matter if gcc used 16 byte
stack alignment or not because the current RSP will be rounded down to
the 8KB boundary when calculate the stack pointer.
  
#define STACK_RESERVED \
    (sizeof(struct cpu_user_regs) + sizeof(struct domain *))

static inline struct cpu_user_regs *get_cpu_user_regs(void)
{
    struct cpu_user_regs *cpu_user_regs;
    __asm__( "andq %%rsp,%0; addq %2,%0"
	    : "=r" (cpu_user_regs)
	    : "0" (~(STACK_SIZE-1)), "i" (STACK_SIZE-STACK_RESERVED) ); 
    return cpu_user_regs;
}

/*
 * Get the bottom-of-stack, as stored in the per-CPU TSS. This is
actually
 * 48 bytes before the real bottom of the stack to allow space for:
 * domain pointer, padding, DS, ES, FS, GS. The padding is required to
 * have the stack pointer 16-byte aligned.
 */
static inline unsigned long get_stack_bottom(void)
{
    unsigned long p;
    __asm__( "andq %%rsp,%0; addq %2,%0"
	    : "=r" (p)
	    : "0" (~(STACK_SIZE-1)), "i" (STACK_SIZE-48) );
    return p;
}

Jun

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-05-18 16:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-18  2:25 [PATCH] Fixing stack alignment in x86-64 Xen Nakajima, Jun
2005-05-18  5:07 ` Scott Parish
2005-05-18  7:35 ` Keir Fraser
2005-05-18 14:06 ` Andi Kleen
  -- strict thread matches above, loose matches on Subject: below --
2005-05-18  7:36 Ian Pratt
2005-05-18 15:09 Nakajima, Jun
2005-05-18 15:32 ` Andi Kleen
2005-05-18 15:57 Nakajima, Jun
2005-05-18 16:14 ` Andi Kleen
2005-05-18 16:38 Nakajima, Jun

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.