pluto/test/mock/kernel/arch_mock.zig

259 lines
7 KiB
Zig
Raw Permalink Normal View History

const std = @import("std");
2020-11-23 21:02:21 +01:00
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const mem = @import("../../../src/kernel/mem.zig");
const MemProfile = mem.MemProfile;
const pci = @import("pci_mock.zig");
const gdt = @import("gdt_mock.zig");
2019-09-17 19:24:27 +02:00
const idt = @import("idt_mock.zig");
const vmm = @import("../../../src/kernel/vmm.zig");
2019-09-10 00:38:06 +02:00
const paging = @import("paging_mock.zig");
const Serial = @import("../../../src/kernel/serial.zig").Serial;
const TTY = @import("../../../src/kernel/tty.zig").TTY;
const Keyboard = @import("../../../src/kernel/keyboard.zig").Keyboard;
const task = @import("../../../src/kernel/task.zig");
const x86_paging = @import("../../../src/kernel/arch/x86/paging.zig");
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
pub const Device = pci.PciDeviceInfo;
pub const DateTime = struct {
second: u32,
minute: u32,
hour: u32,
day: u32,
month: u32,
year: u32,
century: u32,
day_of_week: u32,
};
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;
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
pub const CpuState = struct {
ss: u32,
gs: u32,
fs: u32,
es: u32,
ds: u32,
edi: u32,
esi: u32,
ebp: u32,
esp: u32,
ebx: u32,
edx: u32,
ecx: u32,
eax: u32,
int_num: u32,
error_code: u32,
eip: u32,
cs: u32,
eflags: u32,
user_esp: u32,
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
user_ss: u32,
2022-05-02 03:13:18 +02:00
pub fn empty() CpuState {
return .{
.ss = undefined,
.gs = undefined,
.fs = undefined,
.es = undefined,
.ds = undefined,
.edi = undefined,
.esi = undefined,
.ebp = undefined,
.esp = undefined,
.ebx = undefined,
.edx = undefined,
.ecx = undefined,
.eax = undefined,
.int_num = undefined,
.error_code = undefined,
.eip = undefined,
.cs = undefined,
.eflags = undefined,
.user_esp = undefined,
.user_ss = undefined,
};
}
};
2021-06-08 00:00:33 +02:00
pub const VmmPayload = switch (builtin.cpu.arch) {
2020-11-23 21:02:21 +01:00
.i386 => *x86_paging.Directory,
else => unreachable,
};
2021-06-08 00:00:33 +02:00
pub const KERNEL_VMM_PAYLOAD: VmmPayload = switch (builtin.cpu.arch) {
2020-11-23 21:02:21 +01:00
.i386 => &x86_paging.kernel_directory,
else => unreachable,
};
2020-01-09 17:16:51 +01:00
pub const MEMORY_BLOCK_SIZE: u32 = paging.PAGE_SIZE_4KB;
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
pub const STACK_SIZE: u32 = MEMORY_BLOCK_SIZE / @sizeOf(u32);
2020-11-23 21:02:21 +01:00
pub const VMM_MAPPER: vmm.Mapper(VmmPayload) = .{ .mapFn = map, .unmapFn = unmap };
pub const BootPayload = u8;
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
pub const Task = task.Task;
// The virtual/physical start/end of the kernel code
var KERNEL_PHYSADDR_START: u32 = 0x00100000;
var KERNEL_PHYSADDR_END: u32 = 0x01000000;
var KERNEL_VADDR_START: u32 = 0xC0100000;
var KERNEL_VADDR_END: u32 = 0xC1100000;
var KERNEL_ADDR_OFFSET: u32 = 0xC0000000;
2019-09-10 00:38:06 +02:00
2021-06-08 00:00:33 +02:00
pub fn map(start: usize, end: usize, p_start: usize, p_end: usize, attrs: vmm.Attributes, allocator: Allocator, payload: VmmPayload) !void {
_ = start;
_ = end;
_ = p_start;
_ = p_end;
_ = attrs;
_ = allocator;
_ = payload;
}
pub fn unmap(start: usize, end: usize, allocator: Allocator, payload: VmmPayload) !void {
_ = start;
_ = end;
_ = allocator;
_ = payload;
}
2020-11-23 21:02:21 +01:00
pub fn out(port: u16, data: anytype) void {
return mock_framework.performAction("out", void, .{ port, data });
}
pub fn in(comptime Type: type, port: u16) Type {
return mock_framework.performAction("in", Type, .{port});
}
pub fn ioWait() void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("ioWait", void, .{});
}
pub fn lgdt(gdt_ptr: *const gdt.GdtPtr) void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("lgdt", void, .{gdt_ptr});
}
pub fn sgdt() gdt.GdtPtr {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("sgdt", gdt.GdtPtr, .{});
}
pub fn ltr(offset: u16) void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("ltr", void, .{offset});
}
pub fn lidt(idt_ptr: *const idt.IdtPtr) void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("lidt", void, .{idt_ptr});
}
2019-09-17 19:24:27 +02:00
pub fn sidt() idt.IdtPtr {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("sidt", idt.IdtPtr, .{});
2019-09-17 19:24:27 +02:00
}
pub fn enableInterrupts() void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("enableInterrupts", void, .{});
}
pub fn disableInterrupts() void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("disableInterrupts", void, .{});
}
pub fn halt() void {
2020-01-01 20:12:36 +01:00
return mock_framework.performAction("halt", void, .{});
}
pub fn spinWait() noreturn {
while (true) {}
}
pub fn haltNoInterrupts() noreturn {
while (true) {}
}
2020-06-09 14:47:06 +02:00
pub fn initSerial(boot_payload: BootPayload) Serial {
2021-06-08 00:00:33 +02:00
// Suppress unused variable warnings
_ = boot_payload;
2020-06-04 15:39:37 +02:00
return .{ .write = undefined };
}
2020-06-12 13:05:50 +02:00
pub fn initTTY(boot_payload: BootPayload) TTY {
2021-06-08 00:00:33 +02:00
// Suppress unused variable warnings
_ = boot_payload;
2020-06-12 13:05:50 +02:00
return .{
.print = undefined,
.setCursor = undefined,
.cols = undefined,
.rows = undefined,
.clear = null,
};
}
pub fn initMem(payload: BootPayload) Allocator.Error!mem.MemProfile {
2021-06-08 00:00:33 +02:00
// Suppress unused variable warnings
_ = payload;
return MemProfile{
.vaddr_end = @ptrCast([*]u8, &KERNEL_VADDR_END),
.vaddr_start = @ptrCast([*]u8, &KERNEL_VADDR_START),
.physaddr_end = @ptrCast([*]u8, &KERNEL_PHYSADDR_END),
.physaddr_start = @ptrCast([*]u8, &KERNEL_PHYSADDR_START),
// Total memory available including the initial 1MiB that grub doesn't include
.mem_kb = 0,
.fixed_allocator = undefined,
.virtual_reserved = undefined,
.physical_reserved = undefined,
.modules = undefined,
};
}
2022-06-06 00:26:29 +02:00
pub fn initTask(t: *Task, entry_point: usize, allocator: Allocator, set_up_stack: bool) Allocator.Error!void {
2021-06-08 00:00:33 +02:00
// Suppress unused variable warnings
_ = t;
_ = entry_point;
_ = allocator;
2022-06-06 00:26:29 +02:00
_ = set_up_stack;
2021-06-08 00:00:33 +02:00
}
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
2021-06-08 00:00:33 +02:00
pub fn initKeyboard(allocator: Allocator) Allocator.Error!?*Keyboard {
// Suppress unused variable warnings
_ = allocator;
2020-07-14 22:52:54 +02:00
return null;
}
2021-06-08 00:00:33 +02:00
pub fn getDevices(allocator: Allocator) Allocator.Error![]Device {
// Suppress unused variable warnings
_ = allocator;
return &[_]Device{};
}
pub fn getDateTime() DateTime {
// TODO: Use the std lib std.time.timestamp() and convert
// Hard code 12:12:13 12/12/12 for testing
return .{
.second = 13,
.minute = 12,
.hour = 12,
.day = 12,
.month = 12,
.year = 2012,
.century = 2000,
.day_of_week = 4,
};
}
pub fn init(mem_profile: *const MemProfile) void {
2021-06-08 00:00:33 +02:00
// Suppress unused variable warnings
_ = mem_profile;
Initial scheduler Fix TSS Also change to .{} syntax where appropriate. Added the SS segment Fixed spelling Refactoring GDT Multitasking working for now WIP scheduler Refactored Bitmap a bit WIP still Task switching working Handlers return the stack pointer that will be used to restore the tasks stack, normal handlers will return the same stack pointer it was called with where task switching will return the stack pointer of the next task and restore its state using the interrupt stub. Initial scheduler done Created a stage 2 init task Change u32 to usize Move Task to arch specific WIP WIP2 Removed esp from task, replaced with stack_pointer Removed the debug logs Fixed init task stack Change pickNextTask to pointer manipulation This allows less allocations so faster switching Temporary enable interrupts for some runtime tests PIT and RTC need interrupts enabled to run their runtime tests Renamed schedule => pickNextTask, comptime bitmap for pids not task init And some other stuff: No pub for the task anymore Use the leak detector allocator Fmt Fix unit tests And some other stuff :P PR review Moved Task out of arch and have the stack init in the arch file Mocking clean up Removed commented code Renamed createTask to scheduleTask where the user will have to provide a task to schedule Removed redundant pub in log runtime test Removed global allocator for scheduler Cleaner assembly in paging Fmt Added new Scheduler test mode Added new test mode to CI Removed one of the prints Added doc comment, task test for i386 Removed test WIP Runtime tests work Have a global set in one task and reacted to in another. Also test that local variables are preserved after a task switch. Removed new lines Increased line length Move the allocation of the bool above the task creation
2020-07-18 23:46:24 +02:00
// I'll get back to this as this doesn't effect the current testing.
// When I come on to the mem.zig testing, I'll fix :)
//return mock_framework.performAction("init", void, mem_profile);
}
// User defined mocked functions
pub fn mock_disableInterrupts() void {}
pub fn mock_enableInterrupts() void {}
pub fn mock_ioWait() void {}