[75f3522] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[73abe95] | 7 | // kernel_private.hfa -- |
---|
[75f3522] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Mon Feb 13 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[1805b1b] | 12 | // Last Modified On : Sat Nov 30 19:25:02 2019 |
---|
| 13 | // Update Count : 8 |
---|
[75f3522] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[75f3522] | 17 | |
---|
[58b6d1b] | 18 | #include "kernel.hfa" |
---|
| 19 | #include "thread.hfa" |
---|
[75f3522] | 20 | |
---|
[73abe95] | 21 | #include "alarm.hfa" |
---|
[8834751] | 22 | #include "stats.hfa" |
---|
[fa21ac9] | 23 | |
---|
[13c5e19] | 24 | #include "bits/random.hfa" |
---|
| 25 | |
---|
[4aa2fb2] | 26 | |
---|
[75f3522] | 27 | //----------------------------------------------------------------------------- |
---|
| 28 | // Scheduler |
---|
[1c273d0] | 29 | |
---|
[37ba662] | 30 | struct __attribute__((aligned(128))) __scheduler_lock_id_t; |
---|
[9b1dcc2] | 31 | |
---|
[1c273d0] | 32 | extern "C" { |
---|
[2026bb6] | 33 | void disable_interrupts() OPTIONAL_THREAD; |
---|
[969b3fe] | 34 | void enable_interrupts_noPoll(); |
---|
[36982fc] | 35 | void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
[1c273d0] | 36 | } |
---|
| 37 | |
---|
[9b1dcc2] | 38 | void __schedule_thread( struct __processor_id_t *, $thread * ) __attribute__((nonnull (2))); |
---|
[75f3522] | 39 | |
---|
[e60e0dc] | 40 | //Block current thread and release/wake-up the following resources |
---|
[b0c7419] | 41 | void __leave_thread() __attribute__((noreturn)); |
---|
[db6f06a] | 42 | |
---|
[75f3522] | 43 | //----------------------------------------------------------------------------- |
---|
| 44 | // Processor |
---|
| 45 | void main(processorCtx_t *); |
---|
[85b1deb] | 46 | |
---|
[8c50aed] | 47 | void * __create_pthread( pthread_t *, void * (*)(void *), void * ); |
---|
[1805b1b] | 48 | |
---|
[85b1deb] | 49 | |
---|
[75f3522] | 50 | |
---|
[e60e0dc] | 51 | struct event_kernel_t { |
---|
[fa21ac9] | 52 | alarm_list_t alarms; |
---|
[ea7d2b0] | 53 | __spinlock_t lock; |
---|
[fa21ac9] | 54 | }; |
---|
| 55 | |
---|
[e60e0dc] | 56 | extern event_kernel_t * event_kernel; |
---|
| 57 | |
---|
[d8548e2] | 58 | struct __cfa_kernel_preemption_state_t { |
---|
[b69ea6b] | 59 | bool enabled; |
---|
| 60 | bool in_progress; |
---|
| 61 | unsigned short disable_count; |
---|
| 62 | }; |
---|
| 63 | |
---|
[afc2427] | 64 | extern volatile thread_local __cfa_kernel_preemption_state_t preemption_state __attribute__ ((tls_model ( "initial-exec" ))); |
---|
[c81ebf9] | 65 | |
---|
[6502a2b] | 66 | extern cluster * mainCluster; |
---|
| 67 | |
---|
[75f3522] | 68 | //----------------------------------------------------------------------------- |
---|
| 69 | // Threads |
---|
| 70 | extern "C" { |
---|
[c7a900a] | 71 | void __cfactx_invoke_thread(void (*main)(void *), void * this); |
---|
[75f3522] | 72 | } |
---|
| 73 | |
---|
[f7d6bb0] | 74 | __cfaabi_dbg_debug_do( |
---|
[ac2b598] | 75 | extern void __cfaabi_dbg_thread_register ( $thread * thrd ); |
---|
| 76 | extern void __cfaabi_dbg_thread_unregister( $thread * thrd ); |
---|
[f7d6bb0] | 77 | ) |
---|
| 78 | |
---|
[2d8f7b0] | 79 | // KERNEL ONLY unpark with out disabling interrupts |
---|
[9b1dcc2] | 80 | void __unpark( struct __processor_id_t *, $thread * thrd __cfaabi_dbg_ctx_param2 ); |
---|
[2d8f7b0] | 81 | |
---|
[92976d9] | 82 | //----------------------------------------------------------------------------- |
---|
| 83 | // I/O |
---|
[dd4e2d7] | 84 | void __kernel_io_startup ( cluster &, unsigned, bool ); |
---|
[f6660520] | 85 | void __kernel_io_finish_start( cluster & ); |
---|
| 86 | void __kernel_io_prepare_stop( cluster & ); |
---|
| 87 | void __kernel_io_shutdown ( cluster &, bool ); |
---|
[92976d9] | 88 | |
---|
[969b3fe] | 89 | //----------------------------------------------------------------------------- |
---|
| 90 | // Utils |
---|
[7768b8d] | 91 | #define KERNEL_STORAGE(T,X) __attribute((aligned(__alignof__(T)))) static char storage_##X[sizeof(T)] |
---|
[969b3fe] | 92 | |
---|
[13c5e19] | 93 | static inline uint64_t __tls_rand() { |
---|
| 94 | // kernelTLS.rand_seed ^= kernelTLS.rand_seed << 6; |
---|
| 95 | // kernelTLS.rand_seed ^= kernelTLS.rand_seed >> 21; |
---|
| 96 | // kernelTLS.rand_seed ^= kernelTLS.rand_seed << 7; |
---|
| 97 | // return kernelTLS.rand_seed; |
---|
| 98 | return __lehmer64( kernelTLS.rand_seed ); |
---|
[21184e3] | 99 | } |
---|
| 100 | |
---|
[de94a60] | 101 | |
---|
[a1a17a74] | 102 | void doregister( struct cluster & cltr ); |
---|
| 103 | void unregister( struct cluster & cltr ); |
---|
[de94a60] | 104 | |
---|
[ac2b598] | 105 | void doregister( struct cluster * cltr, struct $thread & thrd ); |
---|
| 106 | void unregister( struct cluster * cltr, struct $thread & thrd ); |
---|
[de94a60] | 107 | |
---|
[7768b8d] | 108 | //======================================================================= |
---|
| 109 | // Cluster lock API |
---|
| 110 | //======================================================================= |
---|
[b388ee81] | 111 | // Cells use by the reader writer lock |
---|
| 112 | // while not generic it only relies on a opaque pointer |
---|
[37ba662] | 113 | struct __attribute__((aligned(128))) __scheduler_lock_id_t { |
---|
[64a7146] | 114 | // Spin lock used as the underlying lock |
---|
[7768b8d] | 115 | volatile bool lock; |
---|
[64a7146] | 116 | |
---|
| 117 | // Handle pointing to the proc owning this cell |
---|
| 118 | // Used for allocating cells and debugging |
---|
| 119 | __processor_id_t * volatile handle; |
---|
| 120 | |
---|
| 121 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 122 | // Debug, check if this is owned for reading |
---|
| 123 | bool owned; |
---|
| 124 | #endif |
---|
[7768b8d] | 125 | }; |
---|
| 126 | |
---|
[64a7146] | 127 | static_assert( sizeof(struct __scheduler_lock_id_t) <= __alignof(struct __scheduler_lock_id_t)); |
---|
| 128 | |
---|
[7768b8d] | 129 | // Lock-Free registering/unregistering of threads |
---|
| 130 | // Register a processor to a given cluster and get its unique id in return |
---|
[9b1dcc2] | 131 | unsigned doregister( struct __processor_id_t * proc ); |
---|
[7768b8d] | 132 | |
---|
| 133 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
[9b1dcc2] | 134 | void unregister( struct __processor_id_t * proc ); |
---|
[7768b8d] | 135 | |
---|
| 136 | //======================================================================= |
---|
| 137 | // Reader-writer lock implementation |
---|
| 138 | // Concurrent with doregister/unregister, |
---|
| 139 | // i.e., threads can be added at any point during or between the entry/exit |
---|
[dca5802] | 140 | |
---|
| 141 | //----------------------------------------------------------------------- |
---|
| 142 | // simple spinlock underlying the RWLock |
---|
| 143 | // Blocking acquire |
---|
[7768b8d] | 144 | static inline void __atomic_acquire(volatile bool * ll) { |
---|
| 145 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) { |
---|
| 146 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED)) |
---|
| 147 | asm volatile("pause"); |
---|
| 148 | } |
---|
| 149 | /* paranoid */ verify(*ll); |
---|
| 150 | } |
---|
| 151 | |
---|
[dca5802] | 152 | // Non-Blocking acquire |
---|
[7768b8d] | 153 | static inline bool __atomic_try_acquire(volatile bool * ll) { |
---|
[b798713] | 154 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST); |
---|
[7768b8d] | 155 | } |
---|
| 156 | |
---|
[dca5802] | 157 | // Release |
---|
[7768b8d] | 158 | static inline void __atomic_unlock(volatile bool * ll) { |
---|
| 159 | /* paranoid */ verify(*ll); |
---|
| 160 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE); |
---|
| 161 | } |
---|
| 162 | |
---|
[b388ee81] | 163 | //----------------------------------------------------------------------- |
---|
| 164 | // Reader-Writer lock protecting the ready-queues |
---|
| 165 | // while this lock is mostly generic some aspects |
---|
| 166 | // have been hard-coded to for the ready-queue for |
---|
| 167 | // simplicity and performance |
---|
| 168 | struct __scheduler_RWLock_t { |
---|
| 169 | // total cachelines allocated |
---|
| 170 | unsigned int max; |
---|
| 171 | |
---|
| 172 | // cachelines currently in use |
---|
| 173 | volatile unsigned int alloc; |
---|
| 174 | |
---|
| 175 | // cachelines ready to itereate over |
---|
| 176 | // (!= to alloc when thread is in second half of doregister) |
---|
| 177 | volatile unsigned int ready; |
---|
| 178 | |
---|
| 179 | // writer lock |
---|
| 180 | volatile bool lock; |
---|
| 181 | |
---|
| 182 | // data pointer |
---|
[9b1dcc2] | 183 | __scheduler_lock_id_t * data; |
---|
[b388ee81] | 184 | }; |
---|
| 185 | |
---|
| 186 | void ?{}(__scheduler_RWLock_t & this); |
---|
| 187 | void ^?{}(__scheduler_RWLock_t & this); |
---|
| 188 | |
---|
| 189 | extern __scheduler_RWLock_t * __scheduler_lock; |
---|
| 190 | |
---|
[7768b8d] | 191 | //----------------------------------------------------------------------- |
---|
| 192 | // Reader side : acquire when using the ready queue to schedule but not |
---|
| 193 | // creating/destroying queues |
---|
[9b1dcc2] | 194 | static inline void ready_schedule_lock( struct __processor_id_t * proc) with(*__scheduler_lock) { |
---|
[7768b8d] | 195 | unsigned iproc = proc->id; |
---|
| 196 | /*paranoid*/ verify(data[iproc].handle == proc); |
---|
| 197 | /*paranoid*/ verify(iproc < ready); |
---|
| 198 | |
---|
| 199 | // Step 1 : make sure no writer are in the middle of the critical section |
---|
| 200 | while(__atomic_load_n(&lock, (int)__ATOMIC_RELAXED)) |
---|
| 201 | asm volatile("pause"); |
---|
| 202 | |
---|
| 203 | // Fence needed because we don't want to start trying to acquire the lock |
---|
| 204 | // before we read a false. |
---|
| 205 | // Not needed on x86 |
---|
| 206 | // std::atomic_thread_fence(std::memory_order_seq_cst); |
---|
| 207 | |
---|
| 208 | // Step 2 : acquire our local lock |
---|
| 209 | __atomic_acquire( &data[iproc].lock ); |
---|
| 210 | /*paranoid*/ verify(data[iproc].lock); |
---|
[64a7146] | 211 | |
---|
| 212 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 213 | // Debug, check if this is owned for reading |
---|
| 214 | data[iproc].owned = true; |
---|
| 215 | #endif |
---|
[7768b8d] | 216 | } |
---|
| 217 | |
---|
[9b1dcc2] | 218 | static inline void ready_schedule_unlock( struct __processor_id_t * proc) with(*__scheduler_lock) { |
---|
[7768b8d] | 219 | unsigned iproc = proc->id; |
---|
| 220 | /*paranoid*/ verify(data[iproc].handle == proc); |
---|
| 221 | /*paranoid*/ verify(iproc < ready); |
---|
| 222 | /*paranoid*/ verify(data[iproc].lock); |
---|
[64a7146] | 223 | /*paranoid*/ verify(data[iproc].owned); |
---|
| 224 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 225 | // Debug, check if this is owned for reading |
---|
| 226 | data[iproc].owned = false; |
---|
| 227 | #endif |
---|
[dca5802] | 228 | __atomic_unlock(&data[iproc].lock); |
---|
[7768b8d] | 229 | } |
---|
| 230 | |
---|
[64a7146] | 231 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 232 | static inline bool ready_schedule_islocked( struct __processor_id_t * proc) { |
---|
| 233 | return __scheduler_lock->data[proc->id].owned; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static inline bool ready_mutate_islocked() { |
---|
| 237 | return __scheduler_lock->lock; |
---|
| 238 | } |
---|
| 239 | #endif |
---|
| 240 | |
---|
[7768b8d] | 241 | //----------------------------------------------------------------------- |
---|
| 242 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
| 243 | // queues or removing them. |
---|
[b388ee81] | 244 | uint_fast32_t ready_mutate_lock( void ); |
---|
[7768b8d] | 245 | |
---|
[b388ee81] | 246 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ ); |
---|
[7768b8d] | 247 | |
---|
[b798713] | 248 | //======================================================================= |
---|
| 249 | // Ready-Queue API |
---|
[64a7146] | 250 | //----------------------------------------------------------------------- |
---|
| 251 | // pop thread from the ready queue of a cluster |
---|
| 252 | // returns 0p if empty |
---|
| 253 | __attribute__((hot)) bool query(struct cluster * cltr); |
---|
| 254 | |
---|
[dca5802] | 255 | //----------------------------------------------------------------------- |
---|
| 256 | // push thread onto a ready queue for a cluster |
---|
| 257 | // returns true if the list was previously empty, false otherwise |
---|
[504a7dc] | 258 | __attribute__((hot)) bool push(struct cluster * cltr, struct $thread * thrd); |
---|
[dca5802] | 259 | |
---|
| 260 | //----------------------------------------------------------------------- |
---|
| 261 | // pop thread from the ready queue of a cluster |
---|
| 262 | // returns 0p if empty |
---|
[504a7dc] | 263 | __attribute__((hot)) struct $thread * pop(struct cluster * cltr); |
---|
[dca5802] | 264 | |
---|
[13c5e19] | 265 | //----------------------------------------------------------------------- |
---|
| 266 | // remove thread from the ready queue of a cluster |
---|
| 267 | // returns bool if it wasn't found |
---|
| 268 | bool remove_head(struct cluster * cltr, struct $thread * thrd); |
---|
| 269 | |
---|
[dca5802] | 270 | //----------------------------------------------------------------------- |
---|
| 271 | // Increase the width of the ready queue (number of lanes) by 4 |
---|
[b798713] | 272 | void ready_queue_grow (struct cluster * cltr); |
---|
[dca5802] | 273 | |
---|
| 274 | //----------------------------------------------------------------------- |
---|
| 275 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
[b798713] | 276 | void ready_queue_shrink(struct cluster * cltr); |
---|
| 277 | |
---|
[dca5802] | 278 | //----------------------------------------------------------------------- |
---|
| 279 | // Statics call at the end of each thread to register statistics |
---|
[b798713] | 280 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[8834751] | 281 | static inline struct __stats_t * __tls_stats() { |
---|
| 282 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 283 | /* paranoid */ verify( kernelTLS.this_stats ); |
---|
| 284 | return kernelTLS.this_stats; |
---|
| 285 | } |
---|
[b798713] | 286 | #endif |
---|
[de94a60] | 287 | |
---|
[75f3522] | 288 | // Local Variables: // |
---|
| 289 | // mode: c // |
---|
| 290 | // tab-width: 4 // |
---|
[4aa2fb2] | 291 | // End: // |
---|