Commit graph

431 commits

Author SHA1 Message Date
Liav A fb7b4caa57 Kernel/Storage: Implement basic AHCI hotplug support
This is really a basic support for AHCI hotplug events, so we know how
to add a node representing the device in /sys/dev/block and removing it
according to the event type (insertion/removal).

This change doesn't take into account what happens if the device was
mounted or a read/write operation is being handled.

For this to work correctly, StorageManagement now uses the Singleton
container, as it might be accessed simultaneously from many CPUs
for hotplug events. DiskPartition holds a WeakPtr instead of a RefPtr,
to allow removal of a StorageDevice object from the heap.
StorageDevices are now stored and being referenced to via an
IntrusiveList to make it easier to remove them on hotplug event.

In future changes, all of the stated above might change, but for now,
this commit represents the least amount of changes to make everything
to work correctly.
2021-09-08 00:42:20 +02:00
Liav A 6a9c717a30 Kernel: Expose device presence in /sys/dev/char and /sys/dev/block
These files are not marked as block devices or character devices so they
are not meant to be used as device nodes. The filenames are formatted to
the pattern "major:minor", but a Userland program need to call the parse
these format and inspect the the major and minor numbers and create the
real device nodes in /dev.

Later on, it might be a good idea to ensure we don't create new
SysFSComponents on the heap for each Device, but rather generate
them only when required (and preferably to not create a SysFSComponent
at all if possible).
2021-09-08 00:42:20 +02:00
Andreas Kling 55b0b06897 Kernel: Store process names as KString 2021-09-07 13:53:14 +02:00
Andreas Kling 71187d865e Kernel: Tidy up VirtualFileSystem::mount_root() a little bit
- Return KResult instead of bool
- Use TRY()
2021-09-05 14:46:44 +02:00
Andreas Kling 3b995c6d01 Kernel: Tidy up Process::try_create_user_process()
This function is currently only ever used to create the init process
(SystemServer). It had a few idiosyncratic things about it that this
patch cleans up:

- Errors were returned in an int& out-param.
- It had a path for non-0 process PIDs which was never taken.
2021-09-04 23:11:04 +02:00
Liav A 01ae614727 Kernel/VirtIO: Remove redundant VirtIO word from filenames
Now that all related VirtIO classes are in the VirtIO namespace, let's
just remove the redundant VirtIO word from filenames.
2021-08-31 16:51:13 +02:00
Andreas Kling ae197deb6b Kernel: Strongly typed user & group ID's
Prior to this change, both uid_t and gid_t were typedef'ed to `u32`.
This made it easy to use them interchangeably. Let's not allow that.

This patch adds UserID and GroupID using the AK::DistinctNumeric
mechanism we've already been employing for pid_t/ProcessID.
2021-08-29 01:09:19 +02:00
Idan Horowitz cf271183b4 Kernel: Make Process::current() return a Process& instead of Process*
This has several benefits:
1) We no longer just blindly derefence a null pointer in various places
2) We will get nicer runtime error messages if the current process does
turn out to be null in the call location
3) GCC no longer complains about possible nullptr dereferences when
compiling without KUBSAN
2021-08-19 23:49:53 +02:00
Andreas Kling 37304203dd Kernel: Lock thread list while in Thread::unref()
This patch does three things:

- Convert the global thread list from a HashMap to an IntrusiveList
- Combine the thread list and its lock into a SpinLockProtectedValue
- Customize Thread::unref() so it locks the list while unreffing

This closes the same race window for Thread as @sin-ack's recent changes
did for Process.

Note that the HashMap->IntrusiveList conversion means that we lose O(1)
lookups, but the majority of clients of this list are doing traversal,
not lookup. Once we have an intrusive hashing solution, we should port
this to use that, but for now, this gets rid of heap allocations during
a sensitive time.
2021-08-15 12:44:35 +02:00
Andreas Kling 1b739a72c2 Kernel+Userland: Remove chroot functionality
We are not using this for anything and it's just been sitting there
gathering dust for well over a year, so let's stop carrying all this
complexity around for no good reason.
2021-08-15 12:44:35 +02:00
Liav A 18eb262157 Kernel: Move VirtIO code into the Bus source folder
The VirtIO code handles functionality related to the VirtIO bus, so it
really should be in the Bus folder.
2021-08-13 08:06:47 +02:00
Luke 7a86a8df90 Kernel/USB: Create controller base class and introduce USBManagement
This removes Pipes dependency on the UHCIController by introducing a
controller base class. This will be used to implement other controllers
such as OHCI.

