blob: 77e6a5d12e8804f22906ef7b8338ac9ab5ce131f [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
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 Geiselbrecht68372232009-01-24 21:21:08 -080026#include <app.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070027#include <arch.h>
28#include <platform.h>
29#include <target.h>
30#include <project.h>
31#include <lib/heap.h>
32#include <kernel/thread.h>
33#include <kernel/timer.h>
34#include <kernel/dpc.h>
35
36extern void *__ctor_list;
37extern void *__ctor_end;
38extern int __bss_start;
39extern int _end;
40
41static int bootstrap2(void *arg);
42
43static void call_constructors(void)
44{
45 void **ctor;
46
47 ctor = &__ctor_list;
48 while(ctor != &__ctor_end) {
49 void (*func)(void);
50
51 func = (void (*)())*ctor;
52
53 func();
54 ctor++;
55 }
56}
57
58/* called from crt0.S */
59void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE;
60void kmain(void)
61{
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000062 // get us into some sort of thread context
63 thread_init_early();
64
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070065 // early arch stuff
66 arch_early_init();
67
68 // do any super early platform initialization
69 platform_early_init();
70
71 // do any super early target initialization
72 target_early_init();
73
Travis Geiselbrechteb946052008-09-07 22:32:49 -070074 dprintf(INFO, "welcome to lk\n\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070075
76 // deal with any static constructors
Travis Geiselbrechteb946052008-09-07 22:32:49 -070077 dprintf(SPEW, "calling constructors\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070078 call_constructors();
79
80 // bring up the kernel heap
Travis Geiselbrechteb946052008-09-07 22:32:49 -070081 dprintf(SPEW, "initializing heap\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070082 heap_init();
83
84 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070085 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070086 thread_init();
87
88 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070089 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070090 dpc_init();
91
92 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -070093 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070094 timer_init();
95
96 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -070097 dprintf(SPEW, "creating bootstrap completion thread\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070098 thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE));
99
100 // enable interrupts
101 exit_critical_section();
102
103 // become the idle thread
104 thread_become_idle();
105}
106
107int main(void);
108
109static int bootstrap2(void *arg)
110{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700111 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700112
113 arch_init();
114
115 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700116 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700117 platform_init();
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700118
119 // initialize the target
120 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700121 target_init();
122
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800123 dprintf(SPEW, "calling apps_init()\n");
124 apps_init();
125
Travis Geiselbrechtb57cdae2008-10-10 03:09:00 -0700126 dprintf(SPEW, "calling project_init()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700127 project_init();
128
129 return 0;
130}
131