Commit Graph

286 Commits

Author SHA1 Message Date
Timon Kruiper
026f37b031 Kernel: Move Spinlock functions back to arch independent Locking folder
Now that the Spinlock code is not dependent on architectural specific
code anymore, we can move it back to the Locking folder. This also means
that the Spinlock implemenation is now used for the aarch64 kernel.
2022-08-26 12:51:57 +02:00
Timon Kruiper
e8aff0c1c8 Kernel: Use InterruptsState in Spinlock code
This commit updates the lock function from Spinlock and
RecursiveSpinlock to return the InterruptsState of the processor,
instead of the processor flags. The unlock functions would only look at
the interrupt flag of the processor flags, so we now use the
InterruptsState enum to clarify the intent, and such that we can use the
same Spinlock code for the aarch64 build.

To not break the build, all the call sites are updated aswell.
2022-08-26 12:51:57 +02:00
Andreas Kling
cf16b2c8e6 Kernel: Wrap process address spaces in SpinlockProtected
This forces anyone who wants to look into and/or manipulate an address
space to lock it. And this replaces the previous, more flimsy, manual
spinlock use.

Note that pointers *into* the address space are not safe to use after
you unlock the space. We've got many issues like this, and we'll have
to track those down as wlel.
2022-08-24 14:57:51 +02:00
Andreas Kling
4c081e0479 Kernel/x86: Protect the CR3->PD map with a spinlock
This can be accessed from multiple CPUs at the same time, so relying on
the interrupt flag is clearly insufficient.
2022-08-22 17:56:03 +02:00
Andreas Kling
6cd3695761 Kernel: Stop taking MM lock while using regular quickmaps
You're still required to disable interrupts though, as the mappings are
per-CPU. This exposed the fact that our CR3 lookup map is insufficiently
protected (but we'll address that in a separate commit.)
2022-08-22 17:56:03 +02:00
Andreas Kling
11eee67b85 Kernel: Make self-contained locking smart pointers their own classes
Until now, our kernel has reimplemented a number of AK classes to
provide automatic internal locking:

- RefPtr
- NonnullRefPtr
- WeakPtr
- Weakable

This patch renames the Kernel classes so that they can coexist with
the original AK classes:

- RefPtr => LockRefPtr
- NonnullRefPtr => NonnullLockRefPtr
- WeakPtr => LockWeakPtr
- Weakable => LockWeakable

The goal here is to eventually get rid of the Lock* classes in favor of
using external locking.
2022-08-20 17:20:43 +02:00
Andreas Kling
e475263113 AK+Kernel: Add AK::AtomicRefCounted and use everywhere in the kernel
Instead of having two separate implementations of AK::RefCounted, one
for userspace and one for kernelspace, there is now RefCounted and
AtomicRefCounted.
2022-08-20 17:15:52 +02:00
Idan Horowitz
ae9c6a9ded Kernel: Add 8-byte atomics for i686 GCC
Unlike Clang, GCC does not support 8-byte atomics on i686 with the
-mno-80387 flag set, so until that is fixed, implement a minimal set of
atomics that are currently required.
2022-08-19 19:49:38 +03:00
Andreas Kling
a84d893af8 Kernel/x86: Re-enable interrupts ASAP when handling page faults
As soon as we've saved CR2 (the faulting address), we can re-enable
interrupt processing. This should make the kernel more responsive under
heavy fault loads.
2022-08-19 12:14:57 +02:00
Andreas Kling
ae3fa20252 Kernel/x86: Don't re-enable interrupts too soon when unlocking spinlocks
To ensure that we stay on the same CPU that acquired the spinlock until
we're completely unlocked, we now leave the critical section *before*
re-enabling interrupts.
2022-08-18 00:58:34 +02:00
Andreas Kling
51bc87d15a Kernel/x86: Disable interrupts when leaving critical sections
This fixes an issue where we could get preempted after acquiring the
current Processor pointer, but before calling methods on it.

I strongly suspect this was the cause of "Processor::current() == this"
assertion failures.
2022-08-18 00:58:34 +02:00
Andreas Kling
cd348918f9 Kernel/x86: Move Processor::{leave,clear}_critical() out of line
I don't think this code needs to be ALWAYS_INLINE, and moving it out
of line will make backtraces nicer.
2022-08-18 00:58:34 +02:00
Liav A
1c499e75bd Kernel+Userland: Remove supervisor pages concept
There's no real value in separating physical pages to supervisor and
user types, so let's remove the concept and just let everyone to use
"user" physical pages which can be allocated from any PhysicalRegion
we want to use. Later on, we will remove the "user" prefix as this
prefix is not needed anymore.
2022-07-14 23:27:46 +02:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
sin-ack
c70f45ff44 Everywhere: Explicitly specify the size in StringView constructors
This commit moves the length calculations out to be directly on the
StringView users. This is an important step towards the goal of removing
StringView(char const*), as it moves the responsibility of calculating
the size of the string to the user of the StringView (which will prevent
naive uses causing OOB access).
2022-07-12 23:11:35 +02:00
Tim Schumacher
7d3f71a648 Kernel: Do not disable userland access to the RDTSC instruction
Access to RDTSC is occasionally restricted to give malware one less
option to accurately time attacks (side-channels, etc.).

However, QEMU requires access to the timestamp counter for the exact
same reason (which is accurately timing its CPU ticks), so lets just
enable it for now.
2022-07-08 22:27:38 +00:00
Idan Horowitz
1950e79d48 Kernel: Eliminate possible KASLR leak by disabling CR4.FSGSBASE
The RDGSBASE userspace instruction allows programs to read the contents
of the gs segment register which contains a kernel pointer to the base
of the current Processor struct.

Since we don't use this instruction in Serenity at the moment, we can
simply disable it for now to ensure we don't break KASLR. Support can
later be restored once proper swapping of the contents of gs is done on
userspace/kernel boundaries.
2022-06-22 07:52:35 +03:00
Liav A
7310a9a641 Kernel/Interrupts: Add missing include for IRQController header file
This is necessary for the next commit in the patch, otherwise this can't
be compiled. It seems like this was a hidden issue that is discovered
now only by changing includes in a mass-scale.
2022-06-17 11:01:27 +02:00
Timon Kruiper
f085903f62 Kernel: Move IRQController and InterruptManagement to Arch directory
These 2 classes currently contain much code that is x86(_64) specific.
Move them to the architecture specific directory. This also allows for a
simpler implementation for aarch64.
2022-06-02 13:14:12 +01:00
Timon Kruiper
cbe1717181 Kernel: Rename idt_init() to initialize_interrupts()
Also move the function out of the x86/Interrupts.h file into the generic
Interrupts.h file and add a stub for aarch64.
2022-06-02 13:14:12 +01:00
Timon Kruiper
a4534678f9 Kernel: Implement InterruptDisabler using generic Processor functions
Now that the code does not use architectural specific code, it is moved
to the generic Arch directory and the paths are modified accordingly.
2022-06-02 13:14:12 +01:00
Timon Kruiper
ea9cf8b6ab Kernel: Separate NonMaskableInterruptDisabler into its own file
This is for the upcoming change to make InterruptDisabler class work for
the aarch64 build.
2022-06-02 13:14:12 +01:00
Timon Kruiper
9413fe9fe5 Kernel: Add interrupt related functions to Processor class
This adds {enable, disable}_interrupts() and are_interrupts_enabled()
to the Processor class, and also implements them for x86(_64) and
aarch64.
2022-06-02 13:14:12 +01:00
Timon Kruiper
2fd5e9f729 Kernel: Add GenericInterruptHandler.cpp to aarch64 build
This requires us to add an Interrupts.h file in the Kernel/Arch
directory, which includes the architecture specific files.

The commit also stubs out the functions to be able to compile the
aarch64 Kernel.
2022-06-02 13:14:12 +01:00
SeekingBlues
df8df947f6 Kernel: Do not include AK/Platform.h in mcontext headers
Including signal.h would cause several ports to fail on build,
because it would end up including AK/Platform.h through these
mcontext headers. This is problematic because AK/Platform.h defines
several macros with very common names, such as `NAKED` (breaks radare2),
and `NO_SANITIZE_ADDRESS` and `ALWAYS_INLINE` (breaks ruby).
2022-05-30 21:39:41 +02:00
Lucas CHOLLET
a506ec08d8 Kernel: Expose cache size for Intel CPUs
The patch also prevents any try if the CPU's vendor isn't known and
improves the const-correctness of the AMD version.
2022-05-30 13:41:23 +01:00
Linus Groh
20e2e39fcc Kernel: Expose size of L1 data/instruction, L2, and L3 CPU caches :^)
These are added as properties of the "caches" object to each processor,
if available.
2022-05-29 15:23:57 +02:00
Jesse Buhagiar
964f8fbf3a Kernel: Implement AVX XSAVE support
This adds some new buffers to the `FPUState` struct, which contains
enough space for the `xsave` instruction to run. This instruction writes
the upper part of the x86 SIMD registers (YMM0-15) to a seperate
256-byte area, as well as an "xsave header" describing the region.

