| 1 | # coreboot Threads |
| 2 | |
| 3 | ## Thread Management |
| 4 | |
| 5 | coreboot provides a cooperative threading system that allows for |
| 6 | concurrent execution of tasks during the boot process. The thread API is |
| 7 | particularly useful for implementing asynchronous operations and |
| 8 | managing hardware initialization sequences. |
| 9 | |
| 10 | |
| 11 | ### Thread Creation and Management |
| 12 | |
| 13 | #### thread_run |
| 14 | |
| 15 | ```c |
| 16 | int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg) |
| 17 | ``` |
| 18 | Creates and starts a new thread to execute the specified function. |
| 19 | |
| 20 | **Parameters:** |
| 21 | - `handle`: Pointer to a thread handle structure to track thread state |
| 22 | (Note: `struct thread_handle` is an opaque structure used by the API |
| 23 | to manage the thread's state.) |
| 24 | - `func`: Function to execute in the new thread |
| 25 | - `arg`: Argument to pass to the function |
| 26 | |
| 27 | **Returns:** |
| 28 | - 0 on success |
| 29 | - < 0 on failure |
| 30 | |
| 31 | **Example:** |
| 32 | ```c |
| 33 | struct thread_handle th; |
| 34 | enum cb_err thread_func(void *arg) { |
| 35 | // Thread work here |
| 36 | return CB_SUCCESS; |
| 37 | } |
| 38 | |
| 39 | if (thread_run(&th, thread_func, NULL) < 0) { |
| 40 | printk(BIOS_ERR, "Failed to create thread\n"); |
| 41 | } else { |
| 42 | // Wait for the thread to complete and check its status |
| 43 | enum cb_err err = thread_join(&th); |
| 44 | if (err != CB_SUCCESS) { |
| 45 | printk(BIOS_ERR, "Thread failed with error %d\n", err); |
| 46 | } |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | |
| 51 | #### thread_run_until |
| 52 | |
| 53 | ```c |
| 54 | int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg, |
| 55 | boot_state_t state, boot_state_sequence_t seq) |
| 56 | ``` |
| 57 | Creates a thread that blocks boot state transitions until completion. |
| 58 | |
| 59 | **Parameters:** |
| 60 | - `handle`: Pointer to a thread handle structure |
| 61 | - `func`: Function to execute |
| 62 | - `arg`: Argument to pass to the function |
| 63 | - `state`: Boot state to block |
| 64 | - `seq`: Boot state sequence to block |
| 65 | |
| 66 | **Returns:** |
| 67 | - 0 on success |
| 68 | - < 0 on failure |
| 69 | |
| 70 | **Example:** |
| 71 | ```c |
| 72 | struct thread_handle th; |
| 73 | enum cb_err init_func(void *arg) { |
| 74 | // Hardware initialization |
| 75 | return CB_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | // Block BS_DEV_ENABLE until initialization completes |
| 79 | thread_run_until(&th, init_func, NULL, BS_DEV_ENABLE, 0); |
| 80 | ``` |
| 81 | |
| 82 | |
| 83 | ### Thread Synchronization |
| 84 | |
| 85 | #### thread_join |
| 86 | |
| 87 | ```c |
| 88 | enum cb_err thread_join(struct thread_handle *handle) |
| 89 | ``` |
| 90 | Waits for a thread to complete and returns its error code. |
| 91 | |
| 92 | **Parameters:** |
| 93 | - `handle`: Thread handle to wait for |
| 94 | |
| 95 | **Returns:** |
| 96 | - Thread's error code (e.g., `CB_SUCCESS`, `CB_ERR`). See |
| 97 | `src/include/cb_err.h` for details. |
| 98 | |
| 99 | **Example:** |
| 100 | ```c |
| 101 | struct thread_handle th; |
| 102 | // ... create thread ... |
| 103 | |
| 104 | enum cb_err err = thread_join(&th); |
| 105 | if (err != CB_SUCCESS) { |
| 106 | printk(BIOS_ERR, "Thread failed with error %d\n", err); |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | |
| 111 | ### Thread Yielding |
| 112 | |
| 113 | Yielding is crucial in a cooperative multitasking system like |
| 114 | coreboot's. Threads must explicitly yield control using `thread_yield` |
| 115 | or `thread_yield_microseconds` to allow other threads to run. Failure to |
| 116 | yield can lead to a single thread monopolizing the CPU, preventing other |
| 117 | tasks from executing. |
| 118 | |
| 119 | #### thread_yield |
| 120 | |
| 121 | ```c |
| 122 | int thread_yield(void) |
| 123 | ``` |
| 124 | Yields the current thread's execution to allow other threads to run. |
| 125 | |
| 126 | **Returns:** |
| 127 | - 0 on success |
| 128 | - < 0 if thread cannot yield |
| 129 | |
| 130 | **Example:** |
| 131 | ```c |
| 132 | while (!condition) { |
| 133 | if (thread_yield() < 0) { |
| 134 | printk(BIOS_ERR, "Failed to yield thread\n"); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | |
| 141 | #### thread_yield_microseconds |
| 142 | |
| 143 | ```c |
| 144 | int thread_yield_microseconds(unsigned int microsecs) |
| 145 | ``` |
| 146 | Yields the current thread for a specified number of microseconds. |
| 147 | |
| 148 | **Parameters:** |
| 149 | - `microsecs`: Number of microseconds to yield |
| 150 | |
| 151 | **Returns:** |
| 152 | - 0 on success |
| 153 | - < 0 if thread cannot yield |
| 154 | |
| 155 | **Example:** |
| 156 | ```c |
| 157 | // Wait for 100 microseconds |
| 158 | if (thread_yield_microseconds(100) < 0) { |
| 159 | printk(BIOS_ERR, "Failed to yield thread\n"); |
| 160 | } |
| 161 | ``` |
| 162 | |
| 163 | |
| 164 | ### Thread Cooperation Control |
| 165 | |
| 166 | #### thread_coop_enable |
| 167 | |
| 168 | ```c |
| 169 | void thread_coop_enable(void) |
| 170 | ``` |
| 171 | Enables cooperative behavior for the current thread. |
| 172 | |
| 173 | **Example:** |
| 174 | ```c |
| 175 | thread_coop_enable(); // Allow thread to yield |
| 176 | ``` |
| 177 | |
| 178 | #### thread_coop_disable |
| 179 | |
| 180 | ```c |
| 181 | void thread_coop_disable(void) |
| 182 | ``` |
| 183 | Disables cooperative behavior for the current thread. |
| 184 | |
| 185 | **Example:** |
| 186 | ```c |
| 187 | thread_coop_disable(); // Prevent thread from yielding |
| 188 | ``` |
| 189 | |
| 190 | |
| 191 | ### Thread Mutexes |
| 192 | |
| 193 | #### thread_mutex_lock |
| 194 | |
| 195 | ```c |
| 196 | void thread_mutex_lock(struct thread_mutex *mutex) |
| 197 | ``` |
| 198 | Acquires a mutex lock, waiting if necessary. |
| 199 | |
| 200 | **Parameters:** |
| 201 | - `mutex`: Mutex to lock |
| 202 | |
| 203 | **Example:** |
| 204 | ```c |
| 205 | struct thread_mutex mtx = THREAD_MUTEX_INITIALIZER; // Or = { .locked = false }; |
| 206 | thread_mutex_lock(&mtx); |
| 207 | // Critical section |
| 208 | thread_mutex_unlock(&mtx); |
| 209 | ``` |
| 210 | |
| 211 | |
| 212 | #### thread_mutex_unlock |
| 213 | |
| 214 | ```c |
| 215 | void thread_mutex_unlock(struct thread_mutex *mutex) |
| 216 | ``` |
| 217 | Releases a mutex lock. |
| 218 | |
| 219 | **Parameters:** |
| 220 | - `mutex`: Mutex to unlock |
| 221 | |
| 222 | ## Best Practices |
| 223 | |
| 224 | 1. **Thread Safety**: |
| 225 | - Use mutexes to protect shared resources |
| 226 | - Be careful with global variables in threaded code |
| 227 | - Consider thread cooperation when implementing critical sections |
| 228 | |
| 229 | 2. **Resource Management**: |
| 230 | - Always join threads that you create using `thread_run` to check |
| 231 | their completion status and clean up resources. Threads started |
| 232 | with `thread_run_until` are implicitly managed by the boot state |
| 233 | machine and typically do not require explicit joining. |
| 234 | - Consistently check return values from thread creation and operation |
| 235 | functions (like `thread_run`, `thread_yield`, `thread_join`) to |
| 236 | detect errors early. |
| 237 | - Clean up resources allocated or used within thread functions before |
| 238 | they exit. |
| 239 | |
| 240 | 3. **Performance Considerations**: |
| 241 | - Use thread_yield_microseconds for precise timing |
| 242 | - Minimize time spent in critical sections |
| 243 | - Consider using thread_run_until for hardware initialization |
| 244 | |
| 245 | 4. **Error Handling**: |
| 246 | - Check thread creation and operation return values (as noted in |
| 247 | Resource Management). |
| 248 | - Implement proper error handling within thread functions, returning |
| 249 | appropriate `cb_err` values. |
| 250 | - Use `thread_join` (for `thread_run` threads) to check the final |
| 251 | completion status. |
| 252 | |
| 253 | |
| 254 | ## Common Patterns |
| 255 | |
| 256 | ### Hardware Initialization |
| 257 | |
| 258 | ```c |
| 259 | struct thread_handle init_th; |
| 260 | enum cb_err init_hardware(void *arg) { |
| 261 | // Initialize hardware |
| 262 | if (hardware_init() != 0) |
| 263 | return CB_ERR; |
| 264 | return CB_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | // Run initialization in a thread |
| 268 | thread_run_until(&init_th, init_hardware, NULL, BS_DEV_ENABLE, 0); |
| 269 | ``` |
| 270 | |
| 271 | ### Asynchronous Operation |
| 272 | |
| 273 | ```c |
| 274 | struct thread_handle async_th; |
| 275 | enum cb_err async_operation(void *arg) { |
| 276 | // Perform async operation |
| 277 | while (!operation_complete()) { |
| 278 | if (thread_yield() < 0) |
| 279 | return CB_ERR; |
| 280 | } |
| 281 | return CB_SUCCESS; |
| 282 | } |
| 283 | |
| 284 | // Start async operation |
| 285 | thread_run(&async_th, async_operation, NULL); |
| 286 | ``` |
| 287 | |
| 288 | |
| 289 | ### Critical Section Protection |
| 290 | |
| 291 | ```c |
| 292 | struct thread_mutex resource_mtx = { .locked = false }; |
| 293 | |
| 294 | void access_shared_resource(void) { |
| 295 | thread_mutex_lock(&resource_mtx); |
| 296 | // Access shared resource |
| 297 | thread_mutex_unlock(&resource_mtx); |
| 298 | } |
| 299 | ``` |
| 300 | |
| 301 | ## Limitations |
| 302 | |
| 303 | 1. The thread system is cooperative, not preemptive. |
| 304 | 2. Threads must explicitly yield to allow other threads to run. |
| 305 | 3. Thread operations are typically only available after RAM |
| 306 | initialization (in ramstage and later). Check specific environment |
| 307 | constraints if unsure. |
| 308 | 4. Thread count is limited by the `CONFIG_NUM_THREADS` Kconfig option. |
| 309 | 5. Thread stack size is fixed by the `CONFIG_STACK_SIZE` Kconfig option. |
| 310 |