Commit Graph

5481 Commits

Author SHA1 Message Date
Andreas Kling
0672163840 Kernel: Simplify AnonymousVMObject copy constructor
It was doing a bunch of things it didn't need to do. I think we had
misunderstood the base class as having copied m_lock in its copy
constructor but it's actually default initialized.
2021-08-05 17:41:58 +02:00
Andreas Kling
fa627c1eb2 Kernel: Use RAII to manage committed physical pages
We had issues with committed physical pages getting miscounted in some
situations, and instead of figuring out what was going wrong and making
sure all the commits had matching uncommits, this patch makes the
problem go away by adding an RAII class to manage this instead. :^)

MemoryManager::commit_user_physical_pages() now returns an (optional)
CommittedPhysicalPageSet. You can then allocate pages from the page set
by calling take_one() on it. Any unallocated pages are uncommitted upon
destruction of the page set.
2021-08-05 17:41:58 +02:00
Andreas Kling
ec49213f7b Kernel: Add MemoryManager to Forward.h 2021-08-05 17:41:58 +02:00
Idan Horowitz
ffee3d6c5d Kernel: Remove the always 1-sized super physical regions Vector
Since we only ever add 1 super physical region, theres no reason to
add the extra redirection and allocation that comes with a dynamically
sized Vector.
2021-08-04 21:02:40 +02:00
Luke
cbbbc38f27 Kernel: Print panic backtrace to both the screen and serial
Previously it would only print the backtrace to serial, which would be
inaccessible if you don't have serial setup.
2021-08-04 20:14:54 +02:00
Liav A
7c617394a1 Kernel: Ensure we read valid values from the RTC CMOS registers
We try to read twice from the RTC CMOS registers, and if the values are
not the same for 5 attempts, we know there's a malfunction with the
hardware so we declare these values as bogus in the kernel log.
2021-08-04 19:53:04 +02:00
Liav A
517460d3a9 Kernel: Ensure we don't get in an endless loop while querying the CMOS
When we try to query the time from the RTC CMOS, we try to check if
the CMOS is updated. If it is updated for a long period of time (as a
result of hardware malfunction), break the loop and return Unix epoch
time.
2021-08-04 19:53:04 +02:00
sin-ack
bed51d856a AK+Kernel: Print TODO when a TODO() is executed
Previously we would just print "ASSERTION FAILED: false", which was
kinda cryptic and also didn't make it clear whether this was a TODO or
an unreachable condition. Now, we actually print "ASSERTION FAILED:
TODO", making it crystal clear.
2021-08-04 11:01:16 +02:00
Brian Gianforcaro
2caafacd9b Kernel: Remove OOM unsafe API KBuffer::create_with_size 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
720a686a76 Kernel: Handle OOM when allocating Packet KBuffers 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
8c4785bd10 Kernel: Use normal initialization for TCPPacket instead of memset 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
c1a0e379e6 Kernel: Handle OOM when allocating IPv4Socket optional scratch buffer 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
a6db2f985a Kernel: Handle OOM in DiskCache when mounting Ext2 filesystems
Create the disk cache up front, so we can verify it succeeds.
Make the KBuffer allocation fail-able, so we can properly handle
failure when the user asks up to mount a Ext2 filesystem under
OOM conditions.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
187c086270 Kernel: Handle OOM from KBuffer creation in sys$module() 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
cbb263e350 Kernel: Remove OOM unsafe DoubleBuffer constructor
Remove this dangerous and now unused constructor.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
ca94a83337 Kernel: Handle OOM from DoubleBuffer usage in IPv4Socket
The IPv4Socket requires a DoubleBuffer for storage of any data it
received on the socket. However it was previously using the default
constructor which can not observe allocation failure. Address this by
plumbing the receive buffer through the various derived classes.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
109c885585 Kernel: Handle OOM from DoubleBuffer usage in Net/LocalSocket
LocalSockets keep a DoubleBuffer for both client and server usage.
This change converts the usage from using the default constructor
which is unable to observe OOM, to the new try_create factory and
plumb the result through the constructor.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
8d3b819daf Kernel: Handle OOM from DoubleBuffer creation in FIFO creation 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
15cd5d324c Kernel: Handle OOM from KBuffer usage in Ext2FS::get_bitmap_block()
Fixes up error handling on an OOM-able path, and removes one more usage
of KBuffer::create_with_size.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
43f930d3aa Kernel: Convert MasterPTY creation to use DoubleBuffer factory
In order to remove the public DoubleBuffer constructor, we need to
convert the callers to the factory instead, allowing the caller to
observe OOM.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
f816abcbad Kernel: Add DoubleBuffer::try_create() factory method for OOM hardening
We need to expose the ability for DoubleBuffer creation to expose
failure, as DoubleBuffer depends on KBuffer, which also has to be able
to expose failure during OOM.

We will remove the non OOM API once all users have been converted.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
fc91eb365d Kernel: Do not cancel stale timers when servicing sys$alarm
The sys$alarm() syscall has logic to cache a m_alarm_timer to avoid
allocating a new timer for every call to alarm. Unfortunately that
logic was broken, and there were conditions in which we could have
a timer allocated, but it was no longer on the timer queue, and we
would attempt to cancel that timer again resulting in an infinite
loop waiting for the timers callback to fire.

To fix this, we need to track if a timer is currently in use or not,
allowing us to avoid attempting to cancel inactive timers.

Luke and Tom did the initial investigation, I just happened to have
time to write a repro and attempt a fix, so I'm adding them as the
as co-authors of this commit.

Co-authored-by: Luke <luke.wilde@live.co.uk>
Co-authored-by: Tom <tomut@yahoo.com>
2021-08-03 18:44:01 +02:00
Lenny Maiorani
97bd13264a Everywhere: Make use of container version of all_of
Problem:
- New `all_of` implementation takes the entire container so the user
  does not need to pass explicit begin/end iterators. This is unused
  except is in tests.

Solution:
- Make use of the new and more user-friendly version where possible.
2021-08-03 10:46:43 +02:00
Andreas Kling
26d7261347 Kernel: Make NullDevice (/dev/null) seekable 2021-08-03 10:36:48 +02:00
Andreas Kling
821a6e8b4c Kernel: Remap regions after changing purgeable VM object volatile flag
Otherwise we could end up with stale page table mappings that give us
erroneous Protection Violation faults when accessing valid addresses.
2021-08-03 10:23:36 +02:00
Thomas Wagenveld
1f078827c4 Kernel: Set initial link up status for RTL8139
On startup the link was assumed to be down, making the adapter not work
until the link up status was cycled.
2021-08-03 10:20:51 +02:00
Thomas Wagenveld
32c8d35ef0 Kernel: Expose link speed and duplex through /proc/net/adapters
Add the fields 'link_speed' (integer or -1) for the speed and
'link_full_duplex' (true for full, false for half) to indicate
link duplex.
2021-08-03 10:20:51 +02:00
Thomas Wagenveld
59fdeec7f5 Kernel: Add interface to read link speed and duplex for NetworkAdapter
Read the appropriate registers for RTL8139, RTL8168 and E1000.
For NE2000 just assume 10mbit full duplex as there is no indicator
for it in the pure NE2000 spec. Mock values for loopback.
2021-08-03 10:20:51 +02:00
brapru
9c3e6f3f63 Kernel: Send RST/ACK if no socket is available
Previously there was no way for Serenity to send a packet without an
established socket connection, and there was no way to appropriately
respond to a SYN packet on a non-listening port. This patch will respond
to any non-established socket attempts with the appropraite RST/ACK,
letting the client know to close the connection.
2021-08-02 02:45:56 +02:00
brapru
63a15ed19d Kernel: Do not send delayed ack in response to RST/ACK
In accordance with RFC 793, if the receiver is in the SYN-SENT state
it should respond to a RST by aborting the connection and immediately
move to the CLOSED state.

Previously the system would ACK all RST/ACKs, and the remote peer would
just respond with more RST packets.
2021-08-02 02:45:56 +02:00
LuK1337
3dd40535c1 VirtualFileSystem: Don't let rename() overwrite non-empty directory
According to POSIX, rename shouldn't succeed if newpath is a non-empty
directory.
2021-08-02 01:04:34 +02:00
brapru
ea2abb3200 Kernel: Convert NetworkTask to east-const style 2021-08-02 00:32:55 +02:00
Brian Gianforcaro
0fc853f5ba Kernel: Remove ThreadTracer.h include from Process.h / Thread.h
This isn't needed for Process / Thread as they only reference it
by pointer and it's already part of Kernel/Forward.h. So just include
it where the implementation needs to call it.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
44e992429f Kernel: Only include AK/SourceLocation.h if LOCK_DEBUG is enabled
Don't pay the header inclusion cost if we aren't compiling with the
LOCK_DEBUG option enabled.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
14c674183b Kernel: Only include KCOVDevice.h if ENABLE_KERNEL_COVERAGE_COLLECTION
There's no reason to include this header if we aren't going to actually
use it.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
ed996fcced Kernel: Remove unused header includes 2021-08-01 08:10:16 +02:00
Andreas Kling
363a901603 Kernel: Copy the "purgeable" flag when cloning AnonymousVMObject 2021-07-31 11:22:53 +02:00
Brian Gianforcaro
7fce0693a5 Prekernel: Disable KASAN, so it has no effect when enabled
I was working on some more KASAN changes and realized the system
no longer links when passing -DENABLE_KERNEL_ADDRESS_SANITIZER=ON.

Prekernel will likely never have KASAN support given it's limited
environment, so just suppress it's usage.
2021-07-30 16:58:09 +02:00
Andreas Kling
25b76462bf Revert "Kernel: Share committed COW pages between whole VMObject lineage"
This reverts commit 2c0df5e7e7.

