zig/lib/std/os/linux/hexagon.zig
mlugg a18fd41064
std: rework/remove ucontext_t
Our usage of `ucontext_t` in the standard library was kind of
problematic. We unnecessarily mimiced libc-specific structures, and our
`getcontext` implementation was overkill for our use case of stack
tracing.

This commit introduces a new namespace, `std.debug.cpu_context`, which
contains "context" types for various architectures (currently x86,
x86_64, ARM, and AARCH64) containing the general-purpose CPU registers;
the ones needed in practice for stack unwinding. Each implementation has
a function `current` which populates the structure using inline
assembly. The structure is user-overrideable, though that should only be
necessary if the standard library does not have an implementation for
the *architecture*: that is to say, none of this is OS-dependent.

Of course, in POSIX signal handlers, we get a `ucontext_t` from the
kernel. The function `std.debug.cpu_context.fromPosixSignalContext`
converts this to a `std.debug.cpu_context.Native` with a big ol' target
switch.

This functionality is not exposed from `std.c` or `std.posix`, and
neither are `ucontext_t`, `mcontext_t`, or `getcontext`. The rationale
is that these types and functions do not conform to a specific ABI, and
in fact tend to get updated over time based on CPU features and
extensions; in addition, different libcs use different structures which
are "partially compatible" with the kernel structure. Overall, it's a
mess, but all we need is the kernel context, so we can just define a
kernel-compatible structure as long as we don't claim C compatibility by
putting it in `std.c` or `std.posix`.

This change resulted in a few nice `std.debug` simplifications, but
nothing too noteworthy. However, the main benefit of this change is that
DWARF unwinding---sometimes necessary for collecting stack traces
reliably---now requires far less target-specific integration.

Also fix a bug I noticed in `PageAllocator` (I found this due to a bug
in my distro's QEMU distribution; thanks, broken QEMU patch!) and I
think a couple of minor bugs in `std.debug`.

Resolves: #23801
Resolves: #23802
2025-09-30 13:44:54 +01:00

239 lines
5.7 KiB
Zig

const builtin = @import("builtin");
const std = @import("../../std.zig");
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
const linux = std.os.linux;
const SYS = linux.SYS;
const uid_t = std.os.linux.uid_t;
const gid_t = std.os.linux.gid_t;
const pid_t = std.os.linux.pid_t;
const sockaddr = linux.sockaddr;
const socklen_t = linux.socklen_t;
const timespec = std.os.linux.timespec;
pub fn syscall0(number: SYS) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
: .{ .memory = true });
}
pub fn syscall1(number: SYS, arg1: usize) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
: .{ .memory = true });
}
pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
[arg2] "{r1}" (arg2),
: .{ .memory = true });
}
pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
[arg2] "{r1}" (arg2),
[arg3] "{r2}" (arg3),
: .{ .memory = true });
}
pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
[arg2] "{r1}" (arg2),
[arg3] "{r2}" (arg3),
[arg4] "{r3}" (arg4),
: .{ .memory = true });
}
pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
[arg2] "{r1}" (arg2),
[arg3] "{r2}" (arg3),
[arg4] "{r3}" (arg4),
[arg5] "{r4}" (arg5),
: .{ .memory = true });
}
pub fn syscall6(
number: SYS,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile ("trap0(#1)"
: [ret] "={r0}" (-> usize),
: [number] "{r6}" (@intFromEnum(number)),
[arg1] "{r0}" (arg1),
[arg2] "{r1}" (arg2),
[arg3] "{r2}" (arg3),
[arg4] "{r3}" (arg4),
[arg5] "{r4}" (arg5),
[arg6] "{r5}" (arg6),
: .{ .memory = true });
}
pub fn clone() callconv(.naked) usize {
// __clone(func, stack, flags, arg, ptid, tls, ctid)
// r0, r1, r2, r3, r4, r5, +0
//
// syscall(SYS_clone, flags, stack, ptid, ctid, tls)
// r6 r0, r1, r2, r3, r4
asm volatile (
\\ allocframe(#8)
\\
\\ r11 = r0
\\ r10 = r3
\\
\\ r6 = #220 // SYS_clone
\\ r0 = r2
\\ r1 = and(r1, #-8)
\\ r2 = r4
\\ r3 = memw(r30 + #8)
\\ r4 = r5
\\ trap0(#1)
\\
\\ p0 = cmp.eq(r0, #0)
\\ if (!p0) dealloc_return
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined r31
);
asm volatile (
\\ r30 = #0
\\ r31 = #0
\\
\\ r0 = r10
\\ callr r11
\\
\\ r6 = #93 // SYS_exit
\\ r0 = #0
\\ trap0(#1)
);
}
pub const F = struct {
pub const DUPFD = 0;
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
pub const SETFL = 4;
pub const GETLK = 5;
pub const SETLK = 6;
pub const SETLKW = 7;
pub const SETOWN = 8;
pub const GETOWN = 9;
pub const SETSIG = 10;
pub const GETSIG = 11;
pub const RDLCK = 0;
pub const WRLCK = 1;
pub const UNLCK = 2;
pub const SETOWN_EX = 15;
pub const GETOWN_EX = 16;
pub const GETOWNER_UIDS = 17;
};
pub const timeval = extern struct {
sec: time_t,
usec: i32,
};
pub const Flock = extern struct {
type: i16,
whence: i16,
start: off_t,
len: off_t,
pid: pid_t,
__unused: [4]u8,
};
pub const msghdr = extern struct {
name: ?*sockaddr,
namelen: socklen_t,
iov: [*]iovec,
iovlen: i32,
__pad1: i32 = 0,
control: ?*anyopaque,
controllen: socklen_t,
__pad2: socklen_t = 0,
flags: i32,
};
pub const msghdr_const = extern struct {
name: ?*const sockaddr,
namelen: socklen_t,
iov: [*]const iovec_const,
iovlen: i32,
__pad1: i32 = 0,
control: ?*const anyopaque,
controllen: socklen_t,
__pad2: socklen_t = 0,
flags: i32,
};
pub const blksize_t = i32;
pub const nlink_t = u32;
pub const time_t = i32;
pub const mode_t = u32;
pub const off_t = i64;
pub const ino_t = u64;
pub const dev_t = u64;
pub const blkcnt_t = i64;
// The `stat` definition used by the Linux kernel.
pub const Stat = extern struct {
dev: dev_t,
ino: ino_t,
mode: mode_t,
nlink: nlink_t,
uid: uid_t,
gid: gid_t,
rdev: dev_t,
__pad: u32,
size: off_t,
blksize: blksize_t,
__pad2: i32,
blocks: blkcnt_t,
atim: timespec,
mtim: timespec,
ctim: timespec,
__unused: [2]u32,
pub fn atime(self: @This()) timespec {
return self.atim;
}
pub fn mtime(self: @This()) timespec {
return self.mtim;
}
pub fn ctime(self: @This()) timespec {
return self.ctim;
}
};
pub const Elf_Symndx = u32;
pub const VDSO = void;
/// TODO
pub const ucontext_t = void;