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 Feb 8 23:52:19 2018
|
---|
13 | // Update Count : 5
|
---|
14 | //
|
---|
15 |
|
---|
16 | //C Includes
|
---|
17 | #include <stddef.h>
|
---|
18 | extern "C" {
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <fenv.h>
|
---|
21 | #include <sys/resource.h>
|
---|
22 | #include <signal.h>
|
---|
23 | #include <unistd.h>
|
---|
24 | }
|
---|
25 |
|
---|
26 | //CFA Includes
|
---|
27 | #include "kernel_private.h"
|
---|
28 | #include "preemption.h"
|
---|
29 | #include "startup.h"
|
---|
30 |
|
---|
31 | //Private includes
|
---|
32 | #define __CFA_INVOKE_PRIVATE__
|
---|
33 | #include "invoke.h"
|
---|
34 |
|
---|
35 | //Start and stop routine for the kernel, declared first to make sure they run first
|
---|
36 | void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
---|
37 | void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
---|
38 |
|
---|
39 | //-----------------------------------------------------------------------------
|
---|
40 | // Kernel storage
|
---|
41 | KERNEL_STORAGE(cluster, mainCluster);
|
---|
42 | KERNEL_STORAGE(processor, mainProcessor);
|
---|
43 | KERNEL_STORAGE(processorCtx_t, mainProcessorCtx);
|
---|
44 | KERNEL_STORAGE(thread_desc, mainThread);
|
---|
45 | KERNEL_STORAGE(machine_context_t, mainThreadCtx);
|
---|
46 |
|
---|
47 | cluster * mainCluster;
|
---|
48 | processor * mainProcessor;
|
---|
49 | thread_desc * mainThread;
|
---|
50 |
|
---|
51 | //-----------------------------------------------------------------------------
|
---|
52 | // Global state
|
---|
53 |
|
---|
54 | thread_local coroutine_desc * volatile this_coroutine;
|
---|
55 | thread_local thread_desc * volatile this_thread;
|
---|
56 | thread_local processor * volatile this_processor;
|
---|
57 |
|
---|
58 | // volatile thread_local bool preemption_in_progress = 0;
|
---|
59 | // volatile thread_local bool preemption_enabled = false;
|
---|
60 | // volatile thread_local unsigned short disable_preempt_count = 1;
|
---|
61 |
|
---|
62 | volatile thread_local __cfa_kernel_preemption_state_t preemption_state = { false, false, 1 };
|
---|
63 |
|
---|
64 | //-----------------------------------------------------------------------------
|
---|
65 | // Main thread construction
|
---|
66 | struct current_stack_info_t {
|
---|
67 | machine_context_t ctx;
|
---|
68 | unsigned int size; // size of stack
|
---|
69 | void *base; // base of stack
|
---|
70 | void *storage; // pointer to stack
|
---|
71 | void *limit; // stack grows towards stack limit
|
---|
72 | void *context; // address of cfa_context_t
|
---|
73 | void *top; // address of top of storage
|
---|
74 | };
|
---|
75 |
|
---|
76 | void ?{}( current_stack_info_t & this ) {
|
---|
77 | CtxGet( this.ctx );
|
---|
78 | this.base = this.ctx.FP;
|
---|
79 | this.storage = this.ctx.SP;
|
---|
80 |
|
---|
81 | rlimit r;
|
---|
82 | getrlimit( RLIMIT_STACK, &r);
|
---|
83 | this.size = r.rlim_cur;
|
---|
84 |
|
---|
85 | this.limit = (void *)(((intptr_t)this.base) - this.size);
|
---|
86 | this.context = &storage_mainThreadCtx;
|
---|
87 | this.top = this.base;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
|
---|
91 | size = info->size;
|
---|
92 | storage = info->storage;
|
---|
93 | limit = info->limit;
|
---|
94 | base = info->base;
|
---|
95 | context = info->context;
|
---|
96 | top = info->top;
|
---|
97 | userStack = true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
|
---|
101 | stack{ info };
|
---|
102 | name = "Main Thread";
|
---|
103 | errno_ = 0;
|
---|
104 | state = Start;
|
---|
105 | starter = NULL;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
|
---|
109 | self_cor{ info };
|
---|
110 | curr_cor = &self_cor;
|
---|
111 | self_mon.owner = &this;
|
---|
112 | self_mon.recursion = 1;
|
---|
113 | self_mon_p = &self_mon;
|
---|
114 | next = NULL;
|
---|
115 | __cfaabi_dbg_debug_do(
|
---|
116 | dbg_next = NULL;
|
---|
117 | dbg_prev = NULL;
|
---|
118 | __cfaabi_dbg_thread_register(&this);
|
---|
119 | )
|
---|
120 |
|
---|
121 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
122 | }
|
---|
123 |
|
---|
124 | //-----------------------------------------------------------------------------
|
---|
125 | // Processor coroutine
|
---|
126 | void ?{}(processorCtx_t & this) {}
|
---|
127 |
|
---|
128 | // Construct the processor context of the main processor
|
---|
129 | void ?{}(processorCtx_t & this, processor * proc) {
|
---|
130 | (this.__cor){ "Processor" };
|
---|
131 | this.__cor.starter = NULL;
|
---|
132 | this.proc = proc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | // Construct the processor context of non-main processors
|
---|
136 | void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
137 | (this.__cor){ info };
|
---|
138 | this.proc = proc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | void ?{}(processor & this) {
|
---|
142 | this{ mainCluster };
|
---|
143 | }
|
---|
144 |
|
---|
145 | void ?{}(processor & this, cluster * cltr) with( this ) {
|
---|
146 | this.cltr = cltr;
|
---|
147 | terminated{ 0 };
|
---|
148 | do_terminate = false;
|
---|
149 | preemption_alarm = NULL;
|
---|
150 | pending_preemption = false;
|
---|
151 | runner.proc = &this;
|
---|
152 |
|
---|
153 | start( &this );
|
---|
154 | }
|
---|
155 |
|
---|
156 | void ?{}(processor & this, cluster * cltr, processorCtx_t & runner) with( this ) {
|
---|
157 | this.cltr = cltr;
|
---|
158 | terminated{ 0 };
|
---|
159 | do_terminate = false;
|
---|
160 | preemption_alarm = NULL;
|
---|
161 | pending_preemption = false;
|
---|
162 | kernel_thread = pthread_self();
|
---|
163 | runner.proc = &this;
|
---|
164 |
|
---|
165 | __cfaabi_dbg_print_safe("Kernel : constructing main processor context %p\n", &runner);
|
---|
166 | runner{ &this };
|
---|
167 | }
|
---|
168 |
|
---|
169 | void ^?{}(processor & this) with( this ){
|
---|
170 | if( ! do_terminate ) {
|
---|
171 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
|
---|
172 | terminate(&this);
|
---|
173 | verify(this.do_terminate);
|
---|
174 | verify(this_processor != &this);
|
---|
175 | P( terminated );
|
---|
176 | verify(this_processor != &this);
|
---|
177 | pthread_join( kernel_thread, NULL );
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void ?{}(cluster & this) with( this ) {
|
---|
182 | ready_queue{};
|
---|
183 | ready_queue_lock{};
|
---|
184 |
|
---|
185 | preemption_rate = default_preemption();
|
---|
186 | }
|
---|
187 |
|
---|
188 | void ^?{}(cluster & this) {
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | //=============================================================================================
|
---|
193 | // Kernel Scheduling logic
|
---|
194 | //=============================================================================================
|
---|
195 | //Main of the processor contexts
|
---|
196 | void main(processorCtx_t & runner) {
|
---|
197 | processor * this = runner.proc;
|
---|
198 | verify(this);
|
---|
199 |
|
---|
200 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
|
---|
201 |
|
---|
202 | {
|
---|
203 | // Setup preemption data
|
---|
204 | preemption_scope scope = { this };
|
---|
205 |
|
---|
206 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
207 |
|
---|
208 | thread_desc * readyThread = NULL;
|
---|
209 | for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
|
---|
210 | {
|
---|
211 | readyThread = nextThread( this->cltr );
|
---|
212 |
|
---|
213 | if(readyThread)
|
---|
214 | {
|
---|
215 | verify( !preemption_state.enabled );
|
---|
216 |
|
---|
217 | runThread(this, readyThread);
|
---|
218 |
|
---|
219 | verify( !preemption_state.enabled );
|
---|
220 |
|
---|
221 | //Some actions need to be taken from the kernel
|
---|
222 | finishRunning(this);
|
---|
223 |
|
---|
224 | spin_count = 0;
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | spin(this, &spin_count);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
|
---|
233 | }
|
---|
234 |
|
---|
235 | V( this->terminated );
|
---|
236 |
|
---|
237 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
|
---|
238 | }
|
---|
239 |
|
---|
240 | // runThread runs a thread by context switching
|
---|
241 | // from the processor coroutine to the target thread
|
---|
242 | void runThread(processor * this, thread_desc * dst) {
|
---|
243 | assert(dst->curr_cor);
|
---|
244 | coroutine_desc * proc_cor = get_coroutine(this->runner);
|
---|
245 | coroutine_desc * thrd_cor = dst->curr_cor;
|
---|
246 |
|
---|
247 | //Reset the terminating actions here
|
---|
248 | this->finish.action_code = No_Action;
|
---|
249 |
|
---|
250 | //Update global state
|
---|
251 | this_thread = dst;
|
---|
252 |
|
---|
253 | // Context Switch to the thread
|
---|
254 | ThreadCtxSwitch(proc_cor, thrd_cor);
|
---|
255 | // when ThreadCtxSwitch returns we are back in the processor coroutine
|
---|
256 | }
|
---|
257 |
|
---|
258 | void returnToKernel() {
|
---|
259 | coroutine_desc * proc_cor = get_coroutine(this_processor->runner);
|
---|
260 | coroutine_desc * thrd_cor = this_thread->curr_cor = this_coroutine;
|
---|
261 | ThreadCtxSwitch(thrd_cor, proc_cor);
|
---|
262 | }
|
---|
263 |
|
---|
264 | // Once a thread has finished running, some of
|
---|
265 | // its final actions must be executed from the kernel
|
---|
266 | void finishRunning(processor * this) with( this->finish ) {
|
---|
267 | if( action_code == Release ) {
|
---|
268 | verify( !preemption_state.enabled );
|
---|
269 | unlock( *lock );
|
---|
270 | }
|
---|
271 | else if( action_code == Schedule ) {
|
---|
272 | ScheduleThread( thrd );
|
---|
273 | }
|
---|
274 | else if( action_code == Release_Schedule ) {
|
---|
275 | verify( !preemption_state.enabled );
|
---|
276 | unlock( *lock );
|
---|
277 | ScheduleThread( thrd );
|
---|
278 | }
|
---|
279 | else if( action_code == Release_Multi ) {
|
---|
280 | verify( !preemption_state.enabled );
|
---|
281 | for(int i = 0; i < lock_count; i++) {
|
---|
282 | unlock( *locks[i] );
|
---|
283 | }
|
---|
284 | }
|
---|
285 | else if( action_code == Release_Multi_Schedule ) {
|
---|
286 | for(int i = 0; i < lock_count; i++) {
|
---|
287 | unlock( *locks[i] );
|
---|
288 | }
|
---|
289 | for(int i = 0; i < thrd_count; i++) {
|
---|
290 | ScheduleThread( thrds[i] );
|
---|
291 | }
|
---|
292 | }
|
---|
293 | else {
|
---|
294 | assert(action_code == No_Action);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | // Handles spinning logic
|
---|
299 | // TODO : find some strategy to put cores to sleep after some time
|
---|
300 | void spin(processor * this, unsigned int * spin_count) {
|
---|
301 | (*spin_count)++;
|
---|
302 | }
|
---|
303 |
|
---|
304 | // Context invoker for processors
|
---|
305 | // This is the entry point for processors (kernel threads)
|
---|
306 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
307 | void * CtxInvokeProcessor(void * arg) {
|
---|
308 | processor * proc = (processor *) arg;
|
---|
309 | this_processor = proc;
|
---|
310 | this_coroutine = NULL;
|
---|
311 | this_thread = NULL;
|
---|
312 | preemption_state.enabled = false;
|
---|
313 | preemption_state.disable_count = 1;
|
---|
314 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
315 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
316 | // to waste the perfectly valid stack create by pthread.
|
---|
317 | current_stack_info_t info;
|
---|
318 | machine_context_t ctx;
|
---|
319 | info.context = &ctx;
|
---|
320 | (proc->runner){ proc, &info };
|
---|
321 |
|
---|
322 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
|
---|
323 |
|
---|
324 | //Set global state
|
---|
325 | this_coroutine = get_coroutine(proc->runner);
|
---|
326 | this_thread = NULL;
|
---|
327 |
|
---|
328 | //We now have a proper context from which to schedule threads
|
---|
329 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
---|
330 |
|
---|
331 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
332 | // resume it to start it like it normally would, it will just context switch
|
---|
333 | // back to here. Instead directly call the main since we already are on the
|
---|
334 | // appropriate stack.
|
---|
335 | get_coroutine(proc->runner)->state = Active;
|
---|
336 | main( proc->runner );
|
---|
337 | get_coroutine(proc->runner)->state = Halted;
|
---|
338 |
|
---|
339 | // Main routine of the core returned, the core is now fully terminated
|
---|
340 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
---|
341 |
|
---|
342 | return NULL;
|
---|
343 | }
|
---|
344 |
|
---|
345 | void start(processor * this) {
|
---|
346 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
|
---|
347 |
|
---|
348 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
|
---|
349 |
|
---|
350 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
351 | }
|
---|
352 |
|
---|
353 | void kernel_first_resume(processor * this) {
|
---|
354 | coroutine_desc * src = this_coroutine;
|
---|
355 | coroutine_desc * dst = get_coroutine(this->runner);
|
---|
356 |
|
---|
357 | verify( !preemption_state.enabled );
|
---|
358 |
|
---|
359 | create_stack(&dst->stack, dst->stack.size);
|
---|
360 | CtxStart(&this->runner, CtxInvokeCoroutine);
|
---|
361 |
|
---|
362 | verify( !preemption_state.enabled );
|
---|
363 |
|
---|
364 | dst->last = src;
|
---|
365 | dst->starter = dst->starter ? dst->starter : src;
|
---|
366 |
|
---|
367 | // set state of current coroutine to inactive
|
---|
368 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
369 |
|
---|
370 | // set new coroutine that task is executing
|
---|
371 | this_coroutine = dst;
|
---|
372 |
|
---|
373 | // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
|
---|
374 | // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
|
---|
375 | // This is consistent with thread creation. However, when creating the main processor coroutine,
|
---|
376 | // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
|
---|
377 | // stay disabled.
|
---|
378 | disable_interrupts();
|
---|
379 |
|
---|
380 | // context switch to specified coroutine
|
---|
381 | assert( src->stack.context );
|
---|
382 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
383 | // when CtxSwitch returns we are back in the src coroutine
|
---|
384 |
|
---|
385 | // set state of new coroutine to active
|
---|
386 | src->state = Active;
|
---|
387 |
|
---|
388 | verify( !preemption_state.enabled );
|
---|
389 | }
|
---|
390 |
|
---|
391 | //-----------------------------------------------------------------------------
|
---|
392 | // Scheduler routines
|
---|
393 | void ScheduleThread( thread_desc * thrd ) {
|
---|
394 | // if( !thrd ) return;
|
---|
395 | verify( thrd );
|
---|
396 | verify( thrd->self_cor.state != Halted );
|
---|
397 |
|
---|
398 | verify( !preemption_state.enabled );
|
---|
399 |
|
---|
400 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
|
---|
401 |
|
---|
402 | with( *this_processor->cltr ) {
|
---|
403 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
404 | append( ready_queue, thrd );
|
---|
405 | unlock( ready_queue_lock );
|
---|
406 | }
|
---|
407 |
|
---|
408 | verify( !preemption_state.enabled );
|
---|
409 | }
|
---|
410 |
|
---|
411 | thread_desc * nextThread(cluster * this) with( *this ) {
|
---|
412 | verify( !preemption_state.enabled );
|
---|
413 | lock( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
414 | thread_desc * head = pop_head( ready_queue );
|
---|
415 | unlock( ready_queue_lock );
|
---|
416 | verify( !preemption_state.enabled );
|
---|
417 | return head;
|
---|
418 | }
|
---|
419 |
|
---|
420 | void BlockInternal() {
|
---|
421 | disable_interrupts();
|
---|
422 | verify( !preemption_state.enabled );
|
---|
423 | returnToKernel();
|
---|
424 | verify( !preemption_state.enabled );
|
---|
425 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
426 | }
|
---|
427 |
|
---|
428 | void BlockInternal( __spinlock_t * lock ) {
|
---|
429 | disable_interrupts();
|
---|
430 | this_processor->finish.action_code = Release;
|
---|
431 | this_processor->finish.lock = lock;
|
---|
432 |
|
---|
433 | verify( !preemption_state.enabled );
|
---|
434 | returnToKernel();
|
---|
435 | verify( !preemption_state.enabled );
|
---|
436 |
|
---|
437 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
438 | }
|
---|
439 |
|
---|
440 | void BlockInternal( thread_desc * thrd ) {
|
---|
441 | disable_interrupts();
|
---|
442 | this_processor->finish.action_code = Schedule;
|
---|
443 | this_processor->finish.thrd = thrd;
|
---|
444 |
|
---|
445 | verify( !preemption_state.enabled );
|
---|
446 | returnToKernel();
|
---|
447 | verify( !preemption_state.enabled );
|
---|
448 |
|
---|
449 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
450 | }
|
---|
451 |
|
---|
452 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
|
---|
453 | assert(thrd);
|
---|
454 | disable_interrupts();
|
---|
455 | this_processor->finish.action_code = Release_Schedule;
|
---|
456 | this_processor->finish.lock = lock;
|
---|
457 | this_processor->finish.thrd = thrd;
|
---|
458 |
|
---|
459 | verify( !preemption_state.enabled );
|
---|
460 | returnToKernel();
|
---|
461 | verify( !preemption_state.enabled );
|
---|
462 |
|
---|
463 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
464 | }
|
---|
465 |
|
---|
466 | void BlockInternal(__spinlock_t * locks [], unsigned short count) {
|
---|
467 | disable_interrupts();
|
---|
468 | this_processor->finish.action_code = Release_Multi;
|
---|
469 | this_processor->finish.locks = locks;
|
---|
470 | this_processor->finish.lock_count = count;
|
---|
471 |
|
---|
472 | verify( !preemption_state.enabled );
|
---|
473 | returnToKernel();
|
---|
474 | verify( !preemption_state.enabled );
|
---|
475 |
|
---|
476 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
477 | }
|
---|
478 |
|
---|
479 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
|
---|
480 | disable_interrupts();
|
---|
481 | this_processor->finish.action_code = Release_Multi_Schedule;
|
---|
482 | this_processor->finish.locks = locks;
|
---|
483 | this_processor->finish.lock_count = lock_count;
|
---|
484 | this_processor->finish.thrds = thrds;
|
---|
485 | this_processor->finish.thrd_count = thrd_count;
|
---|
486 |
|
---|
487 | verify( !preemption_state.enabled );
|
---|
488 | returnToKernel();
|
---|
489 | verify( !preemption_state.enabled );
|
---|
490 |
|
---|
491 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
492 | }
|
---|
493 |
|
---|
494 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
|
---|
495 | verify( !preemption_state.enabled );
|
---|
496 | this_processor->finish.action_code = thrd ? Release_Schedule : Release;
|
---|
497 | this_processor->finish.lock = lock;
|
---|
498 | this_processor->finish.thrd = thrd;
|
---|
499 |
|
---|
500 | returnToKernel();
|
---|
501 | }
|
---|
502 |
|
---|
503 | //=============================================================================================
|
---|
504 | // Kernel Setup logic
|
---|
505 | //=============================================================================================
|
---|
506 | //-----------------------------------------------------------------------------
|
---|
507 | // Kernel boot procedures
|
---|
508 | void kernel_startup(void) {
|
---|
509 | verify( !preemption_state.enabled );
|
---|
510 | __cfaabi_dbg_print_safe("Kernel : Starting\n");
|
---|
511 |
|
---|
512 | // Start by initializing the main thread
|
---|
513 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
514 | // which will then be scheduled by the mainProcessor normally
|
---|
515 | mainThread = (thread_desc *)&storage_mainThread;
|
---|
516 | current_stack_info_t info;
|
---|
517 | (*mainThread){ &info };
|
---|
518 |
|
---|
519 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
|
---|
520 |
|
---|
521 | // Initialize the main cluster
|
---|
522 | mainCluster = (cluster *)&storage_mainCluster;
|
---|
523 | (*mainCluster){};
|
---|
524 |
|
---|
525 | __cfaabi_dbg_print_safe("Kernel : main cluster ready\n");
|
---|
526 |
|
---|
527 | // Initialize the main processor and the main processor ctx
|
---|
528 | // (the coroutine that contains the processing control flow)
|
---|
529 | mainProcessor = (processor *)&storage_mainProcessor;
|
---|
530 | (*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx };
|
---|
531 |
|
---|
532 | //initialize the global state variables
|
---|
533 | this_processor = mainProcessor;
|
---|
534 | this_thread = mainThread;
|
---|
535 | this_coroutine = &mainThread->self_cor;
|
---|
536 |
|
---|
537 | // Enable preemption
|
---|
538 | kernel_start_preemption();
|
---|
539 |
|
---|
540 | // Add the main thread to the ready queue
|
---|
541 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
---|
542 | ScheduleThread(mainThread);
|
---|
543 |
|
---|
544 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
---|
545 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
|
---|
546 | // mainThread is on the ready queue when this call is made.
|
---|
547 | kernel_first_resume( this_processor );
|
---|
548 |
|
---|
549 |
|
---|
550 |
|
---|
551 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
552 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
|
---|
553 |
|
---|
554 | verify( !preemption_state.enabled );
|
---|
555 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
556 | verify( preemption_state.enabled );
|
---|
557 | }
|
---|
558 |
|
---|
559 | void kernel_shutdown(void) {
|
---|
560 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
561 |
|
---|
562 | verify( preemption_state.enabled );
|
---|
563 | disable_interrupts();
|
---|
564 | verify( !preemption_state.enabled );
|
---|
565 |
|
---|
566 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
---|
567 | // When its coroutine terminates, it return control to the mainThread
|
---|
568 | // which is currently here
|
---|
569 | mainProcessor->do_terminate = true;
|
---|
570 | returnToKernel();
|
---|
571 |
|
---|
572 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
573 |
|
---|
574 | // Disable preemption
|
---|
575 | kernel_stop_preemption();
|
---|
576 |
|
---|
577 | // Destroy the main processor and its context in reverse order of construction
|
---|
578 | // These were manually constructed so we need manually destroy them
|
---|
579 | ^(mainProcessor->runner){};
|
---|
580 | ^(mainProcessor){};
|
---|
581 |
|
---|
582 | // Final step, destroy the main thread since it is no longer needed
|
---|
583 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
584 | ^(mainThread){};
|
---|
585 |
|
---|
586 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
|
---|
587 | }
|
---|
588 |
|
---|
589 | //=============================================================================================
|
---|
590 | // Unexpected Terminating logic
|
---|
591 | //=============================================================================================
|
---|
592 |
|
---|
593 |
|
---|
594 | static __spinlock_t kernel_abort_lock;
|
---|
595 | static __spinlock_t kernel_debug_lock;
|
---|
596 | static bool kernel_abort_called = false;
|
---|
597 |
|
---|
598 | void * kernel_abort (void) __attribute__ ((__nothrow__)) {
|
---|
599 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
---|
600 | // the globalAbort flag is true.
|
---|
601 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
---|
602 |
|
---|
603 | // first task to abort ?
|
---|
604 | if ( !kernel_abort_called ) { // not first task to abort ?
|
---|
605 | kernel_abort_called = true;
|
---|
606 | unlock( kernel_abort_lock );
|
---|
607 | }
|
---|
608 | else {
|
---|
609 | unlock( kernel_abort_lock );
|
---|
610 |
|
---|
611 | sigset_t mask;
|
---|
612 | sigemptyset( &mask );
|
---|
613 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
---|
614 | sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals
|
---|
615 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
---|
616 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
---|
617 | }
|
---|
618 |
|
---|
619 | return this_thread;
|
---|
620 | }
|
---|
621 |
|
---|
622 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
---|
623 | thread_desc * thrd = kernel_data;
|
---|
624 |
|
---|
625 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
626 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
627 |
|
---|
628 | if ( thrd != this_coroutine ) {
|
---|
629 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
|
---|
630 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
631 | }
|
---|
632 | else {
|
---|
633 | __cfaabi_dbg_bits_write( ".\n", 2 );
|
---|
634 | }
|
---|
635 | }
|
---|
636 |
|
---|
637 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
638 | return get_coroutine(this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
---|
639 | }
|
---|
640 |
|
---|
641 | extern "C" {
|
---|
642 | void __cfaabi_dbg_bits_acquire() {
|
---|
643 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
644 | }
|
---|
645 |
|
---|
646 | void __cfaabi_dbg_bits_release() {
|
---|
647 | unlock( kernel_debug_lock );
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | //=============================================================================================
|
---|
652 | // Kernel Utilities
|
---|
653 | //=============================================================================================
|
---|
654 | //-----------------------------------------------------------------------------
|
---|
655 | // Locks
|
---|
656 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
657 | (this.lock){};
|
---|
658 | this.count = count;
|
---|
659 | (this.waiting){};
|
---|
660 | }
|
---|
661 | void ^?{}(semaphore & this) {}
|
---|
662 |
|
---|
663 | void P(semaphore & this) with( this ){
|
---|
664 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
665 | count -= 1;
|
---|
666 | if ( count < 0 ) {
|
---|
667 | // queue current task
|
---|
668 | append( waiting, (thread_desc *)this_thread );
|
---|
669 |
|
---|
670 | // atomically release spin lock and block
|
---|
671 | BlockInternal( &lock );
|
---|
672 | }
|
---|
673 | else {
|
---|
674 | unlock( lock );
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | void V(semaphore & this) with( this ) {
|
---|
679 | thread_desc * thrd = NULL;
|
---|
680 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
681 | count += 1;
|
---|
682 | if ( count <= 0 ) {
|
---|
683 | // remove task at head of waiting list
|
---|
684 | thrd = pop_head( waiting );
|
---|
685 | }
|
---|
686 |
|
---|
687 | unlock( lock );
|
---|
688 |
|
---|
689 | // make new owner
|
---|
690 | WakeThread( thrd );
|
---|
691 | }
|
---|
692 |
|
---|
693 | //-----------------------------------------------------------------------------
|
---|
694 | // Debug
|
---|
695 | __cfaabi_dbg_debug_do(
|
---|
696 | struct {
|
---|
697 | thread_desc * tail;
|
---|
698 | } __cfaabi_dbg_thread_list = { NULL };
|
---|
699 |
|
---|
700 | void __cfaabi_dbg_thread_register( thread_desc * thrd ) {
|
---|
701 | if( !__cfaabi_dbg_thread_list.tail ) {
|
---|
702 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
703 | return;
|
---|
704 | }
|
---|
705 | __cfaabi_dbg_thread_list.tail->dbg_next = thrd;
|
---|
706 | thrd->dbg_prev = __cfaabi_dbg_thread_list.tail;
|
---|
707 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
708 | }
|
---|
709 |
|
---|
710 | void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) {
|
---|
711 | thread_desc * prev = thrd->dbg_prev;
|
---|
712 | thread_desc * next = thrd->dbg_next;
|
---|
713 |
|
---|
714 | if( next ) { next->dbg_prev = prev; }
|
---|
715 | else {
|
---|
716 | assert( __cfaabi_dbg_thread_list.tail == thrd );
|
---|
717 | __cfaabi_dbg_thread_list.tail = prev;
|
---|
718 | }
|
---|
719 |
|
---|
720 | if( prev ) { prev->dbg_next = next; }
|
---|
721 |
|
---|
722 | thrd->dbg_prev = NULL;
|
---|
723 | thrd->dbg_next = NULL;
|
---|
724 | }
|
---|
725 | )
|
---|
726 | // Local Variables: //
|
---|
727 | // mode: c //
|
---|
728 | // tab-width: 4 //
|
---|
729 | // End: //
|
---|