Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/startup/main.c
2092 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Main.
32
*/
33
34
#include <types.h>
35
#include <kern/errno.h>
36
#include <kern/reboot.h>
37
#include <kern/unistd.h>
38
#include <lib.h>
39
#include <spl.h>
40
#include <clock.h>
41
#include <thread.h>
42
#include <current.h>
43
#include <synch.h>
44
#include <vm.h>
45
#include <mainbus.h>
46
#include <vfs.h>
47
#include <device.h>
48
#include <syscall.h>
49
#include <test.h>
50
#include <version.h>
51
#include "autoconf.h" // for pseudoconfig
52
53
54
/*
55
* These two pieces of data are maintained by the makefiles and build system.
56
* buildconfig is the name of the config file the kernel was configured with.
57
* buildversion starts at 1 and is incremented every time you link a kernel.
58
*
59
* The purpose is not to show off how many kernels you've linked, but
60
* to make it easy to make sure that the kernel you just booted is the
61
* same one you just built.
62
*/
63
extern const int buildversion;
64
extern const char buildconfig[];
65
66
/*
67
* Copyright message for the OS/161 base code.
68
*/
69
static const char harvard_copyright[] =
70
"Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009\n"
71
" President and Fellows of Harvard College. All rights reserved.\n";
72
73
74
/*
75
* Initial boot sequence.
76
*/
77
static
78
void
79
boot(void)
80
{
81
/*
82
* The order of these is important!
83
* Don't go changing it without thinking about the consequences.
84
*
85
* Among other things, be aware that console output gets
86
* buffered up at first and does not actually appear until
87
* mainbus_bootstrap() attaches the console device. This can
88
* be remarkably confusing if a bug occurs at this point. So
89
* don't put new code before mainbus_bootstrap if you don't
90
* absolutely have to.
91
*
92
* Also note that the buffer for this is only 1k. If you
93
* overflow it, the system will crash without printing
94
* anything at all. You can make it larger though (it's in
95
* dev/generic/console.c).
96
*/
97
98
kprintf("\n");
99
kprintf("OS/161 base version %s ASST1 solution version %s\n", BASE_VERSION, ASST1SOL_VERSION);
100
kprintf("%s", harvard_copyright);
101
kprintf("\n");
102
103
kprintf("Put-your-group-name-here's system version %s (%s #%d)\n",
104
GROUP_VERSION, buildconfig, buildversion);
105
kprintf("\n");
106
107
/* Early initialization. */
108
ram_bootstrap();
109
thread_bootstrap();
110
hardclock_bootstrap();
111
vfs_bootstrap();
112
113
/* Probe and initialize devices. Interrupts should come on. */
114
kprintf("Device probe...\n");
115
KASSERT(curthread->t_curspl > 0);
116
mainbus_bootstrap();
117
KASSERT(curthread->t_curspl == 0);
118
/* Now do pseudo-devices. */
119
pseudoconfig();
120
kprintf("\n");
121
122
/* Late phase of initialization. */
123
vm_bootstrap();
124
kprintf_bootstrap();
125
thread_start_cpus();
126
127
/* Default bootfs - but ignore failure, in case emu0 doesn't exist */
128
vfs_setbootfs("emu0");
129
130
131
/*
132
* Make sure various things aren't screwed up.
133
*/
134
COMPILE_ASSERT(sizeof(userptr_t) == sizeof(char *));
135
COMPILE_ASSERT(sizeof(*(userptr_t)0) == sizeof(char));
136
}
137
138
/*
139
* Shutdown sequence. Opposite to boot().
140
*/
141
static
142
void
143
shutdown(void)
144
{
145
146
kprintf("Shutting down.\n");
147
148
vfs_clearbootfs();
149
vfs_clearcurdir();
150
vfs_unmountall();
151
152
thread_shutdown();
153
154
splhigh();
155
}
156
157
/*****************************************/
158
159
/*
160
* reboot() system call.
161
*
162
* Note: this is here because it's directly related to the code above,
163
* not because this is where system call code should go. Other syscall
164
* code should probably live in the "syscall" directory.
165
*/
166
int
167
sys_reboot(int code)
168
{
169
switch (code) {
170
case RB_REBOOT:
171
case RB_HALT:
172
case RB_POWEROFF:
173
break;
174
default:
175
return EINVAL;
176
}
177
178
shutdown();
179
180
switch (code) {
181
case RB_HALT:
182
kprintf("The system is halted.\n");
183
mainbus_halt();
184
break;
185
case RB_REBOOT:
186
kprintf("Rebooting...\n");
187
mainbus_reboot();
188
break;
189
case RB_POWEROFF:
190
kprintf("The system is halted.\n");
191
mainbus_poweroff();
192
break;
193
}
194
195
panic("reboot operation failed\n");
196
return 0;
197
}
198
199
/*
200
* Kernel main. Boot up, then fork the menu thread; wait for a reboot
201
* request, and then shut down.
202
*/
203
void
204
kmain(char *arguments)
205
{
206
boot();
207
208
menu(arguments);
209
210
/* Should not get here */
211
}
212
213