diff --git a/arch/x86/amd/stage0.S b/arch/x86/amd/stage0.S index 0191756398..8a87eb8e22 100644 --- a/arch/x86/amd/stage0.S +++ b/arch/x86/amd/stage0.S @@ -23,7 +23,9 @@ #define CacheBase CONFIG_CARBASE #define MEM_TOPK 2048 -/* leave some space for global variable to pass to RAM stage */ +/* Leave some space for a pointer to the global variables. + * This should most likely be 4. + */ #define GlobalVarSize 32 #ifdef CONFIG_CPU_AMD_K10 diff --git a/arch/x86/geodelx/stage0.S b/arch/x86/geodelx/stage0.S index 78b75253f6..56551154b9 100644 --- a/arch/x86/geodelx/stage0.S +++ b/arch/x86/geodelx/stage0.S @@ -361,13 +361,10 @@ DCacheSetupGood: movw %ax, %ss lout: -#ifdef CONFIG_CONSOLE_BUFFER - /* Store pointer to start of printk buffer, should really use - * PRINTK_BUF_ADDR_CAR instead. - */ - movl $CONFIG_CARBASE, %eax - pushl %eax /* printk buffer */ -#endif + /* Store zero for the pointer to the global variables. */ + movl $0, %eax + pushl %eax + /* Restore the BIST result. */ movl %ebp, %eax diff --git a/arch/x86/stage0_i586.S b/arch/x86/stage0_i586.S index 3b0d9b778c..10a650edba 100644 --- a/arch/x86/stage0_i586.S +++ b/arch/x86/stage0_i586.S @@ -435,13 +435,10 @@ clear_fixed_var_mtrr_out: movw %ax, %ss lout: -#ifdef CONFIG_CONSOLE_BUFFER - /* Store pointer to start of printk buffer, should really use - * PRINTK_BUF_ADDR_CAR instead. - */ - movl $CONFIG_CARBASE, %eax - pushl %eax /* printk buffer */ -#endif + /* Store zero for the pointer to the global variables. */ + movl $0, %eax + pushl %eax + /* Restore the BIST result */ movl %ebp, %eax /* We need to set ebp ? No need */ diff --git a/arch/x86/stage1.c b/arch/x86/stage1.c index 922784b5e9..a15943e5cc 100644 --- a/arch/x86/stage1.c +++ b/arch/x86/stage1.c @@ -82,7 +82,13 @@ void *bottom_of_stack(void) struct global_vars *global_vars(void) { - return (struct global_vars *)(bottom_of_stack() - sizeof(struct global_vars)); + return *(struct global_vars **)(bottom_of_stack() - sizeof(struct global_vars *)); +} + +void global_vars_init(struct global_vars *globvars) +{ + memset(globvars, 0, sizeof(struct global_vars)); + *(struct global_vars **)(bottom_of_stack() - sizeof(struct global_vars *)) = globvars; } void dump_mem_range(int msg_level, unsigned char *buf, int size) @@ -119,9 +125,11 @@ int legacy(struct mem_file *archive, char *name, void *where, struct lb_memory * /* * This function is called from assembler code with its argument on the * stack. Force the compiler to generate always correct code for this case. + * We have cache as ram running and can start executing code in C. */ void __attribute__((stdcall)) stage1_main(u32 bist) { + struct global_vars globvars; int ret; struct mem_file archive; void *entry; @@ -150,10 +158,13 @@ void __attribute__((stdcall)) stage1_main(u32 bist) stop_ap(); } - // We have cache as ram running and can start executing code in C. + /* Initialize global variables before we can think of using them. + * NEVER run this on an AP! + */ + global_vars_init(&globvars); #ifdef CONFIG_CONSOLE_BUFFER - /* Initialize the printk buffer. */ + /* Initialize the printk buffer. NEVER run this on an AP! */ printk_buffer_init(); #endif diff --git a/include/console.h b/include/console.h index dca1db9d3d..a5d87164f2 100644 --- a/include/console.h +++ b/include/console.h @@ -60,10 +60,10 @@ struct printk_buffer { #endif /* - * If you change struct global_vars in any way, you have to fix all stage0 asm - * code. The stage0 asm code modification is nontrivial (size of the struct, - * alignment, initialization, order of struct members, initialization). - * Depending on your compiler, real breakage may happen. + * struct global_vars is managed entirely from C code. Keep in mind that there + * is NO buffer at the end of the struct, so having zero-sized arrays at the + * end or similar stuff for which the compiler can't determine the final size + * will corrupt memory. If you don't try to be clever, everything will be fine. */ struct global_vars { #ifdef CONFIG_CONSOLE_BUFFER