summaryrefslogtreecommitdiff
path: root/Kernel/Thread.h
AgeCommit message (Collapse)Author
2020-12-02Kernel: Add CLOCK_REALTIME support to the TimerQueueTom
This allows us to use blocking timeouts with either monotonic or real time for all blockers. Which means that clock_nanosleep() now also supports CLOCK_REALTIME. Also, switch alarm() to use CLOCK_REALTIME as per specification.
2020-12-02Kernel: TimerQueue::cancel_timer needs to wait if timer is executingTom
We need to be able to guarantee that a timer won't be executing after TimerQueue::cancel_timer returns. In the case of multiple processors this means that we may need to wait while the timer handler finishes execution on another core. This also fixes a problem in Thread::block and Thread::wait_on where theoretically the timer could execute after the function returned and the Thread disappeared.
2020-12-01Kernel: Fix some problems with Thread::wait_on and LockTom
This changes the Thread::wait_on function to not enable interrupts upon leaving, which caused some problems with page fault handlers and in other situations. It may now be called from critical sections, with interrupts enabled or disabled, and returns to the same state. This also requires some fixes to Lock. To aid debugging, a new define LOCK_DEBUG is added that enables checking for Lock leaks upon finalization of a Thread.
2020-11-30Kernel: Move block condition evaluation out of the SchedulerTom
This makes the Scheduler a lot leaner by not having to evaluate block conditions every time it is invoked. Instead evaluate them as the states change, and unblock threads at that point. This also implements some more waitid/waitpid/wait features and behavior. For example, WUNTRACED and WNOWAIT are now supported. And wait will now not return EINTR when SIGCHLD is delivered at the same time.
2020-11-30Kernel: Allow passing a thread argument for new kernel threadsTom
This adds the ability to pass a pointer to kernel thread/process. Also add the ability to use a closure as thread function, which allows passing information to a kernel thread more easily.
2020-11-30Kernel: Move some time related code from Scheduler into TimeManagementTom
Use the TimerQueue to expire blocking operations, which is one less thing the Scheduler needs to check on every iteration. Also, add a BlockTimeout class that will automatically handle relative or absolute timeouts as well as overriding timeouts (e.g. socket timeouts) more consistently. Also, rework the TimerQueue class to be able to fire events from any processor, which requires Timer to be RefCounted. Also allow creating id-less timers for use by blocking operations.
2020-11-22Kernel: Make CLOCK_MONOTONIC respect the system tick frequencyAndreas Kling
The time returned by sys$clock_gettime() was not aligned with the delay calculations in sys$clock_nanosleep(). This patch fixes that by taking the system's ticks_per_second value into account in both functions. This patch also removes the need for Thread::sleep_until() and uses Thread::sleep() for both absolute and relative sleeps. This was causing the nesalizer emulator port to sleep for a negative amount of time at the end of each frame, making it run way too fast.
2020-10-26Kernel: Fix a few deadlocks with Thread::m_lock and g_scheduler_lockTom
g_scheduler_lock cannot safely be acquired after Thread::m_lock because another processor may already hold g_scheduler_lock and wait for the same Thread::m_lock.
2020-09-27Kernel: Make Thread refcountedTom
Similar to Process, we need to make Thread refcounted. This will solve problems that will appear once we schedule threads on more than one processor. This allows us to hold onto threads without necessarily holding the scheduler lock for the entire duration.
2020-09-26Kernel: Allow killing queued threadsTom
We need to dequeue and wake threads that are waiting if the process terminates. Fixes #3603 without the HackStudio fixes in #3606.
2020-09-26Kernel: Fix thread joining issuesTom
The thread joining logic hadn't been updated to account for the subtle differences introduced by software context switching. This fixes several race conditions related to thread destruction and joining, as well as finalization which did not properly account for detached state and the fact that threads can be joined after termination as long as they're not detached. Fixes #3596
2020-09-16Kernel: Return ENOMEM in more placesLuke
There are plenty of places in the kernel that aren't checking if they actually got their allocation. This fixes some of them, but definitely not all. Fixes #3390 Fixes #3391 Also, let's make find_one_free_page() return nullptr if it doesn't get a free index. This stops the kernel crashing when out of memory and allows memory purging to take place again. Fixes #3487
2020-09-13Kernel: Make copy_to/from_user safe and remove unnecessary checksTom
Since the CPU already does almost all necessary validation steps for us, we don't really need to attempt to do this. Doing it ourselves doesn't really work very reliably, because we'd have to account for other processors modifying virtual memory, and we'd have to account for e.g. pages not being able to be allocated due to insufficient resources. So change the copy_to/from_user (and associated helper functions) to use the new safe_memcpy, which will return whether it succeeded or not. The only manual validation step needed (which the CPU can't perform for us) is making sure the pointers provided by user mode aren't pointing to kernel mappings. To make it easier to read/write from/to either kernel or user mode data add the UserOrKernelBuffer helper class, which will internally either use copy_from/to_user or directly memcpy, or pass the data through directly using a temporary buffer on the stack. Last but not least we need to keep syscall params trivial as we need to copy them from/to user mode using copy_from/to_user.
2020-09-12Kernel: Fix various forward declarationsBen Wiederhake
I decided to modify MappedROM.h because all other entried in Forward.h are also classes, and this is visually more pleasing. Other than that, it just doesn't make any difference which way we resolve the conflicts.
2020-09-09Kernel: Keep signal state in syncTom
In c3d231616c1d20309b2b568f383fbcb736887dad we added the atomic variable m_have_any_unmasked_pending_signals tracking the state of pending signals. Add helper functions that automatically update this variable as needed.
2020-09-07Kernel: Fix crash when delivering signal to barely created threadTom
We need to wait until a thread is fully set up and ready for running before attempting to deliver a signal. Otherwise we may not have a user stack yet. Also, remove the Skip0SchedulerPasses and Skip1SchedulerPass thread states that we don't really need anymore with software context switching. Fixes the kernel crash reported in #3419
2020-08-19Kernel: Remove an unimplemented function (#3210)Muhammad Zahalqa
2020-08-16AK: Rename KB, MB, GB to KiB, MiB, GiBNico Weber
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9". The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30". Let's use the correct name, at least in code. Only changes the name of the constants, no other behavior change.
2020-08-15Kernel: Briefly resume stopped threads when being killedTom
We need to briefly put Stopped threads back into Running state so that the kernel stacks can get cleaned up when they're being killed. Fixes #3130
2020-08-11Kernel: Always return from Thread::wait_onTom
We need to always return from Thread::wait_on, even when a thread is being killed. This is necessary so that the kernel call stack can clean up and release references held by it. Then, right before transitioning back to user mode, we check if the thread is supposed to die, and at that point change the thread state to Dying to prevent further scheduling of this thread. This addresses some possible resource leaks similar to #3073
2020-08-10Kernel: More PID/TID typingBen Wiederhake
2020-08-10Kernel: PID/TID typingBen Wiederhake
This compiles, and contains exactly the same bugs as before. The regex 'FIXME: PID/' should reveal all markers that I left behind, including: - Incomplete conversion - Issues or things that look fishy - Actual bugs that will go wrong during runtime
2020-08-06Kernel: Dequeue dying threads from WaitQueueTom
If a thread is waiting but getting killed, we need to dequeue the thread from the WaitQueue so that a potential wake before finalization doesn't happen.
2020-08-03Kernel: Consolidate timeout logicTom
Allow passing in an optional timeout to Thread::block and move the timeout check out of Thread::Blocker. This way all Blockers implicitly support timeouts and don't need to implement it themselves. Do however allow them to override timeouts (e.g. for sockets).
2020-08-03Kernel: Fix a few Thread::block related racesTom
We need to have a Thread lock to protect threading related operations, such as Thread::m_blocker which is used in Thread::block. Also, if a Thread::Blocker indicates that it should be unblocking immediately, don't actually block the Thread and instead return immediately in Thread::block.
2020-08-02Kernel: Fix signal delivery when no syscall is madeTom
This fixes a regression introduced by the new software context switching where the Kernel would not deliver a signal unless the process is making system calls. This is because the TSS no longer updates the CS value, so the scheduler never considered delivery as the process always appeared to be in kernel mode. With software context switching we can just set up the signal trampoline at any time and when the processor returns back to user mode it'll get executed. This should fix e.g. killing programs that are stuck in some tight loop that doesn't make any system calls and is only pre-empted by the timer interrupt. Fixes #2958
2020-08-02Kernel: Remove ProcessInspectionHandle and make Process RefCountedTom
By making the Process class RefCounted we don't really need ProcessInspectionHandle anymore. This also fixes some race conditions where a Process may be deleted while still being used by ProcFS. Also make sure to acquire the Process' lock when accessing regions. Last but not least, there's no reason why a thread can't be scheduled while being inspected, though in practice it won't happen anyway because the scheduler lock is held at the same time.
2020-07-25Kernel: Allow Thread::sleep for more than 388 daysBen Wiederhake
Because Thread::sleep is an internal interface, it's easy to check that there are only few callers: Process::sys$sleep, usleep, and nanosleep are happy with this increased size, because now they support the entire range of their arguments (assuming small-ish values for ticks_per_second()). SyncTask doesn't care. Note that the old behavior wasn't "cap out at 388 days", which would have been reasonable. Instead, the code resulted in unsigned overflow, meaning that a very long sleep would "on average" end after about 194 days, sometimes much quicker.
2020-07-07Kernel: Fix checking BlockResultTom
We now have BlockResult::WokeNormally and BlockResult::NotBlocked, both of which indicate no error. We can no longer just check for BlockResult::WokeNormally and assume anything else must be an interruption.
2020-07-07Kernel+LibELF: Expose ELF Auxiliary Vector to UserspaceAndrew Kaster
The AT_* entries are placed after the environment variables, so that they can be found by iterating until the end of the envp array, and then going even further beyond :^)
2020-07-06Kernel: Enhance WaitQueue to remember pending wakesTom
If WaitQueue::wake_all, WaitQueue::wake_one, or WaitQueue::wake_n is called but nobody is currently waiting, we should remember that fact and prevent someone from waiting after such a request. This solves a race condition where the Finalizer thread is notified to finalize a thread, but it is not (yet) waiting on this queue. Fixes #2693
2020-07-06Kernel: Various context switch fixesTom
These changes solve a number of problems with the software context swithcing: * The scheduler lock really should be held throughout context switches * Transitioning from the initial (idle) thread to another needs to hold the scheduler lock * Transitioning from a dying thread to another also needs to hold the scheduler lock * Dying threads cannot necessarily be finalized if they haven't switched out of it yet, so flag them as active while a processor is running it (the Running state may be switched to Dying while it still is actually running)
2020-07-06Kernel: Require a reason to be passed to Thread::wait_onTom
The Lock class still permits no reason, but for everything else require a reason to be passed to Thread::wait_on. This makes it easier to diagnose why a Thread is in Queued state.
2020-07-03Kernel: Fix retreiving frame pointer from a threadTom
If we're trying to walk the stack for another thread, we can no longer retreive the EBP register from Thread::m_tss. Instead, we need to look at the top of the kernel stack, because all threads not currently running were last in kernel mode. Context switches now always trigger a brief switch to kernel mode, and Thread::m_tss only is used to save ESP and EIP. Fixes #2678
2020-07-03Kernel: Fix signal deliveryTom
When delivering urgent signals to the current thread we need to check if we should be unblocked, and if not we need to yield to another process. We also need to make sure that we suppress context switches during Process::exec() so that we don't clobber the registers that it sets up (eip mainly) by a context switch. To be able to do that we add the concept of a critical section, which are similar to Process::m_in_irq but different in that they can be requested at any time. Calls to Scheduler::yield and Scheduler::donate_to will return instantly without triggering a context switch, but the processor will then asynchronously trigger a context switch once the critical section is left.
2020-07-02Kernel: Remove no-longer-used GDT selector from ThreadAndreas Kling
Now that we use software context switching, each thread no longer has its own GDT entry (yay!) so we can get rid of this Thread member. :^)
2020-07-01Kernel: Turn Thread::current and Process::current into functionsTom
This allows us to query the current thread and process on a per processor basis
2020-07-01Kernel/LibCore: Expose processor id where a thread last ranTom
2020-07-01Kernel: Implement software context switching and Processor structureTom
Moving certain globals into a new Processor structure for each CPU allows us to eventually run an instance of the scheduler on each CPU.
2020-06-22LibC: Implement pselectNico Weber
pselect() is similar() to select(), but it takes its timeout as timespec instead of as timeval, and it takes an additional sigmask parameter. Change the sys$select parameters to match pselect() and implement select() in terms of pselect().
2020-05-20AK+Kernel: Help the compiler inline a bunch of trivial methodsSergey Bugaev
If these methods get inlined, the compiler is able to statically eliminate most of the assertions. Alas, it doesn't realize this, and believes inlining them to be too expensive. So give it a strong hint that it's not the case. This *decreases* the kernel binary size.
2020-04-26Kernel: Add timeout support to Thread::wait_onBrian Gianforcaro
This change plumbs a new optional timeout option to wait_on. The timeout is enabled by enqueing a timer on the timer queue while we are waiting. We can then see if we were woken up or timed out by checking if we are still on the wait queue or not.
2020-04-22Kernel: Make Process and Thread non-copyable and non-movableAndreas Kling
2020-04-13ptrace: Add PT_SETREGSItamar
PT_SETTREGS sets the regsiters of the traced thread. It can only be used when the tracee is stopped. Also, refactor ptrace. The implementation was getting long and cluttered the alraedy large Process.cpp file. This commit moves the bulk of the implementation to Kernel/Ptrace.cpp, and factors out peek & poke to separate methods of the Process class.
2020-04-13CPU: Handle breakpoint trapItamar
Also, start working on the debugger app.
2020-04-11Kernel: Include the current instruction pointer in profile samplesAndreas Kling
We were missing the innermost instruction pointer when sampling. This makes the instruction-level profile info a lot cooler! :^)
2020-03-28Kernel: Add 'ptrace' syscallItamar
This commit adds a basic implementation of the ptrace syscall, which allows one process (the tracer) to control another process (the tracee). While a process is being traced, it is stopped whenever a signal is received (other than SIGCONT). The tracer can start tracing another thread with PT_ATTACH, which causes the tracee to stop. From there, the tracer can use PT_CONTINUE to continue the execution of the tracee, or use other request codes (which haven't been implemented yet) to modify the state of the tracee. Additional request codes are PT_SYSCALL, which causes the tracee to continue exection but stop at the next entry or exit from a syscall, and PT_GETREGS which fethces the last saved register set of the tracee (can be used to inspect syscall arguments and return value). A special request code is PT_TRACE_ME, which is issued by the tracee and causes it to stop when it calls execve and wait for the tracer to attach.
2020-03-23AK: Reduce header dependency graph of String.hAndreas Kling
String.h no longer pulls in StringView.h. We do this by moving a bunch of String functions out-of-line.
2020-03-08AK: Add global FlatPtr typedef. It's u32 or u64, based on sizeof(void*)Andreas Kling
Use this instead of uintptr_t throughout the codebase. This makes it possible to pass a FlatPtr to something that has u32 and u64 overloads.
2020-03-01Kernel: Restore the previous thread state on SIGCONT after SIGSTOPAndreas Kling
When stopping a thread with the SIGSTOP signal, we now store the thread state in Thread::m_stop_state. That state is then restored on SIGCONT. This fixes an issue where previously-blocked threads would unblock upon resume. Now they simply resume in the Blocked state, and it's up to the regular unblocking mechanism to unblock them. Fixes #1326.