blob: adebb2d68e80f515aa4a7f128aedd44346db96ce [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2010-2016 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @brief fixed-size stack object
9 */
10
Flavio Ceolin11b85ee2024-03-14 16:32:07 -070011#include <zephyr/sys/math_extras.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020012#include <zephyr/kernel.h>
13#include <zephyr/kernel_structs.h>
Anas Nashif4d994af2021-04-18 23:24:40 -040014
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020015#include <zephyr/toolchain.h>
Benjamin Walshb4b108d2016-10-13 10:31:48 -040016#include <ksched.h>
Anas Nashif8634c3b2023-08-29 17:03:12 +000017#include <wait_q.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020018#include <zephyr/sys/check.h>
19#include <zephyr/init.h>
Anas Nashif4e396172023-09-26 22:46:01 +000020#include <zephyr/internal/syscall_handler.h>
Andy Ross4f911e12018-09-05 10:13:38 -070021#include <kernel_internal.h>
Allan Stephense7d2cc22016-10-19 16:10:46 -050022
Peter Mitsis6df8efe2023-05-11 14:06:46 -040023#ifdef CONFIG_OBJ_CORE_STACK
24static struct k_obj_type obj_type_stack;
Simon Heinbcd1d192024-03-08 12:00:10 +010025#endif /* CONFIG_OBJ_CORE_STACK */
Peter Mitsis6df8efe2023-05-11 14:06:46 -040026
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040027void k_stack_init(struct k_stack *stack, stack_data_t *buffer,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050028 uint32_t num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040029{
Patrik Flykt4344e272019-03-08 14:19:05 -070030 z_waitq_init(&stack->wait_q);
Andy Rossf0933d02018-07-26 10:23:02 -070031 stack->lock = (struct k_spinlock) {};
32 stack->next = stack->base = buffer;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040033 stack->top = stack->base + num_entries;
34
Torbjörn Leksell69e88692021-03-26 12:31:43 +010035 SYS_PORT_TRACING_OBJ_INIT(k_stack, stack);
Anas Nashifc91cad72023-09-26 21:32:13 +000036 k_object_init(stack);
Peter Mitsis6df8efe2023-05-11 14:06:46 -040037
38#ifdef CONFIG_OBJ_CORE_STACK
39 k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
Simon Heinbcd1d192024-03-08 12:00:10 +010040#endif /* CONFIG_OBJ_CORE_STACK */
Benjamin Walsh456c6da2016-09-02 18:55:39 -040041}
42
Kumar Galaa1b77fd2020-05-27 11:26:57 -050043int32_t z_impl_k_stack_alloc_init(struct k_stack *stack, uint32_t num_entries)
Andrew Boiee8734462017-09-29 16:42:07 -070044{
Andrew Boief3bee952018-05-02 17:44:39 -070045 void *buffer;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050046 int32_t ret;
Andrew Boiee8734462017-09-29 16:42:07 -070047
Torbjörn Leksell69e88692021-03-26 12:31:43 +010048 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, alloc_init, stack);
49
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040050 buffer = z_thread_malloc(num_entries * sizeof(stack_data_t));
Flavio Ceolinea716bf2018-09-20 16:30:45 -070051 if (buffer != NULL) {
Andrew Boief3bee952018-05-02 17:44:39 -070052 k_stack_init(stack, buffer, num_entries);
53 stack->flags = K_STACK_FLAG_ALLOC;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050054 ret = (int32_t)0;
Andrew Boief3bee952018-05-02 17:44:39 -070055 } else {
56 ret = -ENOMEM;
57 }
58
Torbjörn Leksell69e88692021-03-26 12:31:43 +010059 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, alloc_init, stack, ret);
60
Andrew Boief3bee952018-05-02 17:44:39 -070061 return ret;
62}
63
64#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -050065static inline int32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
66 uint32_t num_entries)
Andrew Boief3bee952018-05-02 17:44:39 -070067{
Flavio Ceolin11b85ee2024-03-14 16:32:07 -070068 size_t total_size;
69
Anas Nashifa08bfeb2023-09-27 11:20:28 +000070 K_OOPS(K_SYSCALL_OBJ_NEVER_INIT(stack, K_OBJ_STACK));
71 K_OOPS(K_SYSCALL_VERIFY(num_entries > 0));
Flavio Ceolin11b85ee2024-03-14 16:32:07 -070072 K_OOPS(K_SYSCALL_VERIFY(!size_mul_overflow(num_entries, sizeof(stack_data_t),
73 &total_size)));
Andy Ross65649742019-08-06 13:34:31 -070074 return z_impl_k_stack_alloc_init(stack, num_entries);
Andrew Boiee8734462017-09-29 16:42:07 -070075}
Andy Ross65649742019-08-06 13:34:31 -070076#include <syscalls/k_stack_alloc_init_mrsh.c>
Simon Heinbcd1d192024-03-08 12:00:10 +010077#endif /* CONFIG_USERSPACE */
Andrew Boiee8734462017-09-29 16:42:07 -070078
Anas Nashif1ed67d12019-06-16 08:58:10 -040079int k_stack_cleanup(struct k_stack *stack)
Andrew Boief3bee952018-05-02 17:44:39 -070080{
Torbjörn Leksell69e88692021-03-26 12:31:43 +010081 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, cleanup, stack);
82
Anas Nashif1ed67d12019-06-16 08:58:10 -040083 CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
Torbjörn Leksell69e88692021-03-26 12:31:43 +010084 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, -EAGAIN);
85
Anas Nashif1ed67d12019-06-16 08:58:10 -040086 return -EAGAIN;
87 }
Andrew Boief3bee952018-05-02 17:44:39 -070088
Kumar Galaa1b77fd2020-05-27 11:26:57 -050089 if ((stack->flags & K_STACK_FLAG_ALLOC) != (uint8_t)0) {
Andrew Boief3bee952018-05-02 17:44:39 -070090 k_free(stack->base);
91 stack->base = NULL;
92 stack->flags &= ~K_STACK_FLAG_ALLOC;
93 }
Torbjörn Leksell69e88692021-03-26 12:31:43 +010094
95 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, 0);
96
Anas Nashif1ed67d12019-06-16 08:58:10 -040097 return 0;
Andrew Boief3bee952018-05-02 17:44:39 -070098}
99
Anas Nashif1ed67d12019-06-16 08:58:10 -0400100int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101{
102 struct k_thread *first_pending_thread;
Jennifer Williams4d330072020-09-29 15:16:39 -0700103 int ret = 0;
104 k_spinlock_key_t key = k_spin_lock(&stack->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100106 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, push, stack);
107
Anas Nashif1ed67d12019-06-16 08:58:10 -0400108 CHECKIF(stack->next == stack->top) {
Jennifer Williams4d330072020-09-29 15:16:39 -0700109 ret = -ENOMEM;
110 goto out;
Anas Nashif1ed67d12019-06-16 08:58:10 -0400111 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112
Patrik Flykt4344e272019-03-08 14:19:05 -0700113 first_pending_thread = z_unpend_first_thread(&stack->wait_q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114
Flavio Ceolinea716bf2018-09-20 16:30:45 -0700115 if (first_pending_thread != NULL) {
Andrew Boie4ad9f682019-09-21 16:25:56 -0700116 z_thread_return_value_set_with_data(first_pending_thread,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117 0, (void *)data);
James Harrisc7bb4232021-03-02 13:22:52 -0800118
119 z_ready_thread(first_pending_thread);
Patrik Flykt4344e272019-03-08 14:19:05 -0700120 z_reschedule(&stack->lock, key);
Jennifer Williams4d330072020-09-29 15:16:39 -0700121 goto end;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122 } else {
123 *(stack->next) = data;
124 stack->next++;
Jennifer Williams4d330072020-09-29 15:16:39 -0700125 goto out;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126 }
127
Jennifer Williams4d330072020-09-29 15:16:39 -0700128out:
129 k_spin_unlock(&stack->lock, key);
130
131end:
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100132 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, push, stack, ret);
133
Jennifer Williams4d330072020-09-29 15:16:39 -0700134 return ret;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400135}
136
Andrew Boiee8734462017-09-29 16:42:07 -0700137#ifdef CONFIG_USERSPACE
Anas Nashif1ed67d12019-06-16 08:58:10 -0400138static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
Andrew Boiee8734462017-09-29 16:42:07 -0700139{
Anas Nashifa08bfeb2023-09-27 11:20:28 +0000140 K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
Anas Nashif1ed67d12019-06-16 08:58:10 -0400141
142 return z_impl_k_stack_push(stack, data);
Andrew Boiee8734462017-09-29 16:42:07 -0700143}
Andy Ross65649742019-08-06 13:34:31 -0700144#include <syscalls/k_stack_push_mrsh.c>
Simon Heinbcd1d192024-03-08 12:00:10 +0100145#endif /* CONFIG_USERSPACE */
Andrew Boiee8734462017-09-29 16:42:07 -0700146
Andy Ross78327382020-03-05 15:18:14 -0800147int z_impl_k_stack_pop(struct k_stack *stack, stack_data_t *data,
148 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400149{
Andy Rossf0933d02018-07-26 10:23:02 -0700150 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151 int result;
152
Andy Rossf0933d02018-07-26 10:23:02 -0700153 key = k_spin_lock(&stack->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400154
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100155 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, pop, stack, timeout);
156
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 if (likely(stack->next > stack->base)) {
158 stack->next--;
159 *data = *(stack->next);
Andy Rossf0933d02018-07-26 10:23:02 -0700160 k_spin_unlock(&stack->lock, key);
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100161
162 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
163
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400164 return 0;
165 }
166
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100167 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_stack, pop, stack, timeout);
168
Andy Ross78327382020-03-05 15:18:14 -0800169 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Andy Rossf0933d02018-07-26 10:23:02 -0700170 k_spin_unlock(&stack->lock, key);
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100171
172 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EBUSY);
173
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400174 return -EBUSY;
175 }
176
Patrik Flykt4344e272019-03-08 14:19:05 -0700177 result = z_pend_curr(&stack->lock, key, &stack->wait_q, timeout);
Flavio Ceolindfbe0322018-11-01 20:50:25 -0700178 if (result == -EAGAIN) {
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100179 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EAGAIN);
180
Flavio Ceolin8f488ff2018-09-13 11:43:59 -0700181 return -EAGAIN;
Flavio Ceolindfbe0322018-11-01 20:50:25 -0700182 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400183
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -0400184 *data = (stack_data_t)_current->base.swap_data;
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100185
186 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
187
Flavio Ceolin8f488ff2018-09-13 11:43:59 -0700188 return 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400189}
Andrew Boiee8734462017-09-29 16:42:07 -0700190
191#ifdef CONFIG_USERSPACE
Andy Ross643701a2019-08-13 12:58:38 -0700192static inline int z_vrfy_k_stack_pop(struct k_stack *stack,
Andy Ross78327382020-03-05 15:18:14 -0800193 stack_data_t *data, k_timeout_t timeout)
Andrew Boiee8734462017-09-29 16:42:07 -0700194{
Anas Nashifa08bfeb2023-09-27 11:20:28 +0000195 K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
196 K_OOPS(K_SYSCALL_MEMORY_WRITE(data, sizeof(stack_data_t)));
Andy Ross65649742019-08-06 13:34:31 -0700197 return z_impl_k_stack_pop(stack, data, timeout);
Andrew Boiee8734462017-09-29 16:42:07 -0700198}
Andy Ross65649742019-08-06 13:34:31 -0700199#include <syscalls/k_stack_pop_mrsh.c>
Simon Heinbcd1d192024-03-08 12:00:10 +0100200#endif /* CONFIG_USERSPACE */
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400201
202#ifdef CONFIG_OBJ_CORE_STACK
203static int init_stack_obj_core_list(void)
204{
205 /* Initialize stack object type */
206
207 z_obj_type_init(&obj_type_stack, K_OBJ_TYPE_STACK_ID,
208 offsetof(struct k_stack, obj_core));
209
210 /* Initialize and link statically defined stacks */
211
212 STRUCT_SECTION_FOREACH(k_stack, stack) {
213 k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
214 }
215
216 return 0;
217}
218
219SYS_INIT(init_stack_obj_core_list, PRE_KERNEL_1,
220 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
Simon Heinbcd1d192024-03-08 12:00:10 +0100221#endif /* CONFIG_OBJ_CORE_STACK */