This caused OOM on CI, so let's roll it back until we can figure it out.
2021-07-30 13:50:24 +02:00
Andreas Kling
2c0df5e7e7 Kernel: Share committed COW pages between whole VMObject lineage
When cloning an AnonymousVMObject, committed COW pages are shared
between the parent and child object. Whicever object COW's first will
take the shared committed page, and if the other object ends up doing
a COW as well, it will notice that the page is no longer shared by
two objects and simple remap it as read/write.

When a child is COW'ed again, while still having shared committed
pages with its own parent, the grandchild object will now join in the
sharing pool with its parent and grandparent. This means that the first
2 of 3 objects that COW will draw from the shared committed pages, and
3rd will remap read/write.

Previously, we would "fork" the shared committed pages when cloning,
which could lead to a situation where the grandparent held on to 1 of
the 3 needed shared committed pages. If both the child and grandchild
COW'ed, they wouldn't have enough pages, and since the grandparent
maintained an extra +1 ref count on the page, it wasn't possible to
to remap read/write.
2021-07-30 13:17:55 +02:00
Andreas Kling
bccdc08487 Kernel: Unmapping a non-mapped region with munmap() should be a no-op
Not a regression per se from 0fcb9efd86
since we were crashing before that which is obviously worse.
2021-07-30 13:16:55 +02:00
Brian Gianforcaro
0fcb9efd86 Kernel: Return an error when unmap finds no intersecting region
We currently always crash if a user attempts to unmap a range that
does not intersect with an existing region, no matter the size. This
happens because we will never explicitly check to see if the search
for intersecting regions found anything, instead loop over the results,
which might be an empty vector. We then attempt to deallocate the
requested range from the `RangeAllocator` unconditionally, which will
be invalid if the specified range is not managed by the RangeAllocator.
We will assert validating m_total_range.contains(..) the range we are
requesting to deallocate.

This fix to this is straight forward, error out if we weren't able to
find any intersections.

You can get stress-ng to attempt this pattern with the following
arguments, which will attempt to unmap 0x0 through some large offset:

```
stress-ng --vm-segv 1
```

Fixes: #8483

Co-authored-by: Federico Guerinoni <guerinoni.federico@gmail.com>
2021-07-30 11:28:55 +02:00
Luke
e3b588a43d Kernel+LibC: Add linger to sys/sockets.h
Also adds SO_BROADCAST in UnixTypes.h to match sys/sockets.h.
Required by bash 5.1.8.
2021-07-29 19:35:03 +01:00
Gunnar Beutner
b7ca269b4d Kernel: Use our toolchain's c++filt tool for the kernel map
The host's version of c++filt might not work on some operating systems,
e.g. macOS.
2021-07-29 10:38:31 +02:00
Gunnar Beutner
a68d912dc0 Kernel: Embed the right symbol count into the kernel.map file on macOS
On macOS the wc tool prefixes its output with a bunch of spaces which
resulted in us incorrectly using "00000000" as the symbol count.

Fixes #9080.
2021-07-29 10:38:31 +02:00
Gunnar Beutner
3cfb1787b8 Kernel: Print relative symbol addresses in dump_backtrace_impl
By subtracting the load base we get addresses which the user can paste
into addr2line.
2021-07-28 23:43:58 +02:00
Andreas Kling
b807f1c3fc Kernel: Fail madvise() volatile change with EINVAL for non-purgeable mem
AnonymousVMObject::set_volatile() assumes that nobody ever calls it on
non-purgeable objects, so let's make sure we don't do that.

Also return EINVAL instead of EPERM for non-anonymous VM objects so the
error codes match.
2021-07-28 20:42:49 +02:00
Brian Gianforcaro
ddc950ce42 Kernel: Avoid file descriptor leak in Process::sys$socketpair on error
Previously it was possible to leak the file descriptor if we error out
after allocating the first descriptor. Now we perform both fd
allocations back to back so we can handle the potential error when
processing the second fd allocation.
2021-07-28 19:07:00 +02:00
Brian Gianforcaro
4b2651ddab Kernel: Track allocated FileDescriptionAndFlag elements in each Process
The way the Process::FileDescriptions::allocate() API works today means
that two callers who allocate back to back without associating a
FileDescription with the allocated FD, will receive the same FD and thus
one will stomp over the other.

Naively tracking which FileDescriptions are allocated and moving onto
the next would introduce other bugs however, as now if you "allocate"
a fd and then return early further down the control flow of the syscall
you would leak that fd.

This change modifies this behavior by tracking which descriptions are
allocated and then having an RAII type to "deallocate" the fd if the
association is not setup the end of it's scope.
2021-07-28 19:07:00 +02:00
Brian Gianforcaro
ba03b6ad02 Kernel: Make Process::FileDescriptions::allocate return KResultOr<int>
Modernize more error checking by utilizing KResultOr.
2021-07-28 19:07:00 +02:00
Brian Gianforcaro
d2cee9cbf6 Kernel: Remove unused fd allocation from Process::sys$connect(..) 2021-07-28 19:07:00 +02:00
Andreas Kling
07141434e0 Kernel/ProcFS: Make various things superuser-only once again
File and directory permissions regressed with the recent ProcFS changes.
This patch restores the superuser-only permissions where appropriate.
2021-07-28 18:59:53 +02:00
Andreas Kling
58f62cd1d0 Kernel/ProcFS: Add S_IFREG bit to regular files in /proc
Regular files should have regular file mode.
2021-07-28 18:55:38 +02:00
Gunnar Beutner
3a4d3b15e3 Kernel: Fix CPU initialization for SMP
This was broken by the KASLR changes.
2021-07-27 19:45:38 +02:00
Andreas Kling
c69e147b8b Kernel: Improve some comments in Space
Remove bogus FIXME's and improve some comments.
2021-07-27 15:04:36 +02:00
Andreas Kling
a085168c52 Kernel: Rename Space::create => Space::try_create() 2021-07-27 14:54:35 +02:00
Andreas Kling
8f6bc7fd10 Kernel: Mark the stack check guard as READONLY_AFTER_INIT
This makes it harder for an exploit to replace the kernel's randomized
canary value since the memory containing it will be mapped read-only.
2021-07-27 14:50:10 +02:00
Andreas Kling
84d3428ab3 Kernel: Remove a handful of unused member functions in Processor 2021-07-27 14:38:04 +02:00
Andreas Kling
1e43292c3b Kernel: Introduce ProcessorSpecific<T> for per-CPU data structures
To add a new per-CPU data structure, add an ID for it to the
ProcessorSpecificDataID enum.

Then call ProcessorSpecific<T>::initialize() when you are ready to
construct the per-CPU data structure on the current CPU. It can then
be accessed via ProcessorSpecific<T>::get().

This patch replaces the existing hard-coded mechanisms for Scheduler
and MemoryManager per-CPU data structure.
2021-07-27 14:32:30 +02:00
Andreas Kling
559ab00249 Kernel: Remove unused Region::translate_vmobject_page_range() 2021-07-27 13:17:33 +02:00
Gunnar Beutner
57417a3d6e Kernel: Support loading the kernel at almost arbitrary virtual addresses
This enables further work on implementing KASLR by adding relocation
support to the pre-kernel and updating the kernel to be less dependent
on specific virtual memory layouts.
2021-07-27 13:15:16 +02:00
Gunnar Beutner
b10a86d463 Prekernel: Export some multiboot parameters in our own BootInfo struct
This allows us to specify virtual addresses for things the kernel should
access via virtual addresses later on. By doing this we can make the
kernel independent from specific physical addresses.
2021-07-27 13:15:16 +02:00
Gunnar Beutner
3c616ae00f Kernel: Make the kernel independent from specific physical addresses
Previously the kernel relied on a fixed offset between virtual and
physical addresses based on the kernel's load address. This allows us
to specify an independent offset.
2021-07-27 13:15:16 +02:00
Maciej Zygmanowski
9efeecf903 Kernel: Make LoopbackAdapter always link up 2021-07-27 00:28:12 +02:00
Patrick Meyer
d5fdb97a81 Kernel: Fix integer overflow in KCOV_SETBUFSIZE ioctl 2021-07-26 23:52:15 +02:00
Ali Mohammad Pur
e76af0fe16 Kernel: Make KCOVDevice::ioctl() return KResult
Recent ioctl() changes broke this, this commit fixes that
and the build.
2021-07-27 01:38:06 +04:30
Liav A
713b18b7a6 Kernel: Shutdown on panic in self-test mode
Instead of doing a reset via triple-fault, let's just shutdown the QEMU
virtual machine because this is already a QEMU-specific handling code
for Self-Test CI mode.
2021-07-27 01:25:04 +04:30
Brian Gianforcaro
de9ff0af50 Kernel: Modify the IOCTL API to return KResult
The kernel has been gradually moving towards KResult from just bare
int's, this change migrates the IOCTL paths.
2021-07-27 01:23:37 +04:30
Brian Gianforcaro
46c9b1d81c Kernel+LibC: Use argument for TIOCGPGRP ioctl value
In preparation for modifying the Kernel IOCTL API to return KResult
instead of int, we need to fix this ioctl to an argument to receive
it's return value, instead of using the actual function return value.
2021-07-27 01:23:37 +04:30
Brian Gianforcaro
9a04f53a0f Kernel: Utilize AK::Userspace<T> in the ioctl interface
It's easy to forget the responsibility of validating and safely copying
kernel parameters in code that is far away from syscalls. ioctl's are
one such example, and bugs there are just as dangerous as at the root
syscall level.

