blob: 16a4099c15946affd2933ae2edfac52a73bbba2b [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>
26#include <arch.h>
27#include <platform.h>
28#include <target.h>
29#include <project.h>
30#include <lib/heap.h>
31#include <kernel/thread.h>
32#include <kernel/timer.h>
33#include <kernel/dpc.h>
34
35extern void *__ctor_list;
36extern void *__ctor_end;
37extern int __bss_start;
38extern int _end;
39
40static int bootstrap2(void *arg);
41
42static 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 */
58void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE;
59void kmain(void)
60{
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000061 // get us into some sort of thread context
62 thread_init_early();
63
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070064 // 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 Geiselbrechteb946052008-09-07 22:32:49 -070073 dprintf(INFO, "welcome to lk\n\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070074
75 // deal with any static constructors
Travis Geiselbrechteb946052008-09-07 22:32:49 -070076 dprintf(SPEW, "calling constructors\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070077 call_constructors();
78
79 // bring up the kernel heap
Travis Geiselbrechteb946052008-09-07 22:32:49 -070080 dprintf(SPEW, "initializing heap\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070081 heap_init();
82
83 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070084 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070085 thread_init();
86
87 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070088 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070089 dpc_init();
90
91 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -070092 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070093 timer_init();
94
95 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -070096 dprintf(SPEW, "creating bootstrap completion thread\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070097 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
106int main(void);
107
108static int bootstrap2(void *arg)
109{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700110 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700111
112 arch_init();
113
114 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700115 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700116 platform_init();
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700117
118 // initialize the target
119 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700120 target_init();
121
Travis Geiselbrechtb57cdae2008-10-10 03:09:00 -0700122 dprintf(SPEW, "calling project_init()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700123 project_init();
124
125 return 0;
126}
127