summaryrefslogtreecommitdiff
path: root/Kernel/Thread.cpp
AgeCommit message (Collapse)Author
2020-04-27Kernel: Expose timers via a TimerId typeBrian Gianforcaro
The public consumers of the timer API shouldn't need to know the how timer id's are tracked internally. Expose a typedef instead to allow the internal implementation to be protected from potential churn in the future. It's also just good API design.
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-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-13Thread: Set m_blocker to null in Thread::unblock()Itamar
Before this commit, m_blocker was only set to null in Thread::block, after the thread has been unblocked. Starting with this commit, m_blocker is also set to null in Thread::unblock. This change will allow us to implement a missing feature of the PT_TRACE command of the ptrace syscall - stopping the traced thread when it exits the execve syscall. That feature will be implemented by sending a blocking SIGSTOP to the traced thread after it has executed the execve logic and before it starts executing the new program in userspace. However, since Process::exec arranges the tss to return to userspace (the so-called "yield-teleport"), the code in Thread::block that should be run after the thread unblocks, and sets m_blocker to null, never actually runs. Setting m_blocker to null in Thread::unblock allows us to avoid an incorrect state where the thread is in a Running state but conatins a pointer to a Blocker.
2020-04-11Kernel: Store previous thread state upon all transitions to Stopped (#1753)Peter Nelson
We now store the previous thread state in m_stop_state for all transitions to the Stopped state via Thread::set_state. Fixes #1752 whereupon resuming a thread that was stopped with SIGTSTP, the previous state of the thread is not remembered correctly, resulting in m_stop_state == State::Invalid and the associated assertion fails.
2020-04-11LibELF: Move ELF classes into namespace ELFAndrew Kaster
This is for consistency with other namespace changes that were made a while back to the other libraries :)
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-04-08Kernel: Update cryptically-named functions related to symbolicationAndreas Kling
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-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-02Kernel: Use klog() instead of kprintf()Liav A
Also, duplicate data in dbg() and klog() calls were removed. In addition, leakage of virtual address to kernel log is prevented. This is done by replacing kprintf() calls to dbg() calls with the leaked data instead. Also, other kprintf() calls were replaced with klog().
2020-03-02Kernel: Load executables on demand when symbolicatingAndreas Kling
Previously we would map the entire executable of a program in its own address space (but make it unavailable to userspace code.) This patch removes that and changes the symbolication code to remap the executable on demand (and into the kernel's own address space instead of the process address space.) This opens up a couple of further simplifications that will follow.
2020-03-01Kernel: Move ProcessPagingScope to its own filesAndreas Kling
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.
2020-02-29Kernel: Disable interrupts throughout Thread::raw_backtrace()Andreas Kling
Otherwise we may hit an assertion when validating stack addresses.
2020-02-29Kernel: Simplify some dbg() loggingAndreas Kling
We don't have to log the process name/PID/TID, dbg() automatically adds that as a prefix to every line. Also we don't have to do .characters() on Strings passed to dbg() :^)
2020-02-27Thread: Use dbg() instead of dbgprintf()Liav A
2020-02-27Kernel: Fix ASSERTION failed in join_thread syscallCristian-Bogdan SIRB
set_interrupted_by_death was never called whenever a thread that had a joiner died, so the joiner remained with the joinee pointer there, resulting in an assertion fail in JoinBlocker: m_joinee pointed to a freed task, filled with garbage. Thread::current->m_joinee may not be valid after the unblock Properly return the joinee exit value to the joiner thread.
2020-02-26Kernel: Allow process with multiple threads to call exec and exitCristian-Bogdan SIRB
This allows a process wich has more than 1 thread to call exec, even from a thread. This kills all the other threads, but it won't wait for them to finish, just makes sure that they are not in a running/runable state. In the case where a thread does exec, the new program PID will be the thread TID, to keep the PID == TID in the new process. This introduces a new function inside the Process class, kill_threads_except_self which is called on exit() too (exit with multiple threads wasn't properly working either). Inside the Lock class, there is the need for a new function, clear_waiters, which removes all the waiters from the Process::big_lock. This is needed since after a exit/exec, there should be no other threads waiting for this lock, the threads should be simply killed. Only queued threads should wait for this lock at this point, since blocked threads are handled in set_should_die.
2020-02-25AK: Make Vector use size_t for its size and capacityAndreas Kling
2020-02-22Kernel: Fully validate pointers when walking stack during profilingAndreas Kling
It's not enough to just check that things wouldn't page fault, we also need to verify that addresses are accessible to the profiled thread.
2020-02-21Kernel: Don't trigger page faults during profiling stack walkAndreas Kling
The kernel sampling profiler will walk thread stacks during the timer tick handler. Since it's not safe to trigger page faults during IRQ's, we now avoid this by checking the page tables manually before accessing each stack location.
2020-02-18Kernel: Reset FPU state on exec()Andreas Kling
2020-02-17Kernel: Replace "current" with Thread::current and Process::currentAndreas Kling
Suggested by Sergey. The currently running Thread and Process are now Thread::current and Process::current respectively. :^)
2020-02-16Kernel: Reduce header dependencies of MemoryManager and RegionAndreas Kling
2020-02-16Kernel: Move all code into the Kernel namespaceAndreas Kling
2020-02-16Kernel: Rename RegisterDump => RegisterStateAndreas Kling
2020-02-01Kernel: Finalizer should not go back to sleep if there's more to doAndreas Kling
Before putting itself back on the wait queue, the finalizer task will now check if there's more work to do, and if so, do it first. :^) This patch also puts a bunch of process/thread debug logging behind PROCESS_DEBUG and THREAD_DEBUG since it was unbearable to debug this stuff with all the spam.
2020-01-27Kernel: Expose the signal that stopped a thread via sys$waitpid()Andreas Kling
2020-01-27Kernel: Remove ancient hack that put the current PID in TSS.SS2Andreas Kling
While I was bringing up multitasking, I put the current PID in the SS2 (ring 2 stack segment) slot of the TSS. This was so I could see which PID was currently running when just inspecting the CPU state.
2020-01-27Kernel: Simplify kernel thread stack allocationAndreas Kling
We had two identical code paths doing this for some reason.
2020-01-21Kernel: Tidy up debug logging a little bitAndreas Kling
When using dbg() in the kernel, the output is automatically prefixed with [Process(PID:TID)]. This makes it a lot easier to understand which thread is generating the output. This patch also cleans up some common logging messages and removes the now-unnecessary "dbg() << *current << ..." pattern.
2020-01-20Kernel: Use the templated copy_to/from_user() in more placesAndreas Kling
These ensure that the "to" and "from" pointers have the same type, and also that we copy the correct number of bytes.
2020-01-20Kernel: Remove some unnecessary casts to uintptr_tAndreas Kling
VirtualAddress is constructible from uintptr_t and const void*. PhysicalAddress is constructible from uintptr_t but not const void*.
2020-01-20Use uintptr_t instead of u32 when storing pointers as integersAndreas Kling
uintptr_t is 32-bit or 64-bit depending on the target platform. This will help us write pointer size agnostic code so that when the day comes that we want to do a 64-bit port, we'll be in better shape.
2020-01-19Kernel: Limit Thread::raw_backtrace() to the max profiler stack sizeAndreas Kling
Let's avoid walking overly long stacks here, since kmalloc() is finite.
2020-01-19Kernel: Use copy_from_user() when appropriate during thread backtracingAndreas Kling
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2020-01-13Kernel: Allow unlocking a held Lock with interrupts disabledAndreas Kling
This is needed to eliminate a race in Thread::wait_on() where we'd otherwise have to wait until after unlocking the process lock before we can disable interrupts.
2020-01-12Kernel: Fix Lock racing to the WaitQueueAndreas Kling
There was a time window between releasing Lock::m_lock and calling into the lock's WaitQueue where someone else could take m_lock and bring two threads into a deadlock situation. Fix this issue by holding Lock::m_lock until interrupts are disabled by either Thread::wait_on() or WaitQueue::wake_one().
2020-01-12Kernel: Keep SMAP protection enabled in Thread::backtrace_impl()Andreas Kling
2020-01-12Kernel: Fix busted backtraces when a thread backtraces itselfAndreas Kling
When the current thread is backtracing itself, we now start walking the stack from the current EBP register value, instead of the TSS one. Now SystemMonitor always appears to be running Thread::backtrace() when sampled, which makes perfect sense. :^)
2020-01-10Kernel: Fix kernel null deref on process crash during join_thread()Andreas Kling
The join_thread() syscall is not supposed to be interruptible by signals, but it was. And since the process death mechanism piggybacked on signal interrupts, it was possible to interrupt a pthread_join() by killing the process that was doing it, leading to confusing due to some assumptions being made by Thread::finalize() for threads that have a pending joiner. This patch fixes the issue by making "interrupted by death" a distinct block result separate from "interrupted by signal". Then we handle that state in join_thread() and tidy things up so that thread finalization doesn't get confused by the pending joiner being gone. Test: Tests/Kernel/null-deref-crash-during-pthread_join.cpp
2020-01-09Kernel: Rename {ss,esp}_if_crossRing to userspace_{ss,esp}Andreas Kling
These were always so awkwardly named.
2020-01-09Kernel: Remove unused variable Thread::m_userspace_stack_regionAndreas Kling
2020-01-05Kernel: Fix SMAP violation in thread signal dispatchAndreas Kling
2020-01-05Kernel: Start implementing x86 SMAP supportAndreas Kling
Supervisor Mode Access Prevention (SMAP) is an x86 CPU feature that prevents the kernel from accessing userspace memory. With SMAP enabled, trying to read/write a userspace memory address while in the kernel will now generate a page fault. Since it's sometimes necessary to read/write userspace memory, there are two new instructions that quickly switch the protection on/off: STAC (disables protection) and CLAC (enables protection.) These are exposed in kernel code via the stac() and clac() helpers. There's also a SmapDisabler RAII object that can be used to ensure that you don't forget to re-enable protection before returning to userspace code. THis patch also adds copy_to_user(), copy_from_user() and memset_user() which are the "correct" way of doing things. These functions allow us to briefly disable protection for a specific purpose, and then turn it back on immediately after it's done. Going forward all kernel code should be moved to using these and all uses of SmapDisabler are to be considered FIXME's. Note that we're not realizing the full potential of this feature since I've used SmapDisabler quite liberally in this initial bring-up patch.
2020-01-04Kernel: Use Thread::from_tid() in more placesAndreas Kling
2020-01-02Kernel: Mask kernel addresses in backtraces and profilesAndreas Kling
Addresses outside the userspace virtual range will now show up as 0xdeadc0de in backtraces and profiles generated by unprivileged users.
2020-01-01Kernel: Prevent executing I/O instructions in userspaceAndreas Kling
All threads were running with iomapbase=0 in their TSS, which the CPU interprets as "there's an I/O permission bitmap starting at offset 0 into my TSS". Because of that, any bits that were 1 inside the TSS would allow the thread to execute I/O instructions on the port with that bit index. Fix this by always setting the iomapbase to sizeof(TSS32), and also setting the TSS descriptor's limit to sizeof(TSS32), effectively making the I/O permissions bitmap zero-length. This should make it no longer possible to do I/O from userspace. :^)