From 8d7e7591e9e4828f713b309484c2231c9a69ee4d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 1 Dec 2019 22:10:20 +0100 Subject: [PATCH] cleaned up some task details --- src/arch/x86/gdt.zig | 13 +-------- src/arch/x86/paging.zig | 13 +++++---- src/console.zig | 15 +++++----- src/layout.zig | 10 +++---- src/main.zig | 12 ++------ src/task.zig | 64 ++++++++++++++++++++++++----------------- src/vga.zig | 2 +- src/vmem.zig | 10 +++++-- 8 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/arch/x86/gdt.zig b/src/arch/x86/gdt.zig index 8195a16..4e6622e 100644 --- a/src/arch/x86/gdt.zig +++ b/src/arch/x86/gdt.zig @@ -107,22 +107,11 @@ pub fn setKernelStack(esp0: usize) void { tss.esp0 = esp0; } -//// -// Load the GDT into the system registers (defined in assembly). -// -// Arguments: -// gdtr: Pointer to the GDTR. -// extern fn loadGDT(gdtr: *const GDTRegister) void; -//// -// Initialize the Global Descriptor Table. -// pub fn initialize() void { - // tty.step("Setting up the Global Descriptor Table"); - // Initialize GDT. - loadGDT(&gdtr); + loadGDT(&gdtr); //asm routine // Initialize TSS. // const tss_entry = makeEntry(@ptrToInt(&tss), @sizeOf(TSS) - 1, TSS_ACCESS, PROTECTED); diff --git a/src/arch/x86/paging.zig b/src/arch/x86/paging.zig index 1f9ad99..0d15ed1 100644 --- a/src/arch/x86/paging.zig +++ b/src/arch/x86/paging.zig @@ -20,13 +20,14 @@ fn pageFault() void { while (true) asm volatile ("hlt"); } -inline fn pageBase(virt: usize) usize { - return addr & (~PAGE_SIZE +% 1); +// TODO: inline these +fn pageBase(virt: usize) usize { + return virt & (~PAGE_SIZE +% 1); } -inline fn pde(virt: usize) *PageEntry { +fn pde(virt: usize) *PageEntry { return &PD[virt >> 22]; //relies on recursive mapping } -inline fn pte(virt: usize) *PageEntry { +fn pte(virt: usize) *PageEntry { return &PT[virt >> 12]; //relies on recursive mapping } @@ -38,7 +39,7 @@ pub fn translate(virt: usize) ?usize { pub fn unmap(virt: usize) void { if (translate(virt)) |phys| { - mem.free(phys); + pmem.free(phys); } else { kernel.println("can't unmap 0x{x} because it is not mapped.", virt); } @@ -64,7 +65,7 @@ pub fn initialize() void { assert(pmem.stack_end < kernel.layout.IDENTITY); interrupt.register(14, pageFault); - setupPaging(@ptrToInt(&pageDirectory[0])); + setupPaging(@ptrToInt(&pageDirectory[0])); //asm routine } pub fn introspect() void { diff --git a/src/console.zig b/src/console.zig index 8c5830a..5b21de7 100644 --- a/src/console.zig +++ b/src/console.zig @@ -3,14 +3,15 @@ usingnamespace @import("index.zig"); var command: [10]u8 = undefined; var command_len: usize = 0; -fn execute(com: []u8) void { +fn execute(input: []u8) void { const eql = std.mem.eql; - if (eql(u8, com, "x86paging")) return x86.paging.introspect(); - if (eql(u8, com, "x86memory")) return x86.pmem.introspect(); - if (eql(u8, com, "lspci")) return pci.lspci(); - if (eql(u8, com, "uptime")) return time.uptime(); - if (eql(u8, com, "topbar")) return topbar(); - println("{}: command not found", com); + if (eql(u8, input, "x86paging")) return x86.paging.introspect(); + if (eql(u8, input, "x86memory")) return x86.pmem.introspect(); + if (eql(u8, input, "tasks")) return task.introspect(); + if (eql(u8, input, "lspci")) return pci.lspci(); + if (eql(u8, input, "uptime")) return time.uptime(); + if (eql(u8, input, "topbar")) return topbar(); + println("{}: command not found", input); } pub fn keypress(char: u8) void { diff --git a/src/layout.zig b/src/layout.zig index 5e649de..da2f65a 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -1,8 +1,8 @@ //https://wiki.osdev.org/Memory_Map_(x86) // virtual memory layout of the kernel -const kiB = 1024; // bytes -const MiB = 1024 * kiB; // 0x100000 +const kiB = 1024; +const MiB = 1024 * kiB; const GiB = 1024 * MiB; // zig fmt: off @@ -11,7 +11,7 @@ pub const KERNEL = 1 * MiB; pub const IDENTITY = 4 * MiB; // 0->4MiB pub const HEAP = 8 * MiB; -pub const HEAP_END = 0x1000000; -pub const USER_STACKS = 0x1000000; -pub const USER_STACKS_END = 0x10000000; +pub const HEAP_END = 0x01000000; +pub const USER_STACKS = 0x01000000; +pub const USER_STACKS_END = 0x02000000; // zig fmt: on diff --git a/src/main.zig b/src/main.zig index 3f9224e..a341dd1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -24,17 +24,9 @@ export fn kmain(magic: u32, info: *const multiboot.MultibootInfo) noreturn { println("--- x86 initialization ---"); x86.x86_main(info); println("--- core initialization ---"); - pci.scan(); + // pci.scan(); vmem.initialize(); - - // var a = vmem.allocate(u32) catch unreachable; - // println("a={}", &a); - // const b = vmem.allocate(VGAEntry) catch unreachable; - // println("b={x}", &b); - - const t = task.Task.new(@ptrToInt(topbar)) catch unreachable; - println("task={x}", &t); - + task.initialize() catch unreachable; console.initialize(); while (true) asm volatile ("hlt"); diff --git a/src/task.zig b/src/task.zig index e291fe1..c0dc740 100644 --- a/src/task.zig +++ b/src/task.zig @@ -1,42 +1,52 @@ pub usingnamespace @import("index.zig"); -// var tasks = Array(?*Task).init(&mem.allocator); +const TASK_MAX = 1024; +var tasks = [1]?*Task{null} ** TASK_MAX; const STACK_SIZE = x86.PAGE_SIZE; // Size of thread stacks. +var tid_counter: u16 = 1; pub const Task = struct { - // context: isr.Context, - - //// - // Create a new thread inside the current process. - // NOTE: Do not call this function directly. Use Process.createThread instead. - // - // Arguments: - // entry_point: The entry point of the new thread. - // - // Returns: - // Pointer to the new thread structure. - // tid: u16, + stack_top: usize, + entrypoint: usize, + // context: isr.Context, + //cr3: usize, - pub fn stack(tid: u16) usize { - const stack = layout.USER_STACKS + (2 * (tid - 1) * STACK_SIZE); - assert(stack < layout.USER_STACKS_END); - return stack; + pub fn new(entrypoint: usize) !*Task { + // Allocate and initialize the thread structure. + var t = try vmem.allocate(Task); + + t.entrypoint = entrypoint; + t.tid = tid_counter; + tid_counter +%= 1; + assert(tid_counter != 0); //overflow + + t.stack_top = try vmem.malloc(STACK_SIZE); + assert(t.stack_top < layout.USER_STACKS_END); + + tasks[t.tid] = t; + return t; } - pub fn new(entry_point: usize) !*Task { - // assert(scheduler.current_process == process); - - // map the stack - - // Allocate and initialize the thread structure. - var this = try vmem.allocate(Task); - this.tid = 4; - - return this; + pub fn destroy(self: *Task) void { + tasks[self.tid] = null; + vmem.free(self.stack_top); + vmem.free(@ptrToInt(self)); } }; +pub fn initialize() !void { + const t = try Task.new(0x0); + println("task=0x{x}", t.stack_top); +} + +pub fn introspect() void { + for (tasks) |t| { + if (t == null) continue; + println("{}", t); + } +} + // fn initContext(entry_point: usize, stack: usize) isr.Context { // // Insert a trap return address to destroy the thread on return. // var stack_top = @intToPtr(*usize, stack + STACK_SIZE - @sizeOf(usize)); diff --git a/src/vga.zig b/src/vga.zig index 02d5847..e2ed17a 100644 --- a/src/vga.zig +++ b/src/vga.zig @@ -10,7 +10,7 @@ pub var vga = VGA{ .vram = @intToPtr([*]VGAEntry, 0xb8000)[0..0x4000], .cursor = 80 * 2, .foreground = Color.Black, - .background = Color.Brown, + .background = Color.Green, }; // Color codes. diff --git a/src/vmem.zig b/src/vmem.zig index bb33325..ece503f 100644 --- a/src/vmem.zig +++ b/src/vmem.zig @@ -14,13 +14,17 @@ pub fn available() usize { return stack_index * x86.PAGE_SIZE; } -pub fn allocate(comptime T: type) !*T { - assert(@sizeOf(T) < x86.PAGE_SIZE); // this allocator only support 1:1 mapping +pub fn malloc(size: usize) !usize { if (available() == 0) return error.OutOfMemory; stack_index -= 1; var vaddr: usize = stack[stack_index]; try x86.paging.mmap(vaddr, null); - return @intToPtr(*T, vaddr); + return vaddr; +} + +pub fn allocate(comptime T: type) !*T { + assert(@sizeOf(T) < x86.PAGE_SIZE); // this allocator only support 1:1 mapping + return @intToPtr(*T, try malloc(@sizeOf(T))); } pub fn free(address: usize) void {