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 | //
|
---|
7 | // kernel.c --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Tue Jan 17 12:27:26 2017
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Thu Jul 9 06:22:54 2020
|
---|
13 | // Update Count : 66
|
---|
14 | //
|
---|
15 |
|
---|
16 | #define __cforall_thread__
|
---|
17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
|
---|
18 |
|
---|
19 | //C Includes
|
---|
20 | #include <errno.h>
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <signal.h>
|
---|
23 | #include <unistd.h>
|
---|
24 |
|
---|
25 | //CFA Includes
|
---|
26 | #include "kernel_private.hfa"
|
---|
27 | #include "preemption.hfa"
|
---|
28 |
|
---|
29 | //Private includes
|
---|
30 | #define __CFA_INVOKE_PRIVATE__
|
---|
31 | #include "invoke.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | //-----------------------------------------------------------------------------
|
---|
35 | // Some assembly required
|
---|
36 | #if defined( __i386 )
|
---|
37 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
|
---|
38 | // fcw : X87 FPU control word (preserved across function calls)
|
---|
39 | #define __x87_store \
|
---|
40 | uint32_t __mxcr; \
|
---|
41 | uint16_t __fcw; \
|
---|
42 | __asm__ volatile ( \
|
---|
43 | "stmxcsr %0\n" \
|
---|
44 | "fnstcw %1\n" \
|
---|
45 | : "=m" (__mxcr),\
|
---|
46 | "=m" (__fcw) \
|
---|
47 | )
|
---|
48 |
|
---|
49 | #define __x87_load \
|
---|
50 | __asm__ volatile ( \
|
---|
51 | "fldcw %1\n" \
|
---|
52 | "ldmxcsr %0\n" \
|
---|
53 | ::"m" (__mxcr),\
|
---|
54 | "m" (__fcw) \
|
---|
55 | )
|
---|
56 |
|
---|
57 | #elif defined( __x86_64 )
|
---|
58 | #define __x87_store \
|
---|
59 | uint32_t __mxcr; \
|
---|
60 | uint16_t __fcw; \
|
---|
61 | __asm__ volatile ( \
|
---|
62 | "stmxcsr %0\n" \
|
---|
63 | "fnstcw %1\n" \
|
---|
64 | : "=m" (__mxcr),\
|
---|
65 | "=m" (__fcw) \
|
---|
66 | )
|
---|
67 |
|
---|
68 | #define __x87_load \
|
---|
69 | __asm__ volatile ( \
|
---|
70 | "fldcw %1\n" \
|
---|
71 | "ldmxcsr %0\n" \
|
---|
72 | :: "m" (__mxcr),\
|
---|
73 | "m" (__fcw) \
|
---|
74 | )
|
---|
75 |
|
---|
76 |
|
---|
77 | #elif defined( __ARM_ARCH )
|
---|
78 | #else
|
---|
79 | #error unknown hardware architecture
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | extern $thread * mainThread;
|
---|
83 | extern processor * mainProcessor;
|
---|
84 |
|
---|
85 | //-----------------------------------------------------------------------------
|
---|
86 | // Kernel Scheduling logic
|
---|
87 | static $thread * __next_thread(cluster * this);
|
---|
88 | static $thread * __next_thread_slow(cluster * this);
|
---|
89 | static void __run_thread(processor * this, $thread * dst);
|
---|
90 | static void __wake_one(struct __processor_id_t * id, cluster * cltr);
|
---|
91 |
|
---|
92 | static void push (__cluster_idles & idles, processor & proc);
|
---|
93 | static void remove(__cluster_idles & idles, processor & proc);
|
---|
94 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles );
|
---|
95 |
|
---|
96 |
|
---|
97 | //=============================================================================================
|
---|
98 | // Kernel Scheduling logic
|
---|
99 | //=============================================================================================
|
---|
100 | //Main of the processor contexts
|
---|
101 | void main(processorCtx_t & runner) {
|
---|
102 | // Because of a bug, we couldn't initialized the seed on construction
|
---|
103 | // Do it here
|
---|
104 | kernelTLS.rand_seed ^= rdtscl();
|
---|
105 |
|
---|
106 | processor * this = runner.proc;
|
---|
107 | verify(this);
|
---|
108 |
|
---|
109 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
|
---|
110 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
111 | if( this->print_halts ) {
|
---|
112 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | {
|
---|
117 | // Setup preemption data
|
---|
118 | preemption_scope scope = { this };
|
---|
119 |
|
---|
120 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
|
---|
121 |
|
---|
122 | $thread * readyThread = 0p;
|
---|
123 | MAIN_LOOP:
|
---|
124 | for() {
|
---|
125 | // Try to get the next thread
|
---|
126 | readyThread = __next_thread( this->cltr );
|
---|
127 |
|
---|
128 | if( !readyThread ) {
|
---|
129 | readyThread = __next_thread_slow( this->cltr );
|
---|
130 | }
|
---|
131 |
|
---|
132 | HALT:
|
---|
133 | if( !readyThread ) {
|
---|
134 | // Don't block if we are done
|
---|
135 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
136 |
|
---|
137 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
138 | __tls_stats()->ready.sleep.halts++;
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | // Push self to idle stack
|
---|
142 | push(this->cltr->idles, * this);
|
---|
143 |
|
---|
144 | // Confirm the ready-queue is empty
|
---|
145 | readyThread = __next_thread_slow( this->cltr );
|
---|
146 | if( readyThread ) {
|
---|
147 | // A thread was found, cancel the halt
|
---|
148 | remove(this->cltr->idles, * this);
|
---|
149 |
|
---|
150 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
151 | __tls_stats()->ready.sleep.cancels++;
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | // continue the mai loop
|
---|
155 | break HALT;
|
---|
156 | }
|
---|
157 |
|
---|
158 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
159 | if(this->print_halts) {
|
---|
160 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
|
---|
161 | }
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | wait( this->idle );
|
---|
165 |
|
---|
166 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
167 | if(this->print_halts) {
|
---|
168 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | // We were woken up, remove self from idle
|
---|
173 | remove(this->cltr->idles, * this);
|
---|
174 |
|
---|
175 | // DON'T just proceed, start looking again
|
---|
176 | continue MAIN_LOOP;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* paranoid */ verify( readyThread );
|
---|
180 |
|
---|
181 | // We found a thread run it
|
---|
182 | __run_thread(this, readyThread);
|
---|
183 |
|
---|
184 | // Are we done?
|
---|
185 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
186 | }
|
---|
187 |
|
---|
188 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
|
---|
189 | }
|
---|
190 |
|
---|
191 | V( this->terminated );
|
---|
192 |
|
---|
193 | if(this == mainProcessor) {
|
---|
194 | // HACK : the coroutine context switch expects this_thread to be set
|
---|
195 | // and it make sense for it to be set in all other cases except here
|
---|
196 | // fake it
|
---|
197 | kernelTLS.this_thread = mainThread;
|
---|
198 | }
|
---|
199 |
|
---|
200 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
|
---|
201 | }
|
---|
202 |
|
---|
203 | static int * __volatile_errno() __attribute__((noinline));
|
---|
204 | static int * __volatile_errno() { asm(""); return &errno; }
|
---|
205 |
|
---|
206 | // KERNEL ONLY
|
---|
207 | // runThread runs a thread by context switching
|
---|
208 | // from the processor coroutine to the target thread
|
---|
209 | static void __run_thread(processor * this, $thread * thrd_dst) {
|
---|
210 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
211 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted);
|
---|
212 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
|
---|
213 | __builtin_prefetch( thrd_dst->context.SP );
|
---|
214 |
|
---|
215 | $coroutine * proc_cor = get_coroutine(this->runner);
|
---|
216 |
|
---|
217 | // Update global state
|
---|
218 | kernelTLS.this_thread = thrd_dst;
|
---|
219 |
|
---|
220 | // set state of processor coroutine to inactive
|
---|
221 | verify(proc_cor->state == Active);
|
---|
222 | proc_cor->state = Blocked;
|
---|
223 |
|
---|
224 | // Actually run the thread
|
---|
225 | RUNNING: while(true) {
|
---|
226 | thrd_dst->preempted = __NO_PREEMPTION;
|
---|
227 | thrd_dst->state = Active;
|
---|
228 |
|
---|
229 | __cfaabi_dbg_debug_do(
|
---|
230 | thrd_dst->park_stale = true;
|
---|
231 | thrd_dst->unpark_stale = true;
|
---|
232 | )
|
---|
233 |
|
---|
234 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
235 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
|
---|
236 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
---|
237 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
---|
238 |
|
---|
239 | // set context switch to the thread that the processor is executing
|
---|
240 | verify( thrd_dst->context.SP );
|
---|
241 | __cfactx_switch( &proc_cor->context, &thrd_dst->context );
|
---|
242 | // when __cfactx_switch returns we are back in the processor coroutine
|
---|
243 |
|
---|
244 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst );
|
---|
245 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst );
|
---|
246 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
|
---|
247 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
248 |
|
---|
249 |
|
---|
250 | // We just finished running a thread, there are a few things that could have happened.
|
---|
251 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
|
---|
252 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
|
---|
253 | // 4 - Preempted
|
---|
254 | // In case 1, we may have won a race so we can't write to the state again.
|
---|
255 | // In case 2, we lost the race so we now own the thread.
|
---|
256 |
|
---|
257 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
|
---|
258 | // The thread was preempted, reschedule it and reset the flag
|
---|
259 | __schedule_thread( (__processor_id_t*)this, thrd_dst );
|
---|
260 | break RUNNING;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if(unlikely(thrd_dst->state == Halted)) {
|
---|
264 | // The thread has halted, it should never be scheduled/run again
|
---|
265 | // We may need to wake someone up here since
|
---|
266 | unpark( this->destroyer __cfaabi_dbg_ctx2 );
|
---|
267 | this->destroyer = 0p;
|
---|
268 | break RUNNING;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* paranoid */ verify( thrd_dst->state == Active );
|
---|
272 | thrd_dst->state = Blocked;
|
---|
273 |
|
---|
274 | // set state of processor coroutine to active and the thread to inactive
|
---|
275 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
276 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; )
|
---|
277 | switch(old_ticket) {
|
---|
278 | case 1:
|
---|
279 | // This is case 1, the regular case, nothing more is needed
|
---|
280 | break RUNNING;
|
---|
281 | case 2:
|
---|
282 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking
|
---|
283 | // In this case, just run it again.
|
---|
284 | continue RUNNING;
|
---|
285 | default:
|
---|
286 | // This makes no sense, something is wrong abort
|
---|
287 | abort();
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | // Just before returning to the processor, set the processor coroutine to active
|
---|
292 | proc_cor->state = Active;
|
---|
293 | kernelTLS.this_thread = 0p;
|
---|
294 |
|
---|
295 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
296 | }
|
---|
297 |
|
---|
298 | // KERNEL_ONLY
|
---|
299 | void returnToKernel() {
|
---|
300 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
301 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
|
---|
302 | $thread * thrd_src = kernelTLS.this_thread;
|
---|
303 |
|
---|
304 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
305 | struct processor * last_proc = kernelTLS.this_processor;
|
---|
306 | #endif
|
---|
307 |
|
---|
308 | // Run the thread on this processor
|
---|
309 | {
|
---|
310 | int local_errno = *__volatile_errno();
|
---|
311 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
312 | __x87_store;
|
---|
313 | #endif
|
---|
314 | verify( proc_cor->context.SP );
|
---|
315 | __cfactx_switch( &thrd_src->context, &proc_cor->context );
|
---|
316 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
317 | __x87_load;
|
---|
318 | #endif
|
---|
319 | *__volatile_errno() = local_errno;
|
---|
320 | }
|
---|
321 |
|
---|
322 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
323 | if(last_proc != kernelTLS.this_processor) {
|
---|
324 | __tls_stats()->ready.threads.migration++;
|
---|
325 | }
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
329 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src );
|
---|
330 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src );
|
---|
331 | }
|
---|
332 |
|
---|
333 | //-----------------------------------------------------------------------------
|
---|
334 | // Scheduler routines
|
---|
335 | // KERNEL ONLY
|
---|
336 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) {
|
---|
337 | /* paranoid */ verify( thrd );
|
---|
338 | /* paranoid */ verify( thrd->state != Halted );
|
---|
339 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
340 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
|
---|
341 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
|
---|
342 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
343 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
|
---|
344 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
345 | /* paranoid */ #endif
|
---|
346 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
|
---|
347 |
|
---|
348 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
|
---|
349 |
|
---|
350 | ready_schedule_lock ( id );
|
---|
351 | push( thrd->curr_cluster, thrd );
|
---|
352 | __wake_one(id, thrd->curr_cluster);
|
---|
353 | ready_schedule_unlock( id );
|
---|
354 |
|
---|
355 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
356 | }
|
---|
357 |
|
---|
358 | // KERNEL ONLY
|
---|
359 | static inline $thread * __next_thread(cluster * this) with( *this ) {
|
---|
360 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
361 |
|
---|
362 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor );
|
---|
363 | $thread * thrd = pop( this );
|
---|
364 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
|
---|
365 |
|
---|
366 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
367 | return thrd;
|
---|
368 | }
|
---|
369 |
|
---|
370 | // KERNEL ONLY
|
---|
371 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) {
|
---|
372 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
373 |
|
---|
374 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor );
|
---|
375 | $thread * thrd = pop_slow( this );
|
---|
376 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
|
---|
377 |
|
---|
378 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
379 | return thrd;
|
---|
380 | }
|
---|
381 |
|
---|
382 | // KERNEL ONLY unpark with out disabling interrupts
|
---|
383 | void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) {
|
---|
384 | // record activity
|
---|
385 | __cfaabi_dbg_record_thrd( *thrd, false, caller );
|
---|
386 |
|
---|
387 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
388 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; )
|
---|
389 | switch(old_ticket) {
|
---|
390 | case 1:
|
---|
391 | // Wake won the race, the thread will reschedule/rerun itself
|
---|
392 | break;
|
---|
393 | case 0:
|
---|
394 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
|
---|
395 | /* paranoid */ verify( thrd->state == Blocked );
|
---|
396 |
|
---|
397 | // Wake lost the race,
|
---|
398 | __schedule_thread( id, thrd );
|
---|
399 | break;
|
---|
400 | default:
|
---|
401 | // This makes no sense, something is wrong abort
|
---|
402 | abort();
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
|
---|
407 | if( !thrd ) return;
|
---|
408 |
|
---|
409 | disable_interrupts();
|
---|
410 | __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 );
|
---|
411 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
412 | }
|
---|
413 |
|
---|
414 | void park( __cfaabi_dbg_ctx_param ) {
|
---|
415 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
---|
416 | disable_interrupts();
|
---|
417 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
418 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
|
---|
419 |
|
---|
420 | // record activity
|
---|
421 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
|
---|
422 |
|
---|
423 | returnToKernel();
|
---|
424 |
|
---|
425 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
426 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
427 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
---|
428 |
|
---|
429 | }
|
---|
430 |
|
---|
431 | // KERNEL ONLY
|
---|
432 | void __leave_thread() {
|
---|
433 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
434 | returnToKernel();
|
---|
435 | abort();
|
---|
436 | }
|
---|
437 |
|
---|
438 | // KERNEL ONLY
|
---|
439 | bool force_yield( __Preemption_Reason reason ) {
|
---|
440 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
---|
441 | disable_interrupts();
|
---|
442 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
443 |
|
---|
444 | $thread * thrd = kernelTLS.this_thread;
|
---|
445 | /* paranoid */ verify(thrd->state == Active);
|
---|
446 |
|
---|
447 | // SKULLDUGGERY: It is possible that we are preempting this thread just before
|
---|
448 | // it was going to park itself. If that is the case and it is already using the
|
---|
449 | // intrusive fields then we can't use them to preempt the thread
|
---|
450 | // If that is the case, abandon the preemption.
|
---|
451 | bool preempted = false;
|
---|
452 | if(thrd->link.next == 0p) {
|
---|
453 | preempted = true;
|
---|
454 | thrd->preempted = reason;
|
---|
455 | returnToKernel();
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
459 | enable_interrupts_noPoll();
|
---|
460 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
---|
461 |
|
---|
462 | return preempted;
|
---|
463 | }
|
---|
464 |
|
---|
465 | //=============================================================================================
|
---|
466 | // Kernel Idle Sleep
|
---|
467 | //=============================================================================================
|
---|
468 | // Wake a thread from the front if there are any
|
---|
469 | static void __wake_one(struct __processor_id_t * id, cluster * this) {
|
---|
470 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
471 | /* paranoid */ verify( ready_schedule_islocked( id ) );
|
---|
472 |
|
---|
473 | // Check if there is a sleeping processor
|
---|
474 | processor * p;
|
---|
475 | unsigned idle;
|
---|
476 | unsigned total;
|
---|
477 | [idle, total, p] = query(this->idles);
|
---|
478 |
|
---|
479 | // If no one is sleeping, we are done
|
---|
480 | if( idle == 0 ) return;
|
---|
481 |
|
---|
482 | // We found a processor, wake it up
|
---|
483 | post( p->idle );
|
---|
484 |
|
---|
485 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
486 | __tls_stats()->ready.sleep.wakes++;
|
---|
487 | #endif
|
---|
488 |
|
---|
489 | /* paranoid */ verify( ready_schedule_islocked( id ) );
|
---|
490 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
491 |
|
---|
492 | return;
|
---|
493 | }
|
---|
494 |
|
---|
495 | // Unconditionnaly wake a thread
|
---|
496 | void __wake_proc(processor * this) {
|
---|
497 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
|
---|
498 |
|
---|
499 | disable_interrupts();
|
---|
500 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
501 | bool ret = post( this->idle );
|
---|
502 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
503 | }
|
---|
504 |
|
---|
505 | static void push (__cluster_idles & this, processor & proc) {
|
---|
506 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
507 | lock( this );
|
---|
508 | this.idle++;
|
---|
509 | /* paranoid */ verify( this.idle <= this.total );
|
---|
510 |
|
---|
511 | insert_first(this.list, proc);
|
---|
512 | unlock( this );
|
---|
513 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
514 | }
|
---|
515 |
|
---|
516 | static void remove(__cluster_idles & this, processor & proc) {
|
---|
517 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
518 | lock( this );
|
---|
519 | this.idle--;
|
---|
520 | /* paranoid */ verify( this.idle >= 0 );
|
---|
521 |
|
---|
522 | remove(proc);
|
---|
523 | unlock( this );
|
---|
524 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
525 | }
|
---|
526 |
|
---|
527 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) {
|
---|
528 | for() {
|
---|
529 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST);
|
---|
530 | if( 1 == (l % 2) ) { Pause(); continue; }
|
---|
531 | unsigned idle = this.idle;
|
---|
532 | unsigned total = this.total;
|
---|
533 | processor * proc = &this.list`first;
|
---|
534 | // Thread fence is unnecessary, but gcc-8 and older incorrectly reorder code without it
|
---|
535 | __atomic_thread_fence(__ATOMIC_SEQ_CST);
|
---|
536 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; }
|
---|
537 | return [idle, total, proc];
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | //=============================================================================================
|
---|
542 | // Unexpected Terminating logic
|
---|
543 | //=============================================================================================
|
---|
544 | static __spinlock_t kernel_abort_lock;
|
---|
545 | static bool kernel_abort_called = false;
|
---|
546 |
|
---|
547 | void * kernel_abort(void) __attribute__ ((__nothrow__)) {
|
---|
548 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
---|
549 | // the globalAbort flag is true.
|
---|
550 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
---|
551 |
|
---|
552 | // first task to abort ?
|
---|
553 | if ( kernel_abort_called ) { // not first task to abort ?
|
---|
554 | unlock( kernel_abort_lock );
|
---|
555 |
|
---|
556 | sigset_t mask;
|
---|
557 | sigemptyset( &mask );
|
---|
558 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
---|
559 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals
|
---|
560 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
---|
561 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
---|
562 | }
|
---|
563 | else {
|
---|
564 | kernel_abort_called = true;
|
---|
565 | unlock( kernel_abort_lock );
|
---|
566 | }
|
---|
567 |
|
---|
568 | return kernelTLS.this_thread;
|
---|
569 | }
|
---|
570 |
|
---|
571 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
---|
572 | $thread * thrd = ( $thread * ) kernel_data;
|
---|
573 |
|
---|
574 | if(thrd) {
|
---|
575 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
576 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
577 |
|
---|
578 | if ( &thrd->self_cor != thrd->curr_cor ) {
|
---|
579 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
|
---|
580 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
581 | }
|
---|
582 | else {
|
---|
583 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
|
---|
584 | }
|
---|
585 | }
|
---|
586 | else {
|
---|
587 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
|
---|
588 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
593 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
---|
594 | }
|
---|
595 |
|
---|
596 | static __spinlock_t kernel_debug_lock;
|
---|
597 |
|
---|
598 | extern "C" {
|
---|
599 | void __cfaabi_bits_acquire() {
|
---|
600 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
601 | }
|
---|
602 |
|
---|
603 | void __cfaabi_bits_release() {
|
---|
604 | unlock( kernel_debug_lock );
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | //=============================================================================================
|
---|
609 | // Kernel Utilities
|
---|
610 | //=============================================================================================
|
---|
611 | //-----------------------------------------------------------------------------
|
---|
612 | // Locks
|
---|
613 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
614 | (this.lock){};
|
---|
615 | this.count = count;
|
---|
616 | (this.waiting){};
|
---|
617 | }
|
---|
618 | void ^?{}(semaphore & this) {}
|
---|
619 |
|
---|
620 | bool P(semaphore & this) with( this ){
|
---|
621 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
622 | count -= 1;
|
---|
623 | if ( count < 0 ) {
|
---|
624 | // queue current task
|
---|
625 | append( waiting, kernelTLS.this_thread );
|
---|
626 |
|
---|
627 | // atomically release spin lock and block
|
---|
628 | unlock( lock );
|
---|
629 | park( __cfaabi_dbg_ctx );
|
---|
630 | return true;
|
---|
631 | }
|
---|
632 | else {
|
---|
633 | unlock( lock );
|
---|
634 | return false;
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | bool V(semaphore & this) with( this ) {
|
---|
639 | $thread * thrd = 0p;
|
---|
640 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
641 | count += 1;
|
---|
642 | if ( count <= 0 ) {
|
---|
643 | // remove task at head of waiting list
|
---|
644 | thrd = pop_head( waiting );
|
---|
645 | }
|
---|
646 |
|
---|
647 | unlock( lock );
|
---|
648 |
|
---|
649 | // make new owner
|
---|
650 | unpark( thrd __cfaabi_dbg_ctx2 );
|
---|
651 |
|
---|
652 | return thrd != 0p;
|
---|
653 | }
|
---|
654 |
|
---|
655 | bool V(semaphore & this, unsigned diff) with( this ) {
|
---|
656 | $thread * thrd = 0p;
|
---|
657 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
658 | int release = max(-count, (int)diff);
|
---|
659 | count += diff;
|
---|
660 | for(release) {
|
---|
661 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 );
|
---|
662 | }
|
---|
663 |
|
---|
664 | unlock( lock );
|
---|
665 |
|
---|
666 | return thrd != 0p;
|
---|
667 | }
|
---|
668 |
|
---|
669 | //-----------------------------------------------------------------------------
|
---|
670 | // Debug
|
---|
671 | __cfaabi_dbg_debug_do(
|
---|
672 | extern "C" {
|
---|
673 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
|
---|
674 | this.prev_name = prev_name;
|
---|
675 | this.prev_thrd = kernelTLS.this_thread;
|
---|
676 | }
|
---|
677 |
|
---|
678 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
|
---|
679 | if(park) {
|
---|
680 | this.park_caller = prev_name;
|
---|
681 | this.park_stale = false;
|
---|
682 | }
|
---|
683 | else {
|
---|
684 | this.unpark_caller = prev_name;
|
---|
685 | this.unpark_stale = false;
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 | )
|
---|
690 |
|
---|
691 | //-----------------------------------------------------------------------------
|
---|
692 | // Debug
|
---|
693 | bool threading_enabled(void) __attribute__((const)) {
|
---|
694 | return true;
|
---|
695 | }
|
---|
696 |
|
---|
697 | //-----------------------------------------------------------------------------
|
---|
698 | // Statistics
|
---|
699 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
700 | void print_halts( processor & this ) {
|
---|
701 | this.print_halts = true;
|
---|
702 | }
|
---|
703 | #endif
|
---|
704 | // Local Variables: //
|
---|
705 | // mode: c //
|
---|
706 | // tab-width: 4 //
|
---|
707 | // End: //
|
---|