Compare commits
6 Commits
main
...
08bf0f9201
Author | SHA1 | Date | |
---|---|---|---|
08bf0f9201 | |||
6467fc25e7 | |||
732c00efb2 | |||
166baeec1a | |||
835878ca40 | |||
278a5adf25 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,2 @@
|
|||||||
zig-out/
|
zig-out/
|
||||||
zig-cache/
|
zig-cache/
|
||||||
.zig-cache/
|
|
||||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "lib/zig-network"]
|
||||||
|
path = lib/zig-network
|
||||||
|
url = https://github.com/MasterQ32/zig-network
|
88
build.zig
88
build.zig
@@ -1,35 +1,59 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Although this function looks imperative, note that its job is to
|
fn path(comptime suffix: []const u8) []const u8 {
|
||||||
// declaratively construct a build graph that will be executed by an external
|
if (suffix[0] == '/') @compileError("expected a relative path");
|
||||||
// runner.
|
return comptime (std.fs.path.dirname(@src().file) orelse ".") ++ std.fs.path.sep_str ++ suffix;
|
||||||
pub fn build(b: *std.Build) void {
|
}
|
||||||
// Standard target options allows the person running `zig build` to choose
|
|
||||||
// what target to build for. Here we do not override the defaults, which
|
const pkgs = struct {
|
||||||
// means any target is allowed, and the default is native. Other options
|
const Pkg = std.build.Pkg;
|
||||||
// for restricting supported target set are available.
|
|
||||||
const target = b.standardTargetOptions(.{});
|
pub const gdbstub: Pkg = .{
|
||||||
|
.name = "gdbstub",
|
||||||
// Standard optimization options allow the person running `zig build` to select
|
.source = .{ .path = path("src/lib.zig") },
|
||||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
.dependencies = &[_]Pkg{network},
|
||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
};
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
|
||||||
|
// https://github.com/MasterQ32/zig-network
|
||||||
_ = b.addModule("zba-gdbstub", .{ .root_source_file = b.path("src/lib.zig") });
|
pub const network: Pkg = .{
|
||||||
|
.name = "network",
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
.source = .{ .path = path("lib/zig-network/network.zig") },
|
||||||
// but does not run it.
|
};
|
||||||
const lib_unit_tests = b.addTest(.{
|
};
|
||||||
.root_source_file = b.path("src/lib.zig"),
|
|
||||||
.target = target,
|
pub fn link(exe: *std.build.LibExeObjStep) void {
|
||||||
.optimize = optimize,
|
exe.addPackage(pkgs.gdbstub);
|
||||||
});
|
}
|
||||||
|
|
||||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
pub fn build(b: *std.build.Builder) void {
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
// Similar to creating the run step earlier, this exposes a `test` step to
|
const mode = b.standardReleaseOptions();
|
||||||
// the `zig build --help` menu, providing a way for the user to request
|
|
||||||
// running the unit tests.
|
// -- library --
|
||||||
const test_step = b.step("test", "Run unit tests");
|
const lib = b.addStaticLibrary("gdbstub", "src/lib.zig");
|
||||||
test_step.dependOn(&run_lib_unit_tests.step);
|
lib.addPackage(pkgs.network);
|
||||||
|
|
||||||
|
lib.setBuildMode(mode);
|
||||||
|
lib.install();
|
||||||
|
|
||||||
|
const lib_tests = b.addTest("src/lib.zig");
|
||||||
|
lib_tests.setBuildMode(mode);
|
||||||
|
|
||||||
|
const test_step = b.step("lib-test", "Run Library Tests");
|
||||||
|
test_step.dependOn(&lib_tests.step);
|
||||||
|
|
||||||
|
// -- Executable --
|
||||||
|
const exe = b.addExecutable("gdbserver", "src/main.zig");
|
||||||
|
link(exe);
|
||||||
|
|
||||||
|
exe.setTarget(target);
|
||||||
|
exe.setBuildMode(mode);
|
||||||
|
exe.install();
|
||||||
|
|
||||||
|
const run_cmd = exe.run();
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
if (b.args) |args| run_cmd.addArgs(args);
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
@@ -1,72 +0,0 @@
|
|||||||
.{
|
|
||||||
// This is the default name used by packages depending on this one. For
|
|
||||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
|
||||||
// as the key in the `dependencies` table. Although the user can choose a
|
|
||||||
// different name, most users will stick with this provided value.
|
|
||||||
//
|
|
||||||
// It is redundant to include "zig" in this name because it is already
|
|
||||||
// within the Zig package namespace.
|
|
||||||
.name = "zba-gdbstub",
|
|
||||||
|
|
||||||
// This is a [Semantic Version](https://semver.org/).
|
|
||||||
// In a future version of Zig it will be used for package deduplication.
|
|
||||||
.version = "0.1.0",
|
|
||||||
|
|
||||||
// This field is optional.
|
|
||||||
// This is currently advisory only; Zig does not yet do anything
|
|
||||||
// with this value.
|
|
||||||
//.minimum_zig_version = "0.11.0",
|
|
||||||
|
|
||||||
// This field is optional.
|
|
||||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
|
||||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
|
||||||
// Once all dependencies are fetched, `zig build` no longer requires
|
|
||||||
// internet connectivity.
|
|
||||||
.dependencies = .{
|
|
||||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
|
||||||
//.example = .{
|
|
||||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
|
||||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
|
||||||
// // the new URL.
|
|
||||||
// .url = "https://example.com/foo.tar.gz",
|
|
||||||
//
|
|
||||||
// // This is computed from the file contents of the directory of files that is
|
|
||||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
|
||||||
// // `paths`.
|
|
||||||
// //
|
|
||||||
// // This field is the source of truth; packages do not come from a `url`; they
|
|
||||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
|
||||||
// // obtain a package matching this `hash`.
|
|
||||||
// //
|
|
||||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
|
||||||
// .hash = "...",
|
|
||||||
//
|
|
||||||
// // When this is provided, the package is found in a directory relative to the
|
|
||||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
|
||||||
// // computed. This field and `url` are mutually exclusive.
|
|
||||||
// .path = "foo",
|
|
||||||
|
|
||||||
// // When this is set to `true`, a package is declared to be lazily
|
|
||||||
// // fetched. This makes the dependency only get fetched if it is
|
|
||||||
// // actually used.
|
|
||||||
// .lazy = false,
|
|
||||||
//},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Specifies the set of files and directories that are included in this package.
|
|
||||||
// Only files and directories listed here are included in the `hash` that
|
|
||||||
// is computed for this package. Only files listed here will remain on disk
|
|
||||||
// when using the zig package manager. As a rule of thumb, one should list
|
|
||||||
// files required for compilation plus any license(s).
|
|
||||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
|
||||||
// the build root itself.
|
|
||||||
// A directory listed here means that all files within, recursively, are included.
|
|
||||||
.paths = .{
|
|
||||||
"build.zig",
|
|
||||||
"build.zig.zon",
|
|
||||||
"src",
|
|
||||||
// For example...
|
|
||||||
//"LICENSE",
|
|
||||||
//"README.md",
|
|
||||||
},
|
|
||||||
}
|
|
1
lib/zig-network
Submodule
1
lib/zig-network
Submodule
Submodule lib/zig-network added at caa31ef878
230
src/Packet.zig
230
src/Packet.zig
@@ -1,9 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
const target = @import("Server.zig").target;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Emulator = @import("lib.zig").Emulator;
|
|
||||||
const State = @import("State.zig");
|
|
||||||
const Server = @import("Server.zig");
|
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const log = std.log.scoped(.Packet);
|
const log = std.log.scoped(.Packet);
|
||||||
@@ -18,6 +16,8 @@ pub fn from(allocator: Allocator, str: []const u8) !Self {
|
|||||||
const chksum_str = tokens.next() orelse return error.MissingCheckSum;
|
const chksum_str = tokens.next() orelse return error.MissingCheckSum;
|
||||||
const chksum = std.fmt.parseInt(u8, chksum_str, 16) catch return error.InvalidChecksum;
|
const chksum = std.fmt.parseInt(u8, chksum_str, 16) catch return error.InvalidChecksum;
|
||||||
|
|
||||||
|
// log.info("Contents: {s}", .{contents});
|
||||||
|
|
||||||
if (!Self.verify(contents, chksum)) return error.ChecksumMismatch;
|
if (!Self.verify(contents, chksum)) return error.ChecksumMismatch;
|
||||||
|
|
||||||
return .{ .contents = try allocator.dupe(u8, contents) };
|
return .{ .contents = try allocator.dupe(u8, contents) };
|
||||||
@@ -44,26 +44,34 @@ const String = union(enum) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emulator) !String {
|
pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||||
switch (self.contents[0]) {
|
switch (self.contents[0]) {
|
||||||
// Required
|
// Required
|
||||||
'?' => return .{ .static = "T05" }, // FIXME: which errno?
|
'?' => {
|
||||||
'g' => {
|
const ret: Signal = .Trap;
|
||||||
const r = emu.registers();
|
|
||||||
const cpsr = emu.cpsr();
|
// Deallocated by the caller
|
||||||
|
return .{ .alloc = try std.fmt.allocPrint(allocator, "T{x:0>2}thread:1;", .{@enumToInt(ret)}) };
|
||||||
|
},
|
||||||
|
'g' => {
|
||||||
|
// TODO: Actually reference GBA Registers
|
||||||
|
const r = [_]u32{0xDEAD_BEEF} ** 0x10;
|
||||||
|
const cpsr: u32 = 0xCAFE_B0BA;
|
||||||
|
|
||||||
|
const char_len = 2;
|
||||||
|
const reg_len = @sizeOf(u32) * char_len; // Every byte is represented by 2 characters
|
||||||
|
|
||||||
const reg_len = @sizeOf(u32) * 2; // Every byte is represented by 2 characters
|
|
||||||
const ret = try allocator.alloc(u8, r.len * reg_len + reg_len); // r0 -> r15 + CPSR
|
const ret = try allocator.alloc(u8, r.len * reg_len + reg_len); // r0 -> r15 + CPSR
|
||||||
|
|
||||||
{
|
{
|
||||||
var i: u32 = 0;
|
var i: usize = 0;
|
||||||
while (i < r.len + 1) : (i += 1) {
|
while (i < r.len + 1) : (i += 1) {
|
||||||
var reg: u32 = if (i < r.len) r[i] else cpsr;
|
const reg: u32 = if (i < r.len) r[i] else cpsr;
|
||||||
if (i == 15) reg -|= if (cpsr >> 5 & 1 == 1) 4 else 8; // PC is ahead
|
|
||||||
|
|
||||||
// writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that
|
// bufPrintIntToSlice writes to the provided slice, which is all we want from this
|
||||||
// GDB also expects the bytes to be in the opposite order for whatever reason
|
// consequentially, we ignore the slice it returns since it just references the slice
|
||||||
_ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], @byteSwap(reg), 16, .lower, .{ .fill = '0', .width = 8 });
|
// passed as an argument
|
||||||
|
_ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], reg, 16, .lower, .{ .fill = '0', .width = 8 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,221 +79,101 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
|||||||
},
|
},
|
||||||
'G' => @panic("TODO: Register Write"),
|
'G' => @panic("TODO: Register Write"),
|
||||||
'm' => {
|
'm' => {
|
||||||
|
// TODO: Actually reference GBA Memory
|
||||||
|
log.err("{s}", .{self.contents});
|
||||||
|
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ",");
|
var tokens = std.mem.tokenize(u8, self.contents[1..], ",");
|
||||||
const addr_str = tokens.next() orelse return error.InvalidPacket;
|
const addr_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
|
||||||
const length_str = tokens.next() orelse return error.InvalidPacket;
|
const length_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
|
||||||
|
|
||||||
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
||||||
const len = try std.fmt.parseInt(u32, length_str, 16);
|
const len = try std.fmt.parseInt(u32, length_str, 16);
|
||||||
|
_ = addr;
|
||||||
|
|
||||||
const ret = try allocator.alloc(u8, len * 2);
|
const ret = try allocator.alloc(u8, len * 2);
|
||||||
|
|
||||||
{
|
{
|
||||||
var i: u32 = 0;
|
var i: usize = 0;
|
||||||
while (i < len) : (i += 1) {
|
while (i < len) : (i += 1) {
|
||||||
// writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that
|
const value: u8 = 0;
|
||||||
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], emu.read(addr + i), 16, .lower, .{ .fill = '0', .width = 2 });
|
|
||||||
|
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], value, 16, .lower, .{ .fill = '0', .width = 2 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{ .alloc = ret };
|
return .{ .alloc = ret };
|
||||||
},
|
},
|
||||||
'M' => {
|
'M' => @panic("TODO: Memory Write"),
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ",:");
|
'c' => @panic("TODO: Continue"),
|
||||||
|
's' => @panic("TODO: Step"),
|
||||||
|
|
||||||
const addr_str = tokens.next() orelse return error.InvalidPacket;
|
// Optional
|
||||||
const length_str = tokens.next() orelse return error.InvalidPacket;
|
'H' => {
|
||||||
const bytes = tokens.next() orelse return error.InvalidPacket;
|
log.warn("{s}", .{self.contents});
|
||||||
|
|
||||||
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
|
||||||
const len = try std.fmt.parseInt(u32, length_str, 16);
|
|
||||||
|
|
||||||
{
|
|
||||||
var i: u32 = 0;
|
|
||||||
while (i < len) : (i += 1) {
|
|
||||||
const str = bytes[2 * i ..][0..2];
|
|
||||||
const value = try std.fmt.parseInt(u8, str, 16);
|
|
||||||
|
|
||||||
emu.write(addr + i, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return .{ .static = "OK" };
|
|
||||||
},
|
|
||||||
'c' => {
|
|
||||||
switch (emu.contd()) {
|
|
||||||
.SingleStep => unreachable,
|
|
||||||
.Trap => |r| switch (r) {
|
|
||||||
.HwBkpt => return .{ .static = "T05 hwbreak:;" },
|
|
||||||
.SwBkpt => return .{ .static = "T05 swbreak:;" },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
's' => {
|
|
||||||
// var tokens = std.mem.tokenize(u8, self.contents[1..], " ");
|
|
||||||
// const addr = if (tokens.next()) |s| try std.fmt.parseInt(u32, s, 16) else null;
|
|
||||||
|
|
||||||
switch (emu.step()) {
|
|
||||||
.SingleStep => return .{ .static = "T05" },
|
|
||||||
.Trap => |r| switch (r) {
|
|
||||||
.HwBkpt => return .{ .static = "T05 hwbreak:;" },
|
|
||||||
.SwBkpt => return .{ .static = "T05 swbreak:;" },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// Breakpoints
|
|
||||||
'z' => {
|
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[2..], ",");
|
|
||||||
|
|
||||||
const addr_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
|
||||||
|
|
||||||
switch (self.contents[1]) {
|
switch (self.contents[1]) {
|
||||||
'0' => {
|
'g', 'c' => return .{ .static = "OK" },
|
||||||
emu.removeBkpt(.Software, addr);
|
else => {
|
||||||
return .{ .static = "OK" };
|
log.warn("Unimplemented: {s}", .{self.contents});
|
||||||
|
return .{ .static = "" };
|
||||||
},
|
},
|
||||||
'1' => {
|
|
||||||
emu.removeBkpt(.Hardware, addr);
|
|
||||||
return .{ .static = "OK" };
|
|
||||||
},
|
|
||||||
'2' => return .{ .static = "" }, // TODO: Remove Write Watchpoint
|
|
||||||
'3' => return .{ .static = "" }, // TODO: Remove Read Watchpoint
|
|
||||||
'4' => return .{ .static = "" }, // TODO: Remove Access Watchpoint
|
|
||||||
else => return .{ .static = "" },
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'Z' => {
|
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[2..], ",");
|
|
||||||
const addr_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
const kind_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
|
|
||||||
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
|
||||||
const kind = try std.fmt.parseInt(u32, kind_str, 16);
|
|
||||||
|
|
||||||
switch (self.contents[1]) {
|
|
||||||
'0' => {
|
|
||||||
try emu.addBkpt(.Software, addr, kind);
|
|
||||||
return .{ .static = "OK" };
|
|
||||||
},
|
|
||||||
'1' => {
|
|
||||||
emu.addBkpt(.Hardware, addr, kind) catch |e| {
|
|
||||||
switch (e) {
|
|
||||||
error.OutOfSpace => return .{ .static = "E22" }, // FIXME: which errno?
|
|
||||||
else => return e,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return .{ .static = "OK" };
|
|
||||||
},
|
|
||||||
'2' => return .{ .static = "" }, // TODO: Insert Write Watchpoint
|
|
||||||
'3' => return .{ .static = "" }, // TODO: Insert Read Watchpoint
|
|
||||||
'4' => return .{ .static = "" }, // TODO: Insert Access Watchpoint
|
|
||||||
else => return .{ .static = "" },
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// TODO: Figure out the difference between 'M' and 'X'
|
|
||||||
'D' => {
|
|
||||||
log.info("Disconnecting...", .{});
|
|
||||||
|
|
||||||
state.should_quit = true;
|
|
||||||
return .{ .static = "OK" };
|
|
||||||
},
|
|
||||||
'H' => return .{ .static = "" },
|
|
||||||
'v' => {
|
'v' => {
|
||||||
if (substr(self.contents[1..], "MustReplyEmpty")) return .{ .static = "" };
|
if (substr(self.contents[1..], "MustReplyEmpty")) {
|
||||||
|
return .{ .static = "" };
|
||||||
if (substr(self.contents[1..], "Cont")) {
|
|
||||||
switch (self.contents[5]) {
|
|
||||||
'?' => return .{ .static = "" }, // TODO: Implement vCont
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.warn("Unimplemented: {s}", .{self.contents});
|
log.warn("Unimplemented: {s}", .{self.contents});
|
||||||
return .{ .static = "" };
|
return .{ .static = "" };
|
||||||
},
|
},
|
||||||
'T' => return .{ .static = "OK " }, // We assume single threaded here
|
|
||||||
'q' => {
|
'q' => {
|
||||||
if (self.contents[1] == 'C' and self.contents.len == 2) return .{ .static = "QC1" };
|
if (self.contents[1] == 'C' and self.contents.len == 2) return .{ .static = "QC1" };
|
||||||
|
|
||||||
if (substr(self.contents[1..], "fThreadInfo")) return .{ .static = "m1" };
|
if (substr(self.contents[1..], "fThreadInfo")) return .{ .static = "m1" };
|
||||||
if (substr(self.contents[1..], "sThreadInfo")) return .{ .static = "l" };
|
if (substr(self.contents[1..], "sThreadInfo")) return .{ .static = "l" };
|
||||||
if (substr(self.contents[1..], "Attached")) return .{ .static = "1" }; // Tell GDB we're attached to a process
|
if (substr(self.contents[1..], "Attached")) return .{ .static = "1" }; // Tell GDB we're attached to a process
|
||||||
|
|
||||||
if (substr(self.contents[1..], "ThreadExtraInfo")) {
|
|
||||||
const extra_info = "FIXME: what is even expected here?";
|
|
||||||
const ret = try allocator.dupe(u8, &std.fmt.bytesToHex(extra_info, .lower));
|
|
||||||
|
|
||||||
return .{ .alloc = ret };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (substr(self.contents[1..], "Supported")) {
|
if (substr(self.contents[1..], "Supported")) {
|
||||||
const format = "PacketSize={x:};swbreak+;hwbreak+;qXfer:features:read+;{s}";
|
const format = "PacketSize={x:};qXfer:features:read+;qXfer:memory-map:read+";
|
||||||
const mem_map = if (state.memmap_xml == null) "" else "qXfer:memory-map:read+";
|
|
||||||
|
|
||||||
// TODO: Anything else?
|
// TODO: Anything else?
|
||||||
const ret = try std.fmt.allocPrint(allocator, format, .{ Self.max_len, mem_map });
|
|
||||||
|
const ret = try std.fmt.allocPrint(allocator, format, .{Self.max_len});
|
||||||
return .{ .alloc = ret };
|
return .{ .alloc = ret };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (substr(self.contents[1..], "Xfer:features:read")) {
|
if (substr(self.contents[1..], "Xfer:features:read")) {
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
||||||
_ = tokens.next(); // Xfer
|
_ = tokens.next(); // qXfer
|
||||||
_ = tokens.next(); // features
|
_ = tokens.next(); // features
|
||||||
_ = tokens.next(); // read
|
_ = tokens.next(); // read
|
||||||
|
const annex = tokens.next() orelse return .{ .static = "E00" };
|
||||||
const annex = tokens.next() orelse return error.InvalidPacket;
|
const offset_str = tokens.next() orelse return .{ .static = "E00" };
|
||||||
const offset_str = tokens.next() orelse return error.InvalidPacket;
|
const length_str = tokens.next() orelse return .{ .static = "E00" };
|
||||||
const length_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
|
|
||||||
if (std.mem.eql(u8, annex, "target.xml")) {
|
if (std.mem.eql(u8, annex, "target.xml")) {
|
||||||
|
log.info("Providing ARMv4T target description", .{});
|
||||||
|
|
||||||
const offset = try std.fmt.parseInt(usize, offset_str, 16);
|
const offset = try std.fmt.parseInt(usize, offset_str, 16);
|
||||||
const length = try std.fmt.parseInt(usize, length_str, 16);
|
const length = try std.fmt.parseInt(usize, length_str, 16);
|
||||||
|
|
||||||
// + 2 to account for the "m " in the response
|
// + 2 to account for the "m " in the response
|
||||||
// subtract offset so that the allocated buffer isn't
|
// subtract offset so that the allocated buffer isn't
|
||||||
// larger than it needs to be TODO: Test this?
|
// larger than it needs to be TODO: Test this?
|
||||||
const len = @min(length, (state.target_xml.len + 1) - offset);
|
const len = @min(length, (target.len + 1) - offset);
|
||||||
const ret = try allocator.alloc(u8, len);
|
const ret = try allocator.alloc(u8, len);
|
||||||
|
|
||||||
ret[0] = if (ret.len < length) 'l' else 'm';
|
ret[0] = if (ret.len < length) 'l' else 'm';
|
||||||
@memcpy(ret[1..], state.target_xml[offset..]);
|
std.mem.copy(u8, ret[1..], target[offset..]);
|
||||||
|
|
||||||
return .{ .alloc = ret };
|
return .{ .alloc = ret };
|
||||||
} else {
|
} else {
|
||||||
log.err("Unexpected Annex: {s}", .{annex});
|
log.err("Unexpected Annex: {s}", .{annex});
|
||||||
return .{ .static = "E9999" };
|
return .{ .static = "E00" };
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{ .static = "" };
|
return .{ .static = "" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (substr(self.contents[1..], "Xfer:memory-map:read")) {
|
|
||||||
const mem_map = state.memmap_xml.?;
|
|
||||||
|
|
||||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
|
||||||
_ = tokens.next(); // Xfer
|
|
||||||
_ = tokens.next(); // memory-map
|
|
||||||
_ = tokens.next(); // read
|
|
||||||
const offset_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
const length_str = tokens.next() orelse return error.InvalidPacket;
|
|
||||||
|
|
||||||
const offset = try std.fmt.parseInt(usize, offset_str, 16);
|
|
||||||
const length = try std.fmt.parseInt(usize, length_str, 16);
|
|
||||||
|
|
||||||
// see above
|
|
||||||
const len = @min(length, (mem_map.len + 1) - offset);
|
|
||||||
const ret = try allocator.alloc(u8, len);
|
|
||||||
|
|
||||||
ret[0] = if (ret.len < length) 'l' else 'm';
|
|
||||||
@memcpy(ret[1..], mem_map[offset..]);
|
|
||||||
|
|
||||||
return .{ .alloc = ret };
|
|
||||||
}
|
|
||||||
|
|
||||||
log.warn("Unimplemented: {s}", .{self.contents});
|
log.warn("Unimplemented: {s}", .{self.contents});
|
||||||
return .{ .static = "" };
|
return .{ .static = "" };
|
||||||
},
|
},
|
||||||
@@ -310,7 +198,7 @@ pub fn checksum(input: []const u8) u8 {
|
|||||||
var sum: usize = 0;
|
var sum: usize = 0;
|
||||||
for (input) |char| sum += char;
|
for (input) |char| sum += char;
|
||||||
|
|
||||||
return @truncate(sum);
|
return @truncate(u8, sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify(input: []const u8, chksum: u8) bool {
|
fn verify(input: []const u8, chksum: u8) bool {
|
||||||
@@ -318,7 +206,7 @@ fn verify(input: []const u8, chksum: u8) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Signal = enum(u32) {
|
const Signal = enum(u32) {
|
||||||
Hup = 1, // Hangup
|
Hup, // Hangup
|
||||||
Int, // Interrupt
|
Int, // Interrupt
|
||||||
Quit, // Quit
|
Quit, // Quit
|
||||||
Ill, // Illegal Instruction
|
Ill, // Illegal Instruction
|
||||||
|
146
src/Server.zig
146
src/Server.zig
@@ -1,43 +1,67 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const network = @import("network");
|
||||||
const Packet = @import("Packet.zig");
|
const Packet = @import("Packet.zig");
|
||||||
const Emulator = @import("lib.zig").Emulator;
|
|
||||||
|
|
||||||
|
const Socket = network.Socket;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Server = std.net.Server;
|
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const log = std.log.scoped(.Server);
|
const log = std.log.scoped(.Server);
|
||||||
const port: u16 = 2424;
|
const port: u16 = 2424;
|
||||||
|
|
||||||
|
pub const target: []const u8 =
|
||||||
|
\\<target version="1.0">
|
||||||
|
\\ <architecture>armv4t</architecture>
|
||||||
|
\\ <feature name="org.gnu.gdb.arm.core">
|
||||||
|
\\ <reg name="r0" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r1" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r2" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r3" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r4" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r5" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r6" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r7" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r8" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r9" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r10" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r11" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="r12" bitsize="32" type="uint32"/>
|
||||||
|
\\ <reg name="sp" bitsize="32" type="data_ptr"/>
|
||||||
|
\\ <reg name="lr" bitsize="32"/>
|
||||||
|
\\ <reg name="pc" bitsize="32" type="code_ptr"/>
|
||||||
|
\\
|
||||||
|
\\ <reg name="cpsr" bitsize="32" regnum="25"/>
|
||||||
|
\\ </feature>
|
||||||
|
\\</target>
|
||||||
|
;
|
||||||
|
|
||||||
// FIXME: Shouldn't this be a Packet Struct?
|
// FIXME: Shouldn't this be a Packet Struct?
|
||||||
pkt_cache: ?[]const u8 = null,
|
pkt_cache: ?[]const u8 = null,
|
||||||
|
|
||||||
socket: Server,
|
client: Socket,
|
||||||
state: State,
|
_socket: Socket,
|
||||||
|
|
||||||
emu: Emulator,
|
pub fn init() !Self {
|
||||||
|
try network.init();
|
||||||
|
|
||||||
pub const State = struct {
|
var socket = try Socket.create(.ipv4, .tcp);
|
||||||
should_quit: bool = false,
|
try socket.bindToPort(port);
|
||||||
target_xml: []const u8,
|
try socket.listen();
|
||||||
memmap_xml: ?[]const u8,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Xml = struct { target: []const u8, memory_map: ?[]const u8 };
|
var client = try socket.accept(); // TODO: This blocks, is this OK?
|
||||||
|
|
||||||
pub fn init(emulator: Emulator, xml: Xml) !Self {
|
const endpoint = try client.getLocalEndPoint();
|
||||||
const localhost = std.net.Address.initIp4(.{ 127, 0, 0, 1 }, port);
|
log.info("client connected from {}", .{endpoint});
|
||||||
|
|
||||||
return .{
|
return .{ ._socket = socket, .client = client };
|
||||||
.emu = emulator,
|
|
||||||
.socket = try localhost.listen(.{}),
|
|
||||||
.state = .{ .target_xml = xml.target, .memmap_xml = xml.memory_map },
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self, allocator: Allocator) void {
|
pub fn deinit(self: *Self, allocator: Allocator) void {
|
||||||
self.reset(allocator);
|
self.reset(allocator);
|
||||||
self.socket.deinit();
|
|
||||||
|
self.client.close();
|
||||||
|
self._socket.close();
|
||||||
|
network.deinit();
|
||||||
|
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
@@ -50,67 +74,46 @@ const Action = union(enum) {
|
|||||||
nack,
|
nack,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn run(self: *Self, allocator: Allocator, should_quit: *std.atomic.Value(bool)) !void {
|
pub fn run(self: *Self, allocator: Allocator) !void {
|
||||||
var buf: [Packet.max_len]u8 = undefined;
|
var buf: [Packet.max_len]u8 = undefined;
|
||||||
|
|
||||||
var client = try self.socket.accept();
|
while (true) {
|
||||||
log.info("client connected from {}", .{client.address});
|
const len = try self.client.receive(&buf);
|
||||||
|
|
||||||
while (!should_quit.load(.monotonic)) {
|
|
||||||
if (self.state.should_quit) {
|
|
||||||
// Just in case its the gdbstub that exited first,
|
|
||||||
// attempt to signal to the GUI that it should also exit
|
|
||||||
should_quit.store(true, .monotonic);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const len = try client.stream.read(&buf);
|
|
||||||
if (len == 0) break;
|
if (len == 0) break;
|
||||||
|
|
||||||
const action = try self.parse(allocator, buf[0..len]);
|
const action = try Self.parse(allocator, buf[0..len]);
|
||||||
try self.send(allocator, client, action);
|
try self.send(allocator, action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
fn parse(allocator: Allocator, input: []const u8) !Action {
|
||||||
log.debug("-> {s}", .{input});
|
|
||||||
|
|
||||||
return switch (input[0]) {
|
return switch (input[0]) {
|
||||||
'+' => blk: {
|
'+' => .nothing,
|
||||||
if (input.len == 1) break :blk .nothing;
|
|
||||||
|
|
||||||
break :blk switch (input[1]) {
|
|
||||||
'$' => self.handlePacket(allocator, input[1..]),
|
|
||||||
else => std.debug.panic("Unknown: {s}", .{input}),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
'-' => .retry,
|
'-' => .retry,
|
||||||
'$' => try self.handlePacket(allocator, input),
|
'$' => blk: {
|
||||||
'\x03' => .nothing,
|
// Packet
|
||||||
|
var packet = Packet.from(allocator, input) catch return .nack;
|
||||||
|
defer packet.deinit(allocator);
|
||||||
|
|
||||||
|
var string = packet.parse(allocator) catch return .nack;
|
||||||
|
defer string.deinit(allocator);
|
||||||
|
|
||||||
|
const reply = string.inner();
|
||||||
|
|
||||||
|
// deallocated by the caller
|
||||||
|
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
||||||
|
|
||||||
|
break :blk .{ .send = response };
|
||||||
|
},
|
||||||
else => std.debug.panic("Unknown: {s}", .{input}),
|
else => std.debug.panic("Unknown: {s}", .{input}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handlePacket(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
fn send(self: *Self, allocator: Allocator, action: Action) !void {
|
||||||
var packet = Packet.from(allocator, input) catch return .nack;
|
|
||||||
defer packet.deinit(allocator);
|
|
||||||
|
|
||||||
var string = packet.parse(allocator, &self.state, &self.emu) catch return .nack;
|
|
||||||
defer string.deinit(allocator);
|
|
||||||
|
|
||||||
const reply = string.inner();
|
|
||||||
|
|
||||||
// deallocated by the caller
|
|
||||||
const response = try std.fmt.allocPrint(allocator, "+${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
|
||||||
|
|
||||||
return .{ .send = response };
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send(self: *Self, allocator: Allocator, client: Server.Connection, action: Action) !void {
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
.send => |pkt| {
|
.send => |pkt| {
|
||||||
_ = try client.stream.writeAll(pkt);
|
_ = try self.client.send("+"); // ACK
|
||||||
log.debug("<- {s}", .{pkt});
|
_ = try self.client.send(pkt);
|
||||||
|
|
||||||
self.reset(allocator);
|
self.reset(allocator);
|
||||||
self.pkt_cache = pkt;
|
self.pkt_cache = pkt;
|
||||||
@@ -118,21 +121,14 @@ fn send(self: *Self, allocator: Allocator, client: Server.Connection, action: Ac
|
|||||||
.retry => {
|
.retry => {
|
||||||
log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache});
|
log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache});
|
||||||
|
|
||||||
if (self.pkt_cache) |pkt| {
|
if (self.pkt_cache) |pkt| _ = try self.client.send(pkt); // FIXME: is an ack to a nack necessary?
|
||||||
_ = try client.stream.writeAll(pkt);
|
|
||||||
log.debug("<- {s}", .{pkt});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
.ack => {
|
.ack => {
|
||||||
_ = try client.stream.writeAll("+");
|
_ = try self.client.send("+");
|
||||||
log.debug("<- +", .{});
|
|
||||||
|
|
||||||
self.reset(allocator);
|
self.reset(allocator);
|
||||||
},
|
},
|
||||||
.nack => {
|
.nack => {
|
||||||
_ = try client.stream.writeAll("-");
|
_ = try self.client.send("-");
|
||||||
log.debug("<- -", .{});
|
|
||||||
|
|
||||||
self.reset(allocator);
|
self.reset(allocator);
|
||||||
},
|
},
|
||||||
.nothing => self.reset(allocator),
|
.nothing => self.reset(allocator),
|
||||||
|
122
src/State.zig
122
src/State.zig
@@ -1,122 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
const ArrayList = std.ArrayList;
|
|
||||||
|
|
||||||
hw_bkpt: HwBkpt = .{},
|
|
||||||
sw_bkpt: SwBkpt,
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) @This() {
|
|
||||||
return .{ .sw_bkpt = SwBkpt.init(allocator) };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *@This()) void {
|
|
||||||
self.sw_bkpt.deinit();
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SwBkpt = struct {
|
|
||||||
const log = std.log.scoped(.SwBkpt);
|
|
||||||
|
|
||||||
list: std.ArrayList(Bkpt),
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) @This() {
|
|
||||||
return .{ .list = ArrayList(Bkpt).init(allocator) };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *@This()) void {
|
|
||||||
self.list.deinit();
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn isHit(self: *const @This(), addr: u32) bool {
|
|
||||||
for (self.list.items) |bkpt| {
|
|
||||||
if (bkpt.addr == addr) return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(self: *@This(), addr: u32, kind: u32) !void {
|
|
||||||
for (self.list.items) |bkpt| {
|
|
||||||
if (bkpt.addr == addr) return; // indempotent
|
|
||||||
}
|
|
||||||
|
|
||||||
try self.list.append(.{ .addr = addr, .kind = try Bkpt.Kind.from(u32, kind) });
|
|
||||||
log.warn("Added Breakpoint at 0x{X:0>8}", .{addr});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(self: *@This(), addr: u32) void {
|
|
||||||
for (self.list.items, 0..) |bkpt, i| {
|
|
||||||
if (bkpt.addr == addr) {
|
|
||||||
_ = self.list.orderedRemove(i);
|
|
||||||
log.debug("Removed Breakpoint at 0x{X:0>8}", .{addr});
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const HwBkpt = struct {
|
|
||||||
const log = std.log.scoped(.HwBkpt);
|
|
||||||
|
|
||||||
list: [2]?Bkpt = .{ null, null },
|
|
||||||
|
|
||||||
pub fn isHit(self: *const @This(), addr: u32) bool {
|
|
||||||
for (self.list) |bkpt_opt| {
|
|
||||||
const bkpt = bkpt_opt orelse continue;
|
|
||||||
if (bkpt.addr == addr) return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(self: *@This(), addr: u32, kind: u32) !void {
|
|
||||||
for (&self.list) |*bkpt_opt| {
|
|
||||||
if (bkpt_opt.*) |bkpt| {
|
|
||||||
if (bkpt.addr == addr) return; // idempotent
|
|
||||||
} else {
|
|
||||||
bkpt_opt.* = .{ .addr = addr, .kind = try Bkpt.Kind.from(u32, kind) };
|
|
||||||
log.debug("Added Breakpoint at 0x{X:0>8}", .{addr});
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return error.OutOfSpace;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(self: *@This(), addr: u32) void {
|
|
||||||
for (&self.list) |*bkpt_opt| {
|
|
||||||
const bkpt = bkpt_opt.* orelse continue;
|
|
||||||
|
|
||||||
if (bkpt.addr == addr) {
|
|
||||||
bkpt_opt.* = null;
|
|
||||||
log.debug("Removed Breakpoint at 0x{X:0>8}", .{addr});
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const Bkpt = struct {
|
|
||||||
addr: u32,
|
|
||||||
kind: Kind,
|
|
||||||
|
|
||||||
const Kind = enum(u3) {
|
|
||||||
Arm = 2,
|
|
||||||
Thumb = 4,
|
|
||||||
|
|
||||||
pub fn from(comptime T: type, num: T) !@This() {
|
|
||||||
comptime std.debug.assert(@typeInfo(T) == .Int);
|
|
||||||
|
|
||||||
return switch (num) {
|
|
||||||
2 => .Arm,
|
|
||||||
4 => .Thumb,
|
|
||||||
else => error.UnknownBkptKind,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
106
src/lib.zig
106
src/lib.zig
@@ -1,69 +1,51 @@
|
|||||||
const std = @import("std");
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
/// Re-export of the server interface
|
/// Re-export of the server interface
|
||||||
pub const Server = @import("Server.zig");
|
pub const Server = @import("Server.zig");
|
||||||
const State = @import("State.zig");
|
|
||||||
|
|
||||||
/// Interface for interacting between GDB and a GBA emu
|
/// Interface for interacting between GDB and a GBA emu
|
||||||
pub const Emulator = struct {
|
pub const Emulator = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
const Signal = union(enum) {
|
|
||||||
const Kind = enum { HwBkpt, SwBkpt };
|
|
||||||
|
|
||||||
Trap: Kind,
|
|
||||||
SingleStep: void,
|
|
||||||
};
|
|
||||||
|
|
||||||
state: State,
|
|
||||||
|
|
||||||
ptr: *anyopaque,
|
ptr: *anyopaque,
|
||||||
|
|
||||||
readFn: *const fn (*anyopaque, u32) u8,
|
readFn: *const fn (*anyopaque, u32) u8,
|
||||||
writeFn: *const fn (*anyopaque, u32, u8) void,
|
writeFn: *const fn (*anyopaque, u32, u8) void,
|
||||||
|
|
||||||
registersFn: *const fn (*anyopaque) *[16]u32,
|
// FIXME: Expensive copy
|
||||||
cpsrFn: *const fn (*anyopaque) u32,
|
registersFn: *const fn (*const anyopaque) [16]u32,
|
||||||
|
cpsrFn: *const fn (*const anyopaque) u32,
|
||||||
|
|
||||||
stepFn: *const fn (*anyopaque) void,
|
pub fn init(ptr: anytype) Self {
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, ptr: anytype) Self {
|
|
||||||
const Ptr = @TypeOf(ptr);
|
const Ptr = @TypeOf(ptr);
|
||||||
const ptr_info = @typeInfo(Ptr);
|
const ptr_info = @typeInfo(Ptr);
|
||||||
|
|
||||||
if (ptr_info != .Pointer) @compileError("ptr must be a pointer");
|
if (ptr_info != .Pointer) @compileError("ptr must be a pointer");
|
||||||
if (ptr_info.Pointer.size != .One) @compileError("ptr must be a single-item pointer");
|
if (ptr_info.Pointer.size != .One) @compileError("ptr must be a single-item pointer");
|
||||||
|
|
||||||
|
const alignment = ptr_info.Pointer.alignment;
|
||||||
|
|
||||||
const gen = struct {
|
const gen = struct {
|
||||||
pub fn readImpl(pointer: *anyopaque, addr: u32) u8 {
|
pub fn readImpl(pointer: *anyopaque, addr: u32) u8 {
|
||||||
const self: Ptr = @ptrCast(@alignCast(pointer));
|
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||||
|
|
||||||
return @call(.always_inline, ptr_info.Pointer.child.read, .{ self, addr });
|
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.read, .{ u8, self, addr });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn writeImpl(pointer: *anyopaque, addr: u32, value: u8) void {
|
pub fn writeImpl(pointer: *anyopaque, addr: u32, value: u8) void {
|
||||||
const self: Ptr = @ptrCast(@alignCast(pointer));
|
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||||
|
|
||||||
return @call(.always_inline, ptr_info.Pointer.child.write, .{ self, addr, value });
|
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.read, .{ u8, self, addr, value });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn registersImpl(pointer: *anyopaque) *[16]u32 {
|
pub fn registersImpl(pointer: *const anyopaque) [16]u32 {
|
||||||
const self: Ptr = @ptrCast(@alignCast(pointer));
|
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||||
|
|
||||||
return @call(.always_inline, ptr_info.Pointer.child.registers, .{self});
|
return self.r;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cpsrImpl(pointer: *anyopaque) u32 {
|
pub fn cpsrImpl(pointer: *const anyopaque) u32 {
|
||||||
const self: Ptr = @ptrCast(@alignCast(pointer));
|
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||||
|
|
||||||
return @call(.always_inline, ptr_info.Pointer.child.cpsr, .{self});
|
return self.cpsr.raw;
|
||||||
}
|
|
||||||
|
|
||||||
pub fn stepImpl(pointer: *anyopaque) void {
|
|
||||||
const self: Ptr = @ptrCast(@alignCast(pointer));
|
|
||||||
|
|
||||||
return @call(.always_inline, ptr_info.Pointer.child.step, .{self});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -73,17 +55,9 @@ pub const Emulator = struct {
|
|||||||
.writeFn = gen.writeImpl,
|
.writeFn = gen.writeImpl,
|
||||||
.registersFn = gen.registersImpl,
|
.registersFn = gen.registersImpl,
|
||||||
.cpsrFn = gen.cpsrImpl,
|
.cpsrFn = gen.cpsrImpl,
|
||||||
.stepFn = gen.stepImpl,
|
|
||||||
|
|
||||||
.state = State.init(allocator),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
|
||||||
self.state.deinit();
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub inline fn read(self: Self, addr: u32) u8 {
|
pub inline fn read(self: Self, addr: u32) u8 {
|
||||||
return self.readFn(self.ptr, addr);
|
return self.readFn(self.ptr, addr);
|
||||||
}
|
}
|
||||||
@@ -92,57 +66,11 @@ pub const Emulator = struct {
|
|||||||
self.writeFn(self.ptr, addr, value);
|
self.writeFn(self.ptr, addr, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn registers(self: Self) *[16]u32 {
|
pub inline fn registers(self: Self) [16]u32 {
|
||||||
return self.registersFn(self.ptr);
|
return self.registersFn(self.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn cpsr(self: Self) u32 {
|
pub inline fn cpsr(self: Self) u32 {
|
||||||
return self.cpsrFn(self.ptr);
|
return self.cpsrFn(self.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn contd(self: *Self) Signal {
|
|
||||||
while (true) {
|
|
||||||
const signal = self.step();
|
|
||||||
|
|
||||||
switch (signal) {
|
|
||||||
.SingleStep => {},
|
|
||||||
.Trap => return signal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub inline fn step(self: *Self) Signal {
|
|
||||||
self.stepFn(self.ptr);
|
|
||||||
|
|
||||||
const r = self.registersFn(self.ptr);
|
|
||||||
const is_thumb = self.cpsrFn(self.ptr) >> 5 & 1 == 1;
|
|
||||||
|
|
||||||
const r15 = r[15] -| if (is_thumb) @as(u32, 4) else 8;
|
|
||||||
|
|
||||||
if (self.state.sw_bkpt.isHit(r15)) return .{ .Trap = .SwBkpt };
|
|
||||||
if (self.state.hw_bkpt.isHit(r15)) return .{ .Trap = .HwBkpt };
|
|
||||||
|
|
||||||
return .SingleStep;
|
|
||||||
}
|
|
||||||
|
|
||||||
const BkptType = enum { Hardware, Software };
|
|
||||||
|
|
||||||
// TODO: Consider properly implementing Software interrupts?
|
|
||||||
pub fn addBkpt(self: *Self, comptime @"type": BkptType, addr: u32, kind: u32) !void {
|
|
||||||
switch (@"type") {
|
|
||||||
.Hardware => try self.state.hw_bkpt.add(addr, kind),
|
|
||||||
.Software => try self.state.sw_bkpt.add(addr, kind),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn removeBkpt(self: *Self, comptime @"type": BkptType, addr: u32) void {
|
|
||||||
switch (@"type") {
|
|
||||||
.Hardware => self.state.hw_bkpt.remove(addr),
|
|
||||||
.Software => self.state.sw_bkpt.remove(addr),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
test {
|
|
||||||
_ = @import("test.zig");
|
|
||||||
}
|
|
||||||
|
18
src/main.zig
Normal file
18
src/main.zig
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const Server = @import("gdbstub").Server;
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
const log = std.log.scoped(.Main);
|
||||||
|
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer std.debug.assert(!gpa.deinit());
|
||||||
|
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var server = try Server.init();
|
||||||
|
defer server.deinit(allocator);
|
||||||
|
|
||||||
|
try server.run(allocator);
|
||||||
|
|
||||||
|
log.info("Client disconnected", .{});
|
||||||
|
}
|
154
src/test.zig
154
src/test.zig
@@ -1,154 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const builtin = @import("builtin");
|
|
||||||
|
|
||||||
const Emulator = @import("lib.zig").Emulator;
|
|
||||||
const Server = @import("Server.zig");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
const BarebonesEmulator = struct {
|
|
||||||
|
|
||||||
// I have this ARMv4T and GBA memory map xml lying around so we'll reuse it here
|
|
||||||
const target: []const u8 =
|
|
||||||
\\<target version="1.0">
|
|
||||||
\\ <architecture>armv4t</architecture>
|
|
||||||
\\ <feature name="org.gnu.gdb.arm.core">
|
|
||||||
\\ <reg name="r0" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r1" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r2" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r3" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r4" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r5" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r6" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r7" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r8" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r9" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r10" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r11" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="r12" bitsize="32" type="uint32"/>
|
|
||||||
\\ <reg name="sp" bitsize="32" type="data_ptr"/>
|
|
||||||
\\ <reg name="lr" bitsize="32"/>
|
|
||||||
\\ <reg name="pc" bitsize="32" type="code_ptr"/>
|
|
||||||
\\
|
|
||||||
\\ <reg name="cpsr" bitsize="32" regnum="25"/>
|
|
||||||
\\ </feature>
|
|
||||||
\\</target>
|
|
||||||
;
|
|
||||||
|
|
||||||
const memory_map: []const u8 =
|
|
||||||
\\ <memory-map version="1.0">
|
|
||||||
\\ <memory type="rom" start="0x00000000" length="0x00004000"/>
|
|
||||||
\\ <memory type="ram" start="0x02000000" length="0x00040000"/>
|
|
||||||
\\ <memory type="ram" start="0x03000000" length="0x00008000"/>
|
|
||||||
\\ <memory type="ram" start="0x04000000" length="0x00000400"/>
|
|
||||||
\\ <memory type="ram" start="0x05000000" length="0x00000400"/>
|
|
||||||
\\ <memory type="ram" start="0x06000000" length="0x00018000"/>
|
|
||||||
\\ <memory type="ram" start="0x07000000" length="0x00000400"/>
|
|
||||||
\\ <memory type="rom" start="0x08000000" length="0x02000000"/>
|
|
||||||
\\ <memory type="rom" start="0x0A000000" length="0x02000000"/>
|
|
||||||
\\ <memory type="rom" start="0x0C000000" length="0x02000000"/>
|
|
||||||
\\ </memory-map>
|
|
||||||
;
|
|
||||||
|
|
||||||
r: [16]u32 = [_]u32{0} ** 16,
|
|
||||||
|
|
||||||
pub fn interface(self: *@This(), allocator: Allocator) Emulator {
|
|
||||||
return Emulator.init(allocator, self);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read(_: *const @This(), _: u32) u8 {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(_: *@This(), _: u32, _: u8) void {}
|
|
||||||
|
|
||||||
pub fn registers(self: *@This()) *[16]u32 {
|
|
||||||
return &self.r;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cpsr(_: *const @This()) u32 {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn step(_: *@This()) void {
|
|
||||||
// execute 1 instruction
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
test Server {
|
|
||||||
// https://github.com/ziglang/zig/blob/225fe6ddbfae016395762850e0cd5c51f9e7751c/lib/std/net/test.zig#L146C1-L156
|
|
||||||
if (builtin.single_threaded) return error.SkipZigTest;
|
|
||||||
if (builtin.os.tag == .wasi) return error.SkipZigTest;
|
|
||||||
|
|
||||||
if (builtin.os.tag == .windows)
|
|
||||||
_ = try std.os.windows.WSAStartup(2, 2);
|
|
||||||
|
|
||||||
defer if (builtin.os.tag == .windows) std.os.windows.WSACleanup() catch unreachable;
|
|
||||||
|
|
||||||
const allocator = std.testing.allocator;
|
|
||||||
|
|
||||||
var impl = BarebonesEmulator{};
|
|
||||||
var iface = impl.interface(allocator);
|
|
||||||
defer iface.deinit();
|
|
||||||
|
|
||||||
const clientFn = struct {
|
|
||||||
fn inner(address: std.net.Address) !void {
|
|
||||||
const socket = try std.net.tcpConnectToAddress(address);
|
|
||||||
defer socket.close();
|
|
||||||
|
|
||||||
_ = try socket.writer().writeAll("+");
|
|
||||||
}
|
|
||||||
}.inner;
|
|
||||||
|
|
||||||
var server = try Server.init(
|
|
||||||
iface,
|
|
||||||
.{ .target = BarebonesEmulator.target, .memory_map = BarebonesEmulator.memory_map },
|
|
||||||
);
|
|
||||||
defer server.deinit(allocator);
|
|
||||||
|
|
||||||
const t = try std.Thread.spawn(.{}, clientFn, .{server.socket.listen_address});
|
|
||||||
defer t.join();
|
|
||||||
|
|
||||||
var should_quit = std.atomic.Value(bool).init(false);
|
|
||||||
|
|
||||||
try server.run(std.testing.allocator, &should_quit);
|
|
||||||
}
|
|
||||||
|
|
||||||
test Emulator {
|
|
||||||
const ExampleImpl = struct {
|
|
||||||
r: [16]u32 = [_]u32{0} ** 16,
|
|
||||||
|
|
||||||
pub fn interface(self: *@This(), allocator: std.mem.Allocator) Emulator {
|
|
||||||
return Emulator.init(allocator, self);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read(_: *const @This(), _: u32) u8 {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(_: *@This(), _: u32, _: u8) void {}
|
|
||||||
|
|
||||||
pub fn registers(self: *@This()) *[16]u32 {
|
|
||||||
return &self.r;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cpsr(_: *const @This()) u32 {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn step(_: *@This()) void {
|
|
||||||
// execute 1 instruction
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var impl = ExampleImpl{};
|
|
||||||
var emu = Emulator.init(std.testing.allocator, &impl);
|
|
||||||
|
|
||||||
_ = emu.read(0x0000_0000);
|
|
||||||
emu.write(0x0000_0000, 0x00);
|
|
||||||
|
|
||||||
_ = emu.registers();
|
|
||||||
_ = emu.cpsr();
|
|
||||||
|
|
||||||
_ = emu.step();
|
|
||||||
}
|
|
Reference in New Issue
Block a user