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 <kernel.h>
12 #include <kernel_structs.h>
13 
14 #include <toolchain.h>
15 #include <ksched.h>
16 #include <wait_q.h>
17 #include <sys/check.h>
18 #include <init.h>
19 #include <syscall_handler.h>
20 #include <kernel_internal.h>
21 
k_stack_init(struct k_stack * stack,stack_data_t * buffer,uint32_t num_entries)22 void k_stack_init(struct k_stack *stack, stack_data_t *buffer,
23 		  uint32_t num_entries)
24 {
25 	z_waitq_init(&stack->wait_q);
26 	stack->lock = (struct k_spinlock) {};
27 	stack->next = stack->base = buffer;
28 	stack->top = stack->base + num_entries;
29 
30 	SYS_PORT_TRACING_OBJ_INIT(k_stack, stack);
31 	z_object_init(stack);
32 }
33 
z_impl_k_stack_alloc_init(struct k_stack * stack,uint32_t num_entries)34 int32_t z_impl_k_stack_alloc_init(struct k_stack *stack, uint32_t num_entries)
35 {
36 	void *buffer;
37 	int32_t ret;
38 
39 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, alloc_init, stack);
40 
41 	buffer = z_thread_malloc(num_entries * sizeof(stack_data_t));
42 	if (buffer != NULL) {
43 		k_stack_init(stack, buffer, num_entries);
44 		stack->flags = K_STACK_FLAG_ALLOC;
45 		ret = (int32_t)0;
46 	} else {
47 		ret = -ENOMEM;
48 	}
49 
50 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, alloc_init, stack, ret);
51 
52 	return ret;
53 }
54 
55 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_alloc_init(struct k_stack * stack,uint32_t num_entries)56 static inline int32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
57 					      uint32_t num_entries)
58 {
59 	Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(stack, K_OBJ_STACK));
60 	Z_OOPS(Z_SYSCALL_VERIFY(num_entries > 0));
61 	return z_impl_k_stack_alloc_init(stack, num_entries);
62 }
63 #include <syscalls/k_stack_alloc_init_mrsh.c>
64 #endif
65 
k_stack_cleanup(struct k_stack * stack)66 int k_stack_cleanup(struct k_stack *stack)
67 {
68 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, cleanup, stack);
69 
70 	CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
71 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, -EAGAIN);
72 
73 		return -EAGAIN;
74 	}
75 
76 	if ((stack->flags & K_STACK_FLAG_ALLOC) != (uint8_t)0) {
77 		k_free(stack->base);
78 		stack->base = NULL;
79 		stack->flags &= ~K_STACK_FLAG_ALLOC;
80 	}
81 
82 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, cleanup, stack, 0);
83 
84 	return 0;
85 }
86 
z_impl_k_stack_push(struct k_stack * stack,stack_data_t data)87 int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
88 {
89 	struct k_thread *first_pending_thread;
90 	int ret = 0;
91 	k_spinlock_key_t key = k_spin_lock(&stack->lock);
92 
93 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, push, stack);
94 
95 	CHECKIF(stack->next == stack->top) {
96 		ret = -ENOMEM;
97 		goto out;
98 	}
99 
100 	first_pending_thread = z_unpend_first_thread(&stack->wait_q);
101 
102 	if (first_pending_thread != NULL) {
103 		z_thread_return_value_set_with_data(first_pending_thread,
104 						   0, (void *)data);
105 
106 		z_ready_thread(first_pending_thread);
107 		z_reschedule(&stack->lock, key);
108 		goto end;
109 	} else {
110 		*(stack->next) = data;
111 		stack->next++;
112 		goto out;
113 	}
114 
115 out:
116 	k_spin_unlock(&stack->lock, key);
117 
118 end:
119 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, push, stack, ret);
120 
121 	return ret;
122 }
123 
124 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_push(struct k_stack * stack,stack_data_t data)125 static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
126 {
127 	Z_OOPS(Z_SYSCALL_OBJ(stack, K_OBJ_STACK));
128 
129 	return z_impl_k_stack_push(stack, data);
130 }
131 #include <syscalls/k_stack_push_mrsh.c>
132 #endif
133 
z_impl_k_stack_pop(struct k_stack * stack,stack_data_t * data,k_timeout_t timeout)134 int z_impl_k_stack_pop(struct k_stack *stack, stack_data_t *data,
135 		       k_timeout_t timeout)
136 {
137 	k_spinlock_key_t key;
138 	int result;
139 
140 	key = k_spin_lock(&stack->lock);
141 
142 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_stack, pop, stack, timeout);
143 
144 	if (likely(stack->next > stack->base)) {
145 		stack->next--;
146 		*data = *(stack->next);
147 		k_spin_unlock(&stack->lock, key);
148 
149 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
150 
151 		return 0;
152 	}
153 
154 	SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_stack, pop, stack, timeout);
155 
156 	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
157 		k_spin_unlock(&stack->lock, key);
158 
159 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EBUSY);
160 
161 		return -EBUSY;
162 	}
163 
164 	result = z_pend_curr(&stack->lock, key, &stack->wait_q, timeout);
165 	if (result == -EAGAIN) {
166 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, -EAGAIN);
167 
168 		return -EAGAIN;
169 	}
170 
171 	*data = (stack_data_t)_current->base.swap_data;
172 
173 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_stack, pop, stack, timeout, 0);
174 
175 	return 0;
176 }
177 
178 #ifdef CONFIG_USERSPACE
z_vrfy_k_stack_pop(struct k_stack * stack,stack_data_t * data,k_timeout_t timeout)179 static inline int z_vrfy_k_stack_pop(struct k_stack *stack,
180 				     stack_data_t *data, k_timeout_t timeout)
181 {
182 	Z_OOPS(Z_SYSCALL_OBJ(stack, K_OBJ_STACK));
183 	Z_OOPS(Z_SYSCALL_MEMORY_WRITE(data, sizeof(stack_data_t)));
184 	return z_impl_k_stack_pop(stack, data, timeout);
185 }
186 #include <syscalls/k_stack_pop_mrsh.c>
187 #endif
188