Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/kernel/arch/x86/arch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub const KERNEL_VMM_PAYLOAD = &paging.kernel_directory;
pub const VMM_MAPPER: vmm.Mapper(VmmPayload) = vmm.Mapper(VmmPayload){ .mapFn = paging.map, .unmapFn = paging.unmap };

/// The size of each allocatable block of memory, normally set to the page size.
pub const MEMORY_BLOCK_SIZE = paging.PAGE_SIZE_4KB;
pub const MEMORY_BLOCK_SIZE: usize = paging.PAGE_SIZE_4KB;

///
/// Assembly to write to a given port with a byte of data.
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/arch/x86/paging.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ const TENTRY_AVAILABLE: u32 = 0xE00;
const TENTRY_PAGE_ADDR: u32 = 0xFFFFF000;

/// The number of bytes in 4MB
pub const PAGE_SIZE_4MB: u32 = 0x400000;
pub const PAGE_SIZE_4MB: usize = 0x400000;

/// The number of bytes in 4KB
pub const PAGE_SIZE_4KB: u32 = PAGE_SIZE_4MB / 1024;
pub const PAGE_SIZE_4KB: usize = PAGE_SIZE_4MB / 1024;

/// The kernel's page directory. Should only be used to map kernel-owned code and data
pub var kernel_directory: Directory align(@truncate(u29, PAGE_SIZE_4KB)) = Directory{ .entries = [_]DirectoryEntry{0} ** ENTRIES_PER_DIRECTORY, .tables = [_]?*Table{null} ** ENTRIES_PER_DIRECTORY };
Expand Down
52 changes: 26 additions & 26 deletions src/kernel/bitmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ pub fn Bitmap(comptime BitmapType: type) type {
const Self = @This();

/// The number of entries that one bitmap type can hold. Evaluates to the number of bits the type has
pub const ENTRIES_PER_BITMAP: u32 = std.meta.bitCount(BitmapType);
pub const ENTRIES_PER_BITMAP: usize = std.meta.bitCount(BitmapType);

/// The value that a full bitmap will have
pub const BITMAP_FULL = std.math.maxInt(BitmapType);

/// The type of an index into a bitmap entry. The smallest integer needed to represent all bit positions in the bitmap entry type
pub const IndexType = @Type(builtin.TypeInfo{ .Int = builtin.TypeInfo.Int{ .is_signed = false, .bits = std.math.log2(std.meta.bitCount(BitmapType)) } });

num_bitmaps: u32,
num_entries: u32,
num_bitmaps: usize,
num_entries: usize,
bitmaps: []BitmapType,
num_free_entries: u32,
num_free_entries: usize,

///
/// Create an instance of this bitmap type.
///
/// Arguments:
/// IN num_entries: u32 - The number of entries that the bitmap created will have.
/// IN num_entries: usize - The number of entries that the bitmap created will have.
/// The number of BitmapType required to store this many entries will be allocated and each will be zeroed.
/// IN allocator: *std.mem.Allocator - The allocator to use when allocating the BitmapTypes required.
///
Expand All @@ -49,8 +49,8 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// Error: std.mem.Allocator.Error
/// OutOfMemory: There isn't enough memory available to allocate the required number of BitmapType.
///
pub fn init(num_entries: u32, allocator: *std.mem.Allocator) !Self {
const num = @floatToInt(u32, @ceil(@intToFloat(f32, num_entries) / @intToFloat(f32, ENTRIES_PER_BITMAP)));
pub fn init(num_entries: usize, allocator: *std.mem.Allocator) !Self {
const num = std.mem.alignForward(num_entries, ENTRIES_PER_BITMAP) / ENTRIES_PER_BITMAP;
const self = Self{
.num_bitmaps = num,
.num_entries = num_entries,
Expand All @@ -68,12 +68,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Arguments:
/// INOUT self: *Self - The bitmap to modify.
/// IN idx: BitmapIndex - The index within the bitmap to set.
/// IN idx: usize - The index within the bitmap to set.
///
/// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds.
///
pub fn setEntry(self: *Self, idx: u32) BitmapError!void {
pub fn setEntry(self: *Self, idx: usize) BitmapError!void {
if (idx >= self.num_entries) return BitmapError.OutOfBounds;
if (!try self.isSet(idx)) {
const bit = self.indexToBit(idx);
Expand All @@ -87,12 +87,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Arguments:
/// INOUT self: *Self - The bitmap to modify.
/// IN idx: BitmapIndex - The index within the bitmap to clear.
/// IN idx: usize - The index within the bitmap to clear.
///
/// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds.
///
pub fn clearEntry(self: *Self, idx: u32) BitmapError!void {
pub fn clearEntry(self: *Self, idx: usize) BitmapError!void {
if (idx >= self.num_entries) return BitmapError.OutOfBounds;
if (try self.isSet(idx)) {
const bit = self.indexToBit(idx);
Expand All @@ -106,12 +106,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Arguments:
/// IN self: *const Self - The bitmap to use.
/// IN idx: u32 - The index into all of the bitmap's entries.
/// IN idx: usize - The index into all of the bitmap's entries.
///
/// Return: BitmapType.
/// The bit corresponding to that index but within a single BitmapType.
///
fn indexToBit(self: *const Self, idx: u32) BitmapType {
fn indexToBit(self: *const Self, idx: usize) BitmapType {
return @as(BitmapType, 1) << @intCast(IndexType, idx % ENTRIES_PER_BITMAP);
}

Expand All @@ -120,26 +120,26 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Arguments:
/// INOUT self: *Self - The bitmap to modify.
/// IN num: u32 - The number of entries to set.
/// IN num: usize - The number of entries to set.
///
/// Return: ?u32
/// Return: ?usize
/// The first entry set or null if there weren't enough contiguous entries.
///
pub fn setContiguous(self: *Self, num: u32) ?u32 {
pub fn setContiguous(self: *Self, num: usize) ?usize {
if (num > self.num_free_entries) {
return null;
}

var count: u32 = 0;
var start: ?u32 = null;
var count: usize = 0;
var start: ?usize = null;
for (self.bitmaps) |bmp, i| {
var bit: IndexType = 0;
while (true) {
const entry = bit + @intCast(u32, i * ENTRIES_PER_BITMAP);
const entry = bit + i * ENTRIES_PER_BITMAP;
if (entry >= self.num_entries) {
return null;
}
if ((bmp & @as(u32, 1) << bit) != 0) {
if ((bmp & @as(BitmapType, 1) << bit) != 0) {
// This is a one so clear the progress
count = 0;
start = null;
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn Bitmap(comptime BitmapType: type) type {

if (count == num) {
if (start) |start_entry| {
var i: u32 = 0;
var i: usize = 0;
while (i < num) : (i += 1) {
// Can't fail as the entry was found to be free
self.setEntry(start_entry + i) catch unreachable;
Expand All @@ -184,17 +184,17 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Set the first free entry within the bitmaps as occupied.
///
/// Return: ?u32.
/// Return: ?usize.
/// The index within all bitmaps that was set or null if there wasn't one free.
/// 0 .. ENTRIES_PER_BITMAP - 1 if in the first bitmap, ENTRIES_PER_BITMAP .. ENTRIES_PER_BITMAP * 2 - 1 if in the second etc.
///
pub fn setFirstFree(self: *Self) ?u32 {
pub fn setFirstFree(self: *Self) ?usize {
if (self.num_free_entries == 0) return null;
for (self.bitmaps) |*bmp, i| {
if (bmp.* == BITMAP_FULL)
continue;
const bit = @truncate(IndexType, @ctz(BitmapType, ~bmp.*));
const idx = bit + @intCast(u32, i) * ENTRIES_PER_BITMAP;
const idx = bit + i * ENTRIES_PER_BITMAP;
// Failing here means that the index is outside of the bitmap, so there are no free entries
self.setEntry(idx) catch return null;
return idx;
Expand All @@ -207,15 +207,15 @@ pub fn Bitmap(comptime BitmapType: type) type {
///
/// Arguments:
/// IN self: *const Self - The bitmap to check.
/// IN idx: u32 - The entry to check.
/// IN idx: usize - The entry to check.
///
/// Return: bool.
/// True if the entry is set, else false.
///
/// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds.
///
pub fn isSet(self: *const Self, idx: u32) BitmapError!bool {
pub fn isSet(self: *const Self, idx: usize) BitmapError!bool {
if (idx >= self.num_entries) return BitmapError.OutOfBounds;
return (self.bitmaps[idx / ENTRIES_PER_BITMAP] & self.indexToBit(idx)) != 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/kmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export fn kmain(mb_info: *multiboot.multiboot_info_t, mb_magic: u32) void {
var heap_size = mem_profile.mem_kb / 10 * 1024;
// The heap size must be a power of two so find the power of two smaller than or equal to the heap_size
if (!std.math.isPowerOfTwo(heap_size)) {
heap_size = std.math.floorPowerOfTwo(u32, heap_size);
heap_size = std.math.floorPowerOfTwo(usize, heap_size);
}
var kernel_heap = heap.init(arch.VmmPayload, &kernel_vmm, vmm.Attributes{ .kernel = true, .writable = true, .cachable = true }, heap_size, &fixed_allocator.allocator) catch |e| {
panic_root.panic(@errorReturnTrace(), "Failed to initialise kernel heap: {}\n", .{e});
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub const MemProfile = struct {
physaddr_start: [*]u8,

/// The amount of memory in the system, in kilobytes.
mem_kb: u32,
mem_kb: usize,

/// The size of the fixed buffer allocator used as the first memory allocator.
fixed_alloc_size: u32,
fixed_alloc_size: usize,

/// The boot modules provided by the bootloader.
boot_modules: []multiboot.multiboot_module_t,
Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn init(mb_info: *multiboot.multiboot_info_t) MemProfile {
// Total memory available including the initial 1MiB that grub doesn't include
.mem_kb = mb_info.mem_upper + mb_info.mem_lower + 1024,
.fixed_alloc_size = FIXED_ALLOC_SIZE,
.boot_modules = @intToPtr([*]multiboot.multiboot_mod_list, physToVirt(mb_info.mods_addr))[0..mods_count],
.boot_modules = @intToPtr([*]multiboot.multiboot_mod_list, physToVirt(@intCast(usize, mb_info.mods_addr)))[0..mods_count],
.mem_map = @intToPtr([*]multiboot.multiboot_memory_map_t, mmap_addr)[0..num_mmap_entries],
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/panic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ pub fn init(mem_profile: *const mem.MemProfile, allocator: *std.mem.Allocator) !
// Exit if we haven't loaded all debug modules
if (mem_profile.boot_modules.len < 1)
return;
var kmap_start: u32 = 0;
var kmap_end: u32 = 0;
var kmap_start: usize = 0;
var kmap_end: usize = 0;
for (mem_profile.boot_modules) |module| {
const mod_start = mem.physToVirt(module.mod_start);
const mod_end = mem.physToVirt(module.mod_end) - 1;
const mod_start = mem.physToVirt(@intCast(usize, module.mod_start));
const mod_end = mem.physToVirt(@intCast(usize, module.mod_end) - 1);
const mod_str_ptr = mem.physToVirt(@intToPtr([*:0]u8, module.cmdline));
if (std.mem.eql(u8, std.mem.span(mod_str_ptr), "kernel.map")) {
kmap_start = mod_start;
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/pmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const PmmError = error{
};

/// The size of memory associated with each bitmap entry
pub const BLOCK_SIZE = arch.MEMORY_BLOCK_SIZE;
pub const BLOCK_SIZE: usize = arch.MEMORY_BLOCK_SIZE;

var bitmap: PmmBitmap = undefined;

Expand Down Expand Up @@ -85,10 +85,10 @@ pub fn free(addr: usize) (PmmBitmap.BitmapError || PmmError)!void {
///
/// Get the number of unallocated blocks of memory.
///
/// Return: u32.
/// Return: usize.
/// The number of unallocated blocks of memory
///
pub fn blocksFree() u32 {
pub fn blocksFree() usize {
return bitmap.num_free_entries;
}

Expand Down
12 changes: 6 additions & 6 deletions src/kernel/vmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Allocation = struct {
};

/// The size of each allocatable block, the same as the physical memory manager's block size
pub const BLOCK_SIZE: u32 = pmm.BLOCK_SIZE;
pub const BLOCK_SIZE: usize = pmm.BLOCK_SIZE;

pub const MapperError = error{
InvalidVirtualAddress,
Expand Down Expand Up @@ -121,7 +121,7 @@ pub const VmmError = error{
pub fn VirtualMemoryManager(comptime Payload: type) type {
return struct {
/// The bitmap that keeps track of allocated and free regions
bmp: bitmap.Bitmap(u32),
bmp: bitmap.Bitmap(usize),

/// The start of the memory to be tracked
start: usize,
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
///
pub fn init(start: usize, end: usize, allocator: *std.mem.Allocator, mapper: Mapper(Payload), payload: Payload) std.mem.Allocator.Error!Self {
const size = end - start;
var bmp = try bitmap.Bitmap(u32).init(@floatToInt(u32, @ceil(@intToFloat(f32, size) / @intToFloat(f32, pmm.BLOCK_SIZE))), allocator);
var bmp = try bitmap.Bitmap(usize).init(std.mem.alignForward(size, pmm.BLOCK_SIZE) / pmm.BLOCK_SIZE, allocator);
return Self{
.bmp = bmp,
.start = start,
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
///
/// Arguments:
/// INOUT self: *Self - The manager to allocate for
/// IN num: u32 - The number of blocks to allocate
/// IN num: usize - The number of blocks to allocate
/// IN attrs: Attributes - The attributes to apply to the mapped memory
///
/// Return: ?usize
Expand All @@ -259,7 +259,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
/// Error: std.mem.Allocator.Error
/// std.mem.AllocatorError.OutOfMemory: The required amount of memory couldn't be allocated
///
pub fn alloc(self: *Self, num: u32, attrs: Attributes) std.mem.Allocator.Error!?usize {
pub fn alloc(self: *Self, num: usize, attrs: Attributes) std.mem.Allocator.Error!?usize {
if (num == 0)
return null;
// Ensure that there is both enough physical and virtual address space free
Expand All @@ -269,7 +269,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
var block_list = std.ArrayList(usize).init(self.allocator);
try block_list.ensureCapacity(num);

var i: u32 = 0;
var i: usize = 0;
const vaddr_start = self.start + entry * BLOCK_SIZE;
var vaddr = vaddr_start;
// Map the blocks to physical memory
Expand Down