Additionally, there can be multiple instances of a UHCI controller.
For example, multiple UHCI instances can be required for systems with
EHCI controllers. EHCI relies on using multiple of either UHCI or OHCI
controllers to drive USB 1.x devices.

This means UHCIController can no longer be a singleton. Multiple
instances of it can now be created and passed to the device and then to
the pipe.

To handle finding and creating these instances, USBManagement has been
introduced. It has the same pattern as the other management classes
such as NetworkManagement.
2021-08-09 21:05:25 +02:00
Daniel Bertalan 347c3361ee Kernel: Fix deprecated array comparison
The Clang error message reads like this (`-Wdeprecated-array-compare`):

> error: comparison between two arrays is deprecated; to compare
> array addresses, use unary '+' to decay operands to pointers.
2021-08-08 10:55:36 +02:00
Brian Gianforcaro d496eb48e3 Kernel: Fix boot profiling after big process lock separation regression
When I laid down the foundation for the start of the big process lock
separation, I added asserts to all system call implementations to
validate we hold the big process lock in the locations we think we
should be.

Adding that assert to sys$profiling_enable broke boot time profiling as
we were never holding the lock on boot. Even though it's not technically
required, lets make sure to hold the lock while enabling to appease the
assert.
2021-08-07 12:38:59 +02:00
Andreas Kling 93d98d4976 Kernel: Move Kernel/Memory/ code into Kernel::Memory namespace 2021-08-06 14:05:58 +02:00
Andreas Kling a1d7ebf85a Kernel: Rename Kernel/VM/ to Kernel/Memory/
This directory isn't just about virtual memory, it's about all kinds
of memory management.
2021-08-06 14:05:58 +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
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
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
Gunnar Beutner 8642c831cf Kernel: Mark a few more things as READONLY_AFTER_INIT 2021-07-23 20:23:09 +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
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
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
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
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
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
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
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 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
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 fa9111ac46 Kernel: Rename ProcFSComponentsRegistrar => ProcFSComponentRegistry
This matches the formatting used in SysFS.
2021-07-11 01:40:26 +02:00
Andreas Kling 358a7cc448 Kernel: Rename SysFS related classes in BIOS code
Give them names that sound related to SysFS.
2021-07-11 01:38:01 +02:00
Andreas Kling b1c68dd235 Kernel: Rename SysFS related classes in ACPI code
Give them names that sound related to SysFS.
2021-07-11 01:35:17 +02:00
Andreas Kling 805319ed30 Kernel: Replace "Folder" => "Directory" everywhere
Folders are a GUI concept, file systems have directories. :^)
2021-07-11 01:33:40 +02:00
Andreas Kling 27244eb0ee Kernel: Rename SystemRegistrar => SysFSComponentRegistry 2021-07-11 01:05:26 +02:00
Andreas Kling 0d39bd04d3 Kernel: Rename VFS => VirtualFileSystem 2021-07-11 00:25:24 +02:00
Liav A cc98871383 Kernel: Print if image has become too large again
Instead of just disabling interrupts and halting when entering the C++
section, just halt with a printed message indicating the error.
2021-07-07 22:18:36 +02:00
Liav A 5073bf8e75 Kernel/USB: Move the USB components as a subfolder to the Bus directory 2021-07-02 13:16:12 +02:00
Liav A 6568bb47cb Kernel/PCI: Move the PCI components as a subfolder to the Bus directory 2021-07-02 13:16:12 +02:00
Hendiadyoin1 90cd11fa8c Kernel: Support starting up secondary processors on x86_64 2021-07-01 10:50:00 +02:00
Liav A 12b6e69150 Kernel: Introduce the new ProcFS design
The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.

The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.

The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
2021-06-29 20:53:59 +02:00
Liav A 92c0dab5ab Kernel: Introduce the new SysFS
The intention is to add dynamic mechanism for notifying the userspace
about hotplug events. Currently, the DMI (SMBIOS) blobs and ACPI tables
are exposed in the new filesystem.
2021-06-29 20:53:59 +02:00
Sahan Fernando b9ad6058aa Kernel: Add VirtIOGPU graphics device 2021-06-25 19:26:30 +02:00
Daniel Bertalan 221bd464a0 Kernel: Fix attribute ordering
Clang requires that attributes declared using the bracketed
`[[attr_name]]` syntax come before those with
`__attribute__((attr-name))`.

This fixes a Clang build error.
2021-06-24 17:35:49 +04:30
Hendiadyoin1 62f9377656 Kernel: Move special sections into Sections.h
This also removes a lot of CPU.h includes infavor for Sections.h
2021-06-24 00:38:23 +02:00