Merge pull request #146 from SamTebbs33/bugfix/fix-integer-types

Change integer types to scale up on 64 bit architectures
This commit is contained in:
Sam Tebbs 2020-05-15 14:39:12 +01:00 committed by GitHub
commit fe0b2ffb25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 46 deletions

View file

@ -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 }; 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. /// 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. /// Assembly to write to a given port with a byte of data.

View file

@ -99,10 +99,10 @@ const TENTRY_AVAILABLE: u32 = 0xE00;
const TENTRY_PAGE_ADDR: u32 = 0xFFFFF000; const TENTRY_PAGE_ADDR: u32 = 0xFFFFF000;
/// The number of bytes in 4MB /// 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 /// 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 /// 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 }; pub var kernel_directory: Directory align(@truncate(u29, PAGE_SIZE_4KB)) = Directory{ .entries = [_]DirectoryEntry{0} ** ENTRIES_PER_DIRECTORY, .tables = [_]?*Table{null} ** ENTRIES_PER_DIRECTORY };

View file

@ -22,7 +22,7 @@ pub fn Bitmap(comptime BitmapType: type) type {
const Self = @This(); const Self = @This();
/// The number of entries that one bitmap type can hold. Evaluates to the number of bits the type has /// 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 /// The value that a full bitmap will have
pub const BITMAP_FULL = std.math.maxInt(BitmapType); pub const BITMAP_FULL = std.math.maxInt(BitmapType);
@ -30,16 +30,16 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// The type of an index into a bitmap entry. The smallest integer needed to represent all bit positions in the bitmap entry type /// 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)) } }); 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_bitmaps: usize,
num_entries: u32, num_entries: usize,
bitmaps: []BitmapType, bitmaps: []BitmapType,
num_free_entries: u32, num_free_entries: usize,
/// ///
/// Create an instance of this bitmap type. /// Create an instance of this bitmap type.
/// ///
/// Arguments: /// 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. /// 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. /// IN allocator: *std.mem.Allocator - The allocator to use when allocating the BitmapTypes required.
/// ///
@ -49,8 +49,8 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// Error: std.mem.Allocator.Error /// Error: std.mem.Allocator.Error
/// OutOfMemory: There isn't enough memory available to allocate the required number of BitmapType. /// 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 { pub fn init(num_entries: usize, allocator: *std.mem.Allocator) !Self {
const num = @floatToInt(u32, @ceil(@intToFloat(f32, num_entries) / @intToFloat(f32, ENTRIES_PER_BITMAP))); const num = std.mem.alignForward(num_entries, ENTRIES_PER_BITMAP) / ENTRIES_PER_BITMAP;
const self = Self{ const self = Self{
.num_bitmaps = num, .num_bitmaps = num,
.num_entries = num_entries, .num_entries = num_entries,
@ -68,12 +68,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Arguments: /// Arguments:
/// INOUT self: *Self - The bitmap to modify. /// 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. /// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds. /// 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 (idx >= self.num_entries) return BitmapError.OutOfBounds;
if (!try self.isSet(idx)) { if (!try self.isSet(idx)) {
const bit = self.indexToBit(idx); const bit = self.indexToBit(idx);
@ -87,12 +87,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Arguments: /// Arguments:
/// INOUT self: *Self - The bitmap to modify. /// 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. /// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds. /// 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 (idx >= self.num_entries) return BitmapError.OutOfBounds;
if (try self.isSet(idx)) { if (try self.isSet(idx)) {
const bit = self.indexToBit(idx); const bit = self.indexToBit(idx);
@ -106,12 +106,12 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Arguments: /// Arguments:
/// IN self: *const Self - The bitmap to use. /// 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. /// Return: BitmapType.
/// The bit corresponding to that index but within a single 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); return @as(BitmapType, 1) << @intCast(IndexType, idx % ENTRIES_PER_BITMAP);
} }
@ -120,26 +120,26 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Arguments: /// Arguments:
/// INOUT self: *Self - The bitmap to modify. /// 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. /// 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) { if (num > self.num_free_entries) {
return null; return null;
} }
var count: u32 = 0; var count: usize = 0;
var start: ?u32 = null; var start: ?usize = null;
for (self.bitmaps) |bmp, i| { for (self.bitmaps) |bmp, i| {
var bit: IndexType = 0; var bit: IndexType = 0;
while (true) { while (true) {
const entry = bit + @intCast(u32, i * ENTRIES_PER_BITMAP); const entry = bit + i * ENTRIES_PER_BITMAP;
if (entry >= self.num_entries) { if (entry >= self.num_entries) {
return null; return null;
} }
if ((bmp & @as(u32, 1) << bit) != 0) { if ((bmp & @as(BitmapType, 1) << bit) != 0) {
// This is a one so clear the progress // This is a one so clear the progress
count = 0; count = 0;
start = null; start = null;
@ -170,7 +170,7 @@ pub fn Bitmap(comptime BitmapType: type) type {
if (count == num) { if (count == num) {
if (start) |start_entry| { if (start) |start_entry| {
var i: u32 = 0; var i: usize = 0;
while (i < num) : (i += 1) { while (i < num) : (i += 1) {
// Can't fail as the entry was found to be free // Can't fail as the entry was found to be free
self.setEntry(start_entry + i) catch unreachable; self.setEntry(start_entry + i) catch unreachable;
@ -184,17 +184,17 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Set the first free entry within the bitmaps as occupied. /// 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. /// 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. /// 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; if (self.num_free_entries == 0) return null;
for (self.bitmaps) |*bmp, i| { for (self.bitmaps) |*bmp, i| {
if (bmp.* == BITMAP_FULL) if (bmp.* == BITMAP_FULL)
continue; continue;
const bit = @truncate(IndexType, @ctz(BitmapType, ~bmp.*)); 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 // Failing here means that the index is outside of the bitmap, so there are no free entries
self.setEntry(idx) catch return null; self.setEntry(idx) catch return null;
return idx; return idx;
@ -207,7 +207,7 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// ///
/// Arguments: /// Arguments:
/// IN self: *const Self - The bitmap to check. /// IN self: *const Self - The bitmap to check.
/// IN idx: u32 - The entry to check. /// IN idx: usize - The entry to check.
/// ///
/// Return: bool. /// Return: bool.
/// True if the entry is set, else false. /// True if the entry is set, else false.
@ -215,7 +215,7 @@ pub fn Bitmap(comptime BitmapType: type) type {
/// Error: BitmapError. /// Error: BitmapError.
/// OutOfBounds: The index given is out of bounds. /// 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; if (idx >= self.num_entries) return BitmapError.OutOfBounds;
return (self.bitmaps[idx / ENTRIES_PER_BITMAP] & self.indexToBit(idx)) != 0; return (self.bitmaps[idx / ENTRIES_PER_BITMAP] & self.indexToBit(idx)) != 0;
} }

View file

@ -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; 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 // 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)) { 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| { 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}); panic_root.panic(@errorReturnTrace(), "Failed to initialise kernel heap: {}\n", .{e});

View file

@ -17,10 +17,10 @@ pub const MemProfile = struct {
physaddr_start: [*]u8, physaddr_start: [*]u8,
/// The amount of memory in the system, in kilobytes. /// 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. /// 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. /// The boot modules provided by the bootloader.
boot_modules: []multiboot.multiboot_module_t, boot_modules: []multiboot.multiboot_module_t,
@ -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 // Total memory available including the initial 1MiB that grub doesn't include
.mem_kb = mb_info.mem_upper + mb_info.mem_lower + 1024, .mem_kb = mb_info.mem_upper + mb_info.mem_lower + 1024,
.fixed_alloc_size = FIXED_ALLOC_SIZE, .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], .mem_map = @intToPtr([*]multiboot.multiboot_memory_map_t, mmap_addr)[0..num_mmap_entries],
}; };
} }

View file

@ -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 // Exit if we haven't loaded all debug modules
if (mem_profile.boot_modules.len < 1) if (mem_profile.boot_modules.len < 1)
return; return;
var kmap_start: u32 = 0; var kmap_start: usize = 0;
var kmap_end: u32 = 0; var kmap_end: usize = 0;
for (mem_profile.boot_modules) |module| { for (mem_profile.boot_modules) |module| {
const mod_start = mem.physToVirt(module.mod_start); const mod_start = mem.physToVirt(@intCast(usize, module.mod_start));
const mod_end = mem.physToVirt(module.mod_end) - 1; const mod_end = mem.physToVirt(@intCast(usize, module.mod_end) - 1);
const mod_str_ptr = mem.physToVirt(@intToPtr([*:0]u8, module.cmdline)); const mod_str_ptr = mem.physToVirt(@intToPtr([*:0]u8, module.cmdline));
if (std.mem.eql(u8, std.mem.span(mod_str_ptr), "kernel.map")) { if (std.mem.eql(u8, std.mem.span(mod_str_ptr), "kernel.map")) {
kmap_start = mod_start; kmap_start = mod_start;

View file

@ -19,7 +19,7 @@ const PmmError = error{
}; };
/// The size of memory associated with each bitmap entry /// 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; var bitmap: PmmBitmap = undefined;
@ -85,10 +85,10 @@ pub fn free(addr: usize) (PmmBitmap.BitmapError || PmmError)!void {
/// ///
/// Get the number of unallocated blocks of memory. /// Get the number of unallocated blocks of memory.
/// ///
/// Return: u32. /// Return: usize.
/// The number of unallocated blocks of memory /// The number of unallocated blocks of memory
/// ///
pub fn blocksFree() u32 { pub fn blocksFree() usize {
return bitmap.num_free_entries; return bitmap.num_free_entries;
} }

View file

@ -31,7 +31,7 @@ const Allocation = struct {
}; };
/// The size of each allocatable block, the same as the physical memory manager's block size /// 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{ pub const MapperError = error{
InvalidVirtualAddress, InvalidVirtualAddress,
@ -121,7 +121,7 @@ pub const VmmError = error{
pub fn VirtualMemoryManager(comptime Payload: type) type { pub fn VirtualMemoryManager(comptime Payload: type) type {
return struct { return struct {
/// The bitmap that keeps track of allocated and free regions /// 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 /// The start of the memory to be tracked
start: usize, start: usize,
@ -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 { 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; 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{ return Self{
.bmp = bmp, .bmp = bmp,
.start = start, .start = start,
@ -250,7 +250,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
/// ///
/// Arguments: /// Arguments:
/// INOUT self: *Self - The manager to allocate for /// 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 /// IN attrs: Attributes - The attributes to apply to the mapped memory
/// ///
/// Return: ?usize /// Return: ?usize
@ -259,7 +259,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
/// Error: std.mem.Allocator.Error /// Error: std.mem.Allocator.Error
/// std.mem.AllocatorError.OutOfMemory: The required amount of memory couldn't be allocated /// 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) if (num == 0)
return null; return null;
// Ensure that there is both enough physical and virtual address space free // Ensure that there is both enough physical and virtual address space free
@ -269,7 +269,7 @@ pub fn VirtualMemoryManager(comptime Payload: type) type {
var block_list = std.ArrayList(usize).init(self.allocator); var block_list = std.ArrayList(usize).init(self.allocator);
try block_list.ensureCapacity(num); try block_list.ensureCapacity(num);
var i: u32 = 0; var i: usize = 0;
const vaddr_start = self.start + entry * BLOCK_SIZE; const vaddr_start = self.start + entry * BLOCK_SIZE;
var vaddr = vaddr_start; var vaddr = vaddr_start;
// Map the blocks to physical memory // Map the blocks to physical memory