[8118303] | 1 | // -*- Mode: CFA -*- |
---|
| 2 | // |
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 4 | // |
---|
| 5 | // The contents of this file are covered under the licence agreement in the |
---|
| 6 | // file "LICENCE" distributed with Cforall. |
---|
| 7 | // |
---|
| 8 | // kernel.c -- |
---|
| 9 | // |
---|
| 10 | // Author : Thierry Delisle |
---|
| 11 | // Created On : Tue Jan 17 12:27:26 2016 |
---|
| 12 | // Last Modified By : Thierry Delisle |
---|
| 13 | // Last Modified On : -- |
---|
| 14 | // Update Count : 0 |
---|
| 15 | // |
---|
| 16 | |
---|
| 17 | //Header |
---|
| 18 | #include "kernel" |
---|
| 19 | |
---|
| 20 | //C Includes |
---|
[c84e80a] | 21 | #include <stddef.h> |
---|
[eb2e723] | 22 | extern "C" { |
---|
| 23 | #include <sys/resource.h> |
---|
| 24 | } |
---|
[8118303] | 25 | |
---|
| 26 | //CFA Includes |
---|
| 27 | #include "libhdr.h" |
---|
| 28 | #include "threads" |
---|
| 29 | |
---|
| 30 | //Private includes |
---|
| 31 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 32 | #include "invoke.h" |
---|
| 33 | |
---|
[eb2e723] | 34 | processor * systemProcessor; |
---|
| 35 | thread_h * mainThread; |
---|
| 36 | |
---|
| 37 | void kernel_startup(void) __attribute__((constructor(101))); |
---|
| 38 | void kernel_shutdown(void) __attribute__((destructor(101))); |
---|
[8118303] | 39 | |
---|
[c84e80a] | 40 | void ?{}(processor * this) { |
---|
[eb2e723] | 41 | this->ctx = NULL; |
---|
[c84e80a] | 42 | this->thread_index = 0; |
---|
| 43 | this->thread_count = 10; |
---|
| 44 | this->terminated = false; |
---|
| 45 | |
---|
| 46 | for(int i = 0; i < 10; i++) { |
---|
| 47 | this->threads[i] = NULL; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void ^?{}(processor * this) { |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | //----------------------------------------------------------------------------- |
---|
| 58 | // Processor coroutine |
---|
[eb2e723] | 59 | struct processorCtx_t { |
---|
[c84e80a] | 60 | processor * proc; |
---|
| 61 | coroutine c; |
---|
| 62 | }; |
---|
| 63 | |
---|
[eb2e723] | 64 | DECL_COROUTINE(processorCtx_t) |
---|
[c84e80a] | 65 | |
---|
[eb2e723] | 66 | void ?{}(processorCtx_t * this, processor * proc) { |
---|
| 67 | (&this->c){}; |
---|
[c84e80a] | 68 | this->proc = proc; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void CtxInvokeProcessor(processor * proc) { |
---|
[eb2e723] | 72 | processorCtx_t proc_cor_storage = {proc}; |
---|
[c84e80a] | 73 | resume( &proc_cor_storage ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | //----------------------------------------------------------------------------- |
---|
| 77 | // Processor running routines |
---|
[eb2e723] | 78 | void main(processorCtx_t * ctx); |
---|
[c84e80a] | 79 | thread_h * nextThread(processor * this); |
---|
| 80 | void runThread(processor * this, thread_h * dst); |
---|
| 81 | void spin(processor * this, unsigned int * spin_count); |
---|
[8118303] | 82 | |
---|
[eb2e723] | 83 | void main(processorCtx_t * ctx) { |
---|
| 84 | processor * this = ctx->proc; |
---|
| 85 | LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this); |
---|
[8118303] | 86 | |
---|
[c84e80a] | 87 | thread_h * readyThread = NULL; |
---|
| 88 | for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) { |
---|
| 89 | |
---|
| 90 | readyThread = nextThread(this); |
---|
[8118303] | 91 | |
---|
[c84e80a] | 92 | if(readyThread) { |
---|
| 93 | runThread(this, readyThread); |
---|
| 94 | spin_count = 0; |
---|
| 95 | } else { |
---|
| 96 | spin(this, &spin_count); |
---|
| 97 | } |
---|
| 98 | } |
---|
[8118303] | 99 | |
---|
[c84e80a] | 100 | LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | thread_h * nextThread(processor * this) { |
---|
| 104 | for(int i = 0; i < this->thread_count; i++) { |
---|
| 105 | this->thread_index = (this->thread_index + 1) % this->thread_count; |
---|
| 106 | |
---|
| 107 | thread_h * thrd = this->threads[this->thread_index]; |
---|
| 108 | if(thrd) return thrd; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return NULL; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void runThread(processor * this, thread_h * dst) { |
---|
[eb2e723] | 115 | coroutine * proc_ctx = get_coroutine(this->ctx); |
---|
[c84e80a] | 116 | coroutine * thrd_ctx = get_coroutine(dst); |
---|
| 117 | thrd_ctx->last = proc_ctx; |
---|
| 118 | |
---|
| 119 | // context switch to specified coroutine |
---|
| 120 | // Which is now the current_coroutine |
---|
[8f49a54] | 121 | // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); |
---|
[c84e80a] | 122 | current_coroutine = thrd_ctx; |
---|
| 123 | CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context ); |
---|
| 124 | current_coroutine = proc_ctx; |
---|
[8f49a54] | 125 | // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); |
---|
[c84e80a] | 126 | |
---|
| 127 | // when CtxSwitch returns we are back in the processor coroutine |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void spin(processor * this, unsigned int * spin_count) { |
---|
| 131 | (*spin_count)++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | //----------------------------------------------------------------------------- |
---|
| 135 | // Kernel runner (Temporary) |
---|
| 136 | |
---|
[eb2e723] | 137 | void scheduler_add( thread_h * thrd ) { |
---|
[c84e80a] | 138 | for(int i = 0; i < systemProcessor->thread_count; i++) { |
---|
| 139 | if(systemProcessor->threads[i] == NULL) { |
---|
| 140 | systemProcessor->threads[i] = thrd; |
---|
| 141 | return; |
---|
| 142 | } |
---|
| 143 | } |
---|
[8f49a54] | 144 | assertf(false, "Scheduler full"); |
---|
[c84e80a] | 145 | } |
---|
| 146 | |
---|
[eb2e723] | 147 | void scheduler_remove( thread_h * thrd ) { |
---|
[c84e80a] | 148 | for(int i = 0; i < systemProcessor->thread_count; i++) { |
---|
| 149 | if(systemProcessor->threads[i] == thrd) { |
---|
| 150 | systemProcessor->threads[i] = NULL; |
---|
| 151 | return; |
---|
| 152 | } |
---|
| 153 | } |
---|
[8f49a54] | 154 | assertf(false, "Trying to unschedule unkown thread"); |
---|
[c84e80a] | 155 | } |
---|
| 156 | |
---|
[eb2e723] | 157 | //----------------------------------------------------------------------------- |
---|
| 158 | // Kernel storage |
---|
| 159 | #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)] |
---|
| 160 | |
---|
| 161 | KERNEL_STORAGE(processorCtx_t, systemProcessorCtx); |
---|
| 162 | KERNEL_STORAGE(processor, systemProcessor); |
---|
| 163 | KERNEL_STORAGE(thread_h, mainThread); |
---|
| 164 | KERNEL_STORAGE(machine_context_t, mainThread_context); |
---|
| 165 | |
---|
| 166 | //----------------------------------------------------------------------------- |
---|
| 167 | // Main thread construction |
---|
| 168 | struct mainThread_info_t { |
---|
| 169 | machine_context_t ctx; |
---|
| 170 | unsigned int size; // size of stack |
---|
| 171 | void *base; // base of stack |
---|
| 172 | void *storage; // pointer to stack |
---|
| 173 | void *limit; // stack grows towards stack limit |
---|
| 174 | void *context; // address of cfa_context_t |
---|
| 175 | void *top; // address of top of storage |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | void ?{}( mainThread_info_t * this ) { |
---|
| 179 | CtxGet( &this->ctx ); |
---|
| 180 | this->base = this->ctx.FP; |
---|
| 181 | this->storage = this->ctx.SP; |
---|
| 182 | |
---|
| 183 | rlimit r; |
---|
| 184 | int ret = getrlimit( RLIMIT_STACK, &r); |
---|
| 185 | this->size = r.rlim_cur; |
---|
| 186 | |
---|
| 187 | this->limit = (void *)(((intptr_t)this->base) - this->size); |
---|
| 188 | this->context = &mainThread_context_storage; |
---|
| 189 | this->top = this->base; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void ?{}( coStack_t * this, mainThread_info_t * info) { |
---|
| 193 | this->size = info->size; |
---|
| 194 | this->storage = info->storage; |
---|
| 195 | this->limit = info->limit; |
---|
| 196 | this->base = info->base; |
---|
| 197 | this->context = info->context; |
---|
| 198 | this->top = info->top; |
---|
| 199 | this->userStack = true; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void ?{}( coroutine * this, mainThread_info_t * info) { |
---|
| 203 | (&this->stack){ info }; |
---|
| 204 | this->name = "Main Thread"; |
---|
| 205 | this->errno_ = 0; |
---|
| 206 | this->state = Inactive; |
---|
| 207 | this->notHalted = true; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void ?{}( thread_h * this, mainThread_info_t * info) { |
---|
| 211 | (&this->c){ info }; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | //----------------------------------------------------------------------------- |
---|
| 215 | // Kernel boot procedures |
---|
| 216 | void kernel_startup(void) { |
---|
| 217 | |
---|
| 218 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
| 219 | // which will then be scheduled by the systemProcessor normally |
---|
| 220 | LIB_DEBUG_PRINTF("Kernel : Starting\n"); |
---|
| 221 | |
---|
| 222 | mainThread_info_t ctx; |
---|
[8f49a54] | 223 | // LIB_DEBUG_PRINTF("Kernel : base : %p\n", ctx.base ); |
---|
| 224 | // LIB_DEBUG_PRINTF("Kernel : top : %p\n", ctx.top ); |
---|
| 225 | // LIB_DEBUG_PRINTF("Kernel : limit : %p\n", ctx.limit ); |
---|
| 226 | // LIB_DEBUG_PRINTF("Kernel : size : %x\n", ctx.size ); |
---|
| 227 | // LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage ); |
---|
| 228 | // LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context ); |
---|
[eb2e723] | 229 | |
---|
| 230 | // Start by initializing the main thread |
---|
| 231 | mainThread = (thread_h *)&mainThread_storage; |
---|
| 232 | LIB_DEBUG_PRINTF("Kernel : Main thread : %p\n", mainThread ); |
---|
| 233 | mainThread{ &ctx }; |
---|
| 234 | |
---|
| 235 | // // Initialize the system processor |
---|
| 236 | systemProcessor = (processor *)&systemProcessor_storage; |
---|
| 237 | systemProcessor{}; |
---|
| 238 | |
---|
| 239 | // Initialize the system processor ctx |
---|
| 240 | // (the coroutine that contains the processing control flow) |
---|
| 241 | systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage; |
---|
| 242 | systemProcessor->ctx{ systemProcessor }; |
---|
| 243 | |
---|
| 244 | scheduler_add(mainThread); |
---|
| 245 | |
---|
| 246 | current_coroutine = &mainThread->c; |
---|
| 247 | |
---|
| 248 | LIB_DEBUG_PRINTF("Kernel : Starting system processor\n"); |
---|
| 249 | resume(systemProcessor->ctx); |
---|
| 250 | |
---|
| 251 | LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n"); |
---|
| 252 | } |
---|
| 253 | void kernel_shutdown(void) { |
---|
| 254 | LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down"); |
---|
| 255 | |
---|
| 256 | LIB_DEBUG_PRINTF("Unscheduling main thread\n"); |
---|
| 257 | scheduler_remove(mainThread); |
---|
| 258 | |
---|
[8f49a54] | 259 | LIB_DEBUG_PRINTF("Kernel : Terminating system processor\n"); |
---|
| 260 | systemProcessor->terminated = true; |
---|
| 261 | |
---|
[eb2e723] | 262 | suspend(); |
---|
| 263 | |
---|
[8f49a54] | 264 | LIB_DEBUG_PRINTF("Kernel : Control returned to initial process thread\n"); |
---|
[eb2e723] | 265 | |
---|
| 266 | ^(systemProcessor->ctx){}; |
---|
| 267 | ^(systemProcessor){}; |
---|
| 268 | |
---|
| 269 | ^(mainThread){}; |
---|
| 270 | |
---|
| 271 | LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n"); |
---|
[8118303] | 272 | } |
---|
| 273 | |
---|
| 274 | // Local Variables: // |
---|
| 275 | // mode: c // |
---|
| 276 | // tab-width: 4 // |
---|
| 277 | // End: // |
---|