1 /*
2  * Copyright (c) 2010-2016 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @brief fixed-size stack object
9  */
10 
11 #include <zephyr/sys/math_extras.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/kernel_structs.h>
14 
15 #include <zephyr/toolchain.h>
16 #include <ksched.h>
17 #include <wait_q.h>
18 #include <zephyr/sys/check.h>
19 #include <zephyr/init.h>
20 #include <zephyr/internal/syscall_handler.h>
21 #include <kernel_internal.h>
22 
23 #ifdef CONFIG_OBJ_CORE_STACK
24 static struct k_obj_type obj_type_stack;
25 #endif /* CONFIG_OBJ_CORE_STACK */
26 
k_stack_init(struct k_stack * stack,stack_data_t * buffer,uint32_t num_entries)27 void k_stack_init(struct k_stack *stack, stack_data_t *buffer,
28 		  uint32_t num_entries)
29 {
30 	z_waitq_init(&stack->wait_q);
31 	stack->lock = (struct k_spinlock) {};
32 	stack->next = buffer;
33 	stack->base = buffer;
34 	stack->top = stack->base + num_entries;
35 
36 	SYS_PORT_TRACING_OBJ_INIT(k_stack, stack);
37 	k_object_init(stack);
38 
39 #ifdef CONFIG_OBJ_CORE_STACK
40 	k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
41 #endif /* CONFIG_OBJ_CORE_STACK */
42 }
43 
z_impl_k_stack_alloc_init(struct k_stack * stack,uint32_t num_entries)44 int32_t z_impl_k_stack_alloc_init(struct k_stack *stack, uint32_t num_entries)
45 {
46 	void *buffer;
47 	int32_t ret;
48 
49 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, alloc_init, stack);
50 
51 	buffer = z_thread_malloc(num_entries * sizeof(stack_data_t));
52 	if (buffer != NULL) {
53 		k_stack_init(stack, buffer, num_entries);
54 		stack->flags = K_STACK_FLAG_ALLOC;
55 		ret = 0;
56 	} else {
57 		ret = -ENOMEM;
58 	}
59 
60 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, alloc_init, stack, ret);
61 
62 	return ret;
63 }
64 
65 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_alloc_init(struct k_stack * stack,uint32_t num_entries)66 static inline int32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
67 					      uint32_t num_entries)
68 {
69 	size_t total_size;
70 
71 	K_OOPS(K_SYSCALL_OBJ_NEVER_INIT(stack, K_OBJ_STACK));
72 	K_OOPS(K_SYSCALL_VERIFY(num_entries > 0));
73 	K_OOPS(K_SYSCALL_VERIFY(!size_mul_overflow(num_entries, sizeof(stack_data_t),
74 					&total_size)));
75 	return z_impl_k_stack_alloc_init(stack, num_entries);
76 }
77 #include <zephyr/syscalls/k_stack_alloc_init_mrsh.c>
78 #endif /* CONFIG_USERSPACE */
79 
k_stack_cleanup(struct k_stack * stack)80 int k_stack_cleanup(struct k_stack *stack)
81 {
82 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, cleanup, stack);
83 
84 	CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
85 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, -EAGAIN);
86 
87 		return -EAGAIN;
88 	}
89 
90 	if ((stack->flags & K_STACK_FLAG_ALLOC) != (uint8_t)0) {
91 		k_free(stack->base);
92 		stack->base = NULL;
93 		stack->flags &= ~K_STACK_FLAG_ALLOC;
94 	}
95 
96 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, 0);
97 
98 	return 0;
99 }
100 
z_impl_k_stack_push(struct k_stack * stack,stack_data_t data)101 int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
102 {
103 	struct k_thread *first_pending_thread;
104 	int ret = 0;
105 	k_spinlock_key_t key = k_spin_lock(&stack->lock);
106 
107 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, push, stack);
108 
109 	CHECKIF(stack->next == stack->top) {
110 		ret = -ENOMEM;
111 		goto out;
112 	}
113 
114 	first_pending_thread = z_unpend_first_thread(&stack->wait_q);
115 
116 	if (first_pending_thread != NULL) {
117 		z_thread_return_value_set_with_data(first_pending_thread,
118 						   0, (void *)data);
119 
120 		z_ready_thread(first_pending_thread);
121 		z_reschedule(&stack->lock, key);
122 		goto end;
123 	} else {
124 		*(stack->next) = data;
125 		stack->next++;
126 		goto out;
127 	}
128 
129 out:
130 	k_spin_unlock(&stack->lock, key);
131 
132 end:
133 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, push, stack, ret);
134 
135 	return ret;
136 }
137 
138 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_push(struct k_stack * stack,stack_data_t data)139 static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
140 {
141 	K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
142 
143 	return z_impl_k_stack_push(stack, data);
144 }
145 #include <zephyr/syscalls/k_stack_push_mrsh.c>
146 #endif /* CONFIG_USERSPACE */
147 
z_impl_k_stack_pop(struct k_stack * stack,stack_data_t * data,k_timeout_t timeout)148 int z_impl_k_stack_pop(struct k_stack *stack, stack_data_t *data,
149 		       k_timeout_t timeout)
150 {
151 	k_spinlock_key_t key;
152 	int result;
153 
154 	key = k_spin_lock(&stack->lock);
155 
156 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, pop, stack, timeout);
157 
158 	if (likely(stack->next > stack->base)) {
159 		stack->next--;
160 		*data = *(stack->next);
161 		k_spin_unlock(&stack->lock, key);
162 
163 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
164 
165 		return 0;
166 	}
167 
168 	SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_stack, pop, stack, timeout);
169 
170 	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
171 		k_spin_unlock(&stack->lock, key);
172 
173 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EBUSY);
174 
175 		return -EBUSY;
176 	}
177 
178 	result = z_pend_curr(&stack->lock, key, &stack->wait_q, timeout);
179 	if (result == -EAGAIN) {
180 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EAGAIN);
181 
182 		return -EAGAIN;
183 	}
184 
185 	*data = (stack_data_t)_current->base.swap_data;
186 
187 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
188 
189 	return 0;
190 }
191 
192 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_pop(struct k_stack * stack,stack_data_t * data,k_timeout_t timeout)193 static inline int z_vrfy_k_stack_pop(struct k_stack *stack,
194 				     stack_data_t *data, k_timeout_t timeout)
195 {
196 	K_OOPS(K_SYSCALL_OBJ(stack, K_OBJ_STACK));
197 	K_OOPS(K_SYSCALL_MEMORY_WRITE(data, sizeof(stack_data_t)));
198 	return z_impl_k_stack_pop(stack, data, timeout);
199 }
200 #include <zephyr/syscalls/k_stack_pop_mrsh.c>
201 #endif /* CONFIG_USERSPACE */
202 
203 #ifdef CONFIG_OBJ_CORE_STACK
init_stack_obj_core_list(void)204 static int init_stack_obj_core_list(void)
205 {
206 	/* Initialize stack object type */
207 
208 	z_obj_type_init(&obj_type_stack, K_OBJ_TYPE_STACK_ID,
209 			offsetof(struct k_stack, obj_core));
210 
211 	/* Initialize and link statically defined stacks */
212 
213 	STRUCT_SECTION_FOREACH(k_stack, stack) {
214 		k_obj_core_init_and_link(K_OBJ_CORE(stack), &obj_type_stack);
215 	}
216 
217 	return 0;
218 }
219 
220 SYS_INIT(init_stack_obj_core_list, PRE_KERNEL_1,
221 	 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
222 #endif /* CONFIG_OBJ_CORE_STACK */
223