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