Commit Graph

135 Commits

Author SHA1 Message Date
Tom
0fab0ee96a Kernel: Rename Process::is_ring0/3 to Process::is_kernel/user_process
Since "rings" typically refer to code execution and user processes
can also execute in ring 0, rename these functions to more accurately
describe what they mean: kernel processes and user processes.
2020-09-10 19:57:15 +02:00
Tom
19ffd9d677 Kernel: Fix detecting in what ring a crash happened
The ring is determined based on the CS register. This fixes crashes
being handled as ring 3 crashes even though EIP/CS clearly showed
that the crash happened in the kernel.
2020-09-10 19:57:15 +02:00
Luke
8a2fd0e436 Kernel: Fix Processor::features_string() stopping too early and detect more features
The exit condition for the loop was sizeof(m_features) * 8,
which was 32. Presumably this was supposed to mean 32 bits, but it
actually made it stop as soon as it reached the 6th bit.

Also add detection for more SIMD CPU features.
2020-08-31 18:57:54 +02:00
Tom
67dbb56444 Kernel: Release page tables when no longer needed
When unmapping regions, check if page tables can be freed.

This is a follow-up change for #3254.
2020-08-28 09:21:24 +02:00
Muhammad Zahalqa
e035640cd5
Kernel: Remove unneeded #include in ProcessorInfo.cpp (#3211) 2020-08-19 11:25:12 +02:00
Muhammad Zahalqa
ff577e22a3 Kernel: ProcessorInfo.cpp remove unused headers 2020-08-18 13:41:08 +02:00
Nico Weber
430b265cd4 AK: Rename KB, MB, GB to KiB, MiB, GiB
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-16 16:33:28 +02:00
Itamar
3b422564f3 Kernel: Fix behaviour of PT_TRACEME in ptrace
The behaviour of the PT_TRACEME feature has been broken for some time,
this change fixes it.

When this ptrace flag is used, the traced process should be paused
before exiting execve.
We previously were sending the SIGSTOP signal at a stage where
interrupts are disabled, and the traced process continued executing
normally, without pausing and waiting for the tracer.
This change fixes it.
2020-08-15 15:06:35 +02:00
Ben Wiederhake
936d5dcc01 Kernel: Tell compiler about invisible calls
This makes the Kernel build cleanly with -Wmissing-declarations.
2020-08-12 20:40:59 +02:00
Tom
728de56481 Kernel: Prevent recursive calls into the scheduler
Upon leaving a critical section (such as a SpinLock) we need to
check if we're already asynchronously invoking the Scheduler.
Otherwise we might end up triggering another context switch
as soon as leaving the scheduler lock.

Fixes #2883
2020-08-02 17:15:11 +02:00
etaIneLp
586ada7a14 Kernel: Tell the bootloader to put us into graphics mode 2020-08-01 07:57:29 +02:00
Nico Weber
4eb967b5eb LibC+Kernel: Start implementing sysconf
For now, only the non-standard _SC_NPROCESSORS_CONF and
_SC_NPROCESSORS_ONLN are implemented.

Use them to make ninja pick a better default -j value.
While here, make the ninja package script not fail if
no other port has been built yet.
2020-07-15 00:07:20 +02:00
Tom
b02d33bd63 Kernel: Fix some flaws that caused crashes or hangs during boot
We need to halt the BSP briefly until all APs are ready for the
first context switch, but we can't hold the same spinlock by all
of them while doing so. So, while the APs are waiting on each other
they need to release the scheduler lock, and then once signaled
re-acquire it. Should solve some timing dependent hangs or crashes,
most easily observed using qemu with kvm disabled.
2020-07-09 23:24:55 +02:00
Tom
ce5ae83963 Kernel: Detect syscall/sysenter support 2020-07-08 23:47:36 +02:00
Tom
06d50f64b0 Kernel: Aggregate TLB flush requests for Regions for SMP
Rather than sending one TLB flush request for each page,
aggregate them so that we're not spamming the other
processors with FlushTLB IPIs.
2020-07-06 22:39:06 +02:00
Tom
bc107d0b33 Kernel: Add SMP IPI support
We can now properly initialize all processors without
crashing by sending SMP IPI messages to synchronize memory
between processors.

