Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <compiler.h> |
| 24 | #include <debug.h> |
| 25 | #include <string.h> |
Travis Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 26 | #include <app.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 27 | #include <arch.h> |
| 28 | #include <platform.h> |
| 29 | #include <target.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 30 | #include <lib/heap.h> |
| 31 | #include <kernel/thread.h> |
| 32 | #include <kernel/timer.h> |
| 33 | #include <kernel/dpc.h> |
| 34 | |
| 35 | extern void *__ctor_list; |
| 36 | extern void *__ctor_end; |
| 37 | extern int __bss_start; |
| 38 | extern int _end; |
| 39 | |
| 40 | static int bootstrap2(void *arg); |
| 41 | |
| 42 | static void call_constructors(void) |
| 43 | { |
| 44 | void **ctor; |
| 45 | |
| 46 | ctor = &__ctor_list; |
| 47 | while(ctor != &__ctor_end) { |
| 48 | void (*func)(void); |
| 49 | |
| 50 | func = (void (*)())*ctor; |
| 51 | |
| 52 | func(); |
| 53 | ctor++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* called from crt0.S */ |
| 58 | void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE; |
| 59 | void kmain(void) |
| 60 | { |
travis geiselbrecht | 858b1ad | 2008-12-23 21:55:41 +0000 | [diff] [blame] | 61 | // get us into some sort of thread context |
| 62 | thread_init_early(); |
| 63 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 64 | // early arch stuff |
| 65 | arch_early_init(); |
| 66 | |
| 67 | // do any super early platform initialization |
| 68 | platform_early_init(); |
| 69 | |
| 70 | // do any super early target initialization |
| 71 | target_early_init(); |
| 72 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 73 | dprintf(INFO, "welcome to lk\n\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 74 | |
| 75 | // deal with any static constructors |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 76 | dprintf(SPEW, "calling constructors\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 77 | call_constructors(); |
| 78 | |
| 79 | // bring up the kernel heap |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 80 | dprintf(SPEW, "initializing heap\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 81 | heap_init(); |
| 82 | |
| 83 | // initialize the threading system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 84 | dprintf(SPEW, "initializing threads\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 85 | thread_init(); |
| 86 | |
| 87 | // initialize the dpc system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 88 | dprintf(SPEW, "initializing dpc\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 89 | dpc_init(); |
| 90 | |
| 91 | // initialize kernel timers |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 92 | dprintf(SPEW, "initializing timers\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 93 | timer_init(); |
| 94 | |
| 95 | // create a thread to complete system initialization |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 96 | dprintf(SPEW, "creating bootstrap completion thread\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 97 | thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); |
| 98 | |
| 99 | // enable interrupts |
| 100 | exit_critical_section(); |
| 101 | |
| 102 | // become the idle thread |
| 103 | thread_become_idle(); |
| 104 | } |
| 105 | |
| 106 | int main(void); |
| 107 | |
| 108 | static int bootstrap2(void *arg) |
| 109 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 110 | dprintf(SPEW, "top of bootstrap2()\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 111 | |
| 112 | arch_init(); |
| 113 | |
| 114 | // initialize the rest of the platform |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 115 | dprintf(SPEW, "initializing platform\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 116 | platform_init(); |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 117 | |
| 118 | // initialize the target |
| 119 | dprintf(SPEW, "initializing target\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 120 | target_init(); |
| 121 | |
Travis Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 122 | dprintf(SPEW, "calling apps_init()\n"); |
| 123 | apps_init(); |
| 124 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |