forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.cc
313 lines (259 loc) · 8.2 KB
/
loader.cc
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include "drivers/isa-serial.hh"
#include "fs/fs.hh"
#include <bsd/net.hh>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>
#include <cctype>
#include "elf.hh"
#include "tls.hh"
#include "msr.hh"
#include "exceptions.hh"
#include "debug.hh"
#include "drivers/pci.hh"
#include "smp.hh"
#include "xen.hh"
#include "ioapic.hh"
#include "drivers/acpi.hh"
#include "drivers/driver.hh"
#include "drivers/virtio-net.hh"
#include "drivers/virtio-blk.hh"
#include "drivers/xenfront-xenbus.hh"
#include "sched.hh"
#include "drivers/clock.hh"
#include "drivers/clockevent.hh"
#include "drivers/console.hh"
#include "drivers/pvpanic.hh"
#include "barrier.hh"
#include "arch.hh"
#include "osv/trace.hh"
#include <osv/power.hh>
#include <osv/rcu.hh>
#include "mempool.hh"
#include <bsd/porting/networking.h>
#include "dhcp.hh"
#include <osv/version.h>
using namespace osv;
asm(".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1 \n"
".byte 1 \n"
".asciz \"scripts/loader.py\" \n"
".popsection \n");
namespace {
void test_locale()
{
auto loc = std::locale();
auto &fac = std::use_facet<std::ctype<char>>(loc);
bool ok = fac.is(std::ctype_base::digit, '3')
&& !fac.is(std::ctype_base::digit, 'x');
debug(ok ? "locale works\n" : "locale fails\n");
//asm volatile ("1: jmp 1b");
}
}
elf::Elf64_Ehdr* elf_header;
size_t elf_size;
void* elf_start;
elf::tls_data tls_data;
void setup_tls(elf::init_table inittab)
{
tls_data = inittab.tls;
sched::init_tls(tls_data);
memset(tls_data.start + tls_data.filesize, 0, tls_data.size - tls_data.filesize);
extern char tcb0[]; // defined by linker script
memcpy(tcb0, inittab.tls.start, inittab.tls.size);
auto p = reinterpret_cast<thread_control_block*>(tcb0 + inittab.tls.size);
p->self = p;
processor::wrmsr(msr::IA32_FS_BASE, reinterpret_cast<uint64_t>(p));
}
extern "C" {
void premain();
void vfs_init(void);
void mount_usr(void);
void ramdisk_init(void);
}
void disable_pic()
{
// PIC not present in Xen
XENPV_ALTERNATIVE({ outb(0xff, 0x21); outb(0xff, 0xa1); }, {});
}
void premain()
{
disable_pic();
auto inittab = elf::get_init(elf_header);
setup_tls(inittab);
for (auto init = inittab.start; init < inittab.start + inittab.count; ++init) {
(*init)();
}
}
int main(int ac, char **av)
{
debug("OSv " OSV_VERSION " Copyright 2013 Cloudius Systems\n");
test_locale();
idt.load_on_cpu();
void main_cont(int ac, char** av);
sched::init([=] { main_cont(ac, av); });
}
static bool opt_leak = false;
static bool opt_noshutdown = false;
static bool opt_log_backtrace = false;
static bool opt_mount = true;
std::tuple<int, char**> parse_options(int ac, char** av)
{
namespace bpo = boost::program_options;
namespace bpos = boost::program_options::command_line_style;
std::vector<const char*> args = { "osv" };
// due to https://svn.boost.org/trac/boost/ticket/6991, we can't terminate
// command line parsing on the executable name, so we need to look for it
// ourselves
auto nr_options = std::find_if(av, av + ac,
[](const char* arg) { return arg[0] != '-'; }) - av;
std::copy(av, av + nr_options, std::back_inserter(args));
bpo::options_description desc("osv options:\n");
desc.add_options()
("help", "show help text\n")
("trace", bpo::value<std::vector<std::string>>(), "tracepoints to enable\n")
("trace-backtrace", "log backtraces in the tracepoint log\n")
("leak", "start leak detector after boot\n")
("nomount", "don't mount the file system\n")
("noshutdown", "continue running after main() returns\n")
;
bpo::variables_map vars;
// don't allow --foo bar (require --foo=bar) so we can find the first non-option
// argument
int style = bpos::unix_style & ~(bpos::long_allow_next | bpos::short_allow_next);
bpo::store(bpo::parse_command_line(args.size(), args.data(), desc, style), vars);
bpo::notify(vars);
if (vars.count("help")) {
std::cout << desc << "\n";
}
if (vars.count("leak")) {
opt_leak = true;
}
if (vars.count("noshutdown")) {
opt_noshutdown = true;
}
if (vars.count("trace-backtrace")) {
opt_log_backtrace = true;
}
if (vars.count("trace")) {
auto tv = vars["trace"].as<std::vector<std::string>>();
for (auto t : tv) {
std::vector<std::string> tmp;
boost::split(tmp, t, boost::is_any_of(" ,"), boost::token_compress_on);
for (auto t : tmp) {
enable_tracepoint(t);
}
}
}
opt_mount = !vars.count("nomount");
av += nr_options;
ac -= nr_options;
return std::make_tuple(ac, av);
}
struct argblock {
int ac;
char** av;
};
// Java uses this global variable (supplied by Glibc) to figure out the
// initial thread's stack end.
void *__libc_stack_end;
void run_main(elf::program *prog, struct argblock *args)
{
auto av = args->av;
auto ac = args->ac;
// Ensure that the shared library doesn't exit when we return by
// keeping a reference to it in the free store.
auto obj = *(new std::shared_ptr<elf::object>(prog->get_library(av[0])));
if (!obj) {
debug("run_main(): cannot execute %s. Aborting.\n", av[0]);
abort();
}
auto main = obj->lookup<void (int, char**)>("main");
assert(main);
if (opt_leak) {
debug("Enabling leak detector.\n");
memory::tracker_enabled = true;
}
__libc_stack_end = __builtin_frame_address(0);
main(ac, av);
}
void* do_main_thread(void *_args)
{
auto args = static_cast<argblock*>(_args);
// initialize panic drivers
panic::pvpanic::probe_and_setup();
// Enumerate PCI devices
pci::pci_device_enumeration();
// Initialize all drivers
hw::driver_manager* drvman = hw::driver_manager::instance();
drvman->register_driver(virtio::virtio_blk::probe);
drvman->register_driver(virtio::virtio_net::probe);
drvman->register_driver(xenfront::xenbus::probe);
drvman->load_all();
drvman->list_drivers();
if (opt_mount) {
mount_usr();
}
// Start DHCP by default and wait for an IP
if (!osv_start_if("eth0", "0.0.0.0", "255.255.255.0") && !osv_ifup("eth0"))
dhcp_start(true);
else
debug("Could not initialize network interface.\n");
run_main(elf::get_program(), args);
return nullptr;
}
namespace pthread_private {
void init_detached_pthreads_reaper();
}
void main_cont(int ac, char** av)
{
sched::preempt_disable();
std::tie(ac, av) = parse_options(ac, av);
ioapic::init();
smp_launch();
acpi::init();
sched::preempt_enable();
console::console_init();
memory::enable_debug_allocator();
enable_trace();
if (opt_log_backtrace) {
// can only do this after smp_launch, otherwise the IDT is not initialized,
// and backtrace_safe() fails as soon as we get an exception
tracepoint_base::log_backtraces();
}
sched::init_detached_threads_reaper();
rcu_init();
vfs_init();
ramdisk_init();
filesystem fs;
net_init();
processor::sti();
new elf::program(fs);
elf::get_program()->set_search_path({"/", "/usr/lib"});
pthread_t pthread;
// run the payload in a pthread, so pthread_self() etc. work
argblock args{ ac, av };
pthread_create(&pthread, nullptr, do_main_thread, &args);
void* retval;
pthread_join(pthread, &retval);
if (opt_noshutdown) {
// If the --noshutdown option is given, continue running the system,
// and whatever threads might be running, even after main returns
debug("main() returned.\n");
sched::thread::wait_until([] { return false; });
}
if (memory::tracker_enabled) {
debug("Leak testing done. Please use 'osv leak show' in gdb to analyze results.\n");
osv::halt();
} else {
debug("Powering off.\n");
osv::poweroff();
}
}
int __argc;
char** __argv;