diff --git a/.github/workflows/sh4_test.yml b/.github/workflows/sh4_test.yml index aa3235bd..bbfecd27 100644 --- a/.github/workflows/sh4_test.yml +++ b/.github/workflows/sh4_test.yml @@ -14,11 +14,11 @@ jobs: with: repository: SingleStepTests/sh4 path: ~/dev/sh4_json/ + - name: Move JSON test files + run: mv /home/runner/work/Deecy/Deecy/~/dev/sh4_json ../SingleStepTests_sh4/ - name: Transcode JSON test files - working-directory: ~/dev/sh4_json/ + working-directory: ../SingleStepTests_sh4/ run: python ./transcode_json.py - - name: Move JSON test files - run: find . -name "~/dev/sh4_json/*.json" -exec mv "{}" ../SingleStepTests_sh4/ \; - uses: goto-bus-stop/setup-zig@v2 with: version: 0.12.0-dev.3180+83e578a18 diff --git a/src/jit/arm_jit.zig b/src/jit/arm_jit.zig index 50b215d5..7e040b7d 100644 --- a/src/jit/arm_jit.zig +++ b/src/jit/arm_jit.zig @@ -54,7 +54,8 @@ const BlockCache = struct { pub fn deinit(self: *@This()) void { self._allocator.free(self.buffer); - std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); + + self.deallocate_blocks(); } pub fn signal_write(self: *@This(), addr: u32) void { @@ -67,17 +68,41 @@ const BlockCache = struct { } fn allocate_blocks(self: *@This()) !void { - if (@import("builtin").os.tag != .windows) { - @compileError("Unsupported OS - Use mmap on Linux here."); + switch (@import("builtin").os.tag) { + .windows => { + const blocks = try std.os.windows.VirtualAlloc( + null, + @sizeOf(?BasicBlock) * BlockEntryCount, + std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT, + std.os.windows.PAGE_READWRITE, + ); + self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + }, + .linux => { + const blocks = try std.posix.mmap( + null, + @sizeOf(?BasicBlock) * BlockEntryCount, + std.posix.PROT.READ | std.posix.PROT.WRITE | std.posix.PROT.EXEC, + .{ .TYPE = .PRIVATE, .EXECUTABLE = true }, + -1, + 0, + ); + self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + }, + else => @compileError("Unsupported OS."), } + } - const blocks = try std.os.windows.VirtualAlloc( - null, - @sizeOf(?BasicBlock) * BlockEntryCount, - std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT, - std.os.windows.PAGE_READWRITE, - ); - self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + fn deallocate_blocks(self: *@This()) void { + switch (@import("builtin").os.tag) { + .windows => { + std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); + }, + .linux => { + std.posix.munmap(@as([*]align(std.mem.page_size) const u8, @alignCast(@ptrCast(self.blocks.ptr)))[0 .. self.blocks.len * @sizeOf(?BasicBlock)]); + }, + else => @compileError("Unsupported OS."), + } } pub fn reset(self: *@This()) !void { @@ -85,7 +110,7 @@ const BlockCache = struct { self.cursor = 0; - std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); + self.deallocate_blocks(); try self.allocate_blocks(); self.min_address = std.math.maxInt(u32); diff --git a/src/jit/sh4_jit.zig b/src/jit/sh4_jit.zig index 3e35f2ea..d3f29b69 100644 --- a/src/jit/sh4_jit.zig +++ b/src/jit/sh4_jit.zig @@ -50,29 +50,52 @@ const BlockCache = struct { pub fn deinit(self: *@This()) void { self._allocator.free(self.buffer); - std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); + self.deallocate_blocks(); } fn allocate_blocks(self: *@This()) !void { - if (@import("builtin").os.tag != .windows) { - @compileError("Unsupported OS - Use mmap on Linux here."); + switch (@import("builtin").os.tag) { + .windows => { + const blocks = try std.os.windows.VirtualAlloc( + null, + @sizeOf(?BasicBlock) * BlockEntryCount, + std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT, + std.os.windows.PAGE_READWRITE, + ); + self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + }, + .linux => { + const blocks = try std.posix.mmap( + null, + @sizeOf(?BasicBlock) * BlockEntryCount, + std.posix.PROT.READ | std.posix.PROT.WRITE | std.posix.PROT.EXEC, + .{ .TYPE = .PRIVATE, .EXECUTABLE = true }, + -1, + 0, + ); + self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + }, + else => @compileError("Unsupported OS."), } + } - const blocks = try std.os.windows.VirtualAlloc( - null, - @sizeOf(?BasicBlock) * BlockEntryCount, - std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT, - std.os.windows.PAGE_READWRITE, - ); - self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount]; + fn deallocate_blocks(self: *@This()) void { + switch (@import("builtin").os.tag) { + .windows => { + // FIXME: Can I merely decommit and re-commit it? + std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); + }, + .linux => { + std.posix.munmap(@as([*]align(std.mem.page_size) const u8, @alignCast(@ptrCast(self.blocks.ptr)))[0 .. self.blocks.len * @sizeOf(?BasicBlock)]); + }, + else => @compileError("Unsupported OS."), + } } pub fn reset(self: *@This()) !void { self.cursor = 0; - // FIXME: Can I merely decommit and re-commit it? - std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE); - + self.deallocate_blocks(); try self.allocate_blocks(); }