Commit Graph

47 Commits

Author SHA1 Message Date
Andreas Kling
1d94b5eb04 Kernel: Add a random offset to kernel stacks upon syscall entry
When entering the kernel from a syscall, we now insert a small bit of
stack padding after the RegisterDump. This makes kernel stacks less
deterministic across syscalls and may make some bugs harder to exploit.

Inspired by Elena Reshetova's talk on kernel stack exploitation.
2020-01-01 23:21:24 +01:00
Andreas Kling
38f93ef13b Kernel: Disable x86 RDTSC instruction in userspace
It's still possible to read the TSC via the read_tsc() syscall, but we
will now clear some of the bottom bits for unprivileged users.
2020-01-01 18:22:20 +01:00
Andreas Kling
37329c2009 Kernel: Fix typo in Descriptor::set_limit()
x86 descriptor limits are 20 bytes, not 24 bytes. This was already
a 4-bit wide bitfield, so no damage done, but let's be correct.
2020-01-01 17:21:43 +01:00
Andreas Kling
fd740829d1 Kernel: Switch to eagerly restoring x86 FPU state on context switch
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.
2020-01-01 16:54:21 +01:00
Andreas Kling
9c0836ce97 Kernel: Enable x86 UMIP (User Mode Instruction Prevention) if supported
This prevents code running outside of kernel mode from using the
following instructions:

* SGDT - Store Global Descriptor Table
* SIDT - Store Interrupt Descriptor Table
* SLDT - Store Local Descriptor Table
* SMSW - Store Machine Status Word
* STR - Store Task Register

There's no need for userspace to be able to use these instructions so
let's just disable them to prevent information leakage.
2020-01-01 13:21:15 +01:00
Andreas Kling
5aeaab601e Kernel: Move CPU feature detection to Arch/x86/CPU.{cpp.h}
We now refuse to boot on machines that don't support PAE since all
of our paging code depends on it.

Also let's only enable SSE and PGE support if the CPU advertises it.
2020-01-01 12:57:00 +01:00
Andreas Kling
c22a4301ed Kernel: Interpret "reserved bit violation" page faults correctly
We don't actually react to these in any meaningful way other than
crashing, but let's at least print the correct information. :^)
2019-12-25 13:35:57 +01:00
Andreas Kling
52deb09382 Kernel: Enable PAE (Physical Address Extension)
Introduce one more (CPU) indirection layer in the paging code: the page
directory pointer table (PDPT). Each PageDirectory now has 4 separate
PageDirectoryEntry arrays, governing 1 GB of VM each.

A really neat side-effect of this is that we can now share the physical
page containing the >=3GB kernel-only address space metadata between
all processes, instead of lazily cloning it on page faults.

This will give us access to the NX (No eXecute) bit, allowing us to
prevent execution of memory that's not supposed to be executed.
2019-12-25 13:35:57 +01:00
Andreas Kling
336ac9e8e7 Kernel: Clean up CPU fault register dumps
These were looking a bit messy after we started using 32-bit fields
to store segment registers in RegisterDumps.
2019-12-25 02:58:03 +01:00
Andreas Kling
f4b2b72c8e Kernel: Remove AK_MAKE_NONCOPYABLE from PDE and PTE classes
This avoids -Wclass-memaccess warnings exposed by the new Makefiles.
2019-12-20 20:20:23 +01:00
Andreas Kling
f01fd54d1b Kernel: Make separate kernel entry points for each PIC IRQ
Instead of having a common entry point and looking at the PIC ISR to
figure out which IRQ we're servicing, just make a separate entryway
for each IRQ that pushes the IRQ number and jumps to a common routine.

This fixes a weird issue where incoming network packets would sometimes
cause the mouse to stop working. I didn't track it down further than
realizing we were sometimes EOI'ing the wrong IRQ.
2019-12-15 12:47:53 +01:00
Andreas Kling
e49d6cc7e9 Kernel: Tidy up kernel entry points a little bit
Now that we can see the kernel entry points all the time in profiles,
let's tweak the names a little bit and switch to named exceptions.
2019-12-14 16:16:57 +01:00
Andreas Kling
f692577559 Kernel: Disable interrupts while modifying the PIC IMR 2019-12-14 16:16:37 +01:00
Andreas Kling
7d0583a33f Kernel: Ignore IRQ 15 for now
Sometimes QEMU hits us with an IRQ 15 and I don't know what it is.
Just ignore it for now instead of crashing the system.
2019-12-14 12:21:39 +01:00
Andreas Kling
a0e38922bd Kernel: Break out of the idle loop on WaitQueue wake instead of on IRQ
Now that we have proper wait queues to drive waiter wakeup, we can use
the wake actions to break out of the scheduler's idle loop when we've
got a thread to run.
2019-12-08 00:33:35 +01:00
Andreas Kling
9ed272ce98 Kernel: Disable interrupts while setting up a thread blocker
There was a race window between instantiating a WaitQueueBlocker and
setting the thread state to Blocked. If a thread was preempted between
those steps, someone else might try to wake the wait queue and find an
unblocked thread in a wait queue, which is not sane.
2019-12-01 12:47:33 +01:00
Andreas Kling
9a157b5e81 Revert "Kernel: Move Kernel mapping to 0xc0000000"
This reverts commit bd33c66273.

