Skip to content

Commit

Permalink
Add comptime assert for types + pmm.reclaimMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Oct 19, 2023
1 parent a5b8706 commit a781ca3
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 103 deletions.
8 changes: 6 additions & 2 deletions kernel/SpinLock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ inline fn lockFast(self: *SpinLock, comptime cas_fn_name: []const u8) bool {
fn lockSlow(self: *SpinLock) void {
@setCold(true);

for (0..100_000_000) |_| {
// TODO
// for (0..100_000_000) |_| {
while (true) {
if (self.lockFast("tryCompareAndSwap")) {
return;
}
Expand All @@ -47,5 +49,7 @@ fn lockSlow(self: *SpinLock) void {

pub fn unlock(self: *SpinLock) void {
const state = self.state.swap(unlocked, .Release);
std.debug.assert(state == locked);
// TODO
_ = state;
// std.debug.assert(state == locked);
}
8 changes: 7 additions & 1 deletion kernel/arch/x86_64/cpu.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");
const limine = @import("limine");
const root = @import("root");
const x86 = @import("x86_64.zig");
const gdt = @import("idt.zig");
const idt = @import("idt.zig");
const log = @import("std").log.scoped(.cpu);
const log = std.log.scoped(.cpu);

const PAT = packed struct {
// zig fmt: off
Expand All @@ -25,6 +26,11 @@ const PAT = packed struct {
write_back = 6,
uncached = 7,
};

comptime {
std.debug.assert(@sizeOf(PAT) == @sizeOf(u64));
std.debug.assert(@bitSizeOf(PAT) == @bitSizeOf(u64));
}
};

/// https://en.wikipedia.org/wiki/CPUID#EAX=1:_Processor_Info_and_Feature_Bits
Expand Down
13 changes: 12 additions & 1 deletion kernel/arch/x86_64/gdt.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const log = @import("std").log.scoped(.gdt);
const std = @import("std");
const log = std.log.scoped(.gdt);
const SpinLock = @import("root").SpinLock;

const GDTEntry = packed struct {
Expand All @@ -9,6 +10,11 @@ const GDTEntry = packed struct {
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));
}
};

const TSSDescriptor = packed struct {
Expand All @@ -21,6 +27,11 @@ const TSSDescriptor = packed struct {
base_high: u8 = undefined,
base_upper: u32 = undefined,
reserved: u32 = 0,

comptime {
std.debug.assert(@sizeOf(TSSDescriptor) == @sizeOf(u64) * 2);
std.debug.assert(@bitSizeOf(TSSDescriptor) == @bitSizeOf(u64) * 2);
}
};

/// Task State Segment
Expand Down
7 changes: 7 additions & 0 deletions kernel/arch/x86_64/x86_64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const assert = @import("std").debug.assert;

pub const Rflags = packed struct {
CF: u1 = 0,
reserved: u1 = 1,
Expand All @@ -21,6 +23,11 @@ pub const Rflags = packed struct {
VIP: u1 = 0,
ID: u1 = 0,
reserved4: u42 = 0,

comptime {
assert(@sizeOf(Rflags) == @sizeOf(u64));
assert(@bitSizeOf(Rflags) == @bitSizeOf(u64));
}
};

pub const CPUID = struct {
Expand Down
2 changes: 2 additions & 0 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
arch.halt();
}

// TODO: send signal to other cpu to stop them

const fmt = "\x1b[m\x1b[31m\nKernel panic:\x1b[m {s}\n";
if (root.tty0) |tty| {
const writer = tty.writer();
Expand Down
111 changes: 56 additions & 55 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,22 @@ export fn _start() noreturn {
smp.init();
time.init();

// TODO: crash due to page fault
// const kernel_thread = sched.Thread.initKernel(@ptrCast(&main), null, 1) catch unreachable;
// sched.enqueue(kernel_thread) catch unreachable;

arch.enableInterrupts();
const kernel_thread = sched.Thread.initKernel(@ptrCast(&main), null, 1) catch unreachable;
sched.enqueue(kernel_thread) catch unreachable;
sched.wait();
}

fn main() !void {
const fb = framebuffer_request.response.?.framebuffers()[0];
tty0 = TTY.init(fb.address, fb.width, fb.height, callback) catch unreachable;
// const fb = framebuffer_request.response.?.framebuffers()[0];
// tty0 = TTY.init(fb.address, fb.width, fb.height, callback) catch unreachable;

const boot_info = boot_info_request.response.?;
tty0.?.writer().print("Welcome to Ubik, brought to you by {s} {s} :)\n", .{
boot_info.name,
boot_info.version,
}) catch unreachable;
// const boot_info = boot_info_request.response.?;
// tty0.?.writer().print("Welcome to Ubik, brought to you by {s} {s} :)\n", .{
// boot_info.name,
// boot_info.version,
// }) catch unreachable;

ps2.init();
// ps2.init();
// TODO: pci
// TODO: vfs
// TODO: basic syscalls
Expand All @@ -108,48 +105,52 @@ fn main() !void {
// TODO: filesystem
// TODO: IPC: pipe, socket (TCP, UDP, Unix)

var regs = arch.cpuid(0, 0);
std.log.debug("vendor string: {s}{s}{s}", .{
@as([*]const u8, @ptrCast(&regs.ebx))[0..4],
@as([*]const u8, @ptrCast(&regs.edx))[0..4],
@as([*]const u8, @ptrCast(&regs.ecx))[0..4],
});

regs = arch.cpuid(0x80000000, 0);
if (regs.eax >= 0x80000004) {
regs = arch.cpuid(0x80000002, 0);
serial.writer.print("cpu name: {s}{s}{s}{s}", .{
@as([*]const u8, @ptrCast(&regs.eax))[0..4],
@as([*]const u8, @ptrCast(&regs.ebx))[0..4],
@as([*]const u8, @ptrCast(&regs.ecx))[0..4],
@as([*]const u8, @ptrCast(&regs.edx))[0..4],
}) catch unreachable;
regs = arch.cpuid(0x80000003, 0);
serial.writer.print("{s}{s}{s}{s}", .{
@as([*]const u8, @ptrCast(&regs.eax))[0..4],
@as([*]const u8, @ptrCast(&regs.ebx))[0..4],
@as([*]const u8, @ptrCast(&regs.ecx))[0..4],
@as([*]const u8, @ptrCast(&regs.edx))[0..4],
}) catch unreachable;
regs = arch.cpuid(0x80000004, 0);
serial.writer.print("{s}{s}{s}{s}\n", .{
@as([*]const u8, @ptrCast(&regs.eax))[0..4],
@as([*]const u8, @ptrCast(&regs.ebx))[0..4],
@as([*]const u8, @ptrCast(&regs.ecx))[0..4],
@as([*]const u8, @ptrCast(&regs.edx))[0..4],
}) catch unreachable;
}

const rand = @import("rand.zig");
var pcg = rand.Pcg.init(rand.getSeedSlow());
inline for (0..8) |_| {
const thread = sched.Thread.initKernel(
@ptrCast(&hihihi),
null,
pcg.random().int(u4),
) catch unreachable;
sched.enqueue(thread) catch unreachable;
}
// pmm.reclaimMemory();

// var regs = arch.cpuid(0, 0);
// std.log.debug("vendor string: {s}{s}{s}", .{
// @as([*]const u8, @ptrCast(&regs.ebx))[0..4],
// @as([*]const u8, @ptrCast(&regs.edx))[0..4],
// @as([*]const u8, @ptrCast(&regs.ecx))[0..4],
// });

// regs = arch.cpuid(0x80000000, 0);
// if (regs.eax >= 0x80000004) {
// regs = arch.cpuid(0x80000002, 0);
// serial.writer.print("cpu name: {s}{s}{s}{s}", .{
// @as([*]const u8, @ptrCast(&regs.eax))[0..4],
// @as([*]const u8, @ptrCast(&regs.ebx))[0..4],
// @as([*]const u8, @ptrCast(&regs.ecx))[0..4],
// @as([*]const u8, @ptrCast(&regs.edx))[0..4],
// }) catch unreachable;
// regs = arch.cpuid(0x80000003, 0);
// serial.writer.print("{s}{s}{s}{s}", .{
// @as([*]const u8, @ptrCast(&regs.eax))[0..4],
// @as([*]const u8, @ptrCast(&regs.ebx))[0..4],
// @as([*]const u8, @ptrCast(&regs.ecx))[0..4],
// @as([*]const u8, @ptrCast(&regs.edx))[0..4],
// }) catch unreachable;
// regs = arch.cpuid(0x80000004, 0);
// serial.writer.print("{s}{s}{s}{s}\n", .{
// @as([*]const u8, @ptrCast(&regs.eax))[0..4],
// @as([*]const u8, @ptrCast(&regs.ebx))[0..4],
// @as([*]const u8, @ptrCast(&regs.ecx))[0..4],
// @as([*]const u8, @ptrCast(&regs.edx))[0..4],
// }) catch unreachable;
// }

// const rand = @import("rand.zig");
// var pcg = rand.Pcg.init(rand.getSeedSlow());
// inline for (0..8) |_| {
// const thread = sched.Thread.initKernel(
// @ptrCast(&hihihi),
// null,
// pcg.random().int(u4),
// ) catch unreachable;
// sched.enqueue(thread) catch unreachable;
// }

arch.halt();
}

fn hihihi() void {
Expand Down
25 changes: 19 additions & 6 deletions kernel/pmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ const root = @import("root");
const vmm = @import("vmm.zig");
const SpinLock = @import("SpinLock.zig");
const log = std.log.scoped(.pmm);

const page_size = std.mem.page_size;
const free_page = false;

const free_page = false;
// TODO: use u64 and bitwise operation to speed up the process?
// TODO: decide what to do with "useless" stuff
var bitmap: []bool = undefined;
var last_idx: u64 = 0;
var usable_pages: u64 = 0; // useless?
var used_pages: u64 = 0; // useless?
var reserved_pages: u64 = 0; // useless?
var usable_pages: u64 = 0;
var used_pages: u64 = 0;
var reserved_pages: u64 = 0;
var lock: SpinLock = .{}; // TODO: remove lock on pmm and only use
// root.allocator for risky allocations,
// or remove lock on root.allocator?
Expand Down Expand Up @@ -73,6 +71,21 @@ pub fn init() void {
log.info("reserved memory: {} MiB", .{(reserved_pages * page_size) / 1024 / 1024});
}

pub fn reclaimMemory() void {
const memory_map = root.memory_map_request.response.?;

for (memory_map.entries()) |entry| {
if (entry.kind == .bootloader_reclaimable) {
const pages = entry.length / page_size;
usable_pages += pages;
reserved_pages -= pages;
free(entry.base, pages);

log.info("reclaimed {} pages at 0x{x}", .{ pages, entry.base });
}
}
}

fn innerAlloc(pages: usize, limit: u64) ?u64 {
var p: usize = 0;

Expand Down
Loading

0 comments on commit a781ca3

Please sign in to comment.