To avoid this case, utilize the AK::Userspace<T> template in the ioctl
kernel interface so that implementors have no choice but to properly
validate and copy ioctl pointer arguments.
2021-07-27 01:23:37 +04:30
Patrick Meyer
83f88df757 Kernel: Add option to build with coverage instrumentation and KCOV
GCC and Clang allow us to inject a call to a function named
__sanitizer_cov_trace_pc on every edge. This function has to be defined
by us. By noting down the caller in that function we can trace the code
we have encountered during execution. Such information is used by
coverage guided fuzzers like AFL and LibFuzzer to determine if a new
input resulted in a new code path. This makes fuzzing much more
effective.

Additionally this adds a basic KCOV implementation. KCOV is an API that
allows user space to request the kernel to start collecting coverage
information for a given user space thread. Furthermore KCOV then exposes
the collected program counters to user space via a BlockDevice which can
be mmaped from user space.

This work is required to add effective support for fuzzing SerenityOS to
the Syzkaller syscall fuzzer. :^) :^)
2021-07-26 17:40:28 +02:00
Ali Mohammad Pur
5dc6270b57 Kernel: Remove invalid '#' format modifier for printing a faulting addr
This was mistakenly added in 306d898ee5.
2021-07-26 14:12:09 +04:30
Ali Mohammad Pur
eb9c82a487 Kernel: Un-unmap-after-init CommandLine::boot_mode()
This function is now used when the kernel panics, so unmapping it would
make the kernel panic while in panic, which is not a good thing :P
2021-07-26 11:33:14 +02:00
Ali Mohammad Pur
306d898ee5 Kernel: Show the unmapped-after-init symbol being accessed
This makes it a lot easier to figure out what unmapped function is being
accessed, and a lot easier to reason about _why_ it is being accessed.
2021-07-26 11:33:14 +02:00
Brian Gianforcaro
f43423edc3 Build: Only specify -fzero-call-used-regs with compiler >= GCC 11.1
This fixes the use case of using clang, or building inside CLion with
an older host compiler.
2021-07-26 01:00:36 +02:00
Andreas Kling
50472fd69f Kernel: Don't try to return a committed page that we don't have
When we get a COW fault and discover that whoever we were COW'ing
together with has either COW'ed that page on their end (or they have
unmapped/exited) we simplify life for ourselves by clearing the COW
bit and keeping the page we already have. (No need to COW if the page
is not shared!)

The act of doing this does not return a committed page to the pool.
In fact, that committed page we had reserved for this purpose was used
up (allocated) by our COW buddy when they COW'ed the page.

This fixes a kernel panic when running TestLibCMkTemp. :^)
2021-07-26 00:39:10 +02:00
Andreas Kling
101486279f Kernel: Clear the COW bits when making an AnonymousVMObject volatile 2021-07-26 00:39:10 +02:00
Andreas Kling
7aed2cfc02 Kernel: Make some debug logging in Scheduler CPU agnostic 2021-07-26 00:39:10 +02:00
Andreas Kling
06104a4227 Kernel: Remove unused Scheduler::yield_from_critical() 2021-07-26 00:39:10 +02:00
Andreas Kling
cfce92f639 Kernel: Fix handful of clang-tidy warnings in Scheduler
All of them "static member accessed through instance".
2021-07-26 00:39:10 +02:00
Ali Mohammad Pur
54c54dabdd Kernel: PANIC() instead of manually halting the processor in abort() 2021-07-26 02:29:25 +04:30
Ali Mohammad Pur
6b606771b5 Kernel: Reset on panic in self-test mode
This makes a kernel panic immediately fail the on-target CI job.
Otherwise the failed job looks like a test timeout unless one digs into
the details of the job.
2021-07-26 02:29:25 +04:30
Andreas Kling
6a537ceef1 Kernel: Remove ContiguousVMObject, let AnonymousVMObject do the job
We don't need an entirely separate VMObject subclass to influence the
location of the physical pages.

Instead, we simply allocate enough physically contiguous memory first,
and then pass it to the AnonymousVMObject constructor that takes a span
of physical pages.
2021-07-25 18:44:47 +02:00
Andreas Kling
9a701eafc4 Kernel: Run clang-format on AnonymousVMObject.cpp 2021-07-25 18:21:59 +02:00
brapru
bdaaff70cb Utilities: Support static assignment of the ARP table 2021-07-25 17:57:08 +02:00
brapru
8313d35749 Kernel: Support ioctl SIOCSARP and SIOCDARP
Creates ioctl calls necessary to set/delete an entry from the ARP table
2021-07-25 17:57:08 +02:00
brapru
f8c104aaaf Kernel: Add update option to remove an entry from the ARP table
Allows for specifying whether to set/delete an entry from the table.
2021-07-25 17:57:08 +02:00
Andreas Kling
0d963fd641 Kernel: Remove unnecessary counting of VMObject-attached Regions
VMObject already has an IntrusiveList of all the Regions that map it.
We were keeping a counter in addition to this, and only using it in
a single place to avoid iterating over the list in case it only had
1 entry.

Simplify VMObject by removing this counter and always iterating the
list even if there's only 1 entry. :^)
2021-07-25 17:28:06 +02:00
Andreas Kling
ae3778c303 Kernel: Remove unused enum Region::SetVolatileError 2021-07-25 17:28:06 +02:00
Andreas Kling
4648bcd3d4 Kernel: Remove unnecessary weak pointer from Region to owning Process
This was previously used for a single debug logging statement during
memory purging. There are no remaining users of this weak pointer,
so let's get rid of it.
2021-07-25 17:28:06 +02:00
Andreas Kling
25a5fd870c Kernel: Add missing locking when registering VMObjectDeletedHandlers 2021-07-25 17:28:06 +02:00
Andreas Kling
09bc4cee15 Kernel: Remove unused madvise(MADV_GET_VOLATILE)
This was used to query the volatile state of a memory region, however
nothing ever actually used it.
2021-07-25 17:28:06 +02:00
Andreas Kling
5fb91e2e84 Kernel: Don't COW volatile VM objects
If a purgeable VM object is in the "volatile" state when we're asked
to make a COW clone of it, make life simpler by simply "purging"
the cloned object right away.

This effectively means that a fork()'ed child process will discover
its purgeable+volatile regions to be empty if/when it tries making
them non-volatile.
2021-07-25 17:28:05 +02:00
Andreas Kling
297c0748f0 Kernel: Minor cleanup around purge() during physical page allocation 2021-07-25 17:28:05 +02:00
Andreas Kling
2d1a651e0a Kernel: Make purgeable memory a VMObject level concept (again)
This patch changes the semantics of purgeable memory.

- AnonymousVMObject now has a "purgeable" flag. It can only be set when
  constructing the object. (Previously, all anonymous memory was
  effectively purgeable.)

- AnonymousVMObject now has a "volatile" flag. It covers the entire
  range of physical pages. (Previously, we tracked ranges of volatile
  pages, effectively making it a page-level concept.)

- Non-volatile objects maintain a physical page reservation via the
  committed pages mechanism, to ensure full coverage for page faults.

- When an object is made volatile, it relinquishes any unused committed
  pages immediately. If later made non-volatile again, we then attempt
  to make a new committed pages reservation. If this fails, we return
  ENOMEM to userspace.

mmap() now creates purgeable objects if passed the MAP_PURGEABLE option
together with MAP_ANONYMOUS. anon_create() memory is always purgeable.
2021-07-25 17:28:05 +02:00
Andreas Kling
deff554096 Kernel+LibSystem: Add a 4th syscall argument
Let's allow passing 4 function arguments to a syscall. The 4th argument
goes into ESI or RSI.
2021-07-25 14:08:50 +02:00
Thomas Wagenveld
9b57c6a013 Kernel/NE2000: Harvest entropy from NE2000 interrupts 2021-07-24 21:28:22 +02:00
Thomas Wagenveld
e788bbdb55 Kernel/NE2000: Assume link status is up
Right now, NE2000 NICs don't work because the link is down by default
and this will never change. Of all the NE2000 documentation I looked
at I could not find a link status indicator, so just assume the link
is up.
2021-07-24 21:28:22 +02:00
Thomas Wagenveld
de2d5d6a7e Kernel/NE2000: Correct receive ring buffer wrap-around
next_packet_page points to a page, but was being compared to a byte
offset rather than a page offset when adjusting the BOUNDARY register
when the ring buffer wraps around.

Fixes #8327.
2021-07-24 21:28:22 +02:00
Liav A
3645861f31 Kernel: Put a note about the unconditional unblanking of bochs-display
This removes the FIXME note and explains why it's not so bad to do this.
2021-07-24 01:42:10 +02:00
Gunnar Beutner
54fb5637e7 Kernel: Add missing .globl definitions
This ensures that we can properly take the address of these symbols in
other code.
2021-07-23 22:13:43 +02:00
Gunnar Beutner
8642c831cf Kernel: Mark a few more things as READONLY_AFTER_INIT 2021-07-23 20:23:09 +02:00
Gunnar Beutner
18f8d08b98 Kernel: Always build the kernel without default libs
When building the kernel from within SerenityOS we would link it against
default libs which doesn't really make sense to me.
2021-07-23 19:06:51 +02:00
Gunnar Beutner
a6c4a4d2fc Kernel: Make some of the assembly code position-independent on x86_64 2021-07-23 19:06:51 +02:00
Gunnar Beutner
412ce31f7f Prekernel: Don't build the prekernel as a PIE image
This is unnecessary because the prekernel is always loaded at a known
base address.
2021-07-23 19:06:51 +02:00
Gunnar Beutner
0edc17ee76 Kernel: Make -pie work for x86_64 2021-07-23 19:06:51 +02:00
Brian Gianforcaro
9d8482c3e8 Kernel: Use StringView when parsing pledges in sys$pledge(..)
This ensures no potential allocation as in some cases the pledge char*
could be promoted to AK::String by the compiler to execute the
comparison.
2021-07-23 19:02:25 +02:00
Brian Gianforcaro
e4b86aa5d8 Kernel: Fix bug where we half apply pledges in sys$pledge(..)
This bug manifests it self when the caller to sys$pledge() passes valid
promises, but invalid execpromises. The code would apply the promises
and then return an error for the execpromises. This leaves the user in
a confusing state, as the promises were silently applied, but we return
an error suggesting the operation has failed.

