cleaned up some task details
This commit is contained in:
parent
ed5b97a87b
commit
8d7e7591e9
8 changed files with 68 additions and 71 deletions
|
|
@ -107,22 +107,11 @@ pub fn setKernelStack(esp0: usize) void {
|
||||||
tss.esp0 = esp0;
|
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;
|
extern fn loadGDT(gdtr: *const GDTRegister) void;
|
||||||
|
|
||||||
////
|
|
||||||
// Initialize the Global Descriptor Table.
|
|
||||||
//
|
|
||||||
pub fn initialize() void {
|
pub fn initialize() void {
|
||||||
// tty.step("Setting up the Global Descriptor Table");
|
|
||||||
|
|
||||||
// Initialize GDT.
|
// Initialize GDT.
|
||||||
loadGDT(&gdtr);
|
loadGDT(&gdtr); //asm routine
|
||||||
|
|
||||||
// Initialize TSS.
|
// Initialize TSS.
|
||||||
// const tss_entry = makeEntry(@ptrToInt(&tss), @sizeOf(TSS) - 1, TSS_ACCESS, PROTECTED);
|
// const tss_entry = makeEntry(@ptrToInt(&tss), @sizeOf(TSS) - 1, TSS_ACCESS, PROTECTED);
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,14 @@ fn pageFault() void {
|
||||||
while (true) asm volatile ("hlt");
|
while (true) asm volatile ("hlt");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn pageBase(virt: usize) usize {
|
// TODO: inline these
|
||||||
return addr & (~PAGE_SIZE +% 1);
|
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
|
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
|
return &PT[virt >> 12]; //relies on recursive mapping
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,7 +39,7 @@ pub fn translate(virt: usize) ?usize {
|
||||||
|
|
||||||
pub fn unmap(virt: usize) void {
|
pub fn unmap(virt: usize) void {
|
||||||
if (translate(virt)) |phys| {
|
if (translate(virt)) |phys| {
|
||||||
mem.free(phys);
|
pmem.free(phys);
|
||||||
} else {
|
} else {
|
||||||
kernel.println("can't unmap 0x{x} because it is not mapped.", virt);
|
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);
|
assert(pmem.stack_end < kernel.layout.IDENTITY);
|
||||||
|
|
||||||
interrupt.register(14, pageFault);
|
interrupt.register(14, pageFault);
|
||||||
setupPaging(@ptrToInt(&pageDirectory[0]));
|
setupPaging(@ptrToInt(&pageDirectory[0])); //asm routine
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn introspect() void {
|
pub fn introspect() void {
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,15 @@ usingnamespace @import("index.zig");
|
||||||
var command: [10]u8 = undefined;
|
var command: [10]u8 = undefined;
|
||||||
var command_len: usize = 0;
|
var command_len: usize = 0;
|
||||||
|
|
||||||
fn execute(com: []u8) void {
|
fn execute(input: []u8) void {
|
||||||
const eql = std.mem.eql;
|
const eql = std.mem.eql;
|
||||||
if (eql(u8, com, "x86paging")) return x86.paging.introspect();
|
if (eql(u8, input, "x86paging")) return x86.paging.introspect();
|
||||||
if (eql(u8, com, "x86memory")) return x86.pmem.introspect();
|
if (eql(u8, input, "x86memory")) return x86.pmem.introspect();
|
||||||
if (eql(u8, com, "lspci")) return pci.lspci();
|
if (eql(u8, input, "tasks")) return task.introspect();
|
||||||
if (eql(u8, com, "uptime")) return time.uptime();
|
if (eql(u8, input, "lspci")) return pci.lspci();
|
||||||
if (eql(u8, com, "topbar")) return topbar();
|
if (eql(u8, input, "uptime")) return time.uptime();
|
||||||
println("{}: command not found", com);
|
if (eql(u8, input, "topbar")) return topbar();
|
||||||
|
println("{}: command not found", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keypress(char: u8) void {
|
pub fn keypress(char: u8) void {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
//https://wiki.osdev.org/Memory_Map_(x86)
|
//https://wiki.osdev.org/Memory_Map_(x86)
|
||||||
// virtual memory layout of the kernel
|
// virtual memory layout of the kernel
|
||||||
|
|
||||||
const kiB = 1024; // bytes
|
const kiB = 1024;
|
||||||
const MiB = 1024 * kiB; // 0x100000
|
const MiB = 1024 * kiB;
|
||||||
const GiB = 1024 * MiB;
|
const GiB = 1024 * MiB;
|
||||||
|
|
||||||
// zig fmt: off
|
// zig fmt: off
|
||||||
|
|
@ -11,7 +11,7 @@ pub const KERNEL = 1 * MiB;
|
||||||
pub const IDENTITY = 4 * MiB; // 0->4MiB
|
pub const IDENTITY = 4 * MiB; // 0->4MiB
|
||||||
|
|
||||||
pub const HEAP = 8 * MiB;
|
pub const HEAP = 8 * MiB;
|
||||||
pub const HEAP_END = 0x1000000;
|
pub const HEAP_END = 0x01000000;
|
||||||
pub const USER_STACKS = 0x1000000;
|
pub const USER_STACKS = 0x01000000;
|
||||||
pub const USER_STACKS_END = 0x10000000;
|
pub const USER_STACKS_END = 0x02000000;
|
||||||
// zig fmt: on
|
// zig fmt: on
|
||||||
|
|
|
||||||
12
src/main.zig
12
src/main.zig
|
|
@ -24,17 +24,9 @@ export fn kmain(magic: u32, info: *const multiboot.MultibootInfo) noreturn {
|
||||||
println("--- x86 initialization ---");
|
println("--- x86 initialization ---");
|
||||||
x86.x86_main(info);
|
x86.x86_main(info);
|
||||||
println("--- core initialization ---");
|
println("--- core initialization ---");
|
||||||
pci.scan();
|
// pci.scan();
|
||||||
vmem.initialize();
|
vmem.initialize();
|
||||||
|
task.initialize() catch unreachable;
|
||||||
// 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);
|
|
||||||
|
|
||||||
console.initialize();
|
console.initialize();
|
||||||
|
|
||||||
while (true) asm volatile ("hlt");
|
while (true) asm volatile ("hlt");
|
||||||
|
|
|
||||||
64
src/task.zig
64
src/task.zig
|
|
@ -1,42 +1,52 @@
|
||||||
pub usingnamespace @import("index.zig");
|
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.
|
const STACK_SIZE = x86.PAGE_SIZE; // Size of thread stacks.
|
||||||
|
var tid_counter: u16 = 1;
|
||||||
|
|
||||||
pub const Task = struct {
|
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,
|
tid: u16,
|
||||||
|
stack_top: usize,
|
||||||
|
entrypoint: usize,
|
||||||
|
// context: isr.Context,
|
||||||
|
//cr3: usize,
|
||||||
|
|
||||||
pub fn stack(tid: u16) usize {
|
pub fn new(entrypoint: usize) !*Task {
|
||||||
const stack = layout.USER_STACKS + (2 * (tid - 1) * STACK_SIZE);
|
// Allocate and initialize the thread structure.
|
||||||
assert(stack < layout.USER_STACKS_END);
|
var t = try vmem.allocate(Task);
|
||||||
return stack;
|
|
||||||
|
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 {
|
pub fn destroy(self: *Task) void {
|
||||||
// assert(scheduler.current_process == process);
|
tasks[self.tid] = null;
|
||||||
|
vmem.free(self.stack_top);
|
||||||
// map the stack
|
vmem.free(@ptrToInt(self));
|
||||||
|
|
||||||
// Allocate and initialize the thread structure.
|
|
||||||
var this = try vmem.allocate(Task);
|
|
||||||
this.tid = 4;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 {
|
// fn initContext(entry_point: usize, stack: usize) isr.Context {
|
||||||
// // Insert a trap return address to destroy the thread on return.
|
// // Insert a trap return address to destroy the thread on return.
|
||||||
// var stack_top = @intToPtr(*usize, stack + STACK_SIZE - @sizeOf(usize));
|
// var stack_top = @intToPtr(*usize, stack + STACK_SIZE - @sizeOf(usize));
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub var vga = VGA{
|
||||||
.vram = @intToPtr([*]VGAEntry, 0xb8000)[0..0x4000],
|
.vram = @intToPtr([*]VGAEntry, 0xb8000)[0..0x4000],
|
||||||
.cursor = 80 * 2,
|
.cursor = 80 * 2,
|
||||||
.foreground = Color.Black,
|
.foreground = Color.Black,
|
||||||
.background = Color.Brown,
|
.background = Color.Green,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Color codes.
|
// Color codes.
|
||||||
|
|
|
||||||
10
src/vmem.zig
10
src/vmem.zig
|
|
@ -14,13 +14,17 @@ pub fn available() usize {
|
||||||
return stack_index * x86.PAGE_SIZE;
|
return stack_index * x86.PAGE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocate(comptime T: type) !*T {
|
pub fn malloc(size: usize) !usize {
|
||||||
assert(@sizeOf(T) < x86.PAGE_SIZE); // this allocator only support 1:1 mapping
|
|
||||||
if (available() == 0) return error.OutOfMemory;
|
if (available() == 0) return error.OutOfMemory;
|
||||||
stack_index -= 1;
|
stack_index -= 1;
|
||||||
var vaddr: usize = stack[stack_index];
|
var vaddr: usize = stack[stack_index];
|
||||||
try x86.paging.mmap(vaddr, null);
|
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 {
|
pub fn free(address: usize) void {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue