Skip to content

Commit b92e0b6

Browse files
wangchdoacassis
authored andcommitted
arch: fix alignment bug for archs that need stack alignment
These archs only align the size of the stack, forgeting to do the stack start addr alignment, this patch fixes it. Signed-off-by: Chengdong Wang <[email protected]>
1 parent bd28635 commit b92e0b6

File tree

17 files changed

+81
-318
lines changed

17 files changed

+81
-318
lines changed

arch/arm64/src/common/arm64_usestack.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484

8585
int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
8686
{
87+
uintptr_t top_of_stack;
88+
size_t size_of_stack;
89+
8790
#ifdef CONFIG_TLS_ALIGNED
8891
/* Make certain that the user provided stack is properly aligned */
8992

@@ -115,9 +118,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
115118
/* Save the new stack allocation */
116119

117120
tcb->stack_alloc_ptr = stack;
118-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
119-
tcb->adj_stack_size =
120-
STACK_ALIGN_DOWN((uintptr_t)stack + stack_size) - (uintptr_t)stack;
121+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
122+
123+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
124+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
125+
tcb->adj_stack_size = size_of_stack;
121126

122127
#ifdef CONFIG_STACK_COLORATION
123128
/* If stack debug is enabled, then fill the stack with a

arch/avr/src/avr32/avr_usestack.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,27 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, STACK_COLOR, stack_size);
103108
#endif
104109

105-
/* The AVR32 uses a push-down stack: the stack grows
106-
* toward loweraddresses in memory. The stack pointer
107-
* register, points to the lowest, valid work address
108-
* (the "top" of the stack). Items on the stack are
109-
* referenced as positive word offsets from sp.
110-
*/
111-
112-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
113-
114-
/* The AVR32 stack must be aligned at word (4 byte)
115-
* boundaries. If necessary top_of_stack must be rounded
116-
* down to the next boundary
117-
*/
118-
119-
top_of_stack &= ~3;
120-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
121-
122-
/* Save the adjusted stack values in the struct tcb_s */
123-
124-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
125-
tcb->adj_stack_size = size_of_stack;
126-
127110
return OK;
128111
}

arch/ceva/src/common/ceva_usestack.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9191
/* Save the new stack allocation */
9292

9393
tcb->stack_alloc_ptr = stack;
94+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
9495

95-
/* The CEVA uses a push-down stack: the stack grows toward lower addresses
96-
* in memory. The stack pointer register, points to the lowest, valid
97-
* work address (the "top" of the stack). Items on the stack are
98-
* referenced as positive word offsets from sp.
99-
*/
100-
101-
/* The CEVA stack must be aligned to 4-byte alignment.
102-
* If necessary size_of_stack must be rounded down to the next
103-
* boundary
104-
*/
105-
106-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
107-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
108-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
109-
110-
/* Save the adjusted stack values in the struct tcb_s */
111-
112-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
113-
tcb->adj_stack_size = size_of_stack;
96+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
97+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
98+
tcb->adj_stack_size = size_of_stack;
11499

115100
#ifdef CONFIG_STACK_COLORATION
116101
/* If stack debug is enabled, then fill the stack with a recognizable

arch/hc/src/common/hc_usestack.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size)
9292
/* Save the new stack allocation */
9393

9494
tcb->stack_alloc_ptr = stack;
95+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
96+
97+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
98+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
99+
tcb->adj_stack_size = size_of_stack;
95100

96101
/* If stack debug is enabled, then fill the stack with a recognizable value
97102
* that we can use later to test for high water marks.
@@ -101,27 +106,5 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size)
101106
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
102107
#endif
103108

104-
/* The CPU12 uses a push-down stack: the stack grows
105-
* toward lower addresses in memory. Because the CPU12 stack
106-
* operates as a decrement then store stack, the value assigned
107-
* to the initial stack pointer is one more than the last valid
108-
* stack address.
109-
*/
110-
111-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
112-
113-
/* The CPU12 stack should be aligned at half-word (2 byte)
114-
* boundaries. If necessary top_of_stack must be rounded
115-
* down to the next boundary
116-
*/
117-
118-
top_of_stack &= ~1;
119-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
120-
121-
/* Save the adjusted stack values in the struct tcb_s */
122-
123-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
124-
tcb->adj_stack_size = size_of_stack;
125-
126109
return OK;
127110
}

arch/mips/src/common/mips_usestack.c

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,33 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
103108
#endif
104109