Avoid this situation by tweaking the implementation to only apply the
promises / execpromises after all validation has occurred.
2021-07-23 19:02:25 +02:00
Brian Gianforcaro
36ff717c54 Kernel: Migrate sys$pledge to use the KString API
This avoids potential unhandled OOM that's possible with the old
copy_string_from_user API.
2021-07-23 19:02:25 +02:00
Brian Gianforcaro
8acbe03342 Kernel: Annotate kernel_base and friends as READONLY_AFTER_INIT
We don't want kernel_base to be modifiable by an attacker or a stray
memory scribbler bug, so lets mark it as READONLY_AFTER_INIT.
2021-07-23 19:02:25 +02:00
Brian Gianforcaro
baec9e2d2d Kernel: Migrate sys$unveil to use the KString API
This avoids potential unhandled OOM that's possible with the old
copy_string_from_user API.
2021-07-23 19:02:25 +02:00
Brian Gianforcaro
2e7728bb05 Kernel: Use StringView literals for fs_type match in sys$mount(..) 2021-07-23 19:02:25 +02:00
Brian Gianforcaro
a3787b9db7 Kernel: Remove another ARCH ifdef using RegisterState::flags() 2021-07-23 19:02:25 +02:00
Andreas Kling
13a2e91fc5 Kernel: No need to use safe_memcpy() when handling an inode fault
We're copying the inode contents from a stack buffer into a page that
we just quick-mapped, so there's no reason for this memcpy() to fail.
2021-07-23 14:19:47 +02:00
Brian Gianforcaro
204d5ff8f8 Kernel: Reduce useful ROP gadgets by zeroing used function registers
GCC-11 added a new option `-fzero-call-used-regs` which causes the
compiler to zero function arguments before return of a function. The
goal being to reduce the possible attack surface by disarming ROP
gadgets that might be potentially useful to attackers, and reducing
the risk of information leaks via stale register data. You can find
the GCC commit below[0].

This is a mitigation I noticed on the Linux KSPP issue tracker[1] and
thought it would be useful mitigation for the SerenityOS Kernel.

The reduction in ROP gadgets is observable using the ropgadget utility:

    $ ROPgadget --nosys --nojop --binary Kernel | tail -n1
    Unique gadgets found: 42754

    $ ROPgadget --nosys --nojop --binary Kernel.RegZeroing | tail -n1
    Unique gadgets found: 41238

The size difference for the i686 Kernel binary is negligible:

    $ size Kernel Kernel.RegZerogin
        text    data     bss     dec      hex filename
    13253648 7729637 6302360 27285645 1a0588d Kernel
    13277504 7729637 6302360 27309501 1a0b5bd Kernel.RegZeroing

We don't have any great workloads to measure regressions in Kernel
performance, but Kees Cook mentioned he measured only around %1
performance regression with this enabled on his Linux kernel build.[2]

References:
[0] d10f3e900b
[1] https://github.com/KSPP/linux/issues/84
[2] https://lore.kernel.org/lkml/20210714220129.844345-1-keescook@chromium.org/
2021-07-23 14:18:04 +02:00
Andreas Kling
082ed6f417 Kernel: Simplify VMObject locking & page fault handlers
This patch greatly simplifies VMObject locking by doing two things:

1. Giving VMObject an IntrusiveList of all its mapping Region objects.
2. Removing VMObject::m_paging_lock in favor of VMObject::m_lock

Before (1), VMObject::for_each_region() was forced to acquire the
global MM lock (since it worked by walking MemoryManager's list of
all regions and checking for regions that pointed to itself.)

With each VMObject having its own list of Regions, VMObject's own
m_lock is all we need.

Before (2), page fault handlers used a separate mutex for preventing
overlapping work. This design required multiple temporary unlocks
and was generally extremely hard to reason about.

Instead, page fault handlers now use VMObject's own m_lock as well.
2021-07-23 03:24:44 +02:00
Andreas Kling
64babcaa83 Kernel: Remove unused MAP_SHARED_ZERO_PAGE_LAZILY code path 2021-07-23 03:24:44 +02:00
Andreas Kling
e44a41d0bf Kernel: Convert Region to east-const style 2021-07-22 23:34:33 +02:00
Gunnar Beutner
f2be1f9326 Kernel: Fix the variable declaration for some linker script symbols
Despite what the declaration would have us believe these are not "u8*".
If they were we wouldn't have to use the & operator to get the address
of them and then cast them to "u8*"/FlatPtr afterwards.
2021-07-22 22:27:11 +02:00
Andreas Kling
6115258a5c Kernel: Add /proc/kernel_base (superuser only)
This file contains the kernel base address as a decimal integer.
2021-07-22 14:20:05 +02:00
Andreas Kling
0642f8f2c6 Kernel: Make committed physical page allocation return NonnullRefPtr
Since we're taking from the committed set of pages, there should never
be a reason for this call to fail.

Also add a Badge to disallow taking committed pages from anywhere but
the Region class.
2021-07-22 14:20:05 +02:00
Andreas Kling
5217875f6a Kernel: Consolidate API for creating AnonymousVMObject with given pages
We don't need to have a dedicated API for creating a VMObject with a
single page, the multi-page API option works in all cases.