We now initialize the APs once we have the scheduler running.
This is so that we can process IPI messages from the other
cores.

Also rework interrupt handling a bit so that it's more of a
1:1 mapping. We need to allocate non-sharable interrupts for
IPIs.

This also fixes the occasional hang/crash because all
CPUs now synchronize memory with each other.
2020-07-06 17:07:44 +02:00
Andreas Kling
163c9d5f8f Kernel: Thread::wait_on() must always leave interrupts enabled on exit
The short-circuit path added for waiting on a queue that already had a
pending wake was able to return with interrupts disabled, which breaks
the API contract of wait_on() always returning with IF=1.

Fix this by adding a way to override the restored IF in ScopedCritical.
2020-07-06 11:33:32 +02:00
Andreas Kling
3e0020e67d Kernel: Tidy up the ScopedCritical class a little bit 2020-07-06 11:33:32 +02:00
Tom
2a82a25fec Kernel: Various context switch fixes
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-06 10:00:24 +02:00
Tom
bb84fad0bf Kernel: Fix retreiving frame pointer from a thread
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-03 21:16:56 +02:00
Tom
9b4e6f6a23 Kernel: Consolidate features into CPUFeature enum
This allows us to consolidate printing out all the CPU features
into one log statement. Also expose them in /proc/cpuinfo
2020-07-03 19:32:34 +02:00
Tom
e373e5f007 Kernel: Fix signal delivery
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-03 19:32:34 +02:00
Tom
57b61b2dde Kernel: Split initialization of Processor structure
We need to very early on initialize the Processor structure so
that we can use RecursiveSpinLock early on.
2020-07-03 19:32:34 +02:00
Tom
2a38cc9a12 Kernel: Add a quickmap region for each processor
Threads need to be able to concurrently quickmap things.
2020-07-01 12:07:01 +02:00
Tom
16783bd14d Kernel: Turn Thread::current and Process::current into functions
This allows us to query the current thread and process on a
per processor basis
2020-07-01 12:07:01 +02:00
Tom
d99901660d Kernel/LibCore: Expose processor id where a thread last ran 2020-07-01 12:07:01 +02:00
Tom
d98edb3171 Kernel: List all CPUs in /proc/cpuinfo 2020-07-01 12:07:01 +02:00
Tom
fb41d89384 Kernel: Implement software context switching and Processor structure
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-07-01 12:07:01 +02:00
3541
4fa6301523 Kernel: Add g_cpu_supports_rdseed
CPUs which support RDRAND do not necessarily support RDSEED. This
introduces a flag g_cpu_supports_rdseed which is set appropriately
by CPUID. This causes Haswell CPUs in particular (and probably a lot
of AMD chips) to now fail to boot with #2634, rather than an illegal
instruction.

It seems like the KernelRng needs either an initial reseed call or
more random events added before the first call to get_good_random,
but I don't feel qualified to make that kind of change.
2020-06-27 12:57:03 +02:00
Andreas Kling
723f4e5ee6 Meta: Scale back overly informal user-facing strings
We were getting a little overly memey in some places, so let's scale
things back to business-casual.

Informal language is fine in comments, commits and debug logs,
but let's keep the runtime nice and presentable. :^)
2020-06-17 18:35:49 +02:00
Tom
0bc92c259d Kernel: Detect APs and boot them into protected mode
This isn't fully working, the APs pretend like they're
fully initialized and are just halted permanently for now.
2020-06-04 18:15:23 +02:00
etaIneLp
7bc871ca8d Kernel: Stop bootloader from setting video mode with Multiboot
Meta: Update INSTALL.md and grub configs for new boot_mode option
2020-05-28 00:50:55 +02:00
etaIneLp
82d0352a67 Kernel: Unmap first MB after jumping above 3GB 2020-05-26 09:50:12 +02:00
etaIneLp
826dc94187 Kernel: Create page structures correctly in boot.s 2020-05-26 09:50:12 +02:00
Andreas Kling
84b7bc5e14 Kernel: Add convenient ways to map whole BIOS and EBDA into memory
This patch adds a MappedROM abstraction to the Kernel VM subsystem.
It's basically the read-only byte buffer equivalent of a TypedMapping.

