authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-09-29 14:48:24+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-09-29 14:48:24+02:00
logf624191f9a759c0d310f3615c10dbfbbc6ae511e
tree78a8231fb05e38cfa91cfe41b1f7875617032128
parentf90510b081e06449ab0bd97a1254c78f5be6a8d4
parentfe468e4fa3538ad4de7c890b4d971f74092d9f7b
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #25388 from alexrp/ksigaction

`std.os.linux`: Fix `k_sigaction` ABI issue on platforms w/o `SA_RESTORER`

14 files changed, 34 insertions(+), 132 deletions(-)

lib/libc/musl/arch/hexagon/bits/signal.h-1
......@@ -61,7 +61,6 @@ typedef struct __ucontext {
6161#define SA_RESTART 0x10000000
6262#define SA_NODEFER 0x40000000
6363#define SA_RESETHAND 0x80000000
64#define SA_RESTORER 0x04000000
6564
6665#endif
6766
lib/libc/musl/arch/riscv32/bits/signal.h-1
......@@ -78,7 +78,6 @@ typedef struct __ucontext
7878#define SA_RESTART 0x10000000
7979#define SA_NODEFER 0x40000000
8080#define SA_RESETHAND 0x80000000
81#define SA_RESTORER 0x04000000
8281
8382#endif
8483
lib/libc/musl/src/signal/hexagon/restore.s deleted-11
......@@ -1,11 +0,0 @@
1// TODO - Test this if sa_restorer is ever supported in our kernel
2.global __restore
3.type __restore,%function
4.global __restore_rt
5.type __restore_rt,%function
6__restore:
7__restore_rt:
8 r6 = #139 // SYS_rt_sigreturn
9 trap0(#0)
10.size __restore, .-__restore
11.size __restore_rt, .-__restore_rt
lib/libc/musl/src/signal/loongarch64/restore.s deleted-10
......@@ -1,10 +0,0 @@
1.global __restore_rt
2.global __restore
3.hidden __restore_rt
4.hidden __restore
5.type __restore_rt,@function
6.type __restore,@function
7__restore_rt:
8__restore:
9 li.w $a7, 139
10 syscall 0
lib/libc/musl/src/signal/riscv32/restore.s deleted-10
......@@ -1,10 +0,0 @@
1.global __restore
2.hidden __restore
3.type __restore, %function
4__restore:
5.global __restore_rt
6.hidden __restore_rt
7.type __restore_rt, %function
8__restore_rt:
9 li a7, 139 # SYS_rt_sigreturn
10 ecall
lib/libc/musl/src/signal/riscv64/restore.s deleted-10
......@@ -1,10 +0,0 @@
1.global __restore
2.hidden __restore
3.type __restore, %function
4__restore:
5.global __restore_rt
6.hidden __restore_rt
7.type __restore_rt, %function
8__restore_rt:
9 li a7, 139 # SYS_rt_sigreturn
10 ecall
lib/std/os/linux.zig+34-12
......@@ -15,8 +15,10 @@ const dl = @import("../dynamic_library.zig");
1515const native_arch = builtin.cpu.arch;
1616const native_abi = builtin.abi;
1717const native_endian = native_arch.endian();
18const is_loongarch = native_arch.isLoongArch();
1819const is_mips = native_arch.isMIPS();
1920const is_ppc = native_arch.isPowerPC();
21const is_riscv = native_arch.isRISCV();
2022const is_sparc = native_arch.isSPARC();
2123const iovec = std.posix.iovec;
2224const iovec_const = std.posix.iovec_const;
......@@ -1868,15 +1870,23 @@ pub fn sigaction(sig: u8, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
18681870 const mask_size = @sizeOf(@TypeOf(ksa.mask));
18691871
18701872 if (act) |new| {
1871 // Zig needs to install our arch restorer function with any signal handler, so
1872 // must copy the Sigaction struct
1873 const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) &restore_rt else &restore;
1874 ksa = k_sigaction{
1875 .handler = new.handler.handler,
1876 .flags = new.flags | SA.RESTORER,
1877 .mask = new.mask,
1878 .restorer = @ptrCast(restorer_fn),
1879 };
1873 if (native_arch == .hexagon or is_loongarch or is_mips or is_riscv) {
1874 ksa = .{
1875 .handler = new.handler.handler,
1876 .flags = new.flags,
1877 .mask = new.mask,
1878 };
1879 } else {
1880 // Zig needs to install our arch restorer function with any signal handler, so
1881 // must copy the Sigaction struct
1882 const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) &restore_rt else &restore;
1883 ksa = .{
1884 .handler = new.handler.handler,
1885 .flags = new.flags | SA.RESTORER,
1886 .mask = new.mask,
1887 .restorer = @ptrCast(restorer_fn),
1888 };
1889 }
18801890 }
18811891
18821892 const ksa_arg = if (act != null) @intFromPtr(&ksa) else 0;
......@@ -3668,7 +3678,6 @@ pub const SA = if (is_mips) struct {
36683678 pub const RESETHAND = 0x80000000;
36693679 pub const ONSTACK = 0x08000000;
36703680 pub const NODEFER = 0x40000000;
3671 pub const RESTORER = 0x04000000;
36723681} else if (is_sparc) struct {
36733682 pub const NOCLDSTOP = 0x8;
36743683 pub const NOCLDWAIT = 0x100;
......@@ -3678,6 +3687,14 @@ pub const SA = if (is_mips) struct {
36783687 pub const ONSTACK = 0x1;
36793688 pub const NODEFER = 0x20;
36803689 pub const RESTORER = 0x04000000;
3690} else if (native_arch == .hexagon or is_loongarch or is_riscv) struct {
3691 pub const NOCLDSTOP = 1;
3692 pub const NOCLDWAIT = 2;
3693 pub const SIGINFO = 4;
3694 pub const RESTART = 0x10000000;
3695 pub const RESETHAND = 0x80000000;
3696 pub const ONSTACK = 0x08000000;
3697 pub const NODEFER = 0x40000000;
36813698} else struct {
36823699 pub const NOCLDSTOP = 1;
36833700 pub const NOCLDWAIT = 2;
......@@ -5743,13 +5760,18 @@ const k_sigaction_funcs = struct {
57435760 const restorer = *const fn () callconv(.c) void;
57445761};
57455762
5746/// Kernel sigaction struct, as expected by the `rt_sigaction` syscall. Includes restorer.
5763/// Kernel sigaction struct, as expected by the `rt_sigaction` syscall. Includes `restorer` on
5764/// targets where userspace is responsible for hooking up `rt_sigreturn`.
57475765pub const k_sigaction = switch (native_arch) {
57485766 .mips, .mipsel, .mips64, .mips64el => extern struct {
57495767 flags: c_uint,
57505768 handler: k_sigaction_funcs.handler,
57515769 mask: sigset_t,
5752 restorer: k_sigaction_funcs.restorer,
5770 },
5771 .hexagon, .loongarch32, .loongarch64, .riscv32, .riscv64 => extern struct {
5772 handler: k_sigaction_funcs.handler,
5773 flags: c_ulong,
5774 mask: sigset_t,
57535775 },
57545776 else => extern struct {
57555777 handler: k_sigaction_funcs.handler,
lib/std/os/linux/hexagon.zig-10
......@@ -128,16 +128,6 @@ pub fn clone() callconv(.naked) usize {
128128 );
129129}
130130
131pub const restore = restore_rt;
132
133pub fn restore_rt() callconv(.naked) noreturn {
134 asm volatile (
135 \\ trap0(#0)
136 :
137 : [number] "{r6}" (@intFromEnum(SYS.rt_sigreturn)),
138 );
139}
140
141131pub const F = struct {
142132 pub const DUPFD = 0;
143133 pub const GETFD = 1;
lib/std/os/linux/loongarch64.zig-11
......@@ -135,17 +135,6 @@ pub fn clone() callconv(.naked) usize {
135135 );
136136}
137137
138pub const restore = restore_rt;
139
140pub fn restore_rt() callconv(.naked) noreturn {
141 asm volatile (
142 \\ or $a7, $zero, %[number]
143 \\ syscall 0
144 :
145 : [number] "r" (@intFromEnum(SYS.rt_sigreturn)),
146 );
147}
148
149138pub const msghdr = extern struct {
150139 name: ?*sockaddr,
151140 namelen: socklen_t,
lib/std/os/linux/mips.zig-16
......@@ -241,22 +241,6 @@ pub fn clone() callconv(.naked) usize {
241241 );
242242}
243243
244pub fn restore() callconv(.naked) noreturn {
245 asm volatile (
246 \\ syscall
247 :
248 : [number] "{$2}" (@intFromEnum(SYS.sigreturn)),
249 : .{ .r1 = true, .r3 = true, .r4 = true, .r5 = true, .r6 = true, .r7 = true, .r8 = true, .r9 = true, .r10 = true, .r11 = true, .r12 = true, .r13 = true, .r14 = true, .r15 = true, .r24 = true, .r25 = true, .hi = true, .lo = true, .memory = true });
250}
251
252pub fn restore_rt() callconv(.naked) noreturn {
253 asm volatile (
254 \\ syscall
255 :
256 : [number] "{$2}" (@intFromEnum(SYS.rt_sigreturn)),
257 );
258}
259
260244pub const F = struct {
261245 pub const DUPFD = 0;
262246 pub const GETFD = 1;
lib/std/os/linux/mips64.zig-16
......@@ -220,22 +220,6 @@ pub fn clone() callconv(.naked) usize {
220220 );
221221}
222222
223pub fn restore() callconv(.naked) noreturn {
224 asm volatile (
225 \\ syscall
226 :
227 : [number] "{$2}" (@intFromEnum(SYS.rt_sigreturn)),
228 : .{ .r1 = true, .r3 = true, .r4 = true, .r5 = true, .r6 = true, .r7 = true, .r8 = true, .r9 = true, .r10 = true, .r11 = true, .r12 = true, .r13 = true, .r14 = true, .r15 = true, .r24 = true, .r25 = true, .hi = true, .lo = true, .memory = true });
229}
230
231pub fn restore_rt() callconv(.naked) noreturn {
232 asm volatile (
233 \\ syscall
234 :
235 : [number] "{$2}" (@intFromEnum(SYS.rt_sigreturn)),
236 );
237}
238
239223pub const F = struct {
240224 pub const DUPFD = 0;
241225 pub const GETFD = 1;
lib/std/os/linux/riscv32.zig-10
......@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {
135135 );
136136}
137137
138pub const restore = restore_rt;
139
140pub fn restore_rt() callconv(.naked) noreturn {
141 asm volatile (
142 \\ ecall
143 :
144 : [number] "{x17}" (@intFromEnum(SYS.rt_sigreturn)),
145 );
146}
147
148138pub const F = struct {
149139 pub const DUPFD = 0;
150140 pub const GETFD = 1;
lib/std/os/linux/riscv64.zig-10
......@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {
135135 );
136136}
137137
138pub const restore = restore_rt;
139
140pub fn restore_rt() callconv(.naked) noreturn {
141 asm volatile (
142 \\ ecall
143 :
144 : [number] "{x17}" (@intFromEnum(SYS.rt_sigreturn)),
145 );
146}
147
148138pub const F = struct {
149139 pub const DUPFD = 0;
150140 pub const GETFD = 1;
src/libs/musl.zig-4
......@@ -1538,13 +1538,11 @@ const src_files = [_][]const u8{
15381538 "musl/src/signal/arm/sigsetjmp.s",
15391539 "musl/src/signal/block.c",
15401540 "musl/src/signal/getitimer.c",
1541 "musl/src/signal/hexagon/restore.s",
15421541 "musl/src/signal/hexagon/sigsetjmp.s",
15431542 "musl/src/signal/i386/restore.s",
15441543 "musl/src/signal/i386/sigsetjmp.s",
15451544 "musl/src/signal/kill.c",
15461545 "musl/src/signal/killpg.c",
1547 "musl/src/signal/loongarch64/restore.s",
15481546 "musl/src/signal/loongarch64/sigsetjmp.s",
15491547 "musl/src/signal/m68k/sigsetjmp.s",
15501548 "musl/src/signal/mips64/sigsetjmp.s",
......@@ -1558,9 +1556,7 @@ const src_files = [_][]const u8{
15581556 "musl/src/signal/psignal.c",
15591557 "musl/src/signal/raise.c",
15601558 "musl/src/signal/restore.c",
1561 "musl/src/signal/riscv32/restore.s",
15621559 "musl/src/signal/riscv32/sigsetjmp.s",
1563 "musl/src/signal/riscv64/restore.s",
15641560 "musl/src/signal/riscv64/sigsetjmp.s",
15651561 "musl/src/signal/s390x/restore.s",
15661562 "musl/src/signal/s390x/sigsetjmp.s",