Also make the API take a Span<NonnullRefPtr<PhysicalPage>> instead of
a NonnullRefPtrVector<PhysicalPage>.
2021-07-22 09:17:02 +02:00
Andreas Kling
9e15708aa0 Kernel: Convert VMObject & subclasses to east-const style 2021-07-22 09:17:02 +02:00
Gunnar Beutner
eaad94751c Kernel: Fix incorrect format template 2021-07-22 08:57:01 +02:00
Gunnar Beutner
b4272d731f Kernel: Make sure crash dumps are properly aligned on x86_64 2021-07-22 08:57:01 +02:00
Gunnar Beutner
36e36507d5 Everywhere: Prefer using {:#x} over 0x{:x}
We have a dedicated format specifier which adds the "0x" prefix, so
let's use that instead of adding it manually.
2021-07-22 08:57:01 +02:00
Gunnar Beutner
31f30e732a Everywhere: Prefix hexadecimal numbers with 0x
Depending on the values it might be difficult to figure out whether a
value is decimal or hexadecimal. So let's make this more obvious. Also
this allows copying and pasting those numbers into GNOME calculator and
probably also other apps which auto-detect the base.
2021-07-22 08:57:01 +02:00
Andreas Kling
f9b7ea6de9 Revert "Kernel: Use IntrusiveList for keeping track of InodeWatchers"
This reverts commit 43d6a7e74e.

This breaks multi-inode watchers.
2021-07-21 21:24:26 +02:00
Andreas Kling
79745507a9 Kernel: Use IntrusiveList for keeping track of GenericInterruptHandlers 2021-07-21 20:21:29 +02:00
Andreas Kling
a9f76b8270 Kernel: Remove Inode's inheritance from Weakable
Nobody was using WeakPtr<Inode> anywhere, so there's no need for this
to inherit from Weakable.
2021-07-21 20:17:55 +02:00
Andreas Kling
43d6a7e74e Kernel: Use IntrusiveList for keeping track of InodeWatchers 2021-07-21 20:17:55 +02:00
Tom
5ae42736f8 Kernel: VirtIO framebuffer should clamp pending dirty rects if needed
If we change to a resolution smaller than what any pending dirty
rectangles contain, we need to clamp them to the new resolution.
2021-07-21 00:06:58 +02:00
Andreas Kling
f85b94e6d4 Kernel: Remove KBufferBuilder's can_expand restriction
KBufferBuilder is always allowed to expand if it wants to. This
restriction was added a long time ago when it was unsafe to allocate
VM while generating ProcFS contents.
2021-07-20 18:05:05 +02:00
Andreas Kling
fef835de7f Kernel: Remove KBufferBuilder API for reusing an existing buffer
This is not used anywhere anymore anyway.
2021-07-20 18:05:05 +02:00
Andreas Kling
a3063dfd33 Kernel: Simplify ProcFS generated buffer caching
Use a Mutex instead of a SpinLock to protect the per-FileDescription
generated data cache. This allows processes to go to sleep while
waiting their turn.

Also don't try to be clever by reusing existing cache buffers.
Just allocate KBuffers as needed (and make sure to surface failures.)
2021-07-20 18:05:05 +02:00
Andreas Kling
4d2473b7fa Kernel: Remove confused comment in KBufferBuilder::appendff()
KBufferBuilder exists for code that wants to build a KBuffer instead
of a String. KBuffer is backed by anonymous VM, while String is backed
by a kernel heap allocation.
2021-07-20 18:05:05 +02:00
Peter Elliott
3fa2816642 Kernel+LibC: Implement fcntl(2) advisory locks
Advisory locks don't actually prevent other processes from writing to
the file, but they do prevent other processes looking to acquire and
advisory lock on the file.

This implementation currently only adds non-blocking locks, which are
all I need for now.
2021-07-20 17:44:30 +04:30
Gunnar Beutner
4fdee56ba3 Prekernel: Make sure to reload CR3 after modifying the page tables 2021-07-20 15:12:19 +02:00
Gunnar Beutner
05fc75f994 Prekernel: Don't wrap around the PTE index improperly
The boot_pd0_pts variable contains more than 512 PTEs so we shouldn't
wrap the index here.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
d29981e4a1 Prekernel: Properly initialize variables 2021-07-20 15:12:19 +02:00
Gunnar Beutner
ac1455d3ba Kernel: Specify protection flags for ELF load headers
These are currently unused by the prekernel and ld used the same flags
by default - except for the .ksyms section which was marked as
read-write.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
2019cf3289 Kernel: Use the C preprocessor to avoid two copies of the linker script 2021-07-20 15:12:19 +02:00
Gunnar Beutner
56f952a5f2 Prekernel: Don't assume that PT_LOAD headers are ordered by address
These headers are ordered by virtual address - at least with GCC - but
that might not always be the case.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
5188185374 Kernel: Rename .boot_bss to .super_pages to better reflect what it is
This also removes the section attribute for kernel_base which had no
effect because the section wasn't included in the linker script.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
be795d5812 Prekernel: Use physical addresses for some of the BootInfo parameters
The kernel would just turn those virtual addresses into physical
addresses later on, so let's just use physical addresses right from the
start.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
dd42093b93 Kernel: Move boot info declarations to a header file
Instead of manually redeclaring those variables in various files this
now adds a header file for them.
2021-07-20 15:12:19 +02:00
Gunnar Beutner
b4600f2996 Kernel: Initialize serial debug after setting kernel command-line 2021-07-20 11:38:45 +01:00
Brian Gianforcaro
8f01a8b741 Kernel: Disable big process lock for sys$yield() 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
5c10fb4007 Kernel: Disable big process lock for sys$gettid()
This syscall reads a read only value from the current thread, and hence
has no need for the big process lock.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
638598b15d Kernel: Disable big process lock for sys$getpid() 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
bfd4635274 Kernel: Disable big process lock for sys$uname() 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
10ce896d4f Kernel: Disable big process lock in sys$gethostname() sys$sethostname() 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
9201a06027 Kernel: Annotate all syscalls with VERIFY_PROCESS_BIG_LOCK_ACQUIRED
Before we start disabling acquisition of the big process lock for
specific syscalls, make sure to document and assert that all the
lock is held during all syscalls.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
120b9bc05b Kernel: Conditionally acquire the big lock based on syscall metadata 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
bb1fa019de Kernel: Allow MutexLocker to be conditionally initialized
There are cases where we want to conditionally take a lock, but still
would like to use an RAII type to make sure we don't leak the lock.

This was previously impossible to do with `MutexLocker` due to it's
design. This commit tweaks the design to allow the object to be
initialized to an "empty" state without a lock associated, so it does
nothing, and then later a lock can be "attached" to the locker.

I realized that the get_lock() API's where also unused, and would no
longer make sense for empty locks, so they were removed.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
354e18a5a0 Kernel: Move validate_syscall_preconditions outside of the big lock
Now that we hold the space lock for the duration of the validation
it should be safe to move the validation outside the big lock.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
85e95105c6 Kernel: Mark read only RegisterState function parameters as const 2021-07-20 03:21:14 +02:00
Brian Gianforcaro
27e1120dff Kernel: Move syscall precondition validates to MM
Move these to MM to simplify the flow of the syscall handler.

While here, also make sure we hold the process space lock for
the duration of the validation to avoid potential issues where
another thread attempts to modify the process space during the
validation. This will allow us to move the validation out of the
big process lock scope in a future change.

Additionally utilize the new no_lock variants of functions to avoid
unnecessary recursive process space spinlock acquisitions.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
af543328ea Kernel: Instrument syscalls with their process big lock requirements
Currently all syscalls run under the Process:m_big_lock, which is an
obvious bottleneck. Long term we would like to remove the big lock and
replace it with the required fine grained locking.

To facilitate this goal we need a way of gradually decomposing the big
lock into the all of the required fine grained locks. This commit
introduces instrumentation to the syscall table, allowing the big lock
requirement to be toggled on/off per syscall.

Eventually when we are finished, no syscall will required the big lock,
and we'll be able to remove all of this instrumentation.
2021-07-20 03:21:14 +02:00
Brian Gianforcaro
308396bca1 Kernel: No lock validate_user_stack variant, switch to Space as argument
The entire process is not needed, just require the user to pass in the
Space. Also provide no_lock variant to use when you already have the
VM/Space lock acquired, to avoid unnecessary recursive spinlock
acquisitions.
2021-07-20 03:21:14 +02:00
Gunnar Beutner
69fd68b1c8 Prekernel: Make sure we're not overwriting the ELF header
This copies the ELF header because we might end up overwriting when
loading the ELF sections.
2021-07-20 01:50:55 +02:00
ls
222b97488a VirtualFileSystem: Check for '.' '..' and empty filenames
This commit adds a check, to prevent empty dot or dot-dot filenames when
renaming a file and returns EINVAL in that case.
2021-07-19 18:20:34 +02:00
Gunnar Beutner
3678f78bfb Prekernel: Make sure the last few bytes of the kernel image are mapped
Depending on the exact layout of the .ksyms section the kernel would
fail to boot because the kernel_load_end variable didn't account for the
section's size.
2021-07-19 17:47:54 +02:00
Gunnar Beutner
675de847f7 Kernel: Remove obsolete code 2021-07-19 11:29:09 +02:00
Gunnar Beutner
b6ddb4b70f Kernel: Simplify the linker script for the prekernel 2021-07-19 11:29:09 +02:00
Gunnar Beutner
a364f5c7b7 Kernel: Make sure super pages are in the first 16MiB of physical memory
This was broken by recent changes.
2021-07-19 11:29:09 +02:00
Brian Gianforcaro
121e7626d0 Kernel: Rename PerformanceEvent methods to be more ARCH independent 2021-07-19 08:46:55 +02:00
Brian Gianforcaro
1cffecbe8d Kernel: Push ARCH specific ifdef's down into RegisterState functions
The non CPU specific code of the kernel shouldn't need to deal with
architecture specific registers, and should instead deal with an
abstract view of the machine. This allows us to remove a variety of
architecture specific ifdefs and helps keep the code slightly more
portable.

We do this by exposing the abstract representation of instruction
pointer, stack pointer, base pointer, return register, etc on the
RegisterState struct.
2021-07-19 08:46:55 +02:00
Andreas Kling
e49af4bac9 Kernel/E1000: Consolidate RX and TX buffer regions
Allocate all the RX buffers in one big memory region (and same for TX.)
This removes 38 lines from every crash dump (and just seems like a
reasonable idea in general.)
2021-07-19 02:16:24 +02:00
Tom
668de76fa7 Kernel: Don't truncate physical address in ACPI table to 32 bits
We need to cast physical addresses to PhysicalPtr instead of FlatPtr,
which is currently always 64 bits. However, if one day we were to
support 32 bit non-pae mode then it would also truncate appropriately.
2021-07-18 22:09:05 +02:00
Tom
a635ff4e60 Everywhere: Make tracking cpu usage independent from system ticks
This switches tracking CPU usage to more accurately measure time in
user and kernel land using either the TSC or another time source.
This will also come in handy when implementing a tickless kernel mode.
2021-07-18 22:08:26 +02:00
Tom
7e77a2ec40 Everywhere: Improve CPU usage calculation
As threads come and go, we can't simply account for how many time
slices the threads at any given point may have been using. We need to
also account for threads that have since disappeared. This means we
also need to track how many time slices we have expired globally.

However, because this doesn't account for context switches outside of
the system timer tick values may still be under-reported. To solve this
we will need to track more accurate time information on each context
switch.

This also fixes top's cpu usage calculation which was still based on
the number of context switches.

Fixes #6473
2021-07-18 22:08:26 +02:00
Gunnar Beutner
5ff1416076 Kernel: Rename bootloader to prekernel
There are a few occurrences of the old name that slipped through.
2021-07-18 22:08:03 +02:00
Max Wipfli
a6c4e84ac8 Kernel: Add .profile extension to perfcore files
This adds a ".profile" extension to perfcore files written by the
Kernel. Also, the process name is now visible in the perfcore filename.

Furthermore, this patch adds error handling for the case where the
filename generated by the Kernel is already taken. In that case, a digit
will be added to the filename (before the extension).

This also adds some more error logging to dump_perfcore().
2021-07-18 19:01:57 +02:00
Gunnar Beutner
f56ee10a13 Kernel: Hand out 64-bit addresses to userspace 2021-07-18 17:31:13 +02:00
Liav A
5938d882d8 Kernel: Use a different kernel load address for x86_64
Co-authored-by: Gunnar Beutner <gbeutner@serenityos.org>
2021-07-18 17:31:13 +02:00
Gunnar Beutner
7e94b090fe Kernel: Introduce basic pre-kernel environment
This implements a simple bootloader that is capable of loading ELF64
kernel images. It does this by using QEMU/GRUB to load the kernel image
from disk and pass it to our bootloader as a Multiboot module.

The bootloader then parses the ELF image and sets it up appropriately.
The kernel's entry point is a C++ function with architecture-native
code.

Co-authored-by: Liav A <liavalb@gmail.com>
2021-07-18 17:31:13 +02:00
Gunnar Beutner
357ddd393e Kernel: Make allocate_randomized() work for 64-bit addresses
The odds of finding a suitable address in 1000 attempts were not in our
favor given the size of the 64-bit address space.
2021-07-18 17:31:13 +02:00
Gunnar Beutner
f0c4941beb Kernel: Fix 64-bit address truncation in MemoryManager::ensure_pte() 2021-07-18 17:31:13 +02:00
Gunnar Beutner
98f8ecd9d2 Kernel: Split debug symbols into a separate file
This speeds up the boot process considerably when specifying the kernel
image via -initrd.
2021-07-18 17:31:13 +02:00
Gunnar Beutner
b708b23b13 Kernel: Add support for kernel addresses other than 3-4GB 2021-07-18 17:31:13 +02:00
Gunnar Beutner
6c6b778e2e Kernel: Support 64-bit addresses for kernel thread stacks 2021-07-18 17:31:13 +02:00
Gunnar Beutner
c878054682 Kernel: Make VirtualAddress::page_base() work with 64-bit addresses 2021-07-18 17:31:13 +02:00
Gunnar Beutner
679ae6af81 Kernel: Remove some more hardcoded addresses 2021-07-18 17:31:13 +02:00
Gunnar Beutner
7c3bfde8fd Kernel: Make SCHEDULER_DEBUG work on x86_64 2021-07-18 17:31:13 +02:00
Gunnar Beutner
741ca5477c Kernel: Fix {read,write}_gs_ptr() for 64-bit addresses 2021-07-18 17:31:13 +02:00
Gunnar Beutner
294b918e9b Kernel: Ignore KUBSAN symbols in the kernel symbol map 2021-07-18 17:31:13 +02:00
Sahan Fernando
2845c6753e Kernel: Clean up VirtIOGPU class interfaces 2021-07-18 19:58:17 +04:30
Sahan Fernando
d9330c42fa Kernel: Replace VirtIOGPU reinterpret casts with AK::BinaryBufferWriter 2021-07-18 19:58:17 +04:30
Sahan Fernando
04226f0c0a Kernel: Improve debug prints for VirtIOGPU device config events 2021-07-18 19:58:17 +04:30
Sahan Fernando
4dddc56ad9 Kernel: Rename VirtIOGPU/VirtIOGPU.cpp to VirtIOGPU/GPU.cpp 2021-07-18 19:58:17 +04:30
Sahan Fernando
1c77f80676 Kernel: Put VirtIOGPU related types into a namespace 2021-07-18 19:58:17 +04:30
Andreas Kling
7791f7b9e6 Kernel/TmpFS: Remove some unnecessary includes 2021-07-18 14:29:27 +02:00
Andreas Kling
61e17ed590 Kernel/TmpFS: Use IntrusiveList and KString for OOM safety
This patch moves TmpFS to using OOM-safe data types for storing
directory children.
2021-07-18 14:29:27 +02:00
Tom
ae8472f9ca Kernel: Fix blocking relock of the big_lock while unlocking other Lock
When a Thread is being unblocked and we need to re-lock the process
big_lock and re-locking blocks again, then we may end up in
Thread::block again while still servicing the original lock's
Thread::block. So permit recursion as long as it's only the big_lock
that we block on again.

Fixes #8822
2021-07-18 13:08:51 +02:00
Andreas Kling
df5fa8aa39 Kernel/ProcFS: Remove redundant ProcFS& member from ProcFSInode 2021-07-18 02:24:26 +02:00
Andreas Kling
d298de5e09 Kernel/SysFS: Remove redundant SysFS& member from SysFSInode 2021-07-18 02:23:22 +02:00
Andreas Kling
d3cf4cd8f0 Kernel/DevFS: Remove redundant DevFS& member from DevFSInode
All Inode subclasses can get to their FileSystem via Inode::fs().
2021-07-18 02:21:34 +02:00
Andreas Kling
bd37840cf5 Kernel/Ext2FS: Surface Vector allocation failures in block allocation 2021-07-18 02:15:56 +02:00
Andreas Kling
1f18558ee2 Kernel: Make FileSystem::root_inode() return a plain Inode&
All file system classes are expected to keep their root Inode object
in memory, so this function can safely return an Inode&.
2021-07-18 01:53:04 +02:00
Andreas Kling
58c6d30f6a Kernel/Ext2FS: Cache the root inode in a member variable
We often get queried for the root inode, and it will always be cached
in memory anyway, so let's make Ext2FS::root_inode() fast by keeping
the root inode in a dedicated member variable.
2021-07-18 01:53:04 +02:00
Andreas Kling
9457d83986 Kernel: Rename Locker => MutexLocker 2021-07-18 01:53:04 +02:00
Andreas Kling
ab50a1480f Kernel/USB: Remove unneeded friend declaration in SysFSUSBBusDirectory 2021-07-18 01:53:04 +02:00
Andreas Kling
b975a74a1d Kernel/USB: Move USB bus information from /proc to /sys
This patch moves all the USB data from /proc/bus/usb to /sys/bus/usb.
2021-07-18 00:55:16 +02:00
Andreas Kling
6fae2a5f9e Kernel/ProcFS: Remove unused ProcFSExposedComponent::entries_count() 2021-07-17 23:50:01 +02:00
Andreas Kling
d686d2ef18 Kernel/SysFS: Remove unused SysFSComponent::entries_count() 2021-07-17 23:50:01 +02:00
Andreas Kling
2da0581fd2 Kernel: Replace "folder" => "directory" everywhere
Folders are a GUI concept. File systems have directories.
2021-07-17 23:50:00 +02:00
Andreas Kling
b8d6c3722d Kernel: Remove Inode::directory_entry_count()
This was only used in one place: VirtualFileSystem::rmdir(), and that
has now been converted to a simple directory traversal.
2021-07-17 22:36:04 +02:00
Andreas Kling
d1bbe8b652 Kernel: Count remaining children in VirtualFileSystem::rmdir() manually
To count the remaining children, we simply need to traverse the
directory and increment a counter. No need for a custom virtual that
all file systems have to implement. :^)
2021-07-17 22:34:43 +02:00
Andreas Kling
a3f58a5003 Kernel/DevFS: Use KString for DevFSDeviceInode::m_name 2021-07-17 22:17:07 +02:00
Andreas Kling
0d89cfcd9a Kernel/DevFS: Use KString for DevFSLinkInode::m_link 2021-07-17 22:11:41 +02:00
Andreas Kling
cd4d73e19e Kernel: Add an API for turning a UserOrKernelBuffer into a KString 2021-07-17 22:07:49 +02:00
Andreas Kling
dd37d0a327 Kernel/DevFS: Use KString for DevFSLinkInode::m_name 2021-07-17 21:40:32 +02:00
Andreas Kling
61c1937d02 Kernel/DevFS: Make DevFSInode::name() return StringView 2021-07-17 21:36:54 +02:00
Andreas Kling
6766efff9c Kernel: Make Inode::create_child() take the name as a StringView
No sense in forcing callers to construct a String. One more small step
towards not using String in the kernel.
2021-07-17 21:32:59 +02:00
Andreas Kling
9359f7801f Kernel/DevFS: Remove some unnecessary inode locking
Unless we're accessing mutex-guarded metadata, there's no need to
acquire the inode lock.

