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 {...@@ -61,7 +61,6 @@ typedef struct __ucontext {
61#define SA_RESTART 0x1000000061#define SA_RESTART 0x10000000
62#define SA_NODEFER 0x4000000062#define SA_NODEFER 0x40000000
63#define SA_RESETHAND 0x8000000063#define SA_RESETHAND 0x80000000
64#define SA_RESTORER 0x04000000
6564
66#endif65#endif
6766
lib/libc/musl/arch/riscv32/bits/signal.h-1
...@@ -78,7 +78,6 @@ typedef struct __ucontext...@@ -78,7 +78,6 @@ typedef struct __ucontext
78#define SA_RESTART 0x1000000078#define SA_RESTART 0x10000000
79#define SA_NODEFER 0x4000000079#define SA_NODEFER 0x40000000
80#define SA_RESETHAND 0x8000000080#define SA_RESETHAND 0x80000000
81#define SA_RESTORER 0x04000000
8281
83#endif82#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");...@@ -15,8 +15,10 @@ const dl = @import("../dynamic_library.zig");
15const native_arch = builtin.cpu.arch;15const native_arch = builtin.cpu.arch;
16const native_abi = builtin.abi;16const native_abi = builtin.abi;
17const native_endian = native_arch.endian();17const native_endian = native_arch.endian();
18const is_loongarch = native_arch.isLoongArch();
18const is_mips = native_arch.isMIPS();19const is_mips = native_arch.isMIPS();
19const is_ppc = native_arch.isPowerPC();20const is_ppc = native_arch.isPowerPC();
21const is_riscv = native_arch.isRISCV();
20const is_sparc = native_arch.isSPARC();22const is_sparc = native_arch.isSPARC();
21const iovec = std.posix.iovec;23const iovec = std.posix.iovec;
22const iovec_const = std.posix.iovec_const;24const iovec_const = std.posix.iovec_const;
...@@ -1868,15 +1870,23 @@ pub fn sigaction(sig: u8, noalias act: ?*const Sigaction, noalias oact: ?*Sigact...@@ -1868,15 +1870,23 @@ pub fn sigaction(sig: u8, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
1868 const mask_size = @sizeOf(@TypeOf(ksa.mask));1870 const mask_size = @sizeOf(@TypeOf(ksa.mask));
18691871
1870 if (act) |new| {1872 if (act) |new| {
1871 // Zig needs to install our arch restorer function with any signal handler, so1873 if (native_arch == .hexagon or is_loongarch or is_mips or is_riscv) {
1872 // must copy the Sigaction struct1874 ksa = .{
1873 const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) &restore_rt else &restore;1875 .handler = new.handler.handler,
1874 ksa = k_sigaction{1876 .flags = new.flags,
1875 .handler = new.handler.handler,1877 .mask = new.mask,
1876 .flags = new.flags | SA.RESTORER,1878 };
1877 .mask = new.mask,1879 } else {
1878 .restorer = @ptrCast(restorer_fn),1880 // Zig needs to install our arch restorer function with any signal handler, so
1879 };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 }
1880 }1890 }
18811891
1882 const ksa_arg = if (act != null) @intFromPtr(&ksa) else 0;1892 const ksa_arg = if (act != null) @intFromPtr(&ksa) else 0;
...@@ -3668,7 +3678,6 @@ pub const SA = if (is_mips) struct {...@@ -3668,7 +3678,6 @@ pub const SA = if (is_mips) struct {
3668 pub const RESETHAND = 0x80000000;3678 pub const RESETHAND = 0x80000000;
3669 pub const ONSTACK = 0x08000000;3679 pub const ONSTACK = 0x08000000;
3670 pub const NODEFER = 0x40000000;3680 pub const NODEFER = 0x40000000;
3671 pub const RESTORER = 0x04000000;
3672} else if (is_sparc) struct {3681} else if (is_sparc) struct {
3673 pub const NOCLDSTOP = 0x8;3682 pub const NOCLDSTOP = 0x8;
3674 pub const NOCLDWAIT = 0x100;3683 pub const NOCLDWAIT = 0x100;
...@@ -3678,6 +3687,14 @@ pub const SA = if (is_mips) struct {...@@ -3678,6 +3687,14 @@ pub const SA = if (is_mips) struct {
3678 pub const ONSTACK = 0x1;3687 pub const ONSTACK = 0x1;
3679 pub const NODEFER = 0x20;3688 pub const NODEFER = 0x20;
3680 pub const RESTORER = 0x04000000;3689 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;
3681} else struct {3698} else struct {
3682 pub const NOCLDSTOP = 1;3699 pub const NOCLDSTOP = 1;
3683 pub const NOCLDWAIT = 2;3700 pub const NOCLDWAIT = 2;
...@@ -5743,13 +5760,18 @@ const k_sigaction_funcs = struct {...@@ -5743,13 +5760,18 @@ const k_sigaction_funcs = struct {
5743 const restorer = *const fn () callconv(.c) void;5760 const restorer = *const fn () callconv(.c) void;
5744};5761};
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`.
5747pub const k_sigaction = switch (native_arch) {5765pub const k_sigaction = switch (native_arch) {
5748 .mips, .mipsel, .mips64, .mips64el => extern struct {5766 .mips, .mipsel, .mips64, .mips64el => extern struct {
5749 flags: c_uint,5767 flags: c_uint,
5750 handler: k_sigaction_funcs.handler,5768 handler: k_sigaction_funcs.handler,
5751 mask: sigset_t,5769 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,
5753 },5775 },
5754 else => extern struct {5776 else => extern struct {
5755 handler: k_sigaction_funcs.handler,5777 handler: k_sigaction_funcs.handler,
lib/std/os/linux/hexagon.zig-10
...@@ -128,16 +128,6 @@ pub fn clone() callconv(.naked) usize {...@@ -128,16 +128,6 @@ pub fn clone() callconv(.naked) usize {
128 );128 );
129}129}
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
141pub const F = struct {131pub const F = struct {
142 pub const DUPFD = 0;132 pub const DUPFD = 0;
143 pub const GETFD = 1;133 pub const GETFD = 1;
lib/std/os/linux/loongarch64.zig-11
...@@ -135,17 +135,6 @@ pub fn clone() callconv(.naked) usize {...@@ -135,17 +135,6 @@ pub fn clone() callconv(.naked) usize {
135 );135 );
136}136}
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
149pub const msghdr = extern struct {138pub const msghdr = extern struct {
150 name: ?*sockaddr,139 name: ?*sockaddr,
151 namelen: socklen_t,140 namelen: socklen_t,
lib/std/os/linux/mips.zig-16
...@@ -241,22 +241,6 @@ pub fn clone() callconv(.naked) usize {...@@ -241,22 +241,6 @@ pub fn clone() callconv(.naked) usize {
241 );241 );
242}242}
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
260pub const F = struct {244pub const F = struct {
261 pub const DUPFD = 0;245 pub const DUPFD = 0;
262 pub const GETFD = 1;246 pub const GETFD = 1;
lib/std/os/linux/mips64.zig-16
...@@ -220,22 +220,6 @@ pub fn clone() callconv(.naked) usize {...@@ -220,22 +220,6 @@ pub fn clone() callconv(.naked) usize {
220 );220 );
221}221}
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
239pub const F = struct {223pub const F = struct {
240 pub const DUPFD = 0;224 pub const DUPFD = 0;
241 pub const GETFD = 1;225 pub const GETFD = 1;
lib/std/os/linux/riscv32.zig-10
...@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {...@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {
135 );135 );
136}136}
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
148pub const F = struct {138pub const F = struct {
149 pub const DUPFD = 0;139 pub const DUPFD = 0;
150 pub const GETFD = 1;140 pub const GETFD = 1;
lib/std/os/linux/riscv64.zig-10
...@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {...@@ -135,16 +135,6 @@ pub fn clone() callconv(.naked) usize {
135 );135 );
136}136}
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
148pub const F = struct {138pub const F = struct {
149 pub const DUPFD = 0;139 pub const DUPFD = 0;
150 pub const GETFD = 1;140 pub const GETFD = 1;
src/libs/musl.zig-4
...@@ -1538,13 +1538,11 @@ const src_files = [_][]const u8{...@@ -1538,13 +1538,11 @@ const src_files = [_][]const u8{
1538 "musl/src/signal/arm/sigsetjmp.s",1538 "musl/src/signal/arm/sigsetjmp.s",
1539 "musl/src/signal/block.c",1539 "musl/src/signal/block.c",
1540 "musl/src/signal/getitimer.c",1540 "musl/src/signal/getitimer.c",
1541 "musl/src/signal/hexagon/restore.s",
1542 "musl/src/signal/hexagon/sigsetjmp.s",1541 "musl/src/signal/hexagon/sigsetjmp.s",
1543 "musl/src/signal/i386/restore.s",1542 "musl/src/signal/i386/restore.s",
1544 "musl/src/signal/i386/sigsetjmp.s",1543 "musl/src/signal/i386/sigsetjmp.s",
1545 "musl/src/signal/kill.c",1544 "musl/src/signal/kill.c",
1546 "musl/src/signal/killpg.c",1545 "musl/src/signal/killpg.c",
1547 "musl/src/signal/loongarch64/restore.s",
1548 "musl/src/signal/loongarch64/sigsetjmp.s",1546 "musl/src/signal/loongarch64/sigsetjmp.s",
1549 "musl/src/signal/m68k/sigsetjmp.s",1547 "musl/src/signal/m68k/sigsetjmp.s",
1550 "musl/src/signal/mips64/sigsetjmp.s",1548 "musl/src/signal/mips64/sigsetjmp.s",
...@@ -1558,9 +1556,7 @@ const src_files = [_][]const u8{...@@ -1558,9 +1556,7 @@ const src_files = [_][]const u8{
1558 "musl/src/signal/psignal.c",1556 "musl/src/signal/psignal.c",
1559 "musl/src/signal/raise.c",1557 "musl/src/signal/raise.c",
1560 "musl/src/signal/restore.c",1558 "musl/src/signal/restore.c",
1561 "musl/src/signal/riscv32/restore.s",
1562 "musl/src/signal/riscv32/sigsetjmp.s",1559 "musl/src/signal/riscv32/sigsetjmp.s",
1563 "musl/src/signal/riscv64/restore.s",
1564 "musl/src/signal/riscv64/sigsetjmp.s",1560 "musl/src/signal/riscv64/sigsetjmp.s",
1565 "musl/src/signal/s390x/restore.s",1561 "musl/src/signal/s390x/restore.s",
1566 "musl/src/signal/s390x/sigsetjmp.s",1562 "musl/src/signal/s390x/sigsetjmp.s",