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(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 | __cfaabi_tls.rand_seed ^= rdtscl(); |
---|
125 | __cfaabi_tls.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 | __cfaabi_tls.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( ! __preemption_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( ! __preemption_enabled() ); |
---|
252 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
253 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
254 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
255 | /* paranoid */ verify( thrd_dst->state != Halted ); |
---|
256 | /* 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 |
---|
257 | /* 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 |
---|
258 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | // set context switch to the thread that the processor is executing |
---|
263 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
264 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
265 | |
---|
266 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
267 | /* 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 ); |
---|
268 | /* 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 ); |
---|
269 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
270 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
271 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
272 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
273 | |
---|
274 | // Reset global state |
---|
275 | kernelTLS().this_thread = 0p; |
---|
276 | |
---|
277 | // We just finished running a thread, there are a few things that could have happened. |
---|
278 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
279 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
280 | // 4 - Preempted |
---|
281 | // In case 1, we may have won a race so we can't write to the state again. |
---|
282 | // In case 2, we lost the race so we now own the thread. |
---|
283 | |
---|
284 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
285 | // The thread was preempted, reschedule it and reset the flag |
---|
286 | __schedule_thread( thrd_dst ); |
---|
287 | break RUNNING; |
---|
288 | } |
---|
289 | |
---|
290 | if(unlikely(thrd_dst->state == Halting)) { |
---|
291 | // The thread has halted, it should never be scheduled/run again |
---|
292 | // finish the thread |
---|
293 | __thread_finish( thrd_dst ); |
---|
294 | break RUNNING; |
---|
295 | } |
---|
296 | |
---|
297 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
298 | thrd_dst->state = Blocked; |
---|
299 | |
---|
300 | // set state of processor coroutine to active and the thread to inactive |
---|
301 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
302 | switch(old_ticket) { |
---|
303 | case TICKET_RUNNING: |
---|
304 | // This is case 1, the regular case, nothing more is needed |
---|
305 | break RUNNING; |
---|
306 | case TICKET_UNBLOCK: |
---|
307 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
308 | // In this case, just run it again. |
---|
309 | continue RUNNING; |
---|
310 | default: |
---|
311 | // This makes no sense, something is wrong abort |
---|
312 | abort(); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | // Just before returning to the processor, set the processor coroutine to active |
---|
317 | proc_cor->state = Active; |
---|
318 | |
---|
319 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
320 | } |
---|
321 | |
---|
322 | // KERNEL_ONLY |
---|
323 | void returnToKernel() { |
---|
324 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
325 | $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner); |
---|
326 | $thread * thrd_src = kernelTLS().this_thread; |
---|
327 | |
---|
328 | #if !defined(__CFA_NO_STATISTICS__) |
---|
329 | struct processor * last_proc = kernelTLS().this_processor; |
---|
330 | #endif |
---|
331 | |
---|
332 | // Run the thread on this processor |
---|
333 | { |
---|
334 | int local_errno = *__volatile_errno(); |
---|
335 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
336 | __x87_store; |
---|
337 | #endif |
---|
338 | /* paranoid */ verify( proc_cor->context.SP ); |
---|
339 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
340 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
341 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
342 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
343 | __x87_load; |
---|
344 | #endif |
---|
345 | *__volatile_errno() = local_errno; |
---|
346 | } |
---|
347 | |
---|
348 | #if !defined(__CFA_NO_STATISTICS__) |
---|
349 | if(last_proc != kernelTLS().this_processor) { |
---|
350 | __tls_stats()->ready.threads.migration++; |
---|
351 | } |
---|
352 | #endif |
---|
353 | |
---|
354 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
355 | /* 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 ); |
---|
356 | /* 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 ); |
---|
357 | } |
---|
358 | |
---|
359 | //----------------------------------------------------------------------------- |
---|
360 | // Scheduler routines |
---|
361 | // KERNEL ONLY |
---|
362 | void __schedule_thread( $thread * thrd ) { |
---|
363 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
364 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
365 | /* paranoid */ verify( thrd ); |
---|
366 | /* paranoid */ verify( thrd->state != Halted ); |
---|
367 | /* paranoid */ verify( thrd->curr_cluster ); |
---|
368 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
369 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
370 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
371 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
372 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
373 | /* paranoid */ #endif |
---|
374 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
375 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
376 | |
---|
377 | |
---|
378 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
379 | |
---|
380 | ready_schedule_lock(); |
---|
381 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid. |
---|
382 | struct cluster * cl = thrd->curr_cluster; |
---|
383 | |
---|
384 | // push the thread to the cluster ready-queue |
---|
385 | push( cl, thrd ); |
---|
386 | |
---|
387 | // variable thrd is no longer safe to use |
---|
388 | |
---|
389 | // wake the cluster using the save variable. |
---|
390 | __wake_one( cl ); |
---|
391 | ready_schedule_unlock(); |
---|
392 | |
---|
393 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
394 | } |
---|
395 | |
---|
396 | // KERNEL ONLY |
---|
397 | static inline $thread * __next_thread(cluster * this) with( *this ) { |
---|
398 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
399 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
400 | |
---|
401 | ready_schedule_lock(); |
---|
402 | $thread * thrd = pop( this ); |
---|
403 | ready_schedule_unlock(); |
---|
404 | |
---|
405 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
406 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
407 | return thrd; |
---|
408 | } |
---|
409 | |
---|
410 | // KERNEL ONLY |
---|
411 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) { |
---|
412 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
413 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
414 | |
---|
415 | ready_schedule_lock(); |
---|
416 | $thread * thrd = pop_slow( this ); |
---|
417 | ready_schedule_unlock(); |
---|
418 | |
---|
419 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
420 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
421 | return thrd; |
---|
422 | } |
---|
423 | |
---|
424 | void unpark( $thread * thrd ) { |
---|
425 | if( !thrd ) return; |
---|
426 | |
---|
427 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
428 | switch(old_ticket) { |
---|
429 | case TICKET_RUNNING: |
---|
430 | // Wake won the race, the thread will reschedule/rerun itself |
---|
431 | break; |
---|
432 | case TICKET_BLOCKED: |
---|
433 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
434 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
435 | |
---|
436 | { |
---|
437 | /* paranoid */ verify( publicTLS_get(this_proc_id) ); |
---|
438 | bool full = publicTLS_get(this_proc_id)->full_proc; |
---|
439 | if(full) disable_interrupts(); |
---|
440 | |
---|
441 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
442 | |
---|
443 | // Wake lost the race, |
---|
444 | __schedule_thread( thrd ); |
---|
445 | |
---|
446 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
447 | |
---|
448 | if(full) enable_interrupts( __cfaabi_dbg_ctx ); |
---|
449 | /* paranoid */ verify( publicTLS_get(this_proc_id) ); |
---|
450 | } |
---|
451 | |
---|
452 | break; |
---|
453 | default: |
---|
454 | // This makes no sense, something is wrong abort |
---|
455 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name); |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | void park( void ) { |
---|
460 | /* paranoid */ verify( __preemption_enabled() ); |
---|
461 | disable_interrupts(); |
---|
462 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
463 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION ); |
---|
464 | |
---|
465 | returnToKernel(); |
---|
466 | |
---|
467 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
468 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
469 | /* paranoid */ verify( __preemption_enabled() ); |
---|
470 | |
---|
471 | } |
---|
472 | |
---|
473 | extern "C" { |
---|
474 | // Leave the thread monitor |
---|
475 | // last routine called by a thread. |
---|
476 | // Should never return |
---|
477 | void __cfactx_thrd_leave() { |
---|
478 | $thread * thrd = active_thread(); |
---|
479 | $monitor * this = &thrd->self_mon; |
---|
480 | |
---|
481 | // Lock the monitor now |
---|
482 | lock( this->lock __cfaabi_dbg_ctx2 ); |
---|
483 | |
---|
484 | disable_interrupts(); |
---|
485 | |
---|
486 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
487 | /* paranoid */ verify( thrd->state == Active ); |
---|
488 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
489 | /* paranoid */ verify( kernelTLS().this_thread == thrd ); |
---|
490 | /* paranoid */ verify( thrd->context.SP ); |
---|
491 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : $thread %p has been corrupted.\n StackPointer too large.\n", thrd ); |
---|
492 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread %p has been corrupted.\n StackPointer too small.\n", thrd ); |
---|
493 | |
---|
494 | thrd->state = Halting; |
---|
495 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); } |
---|
496 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); } |
---|
497 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); } |
---|
498 | |
---|
499 | // Leave the thread |
---|
500 | returnToKernel(); |
---|
501 | |
---|
502 | // Control flow should never reach here! |
---|
503 | abort(); |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | // KERNEL ONLY |
---|
508 | bool force_yield( __Preemption_Reason reason ) { |
---|
509 | /* paranoid */ verify( __preemption_enabled() ); |
---|
510 | disable_interrupts(); |
---|
511 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
512 | |
---|
513 | $thread * thrd = kernelTLS().this_thread; |
---|
514 | /* paranoid */ verify(thrd->state == Active); |
---|
515 | |
---|
516 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
517 | // it was going to park itself. If that is the case and it is already using the |
---|
518 | // intrusive fields then we can't use them to preempt the thread |
---|
519 | // If that is the case, abandon the preemption. |
---|
520 | bool preempted = false; |
---|
521 | if(thrd->link.next == 0p) { |
---|
522 | preempted = true; |
---|
523 | thrd->preempted = reason; |
---|
524 | returnToKernel(); |
---|
525 | } |
---|
526 | |
---|
527 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
528 | enable_interrupts_noPoll(); |
---|
529 | /* paranoid */ verify( __preemption_enabled() ); |
---|
530 | |
---|
531 | return preempted; |
---|
532 | } |
---|
533 | |
---|
534 | //============================================================================================= |
---|
535 | // Kernel Idle Sleep |
---|
536 | //============================================================================================= |
---|
537 | // Wake a thread from the front if there are any |
---|
538 | static void __wake_one(cluster * this) { |
---|
539 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
540 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
541 | |
---|
542 | // Check if there is a sleeping processor |
---|
543 | processor * p; |
---|
544 | unsigned idle; |
---|
545 | unsigned total; |
---|
546 | [idle, total, p] = query(this->idles); |
---|
547 | |
---|
548 | // If no one is sleeping, we are done |
---|
549 | if( idle == 0 ) return; |
---|
550 | |
---|
551 | // We found a processor, wake it up |
---|
552 | post( p->idle ); |
---|
553 | |
---|
554 | #if !defined(__CFA_NO_STATISTICS__) |
---|
555 | __tls_stats()->ready.sleep.wakes++; |
---|
556 | #endif |
---|
557 | |
---|
558 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
559 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
560 | |
---|
561 | return; |
---|
562 | } |
---|
563 | |
---|
564 | // Unconditionnaly wake a thread |
---|
565 | void __wake_proc(processor * this) { |
---|
566 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
567 | |
---|
568 | disable_interrupts(); |
---|
569 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
570 | post( this->idle ); |
---|
571 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
572 | } |
---|
573 | |
---|
574 | static void push (__cluster_idles & this, processor & proc) { |
---|
575 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
576 | lock( this ); |
---|
577 | this.idle++; |
---|
578 | /* paranoid */ verify( this.idle <= this.total ); |
---|
579 | |
---|
580 | insert_first(this.list, proc); |
---|
581 | unlock( this ); |
---|
582 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
583 | } |
---|
584 | |
---|
585 | static void remove(__cluster_idles & this, processor & proc) { |
---|
586 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
587 | lock( this ); |
---|
588 | this.idle--; |
---|
589 | /* paranoid */ verify( this.idle >= 0 ); |
---|
590 | |
---|
591 | remove(proc); |
---|
592 | unlock( this ); |
---|
593 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
594 | } |
---|
595 | |
---|
596 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) { |
---|
597 | for() { |
---|
598 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST); |
---|
599 | if( 1 == (l % 2) ) { Pause(); continue; } |
---|
600 | unsigned idle = this.idle; |
---|
601 | unsigned total = this.total; |
---|
602 | processor * proc = &this.list`first; |
---|
603 | // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it |
---|
604 | asm volatile("": : :"memory"); |
---|
605 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; } |
---|
606 | return [idle, total, proc]; |
---|
607 | } |
---|
608 | } |
---|
609 | |
---|
610 | //============================================================================================= |
---|
611 | // Unexpected Terminating logic |
---|
612 | //============================================================================================= |
---|
613 | static __spinlock_t kernel_abort_lock; |
---|
614 | static bool kernel_abort_called = false; |
---|
615 | |
---|
616 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
617 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
618 | // the globalAbort flag is true. |
---|
619 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
620 | |
---|
621 | // disable interrupts, it no longer makes sense to try to interrupt this processor |
---|
622 | disable_interrupts(); |
---|
623 | |
---|
624 | // first task to abort ? |
---|
625 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
626 | unlock( kernel_abort_lock ); |
---|
627 | |
---|
628 | sigset_t mask; |
---|
629 | sigemptyset( &mask ); |
---|
630 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
631 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals |
---|
632 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
633 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
634 | } |
---|
635 | else { |
---|
636 | kernel_abort_called = true; |
---|
637 | unlock( kernel_abort_lock ); |
---|
638 | } |
---|
639 | |
---|
640 | return __cfaabi_tls.this_thread; |
---|
641 | } |
---|
642 | |
---|
643 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
644 | $thread * thrd = ( $thread * ) kernel_data; |
---|
645 | |
---|
646 | if(thrd) { |
---|
647 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
648 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
649 | |
---|
650 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
651 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
652 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
653 | } |
---|
654 | else { |
---|
655 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
656 | } |
---|
657 | } |
---|
658 | else { |
---|
659 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
660 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
665 | return get_coroutine(kernelTLS().this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
666 | } |
---|
667 | |
---|
668 | static __spinlock_t kernel_debug_lock; |
---|
669 | |
---|
670 | extern "C" { |
---|
671 | void __cfaabi_bits_acquire() { |
---|
672 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
673 | } |
---|
674 | |
---|
675 | void __cfaabi_bits_release() { |
---|
676 | unlock( kernel_debug_lock ); |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | //============================================================================================= |
---|
681 | // Kernel Utilities |
---|
682 | //============================================================================================= |
---|
683 | //----------------------------------------------------------------------------- |
---|
684 | // Locks |
---|
685 | void ?{}( semaphore & this, int count = 1 ) { |
---|
686 | (this.lock){}; |
---|
687 | this.count = count; |
---|
688 | (this.waiting){}; |
---|
689 | } |
---|
690 | void ^?{}(semaphore & this) {} |
---|
691 | |
---|
692 | bool P(semaphore & this) with( this ){ |
---|
693 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
694 | count -= 1; |
---|
695 | if ( count < 0 ) { |
---|
696 | // queue current task |
---|
697 | append( waiting, active_thread() ); |
---|
698 | |
---|
699 | // atomically release spin lock and block |
---|
700 | unlock( lock ); |
---|
701 | park(); |
---|
702 | return true; |
---|
703 | } |
---|
704 | else { |
---|
705 | unlock( lock ); |
---|
706 | return false; |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | bool V(semaphore & this) with( this ) { |
---|
711 | $thread * thrd = 0p; |
---|
712 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
713 | count += 1; |
---|
714 | if ( count <= 0 ) { |
---|
715 | // remove task at head of waiting list |
---|
716 | thrd = pop_head( waiting ); |
---|
717 | } |
---|
718 | |
---|
719 | unlock( lock ); |
---|
720 | |
---|
721 | // make new owner |
---|
722 | unpark( thrd ); |
---|
723 | |
---|
724 | return thrd != 0p; |
---|
725 | } |
---|
726 | |
---|
727 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
728 | $thread * thrd = 0p; |
---|
729 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
730 | int release = max(-count, (int)diff); |
---|
731 | count += diff; |
---|
732 | for(release) { |
---|
733 | unpark( pop_head( waiting ) ); |
---|
734 | } |
---|
735 | |
---|
736 | unlock( lock ); |
---|
737 | |
---|
738 | return thrd != 0p; |
---|
739 | } |
---|
740 | |
---|
741 | //----------------------------------------------------------------------------- |
---|
742 | // Debug |
---|
743 | __cfaabi_dbg_debug_do( |
---|
744 | extern "C" { |
---|
745 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
746 | this.prev_name = prev_name; |
---|
747 | this.prev_thrd = kernelTLS().this_thread; |
---|
748 | } |
---|
749 | } |
---|
750 | ) |
---|
751 | |
---|
752 | //----------------------------------------------------------------------------- |
---|
753 | // Debug |
---|
754 | bool threading_enabled(void) __attribute__((const)) { |
---|
755 | return true; |
---|
756 | } |
---|
757 | |
---|
758 | //----------------------------------------------------------------------------- |
---|
759 | // Statistics |
---|
760 | #if !defined(__CFA_NO_STATISTICS__) |
---|
761 | void print_halts( processor & this ) { |
---|
762 | this.print_halts = true; |
---|
763 | } |
---|
764 | |
---|
765 | void print_stats_now( cluster & this, int flags ) { |
---|
766 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this ); |
---|
767 | } |
---|
768 | |
---|
769 | extern int __print_alarm_stats; |
---|
770 | void print_alarm_stats() { |
---|
771 | __print_alarm_stats = -1; |
---|
772 | } |
---|
773 | #endif |
---|
774 | // Local Variables: // |
---|
775 | // mode: c // |
---|
776 | // tab-width: 4 // |
---|
777 | // End: // |
---|