Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
thread_stack.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
21#ifndef ZEPHYR_INCLUDE_SYS_THREAD_STACK_H
22#define ZEPHYR_INCLUDE_SYS_THREAD_STACK_H
23
24#if !defined(_ASMLANGUAGE)
25#include <arch/cpu.h>
26#include <sys/util.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* Using typedef deliberately here, this is quite intended to be an opaque
33 * type.
34 *
35 * The purpose of this data type is to clearly distinguish between the
36 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
37 * buffer which composes the stack data actually used by the underlying
38 * thread; they cannot be used interchangeably as some arches precede the
39 * stack buffer region with guard areas that trigger a MPU or MMU fault
40 * if written to.
41 *
42 * APIs that want to work with the buffer inside should continue to use
43 * char *.
44 *
45 * Stacks should always be created with K_THREAD_STACK_DEFINE().
46 */
47struct __packed z_thread_stack_element {
48 char data;
49};
50
69static inline char *z_stack_ptr_align(char *ptr)
70{
71 return (char *)ROUND_DOWN(ptr, ARCH_STACK_PTR_ALIGN);
72}
73#define Z_STACK_PTR_ALIGN(ptr) ((uintptr_t)z_stack_ptr_align((char *)(ptr)))
74
88#define Z_STACK_PTR_TO_FRAME(type, ptr) \
89 (type *)((ptr) - sizeof(type))
90
91#ifdef ARCH_KERNEL_STACK_RESERVED
92#define K_KERNEL_STACK_RESERVED ((size_t)ARCH_KERNEL_STACK_RESERVED)
93#else
94#define K_KERNEL_STACK_RESERVED ((size_t)0)
95#endif
96
97#define Z_KERNEL_STACK_SIZE_ADJUST(size) (ROUND_UP(size, \
98 ARCH_STACK_PTR_ALIGN) + \
99 K_KERNEL_STACK_RESERVED)
100
101#ifdef ARCH_KERNEL_STACK_OBJ_ALIGN
102#define Z_KERNEL_STACK_OBJ_ALIGN ARCH_KERNEL_STACK_OBJ_ALIGN
103#else
104#define Z_KERNEL_STACK_OBJ_ALIGN ARCH_STACK_PTR_ALIGN
105#endif
106
107#define Z_KERNEL_STACK_LEN(size) \
108 ROUND_UP(Z_KERNEL_STACK_SIZE_ADJUST(size), Z_KERNEL_STACK_OBJ_ALIGN)
109
118#define K_KERNEL_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
119
136#define K_KERNEL_STACK_ARRAY_EXTERN(sym, nmemb, size) \
137 extern struct z_thread_stack_element \
138 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
139
151#define K_KERNEL_PINNED_STACK_ARRAY_EXTERN(sym, nmemb, size) \
152 extern struct z_thread_stack_element \
153 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
154
175#define Z_KERNEL_STACK_DEFINE_IN(sym, size, lsect) \
176 struct z_thread_stack_element lsect \
177 __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
178 sym[Z_KERNEL_STACK_SIZE_ADJUST(size)]
179
189#define Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, lsect) \
190 struct z_thread_stack_element lsect \
191 __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
192 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
193
216#define K_KERNEL_STACK_DEFINE(sym, size) \
217 Z_KERNEL_STACK_DEFINE_IN(sym, size, __kstackmem)
218
232#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
233#define K_KERNEL_PINNED_STACK_DEFINE(sym, size) \
234 Z_KERNEL_STACK_DEFINE_IN(sym, size, __pinned_noinit)
235#else
236#define K_KERNEL_PINNED_STACK_DEFINE(sym, size) \
237 Z_KERNEL_STACK_DEFINE_IN(sym, size, __kstackmem)
238#endif
239
250#define K_KERNEL_STACK_ARRAY_DEFINE(sym, nmemb, size) \
251 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __kstackmem)
252
267#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
268#define K_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
269 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __pinned_noinit)
270#else
271#define K_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
272 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __kstackmem)
273#endif
274
285#define K_KERNEL_STACK_MEMBER(sym, size) \
286 Z_KERNEL_STACK_DEFINE_IN(sym, size,)
287
288#define K_KERNEL_STACK_SIZEOF(sym) (sizeof(sym) - K_KERNEL_STACK_RESERVED)
289
292static inline char *Z_KERNEL_STACK_BUFFER(k_thread_stack_t *sym)
293{
294 return (char *)sym + K_KERNEL_STACK_RESERVED;
295}
296#ifndef CONFIG_USERSPACE
297#define K_THREAD_STACK_RESERVED K_KERNEL_STACK_RESERVED
298#define K_THREAD_STACK_SIZEOF K_KERNEL_STACK_SIZEOF
299#define K_THREAD_STACK_LEN Z_KERNEL_STACK_LEN
300#define K_THREAD_STACK_DEFINE K_KERNEL_STACK_DEFINE
301#define K_THREAD_STACK_ARRAY_DEFINE K_KERNEL_STACK_ARRAY_DEFINE
302#define K_THREAD_STACK_MEMBER K_KERNEL_STACK_MEMBER
303#define Z_THREAD_STACK_BUFFER Z_KERNEL_STACK_BUFFER
304#define K_THREAD_STACK_EXTERN K_KERNEL_STACK_EXTERN
305#define K_THREAD_STACK_ARRAY_EXTERN K_KERNEL_STACK_ARRAY_EXTERN
306#define K_THREAD_PINNED_STACK_DEFINE K_KERNEL_PINNED_STACK_DEFINE
307#define K_THREAD_PINNED_STACK_ARRAY_DEFINE \
308 K_KERNEL_PINNED_STACK_ARRAY_DEFINE
309#else
326#ifdef ARCH_THREAD_STACK_RESERVED
327#define K_THREAD_STACK_RESERVED ((size_t)(ARCH_THREAD_STACK_RESERVED))
328#else
329#define K_THREAD_STACK_RESERVED ((size_t)0U)
330#endif
331
357#if defined(ARCH_THREAD_STACK_OBJ_ALIGN)
358#define Z_THREAD_STACK_OBJ_ALIGN(size) ARCH_THREAD_STACK_OBJ_ALIGN(size)
359#else
360#define Z_THREAD_STACK_OBJ_ALIGN(size) ARCH_STACK_PTR_ALIGN
361#endif /* ARCH_THREAD_STACK_OBJ_ALIGN */
362
390#if defined(ARCH_THREAD_STACK_SIZE_ADJUST)
391#define Z_THREAD_STACK_SIZE_ADJUST(size) \
392 (ARCH_THREAD_STACK_SIZE_ADJUST(size) + K_THREAD_STACK_RESERVED)
393#else
394#define Z_THREAD_STACK_SIZE_ADJUST(size) \
395 (ROUND_UP((size), ARCH_STACK_PTR_ALIGN) + K_THREAD_STACK_RESERVED)
396#endif /* ARCH_THREAD_STACK_SIZE_ADJUST */
397
406#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
407
418#define K_THREAD_STACK_ARRAY_EXTERN(sym, nmemb, size) \
419 extern struct z_thread_stack_element \
420 sym[nmemb][K_THREAD_STACK_LEN(size)]
421
441#define K_THREAD_STACK_SIZEOF(sym) (sizeof(sym) - K_THREAD_STACK_RESERVED)
442
471#define Z_THREAD_STACK_DEFINE_IN(sym, size, lsect) \
472 struct z_thread_stack_element lsect \
473 __aligned(Z_THREAD_STACK_OBJ_ALIGN(size)) \
474 sym[Z_THREAD_STACK_SIZE_ADJUST(size)]
475
490#define Z_THREAD_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, lsect) \
491 struct z_thread_stack_element lsect \
492 __aligned(Z_THREAD_STACK_OBJ_ALIGN(size)) \
493 sym[nmemb][K_THREAD_STACK_LEN(size)]
494
521#define K_THREAD_STACK_DEFINE(sym, size) \
522 Z_THREAD_STACK_DEFINE_IN(sym, size, __stackmem)
523
554#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
555#define K_THREAD_PINNED_STACK_DEFINE(sym, size) \
556 Z_THREAD_STACK_DEFINE_IN(sym, size, __pinned_noinit)
557#else
558#define K_THREAD_PINNED_STACK_DEFINE(sym, size) \
559 K_THREAD_STACK_DEFINE(sym, size)
560#endif
561
575#define K_THREAD_STACK_LEN(size) \
576 ROUND_UP(Z_THREAD_STACK_SIZE_ADJUST(size), \
577 Z_THREAD_STACK_OBJ_ALIGN(Z_THREAD_STACK_SIZE_ADJUST(size)))
578
592#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
593 Z_THREAD_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __stackmem)
594
612#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
613#define K_THREAD_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
614 Z_THREAD_PINNED_STACK_DEFINE_IN(sym, nmemb, size, __pinned_noinit)
615#else
616#define K_THREAD_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
617 K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
618#endif
619
638#define K_THREAD_STACK_MEMBER(sym, size) \
639 Z_THREAD_STACK_DEFINE_IN(sym, size,)
640
657static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
658{
659 return (char *)sym + K_THREAD_STACK_RESERVED;
660}
661
662#endif /* CONFIG_USERSPACE */
663
664#ifdef __cplusplus
665}
666#endif
667
668#endif /* _ASMLANGUAGE */
669#endif /* ZEPHYR_INCLUDE_SYS_THREAD_STACK_H */
#define ARCH_STACK_PTR_ALIGN
Definition: thread_stack.h:10
struct z_thread_stack_element k_thread_stack_t
Typedef of struct z_thread_stack_element.
Definition: arch_interface.h:44
#define ROUND_DOWN(x, align)
Value of x rounded down to the previous multiple of align, which must be a power of 2.
Definition: util.h:146
#define K_THREAD_STACK_RESERVED
Indicate how much additional memory is reserved for stack objects.
Definition: thread_stack.h:329
#define K_KERNEL_STACK_RESERVED
Definition: thread_stack.h:94
void * ptr
Definition: printk.c:79
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Misc utilities.