This broke the network card drivers, since they depended on kmalloc
addresses being identity-mapped.
2019-11-23 17:27:09 +01:00
Jesse Buhagiar
bd33c66273 Kernel: Move Kernel mapping to 0xc0000000
The kernel is now no longer identity mapped to the bottom 8MiB of
memory, and is now mapped at the higher address of `0xc0000000`.

The lower ~1MiB of memory (from GRUB's mmap), however is still
identity mapped to provide an easy way for the kernel to get
physical pages for things such as DMA etc. These could later be
mapped to the higher address too, as I'm not too sure how to
go about doing this elegantly without a lot of address subtractions.
2019-11-22 16:23:23 +01:00
Andreas Kling
794758df3a Kernel: Implement some basic stack pointer validation
VM regions can now be marked as stack regions, which is then validated
on syscall, and on page fault.

If a thread is caught with its stack pointer pointing into anything
that's *not* a Region with its stack bit set, we'll crash the whole
process with SIGSTKFLT.

Userspace must now allocate custom stacks by using mmap() with the new
MAP_STACK flag. This mechanism was first introduced in OpenBSD, and now
we have it too, yay! :^)
2019-11-17 12:15:43 +01:00
Andreas Kling
b285a1944e Kernel: Clear the x86 DF flag when entering the kernel
The SysV ABI says that the DF flag should be clear on function entry.
That means we have to clear it when jumping into the kernel from some
random userspace context.
2019-11-09 22:42:19 +01:00
supercomputer7
c3c905aa6c Kernel: Removing hardcoded offsets from Memory Manager
Now the kernel page directory and the page tables are located at a
safe address, to prevent from paging data colliding with garbage.
2019-11-08 17:38:23 +01:00
Andreas Kling
9a4b117f48 Kernel: Simplify kernel entry points slightly
It was silly to push the address of the stack pointer when we can also
just change the callee argument to be a value type.
2019-11-06 13:15:55 +01:00
Andreas Kling
1c6f8d3cbd Kernel: Don't build with -mregparm=3
It was really confusing to have different calling conventions in kernel
and userspace. Also this has prevented us from linking with libgcc.
2019-11-06 13:04:47 +01:00
Andreas Kling
5b7f8634e3 Kernel: Set the G (global) bit for kernel page tables
Since the kernel page tables are shared between all processes, there's
no need to (implicitly) flush the TLB for them on every context switch.

Setting the G bit on kernel page tables allows the CPU to keep the
translation caches around.
2019-11-03 23:51:55 +01:00
Andreas Kling
dc35b1d647 Kernel: Remove nonsense in bootstrap code
This code was not doing anything important. Since we're building the
kernel with -mregparm=3, the first function argument goes in %eax.
2019-11-03 14:40:28 +01:00
Tidux
d09a28856f Kernel: Move Boot/ into Arch/i386/Boot (#667) 2019-10-20 08:15:39 +02:00
Tom
00a7c48d6e APIC: Enable APIC and start APs 2019-10-16 19:14:02 +02:00
Andreas Kling
98c7fd7aed Kernel: Clarify code that saves FPU state after FNINIT
After we clear the FPU state in a thread when it uses the FPU for the
first time, we also save the clean slate in the thread's FPU state
buffer. When we're doing that, let's write through current->fpu_state()
just to make it clear what's going on.

It was actually safe, since we'd just overwritten the g_last_fpu_thread
pointer anyway, but this patch improves the communication of intent.

Spotted by Bryan Steele, thanks!
2019-10-13 20:39:59 +02:00
Andreas Kling
44fb71261a Kernel: Fix accidental restore of bogus FPU state after fork
Cloned threads (basically, forked processes) inherit the complete FPU
state of their origin thread. There was a bug in the lazy FPU state
save/restore mechanism where a cloned thread would believe it had a
buffer full of valid FPU state (because the inherited flag said so)
but the origin thread had never actually copied any FPU state into it.

This patch fixes that by forcing out an FPU state save after doing
the initial FPU initialization (FNINIT) in a thread. :^)
2019-10-13 14:39:04 +02:00
Drew Stratford
c136fd3fe2 Kernel: Send SIGSEGV on seg-fault
Now programs can catch the SIGSEGV signal when they segfault.

