blob: 6ada39c9b1eba5485d3d9cfb8f6e3e7098bee015 [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
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020011#include <zephyr/kernel.h>
12#include <zephyr/kernel_structs.h>
Anas Nashif4d994af2021-04-18 23:24:40 -040013
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020014#include <zephyr/toolchain.h>
Benjamin Walshb4b108d2016-10-13 10:31:48 -040015#include <ksched.h>
Anas Nashif8634c3b2023-08-29 17:03:12 +000016#include <wait_q.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020017#include <zephyr/sys/check.h>
18#include <zephyr/init.h>
Anas Nashif4e396172023-09-26 22:46:01 +000019#include <zephyr/internal/syscall_handler.h>
Andy Ross4f911e12018-09-05 10:13:38 -070020#include <kernel_internal.h>
Allan Stephense7d2cc22016-10-19 16:10:46 -050021
Peter Mitsis6df8efe2023-05-11 14:06:46 -040022#ifdef CONFIG_OBJ_CORE_STACK
23static struct k_obj_type obj_type_stack;
24#endif
25
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040026void k_stack_init(struct k_stack *stack, stack_data_t *buffer,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050027 uint32_t num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040028{
Patrik Flykt4344e272019-03-08 14:19:05 -070029 z_waitq_init(&stack->wait_q);
Andy Rossf0933d02018-07-26 10:23:02 -070030 stack->lock = (struct k_spinlock) {};
31 stack->next = stack->base = buffer;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032 stack->top = stack->base + num_entries;
33
Torbjörn Leksell69e88692021-03-26 12:31:43 +010034 SYS_PORT_TRACING_OBJ_INIT(k_stack, stack);
Anas Nashifc91cad72023-09-26 21:32:13 +000035 k_object_init(stack);
Peter Mitsis6df8efe2023-05-11 14:06:46 -040036
37#ifdef CONFIG_OBJ_CORE_STACK
38 k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
39#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -040040}
41
Kumar Galaa1b77fd2020-05-27 11:26:57 -050042int32_t z_impl_k_stack_alloc_init(struct k_stack *stack, uint32_t num_entries)
Andrew Boiee8734462017-09-29 16:42:07 -070043{
Andrew Boief3bee952018-05-02 17:44:39 -070044 void *buffer;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050045 int32_t ret;
Andrew Boiee8734462017-09-29 16:42:07 -070046
Torbjörn Leksell69e88692021-03-26 12:31:43 +010047 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, alloc_init, stack);
48
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040049 buffer = z_thread_malloc(num_entries * sizeof(stack_data_t));
Flavio Ceolinea716bf2018-09-20 16:30:45 -070050 if (buffer != NULL) {
Andrew Boief3bee952018-05-02 17:44:39 -070051 k_stack_init(stack, buffer, num_entries);
52 stack->flags = K_STACK_FLAG_ALLOC;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050053 ret = (int32_t)0;
Andrew Boief3bee952018-05-02 17:44:39 -070054 } else {
55 ret = -ENOMEM;
56 }
57
Torbjörn Leksell69e88692021-03-26 12:31:43 +010058 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, alloc_init, stack, ret);
59
Andrew Boief3bee952018-05-02 17:44:39 -070060 return ret;
61}
62
63#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -050064static inline int32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
65 uint32_t num_entries)
Andrew Boief3bee952018-05-02 17:44:39 -070066{
Anas Nashifa08bfeb2023-09-27 11:20:28 +000067 K_OOPS(K_SYSCALL_OBJ_NEVER_INIT(stack, K_OBJ_STACK));
68 K_OOPS(K_SYSCALL_VERIFY(num_entries > 0));
Andy Ross65649742019-08-06 13:34:31 -070069 return z_impl_k_stack_alloc_init(stack, num_entries);
Andrew Boiee8734462017-09-29 16:42:07 -070070}
Andy Ross65649742019-08-06 13:34:31 -070071#include <syscalls/k_stack_alloc_init_mrsh.c>
Andrew Boiee8734462017-09-29 16:42:07 -070072#endif
73
Anas Nashif1ed67d12019-06-16 08:58:10 -040074int k_stack_cleanup(struct k_stack *stack)
Andrew Boief3bee952018-05-02 17:44:39 -070075{
Torbjörn Leksell69e88692021-03-26 12:31:43 +010076 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, cleanup, stack);
77
Anas Nashif1ed67d12019-06-16 08:58:10 -040078 CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
Torbjörn Leksell69e88692021-03-26 12:31:43 +010079 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, -EAGAIN);
80
Anas Nashif1ed67d12019-06-16 08:58:10 -040081 return -EAGAIN;
82 }
Andrew Boief3bee952018-05-02 17:44:39 -070083
Kumar Galaa1b77fd2020-05-27 11:26:57 -050084 if ((stack->flags & K_STACK_FLAG_ALLOC) != (uint8_t)0) {
Andrew Boief3bee952018-05-02 17:44:39 -070085 k_free(stack->base);
86 stack->base = NULL;
87 stack->flags &= ~K_STACK_FLAG_ALLOC;
88 }
Torbjörn Leksell69e88692021-03-26 12:31:43 +010089
90 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, 0);
91
Anas Nashif1ed67d12019-06-16 08:58:10 -040092 return 0;
Andrew Boief3bee952018-05-02 17:44:39 -070093}
94
Anas Nashif1ed67d12019-06-16 08:58:10 -040095int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096{
97 struct k_thread *first_pending_thread;
Jennifer Williams4d330072020-09-29 15:16:39 -070098 int ret = 0;
99 k_spinlock_key_t key = k_spin_lock(&stack->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100101 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, push, stack);
102
Anas Nashif1ed67d12019-06-16 08:58:10 -0400103 CHECKIF(stack->next == stack->top) {
Jennifer Williams4d330072020-09-29 15:16:39 -0700104 ret = -ENOMEM;
105 goto out;
Anas Nashif1ed67d12019-06-16 08:58:10 -0400106 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107
Patrik Flykt4344e272019-03-08 14:19:05 -0700108 first_pending_thread = z_unpend_first_thread(&stack->wait_q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109
Flavio Ceolinea716bf2018-09-20 16:30:45 -0700110 if (first_pending_thread != NULL) {
Andrew Boie4ad9f682019-09-21 16:25:56 -0700111 z_thread_return_value_set_with_data(first_pending_thread,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112 0, (void *)data);
James Harrisc7bb4232021-03-02 13:22:52 -0800113
114 z_ready_thread(first_pending_thread);
Patrik Flykt4344e272019-03-08 14:19:05 -0700115 z_reschedule(&stack->lock, key);
Jennifer Williams4d330072020-09-29 15:16:39 -0700116 goto end;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117 } else {
118 *(stack->next) = data;
119 stack->next++;
Jennifer Williams4d330072020-09-29 15:16:39 -0700120 goto out;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121 }
122
Jennifer Williams4d330072020-09-29 15:16:39 -0700123out:
124 k_spin_unlock(&stack->lock, key);
125
126end:
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100127 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, push, stack, ret);
128
Jennifer Williams4d330072020-09-29 15:16:39 -0700129 return ret;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130}
131
Andrew Boiee8734462017-09-29 16:42:07 -0700132#ifdef CONFIG_USERSPACE
Anas Nashif1ed67d12019-06-16 08:58:10 -0400133static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
Andrew Boiee8734462017-09-29 16:42:07 -0700134{
Anas Nashifa08bfeb2023-09-27 11:20:28 +0000135 K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
Anas Nashif1ed67d12019-06-16 08:58:10 -0400136
137 return z_impl_k_stack_push(stack, data);
Andrew Boiee8734462017-09-29 16:42:07 -0700138}
Andy Ross65649742019-08-06 13:34:31 -0700139#include <syscalls/k_stack_push_mrsh.c>
Andrew Boiee8734462017-09-29 16:42:07 -0700140#endif
141
Andy Ross78327382020-03-05 15:18:14 -0800142int z_impl_k_stack_pop(struct k_stack *stack, stack_data_t *data,
143 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400144{
Andy Rossf0933d02018-07-26 10:23:02 -0700145 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400146 int result;
147
Andy Rossf0933d02018-07-26 10:23:02 -0700148 key = k_spin_lock(&stack->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400149
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100150 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, pop, stack, timeout);
151
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400152 if (likely(stack->next > stack->base)) {
153 stack->next--;
154 *data = *(stack->next);
Andy Rossf0933d02018-07-26 10:23:02 -0700155 k_spin_unlock(&stack->lock, key);
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100156
157 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
158
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159 return 0;
160 }
161
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100162 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_stack, pop, stack, timeout);
163
Andy Ross78327382020-03-05 15:18:14 -0800164 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Andy Rossf0933d02018-07-26 10:23:02 -0700165 k_spin_unlock(&stack->lock, key);
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100166
167 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EBUSY);
168
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400169 return -EBUSY;
170 }
171
Patrik Flykt4344e272019-03-08 14:19:05 -0700172 result = z_pend_curr(&stack->lock, key, &stack->wait_q, timeout);
Flavio Ceolindfbe0322018-11-01 20:50:25 -0700173 if (result == -EAGAIN) {
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100174 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EAGAIN);
175
Flavio Ceolin8f488ff2018-09-13 11:43:59 -0700176 return -EAGAIN;
Flavio Ceolindfbe0322018-11-01 20:50:25 -0700177 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400178
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -0400179 *data = (stack_data_t)_current->base.swap_data;
Torbjörn Leksell69e88692021-03-26 12:31:43 +0100180
181 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
182
Flavio Ceolin8f488ff2018-09-13 11:43:59 -0700183 return 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400184}
Andrew Boiee8734462017-09-29 16:42:07 -0700185
186#ifdef CONFIG_USERSPACE
Andy Ross643701a2019-08-13 12:58:38 -0700187static inline int z_vrfy_k_stack_pop(struct k_stack *stack,
Andy Ross78327382020-03-05 15:18:14 -0800188 stack_data_t *data, k_timeout_t timeout)
Andrew Boiee8734462017-09-29 16:42:07 -0700189{
Anas Nashifa08bfeb2023-09-27 11:20:28 +0000190 K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
191 K_OOPS(K_SYSCALL_MEMORY_WRITE(data, sizeof(stack_data_t)));
Andy Ross65649742019-08-06 13:34:31 -0700192 return z_impl_k_stack_pop(stack, data, timeout);
Andrew Boiee8734462017-09-29 16:42:07 -0700193}
Andy Ross65649742019-08-06 13:34:31 -0700194#include <syscalls/k_stack_pop_mrsh.c>
Andrew Boiee8734462017-09-29 16:42:07 -0700195#endif
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400196
197#ifdef CONFIG_OBJ_CORE_STACK
198static int init_stack_obj_core_list(void)
199{
200 /* Initialize stack object type */
201
202 z_obj_type_init(&obj_type_stack, K_OBJ_TYPE_STACK_ID,
203 offsetof(struct k_stack, obj_core));
204
205 /* Initialize and link statically defined stacks */
206
207 STRUCT_SECTION_FOREACH(k_stack, stack) {
208 k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
209 }
210
211 return 0;
212}
213
214SYS_INIT(init_stack_obj_core_list, PRE_KERNEL_1,
215 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
216#endif