Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include "test_sched.h" #include <zephyr/ztest.h> #include <zephyr/irq_offload.h> #include <kernel_internal.h> #include <zephyr/ztest_error_hook.h> struct k_thread user_thread; K_SEM_DEFINE(user_sem, 0, 1); ZTEST_BMEM volatile int thread_was_preempt; #define THREAD_TEST_PRIORITY 0 K_THREAD_STACK_DEFINE(ustack, STACK_SIZE); static void sleepy_thread(void *p1, void *p2, void *p3) { ARG_UNUSED(p1); ARG_UNUSED(p2); ARG_UNUSED(p3); k_sleep(K_FOREVER); k_sem_give(&user_sem); } ZTEST_USER(threads_scheduling, test_user_k_wakeup) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, sleepy_thread, NULL, NULL, NULL, k_thread_priority_get(k_current_get()), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_yield(); /* Let thread run and start sleeping forever */ k_wakeup(&user_thread); k_sem_take(&user_sem, K_FOREVER); k_thread_abort(tid); } static void preempt_test_thread(void *p1, void *p2, void *p3) { ARG_UNUSED(p1); ARG_UNUSED(p2); ARG_UNUSED(p3); thread_was_preempt = k_is_preempt_thread(); k_sem_give(&user_sem); } ZTEST_USER(threads_scheduling, test_user_k_is_preempt) { /* thread_was_preempt is volatile, and static analysis doesn't * like to see it being tested inside zassert_true, because * the read is treated as a "side effect" of an assertion * (e.g. a read is significant for things like volatile MMIO * addresses, and assertions may or may not be compiled, even * though here in a test they always will be and in any case * the value is a static variable above marked volatile for * threadsafety). Read it into a local variable first to * evade the warning. */ int twp; k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, preempt_test_thread, NULL, NULL, NULL, k_thread_priority_get(k_current_get()), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_sem_take(&user_sem, K_FOREVER); twp = thread_was_preempt; zassert_false(twp, "unexpected return value"); k_thread_abort(tid); tid = k_thread_create(&user_thread, ustack, STACK_SIZE, preempt_test_thread, NULL, NULL, NULL, K_PRIO_PREEMPT(1), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_sem_take(&user_sem, K_FOREVER); twp = thread_was_preempt; zassert_true(twp, "unexpected return value"); k_thread_abort(tid); } /** * userspace negative test: take NULL as input param to verify * the api will trigger a fatal exception */ #ifdef CONFIG_USERSPACE static void thread_suspend_init_null(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); k_thread_suspend(NULL); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_suspend() API * * @details Create a thread and set k_thread_suspend() input param to NULL * will trigger a fatal error. * * @ingroup kernel_sched_tests * * @see k_thread_suspend() */ ZTEST_USER(threads_scheduling, test_k_thread_suspend_init_null) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_suspend_init_null, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_suspend_init_null) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_resume_init_null(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); k_thread_resume(NULL); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_resume() API * * @details Create a thread and set k_thread_resume() input param to NULL * will trigger a fatal error. * * @ingroup kernel_sched_tests * * @see k_thread_resume() */ ZTEST_USER(threads_scheduling, test_k_thread_resume_init_null) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_resume_init_null, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_resume_init_null) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_priority_get_init_null(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); k_thread_priority_get(NULL); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_priority_get() API * * @details Create a thread and set thread_k_thread_priority_get() param input to * NULL will trigger a fatal error. * * @ingroup kernel_sched_tests * * @see thread_k_thread_priority_get() */ ZTEST_USER(threads_scheduling, test_k_thread_priority_get_init_null) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_priority_get_init_null, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_priority_get_init_null) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_priority_set_init_null(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); k_thread_priority_set(NULL, 0); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_priority_set() API * * @details Create a thread and set k_thread_priority_set() param input to * NULL will trigger a fatal error. * * @ingroup kernel_sched_tests * * @see k_thread_priority_set() */ ZTEST_USER(threads_scheduling, test_k_thread_priority_set_init_null) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_priority_set_init_null, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_priority_set_init_null) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_priority_set_overmax(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); /* set valid priority value outside the priority range will invoke fatal error */ k_thread_priority_set(k_current_get(), K_LOWEST_APPLICATION_THREAD_PRIO + 1); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_priority_set() API * * @details Check input param range overmax in userspace test. * * @ingroup kernel_sched_tests * * @see k_thread_priority_set() */ ZTEST_USER(threads_scheduling, test_k_thread_priority_set_overmax) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_priority_set_overmax, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_priority_set_overmax) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_priority_set_upgrade(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); /* at first, set an valid priority */ k_thread_priority_set(k_current_get(), THREAD_TEST_PRIORITY); /* it cannot upgraded thread priority in usermode */ k_thread_priority_set(k_current_get(), THREAD_TEST_PRIORITY - 1); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_thread_priority_set() API * * @details Check input param range fail in userspace test. * * @ingroup kernel_sched_tests * * @see k_thread_priority_set() */ ZTEST_USER(threads_scheduling, test_k_thread_priority_set_upgrade) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_priority_set_upgrade, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_thread_priority_set_upgrade) { ztest_test_skip(); } #endif #ifdef CONFIG_USERSPACE static void thread_wakeup_init_null(void *p1, void *p2, void *p3) { ztest_set_fault_valid(true); k_wakeup(NULL); /* should not go here */ ztest_test_fail(); } /** * @brief Test k_wakeup() API * * @details Create a thread and set k_wakeup() input param to NULL * will trigger a fatal error * * @ingroup kernel_sched_tests * * @see k_wakeup() */ ZTEST_USER(threads_scheduling, test_k_wakeup_init_null) { k_tid_t tid = k_thread_create(&user_thread, ustack, STACK_SIZE, (k_thread_entry_t)thread_wakeup_init_null, NULL, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), K_USER | K_INHERIT_PERMS, K_NO_WAIT); k_thread_join(tid, K_FOREVER); } #else ZTEST_USER(threads_scheduling, test_k_wakeup_init_null) { ztest_test_skip(); } #endif |