We use this in the ACPI and MP table parsers to scan for interesting
stuff in low memory instead of doing a bunch of address arithmetic.
2020-05-22 13:17:38 +02:00
Andreas Kling
81d35c6891 Kernel: Always inline stac(), clac() and SmapDisabler
Let's not be paying the function call overhead for these tiny ops.
Maybe there's an argument for having fewer gadgets in the kernel but
for now we're actually seeing stac() in profiles so let's put
that above theoretical security issues.
2020-05-20 14:17:01 +02:00
Andreas Kling
21d5f4ada1 Kernel: Absorb LibBareMetal back into the kernel
This was supposed to be the foundation for some kind of pre-kernel
environment, but nobody is working on it right now, so let's move
everything back into the kernel and remove all the confusion.
2020-05-16 12:00:04 +02:00
Andreas Kling
6fe83b0ac4 Kernel: Crash the current process on OOM (instead of panicking kernel)
This patch adds PageFaultResponse::OutOfMemory which informs the fault
handler that we were unable to allocate a necessary physical page and
cannot continue.

In response to this, the kernel will crash the current process. Because
we are OOM, we can't symbolicate the crash like we normally would
(since the ELF symbolication code needs to allocate), so we also
communicate to Process::crash() that we're out of memory.

Now we can survive "allocate 300 MB" (only the allocate process dies.)
This is definitely not perfect and can easily end up killing a random
innocent other process who happened to allocate one page at the wrong
time, but it's a *lot* better than panicking on OOM. :^)
2020-05-06 22:28:23 +02:00
Itamar
d04409b444 CPU: Handle Debug exception
We currently only care about debug exceptions that are triggered
by the single-step execution mode.

The debug exception is translated to a SIGTRAP, which can be caught
and handled by the tracing thread.
2020-04-13 23:20:59 +02:00
Itamar
77f671b462 CPU: Handle breakpoint trap
Also, start working on the debugger app.
2020-04-13 00:53:22 +02:00
Marco Wang
3fae4cb054 Kernel: Prepend missing license 2020-04-03 09:12:29 +02:00
Liav A
f7b207c7ae CPU: Move EOI call to the end of handle_interrupt() 2020-03-24 16:15:33 +01:00
Shannon Booth
81adefef27 Kernel: Run clang-format on files
Let's rip off the band-aid
2020-03-22 01:22:32 +01:00
Liav A
9a303cc5a5 Kernel: Add the NonMaskableInterruptDisabler class
This class will be used later to disable NMIs when we
initialize the RTC timer.
2020-03-19 15:48:00 +01:00
Andreas Kling
b1058b33fb AK: Add global FlatPtr typedef. It's u32 or u64, based on sizeof(void*)
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-08 13:06:51 +01:00
Liav A
9991a36d1a CPU: Prevent leakage of virtual addresses to kernel log 2020-03-06 15:57:19 +01:00
Andreas Kling
c6693f9b3a Kernel: Simplify a bunch of dbg() and klog() calls
LogStream can handle VirtualAddress and PhysicalAddress directly.
2020-03-06 15:00:44 +01:00
Liav A
773afefe7c Kernel: Change HandlerPurpose to HandlerType
Also, GenericInterruptHandler class requires to implement two new
methods.
2020-03-06 11:19:51 +01:00
Andreas Kling
2709116334 Kernel: Fix strange looking output on unhandled page fault 2020-03-06 10:41:08 +01:00
Liav A
f33fb151b4 CPU: Allow to use IRQs in range of 50 to 178 2020-03-06 10:32:32 +01:00