Skip to content

Commit

Permalink
WIP Add more files for vfs :D
Browse files Browse the repository at this point in the history
Replace %% with % for registers in inline asm.
Move libs (lock, term) to kernel/lib/.
Add Tree structure in kernel/lib/tree.zig.
Add fs/zero.zig for /dev/zero and /dev/null.
Start initramfs.
  • Loading branch information
Ratakor committed Nov 3, 2023
1 parent 88ef8e1 commit 4fbc7b8
Show file tree
Hide file tree
Showing 28 changed files with 1,145 additions and 428 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ A kernel
- Add a checklist/roadmap
- Move tty and drivers out of kernel space
- Replace json with zon
- Replace ArrayList(Unmanaged) with HashMap where needed
- Replace unreachable with @panic
- Provide compatibility with Linux ABI
- Support RISC-V64, aarch64 and x86_64
- Replace limine with a custom bootloader?
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.version = "0.1.0",
.dependencies = .{
.limine = .{
.url = "https://github.com/limine-bootloader/limine-zig/archive/71a8e000f66db5ae755a70e4b27a2f93e19e0d1e.tar.gz",
.url = "https://github.com/limine-bootloader/limine-zig/archive/master.tar.gz",
.hash = "12200618cf5955c4f0c1e4639247cd24297641f9443e4f3f183a8201d70aeac52ed2",
},
},
Expand Down
52 changes: 0 additions & 52 deletions kernel/SpinLock.zig

This file was deleted.

2 changes: 1 addition & 1 deletion kernel/TTY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const std = @import("std");
const root = @import("root");
const SpinLock = @import("SpinLock.zig");
const SpinLock = root.SpinLock;

// TODO: termios
// TODO: user_write_lock -> user write on buffer + framebuffer, log user write?
Expand Down
94 changes: 48 additions & 46 deletions kernel/arch/x86_64/gdt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ const std = @import("std");
const log = std.log.scoped(.gdt);
const SpinLock = @import("root").SpinLock;