The file system ID or inode index of a constructed inode will never
change, for example.
2021-07-17 21:26:47 +02:00
Andreas Kling
3b805a57e6 Kernel: Rename Inode::m_lock => m_inode_lock
This makes file system code much easier to read since it was hard when
both the file system and inode locks were called "m_lock".
2021-07-17 21:17:39 +02:00
Andreas Kling
63e1423830 Kernel: Remove unused Inode::is_shared_vmobject() 2021-07-17 21:11:12 +02:00
Andreas Kling
cee9528168 Kernel: Rename Lock to Mutex
Let's be explicit about what kind of lock this is meant to be.
2021-07-17 21:10:32 +02:00
Andreas Kling
a803c4026c Kernel: Make FileSystem::class_name() return a StringView 2021-07-17 20:59:48 +02:00
Brian Gianforcaro
c0987453e6 Kernel: Remove double RedBlackTree lookup in VM/Space region removal
We should never request a regions removal that we don't currently
own. We currently assert this everywhere else by all callers.

Instead lets just push the assert down into the RedBlackTree removal
and assume that we will always successfully remove the region.
2021-07-17 16:22:59 +02:00
Brian Gianforcaro
d879709ec7 Kernel: Convert the PhysicalPage bool parameter to an enum 2021-07-17 18:38:28 +04:30
Brian Gianforcaro
eb282ad410 Kernel: Declare VM/RangeAllocator trivial destructor as default
This is a clang tidy recommendation.
2021-07-17 13:02:09 +02:00
Brian Gianforcaro
24bd664980 Kernel: Remove stale include from VM/RangeAllocator.cpp
This was left over after the latest big refactor of the VM subsystem.
2021-07-17 13:02:09 +02:00
Brian Gianforcaro
dbc77148c9 Kernel: Convert RangeAllocator VERIFY to proper error handling
If a user allocates above 0x0 and below the allowable usermode
virtual address space, we need to return error instead of asserting.

Fixes: #8484
2021-07-17 13:00:21 +02:00
Peter Bindels
a5a62f99c5 LibC: Use 64-bit stack smash value for 64-bit mode
Otherwise it'll use the first 32 bits that happen to come after,
leading to very weird bugs. Fixes #8601
2021-07-17 01:45:20 +02:00
Hediadyoin1
432a34a496 Kernel: Make PAGE_MASK architecture independent 2021-07-17 01:12:15 +02:00
Tom
3cca9e6704 Kernel: Initialize TimeManagement before using KernelRNG
We should initialize the timers before KernelRNG as the RNG may want
to utilize system time as an entropy source.

Fixes #8710
2021-07-17 00:04:57 +02:00
Tom
7bdd66ed89 Kernel: Remove TimeManagement::initialized that wasn't implemented 2021-07-17 00:04:57 +02:00
Daniel Bertalan
3099a6bf2a Kernel+AK: Generate compile-time error for non-sized delete
This is a much more ergonomic option than getting a
`VERIFY_NOT_REACHED()` failure at run-time. I encountered this issue
with Clang, where sized deallocation is not the default due to ABI
breakage concerns.

Note that we can't simply just not declare these functions, because the
C++ standard states:
> If this function with size parameter is defined, the program shall
> also define the version without the size parameter.
2021-07-16 20:51:13 +02:00
Daniel Bertalan
dd4ed4d22d Kernel: Implement aligned operator new and use it
The compiler will use these to allocate objects that have alignment
requirements greater than that of our normal `operator new` (4/8 byte
aligned).

