1 /*
2  * Copyright (c) 1997-2016 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/init.h>
10 #include <zephyr/internal/syscall_handler.h>
11 #include <stdbool.h>
12 #include <zephyr/spinlock.h>
13 #include <ksched.h>
14 #include <wait_q.h>
15 
16 static struct k_spinlock lock;
17 
18 #ifdef CONFIG_OBJ_CORE_TIMER
19 static struct k_obj_type obj_type_timer;
20 #endif
21 
22 /**
23  * @brief Handle expiration of a kernel timer object.
24  *
25  * @param t  Timeout used by the timer.
26  */
z_timer_expiration_handler(struct _timeout * t)27 void z_timer_expiration_handler(struct _timeout *t)
28 {
29 	struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout);
30 	struct k_thread *thread;
31 	k_spinlock_key_t key = k_spin_lock(&lock);
32 
33 	/* In sys_clock_announce(), when a timeout expires, it is first removed
34 	 * from the timeout list, then its expiration handler is called (with
35 	 * unlocked interrupts). For kernel timers, the expiration handler is
36 	 * this function. Usually, the timeout structure related to the timer
37 	 * that is handled here will not be linked to the timeout list at this
38 	 * point. But it may happen that before this function is executed and
39 	 * interrupts are locked again, a given timer gets restarted from an
40 	 * interrupt context that has a priority higher than the system timer
41 	 * interrupt. Then, the timeout structure for this timer will turn out
42 	 * to be linked to the timeout list. And in such case, since the timer
43 	 * was restarted, its expiration handler should not be executed then,
44 	 * so the function exits immediately.
45 	 */
46 	if (sys_dnode_is_linked(&t->node)) {
47 		k_spin_unlock(&lock, key);
48 		return;
49 	}
50 
51 	/*
52 	 * if the timer is periodic, start it again; don't add _TICK_ALIGN
53 	 * since we're already aligned to a tick boundary
54 	 */
55 	if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) &&
56 	    !K_TIMEOUT_EQ(timer->period, K_FOREVER)) {
57 		k_timeout_t next = timer->period;
58 
59 		/* see note about z_add_timeout() in z_impl_k_timer_start() */
60 		next.ticks = MAX(next.ticks - 1, 0);
61 
62 #ifdef CONFIG_TIMEOUT_64BIT
63 		/* Exploit the fact that uptime during a kernel
64 		 * timeout handler reflects the time of the scheduled
65 		 * event and not real time to get some inexpensive
66 		 * protection against late interrupts.  If we're
67 		 * delayed for any reason, we still end up calculating
68 		 * the next expiration as a regular stride from where
69 		 * we "should" have run.  Requires absolute timeouts.
70 		 * (Note offset by one: we're nominally at the
71 		 * beginning of a tick, so need to defeat the "round
72 		 * down" behavior on timeout addition).
73 		 */
74 		next = K_TIMEOUT_ABS_TICKS(k_uptime_ticks() + 1 + next.ticks);
75 #endif
76 		z_add_timeout(&timer->timeout, z_timer_expiration_handler,
77 			      next);
78 	}
79 
80 	/* update timer's status */
81 	timer->status += 1U;
82 
83 	/* invoke timer expiry function */
84 	if (timer->expiry_fn != NULL) {
85 		/* Unlock for user handler. */
86 		k_spin_unlock(&lock, key);
87 		timer->expiry_fn(timer);
88 		key = k_spin_lock(&lock);
89 	}
90 
91 	if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
92 		k_spin_unlock(&lock, key);
93 		return;
94 	}
95 
96 	thread = z_waitq_head(&timer->wait_q);
97 
98 	if (thread == NULL) {
99 		k_spin_unlock(&lock, key);
100 		return;
101 	}
102 
103 	z_unpend_thread_no_timeout(thread);
104 
105 	arch_thread_return_value_set(thread, 0);
106 
107 	k_spin_unlock(&lock, key);
108 
109 	z_ready_thread(thread);
110 }
111 
112 
k_timer_init(struct k_timer * timer,k_timer_expiry_t expiry_fn,k_timer_stop_t stop_fn)113 void k_timer_init(struct k_timer *timer,
114 			 k_timer_expiry_t expiry_fn,
115 			 k_timer_stop_t stop_fn)
116 {
117 	timer->expiry_fn = expiry_fn;
118 	timer->stop_fn = stop_fn;
119 	timer->status = 0U;
120 
121 	if (IS_ENABLED(CONFIG_MULTITHREADING)) {
122 		z_waitq_init(&timer->wait_q);
123 	}
124 
125 	z_init_timeout(&timer->timeout);
126 
127 	SYS_PORT_TRACING_OBJ_INIT(k_timer, timer);
128 
129 	timer->user_data = NULL;
130 
131 	k_object_init(timer);
132 
133 #ifdef CONFIG_OBJ_CORE_TIMER
134 	k_obj_core_init_and_link(K_OBJ_CORE(timer), &obj_type_timer);
135 #endif
136 }
137 
138 
z_impl_k_timer_start(struct k_timer * timer,k_timeout_t duration,k_timeout_t period)139 void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
140 			  k_timeout_t period)
141 {
142 	SYS_PORT_TRACING_OBJ_FUNC(k_timer, start, timer, duration, period);
143 
144 	/* Acquire spinlock to ensure safety during concurrent calls to
145 	 * k_timer_start for scheduling or rescheduling. This is necessary
146 	 * since k_timer_start can be preempted, especially for the same
147 	 * timer instance.
148 	 */
149 	k_spinlock_key_t key = k_spin_lock(&lock);
150 
151 	if (K_TIMEOUT_EQ(duration, K_FOREVER)) {
152 		k_spin_unlock(&lock, key);
153 		return;
154 	}
155 
156 	/* z_add_timeout() always adds one to the incoming tick count
157 	 * to round up to the next tick (by convention it waits for
158 	 * "at least as long as the specified timeout"), but the
159 	 * period interval is always guaranteed to be reset from
160 	 * within the timer ISR, so no round up is desired and 1 is
161 	 * subtracted in there.
162 	 *
163 	 * Note that the duration (!) value gets the same treatment
164 	 * for backwards compatibility.  This is unfortunate
165 	 * (i.e. k_timer_start() doesn't treat its initial sleep
166 	 * argument the same way k_sleep() does), but historical.  The
167 	 * timer_api test relies on this behavior.
168 	 */
169 	if (Z_TICK_ABS(duration.ticks) < 0) {
170 		duration.ticks = MAX(duration.ticks - 1, 0);
171 	}
172 
173 	(void)z_abort_timeout(&timer->timeout);
174 	timer->period = period;
175 	timer->status = 0U;
176 
177 	z_add_timeout(&timer->timeout, z_timer_expiration_handler,
178 		     duration);
179 
180 	k_spin_unlock(&lock, key);
181 }
182 
183 #ifdef CONFIG_USERSPACE
z_vrfy_k_timer_start(struct k_timer * timer,k_timeout_t duration,k_timeout_t period)184 static inline void z_vrfy_k_timer_start(struct k_timer *timer,
185 					k_timeout_t duration,
186 					k_timeout_t period)
187 {
188 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
189 	z_impl_k_timer_start(timer, duration, period);
190 }
191 #include <syscalls/k_timer_start_mrsh.c>
192 #endif
193 
z_impl_k_timer_stop(struct k_timer * timer)194 void z_impl_k_timer_stop(struct k_timer *timer)
195 {
196 	SYS_PORT_TRACING_OBJ_FUNC(k_timer, stop, timer);
197 
198 	bool inactive = (z_abort_timeout(&timer->timeout) != 0);
199 
200 	if (inactive) {
201 		return;
202 	}
203 
204 	if (timer->stop_fn != NULL) {
205 		timer->stop_fn(timer);
206 	}
207 
208 	if (IS_ENABLED(CONFIG_MULTITHREADING)) {
209 		struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q);
210 
211 		if (pending_thread != NULL) {
212 			z_ready_thread(pending_thread);
213 			z_reschedule_unlocked();
214 		}
215 	}
216 }
217 
218 #ifdef CONFIG_USERSPACE
z_vrfy_k_timer_stop(struct k_timer * timer)219 static inline void z_vrfy_k_timer_stop(struct k_timer *timer)
220 {
221 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
222 	z_impl_k_timer_stop(timer);
223 }
224 #include <syscalls/k_timer_stop_mrsh.c>
225 #endif
226 
z_impl_k_timer_status_get(struct k_timer * timer)227 uint32_t z_impl_k_timer_status_get(struct k_timer *timer)
228 {
229 	k_spinlock_key_t key = k_spin_lock(&lock);
230 	uint32_t result = timer->status;
231 
232 	timer->status = 0U;
233 	k_spin_unlock(&lock, key);
234 
235 	return result;
236 }
237 
238 #ifdef CONFIG_USERSPACE
z_vrfy_k_timer_status_get(struct k_timer * timer)239 static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer)
240 {
241 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
242 	return z_impl_k_timer_status_get(timer);
243 }
244 #include <syscalls/k_timer_status_get_mrsh.c>
245 #endif
246 
z_impl_k_timer_status_sync(struct k_timer * timer)247 uint32_t z_impl_k_timer_status_sync(struct k_timer *timer)
248 {
249 	__ASSERT(!arch_is_in_isr(), "");
250 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_timer, status_sync, timer);
251 
252 	if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
253 		uint32_t result;
254 
255 		do {
256 			k_spinlock_key_t key = k_spin_lock(&lock);
257 
258 			if (!z_is_inactive_timeout(&timer->timeout)) {
259 				result = *(volatile uint32_t *)&timer->status;
260 				timer->status = 0U;
261 				k_spin_unlock(&lock, key);
262 				if (result > 0) {
263 					break;
264 				}
265 			} else {
266 				result = timer->status;
267 				k_spin_unlock(&lock, key);
268 				break;
269 			}
270 		} while (true);
271 
272 		return result;
273 	}
274 
275 	k_spinlock_key_t key = k_spin_lock(&lock);
276 	uint32_t result = timer->status;
277 
278 	if (result == 0U) {
279 		if (!z_is_inactive_timeout(&timer->timeout)) {
280 			SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_timer, status_sync, timer, K_FOREVER);
281 
282 			/* wait for timer to expire or stop */
283 			(void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER);
284 
285 			/* get updated timer status */
286 			key = k_spin_lock(&lock);
287 			result = timer->status;
288 		} else {
289 			/* timer is already stopped */
290 		}
291 	} else {
292 		/* timer has already expired at least once */
293 	}
294 
295 	timer->status = 0U;
296 	k_spin_unlock(&lock, key);
297 
298 	/**
299 	 * @note	New tracing hook
300 	 */
301 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_timer, status_sync, timer, result);
302 
303 	return result;
304 }
305 
306 #ifdef CONFIG_USERSPACE
z_vrfy_k_timer_status_sync(struct k_timer * timer)307 static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer)
308 {
309 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
310 	return z_impl_k_timer_status_sync(timer);
311 }
312 #include <syscalls/k_timer_status_sync_mrsh.c>
313 
z_vrfy_k_timer_remaining_ticks(const struct k_timer * timer)314 static inline k_ticks_t z_vrfy_k_timer_remaining_ticks(
315 						const struct k_timer *timer)
316 {
317 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
318 	return z_impl_k_timer_remaining_ticks(timer);
319 }
320 #include <syscalls/k_timer_remaining_ticks_mrsh.c>
321 
z_vrfy_k_timer_expires_ticks(const struct k_timer * timer)322 static inline k_ticks_t z_vrfy_k_timer_expires_ticks(
323 						const struct k_timer *timer)
324 {
325 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
326 	return z_impl_k_timer_expires_ticks(timer);
327 }
328 #include <syscalls/k_timer_expires_ticks_mrsh.c>
329 
z_vrfy_k_timer_user_data_get(const struct k_timer * timer)330 static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer)
331 {
332 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
333 	return z_impl_k_timer_user_data_get(timer);
334 }
335 #include <syscalls/k_timer_user_data_get_mrsh.c>
336 
z_vrfy_k_timer_user_data_set(struct k_timer * timer,void * user_data)337 static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer,
338 						void *user_data)
339 {
340 	K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER));
341 	z_impl_k_timer_user_data_set(timer, user_data);
342 }
343 #include <syscalls/k_timer_user_data_set_mrsh.c>
344 
345 #endif
346 
347 #ifdef CONFIG_OBJ_CORE_TIMER
init_timer_obj_core_list(void)348 static int init_timer_obj_core_list(void)
349 {
350 	/* Initialize timer object type */
351 
352 	z_obj_type_init(&obj_type_timer, K_OBJ_TYPE_TIMER_ID,
353 			offsetof(struct k_timer, obj_core));
354 
355 	/* Initialize and link statically defined timers */
356 
357 	STRUCT_SECTION_FOREACH(k_timer, timer) {
358 		k_obj_core_init_and_link(K_OBJ_CORE(timer), &obj_type_timer);
359 	}
360 
361 	return 0;
362 }
363 SYS_INIT(init_timer_obj_core_list, PRE_KERNEL_1,
364 	 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
365 #endif
366