105-
/* MIPS uses a push-down stack: the stack grows toward loweraddresses in
106-
* memory. The stack pointer register, points to the lowest, valid work
107-
* address (the "top" of the stack). Items on the stack are referenced
108-
* as positive word offsets from sp.
109-
*/
110-
111-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
112-
113-
/* The MIPS stack must be aligned at word (4 byte) or double word (8 byte)
114-
* boundaries. If necessary top_of_stack must be rounded down to the
115-
* next boundary
116-
*/
117-
118-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
119-
120-
/* The size of the stack in bytes is then the difference between
121-
* the top and the bottom of the stack (+4 because if the top
122-
* is the same as the bottom, then the size is one 32-bit element).
123-
* The size need not be aligned.
124-
*/
125-
126-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
127-
128-
/* Save the adjusted stack values in the struct tcb_s */
129-
130-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
131-
tcb->adj_stack_size = size_of_stack;
132-
133110
return OK;
134111
}

arch/misoc/src/lm32/lm32_usestack.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,27 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
103108
#endif
104109

105-
/* LM32 uses a push-down stack: the stack grows toward lower
106-
* addresses in memory. The stack pointer register points to the
107-
* lowest, valid working address (the "top" of the stack). Items on
108-
* the stack are referenced as positive word offsets from sp.
109-
*/
110-
111-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
112-
113-
/* The LM32 stack must be aligned at word (4 byte) boundaries; for
114-
* floating point use, the stack must be aligned to 8-byte addresses.
115-
* If necessary top_of_stack must be rounded down to the next
116-
* boundary to meet these alignment requirements.
117-
*/
118-
119-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
120-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
121-
122-
/* Save the adjusted stack values in the struct tcb_s */
123-
124-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
125-
tcb->adj_stack_size = size_of_stack;
126-
127110
return OK;
128111
}

arch/misoc/src/minerva/minerva_usestack.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,27 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
103108
#endif
104109

105-
/* MINERVA uses a push-down stack: the stack grows toward lower
106-
* addresses in memory. The stack pointer register points to the
107-
* lowest, valid working address (the "top" of the stack). Items on
108-
* the stack are referenced as positive word offsets from sp.
109-
*/
110-
111-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
112-
113-
/* The MINERVA stack must be aligned at word (4 byte) boundaries; for
114-
* floating point use, the stack must be aligned to 8-byte addresses.
115-
* If necessary top_of_stack must be rounded down to the next boundary
116-
* to meet these alignment requirements.
117-
*/
118-
119-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
120-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
121-
122-
/* Save the adjusted stack values in the struct tcb_s */
123-
124-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
125-
tcb->adj_stack_size = size_of_stack;
126-
127110
return OK;
128111
}

arch/or1k/src/common/or1k_usestack.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,14 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
103108
#endif
104109

105-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
106-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
107-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
108-
109-
/* Save the adjusted stack values in the struct tcb_s */
110-
111-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
112-
tcb->adj_stack_size = size_of_stack;
113-
114110
return OK;
115111
}

arch/renesas/src/common/renesas_usestack.c

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
97+
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
96101

97102
/* If stack debug is enabled, then fill the stack with a recognizable value
98103
* that we can use later to test for high water marks.
@@ -102,25 +107,5 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
102107
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
103108
#endif
104109

105-
/* The SH family uses a push-down stack: the stack grows toward lower
106-
* addresses in memory. The stack pointer register, points to the
107-
* lowest, valid work address (the "top" of the stack). Items on the
108-
* stack are referenced as positive word offsets from sp.
109-
*/
110-
111-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
112-
113-
/* The SH stack must be aligned at word (4 byte) boundaries. If necessary
114-
* top_of_stack must be rounded down to the next boundary
115-
*/
116-
117-
top_of_stack &= ~3;
118-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
119-
120-
/* Save the adjusted stack values in the struct tcb_s */
121-
122-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
123-
tcb->adj_stack_size = size_of_stack;
124-
125110
return OK;
126111
}

arch/risc-v/src/common/riscv_usestack.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
9393
/* Save the new stack allocation */
9494

9595
tcb->stack_alloc_ptr = stack;
96+
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
9697

97-
/* RISC-V uses a push-down stack: the stack grows toward lower addresses in
98-
* memory. The stack pointer register, points to the lowest, valid work
99-
* address (the "top" of the stack). Items on the stack are referenced
100-
* as positive word offsets from SP.
101-
*/
102-
103-
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
104-
105-
/* The RISC-V stack must be aligned at 128-bit (16-byte) boundaries.
106-
* If necessary top_of_stack must be rounded down to the next boundary.
107-
*/
108-
109-
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
110-
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
111-
112-
/* Save the adjusted stack values in the struct tcb_s */
113-
114-
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
115-
tcb->adj_stack_size = size_of_stack;
98+
top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
99+
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
100+
tcb->adj_stack_size = size_of_stack;
116101

117102
#if defined(CONFIG_STACK_COLORATION)
118103
/* If stack debug is enabled, then fill the stack with a

0 commit comments

Comments
 (0)