This commit also introduced the send_urgent_signal_to_self method,
which is needed to send signals to a thread when handling exceptions
caused by the same thread.
2019-10-07 16:39:47 +02:00
Drew Stratford
7fc903b97a Kernel: Add exception_code to RegisterDump.
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-07 16:39:47 +02:00
Andreas Kling
e1481dcb42 Kernel: Stop idling after handling an IRQ
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-14 20:21:10 +02:00
Drew Stratford
e529042895 Kernel: Remove reduntant kernel/user signal stacks.
Due to the changes in signal handling m_kernel_stack_for_signal_handler_region
and m_signal_stack_user_region are no longer necessary, and so, have been
removed. I've also removed the similarly reduntant m_tss_to_resume_kernel.
2019-09-09 08:35:43 +02:00
Andreas Kling
e29fd3cd20 Kernel: Display virtual addresses as V%p instead of L%x
The L was a leftover from when these were called linear addresses.
2019-08-26 11:31:58 +02:00
Andreas Kling
272bd1d3ef Kernel: Make crash dumps look aligned once again
This broke with the recent changes to make printf hex fields behave
a bit more correctly.
2019-08-17 21:29:46 +02:00
Andreas Kling
945f8eb22a Kernel: Don't treat read faults like CoW exceptions
I'm not sure why we would have a non-readable CoW region, but I suppose
we could, so let's not Copy-on-Read in those cases.
2019-08-06 09:39:39 +02:00
Andreas Kling
af4cf01560 Kernel: Clean up the page fault handling code a bit
Not using "else" after "return" unnests the code and makes it easier to
follow. Also use an enum for the two different page fault types.
2019-08-06 09:33:35 +02:00
Andreas Kling
eca5c2bdf8 Kernel: Move VirtualAddress.h into VM/ 2019-07-09 15:04:45 +02:00
Andreas Kling
6c87d3afa9 Kernel: Move i8253.cpp => Arch/i386/PIT.cpp 2019-07-09 15:04:45 +02:00
Andreas Kling
9fdcede491 Kernel: Move PIC.cpp into Arch/i386/ 2019-07-09 15:04:43 +02:00
Andreas Kling
27f699ef0c AK: Rename the common integer typedefs to make it obvious what they are.
These types can be picked up by including <AK/Types.h>:

* u8, u16, u32, u64 (unsigned)
* i8, i16, i32, i64 (signed)
2019-07-03 21:20:13 +02:00
Andreas Kling
f6498bb0e9 Kernel: Make more crash info show up in dmesg.
kprintf() shows up in dmesg, dbgprintf() does not.
2019-06-30 15:02:23 +02:00
Andreas Kling
183205d51c Kernel: Make the x86 paging code slightly less insane.
Instead of PDE's and PTE's being weird wrappers around dword*, just have
MemoryManager::ensure_pte() return a PageDirectoryEntry&, which in turn has
a PageTableEntry* entries().

I've been trying to understand how things ended up this way, and I suspect
it was because I inadvertently invoked the PageDirectoryEntry copy ctor in
the original work on this, which must have made me very confused..

Anyways, now things are a bit saner and we can move forward towards a better
future, etc. :^)
2019-06-26 21:45:56 +02:00
Andreas Kling
048705e1c2 Kernel: Share code between all the exceptions that cause process crash. 2019-06-25 05:55:18 +02:00
Andreas Kling
8c0ae711d8 Kernel+LibC: Make page fault crashes a bit more readable.
We'll now try to detect crashes that were due to dereferencing nullptr,
uninitialized malloc() memory, or recently free()'d memory.
It's not perfect but I think it's pretty good. :^)

Also added some color to the most important parts of the crash log,
and added some more modes to /bin/crash for exercising this code.

Fixes #243.
2019-06-19 20:52:12 +02:00
Andreas Kling
15bea7153a Kernel: Symbolicate the crash address too, not just the call stack.
Also print it in shiny red to make it extra easy to spot. :^)
Fixes #244.
2019-06-19 18:51:17 +02:00
Andreas Kling
736092a087 Kernel: Move i386.{cpp,h} => Arch/i386/CPU.{cpp,h}
There's a ton of work that would need to be done before we could spin up on
another architecture, but let's at least try to separate things out a bit.
2019-06-07 20:02:01 +02:00