]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
syscall: support Linux x32 ABI
[public-inbox.git] / lib / PublicInbox / Syscall.pm
1 # This is a fork of the (for now) unmaintained Sys::Syscall 0.25,
2 # specifically the Debian libsys-syscall-perl 0.25-6 version to
3 # fix upstream regressions in 0.25.
4 #
5 # This license differs from the rest of public-inbox
6 #
7 # This module is Copyright (c) 2005 Six Apart, Ltd.
8 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
9 #
10 # All rights reserved.
11 #
12 # You may distribute under the terms of either the GNU General Public
13 # License or the Artistic License, as specified in the Perl README file.
14 package PublicInbox::Syscall;
15 use strict;
16 use parent qw(Exporter);
17 use POSIX qw(ENOSYS SEEK_CUR);
18 use Config;
19
20 # $VERSION = '0.25'; # Sys::Syscall version
21 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
22                   EPOLLIN EPOLLOUT EPOLLET
23                   EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
24                   EPOLLONESHOT EPOLLEXCLUSIVE
25                   signalfd SFD_NONBLOCK);
26 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
27                              EPOLLIN EPOLLOUT
28                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
29                              EPOLLONESHOT EPOLLEXCLUSIVE)],
30                 );
31
32 use constant {
33         EPOLLIN => 1,
34         EPOLLOUT => 4,
35         # EPOLLERR => 8,
36         # EPOLLHUP => 16,
37         # EPOLLRDBAND => 128,
38         EPOLLEXCLUSIVE => (1 << 28),
39         EPOLLONESHOT => (1 << 30),
40         EPOLLET => (1 << 31),
41         EPOLL_CTL_ADD => 1,
42         EPOLL_CTL_DEL => 2,
43         EPOLL_CTL_MOD => 3,
44
45         SFD_CLOEXEC => 02000000,
46         SFD_NONBLOCK => 00004000,
47 };
48
49 our $loaded_syscall = 0;
50
51 sub _load_syscall {
52     # props to Gaal for this!
53     return if $loaded_syscall++;
54     my $clean = sub {
55         delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
56                         _h2ph_pre.ph sys/syscall.ph>};
57     };
58     $clean->(); # don't trust modules before us
59     my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
60     $clean->(); # don't require modules after us trust us
61     return $rv;
62 }
63
64
65 our (
66      $SYS_epoll_create,
67      $SYS_epoll_ctl,
68      $SYS_epoll_wait,
69      $SYS_signalfd4,
70      );
71
72 our $no_deprecated = 0;
73
74 if ($^O eq "linux") {
75     my $machine = (POSIX::uname())[-1];
76     # whether the machine requires 64-bit numbers to be on 8-byte
77     # boundaries.
78     my $u64_mod_8 = 0;
79
80     # if we're running on an x86_64 kernel, but a 32-bit process,
81     # we need to use the x32 or i386 syscall numbers.
82     if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
83         $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
84     }
85
86     # Similarly for mips64 vs mips
87     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
88         $machine = "mips";
89     }
90
91     if ($machine =~ m/^i[3456]86$/) {
92         $SYS_epoll_create = 254;
93         $SYS_epoll_ctl    = 255;
94         $SYS_epoll_wait   = 256;
95         $SYS_signalfd4 = 327;
96     } elsif ($machine eq "x86_64") {
97         $SYS_epoll_create = 213;
98         $SYS_epoll_ctl    = 233;
99         $SYS_epoll_wait   = 232;
100         $SYS_signalfd4 = 289;
101     } elsif ($machine eq 'x32') {
102         $SYS_epoll_create = 1073742037;
103         $SYS_epoll_ctl = 1073742057;
104         $SYS_epoll_wait = 1073742056;
105         $SYS_signalfd4 = 1073742113;
106     } elsif ($machine =~ m/^parisc/) {
107         $SYS_epoll_create = 224;
108         $SYS_epoll_ctl    = 225;
109         $SYS_epoll_wait   = 226;
110         $u64_mod_8        = 1;
111         $SYS_signalfd4 = 309;
112     } elsif ($machine =~ m/^ppc64/) {
113         $SYS_epoll_create = 236;
114         $SYS_epoll_ctl    = 237;
115         $SYS_epoll_wait   = 238;
116         $u64_mod_8        = 1;
117         $SYS_signalfd4 = 313;
118     } elsif ($machine eq "ppc") {
119         $SYS_epoll_create = 236;
120         $SYS_epoll_ctl    = 237;
121         $SYS_epoll_wait   = 238;
122         $u64_mod_8        = 1;
123         $SYS_signalfd4 = 313;
124     } elsif ($machine =~ m/^s390/) {
125         $SYS_epoll_create = 249;
126         $SYS_epoll_ctl    = 250;
127         $SYS_epoll_wait   = 251;
128         $u64_mod_8        = 1;
129         $SYS_signalfd4 = 322;
130     } elsif ($machine eq "ia64") {
131         $SYS_epoll_create = 1243;
132         $SYS_epoll_ctl    = 1244;
133         $SYS_epoll_wait   = 1245;
134         $u64_mod_8        = 1;
135         $SYS_signalfd4 = 289;
136     } elsif ($machine eq "alpha") {
137         # natural alignment, ints are 32-bits
138         $SYS_epoll_create = 407;
139         $SYS_epoll_ctl    = 408;
140         $SYS_epoll_wait   = 409;
141         $u64_mod_8        = 1;
142         $SYS_signalfd4 = 484;
143     } elsif ($machine eq "aarch64") {
144         $SYS_epoll_create = 20;  # (sys_epoll_create1)
145         $SYS_epoll_ctl    = 21;
146         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
147         $u64_mod_8        = 1;
148         $no_deprecated    = 1;
149         $SYS_signalfd4 = 74;
150     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
151         # ARM OABI
152         $SYS_epoll_create = 250;
153         $SYS_epoll_ctl    = 251;
154         $SYS_epoll_wait   = 252;
155         $u64_mod_8        = 1;
156         $SYS_signalfd4 = 355;
157     } elsif ($machine =~ m/^mips64/) {
158         $SYS_epoll_create = 5207;
159         $SYS_epoll_ctl    = 5208;
160         $SYS_epoll_wait   = 5209;
161         $u64_mod_8        = 1;
162         $SYS_signalfd4 = 5283;
163     } elsif ($machine =~ m/^mips/) {
164         $SYS_epoll_create = 4248;
165         $SYS_epoll_ctl    = 4249;
166         $SYS_epoll_wait   = 4250;
167         $u64_mod_8        = 1;
168         $SYS_signalfd4 = 4324;
169     } else {
170         # as a last resort, try using the *.ph files which may not
171         # exist or may be wrong
172         _load_syscall();
173         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
174         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
175         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
176
177         # Note: do NOT add new syscalls to depend on *.ph, here.
178         # Better to miss syscalls (so we can fallback to IO::Poll)
179         # than to use wrong ones, since the names are not stable
180         # (at least not on FreeBSD), if the actual numbers are.
181     }
182
183     if ($u64_mod_8) {
184         *epoll_wait = \&epoll_wait_mod8;
185         *epoll_ctl = \&epoll_ctl_mod8;
186     } else {
187         *epoll_wait = \&epoll_wait_mod4;
188         *epoll_ctl = \&epoll_ctl_mod4;
189     }
190 }
191
192 elsif ($^O eq "freebsd") {
193     if ($ENV{FREEBSD_SENDFILE}) {
194         # this is still buggy and in development
195     }
196 }
197
198 ############################################################################
199 # epoll functions
200 ############################################################################
201
202 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
203
204 sub epoll_create {
205         syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
206 }
207
208 # epoll_ctl wrapper
209 # ARGS: (epfd, op, fd, events_mask)
210 sub epoll_ctl_mod4 {
211     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
212 }
213 sub epoll_ctl_mod8 {
214     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
215 }
216
217 # epoll_wait wrapper
218 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
219 #  arrayref: values modified to be [$fd, $event]
220 our $epoll_wait_events;
221 our $epoll_wait_size = 0;
222 sub epoll_wait_mod4 {
223     # resize our static buffer if requested size is bigger than we've ever done
224     if ($_[1] > $epoll_wait_size) {
225         $epoll_wait_size = $_[1];
226         $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
227     }
228     my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
229     for (0..$ct-1) {
230         @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
231     }
232     return $ct;
233 }
234
235 sub epoll_wait_mod8 {
236     # resize our static buffer if requested size is bigger than we've ever done
237     if ($_[1] > $epoll_wait_size) {
238         $epoll_wait_size = $_[1];
239         $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
240     }
241     my $ct;
242     if ($no_deprecated) {
243         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
244     } else {
245         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
246     }
247     for (0..$ct-1) {
248         # 16 byte epoll_event structs, with format:
249         #    4 byte mask [idx 1]
250         #    4 byte padding (we put it into idx 2, useless)
251         #    8 byte data (first 4 bytes are fd, into idx 0)
252         @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
253     }
254     return $ct;
255 }
256
257 sub signalfd ($$$) {
258         my ($fd, $signos, $flags) = @_;
259         if ($SYS_signalfd4) {
260                 # Not sure if there's a way to get pack/unpack to get the
261                 # contents of POSIX::SigSet to a buffer, but prepping the
262                 # bitmap like one would for select() works:
263                 my $buf = "\0" x 8;
264                 vec($buf, $_ - 1, 1) = 1 for @$signos;
265
266                 syscall($SYS_signalfd4, $fd, $buf, 8, $flags|SFD_CLOEXEC);
267         } else {
268                 $! = ENOSYS;
269                 undef;
270         }
271 }
272
273 1;
274
275 =head1 WARRANTY
276
277 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
278
279 =head1 AUTHORS
280
281 Brad Fitzpatrick <brad@danga.com>