Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
lib/std: support riscv64
Browse files Browse the repository at this point in the history
Borrow the ucontext layout from Go (probably reasonable?) and fix up
some missing imports for shared structures.

I can compile a glibc-linked hello.zig with:

   zig build-exe hello.zig -lc -target riscv64-linux-gnu.2.38 -mcpu=generic_rv64

or a native Zig binary with:

   zig build-exe hello.zig -target riscv64-linux -mcpu=generic_rv64

But the resulting binary segfaults immediately in my (perhaps broken) qemu
setup.  I've no experience in riscv64, so this maybe be broken in subtle
ways, but perhaps its a useful point for someone with the right hardware
to make more progress.

Builds on ziglang#18803
  • Loading branch information
rootbeer authored and RossComputerGuy committed Mar 20, 2024
1 parent 02e8706 commit fb7a9d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {

pub const have_ucontext = @hasDecl(os.system, "ucontext_t") and
(builtin.os.tag != .linux or switch (builtin.cpu.arch) {
.mips, .mipsel, .mips64, .mips64el, .riscv64 => false,
.mips, .mipsel, .mips64, .mips64el => false,
else => true,
});

Expand Down
55 changes: 55 additions & 0 deletions lib/std/os/linux/riscv64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const pid_t = std.os.linux.pid_t;
const sockaddr = linux.sockaddr;
const socklen_t = linux.socklen_t;
const timespec = std.os.linux.timespec;
const stack_t = linux.stack_t;
const sigset_t = linux.sigset_t;

pub fn syscall0(number: SYS) usize {
return asm volatile ("ecall"
Expand Down Expand Up @@ -223,3 +225,56 @@ pub const Stat = extern struct {
pub const Elf_Symndx = u32;

pub const VDSO = struct {};

pub const userregs_t = extern struct {
pc: u64,
ra: u64,
sp: u64,
gp: u64,
tp: u64,
t0: u64,
t1: u64,
t2: u64,
s0: u64,
s1: u64,
a0: u64,
a1: u64,
a2: u64,
a3: u64,
a4: u64,
a5: u64,
a6: u64,
a7: u64,
s2: u64,
s3: u64,
s4: u64,
s5: u64,
s6: u64,
s7: u64,
s8: u64,
s9: u64,
s10: u64,
s11: u64,
t3: u64,
t4: u64,
t5: u64,
t6: u64,
};

pub const fpregs_t = extern struct {
f: [528]u8,
};

pub const mcontext_t = extern struct {
userregs: userregs_t,
fpregs: fpregs_t,
};

pub const ucontext_t = extern struct {
flags: u64,
link: ?*ucontext_t,
stack: stack_t,
sigmask: sigset_t,
reserved: [8]u8 = undefined,
mcontext: mcontext_t,
};

0 comments on commit fb7a9d3

Please sign in to comment.