Index: src/libcfa/concurrency/kernel.c
===================================================================
--- src/libcfa/concurrency/kernel.c	(revision c84e80aa83b9917829e6f5f923487d8ece403218)
+++ src/libcfa/concurrency/kernel.c	(revision 4a3386b40a240bd9769a705886c70d008e23008c)
@@ -20,4 +20,7 @@
 //C Includes
 #include <stddef.h>
+extern "C" {
+#include <sys/resource.h>
+}
 
 //CFA Includes
@@ -29,9 +32,12 @@
 #include "invoke.h"
 
-processor systemProcessorStorage = {};
-processor * systemProcessor = &systemProcessorStorage;
+processor * systemProcessor;
+thread_h * mainThread;
+
+void kernel_startup(void)  __attribute__((constructor(101)));
+void kernel_shutdown(void) __attribute__((destructor(101)));
 
 void ?{}(processor * this) {
-	this->cor = NULL;
+	this->ctx = NULL;
 	this->thread_index = 0;
 	this->thread_count = 10;
@@ -51,27 +57,18 @@
 //-----------------------------------------------------------------------------
 // Processor coroutine
-struct proc_coroutine {
+struct processorCtx_t {
 	processor * proc;
 	coroutine c;
 };
 
-void ?{}(coroutine * this, processor * proc) {
-	this{};
-}
-
-DECL_COROUTINE(proc_coroutine)
-
-void ?{}(proc_coroutine * this, processor * proc) {
-	(&this->c){proc};
+DECL_COROUTINE(processorCtx_t)
+
+void ?{}(processorCtx_t * this, processor * proc) {
+	(&this->c){};
 	this->proc = proc;
-	proc->cor = this;
-}
-
-void ^?{}(proc_coroutine * this) {
-	^(&this->c){};
 }
 
 void CtxInvokeProcessor(processor * proc) {
-	proc_coroutine proc_cor_storage = {proc};
+	processorCtx_t proc_cor_storage = {proc};
 	resume( &proc_cor_storage );
 }
@@ -79,12 +76,12 @@
 //-----------------------------------------------------------------------------
 // Processor running routines
-void main(proc_coroutine * cor);
+void main(processorCtx_t * ctx);
 thread_h * nextThread(processor * this);
 void runThread(processor * this, thread_h * dst);
 void spin(processor * this, unsigned int * spin_count);
 
-void main(proc_coroutine * cor) {
-	processor * this;
-	this = cor->proc;
+void main(processorCtx_t * ctx) {
+	processor * this = ctx->proc;
+	LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
 
 	thread_h * readyThread = NULL;
@@ -116,5 +113,5 @@
 
 void runThread(processor * this, thread_h * dst) {
-	coroutine * proc_ctx = get_coroutine(this->cor);
+	coroutine * proc_ctx = get_coroutine(this->ctx);
 	coroutine * thrd_ctx = get_coroutine(dst);
 	thrd_ctx->last = proc_ctx;
@@ -138,5 +135,5 @@
 // Kernel runner (Temporary)
 
-void scheduler_add( struct thread_h * thrd ) {
+void scheduler_add( thread_h * thrd ) {
 	LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count);
 	for(int i = 0; i < systemProcessor->thread_count; i++) {
@@ -149,5 +146,5 @@
 }
 
-void scheduler_remove( struct thread_h * thrd ) {
+void scheduler_remove( thread_h * thrd ) {
 	LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor);
 	for(int i = 0; i < systemProcessor->thread_count; i++) {
@@ -162,10 +159,121 @@
 		}
 	}
-	LIB_DEBUG_PRINTF("Kernel : terminating core %p\n\n\n", systemProcessor);	
+	LIB_DEBUG_PRINTF("Kernel : terminating core %p\n", systemProcessor);	
 	systemProcessor->terminated = true;
 }
 
-void kernel_run( void ) {
-	CtxInvokeProcessor(systemProcessor);
+//-----------------------------------------------------------------------------
+// Kernel storage
+#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
+
+KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
+KERNEL_STORAGE(processor, systemProcessor);
+KERNEL_STORAGE(thread_h, mainThread);
+KERNEL_STORAGE(machine_context_t, mainThread_context);
+
+//-----------------------------------------------------------------------------
+// Main thread construction
+struct mainThread_info_t {
+	machine_context_t ctx;	
+	unsigned int size;		// size of stack
+	void *base;				// base of stack
+	void *storage;			// pointer to stack
+	void *limit;			// stack grows towards stack limit
+	void *context;			// address of cfa_context_t
+	void *top;				// address of top of storage
+};
+
+void ?{}( mainThread_info_t * this ) {
+	CtxGet( &this->ctx );
+	this->base = this->ctx.FP;
+	this->storage = this->ctx.SP;
+
+	rlimit r;
+	int ret = getrlimit( RLIMIT_STACK, &r);
+	this->size = r.rlim_cur;
+
+	this->limit = (void *)(((intptr_t)this->base) - this->size);
+	this->context = &mainThread_context_storage;
+	this->top = this->base;
+}
+
+void ?{}( coStack_t * this, mainThread_info_t * info) {
+	this->size = info->size;
+	this->storage = info->storage;
+	this->limit = info->limit;
+	this->base = info->base;
+	this->context = info->context;
+	this->top = info->top;
+	this->userStack = true;
+}
+
+void ?{}( coroutine * this, mainThread_info_t * info) {
+	(&this->stack){ info };	
+	this->name = "Main Thread";
+	this->errno_ = 0;
+	this->state = Inactive;
+	this->notHalted = true;
+}
+
+void ?{}( thread_h * this, mainThread_info_t * info) {
+	(&this->c){ info };
+}
+
+//-----------------------------------------------------------------------------
+// Kernel boot procedures
+void kernel_startup(void) {
+
+	// SKULLDUGGERY: the mainThread steals the process main thread 
+	// which will then be scheduled by the systemProcessor normally
+	LIB_DEBUG_PRINTF("Kernel : Starting\n");	
+
+	mainThread_info_t ctx;
+	LIB_DEBUG_PRINTF("Kernel :    base : %p\n", ctx.base );
+	LIB_DEBUG_PRINTF("Kernel :     top : %p\n", ctx.top );
+	LIB_DEBUG_PRINTF("Kernel :   limit : %p\n", ctx.limit );
+	LIB_DEBUG_PRINTF("Kernel :    size : %x\n", ctx.size );
+	LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage );
+	LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context );
+
+	// Start by initializing the main thread
+	mainThread = (thread_h *)&mainThread_storage;
+	LIB_DEBUG_PRINTF("Kernel : Main thread : %p\n", mainThread );
+	mainThread{ &ctx };
+
+	// // Initialize the system processor
+	systemProcessor = (processor *)&systemProcessor_storage;
+	systemProcessor{};
+
+	// Initialize the system processor ctx
+	// (the coroutine that contains the processing control flow)
+	systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage;
+	systemProcessor->ctx{ systemProcessor };
+
+	scheduler_add(mainThread);
+
+	current_coroutine = &mainThread->c;
+
+	LIB_DEBUG_PRINTF("Kernel : Starting system processor\n");	
+	resume(systemProcessor->ctx);
+
+	LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
+}
+void kernel_shutdown(void) {
+	LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down");
+
+	LIB_DEBUG_PRINTF("Unscheduling main thread\n");
+	scheduler_remove(mainThread);
+
+	LIB_DEBUG_PRINTF("Suspending main\n");
+	suspend();
+
+	LIB_DEBUG_PRINTF("Kernel : Control return to initial process thread\n");
+
+	^(systemProcessor->ctx){};
+	^(systemProcessor){};
+
+	^(mainThread){};
+
+	LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");	
 }
 
