1 /*
2  * Copyright (c) 2020 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_ARCH_ARM64_THREAD_STACK_H_
8 #define ZEPHYR_INCLUDE_ARCH_ARM64_THREAD_STACK_H_
9 
10 #include <zephyr/arch/arm64/mm.h>
11 
12 #define ARCH_STACK_PTR_ALIGN			16
13 
14 #if defined(CONFIG_USERSPACE) || defined(CONFIG_ARM64_STACK_PROTECTION)
15 #define Z_ARM64_STACK_BASE_ALIGN		MEM_DOMAIN_ALIGN_AND_SIZE
16 #define Z_ARM64_STACK_SIZE_ALIGN		MEM_DOMAIN_ALIGN_AND_SIZE
17 #else
18 #define Z_ARM64_STACK_BASE_ALIGN		ARCH_STACK_PTR_ALIGN
19 #define Z_ARM64_STACK_SIZE_ALIGN		ARCH_STACK_PTR_ALIGN
20 #endif
21 
22 #if defined(CONFIG_ARM64_STACK_PROTECTION)
23 #define Z_ARM64_STACK_GUARD_SIZE		MEM_DOMAIN_ALIGN_AND_SIZE
24 #define Z_ARM64_K_STACK_BASE_ALIGN		MEM_DOMAIN_ALIGN_AND_SIZE
25 #else
26 #define Z_ARM64_STACK_GUARD_SIZE		0
27 #define Z_ARM64_K_STACK_BASE_ALIGN		ARCH_STACK_PTR_ALIGN
28 #endif
29 
30 /*
31  * [ see also comments in arch/arm64/core/thread.c ]
32  *
33  * High memory addresses
34  *
35  * +-------------------+ <- thread.stack_info.start + thread.stack_info.size
36  * |       TLS         |
37  * +-------------------+ <- initial sp (computable with thread.stack_info.delta)
38  * |                   |
39  * |    Used stack     |
40  * |                   |
41  * +...................+ <- thread's current stack pointer
42  * |                   |
43  * |   Unused stack    |
44  * |                   |
45  * +-------------------+ <- thread.stack_info.start
46  * | Privileged stack  | } K_(THREAD|KERNEL)_STACK_RESERVED
47  * +-------------------+ <- thread stack limit (update on every context switch)
48  * |    Stack guard    | } Z_ARM64_STACK_GUARD_SIZE (protected by MMU/MPU)
49  * +-------------------+ <- thread.stack_obj
50  *
51  * Low Memory addresses
52  */
53 
54 /* thread stack */
55 #define ARCH_THREAD_STACK_OBJ_ALIGN(size)	Z_ARM64_STACK_BASE_ALIGN
56 #define ARCH_THREAD_STACK_SIZE_ADJUST(size)	\
57 	ROUND_UP((size), Z_ARM64_STACK_SIZE_ALIGN)
58 #define ARCH_THREAD_STACK_RESERVED		CONFIG_PRIVILEGED_STACK_SIZE + \
59 	Z_ARM64_STACK_GUARD_SIZE
60 
61 /* kernel stack */
62 #define ARCH_KERNEL_STACK_RESERVED		Z_ARM64_STACK_GUARD_SIZE
63 #define ARCH_KERNEL_STACK_OBJ_ALIGN		Z_ARM64_K_STACK_BASE_ALIGN
64 
65 #ifndef _ASMLANGUAGE
66 
67 struct z_arm64_thread_stack_header {
68 	char privilege_stack[CONFIG_PRIVILEGED_STACK_SIZE];
69 } __packed __aligned(Z_ARM64_STACK_BASE_ALIGN);
70 
71 #endif /* _ASMLANGUAGE */
72 
73 #endif /* ZEPHYR_INCLUDE_ARCH_ARM64_THREAD_STACK_H_ */
74