Skip to content

Commit

Permalink
Start devtmpfs, move memory management to mm
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Nov 8, 2023
1 parent 2b6694e commit f89e31a
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 158 deletions.
4 changes: 2 additions & 2 deletions kernel/acpi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
const root = @import("root");
const arch = @import("arch.zig");
const apic = arch.apic;
const vmm = @import("vmm.zig");
const vmm = root.vmm;
const log = std.log.scoped(.acpi);

/// System Description Table
Expand All @@ -21,7 +21,7 @@ const SDT = extern struct {

inline fn data(self: *const SDT) []const u8 {
const ptr: [*]const u8 = @ptrCast(self);
return ptr[0..self.length][@sizeOf(SDT)..];
return ptr[@sizeOf(SDT)..self.length];
}

fn doChecksum(self: *const SDT) void {
Expand Down
4 changes: 2 additions & 2 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ const source_files = [_][]const u8{
"lib/lock.zig",
"lib/term.zig",
"lib/tree.zig",
"mm/pmm.zig",
"mm/vmm.zig",
"acpi.zig",
"arch.zig",
"debug.zig",
"elf.zig",
"event.zig",
"lib.zig",
"main.zig",
"pmm.zig",
"ps2.zig",
"rand.zig",
"sched.zig",
Expand All @@ -187,7 +188,6 @@ const source_files = [_][]const u8{
"time.zig",
"TTY.zig",
"vfs.zig",
"vmm.zig",
};

// TODO: get source files from filesystem instead + zig std lib files
Expand Down
4 changes: 2 additions & 2 deletions kernel/elf.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const root = @import("root");
const pmm = @import("pmm.zig");
const vmm = @import("vmm.zig");
const pmm = root.pmm;
const vmm = root.vmm;
const page_size = std.mem.page_size;

pub const AuxVal = struct {
Expand Down
117 changes: 117 additions & 0 deletions kernel/fs/devtmpfs.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const std = @import("std");
const root = @import("root");
const vfs = root.vfs;

const Device = struct {
node: vfs.Node,
data: std.ArrayListUnmanaged(u8) = .{},

pub fn init(mode: std.os.mode_t) *Device {
const device = try root.allocator.create(Device);
device.* = .{ .node = try vfs.Node.init("", null, null) }; // TODO

if (std.os.S.ISREG(mode)) {
// resource->capacity = 4096;
// resource->data = alloc(resource->capacity);
// resource->can_mmap = true;
}

// TODO vtable is global
// device.node.vtable.read = devtmpfs_resource_read;
// device.node.vtable.write = devtmpfs_resource_write;
// device.node.vtable.mmap = devtmpfs_resource_mmap;
// device.node.vtable.msync = devtmpfs_resource_msync;
// device.node.vtable.truncate = devtmpfs_truncate;

device.node.stat.size = 0;
device.node.stat.blocks = 0;
device.node.stat.blksize = 512;
// TODO
// device.node.stat.dev = this->dev_id;
// device.node.stat.ino = this->inode_counter++;
device.node.stat.mode = mode;
device.node.stat.nlink = 1;
// TODO set in Node.init;
// resource->stat.st_atim = time_realtime;
// resource->stat.st_ctim = time_realtime;
// resource->stat.st_mtim = time_realtime;

return device;
}

fn read(node: *vfs.Node, buf: []u8, offset: std.os.off_t) vfs.ReadError!usize {
const self = @fieldParentPtr(Device, "node", node);
if (offset >= self.data.items.len) {
return 0;
}

const bytes_read = @min(buf.len, self.data.items.len - offset);
const u_offset: usize = @intCast(offset);
@memcpy(buf[0..bytes_read], self.data.items[u_offset .. u_offset + bytes_read]);
return bytes_read;
}

fn write(node: *vfs.Node, buf: []const u8, offset: std.os.off_t) vfs.WriteError!usize {
const self = @fieldParentPtr(Device, "node", node);
try self.data.insertSlice(root.allocator, @intCast(offset), buf);
return buf.len;
}

fn truncate(node: *vfs.Node, length: usize) vfs.DefaultError!void {
const self = @fieldParentPtr(Device, "node", node);
try self.data.resize(root.allocator, length);
}

// stat
// mmap
};

// TODO: not a struct just a vtable and global dev_id/inode_counter
const FileSystem = struct {
fs: *vfs.FileSystem,
dev_id: std.os.ino_t,
inode_counter: std.os.ino_t,

fn create(fs: *vfs.FileSystem, parent: *vfs.Node, name: []const u8, mode: std.os.mode_t) !*vfs.Node {
_ = mode;
_ = name;
_ = parent;
const self = @fieldParentPtr(FileSystem, "fs", fs);
_ = self;
// const new_node = try vfs.Node.init(fs, parent, name);
// return Device.init(mode);
}

fn symlink(fs: *vfs.FileSystem, parent: *vfs.Node, name: []const u8, target: []const u8) !*vfs.Node {
_ = target;
_ = name;
_ = parent;
_ = fs;
// symlink_target
// const self = @fieldParentPtr(FileSystem, "fs", fs);
// const new_node = try vfs.Node.init(fs, parent, name);
// return Device.init(0o777 | std.os.S.IFLNK);
}
};

var root_node: *vfs.Node = undefined;

// TODO
fn mount(parent: *vfs.Node, name: []const u8, source: *vfs.Node) *vfs.Node {
_ = source;
_ = name;
_ = parent;
return root_node;
}

pub fn init() void {
// const root_node = FileSystem.create({}, null, "", 0o755 | std.os.S.IFDIR) catch unreachable;
vfs.registerFileSystem("devtmpfs", mount);
}

pub fn addDevice(device: *vfs.Node, name: []const u8) !void {
_ = name;
_ = device;
// gop on devtmpfs

}
8 changes: 4 additions & 4 deletions kernel/fs/tmpfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const File = struct {
return buf.len;
}

fn stat(node: *vfs.Node, buf: *std.os.Stat) vfs.StatError!void {
fn stat(node: *vfs.Node, statbuf: *std.os.Stat) vfs.StatError!void {
const self = @fieldParentPtr(File, "node", node);
buf.* = .{
statbuf.* = .{
.dev = undefined,
.ino = node.inode,
.mode = node.mode, // TODO: 0o777 | std.os.S.IFREG,
Expand All @@ -68,7 +68,7 @@ const File = struct {
.rdev = undefined,
.size = @intCast(self.data.items.len),
.blksize = blksize,
.blocks = std.math.divCeil(usize, self.data.items.len, blksize), // TODO: use @divCeil
.blocks = std.math.divCeil(usize, self.data.items.len, 512), // TODO: use @divCeil
.atim = node.atim,
.mtim = node.mtim,
.ctim = node.ctim,
Expand Down Expand Up @@ -224,7 +224,7 @@ const FileSystem = struct {
errdefer root.allocator.destroy(symlink);

symlink.* = .{
.vtable = &.{},
// .vtable = &?,
.filesystem = fs,
.kind = .symlink,
.symlink_target = try root.allocator.dupe(u8, target),
Expand Down
44 changes: 28 additions & 16 deletions kernel/fs/zero.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const root = @import("root");
const vfs = root.vfs;

const Null = struct {
const vtable: vfs.Node.VTable = .{
.open = open,
.close = close,
.read = read,
.write = write,
};

fn open(_: *vfs.Node, _: u64) vfs.OpenError!void {}

fn close(_: *vfs.Node) void {}
Expand All @@ -19,15 +26,14 @@ const Null = struct {
const node = try root.allocator.create(vfs.Node);

node.* = .{
.vtable = &.{
.open = open,
.close = close,
.read = read,
.write = write,
},
.vtable = &vtable,
.name = try root.allocator.dupe(u8, "null"),
.uid = 0,
.gid = 0,
.stat = .{
.ino = 0,
.mode = 0o666,
.uid = 0,
.gid = 0,
},
.kind = .character_device,
.inode = 0,
};
Expand All @@ -37,6 +43,13 @@ const Null = struct {
};

const Zero = struct {
const vtable: vfs.Node.VTable = .{
.open = open,
.close = close,
.read = read,
.write = write,
};

fn open(_: *vfs.Node, _: u64) vfs.OpenError!void {}

fn close(_: *vfs.Node) void {}
Expand All @@ -54,15 +67,14 @@ const Zero = struct {
const node = try root.allocator.create(vfs.Node);

node.* = .{
.vtable = &.{
.open = open,
.close = close,
.read = read,
.write = write,
},
.vtable = &vtable,
.name = try root.allocator.dupe(u8, "zero"),
.uid = 0,
.gid = 0,
.stat = .{
.ino = 0,
.mode = 0o666,
.uid = 0,
.gid = 0,
},
.kind = .character_device,
.inode = 0,
};
Expand Down
12 changes: 8 additions & 4 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ const std = @import("std");
const builtin = @import("builtin");
const limine = @import("limine");
const ubik = @import("ubik");
const arch = @import("arch.zig");
pub const arch = @import("arch.zig");
const debug = @import("debug.zig");
const serial = @import("serial.zig");
const event = @import("event.zig");
pub const pmm = @import("pmm.zig");
pub const vmm = @import("vmm.zig");
pub const pmm = @import("mm/pmm.zig");
pub const vmm = @import("mm/vmm.zig");
const acpi = @import("acpi.zig");
pub const sched = @import("sched.zig");
pub const smp = @import("smp.zig");
pub const time = @import("time.zig");
pub const vfs = @import("vfs.zig");
const ps2 = @import("ps2.zig");
const TTY = @import("TTY.zig");
const PageAllocator = @import("mm/PageAllocator.zig");
const lib = @import("lib.zig");

pub usingnamespace lib;
Expand All @@ -28,7 +29,10 @@ pub const std_options = struct {
pub const os = struct {
pub const system = ubik;
pub const heap = struct {
pub const page_allocator = vmm.page_allocator;
pub const page_allocator = std.mem.Allocator{
.ptr = undefined,
.vtable = &PageAllocator.vtable,
};
};
};

Expand Down
44 changes: 44 additions & 0 deletions kernel/mm/PageAllocator.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const std = @import("std");
const root = @import("root");
const pmm = root.pmm;
const vmm = root.vmm;
const page_size = std.mem.page_size;
const alignForward = std.mem.alignForward;

pub const vtable = std.mem.Allocator.VTable{
.alloc = alloc,
.resize = resize,
.free = free,
};

fn alloc(_: *anyopaque, size: usize, _: u8, _: usize) ?[*]u8 {
std.debug.assert(size > 0);
std.debug.assert(size < std.math.maxInt(usize) - page_size);

const aligned_size = alignForward(usize, size, page_size);
const pages = @divExact(aligned_size, page_size);
const address = pmm.alloc(pages, false) orelse return null;
return @ptrFromInt(address + vmm.hhdm_offset);
}

fn resize(_: *anyopaque, buf: []u8, _: u8, new_size: usize, _: usize) bool {
const aligned_new_size = alignForward(usize, new_size, page_size);
const aligned_buf_len = alignForward(usize, buf.len, page_size);

if (aligned_new_size == aligned_buf_len) return true;

if (aligned_new_size < aligned_buf_len) {
const address = @intFromPtr(buf.ptr + aligned_new_size);
const pages = @divExact((aligned_buf_len - aligned_new_size), page_size);
pmm.free(address - vmm.hhdm_offset, pages);
return true;
}

return false;
}

fn free(_: *anyopaque, buf: []u8, _: u8, _: usize) void {
const aligned_buf_len = alignForward(usize, buf.len, page_size);
const pages = @divExact(aligned_buf_len, page_size);
pmm.free(@intFromPtr(buf.ptr) - vmm.hhdm_offset, pages);
}
2 changes: 1 addition & 1 deletion kernel/pmm.zig → kernel/mm/pmm.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const root = @import("root");
const vmm = @import("vmm.zig");
const vmm = root.vmm;
const SpinLock = root.SpinLock;
const log = std.log.scoped(.pmm);
const page_size = std.mem.page_size;
Expand Down
Loading

0 comments on commit f89e31a

Please sign in to comment.