Remove unneeded mocking files

This commit is contained in:
Sam Tebbs 2020-12-07 23:08:23 +00:00
parent 285b8d9579
commit b8ebc5f341
19 changed files with 52 additions and 235 deletions

View file

@ -8,7 +8,7 @@ const log = std.log.scoped(.x86_isr);
const build_options = @import("build_options");
const mock_path = build_options.arch_mock_path;
const syscalls = @import("syscalls.zig");
const panic = if (is_test) @import(mock_path ++ "panic_mock.zig").panic else @import("../../panic.zig").panic;
const panic = @import("../../panic.zig").panic;
const idt = if (is_test) @import(mock_path ++ "idt_mock.zig") else @import("idt.zig");
const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig");
const interrupts = @import("interrupts.zig");

View file

@ -12,8 +12,8 @@ const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("ar
const isr = @import("isr.zig");
const MemProfile = @import("../../mem.zig").MemProfile;
const tty = @import("../../tty.zig");
const mem = if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("../../mem.zig");
const vmm = if (is_test) @import(mock_path ++ "vmm_mock.zig") else @import("../../vmm.zig");
const mem = @import("../../mem.zig");
const vmm = @import("../../vmm.zig");
const pmm = @import("../../pmm.zig");
const multiboot = @import("multiboot.zig");
const Allocator = std.mem.Allocator;
@ -239,8 +239,10 @@ fn mapDirEntry(dir: *Directory, virt_start: usize, virt_end: usize, phys_start:
// Create a table and put the physical address in the dir entry
table = &(try allocator.alignedAlloc(Table, @truncate(u29, PAGE_SIZE_4KB), 1))[0];
@memset(@ptrCast([*]u8, table), 0, @sizeOf(Table));
const table_phys_addr = vmm.kernel_vmm.virtToPhys(@ptrToInt(table)) catch |e| {
panic(@errorReturnTrace(), "Failed getting the physical address for an allocated page table: {}\n", .{e});
const table_phys_addr = vmm.kernel_vmm.virtToPhys(@ptrToInt(table)) catch |e| blk: {
// When testing this will fail, but that's ok
if (!is_test) panic(@errorReturnTrace(), "Failed getting the physical address for a page table: {}\n", .{e});
break :blk 0;
};
dir_entry.* |= DENTRY_PAGE_ADDR & table_phys_addr;
dir.tables[entry] = table;
@ -535,6 +537,7 @@ test "mapDirEntry" {
defer dir.deinit();
const attrs = vmm.Attributes{ .kernel = false, .writable = false, .cachable = false };
vmm.kernel_vmm = try vmm.VirtualMemoryManager(arch.VmmPayload).init(PAGE_SIZE_4MB, 0xFFFFFFFF, allocator, arch.VMM_MAPPER, undefined);
defer vmm.kernel_vmm.deinit();
{
const phys: usize = 0 * PAGE_SIZE_4MB;
const phys_end: usize = phys + PAGE_SIZE_4MB;
@ -578,6 +581,10 @@ test "map and unmap" {
var allocator = std.testing.allocator;
var dir = Directory{ .entries = [_]DirectoryEntry{0} ** ENTRIES_PER_DIRECTORY, .tables = [_]?*Table{null} ** ENTRIES_PER_DIRECTORY, .allocator = allocator };
defer dir.deinit();
vmm.kernel_vmm = try vmm.VirtualMemoryManager(arch.VmmPayload).init(PAGE_SIZE_4MB, 0xFFFFFFFF, allocator, arch.VMM_MAPPER, undefined);
defer vmm.kernel_vmm.deinit();
const phys_start: usize = PAGE_SIZE_4MB * 2;
const virt_start: usize = PAGE_SIZE_4MB * 4;
const phys_end: usize = PAGE_SIZE_4MB * 4;

View file

@ -9,7 +9,7 @@ const log = std.log.scoped(.x86_pit);
const build_options = @import("build_options");
const mock_path = build_options.arch_mock_path;
const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig");
const panic = if (is_test) @import(mock_path ++ "panic_mock.zig").panic else @import("../../panic.zig").panic;
const panic = @import("../../panic.zig").panic;
const irq = @import("irq.zig");
const pic = @import("pic.zig");

View file

@ -12,7 +12,7 @@ const pic = @import("pic.zig");
const pit = @import("pit.zig");
const irq = @import("irq.zig");
const cmos = if (is_test) @import(mock_path ++ "cmos_mock.zig") else @import("cmos.zig");
const panic = if (is_test) @import(mock_path ++ "panic_mock.zig").panic else @import("../../panic.zig").panic;
const panic = @import("../../panic.zig").panic;
const scheduler = @import("../../scheduler.zig");
/// The Century register is unreliable. We need a APIC interface to infer if we have a century

View file

@ -9,7 +9,7 @@ const log = std.log.scoped(.x86_tty);
const build_options = @import("build_options");
const mock_path = build_options.mock_path;
const vga = if (is_test) @import("../../" ++ mock_path ++ "vga_mock.zig") else @import("vga.zig");
const panic = if (is_test) @import("../../" ++ mock_path ++ "panic_mock.zig").panic else @import("../../panic.zig").panic;
const panic = @import("../../panic.zig").panic;
/// The error set for if there is an error whiles printing.
const TtyError = error{

View file

@ -11,8 +11,8 @@ const mock_path = build_options.mock_path;
const Allocator = std.mem.Allocator;
const AutoHashMap = std.AutoHashMap;
const vfs = @import("vfs.zig");
const mem = if (is_test) @import("../" ++ mock_path ++ "mem_mock.zig") else @import("../mem.zig");
const panic = if (is_test) @import("../" ++ mock_path ++ "panic_mock.zig").panic else @import("../panic.zig").panic;
const mem = @import("../mem.zig");
const panic = @import("../panic.zig").panic;
/// The Initrd file system struct.
/// Format of raw ramdisk:

View file

@ -6,7 +6,7 @@ const builtin = @import("builtin");
const is_test = builtin.is_test;
const build_options = @import("build_options");
const mock_path = build_options.mock_path;
const vmm = if (is_test) @import(mock_path ++ "vmm_mock.zig") else @import("vmm.zig");
const vmm = @import("vmm.zig");
const panic = @import("panic.zig").panic;
pub const FreeListAllocator = struct {

View file

@ -10,10 +10,10 @@ const vga = @import("vga.zig");
const log_root = @import("log.zig");
const pmm = @import("pmm.zig");
const serial = @import("serial.zig");
const vmm = if (is_test) @import(mock_path ++ "vmm_mock.zig") else @import("vmm.zig");
const mem = if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("mem.zig");
const panic_root = if (is_test) @import(mock_path ++ "panic_mock.zig") else @import("panic.zig");
const task = if (is_test) @import(mock_path ++ "task_mock.zig") else @import("task.zig");
const vmm = @import("vmm.zig");
const mem = @import("mem.zig");
const panic_root = @import("panic.zig");
const task = @import("task.zig");
const heap = @import("heap.zig");
const scheduler = @import("scheduler.zig");
const vfs = @import("filesystem/vfs.zig");

View file

@ -4,7 +4,7 @@ const log = std.log.scoped(.pmm);
const build_options = @import("build_options");
const mock_path = build_options.mock_path;
const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig").internals;
const MemProfile = (if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("mem.zig")).MemProfile;
const MemProfile = @import("mem.zig").MemProfile;
const testing = std.testing;
const panic = @import("panic.zig").panic;
const Bitmap = @import("bitmap.zig").Bitmap;

View file

@ -8,10 +8,10 @@ const is_test = builtin.is_test;
const build_options = @import("build_options");
const mock_path = build_options.mock_path;
const arch = @import("arch.zig").internals;
const panic = if (is_test) @import(mock_path ++ "panic_mock.zig").panic else @import("panic.zig").panic;
const task = if (is_test) @import(mock_path ++ "task_mock.zig") else @import("task.zig");
const vmm = if (is_test) @import(mock_path ++ "vmm_mock.zig") else @import("vmm.zig");
const mem = if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("mem.zig");
const panic = @import("panic.zig").panic;
const task = @import("task.zig");
const vmm = @import("vmm.zig");
const mem = @import("mem.zig");
const fs = @import("filesystem/vfs.zig");
const Task = task.Task;
const EntryPoint = task.EntryPoint;
@ -187,13 +187,6 @@ fn destroyTestTask(self: *Task, allocator: *Allocator) void {
}
test "pickNextTask" {
task.initTest();
defer task.freeTest();
task.addConsumeFunction("Task.create", createTestTask);
task.addConsumeFunction("Task.create", createTestTask);
task.addRepeatFunction("Task.destroy", destroyTestTask);
var ctx: arch.CpuState = std.mem.zeroes(arch.CpuState);
var allocator = std.testing.allocator;
@ -216,8 +209,8 @@ test "pickNextTask" {
try scheduleTask(test_fn2_task, allocator);
// Get the stack pointers of the created tasks
const fn1_stack_pointer = tasks.first.?.data.stack_pointer;
const fn2_stack_pointer = tasks.first.?.next.?.data.stack_pointer;
const fn1_stack_pointer = test_fn1_task.stack_pointer;
const fn2_stack_pointer = test_fn2_task.stack_pointer;
expectEqual(pickNextTask(&ctx), fn1_stack_pointer);
// The stack pointer of the re-added task should point to the context
@ -250,12 +243,6 @@ test "pickNextTask" {
}
test "createNewTask add new task" {
task.initTest();
defer task.freeTest();
task.addConsumeFunction("Task.create", createTestTask);
task.addConsumeFunction("Task.destroy", destroyTestTask);
// Set the global allocator
var allocator = std.testing.allocator;
@ -273,18 +260,13 @@ test "createNewTask add new task" {
}
test "init" {
task.initTest();
defer task.freeTest();
task.addConsumeFunction("Task.create", createTestTask);
task.addRepeatFunction("Task.destroy", destroyTestTask);
var allocator = std.testing.allocator;
try init(allocator, undefined);
expectEqual(current_task.pid, 0);
expectEqual(current_task.kernel_stack, @intToPtr([*]u32, @ptrToInt(&KERNEL_STACK_START))[0 .. @ptrToInt(&KERNEL_STACK_END) - @ptrToInt(&KERNEL_STACK_START)]);
expectEqual(@ptrToInt(current_task.kernel_stack.ptr), @ptrToInt(&KERNEL_STACK_START));
expectEqual(current_task.kernel_stack.len, @ptrToInt(&KERNEL_STACK_END) - @ptrToInt(&KERNEL_STACK_START));
expectEqual(tasks.len, 1);

View file

@ -6,7 +6,7 @@ const is_test = builtin.is_test;
const build_options = @import("build_options");
const mock_path = build_options.mock_path;
const arch = @import("arch.zig").internals;
const panic = if (is_test) @import(mock_path ++ "panic_mock.zig").panic else @import("panic.zig").panic;
const panic = @import("panic.zig").panic;
const ComptimeBitmap = @import("bitmap.zig").ComptimeBitmap;
const vmm = @import("vmm.zig");
const Allocator = std.mem.Allocator;
@ -160,7 +160,7 @@ test "create out of memory for task" {
expectEqual(fa.allocated_bytes, fa.freed_bytes);
// Make sure no PIDs were allocated
expectEqual(all_pids.bitmap, 1);
expectEqual(all_pids.bitmap, 0);
}
test "create out of memory for stack" {
@ -174,21 +174,21 @@ test "create out of memory for stack" {
expectEqual(fa.allocated_bytes, fa.freed_bytes);
// Make sure no PIDs were allocated
expectEqual(all_pids.bitmap, 1);
expectEqual(all_pids.bitmap, 0);
}
test "create expected setup" {
var task = try Task.create(@ptrToInt(test_fn1), true, undefined, std.testing.allocator);
defer task.destroy(std.testing.allocator);
// Will allocate the first PID 1, 0 will always be allocated
expectEqual(task.pid, 1);
// Will allocate the first PID 0
expectEqual(task.pid, 0);
expectEqual(task.kernel_stack.len, STACK_SIZE);
expectEqual(task.user_stack.len, 0);
var user_task = try Task.create(@ptrToInt(test_fn1), false, undefined, std.testing.allocator);
defer user_task.destroy(std.testing.allocator);
expectEqual(user_task.pid, 2);
expectEqual(user_task.pid, 1);
expectEqual(user_task.user_stack.len, STACK_SIZE);
expectEqual(user_task.kernel_stack.len, STACK_SIZE);
}
@ -205,42 +205,42 @@ test "destroy cleans up" {
user_task.destroy(allocator);
// All PIDs were freed
expectEqual(all_pids.bitmap, 1);
expectEqual(all_pids.bitmap, 0);
}
test "Multiple create" {
var task1 = try Task.create(@ptrToInt(test_fn1), true, undefined, std.testing.allocator);
var task2 = try Task.create(@ptrToInt(test_fn1), true, undefined, std.testing.allocator);
expectEqual(task1.pid, 1);
expectEqual(task2.pid, 2);
expectEqual(all_pids.bitmap, 7);
expectEqual(task1.pid, 0);
expectEqual(task2.pid, 1);
expectEqual(all_pids.bitmap, 3);
task1.destroy(std.testing.allocator);
expectEqual(all_pids.bitmap, 5);
expectEqual(all_pids.bitmap, 2);
var task3 = try Task.create(@ptrToInt(test_fn1), true, undefined, std.testing.allocator);
expectEqual(task3.pid, 1);
expectEqual(all_pids.bitmap, 7);
expectEqual(task3.pid, 0);
expectEqual(all_pids.bitmap, 3);
task2.destroy(std.testing.allocator);
task3.destroy(std.testing.allocator);
var user_task = try Task.create(@ptrToInt(test_fn1), false, undefined, std.testing.allocator);
expectEqual(user_task.pid, 1);
expectEqual(all_pids.bitmap, 3);
expectEqual(user_task.pid, 0);
expectEqual(all_pids.bitmap, 1);
user_task.destroy(std.testing.allocator);
expectEqual(all_pids.bitmap, 1);
expectEqual(all_pids.bitmap, 0);
}
test "allocatePid and freePid" {
expectEqual(all_pids.bitmap, 1);
expectEqual(all_pids.bitmap, 0);
var i: usize = 1;
var i: usize = 0;
while (i < PidBitmap.NUM_ENTRIES) : (i += 1) {
expectEqual(i, allocatePid());
}

View file

@ -6,7 +6,7 @@ const std = @import("std");
const log = std.log.scoped(.vmm);
const bitmap = @import("bitmap.zig");
const pmm = @import("pmm.zig");
const mem = if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("mem.zig");
const mem = @import("mem.zig");
const tty = @import("tty.zig");
const panic = @import("panic.zig").panic;
const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig").internals;

View file

@ -47,12 +47,9 @@ const types = .{
.{ "IdtPtr", "IDTPTR", "idt_mock", "", "IdtPtr" },
.{ "*const GdtPtr", "PTR_CONST_GDTPTR", "gdt_mock", "", "GdtPtr" },
.{ "*const IdtPtr", "PTR_CONST_IDTPTR", "idt_mock", "", "IdtPtr" },
.{ "*Task", "PTR_TASK", "task_mock", "", "Task" },
.{ "*Allocator", "PTR_ALLOCATOR", "", "std.mem", "Allocator" },
.{ "*VirtualMemoryManager(u8)", "PTR_VMM", "vmm_mock", "", "VirtualMemoryManager" },
.{ "IdtError!void", "ERROR_IDTERROR_RET_VOID", "idt_mock", "", "IdtError" },
.{ "Allocator.Error!*Task", "ERROR_ALLOCATOR_RET_PTRTASK", "", "", "" },
.{ "fn () callconv(.C) void", "FN_CCC_OVOID", "", "", "" },
.{ "fn () callconv(.Naked) void", "FN_CCNAKED_OVOID", "", "", "" },
@ -80,10 +77,6 @@ const types = .{
.{ "fn (u16, u16) void", "FN_IU16_IU16_OVOID", "", "", "" },
.{ "fn (u16, u32) void", "FN_IU16_IU32_OVOID", "", "", "" },
.{ "fn (StatusRegister, bool) u8", "FN_ISTATUSREGISTER_IBOOL_OU8", "", "", "" },
.{ "fn (*Task, usize) void", "FN_IPTRTASK_IUSIZE_OVOID", "", "", "" },
.{ "fn (*Task, *Allocator) void", "FN_IPTRTASK_IPTRALLOCATOR_OVOID", "", "", "" },
.{ "fn (fn () void, *Allocator) Allocator.Error!*Task", "FN_IFNOVOID_IPTRALLOCATOR_EALLOCATOR_OPTRTASK", "", "", "" },
.{ "fn (usize, *Allocator, bool, *VirtualMemoryManager(u8)) Allocator.Error!*Task", "FN_IUSIZE_IPTRALLOCATOR_IBOOL_IVMM_EALLOCATOR_OVOID", "", "", "" },
.{ "fn (StatusRegister, u8, bool) void", "FN_ISTATUSREGISTER_IU8_IBOOL_OVOID", "", "", "" },
};

View file

@ -1,11 +1,11 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const mem = @import("mem_mock.zig");
const mem = @import("../../../src/kernel/mem.zig");
const MemProfile = mem.MemProfile;
const pci = @import("pci_mock.zig");
const gdt = @import("gdt_mock.zig");
const idt = @import("idt_mock.zig");
const vmm = @import("vmm_mock.zig");
const vmm = @import("../../../src/kernel/vmm.zig");
const paging = @import("paging_mock.zig");
const Serial = @import("../../../src/kernel/serial.zig").Serial;
const TTY = @import("../../../src/kernel/tty.zig").TTY;

View file

@ -1,12 +0,0 @@
const std = @import("std");
const mock_framework = @import("mock_framework.zig");
pub const initTest = mock_framework.initTest;
pub const freeTest = mock_framework.freeTest;
pub const addTestParams = mock_framework.addTestParams;
pub const addConsumeFunction = mock_framework.addConsumeFunction;
pub const addRepeatFunction = mock_framework.addRepeatFunction;
pub fn log(comptime level: std.log.Level, comptime format: []const u8, args: anytype) void {
// Just print to std print
std.debug.print(format, args);
}

View file

@ -1,52 +0,0 @@
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,
};
pub var fixed_buffer_allocator: std.heap.FixedBufferAllocator = undefined;
const FIXED_ALLOC_SIZE = 1024 * 1024;
const ADDR_OFFSET: usize = 100;
pub fn virtToPhys(virt: anytype) @TypeOf(virt) {
const T = @TypeOf(virt);
return switch (@typeInfo(T)) {
.Pointer => @intToPtr(T, @ptrToInt(virt) - ADDR_OFFSET),
.Int => virt - ADDR_OFFSET,
else => @compileError("Only pointers and integers are supported"),
};
}
pub fn physToVirt(phys: anytype) @TypeOf(phys) {
const T = @TypeOf(phys);
return switch (@typeInfo(T)) {
.Pointer => @intToPtr(T, @ptrToInt(phys) + ADDR_OFFSET),
.Int => phys + ADDR_OFFSET,
else => @compileError("Only pointers and integers are supported"),
};
}

View file

@ -1,13 +0,0 @@
const builtin = @import("builtin");
const std = @import("std");
const MemProfile = @import("mem_mock.zig").MemProfile;
pub fn panic(trace: ?*builtin.StackTrace, comptime format: []const u8, args: anytype) noreturn {
@setCold(true);
std.debug.panic(format, args);
}
pub fn init(mem_profile: *const MemProfile, allocator: *std.mem.Allocator) !void {
// This is never run so just return an arbitrary error to satisfy the compiler
return error.NotNeeded;
}

View file

@ -1,32 +0,0 @@
const std = @import("std");
const vmm = @import("vmm_mock.zig");
const arch = @import("arch_mock.zig");
const Allocator = std.mem.Allocator;
const mock_framework = @import("mock_framework.zig");
pub const initTest = mock_framework.initTest;
pub const freeTest = mock_framework.freeTest;
pub const addTestParams = mock_framework.addTestParams;
pub const addConsumeFunction = mock_framework.addConsumeFunction;
pub const addRepeatFunction = mock_framework.addRepeatFunction;
pub const EntryPoint = usize;
pub const Task = struct {
const Self = @This();
pid: u32,
kernel_stack: []u32,
user_stack: []u32,
stack_pointer: usize,
kernel: bool,
vmm: vmm.VirtualMemoryManager(arch.VmmPayload),
pub fn create(entry_point: EntryPoint, kernel: bool, task_vmm: *vmm.VirtualMemoryManager(arch.VmmPayload), allocator: *Allocator) Allocator.Error!*Task {
return mock_framework.performAction("Task.create", Allocator.Error!*Task, .{ entry_point, allocator, kernel, task_vmm });
}
pub fn destroy(self: *Self, allocator: *Allocator) void {
return mock_framework.performAction("Task.destroy", void, .{ self, allocator });
}
};

View file

@ -1,56 +0,0 @@
const mem = @import("mem_mock.zig");
const bitmap = @import("../../../src/kernel/bitmap.zig");
const vmm = @import("../../../src/kernel/vmm.zig");
const arch = @import("arch_mock.zig");
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const VmmError = error{
/// A memory region expected to be allocated wasn't
NotAllocated,
};
pub const Attributes = vmm.Attributes;
pub const BLOCK_SIZE: u32 = 1024;
pub const Mapper = vmm.Mapper;
pub const MapperError = error{
InvalidVirtualAddress,
InvalidPhysicalAddress,
AddressMismatch,
MisalignedVirtualAddress,
MisalignedPhysicalAddress,
NotMapped,
};
pub var kernel_vmm: VirtualMemoryManager(arch.VmmPayload) = undefined;
pub fn VirtualMemoryManager(comptime Payload: type) type {
return struct {
const Self = @This();
pub fn init(start: usize, end: usize, allocator: *std.mem.Allocator, mapper: Mapper(Payload), payload: Payload) std.mem.Allocator.Error!Self {
return Self{};
}
pub fn virtToPhys(self: *const Self, virt: usize) VmmError!usize {
return 0;
}
pub fn alloc(self: *Self, num: u32, attrs: Attributes) std.mem.Allocator.Error!?usize {
return std.mem.Allocator.Error.OutOfMemory;
}
pub fn free(self: *Self, vaddr: usize) (bitmap.Bitmap(u32).BitmapError || VmmError)!void {
return VmmError.NotAllocated;
}
pub fn copyData(self: *Self, other: *Self, data: []const u8, dest: usize, from_self: bool) (bitmap.Bitmap(usize).BitmapError || VmmError || Allocator.Error)!void {}
};
}
pub fn init(mem_profile: *const mem.MemProfile, allocator: *Allocator) Allocator.Error!*VirtualMemoryManager(arch.VmmPayload) {
return std.mem.Allocator.Error.OutOfMemory;
}