This means we can now use smart pointers for over-aligned types.

Fixes a FIXME.
2021-07-16 20:51:13 +02:00
Daniel Bertalan
c176680443 Kernel: Tell the compiler about operator new's alignment
By default, the compiler will assume that `operator new` returns
pointers that are aligned correctly for every built-in type. This is not
the case in the kernel on x64, since the assumed alignment is 16
(because of long double), but the kmalloc blocks are only
`alignas(void*)`.
2021-07-16 20:51:13 +02:00
Tom
704e1c2e3d Kernel: Rename functions to be less confusing
Thread::yield_and_release_relock_big_lock releases the big lock, yields
and then relocks the big lock.

Thread::yield_assuming_not_holding_big_lock yields assuming the big
lock is not being held.
2021-07-16 20:30:04 +02:00
Tom
0536a4ff41 Kernel: Release big lock when blocking on another lock
When blocking on a Lock other than the big lock and we're holding the
big lock, we need to release the big lock first. This fixes some
deadlocks where a thread blocks while holding the big lock, preventing
other threads from getting the big lock in order to unblock the waiting
thread.
2021-07-16 20:30:04 +02:00
Tom
710cf14c55 Kernel: Fix some Lock problems and VERIFY statements
When a Lock blocks (e.g. due to a mode mismatch or because someone
else holds it) the lock mode will be updated to what was requested.

There were also some cases where restoring locks may have not worked
as intended as it may have been held already by the same thread.

Fixes #8787
2021-07-16 20:30:04 +02:00
Gunnar Beutner
22a588d394 Kernel: Make the page table for the kernel image larger
Building the x86_64 kernel with ENABLE_EXTRA_KERNEL_DEBUG_SYMBOLS
results in an image that is larger than 0x2000000 bytes.
2021-07-16 18:50:59 +02:00
Gunnar Beutner
a17c25e45e Kernel: Make resizing the page tables for the kernel image easier
By using the KERNEL_PD_OFFSET constant we can avoid some of the
hard-coded values in the boot code.
2021-07-16 18:50:59 +02:00
Gunnar Beutner
cbdb488578 Kernel: Move end_of_kernel_image after the .ksyms section
Without this we won't be able to detect whether .ksyms overlaps the end
of the page table we set up for the kernel image.
2021-07-16 18:50:59 +02:00
Gunnar Beutner
acf8f2a2a3 Kernel: Support specifying a 64-bit KERNEL_BASE address
The kernel doesn't currently boot when using an address other than
0xc0000000 because the page tables aren't set up properly for that
but this at least lets us build the kernel.
2021-07-16 18:50:59 +02:00
Gunnar Beutner
9b431cbe42 Kernel: Avoid unnecessary jump in the boot code
The 32-bit boot code jumps to 0xc0000000 + entry address once page
tables are set up. This is unnecessary for 64-bit mode because we'll
do another far jump just moments later.
2021-07-16 18:50:59 +02:00
Gunnar Beutner
eb1935b62e Kernel: Avoid hard-coding kernel virtual base 2021-07-16 18:50:59 +02:00
Liav A
9e36158ece Kernel: Remove stale forward declaration of BochsFramebufferDevice 2021-07-16 18:50:16 +02:00
Andreas Kling
3108aa0a6b Kernel: Fix bogus check in Thread::WaitBlockCondition::finalize()
I botched this in 859e5741ff, the check
was supposed to be with Process::is_kernel_process().

This fixes an issue with zombie processes hanging around forever.
Thanks tomuta for spotting it! :^)
2021-07-16 15:18:47 +02:00
Timothy
9715311837 AK+Kernel: Implement and use EnumBits has_any_flag()
This duplicates the old functionality of has_flag and will return true
when any flags present in the mask are also in the value.
2021-07-16 11:49:50 +02:00
Luke
2df4d977e2 Kernel: Return ENOMEM on allocation failures in FramebufferDevice::mmap 2021-07-16 11:15:30 +02:00
Andreas Kling
41c0009f6d Kernel/Ext2FS: Don't hog inode lock in traverse_as_directory()
Reimplement directory traversal in terms of read_bytes() instead of
doing direct block access. This lets us avoid taking the inode lock
while iterating over the directory contents.
2021-07-16 02:40:53 +02:00
Andreas Kling
abbd237ec1 Kernel/Ext2FS: Don't hog FS lock when calling base class flush_writes()
Once we've finalized all the file system metadata in flush_writes(),
we no longer need to hold the file system lock during the call to
BlockBasedFileSystem::flush_writes().
2021-07-16 02:40:53 +02:00
Andreas Kling
98c230b370 Kernel/Ext2FS: Uncache unknown inode indices when flushing writes
Ext2FS::get_inode() will remember unknown inode indices that it has
been asked about and put them into the inode cache as null inodes.

flush_writes() was not null-checking these while iterating, which
was a bug I finally managed to hit.

Flushing also seemed like a good time to drop unknown inodes from
the cache, since there's no good reason to hold to them indefinitely.
2021-07-16 02:40:53 +02:00
Andreas Kling
a7d193951f Kernel: Don't hog file system lock when doing BlockBasedFileSystem I/O
The file system lock is meant to protect the file system metadata
(super blocks, bitmaps, etc.) Not protect processes from reading
independent parts of the disk at once.

This patch introduces a new lock to protect the *block cache* instead,
which is the real thing that needs synchronization.
2021-07-16 02:40:53 +02:00
Andreas Kling
abf0249f35 Kernel: Don't explicitly seek before I/O in BlockBasedFileSystem
Use the new FileDescription APIs to avoid doing seek+read or seek+write
as two separate operations.
2021-07-16 02:40:53 +02:00
Andreas Kling
d1395f2eb9 Kernel: Add FileDescription read/write API that bypasses current offset
Forcing users of a FileDescription to seek before they can read/write
makes it inherently racy. This patch adds variants of read/write that
simply ignore the "current offset" of the description in favor of a
caller-supplied offset.
2021-07-16 02:40:53 +02:00
Andreas Kling
ace8b9a0ee Kernel/Ext2FS: Don't hog both locks in Ext2FSInode::lookup()
This function was acquiring both the inode and file system locks (in
that order) which could lead to deadlocks.
2021-07-16 02:40:53 +02:00
Tom
82e9fe8d67 Kernel: Optionally dump scheduler state with stack traces
This will dump stack traces of all threads when pressing
Ctrl+Shift+Alt+F12
2021-07-15 23:46:37 +02:00
Gunnar Beutner
e4f05a9046 Kernel: Make new kernel build process work on macOS
Use objcopy from the toolchain so that the changes introduced in
7236584 will succeed on macOS.

Fixes #8768.
2021-07-15 11:04:30 +02:00
Andreas Kling
15ad4a8fd6 Kernel: Convert RangeAllocator to using a RedBlackTree internally
This data structure is a much better fit for what is essentially a
sorted list of non-overlapping ranges.

Not using Vector means we no longer have to worry about Vector buffers
getting huge. Only nice & small allocations from now on.
2021-07-15 02:03:57 +02:00
Andreas Kling
b0d9b88c49 Kernel: Hoist VERIFY from a loop in RangeAllocator::allocate_specific() 2021-07-15 01:48:09 +02:00
Andreas Kling
7ff14fecba Kernel: Remove unnecessary locking in RangeAllocator::contains()
The total range managed by a RangeAllocator doesn't change, so there's
no need to take a spinlock while comparing against it.
2021-07-15 01:48:09 +02:00
Andreas Kling
d4c73daacb Kernel: Convert RangeAllocator to east-const style 2021-07-15 01:48:09 +02:00
Idan Horowitz
be475cd6a8 Kernel: Handle OOM when adding memory regions to Spaces :^) 2021-07-15 00:49:41 +02:00
Gunnar Beutner
7236584132 Kernel: Make kernel symbols available much earlier in the boot process
This adds a new section .ksyms at the end of the linker map, reserves
5MiB for it (which are after end_of_kernel_image so they get re-used
once MemoryManager is initialized) and then embeds the symbol map into
the kernel binary with objcopy. This also shrinks the .ksyms section to
the real size of the symbol file (around 900KiB at the moment).

By doing this we can make the symbol map available much earlier in the
boot process, i.e. even before VFS is available.
2021-07-14 23:04:34 +02:00
Andreas Kling
859e5741ff Kernel: Fix Process use-after-free in Thread finalization
We leak a ref() onto every user process when constructing them,
either via Process::create_user_process(), or via Process::sys$fork().

This ref() is balanced by a corresponding unref() in
Thread::WaitBlockCondition::finalize().

Since kernel processes don't have a leaked ref() on them, this led to
an extra Process::unref() on kernel processes during finalization.
This happened during every boot, with the `init_stage2` process.

Found by turning off kfree() scrubbing. :^)
2021-07-14 22:36:29 +02:00
Jean-Baptiste Boric
528574d958 Kernel: Detect and display CPUID Hyper-V data 2021-07-14 13:52:34 +02:00
Jean-Baptiste Boric
b22357b17b Kernel: Detect and display CPUID hypervisor signature 2021-07-14 13:52:34 +02:00
Jean-Baptiste Boric
4cc346fb19 Kernel: Add support for hypervisor CPUID feature 2021-07-14 13:52:34 +02:00
Liav A
bee75c1f24 Kernel/ProcFS: Allow a process directory to have a null Process pointer
In case we are about to delete the PID directory, we clear the Process
pointer. If someone still holds a reference to the PID directory (by
opening it), we still need to delete the process, but we can't delete
the directory, so we will keep it alive, but any operation on it will
fail by propogating the error to userspace about that the Process was
deleted and therefore there's no meaning to trying to do operations on
the directory.