const GDTEntry = packed struct {
limit_low: u16 = 0,
base_low: u16 = 0,
base_mid: u8 = 0,
access: u8 = 0,
limit_high: u4 = 0,
flags: u4 = 0,
base_high: u8 = 0,

comptime {
std.debug.assert(@sizeOf(GDTEntry) == @sizeOf(u64));
std.debug.assert(@bitSizeOf(GDTEntry) == @bitSizeOf(u64));
}
};
/// Global Descriptor Table
const GDT = extern struct {
null_entry: Entry align(1),
kernel_code: Entry align(1),
kernel_data: Entry align(1),
user_code: Entry align(1),
user_data: Entry align(1),
tss: TSS.Descriptor align(1),

const Entry = packed struct {
limit_low: u16 = 0,
base_low: u16 = 0,
base_mid: u8 = 0,
access: u8 = 0,
limit_high: u4 = 0,
flags: u4 = 0,
base_high: u8 = 0,
};

const TSSDescriptor = packed struct {
limit_low: u16 = @sizeOf(TSS),
base_low: u16 = undefined,
base_mid: u8 = undefined,
access: u8 = 0b1000_1001,
limit_high: u4 = 0,
flags: u4 = 0,
base_high: u8 = undefined,
base_upper: u32 = undefined,
reserved: u32 = 0,
const Descriptor = extern struct {
limit: u16 align(1) = @sizeOf(GDT) - 1,
base: u64 align(1) = undefined,
};

comptime {
std.debug.assert(@sizeOf(TSSDescriptor) == @sizeOf(u64) * 2);
std.debug.assert(@bitSizeOf(TSSDescriptor) == @bitSizeOf(u64) * 2);
std.debug.assert(@sizeOf(GDT) == 7 * 8);
std.debug.assert(@sizeOf(Entry) == @sizeOf(u64));
std.debug.assert(@bitSizeOf(Entry) == @bitSizeOf(u64));
std.debug.assert(@bitSizeOf(Descriptor) == 80);
}
};

Expand All @@ -51,21 +51,23 @@ pub const TSS = extern struct {
reserved2: u64 align(1) = 0,
reserved3: u16 align(1) = 0,
iopb: u16 align(1),
};

/// Global Descriptor Table
const GDT = extern struct {
null_entry: GDTEntry align(8),
kernel_code: GDTEntry align(8),
kernel_data: GDTEntry align(8),
user_code: GDTEntry align(8),
user_data: GDTEntry align(8),
tss: TSSDescriptor align(8),
};
const Descriptor = packed struct {
limit_low: u16 = @sizeOf(TSS),
base_low: u16 = undefined,
base_mid: u8 = undefined,
access: u8 = 0b1000_1001,
limit_high: u4 = 0,
flags: u4 = 0,
base_high: u8 = undefined,
base_upper: u32 = undefined,
reserved: u32 = 0,
};

const GDTDescriptor = extern struct {
limit: u16 align(1) = @sizeOf(GDT) - 1,
base: u64 align(1) = undefined,
comptime {
std.debug.assert(@sizeOf(Descriptor) == @sizeOf(u64) * 2);
std.debug.assert(@bitSizeOf(Descriptor) == @bitSizeOf(u64) * 2);
}
};

pub const kernel_code = 0x08;
Expand All @@ -74,7 +76,7 @@ pub const user_code = 0x18;
pub const user_data = 0x20;
pub const tss_descriptor = 0x28;

var gdtr: GDTDescriptor = .{};
var gdtr: GDT.Descriptor = .{};
var gdt: GDT = .{
.null_entry = .{},
.kernel_code = .{
Expand Down Expand Up @@ -109,15 +111,15 @@ pub fn reload() void {
\\lgdt (%[gdtr])
// reload code segment register
\\push %[kcode]
\\lea 1f(%%rip), %%rax
\\push %%rax
\\lea 1f(%rip), %rax
\\push %rax
\\lretq
\\1:
// reload other segment registers
\\mov %[kdata], %%ax
\\mov %%ax, %%ds
\\mov %%ax, %%es
\\mov %%ax, %%ss
\\mov %[kdata], %ax
\\mov %ax, %ds
\\mov %ax, %es
\\mov %ax, %ss
:
: [gdtr] "r" (&gdtr),
[kcode] "i" (kernel_code),
Expand Down
92 changes: 46 additions & 46 deletions kernel/arch/x86_64/idt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,68 +243,68 @@ fn makeStubHandler(vector: u8) *const fn () callconv(.Naked) void {
export fn commonStub() callconv(.Naked) void {
asm volatile (
// if (cs != gdt.kernel_code) -> swapgs
\\cmpq $0x08, 24(%%rsp)
\\cmpq $0x08, 0x18(%rsp)
\\je 1f
\\swapgs
\\1:
\\push %%r15
\\push %%r14
\\push %%r13
\\push %%r12
\\push %%r11
\\push %%r10
\\push %%r9
\\push %%r8
\\push %%rbp
\\push %%rdi
\\push %%rsi
\\push %%rdx
\\push %%rcx
\\push %%rbx
\\push %%rax
\\mov %%es, %%ax
\\push %%rax
\\mov %%ds, %%ax
\\push %%rax
\\push %r15
\\push %r14
\\push %r13
\\push %r12
\\push %r11
\\push %r10
\\push %r9
\\push %r8
\\push %rbp
\\push %rdi
\\push %rsi
\\push %rdx
\\push %rcx
\\push %rbx
\\push %rax
\\mov %es, %ax
\\push %rax
\\mov %ds, %ax
\\push %rax
);

asm volatile (
\\mov 0x88(%%rsp), %%rdi
\\imul $8, %%rdi
\\add %%rdi, %%rax
\\mov %%rsp, %%rdi
\\call *(%%rax)
\\mov 0x88(%rsp), %rdi
\\imul $8, %rdi
\\add %rdi, %rax
\\mov %rsp, %rdi
\\call *(%rax)
:
: [_] "{rax}" (&isr),
);

asm volatile (
\\pop %%rax
\\mov %%ax, %%ds
\\pop %%rax
\\mov %%ax, %%es
\\pop %%rax
\\pop %%rbx
\\pop %%rcx
\\pop %%rdx
\\pop %%rsi
\\pop %%rdi
\\pop %%rbp
\\pop %%r8
\\pop %%r9
\\pop %%r10
\\pop %%r11
\\pop %%r12
\\pop %%r13
\\pop %%r14
\\pop %%r15
\\pop %rax
\\mov %ax, %ds
\\pop %rax
\\mov %ax, %es
\\pop %rax
\\pop %rbx
\\pop %rcx
\\pop %rdx
\\pop %rsi
\\pop %rdi
\\pop %rbp
\\pop %r8
\\pop %r9
\\pop %r10
\\pop %r11
\\pop %r12
\\pop %r13
\\pop %r14
\\pop %r15
// if (cs != gdt.kernel_code) -> swapgs
\\cmpq $0x08, 24(%%rsp)
\\cmpq $0x08, 24(%rsp)
\\je 1f
\\swapgs
\\1:
// restore stack
\\add $16, %%rsp
\\add $16, %rsp
\\iretq
);
}
4 changes: 2 additions & 2 deletions kernel/arch/x86_64/x86_64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ pub inline fn in(comptime T: type, port: u16) T {
}

pub inline fn readRegister(comptime reg: []const u8) u64 {
return asm volatile ("mov %%" ++ reg ++ ", %[res]"
return asm volatile ("mov %" ++ reg ++ ", %[res]"
: [res] "=r" (-> u64),
:
: "memory"
);
}

pub inline fn writeRegister(comptime reg: []const u8, value: u64) void {
asm volatile ("mov %[val], %%" ++ reg
asm volatile ("mov %[val], %" ++ reg
:
: [val] "r" (value),
: "memory"
Expand Down
11 changes: 8 additions & 3 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const root = @import("root");
const arch = @import("arch.zig");
const serial = @import("serial.zig");
const smp = @import("smp.zig");
const SpinLock = @import("SpinLock.zig");
const SpinLock = root.SpinLock;
const StackIterator = std.debug.StackIterator;
const readIntLittle = std.mem.readIntLittle;

Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, ret_addr: ?usize) nor
const writer = tty.writer();
writer.print(fmt, .{msg}) catch {};
printStackIterator(writer, stack_iter);
std.os.system.term.hideCursor(writer) catch {};
root.term.hideCursor(writer) catch {};
} else {
serial.print(fmt, .{msg});
printStackIterator(serial.writer, stack_iter);
Expand Down Expand Up @@ -152,20 +152,25 @@ const source_files = [_][]const u8{
"arch/x86_64/mem.zig",
"arch/x86_64/x86_64.zig",
"arch/x86_64.zig",
"fs/initramfs.zig",
"fs/tmpfs.zig",
"fs/zero.zig",
"lib/lock.zig",
"lib/term.zig",
"lib/tree.zig",
"acpi.zig",
"arch.zig",
"debug.zig",
"elf.zig",
"event.zig",
"lib.zig",
"main.zig",
"pmm.zig",
"ps2.zig",
"rand.zig",
"sched.zig",
"serial.zig",
"smp.zig",
"SpinLock.zig",
"time.zig",
"TTY.zig",
"vfs.zig",
Expand Down
2 changes: 1 addition & 1 deletion kernel/event.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const arch = @import("arch.zig");
const Thread = @import("sched.zig").Thread;
const SpinLock = @import("SpinLock.zig");
const SpinLock = @import("root").SpinLock;

// TODO: this is really ugly, also need threads to work
// TODO: using i as variable is ugly, use slices
Expand Down
Loading

0 comments on commit 4fbc7b8

Please sign in to comment.