Skip to content

Commit

Permalink
Rename pmem and vmeme to pmm and vmm, starting vmm
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Sep 27, 2023
1 parent b6a1514 commit 4cba3b2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
23 changes: 10 additions & 13 deletions kernel/idt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ pub fn init() void {
// idt[sched_call_vector].ist = 1;
// idt[syscall_vector].type_attributes = 0xEE;

idtReload();
reloadIDT();
}

pub fn reloadIDT() void {
asm volatile ("lidt (%[idtr])"
:
: [idtr] "r" (&idtr),
: "memory"
);
}

pub fn allocateVector() u8 {
Expand All @@ -109,14 +117,6 @@ pub fn registerHandler(vector: u8, handler: InterruptHandler) void {
isr[vector] = handler;
}

fn idtReload() void {
asm volatile ("lidt (%[idtr])"
:
: [idtr] "r" (&idtr),
: "memory"
);
}

fn exceptionHandler(ctx: *cpu.Context) void {
const vector = ctx.isr_vector;

Expand All @@ -132,10 +132,7 @@ fn exceptionHandler(ctx: *cpu.Context) void {
ctx.error_code,
});
} else {
std.debug.panic("Unhandled interrupt triggered with vector: {} and error code: {}", .{
vector,
ctx.error_code,
});
std.debug.panic("Unhandled exception triggered, dumping context\n{}", .{ctx});
}
}

Expand Down
18 changes: 9 additions & 9 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const serial = @import("serial.zig");
const tty = @import("tty.zig");
const gdt = @import("gdt.zig");
const idt = @import("idt.zig");
const pmem = @import("pmem.zig");
const vmem = @import("vmem.zig");
const pmm = @import("pmm.zig");
const vmm = @import("vmm.zig");
const debug = @import("debug.zig");

// pub const page_allocator = mem.page_allocator;
Expand Down Expand Up @@ -84,17 +84,17 @@ fn main() !void {
serial.init();
gdt.init();
idt.init();
// TODO: init events <-- for interrupts
try pmm.init();

try pmem.init();

const buf = try pmem.alloc(u8, 1, false);
defer pmem.free(buf);
const buf = try pmm.alloc(u8, 1, false);
defer pmm.free(buf);
tty.print("{*} {}\n", .{ buf.ptr, buf.len });
const buf2 = try pmem.alloc(u64, 1, false);
defer pmem.free(buf2);
const buf2 = try pmm.alloc(u64, 1, false);
defer pmm.free(buf2);
tty.print("{*} {}\n", .{ buf2.ptr, buf2.len });

try vmem.init();
try vmm.init();

asm volatile ("sti");
@breakpoint();
Expand Down
8 changes: 4 additions & 4 deletions kernel/mem.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const vmem = @import("vmem.zig");
const vmm = @import("vmm.zig");

const Allocator = std.mem.Allocator;
const page_size = std.mem.page_size;
Expand All @@ -21,7 +21,7 @@ fn alloc(_: *anyopaque, size: usize, _: u8, _: usize) ?[*]u8 {

// TODO
_ = aligned_size;
// return pmem.alloc(u8, @divExact(aligned_size, page_size), false) catch return null;
// return pmm.alloc(u8, @divExact(aligned_size, page_size), false) catch return null;

return null;
}
Expand All @@ -36,7 +36,7 @@ fn resize(_: *anyopaque, buf: []u8, _: u8, new_size: usize, _: usize) bool {
const ptr = buf.ptr + aligned_new_size;
// TODO
_ = ptr;
// vmem.free(@alignCast(ptr[0 .. aligned_buf_len - aligned_new_size]));
// vmm.free(@alignCast(ptr[0 .. aligned_buf_len - aligned_new_size]));
return true;
}

Expand All @@ -47,5 +47,5 @@ fn free(_: *anyopaque, buf: []u8, _: u8, _: usize) void {
// TODO
_ = buf;
// const aligned_buf_len = std.mem.alignForward(usize, buf.len, page_size);
// vmem.free(@alignCast(ptr[0 .. aligned_buf_len]));
// vmm.free(@alignCast(ptr[0 .. aligned_buf_len]));
}
13 changes: 6 additions & 7 deletions kernel/pmem.zig → kernel/pmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const page_size = std.mem.page_size;
const free_page = false;

// TODO use u64 and logical operation to speed up the process ?
var lock: SpinLock = .{};
var bitmap: []bool = undefined;
var last_idx: u64 = 0;
var usable_pages: u64 = 0;
var used_pages: u64 = 0;
var reserved_pages: u64 = 0;
var lock: SpinLock = .{};

pub fn init() !void {
const memory_map = root.memory_map_request.response.?;
Expand Down Expand Up @@ -101,6 +101,7 @@ fn innerAlloc(pages: usize, limit: u64) ?u64 {
p = 0;
}
}
last_idx = 0;
return null;
}

Expand Down Expand Up @@ -138,20 +139,18 @@ pub fn free(memory: anytype) void {
used_pages -= pages;
}

// pub fn alloc(pages: usize, zero: bool) ?*anyopaque {
// pub fn alloc(pages: usize, comptime zero: bool) ?*anyopaque {
// lock.lock();
// defer lock.unlock();

// const last = last_idx;
// const address = innerAlloc(pages, bitmap.len) orelse blk: {
// last_idx = 0;
// break :blk innerAlloc(pages, last) orelse return null;
// };
// const address = innerAlloc(pages, bitmap.len) orelse
// innerAlloc(pages, last) orelse return null;

// used_pages += pages;
// const ptr: [*]u8 = @ptrFromInt(address);
// const slice = ptr[0 .. pages * page_size];
// if (zero) @memset(slice, 0);
// comptime if (zero) @memset(slice, 0);

// return slice;
// }
Expand Down
4 changes: 4 additions & 0 deletions kernel/vmem.zig → kernel/vmm.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const std = @import("std");
const limine = @import("limine");
const root = @import("root");
const SpinLock = @import("lock.zig").SpinLock;
const tty = @import("tty.zig");
const pmm = @import("pmm.zig");

const page_size = std.mem.page_size;

pub const Flags = enum(u64) {
present = 1 << 0,
Expand Down

0 comments on commit 4cba3b2

Please sign in to comment.