Fixes #8576.
2021-07-14 13:40:01 +02:00
Andreas Kling
dc26c02379 Kernel: Convert MemoryManager to east-const style 2021-07-14 13:31:21 +02:00
Daniel Bertalan
ef40de9c6c Kernel: Don't mix AT&T and Intel ASM syntax in boot.S
The rest of the file is in AT&T syntax, so for the time being, I'll
switch these instructions to AT&T too to make Clang shut up.
2021-07-14 13:12:25 +02:00
Daniel Bertalan
b847541ee8 Kernel: Allow passing null pointer to delete
The C++ standard says that it's legal to call the `delete` operator with
a null pointer argument, in which case it should be a no-op. I
encountered this issue when running a kernel that's compiled with Clang.
I assume this fact was used for some kind of optimization.
2021-07-14 13:12:25 +02:00
x-yl
42c5df7256 Kernel: Fix inverted check in VirtIOConsolePort
We should really only try to open if we're closed. Oops :P
2021-07-14 12:33:07 +02:00
Tom
d7e5521a04 Kernel: Ignore subsequent calls to Process::die
It's possible that another thread might try to exit the process just
about the same time another thread does the same, or a crash happens.
Also, we may not be able to kill all other threads instantly as they
may be blocked in the kernel (though in this case they would get killed
before ever returning back to user mode. So keep track of whether
Process::die was already called and ignore it on subsequent calls.

Fixes #8485
2021-07-14 12:30:41 +02:00
Hendiadyoin1
d761c5024b AK: Generalize ByteReader
Also use it instead of CPU.h's possibly_unaligned_data interface
2021-07-14 11:26:34 +04:30
Andreas Kling
c42807e3dc Kernel: Remove debug spam when PhysicalRegion::take_free_page() fails
We can have multiple PhysicalRegions (often the case when there is a
huge amount of RAM) so we really shouldn't print a debug message any
time someone tries to allocate from one. They will move on to another
region anyway.
2021-07-14 01:37:31 +02:00
Andreas Kling
5c24d18923 Kernel: Fix logic error in PhysicalRegion::contains()
This was incorrectly returning true for the address one byte past the
end of the region.
2021-07-14 01:37:19 +02:00
Andreas Kling
6cc1247395 Kernel: Cut allocation size for physical buddy bitmaps in half
We were allocating twice as much memory as we needed for these bitmaps
due to a silly typo. Found by tomuta trying to boot with 24 GiB of RAM.
2021-07-13 23:47:49 +02:00
Andreas Kling
d8ff46594a Kernel: Re-add accidentally removed friendship
PageDirectory and MemoryManager need to remain friends, for now..
2021-07-13 23:21:22 +02:00
Andreas Kling
de4ba1f39b Kernel: Remove some friendships and make some classes non-copy/moveable 2021-07-13 23:19:00 +02:00
Andreas Kling
424afdd72b Kernel: Remove some unnecessary includes in VM/Physical* 2021-07-13 23:11:06 +02:00
Andreas Kling
0a21d421d9 Kernel: Print a summary of physical zones during boot
Let's not print out every single zone, since that gets very noisy on
machines with a lot of RAM. :^)
2021-07-13 23:08:45 +02:00
Andreas Kling
bf5e4326ac Kernel: Fix bogus address calculation in initialize_physical_pages()
We were incorrectly using sizeof(PhysicalPageEntry) for some address
calculations instead of sizeof(PageTableEntry).

It still worked correctly because they happen to be the same size.
2021-07-13 23:08:45 +02:00
Andreas Kling
e323942623 Kernel: Only loop through usable zones when allocating >1 physical page
We still have to loop here, since a zone can be "usable" while not
being able to satisfy a multi-page allocation request.
2021-07-13 23:08:45 +02:00
Andreas Kling
379bcd26e4 Kernel: Avoid O(n) loop over zones when allocating from PhysicalRegion
We now keep all the PhysicalZones on one of two intrusive lists within
the PhysicalRegion.

The "usable" list contains all zones that can be allocated from,
and the "full" list contains all zones with no free pages.
2021-07-13 23:08:45 +02:00
Andreas Kling
9ae067aa7f Kernel: Make PhysicalRegion eternally allocated 2021-07-13 22:40:25 +02:00
Andreas Kling
959ceb4424 Kernel: Remove PhysicalRegion::finalize_capacity()
There's no reason to delay calculating the capacity (total page count)
of each PhysicalRegion. Just do it in the constructor.
2021-07-13 22:40:25 +02:00
Andreas Kling
5171249540 Kernel: Simplify the way PhysicalRegions are constructed
Instead of creating a PhysicalRegion and then expanding it over and
over as we traverse the memory map on boot, we now compute the final
size of the contiguous physical range up front, and *then* create a
PhysicalRegion object.
2021-07-13 22:40:25 +02:00
Andreas Kling
479df315d2 Kernel: Make PhysicalZone an eternally allocated object
Until we start supporting hot-pluggable RAM, these will not be freed
or reallocated during the kernel's lifetime. :^)
2021-07-13 22:40:25 +02:00
Andreas Kling
6ea5db20ff Kernel: Remove unused used/free pages API's from PhysicalRegion 2021-07-13 22:40:25 +02:00
Andreas Kling
be90e51355 Kernel: Remove API for requesting physical allocation alignment
Nobody was using this API to request anythign about `PAGE_SIZE`
alignment, so let's get rid of it for now. We can reimplement it if
we end up needing it.

Also note that it wasn't actually used anywhere.
2021-07-13 22:40:25 +02:00
Andreas Kling
ba87571366 Kernel: Implement zone-based buddy allocator for physical memory
The previous allocator was very naive and kept the state of all pages
in one big bitmap. When allocating, we had to scan through the bitmap
until we found an unset bit.

This patch introduces a new binary buddy allocator that manages the
physical memory pages.

Each PhysicalRegion is divided into zones (PhysicalZone) of 16MB each.
Any extra pages at the end of physical RAM that don't fit into a 16MB
zone are turned into 15 or fewer 1MB zones.

Each zone starts out with one full-sized block, which is then
recursively subdivided into halves upon allocation, until a block of
the request size can be returned.

There are more opportunities for improvement here: the way zone objects
are allocated and stored is non-optimal. Same goes for the allocation
of buddy block state bitmaps.
2021-07-13 22:40:25 +02:00
Tom
b919789db2 Kernel: Kill user mode threads that are marked to die
Threads that don't make syscalls still need to be killed, and we can
do that at any time we want so long the thread is in user mode and
not somehow blocked (e.g. page fault).
2021-07-13 20:23:10 +02:00
Tom
fa8fe40266 Revert "Kernel: Make sure threads which don't do any syscalls are t..."
This reverts commit 3c3a1726df.

We cannot blindly kill threads just because they're not executing in a
system call. Being blocked (including in a page fault) needs proper
unblocking and potentially kernel stack cleanup before we can mark a
thread as Dying.

Fixes #8691
2021-07-13 20:23:10 +02:00
Liav A
2a1bf53435 Kernel/Graphics: Move Bochs graphics related code into a separate folder 2021-07-12 22:53:08 +02:00
Liav A
3fee1cbe66 Kernel/Graphics: Remove unnecessary Bochs.h file
Nobody excepts BochsGraphicsAdapter code uses these definitions, so
let's put them back into the BochsGraphicsAdapter.cpp file.
2021-07-12 22:53:08 +02:00
Tom
026ffa343d Kernel: Allow Lock to block from BlockCondition
This enables the Lock class to block a thread even while the thread is
working on a BlockCondition. A thread can still only be either blocked
by a Lock or a BlockCondition.

This also establishes a linked list of threads that are blocked by a
Lock and unblocking directly unlocks threads and wakes them directly.
2021-07-12 11:27:18 +02:00
Tom
d9fb93c5ce Kernel: Fix deadlock cancelling timer
It's possible that a timer may have been queued to be executed by
the timer irq handler, but if we're in a critical section on the
same processor and are trying to cancel that timer, we would spin
forever waiting for it to be executed.
2021-07-12 11:27:18 +02:00
Tom
6938be00f1 Kernel: Initialize threading and process management earlier
This re-arranges the order of how things are initialized so that we
try to initialize process and thread management earlier. This is
neccessary because a lot of the code uses the Lock class, which really
needs to have a running scheduler in place so that we can properly
preempt.

This also enables us to potentially initialize some things in parallel.
2021-07-12 11:27:18 +02:00
Andreas Kling
c2792212f4 Kernel: Remove "supervisor" bit from PhysicalPage
Instead of each PhysicalPage knowing whether it comes from the
supervisor pages or from the user pages, we can just check in both
sets when freeing a page.

It's just a handful of pointer range checks, nothing expensive.
2021-07-12 11:09:42 +02:00
Tom
9318d9f284 Kernel: Fix allocating VMObject from page array
The VMObject constructor takes the size in bytes.

Fixes #8670
2021-07-12 10:45:53 +02:00
Brian Gianforcaro
84b4b9447d Kernel: Move new process registration out of Space spinlock scope
There appears to be no reason why the process registration needs
to happen under the space spin lock. As the first thread is not started
yet it should be completely uncontested, but it's still bad practice.
2021-07-12 10:20:21 +02:00
Tom
60a559af7e Kernel: Avoid unnecessary context switch when no other thread is ready
If no other thread is ready to be run we don't need to switch to the
idle thread and wait for the next timer interrupt. We can just give
the thread another timeslice and keep it running.
2021-07-12 10:19:31 +02:00
Andreas Kling
2dfdaafb23 Kernel: Use "new (nothrow)" for SharedInodeVMObject 2021-07-11 21:56:26 +02:00