From 83fc7933f968173822ed3f636d75860d4a747e76 Mon Sep 17 00:00:00 2001 From: asterix Date: Wed, 9 Sep 2009 15:31:29 +0000 Subject: [PATCH] Fix stack alineament on new process. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2913 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/types.h | 15 +++++++++++++++ bertos/kern/proc.c | 6 +++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bertos/cpu/types.h b/bertos/cpu/types.h index 8b817938..daeec2ea 100644 --- a/bertos/cpu/types.h +++ b/bertos/cpu/types.h @@ -49,9 +49,11 @@ typedef uint16_t cpu_flags_t; // FIXME typedef unsigned int cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; typedef unsigned int cpu_atomic_t; #warning Verify following constant #define SIZEOF_CPUSTACK_T 2 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #elif CPU_X86 @@ -65,10 +67,14 @@ #if CPU_X86_64 typedef uint64_t cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; #define SIZEOF_CPUSTACK_T 8 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #else typedef uint32_t cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; #define SIZEOF_CPUSTACK_T 4 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #endif #elif CPU_ARM @@ -76,7 +82,9 @@ typedef uint32_t cpu_flags_t; typedef uint32_t cpu_atomic_t; typedef uint32_t cpu_stack_t; + typedef uint64_t cpu_aligned_stack_t; #define SIZEOF_CPUSTACK_T 4 + #define SIZEOF_CPUALIGNED_T 8 #elif CPU_PPC @@ -88,22 +96,28 @@ typedef uint32_t cpu_atomic_t; typedef uint32_t cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; #define SIZEOF_CPUSTACK_T 4 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #elif CPU_DSP56K typedef uint16_t cpu_flags_t; typedef uint16_t cpu_atomic_t; typedef unsigned int cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; #warning Verify following costant #define SIZEOF_CPUSTACK_T 2 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #elif CPU_AVR typedef uint8_t cpu_flags_t; typedef uint8_t cpu_atomic_t; typedef uint8_t cpu_stack_t; + typedef cpu_stack_t cpu_aligned_stack_t; #define SIZEOF_CPUSTACK_T 1 + #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T #else #error No CPU_... defined. @@ -200,6 +214,7 @@ STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64); STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64); #endif STATIC_ASSERT(sizeof(cpu_stack_t) == SIZEOF_CPUSTACK_T); +STATIC_ASSERT(sizeof(cpu_aligned_stack_t) == SIZEOF_CPUALIGNED_T); #endif /* CPU_TYPES_H */ diff --git a/bertos/kern/proc.c b/bertos/kern/proc.c index 25d410df..6b1aaaff 100644 --- a/bertos/kern/proc.c +++ b/bertos/kern/proc.c @@ -202,17 +202,21 @@ struct Process *proc_new_with_name(UNUSED_ARG(const char *, name), void (*entry) { proc = (Process *)stack_base; proc->stack = stack_base + PROC_SIZE_WORDS; + // On some architecture stack should be aligned, so we do it. + proc->stack = (void *)proc->stack + (sizeof(cpu_aligned_stack_t) - ((long)proc->stack % sizeof(cpu_stack_aligned_t))); if (CPU_SP_ON_EMPTY_SLOT) proc->stack++; } else { proc = (Process *)(stack_base + stack_size / sizeof(cpu_stack_t) - PROC_SIZE_WORDS); - proc->stack = (cpu_stack_t *)proc; + // On some architecture stack should be aligned, so we do it. + proc->stack = (void *)proc - ((long)proc % sizeof(cpu_aligned_stack_t)); if (CPU_SP_ON_EMPTY_SLOT) proc->stack--; } + stack_size = stack_size - PROC_SIZE_WORDS; proc_init_struct(proc); proc->user_data = data; -- 2.25.1