20 #include "signal-syscalls.h" 21 #include "file-syscalls.h" 22 #include "pedigree/kernel/Log.h" 23 #include "pedigree/kernel/syscallError.h" 24 #include "pthread-syscalls.h" 25 #include "system-syscalls.h" 27 #include "pedigree/kernel/process/PerProcessorScheduler.h" 28 #include "pedigree/kernel/process/Scheduler.h" 29 #include "pedigree/kernel/processor/PhysicalMemoryManager.h" 31 #define MACHINE_FORWARD_DECL_ONLY 32 #include "pedigree/kernel/machine/Machine.h" 33 #include "pedigree/kernel/machine/Timer.h" 35 #include "pedigree/kernel/Subsystem.h" 36 #include <PosixProcess.h> 37 #include <PosixSubsystem.h> 42 extern void sigret_stub();
43 extern char sigret_stub_end;
46 static int doProcessKill(
Process *p,
int sig);
47 static int doThreadKill(
Thread *p,
int sig);
51 #define SIGNAL_HANDLER_EXIT(name, errcode) \ 52 static void name(int) NORETURN; \ 53 static void name(int) \ 55 posix_exit(errcode); \ 57 #define SIGNAL_HANDLER_EMPTY(name) \ 58 static void name(int s) \ 60 NOTICE("EMPTY handler."); \ 62 #define SIGNAL_HANDLER_EXITMSG(name, errcode, msg) \ 63 static void name(int) NORETURN; \ 64 static void name(int) \ 66 Processor::setInterrupts(true); \ 67 posix_write(1, msg, StringLength(msg), true); \ 68 Scheduler::instance().yield(); \ 69 posix_exit(errcode); \ 71 #define SIGNAL_HANDLER_SUSPEND(name) \ 72 static void name(int s) \ 75 Processor::information().getCurrentThread()->getParent(); \ 77 "SUSPEND [pid=" << pParent->getId() << ", signal " << s << "]"); \ 80 #define SIGNAL_HANDLER_RESUME(name) \ 81 static void name(int s) \ 83 NOTICE("RESUME [signal " << s << "]"); \ 84 Processor::information().getCurrentThread()->getParent()->resume(); \ 87 static char SSIGILL[] =
"Illegal instruction.\n";
88 static char SSIGSEGV[] =
"Segmentation fault.\n";
89 static char SSIGBUS[] =
"Bus error.\n";
90 static char SSIGABRT[] =
"Abort.\n";
92 SIGNAL_HANDLER_EXITMSG(sigabrt, SIGABRT, SSIGABRT)
93 SIGNAL_HANDLER_EXIT(sigalrm, SIGALRM)
94 SIGNAL_HANDLER_EXITMSG(sigbus, SIGBUS, SSIGBUS)
95 SIGNAL_HANDLER_EMPTY(sigchld)
96 SIGNAL_HANDLER_RESUME(sigcont)
97 SIGNAL_HANDLER_EXIT(sigfpe, SIGFPE)
98 SIGNAL_HANDLER_EXIT(sighup, SIGHUP)
99 SIGNAL_HANDLER_EXITMSG(sigill, SIGILL, SSIGILL)
100 SIGNAL_HANDLER_EXIT(sigint, SIGINT)
101 SIGNAL_HANDLER_EXIT(sigkill, SIGKILL)
102 SIGNAL_HANDLER_EXIT(sigpipe, SIGPIPE)
103 SIGNAL_HANDLER_EXIT(sigquit, SIGQUIT)
104 SIGNAL_HANDLER_EXITMSG(sigsegv, SIGSEGV, SSIGSEGV)
105 SIGNAL_HANDLER_SUSPEND(sigstop)
106 SIGNAL_HANDLER_EXIT(sigterm, SIGTERM)
107 SIGNAL_HANDLER_SUSPEND(sigtstp)
108 SIGNAL_HANDLER_SUSPEND(sigttin)
109 SIGNAL_HANDLER_SUSPEND(sigttou)
110 SIGNAL_HANDLER_EMPTY(sigusr1)
111 SIGNAL_HANDLER_EMPTY(sigusr2)
112 SIGNAL_HANDLER_EMPTY(sigurg)
114 SIGNAL_HANDLER_EMPTY(sigign);
116 static _sig_func_ptr default_sig_handlers[32] = {
152 int sig,
const struct sigaction *act,
struct sigaction *oact)
155 "sigaction(" <<
Dec << sig <<
Hex <<
", " 156 << reinterpret_cast<uintptr_t>(act) <<
", " 157 << reinterpret_cast<uintptr_t>(oact) <<
")");
159 reinterpret_cast<uintptr_t>(act),
sizeof(
struct sigaction),
160 PosixSubsystem::SafeRead)) ||
162 reinterpret_cast<uintptr_t>(oact),
163 sizeof(
struct sigaction), PosixSubsystem::SafeWrite)))
165 SYSCALL_ERROR(InvalidArgument);
175 ERROR(
"posix_sigaction: no subsystem");
180 if ((sig > 32) || (sig == SIGKILL || sig == SIGSTOP))
182 SYSCALL_ERROR(InvalidArgument);
192 if (oldSignalHandler)
194 oact->sa_flags = oldSignalHandler->
flags;
196 if (oldSignalHandler->
type == 0)
197 oact->sa_handler =
reinterpret_cast<void (*)(
int)
>(
199 else if (oldSignalHandler->
type == 1)
200 oact->sa_handler =
reinterpret_cast<void (*)(
int)
>(0);
201 else if (oldSignalHandler->
type == 2)
202 oact->sa_handler =
reinterpret_cast<void (*)(
int)
>(1);
205 ByteSet(oact, 0,
sizeof(
struct sigaction));
214 sigHandler->
flags = act->sa_flags;
216 uintptr_t newHandler =
reinterpret_cast<uintptr_t
>(act->sa_handler);
219 SG_NOTICE(
" + SIG_DFL");
220 newHandler =
reinterpret_cast<uintptr_t
>(default_sig_handlers[sig]);
221 sigHandler->
type = 1;
223 else if (newHandler == 1)
225 SG_NOTICE(
" + SIG_IGN");
226 newHandler =
reinterpret_cast<uintptr_t
>(sigign);
227 sigHandler->
type = 2;
229 else if (static_cast<int>(newHandler) == -1)
231 SG_NOTICE(
" + Invalid");
233 SYSCALL_ERROR(InvalidArgument);
239 sigHandler->
type = 0;
243 new SignalEvent(newHandler, static_cast<size_t>(sig));
245 "Creating the event (" 246 << reinterpret_cast<uintptr_t>(sigHandler->
pEvent) <<
").");
252 SYSCALL_ERROR(InvalidArgument);
258 uintptr_t posix_signal(
int sig,
void *func)
260 ERROR(
"signal called but glue signal should redirect to sigaction");
264 int posix_raise(
int sig, SyscallState &State)
275 ERROR(
"posix_raise: no subsystem");
288 if (signalHandler->
pEvent)
292 uintptr_t stackPointer = State.getStackPointer();
296 stackPointer = (currStack.
base + currStack.
size) - 1;
307 int pedigree_sigret()
309 SG_NOTICE(
"pedigree_sigret");
321 currStack.
inUse =
false;
326 FATAL(
"eventHandlerReturned() returned!");
329 void pedigree_unwind_signal()
331 SG_NOTICE(
"pedigree_unwind_signal");
338 static int doThreadKill(
Thread *p,
int sig)
343 if (!(sig == SIGKILL || sig == SIGCONT))
345 WARNING(
"kill: can't send anything other than SIGKILL or SIGCONT " 346 "to a suspended process.");
360 "posix_kill: no subsystem on process " << p->
getParent()->
getId());
365 if (signalHandler && signalHandler->
pEvent)
384 static int doProcessKill(
Process *p,
int sig)
386 return doThreadKill(p->
getThread(0), sig);
389 int posix_kill(
int pid,
int sig)
391 SG_NOTICE(
"kill(" << pid <<
", " << sig <<
")");
398 ProcessGroup *pThisGroup = pThisProcess->getProcessGroup();
400 bool bKillingSelf =
false;
411 if (static_cast<int>(pProcess->
getId()) == pid)
416 else if ((pid <= 0) && (pProcess->
getType() == Process::Posix))
419 ProcessGroup *pGroup = pPosixProcess->getProcessGroup();
423 if (!(pGroup && pThisGroup))
428 if (pGroup != pThisGroup)
430 SC_NOTICE(
" -> same group IDs but different groups??");
434 " -> killing process " << pProcess->
getId() <<
" in group [" 441 if (pProcess->
getParent() != pThisProcess)
450 else if ((pid > 0) && (
static_cast<int>(pProcess->
getId()) != pid))
452 else if (pProcess->
getType() != Process::Posix)
465 if (processList.
count() == 0)
467 SYSCALL_ERROR(NoSuchProcess);
468 SG_NOTICE(
" -> no such process");
474 it != processList.
end(); ++it)
477 if (member != pThisProcess)
480 " -> not killing current process, killing " << member->
getId());
482 "sending #" <<
Dec << member->
getId() <<
" signal #" << sig
483 <<
" from #" << pThisProcess->
getId());
484 doProcessKill(member, sig);
489 " -> killing current process (" << pThisProcess->
getId()
500 SG_NOTICE(
"performing kill of " << pThisProcess->
getId() <<
"...");
502 "sending self #" <<
Dec << pThisProcess->
getId() <<
" signal #" 504 doProcessKill(pThisProcess, sig);
515 int posix_sigprocmask(
int how,
const uint32_t *
set, uint32_t *oset)
520 size_t posix_alarm(uint32_t seconds)
522 SG_NOTICE(
"alarm(" << seconds <<
")");
531 ERROR(
"posix_alarm: no subsystem");
567 int posix_sleep(uint32_t seconds)
572 Time::delay(seconds * Time::Multiplier::Second);
583 uint64_t elapsed = endTick - startTick;
584 uint32_t elapsedSecs =
static_cast<uint32_t
>(elapsed / 1000) + 1;
586 if (elapsedSecs >= seconds)
595 int posix_usleep(
size_t useconds)
599 Time::delay(useconds * Time::Multiplier::Microsecond);
608 int posix_nanosleep(
const struct timespec *rqtp,
struct timespec *rmtp)
611 reinterpret_cast<uintptr_t>(rqtp),
sizeof(
struct timespec),
612 PosixSubsystem::SafeRead)) ||
614 reinterpret_cast<uintptr_t>(rmtp),
sizeof(
struct timespec),
615 PosixSubsystem::SafeWrite)))
617 SG_NOTICE(
"nanosleep -> invalid address");
618 SYSCALL_ERROR(InvalidArgument);
623 "nanosleep(" <<
Dec << rqtp->tv_sec <<
":" << rqtp->tv_nsec <<
Hex 624 <<
") - " << Machine::instance().getTimer()->getTickCount()
628 (rqtp->tv_sec * Time::Multiplier::Second) + rqtp->tv_nsec;
633 rmtp->tv_nsec = rqtp->tv_nsec;
634 rmtp->tv_sec = rqtp->tv_sec;
644 int posix_clock_gettime(clockid_t clock_id,
struct timespec *tp)
646 SG_NOTICE(
"clock_gettime");
648 reinterpret_cast<uintptr_t>(tp),
sizeof(
struct timespec),
649 PosixSubsystem::SafeWrite))
651 SYSCALL_ERROR(InvalidArgument);
660 tp->tv_nsec =
static_cast<time_t
>(
669 int posix_sigaltstack(
const stack_t *stack, stack_t *oldstack)
674 if (!stack && !oldstack)
676 SYSCALL_ERROR(InvalidArgument);
679 if (stack && (stack->ss_size < MINSIGSTKSZ))
681 SYSCALL_ERROR(OutOfMemory);
698 SG_NOTICE(
"Can't set new alternate signal stack as it's the one we're " 700 SYSCALL_ERROR(InvalidArgument);
707 oldstack->ss_sp =
reinterpret_cast<void *
>(currStack.
base);
708 oldstack->ss_size = currStack.
size;
709 oldstack->ss_flags = (currStack.
inUse ? SA_ONSTACK : 0);
715 currStack.
base =
reinterpret_cast<uintptr_t
>(stack->ss_sp);
716 currStack.
size = stack->ss_size;
724 void pedigree_init_sigret()
726 SG_NOTICE(
"init_sigret");
727 static physical_uintptr_t sigretPhys = 0;
743 reinterpret_cast<void *>(sigret_stub),
744 (reinterpret_cast<uintptr_t>(&sigret_stub_end) -
745 reinterpret_cast<uintptr_t>(sigret_stub)));
770 pProcess->setSubsystem(pSubsystem);
774 for (
size_t i = 0; i < 32; i++)
778 int signalDisposition = 1;
784 if (existingHandler->
type == 2)
786 signalDisposition = 2;
796 sigHandler->
type = signalDisposition;
798 uintptr_t newHandler =
799 signalDisposition == 1 ?
800 reinterpret_cast<uintptr_t
>(default_sig_handlers[i]) :
801 reinterpret_cast<uintptr_t>(sigign);
808 SG_NOTICE(
"Creating initial set of signal handlers is complete");
void pushBack(const T &value)
virtual void removeAlarm(class Event *pEvent)=0
static PhysicalMemoryManager & instance()
static bool getInterrupts()
void popState(bool clean=true)
static uintptr_t getTrampoline()
virtual void pin(physical_uintptr_t page)=0
virtual Timer * getTimer()=0
virtual physical_uintptr_t allocatePage(size_t pageConstraints=0)=0
size_t size
Size of the stack.
static const size_t Execute
static ProcessorInformation & information()
virtual Time::Timestamp getUnixTimestamp()
static bool checkAddress(uintptr_t addr, size_t extent, size_t flags)
static const size_t Write
uintptr_t getHandlerAddress()
bool sendEvent(Event *pEvent)
uint32_t flags
Signal handler flags.
SignalHandler * getSignalHandler(size_t sig)
virtual uint64_t getTickCount()=0
static const size_t Shared
int type
Type - 0 = normal, 1 = SIG_DFL, 2 = SIG_IGN.
SignalEvent * pEvent
Event for the signal handler.
static Scheduler & instance()
Process * getProcess(size_t n)
static void setInterrupts(bool bEnable)
uintptr_t base
The location of this stack.
bool inUse
Are we to use this alternate stack rather than a normal stack?
Thread * getThread(size_t n)
Process * getParent() const
void setSignalHandler(size_t sig, SignalHandler *handler)
virtual ProcessType getType()
virtual void addAlarm(class Event *pEvent, size_t alarmSecs, size_t alarmUsecs=0)=0
virtual void setProcess(Process *p)
AlternateSignalStack & getAlternateSignalStack()