pluto/test/mock/kernel/mem_mock.zig

51 lines
1.2 KiB
Zig
Raw Normal View History

const std = @import("std");
const multiboot = @import("../../../src/kernel/multiboot.zig");
pub const Module = struct {
region: Range,
name: []const u8,
};
pub const Map = struct {
virtual: Range,
physical: ?Range,
};
pub const Range = struct {
start: usize,
end: usize,
};
pub const MemProfile = struct {
vaddr_end: [*]u8,
vaddr_start: [*]u8,
physaddr_end: [*]u8,
physaddr_start: [*]u8,
mem_kb: u32,
modules: []Module,
virtual_reserved: []Map,
physical_reserved: []Range,
fixed_allocator: std.heap.FixedBufferAllocator,
};
const FIXED_ALLOC_SIZE = 1024 * 1024;
2020-08-23 20:48:10 +02:00
const ADDR_OFFSET: usize = 100;
2020-07-12 19:06:54 +02:00
pub fn virtToPhys(virt: anytype) @TypeOf(virt) {
2020-01-09 17:16:51 +01:00
const T = @TypeOf(virt);
return switch (@typeInfo(T)) {
2020-08-23 20:48:10 +02:00
.Pointer => @intToPtr(T, @ptrToInt(virt) - ADDR_OFFSET),
.Int => virt - ADDR_OFFSET,
2020-01-09 17:16:51 +01:00
else => @compileError("Only pointers and integers are supported"),
};
}
2020-07-12 19:06:54 +02:00
pub fn physToVirt(phys: anytype) @TypeOf(phys) {
2020-01-09 17:16:51 +01:00
const T = @TypeOf(phys);
return switch (@typeInfo(T)) {
2020-08-23 20:48:10 +02:00
.Pointer => @intToPtr(T, @ptrToInt(phys) + ADDR_OFFSET),
.Int => phys + ADDR_OFFSET,
2020-01-09 17:16:51 +01:00
else => @compileError("Only pointers and integers are supported"),
};
}