Zephyr Project API  3.2.0
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 <zephyr/arch/cpu.h>
26#include <zephyr/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
124#define K_KERNEL_STACK_DECLARE(sym, size) \
125 extern struct z_thread_stack_element \
126 sym[Z_KERNEL_STACK_SIZE_ADJUST(size)]
127
138#define K_KERNEL_STACK_ARRAY_DECLARE(sym, nmemb, size) \
139 extern struct z_thread_stack_element \
140 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
141
152#define K_KERNEL_PINNED_STACK_ARRAY_DECLARE(sym, nmemb, size) \
153 extern struct z_thread_stack_element \
154 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
155
166#define K_KERNEL_STACK_EXTERN(sym) __DEPRECATED_MACRO \
167 extern k_thread_stack_t sym[]
168
181#define K_KERNEL_STACK_ARRAY_EXTERN(sym, nmemb, size) __DEPRECATED_MACRO \
182 extern struct z_thread_stack_element \
183 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
184
197#define K_KERNEL_PINNED_STACK_ARRAY_EXTERN(sym, nmemb, size) __DEPRECATED_MACRO \
198 extern struct z_thread_stack_element \
199 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
200
220#define Z_KERNEL_STACK_DEFINE_IN(sym, size, lsect) \
221 struct z_thread_stack_element lsect \
222 __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
223 sym[Z_KERNEL_STACK_SIZE_ADJUST(size)]
224
233#define Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, lsect) \
234 struct z_thread_stack_element lsect \
235 __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
236 sym[nmemb][Z_KERNEL_STACK_LEN(size)]
237
259#define K_KERNEL_STACK_DEFINE(sym, size) \
260 Z_KERNEL_STACK_DEFINE_IN(sym, size, __kstackmem)
261
274#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
275#define K_KERNEL_PINNED_STACK_DEFINE(sym, size) \
276 Z_KERNEL_STACK_DEFINE_IN(sym, size, __pinned_noinit)
277#else
278#define K_KERNEL_PINNED_STACK_DEFINE(sym, size) \
279 Z_KERNEL_STACK_DEFINE_IN(sym, size, __kstackmem)
280#endif
281
291#define K_KERNEL_STACK_ARRAY_DEFINE(sym, nmemb, size) \
292 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __kstackmem)
293
307#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
308#define K_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
309 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __pinned_noinit)
310#else
311#define K_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
312 Z_KERNEL_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __kstackmem)
313#endif
314
324#define K_KERNEL_STACK_MEMBER(sym, size) \
325 Z_KERNEL_STACK_DEFINE_IN(sym, size,)
326
327#define K_KERNEL_STACK_SIZEOF(sym) (sizeof(sym) - K_KERNEL_STACK_RESERVED)
328
331static inline char *Z_KERNEL_STACK_BUFFER(k_thread_stack_t *sym)
332{
333 return (char *)sym + K_KERNEL_STACK_RESERVED;
334}
335#ifndef CONFIG_USERSPACE
336#define K_THREAD_STACK_RESERVED K_KERNEL_STACK_RESERVED
337#define K_THREAD_STACK_SIZEOF K_KERNEL_STACK_SIZEOF
338#define K_THREAD_STACK_LEN Z_KERNEL_STACK_LEN
339#define K_THREAD_STACK_DEFINE K_KERNEL_STACK_DEFINE
340#define K_THREAD_STACK_ARRAY_DEFINE K_KERNEL_STACK_ARRAY_DEFINE
341#define K_THREAD_STACK_MEMBER K_KERNEL_STACK_MEMBER
342#define Z_THREAD_STACK_BUFFER Z_KERNEL_STACK_BUFFER
343#define K_THREAD_STACK_DECLARE K_KERNEL_STACK_DECLARE
344#define K_THREAD_STACK_ARRAY_DECLARE K_KERNEL_STACK_ARRAY_DECLARE
345#define K_THREAD_STACK_EXTERN K_KERNEL_STACK_EXTERN
346#define K_THREAD_STACK_ARRAY_EXTERN K_KERNEL_STACK_ARRAY_EXTERN
347#define K_THREAD_PINNED_STACK_DEFINE K_KERNEL_PINNED_STACK_DEFINE
348#define K_THREAD_PINNED_STACK_ARRAY_DEFINE \
349 K_KERNEL_PINNED_STACK_ARRAY_DEFINE
350#else
366#ifdef ARCH_THREAD_STACK_RESERVED
367#define K_THREAD_STACK_RESERVED ((size_t)(ARCH_THREAD_STACK_RESERVED))
368#else
369#define K_THREAD_STACK_RESERVED ((size_t)0U)
370#endif
371
397#if defined(ARCH_THREAD_STACK_OBJ_ALIGN)
398#define Z_THREAD_STACK_OBJ_ALIGN(size) \
399 ARCH_THREAD_STACK_OBJ_ALIGN(Z_THREAD_STACK_SIZE_ADJUST(size))
400#else
401#define Z_THREAD_STACK_OBJ_ALIGN(size) ARCH_STACK_PTR_ALIGN
402#endif /* ARCH_THREAD_STACK_OBJ_ALIGN */
403
430#if defined(ARCH_THREAD_STACK_SIZE_ADJUST)
431#define Z_THREAD_STACK_SIZE_ADJUST(size) \
432 ARCH_THREAD_STACK_SIZE_ADJUST((size) + K_THREAD_STACK_RESERVED)
433#else
434#define Z_THREAD_STACK_SIZE_ADJUST(size) \
435 (ROUND_UP((size), ARCH_STACK_PTR_ALIGN) + K_THREAD_STACK_RESERVED)
436#endif /* ARCH_THREAD_STACK_SIZE_ADJUST */
437
452#define K_THREAD_STACK_DECLARE(sym, size) \
453 extern struct z_thread_stack_element \
454 sym[Z_THREAD_STACK_SIZE_ADJUST(size)]
455
466#define K_THREAD_STACK_ARRAY_DECLARE(sym, nmemb, size) \
467 extern struct z_thread_stack_element \
468 sym[nmemb][K_THREAD_STACK_LEN(size)]
469
480#define K_THREAD_STACK_EXTERN(sym) __DEPRECATED_MACRO \
481 extern k_thread_stack_t sym[]
482
495#define K_THREAD_STACK_ARRAY_EXTERN(sym, nmemb, size) __DEPRECATED_MACRO \
496 extern struct z_thread_stack_element \
497 sym[nmemb][K_THREAD_STACK_LEN(size)]
498
513#define K_THREAD_STACK_SIZEOF(sym) (sizeof(sym) - K_THREAD_STACK_RESERVED)
514
543#define Z_THREAD_STACK_DEFINE_IN(sym, size, lsect) \
544 struct z_thread_stack_element lsect \
545 __aligned(Z_THREAD_STACK_OBJ_ALIGN(size)) \
546 sym[Z_THREAD_STACK_SIZE_ADJUST(size)]
547
562#define Z_THREAD_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, lsect) \
563 struct z_thread_stack_element lsect \
564 __aligned(Z_THREAD_STACK_OBJ_ALIGN(size)) \
565 sym[nmemb][K_THREAD_STACK_LEN(size)]
566
593#define K_THREAD_STACK_DEFINE(sym, size) \
594 Z_THREAD_STACK_DEFINE_IN(sym, size, __stackmem)
595
626#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
627#define K_THREAD_PINNED_STACK_DEFINE(sym, size) \
628 Z_THREAD_STACK_DEFINE_IN(sym, size, __pinned_noinit)
629#else
630#define K_THREAD_PINNED_STACK_DEFINE(sym, size) \
631 K_THREAD_STACK_DEFINE(sym, size)
632#endif
633
647#define K_THREAD_STACK_LEN(size) \
648 ROUND_UP(Z_THREAD_STACK_SIZE_ADJUST(size), \
649 Z_THREAD_STACK_OBJ_ALIGN(size))
650
664#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
665 Z_THREAD_STACK_ARRAY_DEFINE_IN(sym, nmemb, size, __stackmem)
666
684#if defined(CONFIG_LINKER_USE_PINNED_SECTION)
685#define K_THREAD_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
686 Z_THREAD_PINNED_STACK_DEFINE_IN(sym, nmemb, size, __pinned_noinit)
687#else
688#define K_THREAD_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
689 K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
690#endif
691
710#define K_THREAD_STACK_MEMBER(sym, size) \
711 Z_THREAD_STACK_DEFINE_IN(sym, size,)
712
729static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
730{
731 return (char *)sym + K_THREAD_STACK_RESERVED;
732}
733
734#endif /* CONFIG_USERSPACE */
735
736#ifdef __cplusplus
737}
738#endif
739
740#endif /* _ASMLANGUAGE */
741#endif /* ZEPHYR_INCLUDE_SYS_THREAD_STACK_H */
#define ARCH_STACK_PTR_ALIGN
Definition: thread_stack.h:11
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:225
#define K_THREAD_STACK_RESERVED
Indicate how much additional memory is reserved for stack objects.
Definition: thread_stack.h:369
#define K_KERNEL_STACK_RESERVED
Definition: thread_stack.h:94
void * ptr
Definition: printk.c:111
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Misc utilities.