Repository navigation
Expand file tree
/
Copy pathSignals.cpp
More file actions
653 lines (600 loc) · 24.2 KB
/
Copy pathSignals.cpp
File metadata and controls
653 lines (600 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/Pthread/Posix/Common.hpp"
#include "prx/libkernel/Pthread/include/Pthread.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <csignal>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI getpid_nid_postfix(void);
extern "C" Pthread APS5_VABI scePthreadSelf(void);
struct GuestSignalSet {
std::uint32_t bits[4];
};
struct GuestSigaction {
std::uintptr_t handler;
int flags;
GuestSignalSet mask;
};
static_assert(sizeof(GuestSigaction) == 32 && offsetof(GuestSigaction, flags) == 8 && offsetof(GuestSigaction, mask) == 12);
namespace {
using GuestHandler = void (APS5_VABI *)(int);
constexpr int MaxSignal = 128;
constexpr int GuestSigkill = 9;
constexpr int GuestSigstop = 17;
constexpr int GuestSigalrm = 14;
constexpr int SaRestart = 0x2;
constexpr int SaSiginfo = 0x40;
std::atomic<GuestHandler> handlers[MaxSignal + 1]{};
static_assert(std::atomic<GuestHandler>::is_always_lock_free);
std::atomic<std::uint32_t> blockedMask{0};
std::atomic<std::uint32_t> blockedMaskExtra[3]{};
static_assert(std::atomic<std::uint32_t>::is_always_lock_free);
std::mutex registration;
GuestSigaction dispositions[MaxSignal + 1]{};
constexpr int GuestSigblock = 1;
constexpr int GuestSigunblock = 2;
constexpr int GuestSigsetmask = 3;
constexpr int GuestEintr = 4;
constexpr int GuestEagain = 35;
constexpr int GuestSiuser = 0;
struct GuestSiginfo {
std::int32_t signo;
std::int32_t error;
std::int32_t code;
std::int32_t pid;
std::uint32_t uid;
std::int32_t status;
std::uintptr_t address;
std::int32_t value;
std::int32_t valueTail;
std::int64_t band;
std::int32_t spare[7];
std::int32_t tail;
};
static_assert(sizeof(GuestSiginfo) == 80 && offsetof(GuestSiginfo, code) == 8 && offsetof(GuestSiginfo, pid) == 12 &&
offsetof(GuestSiginfo, uid) == 16 && offsetof(GuestSiginfo, address) == 24 && offsetof(GuestSiginfo, value) == 32 &&
offsetof(GuestSiginfo, band) == 40);
struct GuestTimespec {
std::int64_t seconds;
std::int64_t nanoseconds;
};
static_assert(sizeof(GuestTimespec) == 16 && offsetof(GuestTimespec, nanoseconds) == 8);
std::uint32_t SignalBit(int guest) { return 1u << ((guest - 1) % 32); }
std::uint32_t SignalWord(int guest) { return static_cast<std::uint32_t>((guest - 1) / 32); }
bool SignalInSet(const GuestSignalSet& set, int guest) { return (set.bits[SignalWord(guest)] & SignalBit(guest)) != 0; }
bool SetHasSignal(const GuestSignalSet& set) { return (set.bits[0] | set.bits[1] | set.bits[2] | set.bits[3]) != 0; }
bool SignalBlocked(int guest) {
if (guest < 1 || guest > MaxSignal) return false;
const auto word = SignalWord(guest);
const auto mask = word == 0 ? blockedMask.load() : blockedMaskExtra[word - 1].load();
return (mask & SignalBit(guest)) != 0;
}
struct PendingSignals {
std::mutex lock;
std::condition_variable changed;
std::atomic<std::uint32_t> words[4]{};
};
PendingSignals& Pending() {
static auto* state = new PendingSignals();
return *state;
}
void MarkPending(int guest) {
auto& pending = Pending();
{
// under the same lock the waits check the set with: without it a signal raised
// between the check and the wait is lost and the wait never returns
std::lock_guard lock(pending.lock);
pending.words[SignalWord(guest)].fetch_or(SignalBit(guest), std::memory_order_relaxed);
}
pending.changed.notify_all();
}
bool TakePending(int guest) {
auto& pending = Pending();
auto& word = pending.words[SignalWord(guest)];
const std::uint32_t bit = SignalBit(guest);
if ((word.load(std::memory_order_relaxed) & bit) == 0) return false;
return (word.fetch_and(~bit, std::memory_order_relaxed) & bit) != 0;
}
bool PendingInSet(const GuestSignalSet& set, int* guest) {
for (int candidate = 1; candidate <= MaxSignal; ++candidate) {
if (!SignalInSet(set, candidate) || !TakePending(candidate)) continue;
*guest = candidate;
return true;
}
return false;
}
bool PendingInSetPresent(const GuestSignalSet& set) {
for (int candidate = 1; candidate <= MaxSignal; ++candidate) {
if (!SignalInSet(set, candidate)) continue;
if ((Pending().words[SignalWord(candidate)].load(std::memory_order_relaxed) & SignalBit(candidate)) != 0) return true;
}
return false;
}
int NativeSignal(int guest) {
switch (guest) {
case 2: return SIGINT;
case 4: return SIGILL;
case 6: return SIGABRT;
case 8: return SIGFPE;
case 11: return SIGSEGV;
case 15: return SIGTERM;
default: return 0;
}
}
void Dispatch(int native) {
int guest = 0;
for (int candidate : {2, 4, 6, 8, 11, 15})
if (NativeSignal(candidate) == native) { guest = candidate; break; }
if (!guest) return;
#ifdef _WIN32
// Preserve the guest's persistent registration across CRT delivery.
std::signal(native, Dispatch);
#endif
if (SignalBlocked(guest)) {
MarkPending(guest);
return;
}
const auto callback = handlers[guest].load();
if (reinterpret_cast<std::uintptr_t>(callback) > 1) callback(guest);
}
bool Delivered(const GuestSigaction& action) {
return action.handler > 1 && (action.flags & SaSiginfo) == 0;
}
bool Install(int guest, const GuestSigaction& action) {
const int native = NativeSignal(guest);
if (!native) return true;
const auto previous = handlers[guest].exchange(Delivered(action) ? reinterpret_cast<GuestHandler>(action.handler) : nullptr);
auto hostHandler = action.handler == 1 ? SIG_IGN : Delivered(action) ? Dispatch : SIG_DFL;
if (std::signal(native, hostHandler) == SIG_ERR) {
handlers[guest].store(previous);
return false;
}
return true;
}
}
namespace {
GuestSigaction Disposition(int guest) {
std::lock_guard lock(registration);
return dispositions[guest];
}
bool CaughtByGuest(int guest) {
return Disposition(guest).handler > 1;
}
bool TerminatesWithoutHandler(int guest) {
return guest == GuestSigalrm && Disposition(guest).handler == 0;
}
[[noreturn]] void TerminateBySigalrm(const char* cause) {
std::fprintf(stderr, "SIGALRM: %s with the default disposition; the process terminates\n", cause);
std::fflush(stderr);
std::_Exit(128 + GuestSigalrm);
}
bool Deliverable(int guest) {
return NativeSignal(guest) != 0 || CaughtByGuest(guest) || TerminatesWithoutHandler(guest);
}
bool TakePendingUnblocked(int* guest) {
for (int candidate = 1; candidate <= MaxSignal; ++candidate) {
if (SignalBlocked(candidate) || !Deliverable(candidate)) continue;
if (!TakePending(candidate)) continue;
*guest = candidate;
return true;
}
return false;
}
bool DeliverPendingSignal(int guest) {
const GuestSigaction action = Disposition(guest);
if (action.handler == 1) return false;
if (action.handler > 1 && !Delivered(action)) throw std::runtime_error("signal: SA_SIGINFO handlers are not delivered");
if (guest == GuestSigalrm && action.handler == 0) TerminateBySigalrm("a pending SIGALRM was unblocked");
const int native = NativeSignal(guest);
if (native != 0) {
if (std::raise(native) != 0) throw std::runtime_error("signal: delivering the signal to the host failed");
return action.handler > 1;
}
if (action.handler > 1) {
reinterpret_cast<GuestHandler>(action.handler)(guest);
return true;
}
return false;
}
int WaitForPendingInSet(const GuestSignalSet& set, bool waitForever, const GuestTimespec* timeout, int* guest) {
auto& pending = Pending();
std::unique_lock lock(pending.lock);
for (;;) {
if (PendingInSet(set, guest)) return 0;
if (waitForever) {
pending.changed.wait(lock);
continue;
}
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(timeout->seconds) +
std::chrono::nanoseconds(timeout->nanoseconds);
if (pending.changed.wait_until(lock, deadline) == std::cv_status::no_timeout) continue;
return PendingInSet(set, guest) ? 0 : GuestEagain;
}
}
void FillSiginfo(GuestSiginfo* info, int guest) {
*info = {};
info->signo = guest;
info->code = GuestSiuser;
info->pid = getpid_nid_postfix();
}
void WaitForDeliverableSignal() {
auto& pending = Pending();
for (;;) {
int guest = 0;
{
std::unique_lock lock(pending.lock);
if (!TakePendingUnblocked(&guest)) {
pending.changed.wait(lock);
continue;
}
}
if (DeliverPendingSignal(guest)) return;
}
}
extern "C" int APS5_VABI _sigprocmask_nid_postfix(int how, const GuestSignalSet* set, GuestSignalSet* previousSet);
}
namespace {
struct AlarmState {
std::mutex lock;
std::condition_variable changed;
std::optional<std::chrono::steady_clock::time_point> deadline;
std::chrono::microseconds interval{0};
bool threadStarted = false;
};
AlarmState& Alarms() {
static auto* state = new AlarmState();
return *state;
}
std::string DescribeInterval(std::chrono::microseconds interval) {
char text[64];
std::snprintf(text, sizeof(text), "%lld.%06lld s", static_cast<long long>(interval.count() / 1000000),
static_cast<long long>(interval.count() % 1000000));
return text;
}
void ExpireAlarm(std::chrono::microseconds interval) {
std::uintptr_t handler = 0;
{
std::lock_guard lock(registration);
handler = dispositions[GuestSigalrm].handler;
}
if (handler == 1) return;
if (SignalBlocked(GuestSigalrm)) {
MarkPending(GuestSigalrm);
std::atomic_thread_fence(std::memory_order_seq_cst);
if (SignalBlocked(GuestSigalrm) || !TakePending(GuestSigalrm)) return;
handler = Disposition(GuestSigalrm).handler;
if (handler == 1) return;
}
if (handler == 0) TerminateBySigalrm("the alarm expired");
throw std::runtime_error("ITIMER_REAL (it_interval " + DescribeInterval(interval) + ") expired: delivering SIGALRM to a guest handler is not implemented");
}
void RunAlarms() {
auto& alarms = Alarms();
std::unique_lock lock(alarms.lock);
for (;;) {
if (!alarms.deadline) {
alarms.changed.wait(lock);
continue;
}
const auto deadline = *alarms.deadline;
if (alarms.changed.wait_until(lock, deadline) != std::cv_status::timeout || alarms.deadline != deadline) continue;
const auto interval = alarms.interval;
if (interval.count() == 0) alarms.deadline.reset();
else {
const auto now = std::chrono::steady_clock::now();
const auto periods = now < deadline ? 1 : (now - deadline) / interval + 1;
alarms.deadline = deadline + periods * interval;
}
lock.unlock();
ExpireAlarm(interval);
lock.lock();
}
}
std::chrono::microseconds RemainingAlarm(const AlarmState& alarms, std::chrono::steady_clock::time_point now) {
if (!alarms.deadline || *alarms.deadline <= now) return std::chrono::microseconds(0);
return std::chrono::duration_cast<std::chrono::microseconds>(*alarms.deadline - now);
}
void NotifyAlarms(AlarmState& alarms) {
if (!alarms.threadStarted) {
std::thread(RunAlarms).detach();
alarms.threadStarted = true;
}
alarms.changed.notify_all();
}
}
extern "C" unsigned int APS5_VABI GuestAlarm_nid_no_patch(unsigned int seconds) {
auto& alarms = Alarms();
std::lock_guard lock(alarms.lock);
const auto now = std::chrono::steady_clock::now();
const auto micros = RemainingAlarm(alarms, now).count();
const auto remaining = static_cast<unsigned int>((micros + 999999) / 1000000);
alarms.interval = std::chrono::microseconds(0);
if (seconds == 0) alarms.deadline.reset();
else alarms.deadline = now + std::chrono::seconds(seconds);
NotifyAlarms(alarms);
return remaining;
}
struct GuestItimerval {
KernelTimeval interval;
KernelTimeval value;
};
static_assert(sizeof(GuestItimerval) == 32 && offsetof(GuestItimerval, value) == 16);
extern "C" int APS5_VABI getitimer_nid_postfix(int which, GuestItimerval* output) {
if (which < 0 || which > 2) {
*__error_nid_postfix() = 22;
return -1;
}
if (output == nullptr) {
*__error_nid_postfix() = 14;
return -1;
}
if (which != 0) {
*output = {};
return 0;
}
GuestItimerval result{};
auto& alarms = Alarms();
{
std::lock_guard lock(alarms.lock);
const auto now = std::chrono::steady_clock::now();
if (alarms.deadline && *alarms.deadline > now) {
const auto micros = std::chrono::duration_cast<std::chrono::microseconds>(*alarms.deadline - now).count();
result.value.tv_sec = micros / 1000000;
result.value.tv_usec = micros % 1000000;
}
const auto interval = alarms.interval.count();
result.interval.tv_sec = interval / 1000000;
result.interval.tv_usec = interval % 1000000;
}
*output = result;
return 0;
}
namespace {
constexpr int ItimerReal = 0;
constexpr int ItimerProf = 2;
constexpr std::int64_t MaxItimerSeconds = 0x7fffffff / 2;
constexpr std::int64_t MinExactIntervalMicros = 1000;
bool ValidItimer(const KernelTimeval& time) {
return time.tv_sec >= 0 && time.tv_usec >= 0 && time.tv_usec < 1000000 && time.tv_sec <= MaxItimerSeconds;
}
bool ItimerSet(const KernelTimeval& time) {
return time.tv_sec != 0 || time.tv_usec != 0;
}
std::chrono::microseconds ItimerDuration(const KernelTimeval& time) {
return std::chrono::seconds(time.tv_sec) + std::chrono::microseconds(time.tv_usec);
}
KernelTimeval ItimerTimeval(std::chrono::microseconds duration) {
return {duration.count() / 1000000, duration.count() % 1000000};
}
}
extern "C" int APS5_VABI setitimer_nid_postfix(int which, const GuestItimerval* value, GuestItimerval* previous) {
if (which < ItimerReal || which > ItimerProf) { *__error_nid_postfix() = 22; return -1; }
if (value == nullptr && previous == nullptr) { *__error_nid_postfix() = 14; return -1; }
GuestItimerval requested{};
if (value != nullptr) {
requested = *value;
if (!ValidItimer(requested.value)) { *__error_nid_postfix() = 22; return -1; }
if (!ItimerSet(requested.value)) requested.interval = {};
else if (!ValidItimer(requested.interval)) { *__error_nid_postfix() = 22; return -1; }
}
if (which != ItimerReal) {
if (ItimerSet(requested.value))
throw std::runtime_error(std::string("setitimer: ") + (which == ItimerProf ? "ITIMER_PROF" : "ITIMER_VIRTUAL") + " needs process CPU time accounting, which is not implemented");
if (previous != nullptr) *previous = {};
return 0;
}
if (requested.interval.tv_sec == 0 && requested.interval.tv_usec != 0 && requested.interval.tv_usec < MinExactIntervalMicros)
throw std::runtime_error("setitimer: it_interval " + DescribeInterval(ItimerDuration(requested.interval)) + " is below 1 ms, where the kernel rounds it up to a clock tick that is not known");
GuestItimerval old{};
{
auto& alarms = Alarms();
std::lock_guard lock(alarms.lock);
const auto now = std::chrono::steady_clock::now();
old.interval = ItimerTimeval(alarms.interval);
old.value = ItimerTimeval(RemainingAlarm(alarms, now));
if (value != nullptr) {
alarms.interval = ItimerDuration(requested.interval);
if (ItimerSet(requested.value)) alarms.deadline = now + ItimerDuration(requested.value);
else alarms.deadline.reset();
NotifyAlarms(alarms);
}
}
if (previous != nullptr) *previous = old;
return 0;
}
struct GuestStack {
void* sp;
std::size_t size;
int flags;
};
static_assert(sizeof(GuestStack) == 24 && offsetof(GuestStack, flags) == 16);
namespace {
constexpr int SsDisable = 4;
constexpr std::size_t MinSignalStackSize = 2048;
thread_local GuestStack alternateStack{nullptr, 0, SsDisable};
}
extern "C" {
GuestHandler APS5_VABI signal_nid_postfix(int guest, GuestHandler handler) {
const auto invalid = reinterpret_cast<GuestHandler>(static_cast<std::uintptr_t>(-1));
if (guest < 1 || guest > MaxSignal || ((guest == GuestSigkill || guest == GuestSigstop) && handler != nullptr)) { *__error_nid_postfix() = 22; return invalid; }
std::lock_guard lock(registration);
const GuestSigaction action{reinterpret_cast<std::uintptr_t>(handler), SaRestart, {}};
if (!Install(guest, action)) { *__error_nid_postfix() = 22; return invalid; }
const auto previous = reinterpret_cast<GuestHandler>(dispositions[guest].handler);
dispositions[guest] = action;
return previous;
}
int APS5_VABI sigaction_nid_postfix(int guest, const GuestSigaction* action, GuestSigaction* previous) {
if (guest < 1 || guest > MaxSignal) { *__error_nid_postfix() = 22; return -1; }
if (action && (guest == GuestSigkill || guest == GuestSigstop) && action->handler != 0) { *__error_nid_postfix() = 22; return -1; }
std::lock_guard lock(registration);
const GuestSigaction old = dispositions[guest];
if (action) {
if (!Install(guest, *action)) { *__error_nid_postfix() = 22; return -1; }
dispositions[guest] = *action;
}
if (previous) *previous = old;
return 0;
}
int APS5_VABI raise_nid_postfix(int guest) {
if (guest < 1 || guest > MaxSignal) { *__error_nid_postfix() = 22; return -1; }
const GuestSigaction action = Disposition(guest);
if (action.handler > 1 && !Delivered(action)) throw std::runtime_error("raise: SA_SIGINFO handlers are not delivered");
if (SignalBlocked(guest)) {
if (action.handler != 1) MarkPending(guest);
return 0;
}
const int native = NativeSignal(guest);
if (native != 0) {
const int result = std::raise(native);
if (result) *__error_nid_postfix() = 22;
return result ? -1 : 0;
}
if (!CaughtByGuest(guest)) { *__error_nid_postfix() = 22; return -1; }
DeliverPendingSignal(guest);
return 0;
}
int APS5_VABI kill_nid_postfix(int pid, int guest) {
if (guest < 0 || guest > MaxSignal) { *__error_nid_postfix() = 22; return -1; }
const int self = getpid_nid_postfix();
if (pid != self && pid != 0 && pid != -self) { *__error_nid_postfix() = 3; return -1; }
return guest == 0 ? 0 : raise_nid_postfix(guest);
}
int APS5_VABI killpg_nid_postfix(int pgrp, int guest) {
if (pgrp < 0) { *__error_nid_postfix() = 22; return -1; }
return kill_nid_postfix(-pgrp, guest);
}
int APS5_VABI pthread_kill_nid_postfix(Pthread thread, int guest) {
if (guest < 0 || guest > MaxSignal) return PosixThread::GUEST_EINVAL;
if (thread == nullptr) return PosixThread::GUEST_ESRCH;
if (guest == 0) return 0;
if (thread == scePthreadSelf()) return raise_nid_postfix(guest) == 0 ? 0 : *__error_nid_postfix();
if (!GuestThreadIsLive(thread)) return PosixThread::GUEST_ESRCH;
if (Disposition(guest).handler != 1) MarkPending(guest);
return 0;
}
int APS5_VABI sigaltstack_nid_postfix(const GuestStack* stack, GuestStack* previous) {
GuestStack replacement = alternateStack;
if (stack != nullptr) {
if ((stack->flags & ~SsDisable) != 0) { *__error_nid_postfix() = 22; return -1; }
if (stack->flags & SsDisable) replacement.flags = SsDisable;
else if (stack->size < MinSignalStackSize) { *__error_nid_postfix() = 12; return -1; }
else replacement = {stack->sp, stack->size, 0};
}
if (previous != nullptr) *previous = alternateStack;
alternateStack = replacement;
return 0;
}
int APS5_VABI _sigprocmask_nid_postfix(int how, const GuestSignalSet* set, GuestSignalSet* previousSet) {
{
std::lock_guard lock(registration);
if (previousSet != nullptr) {
previousSet->bits[0] = blockedMask.load();
for (int word = 1; word < 4; ++word) previousSet->bits[word] = blockedMaskExtra[word - 1].load();
}
if (set != nullptr) {
switch (how) {
case 1:
blockedMask.fetch_or(set->bits[0]);
for (int word = 1; word < 4; ++word) blockedMaskExtra[word - 1].fetch_or(set->bits[word]);
break;
case 2:
blockedMask.fetch_and(~set->bits[0]);
for (int word = 1; word < 4; ++word) blockedMaskExtra[word - 1].fetch_and(~set->bits[word]);
break;
case 3:
blockedMask.store(set->bits[0]);
for (int word = 1; word < 4; ++word) blockedMaskExtra[word - 1].store(set->bits[word]);
break;
default: throw std::invalid_argument("_sigprocmask: invalid how");
}
}
}
std::atomic_thread_fence(std::memory_order_seq_cst);
for (int guest = 1; guest <= MaxSignal; ++guest) {
if (SignalBlocked(guest) || !Deliverable(guest) || !TakePending(guest)) continue;
DeliverPendingSignal(guest);
}
return 0;
}
int APS5_VABI sigprocmask_nid_postfix(int how, const void* set, void* previousSet) {
return _sigprocmask_nid_postfix(how, static_cast<const GuestSignalSet*>(set),
static_cast<GuestSignalSet*>(previousSet));
}
int APS5_VABI pthread_sigmask_nid_postfix(int how, const GuestSignalSet* set, GuestSignalSet* previousSet) {
if (set != nullptr && (how < 1 || how > 3)) return 22;
return _sigprocmask_nid_postfix(how, set, previousSet);
}
int APS5_VABI sigtimedwait_nid_postfix(const GuestSignalSet* set, void* info, const GuestTimespec* timeout) {
if (set == nullptr || !SetHasSignal(*set)) { *__error_nid_postfix() = 22; return -1; }
if (timeout != nullptr && (timeout->nanoseconds < 0 || timeout->nanoseconds >= 1000000000) && !PendingInSetPresent(*set)) {
*__error_nid_postfix() = 22;
return -1;
}
int guest = 0;
const int result = WaitForPendingInSet(*set, timeout == nullptr, timeout, &guest);
if (result != 0) { *__error_nid_postfix() = result; return -1; }
if (info != nullptr) FillSiginfo(static_cast<GuestSiginfo*>(info), guest);
return guest;
}
int APS5_VABI sigwaitinfo_nid_postfix(const GuestSignalSet* set, void* info) {
if (set == nullptr || !SetHasSignal(*set)) { *__error_nid_postfix() = 22; return -1; }
int guest = 0;
if (WaitForPendingInSet(*set, true, nullptr, &guest) != 0) { *__error_nid_postfix() = GuestEagain; return -1; }
if (info != nullptr) FillSiginfo(static_cast<GuestSiginfo*>(info), guest);
return guest;
}
int APS5_VABI sigwait_nid_postfix(const GuestSignalSet* set, int* received) {
if (set == nullptr || received == nullptr || !SetHasSignal(*set)) return 22;
int guest = 0;
const int result = WaitForPendingInSet(*set, true, nullptr, &guest);
if (result != 0) return result;
*received = guest;
return 0;
}
int APS5_VABI pause_nid_postfix(void) {
WaitForDeliverableSignal();
*__error_nid_postfix() = GuestEintr;
return -1;
}
int APS5_VABI sigsuspend_nid_postfix(const GuestSignalSet* set) {
if (set == nullptr) throw std::invalid_argument("sigsuspend: null mask");
GuestSignalSet previous{{0, 0, 0, 0}};
{
std::lock_guard lock(registration);
previous.bits[0] = blockedMask.exchange(set->bits[0]);
for (int word = 1; word < 4; ++word) previous.bits[word] = blockedMaskExtra[word - 1].exchange(set->bits[word]);
}
WaitForDeliverableSignal();
_sigprocmask_nid_postfix(GuestSigsetmask, &previous, nullptr);
*__error_nid_postfix() = GuestEintr;
return -1;
}
int APS5_VABI sigpending_nid_postfix(GuestSignalSet* set) {
if (set == nullptr) { *__error_nid_postfix() = 14; return -1; }
auto& pending = Pending();
std::lock_guard lock(pending.lock);
set->bits[0] = pending.words[0].load(std::memory_order_relaxed) & blockedMask.load();
for (int word = 1; word < 4; ++word)
set->bits[word] = pending.words[word].load(std::memory_order_relaxed) & blockedMaskExtra[word - 1].load();
return 0;
}
}
extern "C" {
int APS5_VABI _is_signal_return_nid_postfix(std::uint64_t programCounter) {
(void)programCounter;
return 0;
}
}