summaryrefslogtreecommitdiff
path: root/Kernel/Scheduler.cpp
AgeCommit message (Collapse)Author
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-28WaitBlocker: don't unblock if thread has pending SIGCONTItamar
Previosuly, if we sent a SIGCONT to a stopped thread and then waitpid() with WSTOPPED on that thread before the signal was dispatched, then the WaitBlocker would first unblock (because the thread is stopped) and only after that the thread would get the SIGCONT signal. This would mean that when waitpid returns the waitee is not stopped. To fix this, we do not unblock the waiting thread if the waitee thread has a pending SIGCONT.
2020-03-19Scheduler: Use monotonic time for blocking threadsLiav A
2020-03-19Kernel: Introduce the new Time management subsystemLiav A
This new subsystem includes better abstractions of how time will be handled in the OS. We take advantage of the existing RTC timer to aid in keeping time synchronized. This is standing in contrast to how we handled time-keeping in the kernel, where the PIT was responsible for that function in addition to update the scheduler about ticks. With that new advantage, we can easily change the ticking dynamically and still keep the time synchronized. In the process context, we no longer use a fixed declaration of TICKS_PER_SECOND, but we call the TimeManagement singleton class to provide us the right value. This allows us to use dynamic ticking in the future, a feature known as tickless kernel. The scheduler no longer does by himself the calculation of real time (Unix time), and just calls the TimeManagment singleton class to provide the value. Also, we can use 2 new boot arguments: - the "time" boot argument accpets either the value "modern", or "legacy". If "modern" is specified, the time management subsystem will try to setup HPET. Otherwise, for "legacy" value, the time subsystem will revert to use the PIT & RTC, leaving HPET disabled. If this boot argument is not specified, the default pattern is to try to setup HPET. - the "hpet" boot argumet accepts either the value "periodic" or "nonperiodic". If "periodic" is specified, the HPET will scan for periodic timers, and will assert if none are found. If only one is found, that timer will be assigned for the time-keeping task. If more than one is found, both time-keeping task & scheduler-ticking task will be assigned to periodic timers. If this boot argument is not specified, the default pattern is to try to scan for HPET periodic timers. This boot argument has no effect if HPET is disabled. In hardware context, PIT & RealTimeClock classes are merely inheriting from the HardwareTimer class, and they allow to use the old i8254 (PIT) and RTC devices, managing them via IO ports. By default, the RTC will be programmed to a frequency of 1024Hz. The PIT will be programmed to a frequency close to 1000Hz. About HPET, depending if we need to scan for periodic timers or not, we try to set a frequency close to 1000Hz for the time-keeping timer and scheduler-ticking timer. Also, if possible, we try to enable the Legacy replacement feature of the HPET. This feature if exists, instructs the chipset to disconnect both i8254 (PIT) and RTC. This behavior is observable on QEMU, and was verified against the source code: https://github.com/qemu/qemu/commit/ce967e2f33861b0e17753f97fa4527b5943c94b6 The HPETComparator class is inheriting from HardwareTimer class, and is responsible for an individual HPET comparator, which is essentially a timer. Therefore, it needs to call the singleton HPET class to perform HPET-related operations. The new abstraction of Hardware timers brings an opportunity of more new features in the foreseeable future. For example, we can change the callback function of each hardware timer, thus it makes it possible to swap missions between hardware timers, or to allow to use a hardware timer for other temporary missions (e.g. calibrating the LAPIC timer, measuring the CPU frequency, etc).
2020-03-19Kernel: Use a const reference to RegisterState in IRQ handlingLiav A
2020-03-03AK: Make quick_sort() a little more ergonomicAndreas Kling
Now it actually defaults to "a < b" comparison, instead of forcing you to provide a trivial less-than comparator. Also you can pass in any collection type that has .begin() and .end() and we'll sort it for you.
2020-03-01AK: Remove unnecessary casts to size_t, after Vector changesAndreas Kling
Now that Vector uses size_t, we can remove a whole bunch of redundant casts to size_t.
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-27Scheduler: Use dbg() instead of dbgprintf()Liav A
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-24Kernel: Include the new PIT class in system componentsLiav A
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: Move all code into the Kernel namespaceAndreas Kling
2020-02-16Kernel: Rename RegisterDump => RegisterStateAndreas Kling
2020-02-10Kernel: Remove some commented-out code in Scheduler::yield()Andreas Kling
2020-02-10Kernel: Remove outdated FIXME from SchedulerAndreas 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-26Kernel: read()/write() should respect timeouts when used on a socketsAndreas Kling
Move timeout management to the ReadBlocker and WriteBlocker classes. Also get rid of the specialized ReceiveBlocker since it no longer does anything that ReadBlocker can't do.
2020-01-18Kernel: Stop flushing GDT/IDT registers all the timeAndreas 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-12Kernel: Dispatch pending signals when returning from a syscallAndreas Kling
It was quite easy to put the system into a heavy churn state by doing e.g "cat /dev/zero". It was then basically impossible to kill the "cat" process, even with "kill -9", since signals are only delivered in two conditions: a) The target thread is blocked in the kernel b) The target thread is running in userspace However, since "cat /dev/zero" command spends most of its time actively running in the kernel, not blocked, the signal dispatch code just kept postponing actually handling the signal indefinitely. To fix this, we now check before returning from a syscall if there are any pending unmasked signals, and if so, we take a dramatic pause by blocking the current thread, knowing it will immediately be unblocked by signal dispatch anyway. :^)
2020-01-12Kernel: Disable SMAP protection while capturing profile samplesAndreas Kling
2020-01-09Kernel: Rename {ss,esp}_if_crossRing to userspace_{ss,esp}Andreas Kling
These were always so awkwardly named.
2020-01-09Kernel: Ignore closed fd's when considering select() unblockAndreas Kling
This fixes a null RefPtr deref (which asserts) in the scheduler if a file descriptor being select()'ed is closed by a second thread while blocked in select(). Test: Kernel/null-deref-close-during-select.cpp
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. :^)
2020-01-01Kernel: Switch to eagerly restoring x86 FPU state on context switchAndreas Kling
Lazy FPU restore is well known to be vulnerable to timing attacks, and eager restore is a lot simpler anyway, so let's just do it eagerly.
2019-12-30Kernel: Refactor scheduler to use dynamic thread prioritiesAndreas Kling
Threads now have numeric priorities with a base priority in the 1-99 range. Whenever a runnable thread is *not* scheduled, its effective priority is incremented by 1. This is tracked in Thread::m_extra_priority. The effective priority of a thread is m_priority + m_extra_priority. When a runnable thread *is* scheduled, its m_extra_priority is reset to zero and the effective priority returns to base. This means that lower-priority threads will always eventually get scheduled to run, once its effective priority becomes high enough to exceed the base priority of threads "above" it. The previous values for ThreadPriority (Low, Normal and High) are now replaced as follows: Low -> 10 Normal -> 30 High -> 50 In other words, it will take 20 ticks for a "Low" priority thread to get to "Normal" effective priority, and another 20 to reach "High". This is not perfect, and I've used some quite naive data structures, but I think the mechanism will allow us to build various new and interesting optimizations, and we can figure out better data structures later on. :^)
2019-12-27Kernel: Add kernel-level timer queue (heavily based on @juliusf's work)Conrad Pankoff
PR #591 defines the rationale for kernel-level timers. They're most immediately useful for TCP retransmission, but will most likely see use in many other areas as well.
2019-12-27Kernel: Separate runnable thread queues by priorityAndreas Kling
This patch introduces three separate thread queues, one for each thread priority available to userspace (Low, Normal and High.) Each queue operates in a round-robin fashion, but we now always prefer to schedule the highest priority thread that currently wants to run. There are tons of tweaks and improvements that we can and should make to this mechanism, but I think this is a step in the right direction. This makes WindowServer significantly more responsive while one of its clients is burning CPU. :^)
2019-12-26Kernel: Move PC speaker beep timing logic from scheduler to the syscallAndreas Kling
I don't know why I put this in the scheduler to begin with.. the caller can just block until the beeping is finished.
2019-12-22Kernel: Get rid of "main thread" conceptAndreas Kling
The idea of all processes reliably having a main thread was nice in some ways, but cumbersome in others. More importantly, it didn't match up with POSIX thread semantics, so let's move away from it. This thread gets rid of Process::main_thread() and you now we just have a bunch of Thread objects floating around each Process. When the finalizer nukes the last Thread in a Process, it will also tear down the Process. There's a bunch of more things to fix around this, but this is where we get started :^)
2019-12-15Kernel+LibC: Publish a "kernel info page" and use it for gettimeofday()Andreas Kling
This patch adds a single "kernel info page" that is mappable read-only by any process and contains the current time of day. This is then used to implement a version of gettimeofday() that doesn't have to make a syscall. To protect against race condition issues, the info page also has a serial number which is incremented whenever the kernel updates the contents of the page. Make sure to verify that the serial number is the same before and after reading the information you want from the page.
2019-12-11Kernel: Implement a simple process time profilerAndreas Kling
The kernel now supports basic profiling of all the threads in a process by calling profiling_enable(pid_t). You finish the profiling by calling profiling_disable(pid_t). This all works by recording thread stacks when the timer interrupt fires and the current thread is in a process being profiled. Note that symbolication is deferred until profiling_disable() to avoid adding more noise than necessary to the profile. A simple "/bin/profile" command is included here that can be used to start/stop profiling like so: $ profile 10 on ... wait ... $ profile 10 off After a profile has been recorded, it can be fetched in /proc/profile There are various limits (or "bugs") on this mechanism at the moment: - Only one process can be profiled at a time. - We allocate 8MB for the samples, if you use more space, things will not work, and probably break a bit. - Things will probably fall apart if the profiled process dies during profiling, or while extracing /proc/profile
2019-12-01Kernel: Use a WaitQueue to implement finalizer wakeupAndreas Kling
This gets rid of the special "Lurking" thread state and replaces it with a generic WaitQueue :^)
2019-12-01Kernel: Use a dedicated thread state for wait-queued threadsAndreas Kling
Instead of using the generic block mechanism, wait-queued threads now go into the special Queued state. This fixes an issue where signal dispatch would unblock a wait-queued thread (because signal dispatch unblocks blocked threads) and cause confusion since the thread only expected to be awoken by the queue.
2019-12-01Kernel: Add a WaitQueue for Thread queueing/waking and use it for LockAndreas Kling
The kernel's Lock class now uses a proper wait queue internally instead of just having everyone wake up regularly to try to acquire the lock. We also keep the donation mechanism, so that whenever someone tries to take the lock and fails, that thread donates the remainder of its timeslice to the current lock holder. After unlocking a Lock, the unlocking thread calls WaitQueue::wake_one, which unblocks the next thread in queue.
2019-11-14Kernel: Move Thread::m_joinee_exit_value into the JoinBlockerAndreas Kling
There's no need for this to be a permanent Thread member. Just use a reference in the JoinBlocker instead.
2019-11-14Kernel+LibPthread: Implement pthread_join()Andreas Kling
It's now possible to block until another thread in the same process has exited. We can also retrieve its exit value, which is whatever value it passed to pthread_exit(). :^)
2019-11-06Kernel: Rework Process::Priority into ThreadPriorityAndreas Kling
Scheduling priority is now set at the thread level instead of at the process level. This is a step towards allowing processes to set different priorities for threads. There's no userspace API for that yet, since only the main thread's priority is affected by sched_setparam().
2019-10-07Kernel: Add exception_code to RegisterDump.Drew Stratford
Added the exception_code field to RegisterDump, removing the need for RegisterDumpWithExceptionCode. To accomplish this, I had to push a dummy exception code during some interrupt entries to properly pad out the RegisterDump. Note that we also needed to change some code in sys$sigreturn to deal with the new RegisterDump layout.
2019-10-02Kernel: Don't update Thread TSS if scheduler tick reschedules itAndreas Kling
If we didn't find anything else that wants to run, we don't need to update the current thread's TSS since we're just gonna return to the same thread anyway.
2019-09-14Kernel: Stop idling after handling an IRQAndreas Kling
If we receive an IRQ while the idle task is running, prevent it from re-halting the CPU after the IRQ handler returns. Instead have the idle task yield to the scheduler, so we can see if the IRQ has unblocked something.
2019-09-09Kernel: Change m_blockers to m_blocker.Drew Stratford
Because of the way signals now work there should not be more than one blocker per thread. This changes the blocker and thread class to reflect that.
2019-09-08Kernel: waitpid() should unblock and -ECHILD if SIG_IGN reaps childAndreas Kling
2019-09-07Kernel: Support thread-local storageAndreas Kling
This patch adds support for TLS according to the x86 System V ABI. Each thread gets a thread-specific memory region, and the GS segment register always points _to a pointer_ to the thread-specific memory. In other words, to access thread-local variables, userspace programs start by dereferencing the pointer at [gs:0]. The Process keeps a master copy of the TLS segment that new threads should use, and when a new thread is created, they get a copy of it. It's basically whatever the PT_TLS program header in the ELF says.
2019-08-10Kernel: Use a more detailed state machine for socket setupConrad Pankoff
2019-08-07Kernel: Don't create Function objects in the scheduling codeAndreas Kling
Each Function is a heap allocation, so let's make an effort to avoid doing that during scheduling. Because of header dependencies, I had to put the runnables iteration helpers in Thread.h, which is a bit meh but at least this cuts out all the kmalloc() traffic in pick_next().
2019-08-01Scheduler: Fix bitrotted SCHEDULER_RUNNABLE_DEBUG codeAndreas Kling
The runnable lists have moved from Thread to Scheduler.
2019-08-01Scheduler: Fix deadlock when first scheduling candidate being inspectedAndreas Kling
Somewhat reproducible by opening ProcessManager and trying to view the stacks for WindowServer. Regressed in 53262cd08b08f3d4d2b77cff9c348e84b1bf5eb9.
2019-07-22Move runnable/non-runnable list control entirely over to SchedulerRobin Burchell
This way, we can change how the scheduler works without having to change Thread too.