forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
227 lines (178 loc) · 5.44 KB
/
kernel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/******************************************************************************
* kernel.c
*
* Assorted crap goes here, including the initial C entry point, jumped at
* from head.S.
*
* Copyright (c) 2002-2003, K A Fraser & R Neugebauer
* Copyright (c) 2005, Grzegorz Milos, Intel Research Cambridge
* Copyright (c) 2006, Robert Kaiser, FH Wiesbaden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <mini-os/os.h>
#include <mini-os/hypervisor.h>
#include <mini-os/mm.h>
#include <mini-os/events.h>
#include <mini-os/time.h>
#include <mini-os/types.h>
#include <mini-os/lib.h>
#include <mini-os/sched.h>
#include <mini-os/xenbus.h>
#include <mini-os/gnttab.h>
#include <mini-os/netfront.h>
#include <mini-os/blkfront.h>
#include <mini-os/fbfront.h>
#include <mini-os/pcifront.h>
#include <mini-os/xmalloc.h>
#ifdef CONFIG_XENBUS
#include <mini-os/shutdown.h>
#endif
#include <fcntl.h>
#include <xen/features.h>
#include <xen/version.h>
#include <xen-features.h>
uint8_t xen_features[XENFEAT_NR_SUBMAPS * 32];
void setup_xen_features(void)
{
xen_feature_info_t fi;
int i, j;
for (i = 0; i < XENFEAT_NR_SUBMAPS; i++)
{
fi.submap_idx = i;
if (HYPERVISOR_xen_version(XENVER_get_features, &fi) < 0)
break;
for (j=0; j<32; j++)
xen_features[i*32+j] = !!(fi.submap & 1<<j);
}
}
/* This should be overridden by the application we are linked against. */
__attribute__((weak)) int app_main(start_info_t *si)
{
printk("Dummy main: start_info=%p\n", si);
return 0;
}
/*
* INITIAL C ENTRY POINT.
*/
void start_kernel(start_info_t *si)
{
static char hello[] = "Bootstrapping...\n";
(void)HYPERVISOR_console_io(CONSOLEIO_write, strlen(hello), hello);
setup_xen_features();
pvh_early_init();
arch_init(si);
trap_init();
/* print out some useful information */
printk("Xen Minimal OS!\n");
printk(" start_info: %p(VA)\n", si);
printk(" nr_pages: 0x%lx\n", si->nr_pages);
printk(" shared_inf: 0x%08lx(MA)\n", si->shared_info);
printk(" pt_base: %p(VA)\n", (void *)si->pt_base);
printk("nr_pt_frames: 0x%lx\n", si->nr_pt_frames);
printk(" mfn_list: %p(VA)\n", (void *)si->mfn_list);
printk(" mod_start: 0x%lx(VA)\n", si->mod_start);
printk(" mod_len: %lu\n", si->mod_len);
printk(" flags: 0x%x\n", (unsigned int)si->flags);
printk(" cmd_line: %s\n",
si->cmd_line ? (const char *)si->cmd_line : "NULL");
/* Set up events. */
init_events();
/* ENABLE EVENT DELIVERY. This is disabled at start of day. */
__sti();
arch_print_info();
/* Init memory management. */
init_mm();
/* Init time and timers. */
init_time();
/* Init the console driver. */
init_console();
/* Init grant tables */
init_gnttab();
/* Init scheduler. */
init_sched();
/* Init XenBus */
init_xenbus();
#ifdef CONFIG_XENBUS
/* Init shutdown thread */
init_shutdown(si);
#endif
/* Call (possibly overridden) app_main() */
app_main(&start_info);
/* Everything initialised, start idle thread */
run_idle_thread();
}
void stop_kernel(void)
{
/* TODO: fs import */
local_irq_disable();
/* Reset grant tables */
fini_gnttab();
/* Reset XenBus */
fini_xenbus();
/* Reset timers */
fini_time();
/* Reset memory management. */
fini_mm();
/* Reset events. */
fini_events();
/* Reset traps */
trap_fini();
/* Reset arch details */
arch_fini();
}
void pre_suspend(void)
{
#ifdef CONFIG_NETFRONT
suspend_netfront();
#endif
suspend_xenbus();
local_irq_disable();
suspend_gnttab();
suspend_time();
suspend_console();
suspend_events();
}
void post_suspend(int canceled)
{
resume_console();
resume_time();
resume_gnttab();
local_irq_enable();
resume_xenbus(canceled);
#ifdef CONFIG_NETFRONT
resume_netfront();
#endif
}
/*
* do_exit: This is called whenever an IRET fails in entry.S.
* This will generally be because an application has got itself into
* a really bad state (probably a bad CS or SS). It must be killed.
* Of course, minimal OS doesn't have applications :-)
*/
void do_exit(void)
{
printk("Do_exit called!\n");
stack_walk();
for( ;; )
{
struct sched_shutdown sched_shutdown = { .reason = SHUTDOWN_crash };
HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
}
}