If the underlying processor supports AVX, the `fxsave` instruction is no
longer used, as `xsave` itself implictly saves all of the SSE and x87
registers.

Co-authored-by: Leon Albrecht <leon.a@serenityos.org>
2022-05-15 12:25:23 +02:00
Timon Kruiper
9abcb6700c Kernel: Move Arch/x86/Spinlock.h and add stubs for aarch64
The code in Spinlock.h has no architectural specific logic, thus can be
moved to the Arch directory. This contains no functional change.

Also add the Spinlock.cpp file for aarch64 which contains stubs for the
lock and unlock functions.
2022-05-03 21:53:36 +02:00
Timon Kruiper
feba7bc8a8 Kernel: Move Kernel/Arch/x86/SafeMem.h to Kernel/Arch/SafeMem.h
The file does not contain any specific architectural code, thus it can
be moved to the Kernel/Arch directory.
2022-05-03 21:53:36 +02:00
Linus Groh
cd3e337487 Kernel: Strip null terminators from all CPUID strings, not just brand
I've noticed that the KVM hypervisor vendor ID string contained null
terminators in the serialized JSON string in /proc/cpuinfo - let's avoid
that, and err on the side of caution and strip them from all strings
built from CPUID register values. They may not be fixed width after all.
2022-04-26 20:20:44 +02:00
Linus Groh
62185452f0 Kernel: Query OS-enabled CPUID features again at the end of cpu_setup()
For OSPKE this is a no-op as we don't enable PKU yet, but the state of
the OSXSAVE flag might have changed if we enabled XSAVE.
2022-04-08 18:53:42 +01:00
Linus Groh
87aabb5ef7 Kernel: Rename OSPKU CPUID feature flag to OSPKE
Unlike "XSAVE enabled by OS" being indicated by the OSXSAVE flag, in
this case it's "PKU enabled by OS" being indicated by the OSPKE flag.
2022-04-08 18:53:42 +01:00
Linus Groh
b0f701d053 Kernel: Convert ProcessorInfo::build_brand_string() to StringBuilder 2022-04-03 23:20:33 +02:00
Linus Groh
3f9c2495e5 Kernel: Remove EBX, ECX, and EDX values from hypervisor dmesgln() 2022-04-03 23:20:33 +02:00
Linus Groh
8d96525b9d Kernel: Move hypervisor vendor ID string to ProcessorInfo
This will make it possible to expose it in /proc/cpuinfo. :^)
2022-04-03 23:20:33 +02:00
Linus Groh
f6181cd47e Kernel: Make ProcessorInfo::build_foo_string() private 2022-04-03 23:20:33 +02:00
Linus Groh
afce63fffc Kernel: Move feature string building to ProcessorInfo
Other than a dmesgln(), ProcessorInfo is the only user of this function
and is already responsible for building other CPUID-related strings.
2022-04-03 23:20:33 +02:00
Linus Groh
53a95a5347 Kernel: Rename some ProcessorInfo members to match Intel manual
Let's use terminology from the the Intel manual to avoid confusion.
Also add `_string` suffixes to better distinguish the numeric values
from the string values.
2022-04-03 23:20:33 +02:00
Linus Groh
ebe2cf8995 Kernel: Move private ProcessorInfo members to the end 2022-04-03 23:20:33 +02:00
James Mintram
d79c772c87 Kernel: Make MemoryManager compile on aarch64 2022-04-02 19:34:20 -07:00
James Mintram
6299a69253 Kernel: Make handle_crash available to aarch64 2022-04-02 19:34:20 -07:00
James Mintram
d3b6201b40 Kernel: Make PageDirectory.cpp compile on aarch64 2022-04-02 19:34:20 -07:00
James Mintram
783a44b18e Kernel: Make PhysicalRegion.cpp compile on aarch64 2022-04-02 19:34:20 -07:00
James Mintram
627fd231d5 Kernel: Make Region.cpp compile on aarch64 2022-04-02 19:34:20 -07:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Linus Groh
1e82c2708d Kernel: Support all AMD-defined CPUID feature flags for EAX=80000001h
We're now able to detect all the AMD-defined CPUID feature flags from
ECX/EDX for EAX=80000001h :^)
2022-03-27 18:54:56 +02:00
Linus Groh
96e6420d8d Kernel: Support all Intel-defined extended CPUID feature flags for EAX=7
We're now able to detect all the extended CPUID feature flags from
EBX/ECX/EDX for EAX=7 :^)
2022-03-27 18:54:56 +02:00
Linus Groh
6ca03b915e Kernel: Support all Intel-defined CPUID feature flags for EAX=1
We're now able to detect all the regular CPUID feature flags from
ECX/EDX for EAX=1 :^)

None of the new ones are being used for anything yet, but they will show
up in /proc/cpuinfo and subsequently lscpu and SystemMonitor.

Note that I replaced the periods from the SSE 4.1 and 4.2 instructions
with underscores, which matches the internal enum names, Linux's
/proc/cpuinfo and the general pattern of replacing special characters
with underscores to limit feature names to [a-z0-9_].

The enum member stringification has been moved to a new function for
better re-usability and to avoid cluttering up Processor.cpp.
2022-03-27 18:54:56 +02:00
Linus Groh
bc7ec02a82 Kernel: Implement CPUFeature as an ArbitrarySizedEnum
This will make it possible to add many, many more CPU features - more
than the current limit 32 and later limit of 64 if we stick with an enum
class to be specific :^)
2022-03-27 18:54:56 +02:00