Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / qorvo-uwb / src / qthread_zephyr.c
qthread_zephyr.c130 lines · 3.6 KiBRaw
| 1 | /* |
| 2 | * Zephyr thread backend for Qorvo's qosal. |
| 3 | * |
| 4 | * Replaces qosal/src/zephyr/qthread.c, which hands the caller's buffer and size straight to |
| 5 | * k_thread_create(). The MAC threads created through it faulted the instant they were |
| 6 | * scheduled with "Illegal load of EXC_RETURN into PC" and an all-zero exception frame — the |
| 7 | * signature of a thread whose stack is unusable, either misaligned or too small for |
| 8 | * Zephyr's frame plus MPU guard. |
| 9 | * |
| 10 | * Rather than trust the supplied buffer, this allocates the stack with |
| 11 | * k_thread_stack_alloc(), which is the only allocator that guarantees what Zephyr's thread |
| 12 | * setup assumes about alignment and size on an MPU target. The requested size is honoured |
| 13 | * as a floor, with a minimum applied because a stack sized for FreeRTOS (whose sizes are in |
| 14 | * words, not bytes) would be a quarter of what it needs to be here. |
| 15 | * |
| 16 | * The requested parameters are logged so a stack that is being under-provisioned by the |
| 17 | * caller is visible rather than presenting as a corrupt frame. |
| 18 | */ |
| 19 | #include "qthread.h" |
| 20 | |
| 21 | #include <zephyr/kernel.h> |
| 22 | #include <zephyr/logging/log.h> |
| 23 | #include <zephyr/sys/util.h> |
| 24 | |
| 25 | #include <string.h> |
| 26 | |
| 27 | LOG_MODULE_REGISTER(qorvo_qthread, CONFIG_QORVO_UWB_LOG_LEVEL); |
| 28 | |
| 29 | /* The MAC creates a couple of threads; a small fixed pool avoids a second allocator. */ |
| 30 | #define MAX_THREADS 4 |
| 31 | |
| 32 | /* |
| 33 | * Floor for a MAC thread. Frame assembly, crypto and region callbacks all nest on these, |
| 34 | * and anything the caller asks for below this is treated as a FreeRTOS-style word count |
| 35 | * rather than a byte count. |
| 36 | */ |
| 37 | #define MIN_STACK_SIZE 2048 |
| 38 | |
| 39 | struct qthread { |
| 40 | struct k_thread thread; |
| 41 | k_thread_stack_t *stack; |
| 42 | bool used; |
| 43 | }; |
| 44 | |
| 45 | static struct qthread threads[MAX_THREADS]; |
| 46 | |
| 47 | static void zephyr_thread_entry(void *p1, void *p2, void *p3) |
| 48 | { |
| 49 | qthread_func func = p1; |
| 50 | |
| 51 | ARG_UNUSED(p3); |
| 52 | func(p2); |
| 53 | } |
| 54 | |
| 55 | struct qthread *qthread_create(qthread_func thread, void *arg, const char *name, void *stack, |
| 56 | uint32_t stack_size, enum qthread_priority prio) |
| 57 | { |
| 58 | struct qthread *th = NULL; |
| 59 | size_t size; |
| 60 | |
| 61 | if (thread == NULL || name == NULL) { |
| 62 | return NULL; |
| 63 | } |
| 64 | if (prio >= QTHREAD_PRIORITY_MAX) { |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | size = MAX(stack_size, MIN_STACK_SIZE); |
| 69 | LOG_INF("qthread_create('%s'): caller stack %p size %u prio %d -> allocating %u bytes", |
| 70 | name, stack, stack_size, (int)prio, (unsigned int)size); |
| 71 | |
| 72 | for (int i = 0; i < MAX_THREADS; i++) { |
| 73 | if (!threads[i].used) { |
| 74 | th = &threads[i]; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (th == NULL) { |
| 79 | LOG_ERR("out of qthread slots"); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * The caller's buffer is deliberately ignored. k_thread_stack_alloc is the only way |
| 85 | * to get memory Zephyr will accept as a thread stack on an MPU target; a plain heap |
| 86 | * buffer is not interchangeable with one, however well aligned. |
| 87 | */ |
| 88 | th->stack = k_thread_stack_alloc(size, 0); |
| 89 | if (th->stack == NULL) { |
| 90 | LOG_ERR("k_thread_stack_alloc(%u) failed for '%s'", (unsigned int)size, name); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | th->used = true; |
| 95 | k_thread_create(&th->thread, th->stack, size, zephyr_thread_entry, thread, arg, NULL, |
| 96 | K_PRIO_PREEMPT((int)prio), 0, K_NO_WAIT); |
| 97 | k_thread_name_set(&th->thread, name); |
| 98 | return th; |
| 99 | } |
| 100 | |
| 101 | static void release(struct qthread *thread) |
| 102 | { |
| 103 | if (thread->stack != NULL) { |
| 104 | (void)k_thread_stack_free(thread->stack); |
| 105 | thread->stack = NULL; |
| 106 | } |
| 107 | thread->used = false; |
| 108 | } |
| 109 | |
| 110 | enum qerr qthread_join(struct qthread *thread) |
| 111 | { |
| 112 | if (thread == NULL || !thread->used) { |
| 113 | return QERR_EINVAL; |
| 114 | } |
| 115 | if (k_thread_join(&thread->thread, K_FOREVER)) { |
| 116 | return QERR_EIO; |
| 117 | } |
| 118 | release(thread); |
| 119 | return QERR_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | enum qerr qthread_delete(struct qthread *thread) |
| 123 | { |
| 124 | if (thread == NULL || !thread->used) { |
| 125 | return QERR_EINVAL; |
| 126 | } |
| 127 | k_thread_abort(&thread->thread); |
| 128 | release(thread); |
| 129 | return QERR_SUCCESS; |
| 130 | } |