Load grub modules on mem init

This commit is contained in:
Sam Tebbs 2019-11-02 02:00:49 +00:00 committed by Sam Tebbs
parent 762ddc3a63
commit 01490f051c
7 changed files with 49 additions and 14 deletions

View file

@ -10,6 +10,7 @@ const pit = @import("pit.zig");
const paging = @import("paging.zig");
const syscalls = @import("syscalls.zig");
const mem = @import("../../mem.zig");
const multiboot = @import("../../multiboot.zig");
const MemProfile = mem.MemProfile;
/// The interrupt context that is given to a interrupt handler. It contains most of the registers
@ -215,7 +216,7 @@ pub fn haltNoInterrupts() noreturn {
/// IN comptime options: type - The build options that is passed to the kernel to be
/// used for run time testing.
///
pub fn init(mem_profile: *const MemProfile, allocator: *Allocator, comptime options: type) void {
pub fn init(mb_info: *multiboot.multiboot_info_t, mem_profile: *const MemProfile, allocator: *Allocator) void {
disableInterrupts();
gdt.init();
@ -227,9 +228,9 @@ pub fn init(mem_profile: *const MemProfile, allocator: *Allocator, comptime opti
pit.init();
paging.init(mem_profile, allocator);
paging.init(mb_info, mem_profile, allocator);
syscalls.init(options);
syscalls.init();
enableInterrupts();
}

View file

@ -9,6 +9,7 @@ const MemProfile = @import("../../mem.zig").MemProfile;
const tty = @import("../../tty.zig");
const log = @import("../../log.zig");
const mem = @import("../../mem.zig");
const multiboot = @import("../../multiboot.zig");
const options = @import("build_options");
const testing = std.testing;
@ -290,7 +291,7 @@ fn pageFault(state: *arch.InterruptContext) void {
/// IN mem_profile: *const MemProfile - The memory profile of the system and kernel
/// IN allocator: *std.mem.Allocator - The allocator to use
///
pub fn init(mem_profile: *const MemProfile, allocator: *std.mem.Allocator) void {
pub fn init(mb_info: *multiboot.multiboot_info_t, mem_profile: *const MemProfile, allocator: *std.mem.Allocator) void {
log.logInfo("Init paging\n");
// Calculate start and end of mapping
const v_start = std.mem.alignBackward(@ptrToInt(mem_profile.vaddr_start), PAGE_SIZE_4KB);
@ -318,6 +319,30 @@ pub fn init(mem_profile: *const MemProfile, allocator: *std.mem.Allocator) void
};
}
// If the kernel mapping didn't cover the multiboot info, map it so it can be accessed by code later on
// There's no way to know the size, so an estimated size of 2MB is used. This will need increasing as the kernel gets bigger.
const mb_info_addr = std.mem.alignBackward(@ptrToInt(mb_info), PAGE_SIZE_4KB);
if (v_start > mb_info_addr) {
const mb_info_end = mb_info_addr + PAGE_SIZE_4MB / 2;
mapDir(kernel_directory, mb_info_addr, mb_info_end, mem.virtToPhys(mb_info_addr), mem.virtToPhys(mb_info_end), allocator) catch |e| {
panic(@errorReturnTrace(), "Failed to map mb_info in kernel directory: {}\n", e);
};
}
// Map in each boot module
for (mem_profile.boot_modules) |*module| {
const mod_p_struct_start = std.mem.alignBackward(@ptrToInt(module), PAGE_SIZE_4KB);
const mod_p_struct_end = std.mem.alignForward(mod_p_struct_start + @sizeOf(multiboot.multiboot_module_t), PAGE_SIZE_4KB);
mapDir(kernel_directory, mem.physToVirt(mod_p_struct_start), mem.physToVirt(mod_p_struct_end), mod_p_struct_start, mod_p_struct_end, allocator) catch |e| {
panic(@errorReturnTrace(), "Failed to map module struct: {}\n", e);
};
const mod_p_start = std.mem.alignBackward(module.mod_start, PAGE_SIZE_4KB);
const mod_p_end = std.mem.alignForward(module.mod_end, PAGE_SIZE_4KB);
mapDir(kernel_directory, mem.physToVirt(mod_p_start), mem.physToVirt(mod_p_end), mod_p_start, mod_p_end, allocator) catch |e| {
panic(@errorReturnTrace(), "Failed to map boot module in kernel directory: {}\n", e);
};
}
const dir_physaddr = @ptrToInt(mem.virtToPhys(kernel_directory));
asm volatile ("mov %[addr], %%cr3"
:

View file

@ -3,6 +3,7 @@ const testing = @import("std").testing;
const assert = @import("std").debug.assert;
const isr = @import("isr.zig");
const log = @import("../../log.zig");
const options = @import("build_options");
/// The isr number associated with syscalls
pub const INTERRUPT: u16 = 0x80;
@ -235,7 +236,7 @@ inline fn syscallArg(ctx: *arch.InterruptContext, comptime arg_idx: u32) u32 {
///
/// Initialise syscalls. Registers the isr associated with INTERRUPT.
///
pub fn init(comptime options: type) void {
pub fn init() void {
log.logInfo("Init syscalls\n");
isr.registerIsr(INTERRUPT, handle) catch unreachable;
log.logInfo("Done\n");