forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiboot.cpp
More file actions
188 lines (157 loc) · 5.85 KB
/
Copy pathmultiboot.cpp
File metadata and controls
188 lines (157 loc) · 5.85 KB
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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <os>
#include <kprint>
#include <boot/multiboot.h>
#include <kernel/memory.hpp>
// #define DEBUG_MULTIBOOT
#if defined(DEBUG_MULTIBOOT)
#undef debug
#define debug(X,...) kprintf(X,##__VA_ARGS__);
#define MYINFO(X,...) kprintf("<Multiboot>" X "\n", ##__VA_ARGS__)
#undef INFO2
#define INFO2(X,...) kprintf("\t" X "\n", ##__VA_ARGS__)
#else
#define debug(X,...)
#define MYINFO(X,...) INFO("Kernel", X, ##__VA_ARGS__)
#endif
extern uintptr_t _end;
using namespace util::bitops;
using namespace util::literals;
static inline multiboot_info_t* bootinfo(uint32_t addr)
{
// NOTE: the address is 32-bit and not a pointer
return (multiboot_info_t*) (uintptr_t) addr;
}
multiboot_info_t* OS::bootinfo()
{
extern uint32_t __multiboot_addr;
return (multiboot_info_t*) (uintptr_t) __multiboot_addr;
}
uintptr_t _multiboot_memory_end(uintptr_t boot_addr) {
auto* info = bootinfo(boot_addr);
if (info->flags & MULTIBOOT_INFO_MEMORY) {
return 0x100000 + (info->mem_upper * 1024);
}
return __arch_max_canonical_addr;
}
// Deterimine the end of multiboot provided data
// (e.g. multiboot's data area as offset to the _end symbol)
uintptr_t _multiboot_free_begin(uintptr_t boot_addr)
{
auto* info = bootinfo(boot_addr);
uintptr_t multi_end = reinterpret_cast<uintptr_t>(&_end);
debug("* Multiboot begin: 0x%x \n", info);
if (info->flags & MULTIBOOT_INFO_CMDLINE
and info->cmdline > multi_end)
{
debug("* Multiboot cmdline @ 0x%x: %s \n", info->cmdline, (char*)info->cmdline);
// We can't use a cmdline that's either insde our ELF or pre-ELF area
Expects(info->cmdline > multi_end
or info->cmdline < 0x100000);
if (info->cmdline > multi_end) {
auto* cmdline_ptr = (const char*) (uintptr_t) info->cmdline;
// Set free begin to after the cmdline string
multi_end = info->cmdline + strlen(cmdline_ptr) + 1;
}
}
debug("* Multiboot end: 0x%x \n", multi_end);
if (info->mods_count == 0)
return multi_end;
auto* mods_list = (multiboot_module_t*) (uintptr_t) info->mods_addr;
debug("* Module list @ %p \n",mods_list);
for (multiboot_module_t* mod = mods_list;
mod < mods_list + info->mods_count;
mod ++) {
debug("\t * Module @ %#x \n", mod->mod_start);
debug("\t * Args: %s \n ", (char*) (uintptr_t) mod->cmdline);
debug("\t * End: %#x \n ", mod->mod_end);
if (mod->mod_end > multi_end)
multi_end = mod->mod_end;
}
debug("* Multiboot end: 0x%x \n", multi_end);
return multi_end;
}
void OS::multiboot(uint32_t boot_addr)
{
MYINFO("Booted with multiboot");
auto* info = ::bootinfo(boot_addr);
INFO2("* Boot flags: %#x", info->flags);
if (info->flags & MULTIBOOT_INFO_MEMORY) {
uint32_t mem_low_start = 0;
uint32_t mem_low_end = (info->mem_lower * 1024) - 1;
uint32_t mem_low_kb = info->mem_lower;
uint32_t mem_high_start = 0x100000;
uint32_t mem_high_end = mem_high_start + (info->mem_upper * 1024) - 1;
uint32_t mem_high_kb = info->mem_upper;
INFO2("* Valid memory (%i Kib):", mem_low_kb + mem_high_kb);
INFO2(" 0x%08x - 0x%08x (%i Kib)",
mem_low_start, mem_low_end, mem_low_kb);
INFO2(" 0x%08x - 0x%08x (%i Kib)",
mem_high_start, mem_high_end, mem_high_kb);
INFO2("");
}
else {
INFO2("No memory information from multiboot");
}
if (info->flags & MULTIBOOT_INFO_CMDLINE) {
const auto* cmdline = (const char*) (uintptr_t) info->cmdline;
INFO2("* Booted with parameters @ %p: %s", cmdline, cmdline);
OS::cmdline = strdup(cmdline);
}
if (info->flags & MULTIBOOT_INFO_MEM_MAP) {
INFO2("* Multiboot provided memory map (%zu entries @ %p)",
info->mmap_length / sizeof(multiboot_memory_map_t),
(void*) (uintptr_t) info->mmap_addr);
gsl::span<multiboot_memory_map_t> mmap {
reinterpret_cast<multiboot_memory_map_t*>(info->mmap_addr),
(int)(info->mmap_length / sizeof(multiboot_memory_map_t))
};
for (auto map : mmap)
{
const char* str_type = map.type & MULTIBOOT_MEMORY_AVAILABLE ? "FREE" : "RESERVED";
const uintptr_t addr = map.addr;
const uintptr_t size = map.len;
INFO2(" 0x%010zx - 0x%010zx %s (%zu Kb.)",
addr, addr + size - 1, str_type, size / 1024 );
if (not (map.type & MULTIBOOT_MEMORY_AVAILABLE)) {
if (util::bits::is_aligned<4_KiB>(map.addr)) {
os::mem::map({addr, addr, os::mem::Access::read | os::mem::Access::write, size},
"Reserved (Multiboot)");
continue;
}
// For non-aligned addresses, assign
memory_map().assign_range({addr, addr + size - 1, "Reserved (Multiboot)"});
}
else
{
// Map as free memory
//os::mem::map_avail({map.addr, map.addr, {os::mem::Access::read | os::mem::Access::write}, map.len}, "Reserved (Multiboot)");
}
}
printf("\n");
}
Span_mods mods = modules();
if (not mods.empty()) {
MYINFO("OS loaded with %zu modules", mods.size());
for (auto mod : mods) {
INFO2("* %s @ 0x%x - 0x%x, size: %ib",
reinterpret_cast<char*>(mod.cmdline),
mod.mod_start, mod.mod_end, mod.mod_end - mod.mod_start);
}
}
}