Commit Graph

5997 Commits

Author SHA1 Message Date
Andreas Kling
645e29a88b Kernel: Don't turn I/O errors during sys$execve() into ENOEXEC
Instead, just propagate whatever the real error was.
2021-09-06 13:06:05 +02:00
Andreas Kling
84addef10f Kernel: Improve arguments retrieval error propagation in sys$execve()
Instead of turning any arguments related error into an EFAULT, we now
propagate the innermost error during arguments retrieval.
2021-09-06 13:06:05 +02:00
Andreas Kling
6e3381ac32 Kernel: Use KResultOr and TRY() for {Shared,Private}InodeVMObject 2021-09-06 13:06:05 +02:00
Andreas Kling
e3a716ceff Kernel: Make Memory::Region::map() return KResult
..and use TRY() at the call sites to propagate errors. :^)
2021-09-06 13:06:05 +02:00
Andreas Kling
7981422500 Kernel: Make Threads always have a name
We previously allowed Thread to exist in a state where its m_name was
null, and had to work around that in various places.

This patch removes that possibility and forces those who would create a
thread (or change the name of one) to provide a NonnullOwnPtr<KString>
with the name.
2021-09-06 13:06:05 +02:00
Andreas Kling
cda2b9e71c Kernel: Improvements to Custody absolute path serialization
- Renamed try_create_absolute_path() => try_serialize_absolute_path()
- Use KResultOr and TRY() to propagate errors
- Don't call this when it's only for debug logging
2021-09-06 13:06:05 +02:00
Andreas Kling
f173f73f10 Kernel/USB: Use TRY() and adopt_nonnull_own_or_enomem() some more 2021-09-06 13:06:05 +02:00
Andreas Kling
e3b063581e Kernel: Use TRY() in sys$alarm() 2021-09-06 13:06:05 +02:00
Andreas Kling
b083b165a7 Kernel/Ext2FS: Wrap calls to traverse_as_directory() in TRY()
Nothing says we can't TRY() a multi-line function call. :^)
2021-09-06 13:06:05 +02:00
Andreas Kling
18dbafec85 Kernel: Use TRY() more in Plan9FS 2021-09-06 13:06:05 +02:00
Andreas Kling
d34f2b643e Kernel: Tidy up Plan9FS construction a bit 2021-09-06 13:06:05 +02:00
Andreas Kling
36725228fa Kernel: Tidy up Ext2FS construction a bit 2021-09-06 13:06:05 +02:00
Andreas Kling
47bfbe343b Kernel: Tidy up SysFS construction
- Use KResultOr and TRY() to propagate errors
- Check for OOM errors
- Move allocation out of constructors

There's still a lot more to do here, as SysFS is still quite brittle
in the face of memory pressure.
2021-09-06 13:06:05 +02:00
Andreas Kling
788b91a65c Kernel: Tidy up DevFS construction and handle OOM errorso
- Use KResultOr and TRY() to propagate errors
- Check for OOM
- Move allocations out of the DevFS constructor
2021-09-06 13:06:05 +02:00
Andreas Kling
efe4e230ee Kernel: Tidy up DevPtsFS construction and handle OOM errors
- Use KResultOr and TRY() to propagate errors
- Check for OOM when creating new inodes
2021-09-06 13:06:05 +02:00
Andreas Kling
f2512071f2 Kernel: Use TRY() in sys$getrandom() 2021-09-06 02:36:21 +02:00
Andreas Kling
a8516681b7 Kernel: Tidy up TmpFS and TmpFSInode construction
- Use KResultOr<NonnullRefPtr<T>>
- Propagate errors
- Use TRY() at call sites
2021-09-06 02:36:21 +02:00
Andreas Kling
398f8e7c96 Kernel: Use TRY() more in process-specific ProcFS code 2021-09-06 02:14:22 +02:00
Andreas Kling
f4a6b60570 Kernel: Use TRY() in Graphics::VirtIOGPU::FrameBufferDevice 2021-09-06 02:11:46 +02:00
Andreas Kling
17a12c8a30 Kernel: Use TRY() in FramebufferDevice 2021-09-06 02:10:01 +02:00
Andreas Kling
7a8061ba0c Kernel: Use TRY() in ISO9660FileSystem 2021-09-06 02:06:49 +02:00
Andreas Kling
a994f11f10 Kernel: Make AddressSpace::add_region() return KResultOr<Region*>
This allows us to use TRY() in a few places.
2021-09-06 02:02:06 +02:00
Andreas Kling
062cc804e7 Kernel: Use TRY() some more in Memory::AddressSpace 2021-09-06 01:59:30 +02:00
Andreas Kling
75564b4a5f Kernel: Make kernel region allocators return KResultOr<NOP<Region>>
This expands the reach of error propagation greatly throughout the
kernel. Sadly, it also exposes the fact that we're allocating (and
doing other fallible things) in constructors all over the place.

This patch doesn't attempt to address that of course. That's work for
our future selves.
2021-09-06 01:55:27 +02:00
Andreas Kling
cb71a73708 Kernel/USB: Tidy up UHCIDescriptorPool construction
- Use KResultOr<NonnullRefPtr<UHCIDescriptorPool<T>>
- Make the constructor private
- Use TRY() at call sites
2021-09-06 01:55:27 +02:00
Andreas Kling
91fe6b6552 Kernel/KCOV: Bring closer to typical SerenityOS coding style
- Remove a bunch of redundant `this->`
- Make class data members private and provide accessors instead
2021-09-06 01:55:27 +02:00
Andreas Kling
79fbad6df9 Kernel/USB: Tidy up USB::Transfer construction 2021-09-06 01:55:27 +02:00
Andreas Kling
f4a9a0d561 Kernel: Make VirtualRangeAllocator return KResultOr<VirtualRange>
This achieves two things:
- The allocator can report more specific errors
- Callers can (and now do) use TRY() :^)
2021-09-06 01:55:27 +02:00
Andreas Kling
21f7932ae2 Kernel: Use TRY() and adopt_nonnull_ref_or_enomem() in AnonymousVMObject 2021-09-06 01:55:27 +02:00
Andreas Kling
52cbbe1dc5 Kernel: Actually share committed CoW pages
Due to a double-move mistake, we were always clearing the shared
committed CoW pages in the parent when forking.
2021-09-06 01:55:27 +02:00
Andreas Kling
2f790cf78f Kernel: Make MM.commit_user_physical_pages() return KResultOr
..and use TRY() at call sites. :^)
2021-09-06 01:55:27 +02:00
Andreas Kling
98dc08fe56 Kernel: Use KResultOr better in ProcessGroup construction
This allows us to use TRY() more.
2021-09-06 01:55:27 +02:00
Andreas Kling
540d62d3b2 Kernel: Simplify WatchDescription::create() 2021-09-06 01:55:27 +02:00
Andreas Kling
5e2bae700d Kernel: Use TRY() in PTYMultiplexer::open() 2021-09-06 01:55:27 +02:00
Andreas Kling
4d961387c1 Kernel: Use TRY() some more in USB::Hub 2021-09-06 01:55:27 +02:00
Andreas Kling
8ceff65161 Kernel: Use TRY() some more in FileDescription 2021-09-06 01:55:27 +02:00
Andreas Kling
5516592748 Kernel: Use TRY() in BlockBasedFileSystem 2021-09-06 01:55:27 +02:00
Ali Mohammad Pur
97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
Ali Mohammad Pur
3a9f00c59b Everywhere: Use OOM-safe ByteBuffer APIs where possible
If we can easily communicate failure, let's avoid asserting and report
failure instead.
2021-09-06 01:53:26 +02:00
Brian Gianforcaro
bb58a4d943 Kernel: Make all Spinlocks use u8 for storage, remove template
The default template argument is only used in one place, and it
looks like it was probably just an oversight. The rest of the Kernel
code all uses u8 as the type. So lets make that the default and remove
the unused template argument, as there doesn't seem to be a reason to
allow the size to be customizable.
2021-09-05 20:46:02 +02:00
Brian Gianforcaro
472454cded Kernel: Switch static_asserts of a type size to AK::AssertSize
This will provide better debug ability when the size comparison fails.
2021-09-05 20:08:57 +02:00
Andreas Kling
100b3835f0 Kernel: Use TRY() in DevFSLinkInode::write_bytes() 2021-09-05 19:02:57 +02:00
Andreas Kling
667a39df6b Kernel: Use TRY() in ProcFS 2021-09-05 19:01:38 +02:00
Andreas Kling
caaeae9607 Kernel: Make FileSystem::get_inode() return KResultOr<NRP<Inode>>
This allows for natural error propagation in a bunch of new places.
2021-09-05 18:55:55 +02:00
Andreas Kling
12d9a6c1fa Kernel: Use TRY() in sys$waitid() 2021-09-05 18:42:32 +02:00
Andreas Kling
c076d765c4 Kernel: Simplify sys$inode_watcher_remove_watch() a little bit 2021-09-05 18:41:28 +02:00
Andreas Kling
d912bfdf38 Kernel: Use TRY() in sys$create_inode_watcher() and friends 2021-09-05 18:41:01 +02:00
Andreas Kling
a9204510a4 Kernel: Make file description lookup return KResultOr
Instead of checking it at every call site (to generate EBADF), we make
file_description(fd) return a KResultOr<NonnullRefPtr<FileDescription>>.

This allows us to wrap all the calls in TRY(). :^)

The only place that got a little bit messier from this is sys$mount(),
and there's a whole bunch of things there in need of cleanup.
2021-09-05 18:36:13 +02:00
Andreas Kling
2d2ea05c97 Kernel: Use TRY() in sys$sethostname() 2021-09-05 18:22:18 +02:00
Andreas Kling
963f847579 Kernel: Use TRY() in sys$mount() 2021-09-05 18:20:57 +02:00
Andreas Kling
3580c5a72e Kernel: Use TRY() in sys$umount() 2021-09-05 18:18:23 +02:00
Andreas Kling
76f2596ce8 Kernel: Use TRY() in sys$set_process_name() 2021-09-05 18:17:06 +02:00
Andreas Kling
53aa01384d Kernel: Use TRY() in sys$set_coredump_metadata() 2021-09-05 18:15:42 +02:00
Andreas Kling
bfe4c84541 Kernel: Use TRY() in sys$create_thread() 2021-09-05 18:15:05 +02:00
Andreas Kling
257fa80312 Kernel: Use TRY() in sys$set_thread_name() 2021-09-05 18:15:05 +02:00
Andreas Kling
afc5bbd56b Kernel: Use TRY() in sys$write() 2021-09-05 18:15:05 +02:00
Andreas Kling
17933b193a Kernel: Use TRY() in sys$perf_register_string() 2021-09-05 18:15:05 +02:00
Andreas Kling
77b7a44691 Kernel: Use TRY() in sys$recvfd() 2021-09-05 18:15:05 +02:00
Andreas Kling
ea911bc125 Kernel: Use TRY() in sys$pledge() 2021-09-05 18:15:05 +02:00
Andreas Kling
95e74d1776 Kernel: Use TRY() even more in sys$mmap() and friends :^) 2021-09-05 18:15:05 +02:00
Andreas Kling
cf2c04eb13 Kernel: Use TRY() in sys$dbgputstr() 2021-09-05 18:15:05 +02:00
Andreas Kling
6dddd500bf Kernel: Use TRY() in sys$map_time_page() 2021-09-05 18:15:05 +02:00
Andreas Kling
d53c60fd9f Kernel: Use TRY() in sys$setkeymap() 2021-09-05 18:15:05 +02:00
Andreas Kling
1f475f7bbc Kernel: Use TRY() in sys$realpath() 2021-09-05 18:15:05 +02:00
Andreas Kling
4ea3dc77f0 Kernel: Use TRY() in sys$statvfs() 2021-09-05 18:15:05 +02:00
Andreas Kling
7efa742a39 Kernel: Use TRY() in sys$stat() 2021-09-05 17:58:08 +02:00
Andreas Kling
de2c1bc5c3 Kernel: Use TRY() in sys$unlink() 2021-09-05 17:56:40 +02:00
Andreas Kling
4e4b7c272c Kernel: Use TRY() in sys$readlink() 2021-09-05 17:55:43 +02:00
Andreas Kling
e0cf9152ca Kernel: Use TRY() in sys$rmdir() 2021-09-05 17:54:33 +02:00
Andreas Kling
780737de7a Kernel: Use TRY() in sys$rename() 2021-09-05 17:53:59 +02:00
Andreas Kling
c5d046c23a Kernel: Use TRY() in sys$utime() 2021-09-05 17:53:28 +02:00
Andreas Kling
789db813d3 Kernel: Use copy_typed_from_user<T> for fetching syscall parameters 2021-09-05 17:51:37 +02:00
Andreas Kling
d7e5768763 Kernel: Add copy_typed_from_userspace<T>(Userspace<T const*>)
This allows easy retrieval of typed POD values from userspace with
implicit error propagation.
2021-09-05 17:50:34 +02:00
Andreas Kling
48a0b31c47 Kernel: Make copy_{from,to}_user() return KResult and use TRY()
This makes EFAULT propagation flow much more naturally. :^)
2021-09-05 17:38:37 +02:00
Andreas Kling
9903f5c6ef Kernel: Use TRY() in sys$read() and sys$readv() 2021-09-05 16:25:40 +02:00
Andreas Kling
c1e18befe8 Kernel: Reorder sys$pipe() to fail more nicely
Try to do both FD allocations up front instead of interleaved between
assigning them to the descriptor table. This prevents us from failing
in the middle of setting up the pipes.
2021-09-05 16:25:40 +02:00
Andreas Kling
4483110990 Kernel: Use TRY() in sys$open() 2021-09-05 16:25:40 +02:00
Andreas Kling
b01e4b171d Kernel: Use TRY() in sys$mknod() 2021-09-05 16:25:40 +02:00
Andreas Kling
2658ab4a80 Kernel: Use TRY() in sys$mkdir() 2021-09-05 16:25:40 +02:00
Andreas Kling
e899ea459c Kernel: Use TRY() in sys$lseek() 2021-09-05 16:25:40 +02:00
Andreas Kling
f605cd04b6 Kernel: Use TRY() in sys$fork()
There's a lot of work to do on improving error propagation in the
fork system call. This just scratches the surface.
2021-09-05 16:25:40 +02:00
Andreas Kling
29bafec43b Kernel: Use is_error() in sys$fcntl() 2021-09-05 16:25:40 +02:00
Andreas Kling
c767e20f91 Kernel: Use TRY() in sys$chown() 2021-09-05 16:25:40 +02:00
Andreas Kling
24e8ad5ade Kernel: Use TRY() in sys$chmod() 2021-09-05 16:25:40 +02:00
Andreas Kling
3631ebe963 Kernel: Use TRY() in AnonymousVMObject 2021-09-05 16:25:40 +02:00
Andreas Kling
abb43468dc Kernel: Use TRY() in FileDescription::attach() 2021-09-05 16:25:40 +02:00
Andreas Kling
aa4d5817af Kernel: Use TRY() in sys$ptrace() 2021-09-05 16:25:40 +02:00
Andreas Kling
b2950c67ea Kernel: Use TRY() in Inode 2021-09-05 16:25:40 +02:00
Andreas Kling
f20d1f0cc6 Kernel: Use TRY() in Region 2021-09-05 16:25:40 +02:00
Andreas Kling
c9a20bcd87 Kernel: Use TRY() in the SoundBlaster16 driver 2021-09-05 16:25:40 +02:00
Andreas Kling
4a2b0f6bec Kernel: Use TRY() in sys$access() 2021-09-05 16:25:40 +02:00
Andreas Kling
c1c774da91 Kernel: Use TRY() in FIFO 2021-09-05 16:25:40 +02:00
Andreas Kling
cad78f5904 Kernel: Use TRY() in InodeFile 2021-09-05 16:25:40 +02:00
Andreas Kling
29a9f80ecf Kernel/USB: Use TRY() in the various USB classes 2021-09-05 16:25:40 +02:00
Andreas Kling
f8fba5f017 Kernel: Use TRY() in sys$socket() and friends 2021-09-05 16:25:40 +02:00
Andreas Kling
8714c550b4 Kernel: Use TRY() in TCPSocket 2021-09-05 16:25:40 +02:00
Andreas Kling
4b7575fabd Kernel: Unbreak x86_64 build (PageDirectory) 2021-09-05 15:38:57 +02:00
Andreas Kling
865eb54f75 Kernel/Ext2FS: Use TRY() in the Ext2FS
This class was just *full* of great opportunities for TRY(). :^)
2021-09-05 15:30:15 +02:00
Andreas Kling
83fed5b2de Kernel: Tidy up Memory::AddressSpace construction
- Return KResultOr<T> in places
- Propagate errors
- Use TRY()
2021-09-05 15:13:20 +02:00
Andreas Kling
0cf65cf7ec Kernel: Use TRY() even more in VirtualFileSystem
Allowing TRY() with KResult unlocked a whole lot more opportunities.
2021-09-05 14:55:51 +02:00
Andreas Kling
e1af24eac8 Kernel: Make KResult usable with TRY() as well
This patch adds release_error() and release_value() to KResult, making
it usable with TRY().

Note that release_value() returns void, since there is no value inside
a KResult.
2021-09-05 14:50:52 +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
b0f2acbd91 Kernel: Use TRY() in FileDescription 2021-09-05 14:43:51 +02:00
Andreas Kling
f2f5df793a Kernel: Use TRY() in sys$chdir() 2021-09-05 14:41:13 +02:00
Andreas Kling
5ba10c6017 Kernel: Use TRY() in Memory::AddressSpace 2021-09-05 14:40:18 +02:00
Andreas Kling
8cd4879946 Kernel: Use TRY() in sys$link() and sys$symlink() 2021-09-05 14:38:28 +02:00
Andreas Kling
c902b3cb0d Kernel: Use TRY() in sys$anon_create() 2021-09-05 14:36:40 +02:00
Andreas Kling
4012099338 Kernel: Tidy up AnonymousFile construction a bit
- Rename create() => try_create()
- Use adopt_nonnull_ref_or_enomem()
2021-09-05 14:33:25 +02:00
Andreas Kling
9a1fdb523f Kernel: Use TRY() in sys$unveil() 2021-09-05 14:30:08 +02:00
Andreas Kling
36efecf3c3 Kernel: Use try in sys$mmap() and friends :^) 2021-09-05 14:27:41 +02:00
Andreas Kling
6bf901b414 Kernel: Use TRY() in sys$execve()
There are more opportunities to use TRY() here, but it will require
improvements to error propagation first.
2021-09-05 14:20:03 +02:00
Andreas Kling
982991d92d Kernel: Use TRY() in IPv4Socket 2021-09-05 14:16:08 +02:00
Andreas Kling
d65fbdc44d Kernel: Use TRY() in Thread 2021-09-05 14:08:12 +02:00
Andreas Kling
ae885b188f Kernel: Use TRY() when creating first thread in a new process 2021-09-05 14:08:12 +02:00
Andreas Kling
2ab8fd89fc Kernel: Simplify Process::get_syscall_path_argument() 2021-09-05 14:08:12 +02:00
Andreas Kling
f30eae7e7a Kernel: Use TRY() in some Process functions 2021-09-05 14:08:12 +02:00
Andreas Kling
81d3f823f7 Kernel: Use TRY() in LocalSocket 2021-09-05 14:08:12 +02:00
Andreas Kling
a5e71debef Kernel: Use TRY() in VirtualFileSystem 2021-09-05 14:08:12 +02:00
Andreas Kling
e1da3b1841 Kernel: Make KResult pull in the TRY macro 2021-09-05 14:08:12 +02:00
Andreas Kling
fd44336ef8 Kernel: Add KResultOr::release_error()
This is just for symmetry with AK::Result, so that we can use them
interchangeably in generic code.
2021-09-05 13:29:06 +02:00
sin-ack
566c5d1e99 AK+Kernel: Move KResult.h to Kernel/API for userspace access
This commit moves the KResult and KResultOr objects to Kernel/API to
signify that they may now be freely used by userspace code at points
where a syscall-related error result is to be expected. It also exposes
KResult and KResultOr to the global namespace to make it nicer to use
for userspace code.
2021-09-05 12:54:48 +02:00
Brian Gianforcaro
3d12d0f408 Kernel: Declare syscall handlers with "using" instead of "typedef"
Also use bit_cast to avoid -Wcast-function-type warning.
2021-09-05 09:48:43 +01:00
Brian Gianforcaro
9d1b27263f Kernel: Declare type aliases with "using" instead of "typedef"
This is the idiomatic way to declare type aliases in modern C++.
Flagged by Sonar Cloud as a "Code Smell", but I happen to agree
with this particular one. :^)
2021-09-05 09:48:43 +01:00
Andreas Kling
7463eb52e8 Kernel: Improve names in the ARP table thread blocker
More instances of functions named "unblock()" that don't actually
unblock in all cases being renamed to something more precise.
2021-09-05 01:10:56 +02:00
Andreas Kling
e851a77346 Kernel: Rename FileBlocker::unblock() => unblock_if_conditions_are_met()
Since this may not actually unblock, the old name was very confusing.
2021-09-05 01:10:56 +02:00
Andreas Kling
68a6d4c30a Kernel: Tidy up InodeWatcher construction
- Rename create() => try_create()
- Use adopt_nonnull_ref_or_enomem()
2021-09-05 01:10:56 +02:00
Andreas Kling
393229e2aa Kernel: Remove UserOrKernelBuffer::copy_into_string()
All former users of this API have been converted to use KString. :^)
2021-09-05 01:10:56 +02:00
Andreas Kling
211c1c087d Kernel/Plan9FS: Use KString instead of String in one place 2021-09-05 01:10:56 +02:00
Andreas Kling
9d736772bd Kernel/Ext2FS: Remove a String allocation in debug logging
We were creating a temporary String just to log the target when writing
a symlink directly into inline storage.
2021-09-05 01:10:55 +02:00
Andreas Kling
7d8e036e26 Kernel: Tidy up UDPSocket creation a bit
- Rename create() => try_create()
- Use adopt_nonnull_ref_or_enomem()
2021-09-04 23:11:04 +02:00
Andreas Kling
648c768d81 Kernel: Tidy up TCPSocket creation a bit
- Rename create() => try_create()
- Use adopt_nonnull_ref_or_enomem()
2021-09-04 23:11:04 +02:00
Andreas Kling
ac85fdeb1c Kernel: Handle ProcessGroup allocation failures better
- Rename create* => try_create*
- Don't null out existing process group on allocation failure
2021-09-04 23:11:04 +02:00
Andreas Kling
12f820eb08 Kernel: Make Process::try_create() propagate errors better 2021-09-04 23:11:04 +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
Andreas Kling
ba1a6ca971 Kernel: Move pledge verification into Process member functions
REQUIRE_PROMISE and REQUIRE_NO_PROMISES were macros for some reason,
and used all over the place.

This patch adds require_promise(Pledge) and require_no_promises()
to Process and makes the macros call these on the current process
instead of inlining code everywhere.
2021-09-04 23:11:04 +02:00
Andreas Kling
5d5a3708c4 Kernel: Rename Thread::clone() => try_clone() and propagate errors 2021-09-04 23:11:04 +02:00
Andreas Kling
cfc1a628d5 Kernel: Add missing error check after Process::try_create()
As the name now suggests, this API can fail. :^)
2021-09-04 23:11:04 +02:00
Andreas Kling
5e2e17c38c Kernel: Rename Process::create() => try_create() 2021-09-04 23:11:03 +02:00
Tom
8a258edfd6 Kernel: Add x2APIC support
This allows addressing all cores on more modern processors. For now,
we still have a hardcoded limit of 64 due to s_processors being a
static array.
2021-09-04 22:22:58 +02:00
Tom
123087e235 Kernel: Allow specifying ecx with CPUID
Some CPUID functions (e.g. 0xb) require input values in ecx.
2021-09-04 22:22:58 +02:00
Liav A
e606ff3751 Kernel/VirtIO: Determine VirtIO device class also with the PCI device ID
According to the VirtIO 1.0 specification:
"Non-transitional devices SHOULD have a PCI Device ID in the range
0x1040 to 0x107f. Non-transitional devices SHOULD have a PCI Revision ID
of 1 or higher. Non-transitional devices SHOULD have a PCI Subsystem
Device ID of 0x40 or higher."

It also says that:
"Transitional devices MUST have a PCI Revision ID of 0. Transitional
devices MUST have the PCI Subsystem Device ID matching the Virtio
Device ID, as indicated in section 5. Transitional devices MUST have the
Transitional PCI Device ID in the range 0x1000 to 0x103f."

So, for legacy devices, we know that revision ID in the PCI header won't
be 1, so we probe for PCI_SUBSYSTEM_ID value.
Instead of using the subsystem device ID, we can probe the DEVICE_ID
value directly in case it's not a legacy device.
This should cover all possibilities for identifying VirtIO devices, both
per the specification of 0.9.5, and future revisions from 1.0 onwards.
2021-09-04 20:53:26 +02:00
Liav A
ed6c1f53af Kernel/VirtIO: Defer initialization of device out of the constructor
This ensures we safely handle interrupts (which can call virtual
functions), so they don't happen in the constructor - this pattern can
lead to a crash, if we are still in the constructor context because
not all methods are available for usage (some are pure virtual,
so it's possible to call __cxa_pure_virtual).

Also, under some conditions like adding a PCI device via PCI-passthrough
mechanism in QEMU, it became exposed to the eye that the code asserts on
RNG::handle_device_config_change(). That device has no configuration but
if the hypervisor still misbehaves and tries to configure it, we should
simply return false to indicate nothing happened.
2021-09-04 16:36:02 +02:00
Liav A
e490c17bde Kernel/SysFS: Ensure data stability when reading from Inodes
Like with the ProcFS, description data can change at anytime, so it's
wise to ensure that when the userland reads from an Inode, data is
consistent unless the userland indicated it wants to refresh the data
(by seeking to offset 0, or re-attaching the Inode).
Otherwise, if the data changes in the middle of the reading, it can
cause silent corruption in output which can lead to random crashes.
2021-09-04 16:33:56 +02:00
Liav A
f2ba44cbdf Kernel/DevFS: Remove unnecessary include 2021-09-04 16:19:12 +02:00
Liav A
d189cb6a24 Kernel/Devices: Remove unnecessary virtual method
The is_disk_device method is not being overrided by any class or called
by anyone, so let's just remove it.
2021-09-04 16:19:12 +02:00
Andreas Kling
74ce098d58 Kernel: Delete the rvalue-ref qualified version of KResultOr::value()
This makes calling value() on a temporary KResultOr be a compile-time
error. This exposed a number of missing error checks (fixed in the
preceding commits.)
2021-09-04 13:08:37 +02:00
Andreas Kling
d065de1fcf Kernel: Add missing error check when opening TTY for a new process 2021-09-04 13:08:37 +02:00
Andreas Kling
4b3bc3805b Kernel: Add missing error check when opening root file system 2021-09-04 13:08:37 +02:00
Andreas Kling
b02eb8224c Kernel/VirtIO: Add two missing error checks in VirtIO::ConsolePort 2021-09-04 13:08:37 +02:00
Daniel Bertalan
d7b6cc6421 Everywhere: Prevent risky implicit casts of (Nonnull)RefPtr
Our existing implementation did not check the element type of the other
pointer in the constructors and move assignment operators. This meant
that some operations that would require explicit casting on raw pointers
were done implicitly, such as:
- downcasting a base class to a derived class (e.g. `Kernel::Inode` =>
  `Kernel::ProcFSDirectoryInode` in Kernel/ProcFS.cpp),
- casting to an unrelated type (e.g. `Promise<bool>` => `Promise<Empty>`
  in LibIMAP/Client.cpp)

This, of course, allows gross violations of the type system, and makes
the need to type-check less obvious before downcasting. Luckily, while
adding the `static_ptr_cast`s, only two truly incorrect usages were
found; in the other instances, our casts just needed to be made
explicit.
2021-09-03 23:20:23 +02:00
brapru
bad23e3f8c Kernel: Convert Routing to east-const style 2021-09-03 23:18:50 +02:00
Luke Wilde
01af7d1ae1 Kernel: Don't use {:p} when printing out invalid userspace stack pointer
`userspace_esp` is a virtual address and thus using `{:p}` on it is
invalid and will cause an assertion failure.

I ran into this while testing #9772.
2021-09-03 11:46:40 +02:00
Andreas Kling
8a89698ea4 Kernel/VirtIO: Stop leaking VirtIO::ConsolePort objects 2021-09-03 02:36:09 +02:00
Andreas Kling
eaf88cc78a AK: Rename create<T> => make_ref_counted<T>
And also try_create<T> => try_make_ref_counted<T>.

A global "create" was a bit much. The new name matches make<T> better,
which we've used for making single-owner objects since forever.
2021-09-03 02:36:09 +02:00
Brian Gianforcaro
c0341215de Kernel: Use = default for empty destructors 2021-09-01 18:06:14 +02:00
Brian Gianforcaro
afa0fb55b0 Kernel: Don't cast to NetworkOrdered<u16>* from random data
NetworkOrdered is a non trivial type, and it's undefined behavior to
cast a random pointer to it and then pretend it's that type.

Instead just call AK::convert_between_host_and_network_endian on the
individual u16*. This suppresses static analysis warnings.

I don't think there was a "bug" in the previous code, it worked, but
it was very brittle.
2021-09-01 18:06:14 +02:00
Brian Gianforcaro
668c429900 Kernel: Convert UserOrKernelBuffer callbacks to use AK::Bytes 2021-09-01 18:06:14 +02:00
Brian Gianforcaro
f3baa5d8c9 Kernel: Convert random bytes interface to use AK::Bytes 2021-09-01 18:06:14 +02:00
Brian Gianforcaro
0678ac265c Kernel: Pass InodeMetadata by reference in TmpFSInode::create
This struct is non-trivially large, it makes sense to pass it by
reference instead of by value in the factory method.

Found by Sonar Cloud.
2021-09-01 18:06:14 +02:00
Andrew Kaster
fcdd7aa990 Kernel: Only unlock Mutex once in execve when PT_TRACE_ME is enabled
Fixes a regression introduced in 70518e6. Fixes #9704.
2021-09-01 13:36:26 +02:00
Peter Elliott
33d7fdca28 Everywhere: Use my cool new @serenityos.org email address 2021-09-01 11:37:25 +04:30
Liav A
8e90a4fd1c Kernel/VirtIO: Don't expose constructors as public method
This leads to a bad pattern where anyone could create an RNG or a
Console object. Instead, let's just use the common pattern of a static
method to instantiate a new object and return it wrapped by a
NonnullRefPtr.
2021-08-31 16:51:13 +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
Liav A
e418740645 Kernel/VirtIO: Move everything into the VirtIO namespace
Before of this change, many specific classes to VirtIO were in the
Kernel namespace, which polluted it.
Everything should be more organized now, but there's still room for
improvement later.
2021-08-31 16:51:13 +02:00
Liav A
9a03c00f45 Kernel/VirtIO: Remove the m_class_name member
This class member was used only to determine the device type when
printing messages to the debug log. Instead, remove this class member,
and add a quick way to find the device type according to how the VirtIO
specification says to do that.

This simplifies construction of VirtIODevices a bit, because now the
constructor doesn't need to ask for a String identified with the device
type.
2021-08-31 16:51:13 +02:00
Liav A
5a0aa66b73 Kernel/PCI: Fix offset error of the PCI_SUBSYSTEM values
Apparently both PCI_SUBSYSTEM_ID and PCI_SUBSYSTEM_VENDOR_ID offsets
should be swapped from one to another to be correct.
2021-08-31 16:51:13 +02:00
Liav A
b596af363c Kernel/VirtIO: Make RNG device to not be a CharacterDevice
This class as a CharacterDevice really was not useful, because you
couldn't even read from it.
Also, the random number generator interface should be the /dev/random,
so any other interface to get random numbers is generally not a good
idea.
Instead, let's keep this functionality as an entropy source for random
numbers generation, but without exposing a device node.
2021-08-31 16:51:13 +02:00
Andreas Kling
5046a1fe38 Kernel: Ignore zero-sized PT_LOAD headers when loading ELF images 2021-08-31 16:46:16 +02:00
Jesse Buhagiar
59eab8148d Kernel/Userland: Expose usb device address and use it in lsusb
We now expose the `USBDevice`'s address in the SysFS object. This means
that device addresses are no longer determined by the name of the file
in the `/bus/usb/` directory. This was an incorrect way of determining
device address, as a standard PC can have multiple USB controllers
(and hence multiple buses) that can have overlapping device IDs.
2021-08-31 16:38:49 +02:00
Owen Smith
22ec21bd61 Kernel: Don't VERIFY_NOT_REACHED in LocalSocket::has_attached_peer()
Invoking sendmsg on a listening socket triggers this assertion as
sendto calls has_attached_peer before checking the result of
send_buffer_for.
2021-08-31 16:00:53 +02:00
LepkoQQ
25f76ed771 Kernel: Fix shift sometimes staying pressed after releasing the key
Previous implementation sometimes didn't release the key after pressing
and holding shift due to repeating key updates when holding keys. This
meant repeating updates would set/unset `m_both_shift_keys_pressed`
repeatedly, sometimes resulting in shift still being considered pressed
even after you released it.

Simplify left and right shift key pressed logic by tracking both key
states separately and always updating modifiers based on them.
2021-08-30 22:35:51 +02:00
Daniel Bertalan
779cf49f38 Kernel: Fix Clang not initializing s_bsp_processor correctly
Initializing the variable this way fixes a kernel panic in Clang where
the object was zero-initialized, so the `m_in_scheduler` contained the
wrong value. GCC got it right, but we're better off making this change,
as leaving uninitialized fields in constant-initialized objects can
cause other weird situations like this. Also, initializing only a single
field to a non-zero value isn't worth the cost of no longer fitting in
`.bss`.

Another two variables suffer from the same problem, even though their
values are supposed to be zero. Removing these causes the
`_GLOBAL_sub_I_` function to no longer be generated and the (not
handled) `.init_array` section to be omitted.
2021-08-30 13:20:34 +02:00
Brian Gianforcaro
f0b3aa0331 Everywhere: Pass AK::Format TypeErasedFormatParams by reference
This silences a overeager warning in sonar cloud, warning that
slicing could occur with `VariadicFormatParams` which derives from
`TypeErasedFormatParams`.

Reference:
https://sonarcloud.io/project/issues?id=SerenityOS_serenity&issues=AXuVPBO_k92xXUF3qWsm&open=AXuVPBO_k92xXUF3qWsm
2021-08-30 15:50:00 +04:30
Andreas Kling
5cd030568f Kernel: Unbreak the LOCK_DEBUG build
Regressed with 68bf6db673
2021-08-30 02:48:31 +02:00
Andreas Kling
68bf6db673 Kernel: Rename Spinlock::is_owned_by_current_thread()
...to is_owned_by_current_processor(). As Tom pointed out, this is
much more accurate. :^)
2021-08-29 22:19:42 +02:00
Andreas Kling
ed0e64943f Kernel: Rename Socket::lock() => Socket::mutex()
"lock" is ambiguous (verb vs noun) while "mutex" is not.
2021-08-29 22:19:42 +02:00
Andreas Kling
0b4671add7 Kernel: {Mutex,Spinlock}::own_lock() => is_locked_by_current_thread()
Rename these API's to make it more clear what they are checking.
2021-08-29 12:53:11 +02:00
Andreas Kling
d9da513959 Kernel: Move "in-scheduler" flag from SchedulerData to Processor
This avoids a race between getting the processor-specific SchedulerData
and accessing it. (Switching to a different CPU in that window means
that we're operating on the wrong SchedulerData.)

Co-authored-by: Tom <tomut@yahoo.com>
2021-08-29 12:43:39 +02:00
Andreas Kling
249d6a490d Kernel: Add read_gs_value() and write_gs_value()
Co-authored-by: Tom <tomut@yahoo.com>
2021-08-29 12:26:40 +02:00
Andreas Kling
6ae60137d7 Kernel: Use StringView instead of C strings in Mutex 2021-08-29 02:21:01 +02:00
Andreas Kling
a28cd921a1 Kernel: Add Socket::set_role() and use it everywhere
Instead of having Socket subclasses write their role into Socket::m_role
directly, add a setter to do this.
2021-08-29 02:21:01 +02:00
Andreas Kling
70b2225b3d Kernel: Store LocalSocket address as a KString internally
Just because we deal with sockaddr_un at the userspace API layer doesn't
mean we have to store an awkward C type internally. :^)
2021-08-29 02:21:01 +02:00
Andreas Kling
48a1a3c0ce Kernel: Rename LocalSocket::create_connected_pair() => try_*() 2021-08-29 01:33:15 +02:00
Andreas Kling
7f96288535 Kernel: Encapsulate assignment of socket origin/acceptor credentials 2021-08-29 01:33:15 +02:00
Andreas Kling
242063866f Kernel: Rename LocalSocket factory to try_create() & tighten return type
Also tighten the return type to KResultOr<NonnullRefPtr<LocalSocket>>
since it cannot return any other socket type.
2021-08-29 01:25:03 +02:00
Andreas Kling
244ede561b Kernel/SysFS: Remove unnecessary mutex lockers in SysFS metadata getters
SysFS inodes have immutable metadata once created.
2021-08-29 01:09:19 +02:00
Andreas Kling
8ebf08b6c8 Kernel: Make all ProcFS and SysFS files zero-sized
There is no value in exposing particular sizes for these files.
2021-08-29 01:09:19 +02:00
Andreas Kling
b959c1bfa9 Kernel: Remove unused members in BIOSSysFSDirectory 2021-08-29 01:09:19 +02:00
Andreas Kling
ed77b6f8cb Kernel: Remove an obviously redundant check in FIFO::read() 2021-08-29 01:09:19 +02:00
Andreas Kling
4cbe348a0f Kernel/Ext2FS: Avoid temporary String allocation during inode creation
Make sure we pass the StringView we get all the way through so it never
turns into a heap-allocated String. :^)
2021-08-29 01:09:19 +02:00
Andreas Kling
4ce99e4ab7 Kernel: Use ProcessID a bit more in Socket
Store the origin and acceptor PID's as ProcessID.
2021-08-29 01:09:19 +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
Andreas Kling
59335bd8ea Kernel: Rename FileDescription::create() => try_create() 2021-08-29 01:09:19 +02:00
Nico Weber
585edb8cff Kernel: Omit all actual code from the kernel on aarch64 for now 2021-08-28 21:51:30 +01:00
Nico Weber
9c5e947e0e Prekernel: Make build on aarch64
Add a dummy Arch/aarch64/boot.S that for now does nothing but
let all processor cores sleep.

For now, none of the actual Prekernel code is built for aarch64.
2021-08-28 21:51:30 +01:00
Nico Weber
da785bd4bd Prekernel: Move boot.S and multiboot.S into an Arch/x86 subfolder 2021-08-28 21:51:30 +01:00
Andrew Kaster
72de228695 Kernel: Verify interrupts are disabled when interacting with Mutexes
This should help prevent deadlocks where a thread blocks on a Mutex
while interrupts are disabled, and makes it impossible for the holder of
the Mutex to make forward progress because it cannot be scheduled in.

Hide it behind a new debug macro LOCK_IN_CRITICAL_DEBUG for now, because
Ext2FS takes a series of Mutexes from the page fault handler, which
executes with interrupts disabled.
2021-08-28 20:53:38 +02:00
Andrew Kaster
4f2520674c Kernel: Don't acquire Mutex for hostname() before scheduling is enabled
There's no reason to acquire the mutex for this resource before APs are
booted, before scheduling is enabled, and before interrupts are enabled.
2021-08-28 20:53:38 +02:00
Andrew Kaster
54161bf5b4 Kernel: Acquire reference to waitee before trying to block in sys$waitid
Previously, we would try to acquire a reference to the all processes
lock or other contended resources while holding both the scheduler lock
and the thread's blocker lock. This could lead to a deadlock if we
actually have to block on those other resources.
2021-08-28 20:53:38 +02:00
Andrew Kaster
dea62fe93c Kernel: Guard the all processes list with a Spinlock rather than a Mutex
There are callers of processes().with or processes().for_each that
require interrupts to be disabled. Taking a Mutexe with interrupts
disabled is a recipe for deadlock, so convert this to a Spinlock.
2021-08-28 20:53:38 +02:00
Andrew Kaster
70518e69f4 Kernel: Unlock ptrace lock before entering a critical section in execve
While it might not be as bad to release a mutex while interrupts are
disabled as it is to acquire one, we don't want to mess with that.
2021-08-28 20:53:38 +02:00
Andrew Kaster
8e70b85215 Kernel: Don't disable interrupts in validate_inode_mmap_prot
There's no need to disable interrupts when trying to access an inode's
shared vmobject. Additionally, Inode::shared_vmobject() acquires a Mutex
which is a recipe for deadlock if acquired with interrupts disabled.
2021-08-28 20:53:38 +02:00
Nico Weber
bbad4758b2 CMake: Let Meta/serenity.sh run aarch64 make it past cmake
This adds just enough scaffolding to make cmake succeed.
The build falls over immediately.
2021-08-28 14:43:07 +01:00
kleines Filmröllchen
d0ceaa24a6 Kernel: Implement ioctl for the SB16 to change sample rate
Two new ioctl requests are used to get and set the sample rate of the
sound card. The SB16 device keeps track of the sample rate separately,
because I don't want to figure out how to read the sample rate from the
device; it's easier that way.

The soundcard write doesn't set the sample rate to 44100 Hz every time
anymore, as we want to change it externally.
2021-08-27 23:35:27 +04:30
kleines Filmröllchen
2c9afaf5ac Kernel: Modernize SB16.cpp
This was some old code that could use mostly some east-const :^)
2021-08-27 23:35:27 +04:30
Brian Gianforcaro
9acf449ced Kernel: Make VirtualAddress methods constexpr
In order to use VirtualAddresses as compile time constants in the
AddressSanitizer implementation, we need to be able to use these
methods in constexpr functions / variable initializations.
2021-08-25 12:12:59 +02:00
Brian Gianforcaro
665e848576 CMake: Remove Prekernel incompatible options instead of overriding
The pattern of having Prekernel inherit all of the build flags of the
Kernel, and then disabling some flags by adding `-fno-<flag>` options
to then disable those options doesn't work in all scenarios. For example
the ASAN flag `-fasan-shadow-offset=<offset>` has no option to disable
it once it's been passed, so in a future change where this flag is added
we need to be able to disable it cleanly.

The cleaner way is to just allow the Prekernel CMake logic to filter out
the COMPILE_OPTIONS specified for that specific target. This allows us
to remove individual options without trashing all inherited options.
2021-08-25 12:12:59 +02:00
Brian Gianforcaro
16ad297078 Kernel: Annotate Memory::Region APIs with [[nodiscard]]
This is an attempt to mitigate callers not observing the result of
map or remap.
2021-08-25 00:18:42 +02:00
Brian Gianforcaro
485f51690d Kernel: Always observe the return value of Region::map and remap
We have seen cases where the map fails, but we return the region
to the caller, causing them to page fault later on when they touch
the region.

The fix is to always observe the return code of map/remap.
2021-08-25 00:18:42 +02:00
Andreas Kling
97f5383525 Kernel: Remove confusing nested scope in Thread::block()
There was a nested scope here that didn't actually scope anything
meaningfully, so just get rid of it.
2021-08-24 16:37:28 +02:00
Andreas Kling
a22634bb59 Kernel: Use TemporaryChange to update Thread::m_in_block
Let's use an RAII helper to avoid having to update this on every path
out of block().

Note that this extends the time under `m_in_block == true` by a little
but that should be harmless.
2021-08-24 16:37:28 +02:00
Andreas Kling
0c1d41cc8a Kernel: Simplify Blockers so they don't need a "should block" flag
The `m_should_block` member variable that many of the Thread::Blocker
subclasses had was really only used to carry state from the constructor
to the immediate-unblock-without-blocking escape hatch.

This patch refactors the blockers so that we don't need to hold on
to this flag after setup_blocker(), and instead the return value from
setup_blocker() is the authority on whether the unblock conditions
are already met.
2021-08-24 16:37:28 +02:00
Andreas Kling
adbf472ca7 Kernel: Remove unused BlockTimeout::m_should_block
This was assigned but never read.
2021-08-24 16:37:28 +02:00
Andreas Kling
cfd9045891 Kernel: Remove unused Thread::Blocker::should_block() virtual
This was previously used after construction to check for early unblock
conditions that couldn't be communicated from the constructor.

Now that we've moved early unblock checks from the constructor into
setup_blocker(), we don't need should_block() anymore.
2021-08-24 16:37:28 +02:00
Andreas Kling
82c3cc4640 Kernel: Move Blocker setup out from constructors into setup_blocker()
Instead of registering with blocker sets and whatnot in the various
Blocker subclass constructors, this patch moves such initialization
to a separate setup_blocker() virtual.

setup_blocker() returns false if there's no need to actually block
the thread. This allows us to bail earlier in Thread::block().
2021-08-24 16:37:28 +02:00
Andreas Kling
b012170d69 Kernel: Don't register thread as custom data for FutexBlocker
Same deal as WaitQueueBlocker, we can get the blocked thread from
Blocker::thread() now, so there's no need to register the current
thread as custom data.
2021-08-24 01:57:11 +02:00
Andreas Kling
2c74533ba6 Kernel: Don't register thread as custom data for WaitQueueBlocker
When adding a WaitQueueBlocker to a WaitQueue, it stored the blocked
thread in the registration's custom "void* data" slot.
This was only used to print the Thread* in some debug logging.

Now that Blocker always knows its origin Thread, we can simply add
a Blocker::thread() accessor and then get the blocked Thread& from
there. No need to register custom data.
2021-08-24 01:57:11 +02:00
Andreas Kling
a58c4bbcf5 Kernel: Make Thread::Blocker::m_thread a NonnullRefPtr<Thread>
There's no harm in the blocker always knowing which thread it originated
from. It also simplifies some logic since we don't need to think about
it ever being null.
2021-08-24 01:57:11 +02:00
Andreas Kling
c351945474 Kernel: Simplify unregistering a Blocker from a BlockerSet
The BlockerSet stores its blockers along with a "void* data" that may
contain some blocker-specific context relevant to the specific blocker
registration (for example, SelectBlocker stores a pointer to the
relevant entry in an array of SelectBlocker::FDInfo structs.)

When unregistering a blocker from a set, we don't need to key the
blocker by both the Blocker* and the data. Just the Blocker* is enough,
since all registrations for that blocker need to be removed anyway as
the blocker is about to be destroyed.

So we stop passing the "void* data" to BlockerSet::remove_blocker(),
which also allows us to remove the now-unneeded Blocker::m_block_data.
2021-08-24 01:57:11 +02:00
Andreas Kling
96909f5200 Kernel: Make Thread::m_block_timer a NonnullRefPtr
Every thread has a block timer, so let's encode that in the type.
2021-08-23 18:07:58 +02:00
Andreas Kling
a8967388d3 Kernel: Remove unused ScopedLockRelease class 2021-08-23 02:17:02 +02:00
Andreas Kling
7006cb82bd Kernel: Rename Blocker::not_blocking(bool) to something more descriptive
Namely, will_unblock_immediately_without_blocking(Reason).

This virtual function is called on a blocker *before any block occurs*,
if it turns out that we don't need to block the thread after all.

This can happens for one of two reasons:

- UnblockImmediatelyReason::UnblockConditionAlreadyMet

    We don't need to block the thread because the condition for
    unblocking it is already met.

- UnblockImmediatelyReason::TimeoutInThePast

    We don't need to block the thread because a timeout was specified
    and that timeout is already in the past.

This patch does not introduce any behavior changes, it's only meant to
clarify this part of the blocking logic.
2021-08-23 02:13:04 +02:00
Andreas Kling
39474830a9 Kernel: Remove unnecessary spinlock in ~Thread::Blocker()
Instead, just VERIFY that nobody is holding the blocker's internal lock.
2021-08-23 01:45:31 +02:00
Andreas Kling
a82b5e6e50 Kernel: Remove unused WaitQueue::should_block(bool)
Nothing ever changed the state of WaitQueue::m_should_block, so this
removes the variable entirely.
2021-08-23 01:42:04 +02:00
Andreas Kling
e51a5e2d5d Kernel: Rename some BlockerSets to foo_blocker_set
Cleanup after renaming BlockCondition to BlockerSet.
2021-08-23 01:42:04 +02:00
Andreas Kling
63f9b0d0dc Kernel: Make Thread::Blocker non-copyable and non-movable 2021-08-23 01:42:04 +02:00
Liav A
aacb1f0bf4 Kernel: Rename PCI::DeviceController => PCI::Device
Now that the old PCI::Device was removed, we can complete the PCI
changes by making the PCI::DeviceController to be named PCI::Device.

Really the entire purpose and the distinction between the two was about
interrupts, but since this is no longer a problem, just rename it to
simplify things further.
2021-08-23 01:07:45 +02:00
Liav A
7b9c3439ec Kernel/PCI: Delete PCI::Device in its current form
I created this class a long time ago just to be able to quickly make a
PCI device to also represent an interrupt handler (because PCI devices
have this capability for most devices).
Then after a while I introduced the PCI::DeviceController, which is
really almost the same thing (a PCI device class that has Address member
in it), but is not tied to interrupts so it can have no interrupts, or
spawn interrupt handlers however it wants to seems fit.

However I decided it's time to say goodbye for this class for
a couple of reasons:
1. It made a whole bunch of weird patterns where you had a PCI::Device
and a PCI::DeviceController being used in the topic of implementation,
where originally, they meant to be used mutually exclusively (you
can't and really don't want to use both).
2. We can really make all the classes that inherit from PCI::Device
to inherit from IRQHandler at this point. Later on, when we have MSI
interrupts support, we can go further and untie things even more.
3. It makes it possible to simplify the VirtIO implementation to a great
extent. While this commit almost doesn't change it, future changes
can untangle some complexity in the VirtIO code.

For UHCIController, E1000NetworkAdapter, NE2000NetworkAdapter,
RTL8139NetworkAdapter, RTL8168NetworkAdapter, E1000ENetworkAdapter we
are simply making them to inherit the IRQHandler. This makes some sense,
because the first 3 devices will never support anything besides IRQs.
For the last 2, they might have MSI support, so when we start to utilize
those, we might need to untie these classes from IRQHandler and spawn
IRQHandler(s) or MSIHandler(s) as needed.

The VirtIODevice class is also a case where we currently need to use
both PCI::DeviceController and IRQHandler classes as parents, but it
could also be untied from the latter.
2021-08-23 01:07:45 +02:00
Liav A
d071ce352c Kernel/VirtIO: Make VirtIODevice constructor protected
This should be accessible only by derived classes.
2021-08-23 01:07:45 +02:00
Andreas Kling
40bc378d81 Kernel: Rename QueueBlocker => WaitQueueBlocker
This is a Thread::Blocker that blocks on a WaitQueue, so let's call it
a WaitQueueBlocker to improve clarity. :^)
2021-08-23 00:10:33 +02:00
Andreas Kling
b30081b49a Kernel: Rename BlockerSet::unblock() to something more accurate
Namely, unblock_all_blockers_whose_conditions_are_met().

The old name made it sound like things were getting unblocked no matter
what, but that's not actually the case.

What this actually does is iterate through the set of blockers,
unblocking those whose conditions are met. So give it a (very) verbose
name that errs on the side of descriptiveness.
2021-08-23 00:02:09 +02:00
Andreas Kling
6c16bedd69 Kernel: Remove unnecessary FutexQueue::did_remove()
This was only ever called immediately after FutexQueue::try_remove()
to VERIFY() that the state looks exactly like it should after returning
from try_remove().
2021-08-23 00:02:09 +02:00
Andreas Kling
928929bbe1 Kernel: VERIFY that nobody is holding lock in ~BlockerSet()
By the time we end up destroying a BlockerSet, we don't need to take
the internal spinlock. And nobody else should be holding it either.
So replace the SpinlockLocker with a VERIFY().
2021-08-23 00:02:09 +02:00
Andreas Kling
85546af417 Kernel: Rename Thread::BlockCondition to BlockerSet
This class represents a set of Thread::Blocker objects attached to
something that those blockers are waiting on.
2021-08-23 00:02:09 +02:00
Andreas Kling
05e1b196e9 Kernel: Make Processor::clean_fpu_state() static
This function returns the same identical FPU state for all CPU's, so
there's no point requiring a Processor instance.
2021-08-23 00:02:09 +02:00
Andreas Kling
8000e8a080 Kernel: Mark Thread::Blocker leaf subclasses final 2021-08-23 00:02:09 +02:00
Andreas Kling
53019f413c Kernel: Mark BlockCondition subclasses as final 2021-08-23 00:02:09 +02:00
Andreas Kling
bcd2025311 Everywhere: Core dump => Coredump
We all know what a coredump is, and it feels more natural to refer to
it as a coredump (most code already does), so let's be consistent.
2021-08-23 00:02:09 +02:00
Andreas Kling
a930877f31 Kernel: Mape quickmap functions VERIFY that MM lock is held
The quickmap_page() and unquickmap_page() functions are used to map a
single physical page at a kernel virtual address for temporary access.

These use the per-CPU quickmap buffer in the page tables, and access to
this is guarded by the MM lock. To prevent bugs, quickmap_page() should
not *take* the MM lock, but rather verify that it is already held!

This exposed two situations where we were using quickmap without holding
the MM lock during page fault handling. This patch is forced to fix
these issues (which is great!) :^)
2021-08-23 00:02:09 +02:00
Andreas Kling
1b9916439f Kernel: Make Processor::platform_string() return StringView 2021-08-23 00:02:09 +02:00
Andreas Kling
492b7152d9 Kernel: Consolidate I386/X86_64 implementations of do_init_context()
We can use ThreadRegisters::set_flags() to avoid the #ifdef's here.
2021-08-23 00:02:09 +02:00
Andreas Kling
7a4f6da61b Kernel: Fix some trivial clang-tidy warnings in x86/common/Processor.cpp 2021-08-23 00:02:09 +02:00
Andreas Kling
dea93a8bb9 Kernel: Rename Processor::id() => current_id()
And let id() be the non-static version that gives you the ID of a
Processor object.
2021-08-23 00:02:09 +02:00
Andreas Kling
0f03a8aece Kernel: Add VERIFY(!m_in_block) when entering Thread::block() 2021-08-23 00:02:09 +02:00
Andreas Kling
d60635cb9d Kernel: Convert Processor::in_irq() to static current_in_irq()
This closes the race window between Processor::current() and a context
switch happening before in_irq().
2021-08-23 00:02:09 +02:00
Andreas Kling
3e3f760808 Kernel: Fix some trivial clang-tidy warnings in Thread.{cpp,h} 2021-08-23 00:02:09 +02:00
Liav A
cd52eb3103 Kernel/SysFS: Don't expose SMBIOS blobs if can't find SMBIOS tables
Don't assume that a platform machine will provide at least the 32 bit
version of SMBIOS tables. If there's no SMBIOS tables, don't expose
directory entries in the /sys/bios/ directory.
2021-08-22 23:42:20 +02:00
Peter Elliott
38e0d1b456 Kernel: Return ENOTSUP instead of panicking on invalid sockopt
X11 handles this gracefully, and it makes more sense than panicking.
2021-08-22 10:29:06 +02:00
Peter Elliott
39a77559f1 Kernel+LibC: Implement FIONREAD ioctl
FIONREAD gets the number of bytes availible to read from a file
descriptor without blocking. I only implemented it for regular files and
sockets
2021-08-22 10:29:06 +02:00
Andreas Kling
c922a7da09 Kernel: Rename ScopedSpinlock => SpinlockLocker
This matches MutexLocker, and doesn't sound like it's a lock itself.
2021-08-22 03:34:10 +02:00
Andreas Kling
55adace359 Kernel: Rename SpinLock => Spinlock 2021-08-22 03:34:10 +02:00
Andreas Kling
7d5d26b048 Kernel: Simplify SpinLockProtected<T>
Same treatment as MutexProtected<T>: inheritance and helper class is
removed, SpinLockProtected now holds a T and a SpinLock.
2021-08-22 03:34:09 +02:00
Andreas Kling
ed6f84c2c9 Kernel: Rename SpinLockProtectedValue<T> => SpinLockProtected<T> 2021-08-22 03:34:09 +02:00
Andreas Kling
532ffa7ddb Kernel: Simplify MutexProtected<T>
This patch removes the MutexContendedResource<T> helper class,
and MutexProtected<T> no longer inherits from T.
Instead, MutexProtected<T> simply has a T and a Mutex.

The LockedResource<T, LockMode> helper class is made a private nested
class in MutexProtected.
2021-08-22 03:34:09 +02:00
Andreas Kling
c2fc33becd Kernel: Rename ProtectedValue<T> => MutexProtected<T>
Let's make it obvious what we're protecting it with.
2021-08-22 03:34:09 +02:00
Andreas Kling
81d990b551 Kernel: Remove some unused classes from Kernel/Locking/ 2021-08-22 03:34:09 +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
Timothy Flynn
02e3633b7f AK: Move FormatParser definition from header to implementation file
This is primarily to be able to remove the GenericLexer include out of
Format.h as well. A subsequent commit will add AK::Result to
GenericLexer, which will cause naming conflicts with other structures
named Result. This can be avoided (for now) by preventing nearly every
file in the system from implicitly including GenericLexer.

Other changes in this commit are to add the GenericLexer include to
files where it is missing.
2021-08-19 23:49:25 +02:00
Andreas Kling
961f727448 Kernel: Consolidate a bunch of i386/x86_64 code paths
Add some arch-specific getters and setters that allow us to merge blocks
that were previously specific to either ARCH(I386) or ARCH(X86_64).
2021-08-19 23:22:02 +02:00
Jesse Buhagiar
6666230d3e Kernel/USB: Remove unused free function from QueueHead
This function is no longer required as the freeing is done
inside of the controller class through the pool interface.
2021-08-19 18:42:07 +02:00
Jesse Buhagiar
3f94857e7c Kernel/USB: Harden Descriptor memory allocation
The previous version of this was pretty bad and caused a lot of
odd behevaiour to occur. We now abstract a lot of the allocation
behind a `template`d pool class that handles all of the memory
allocation.
2021-08-19 18:42:07 +02:00
Jesse Buhagiar
ebb0d3e0eb Kernel/USB: Remove unused include from UHCIController.cpp 2021-08-19 18:42:07 +02:00
Jesse Buhagiar
4abf399a74 Kernel/USB: Move UHCI related structures to subdirectory
The number of UHCI related files is starting to expand to the point
where it's best if we move this into their own subdirectory. It'll
also make it easier to manage when we decide to add some more
controller types (whenever that may be)
2021-08-19 18:42:07 +02:00
Edward Palmer
abccb45060 Kernel: Move set_metadata_dirty calls to notify_watchers 2021-08-19 10:41:54 +02:00
panky-codes
1b093c8b48 Kernel: Const defines for PCI IDs for storage controllers 2021-08-18 18:18:12 +02:00
Sergey Bugaev
ed5b3f8495 Kernel: Specify directory entry types properly
...in a few more places, at least.

find(1) is about to start relying on the reported types more or less
reflecting reality. This is especially relevant for magic symlinks
in ProcFS.
2021-08-18 18:13:59 +02:00
Daniel Bertalan
bd6dc5ccec Meta+LibC: Don't allow text relocations in SerenityOS libraries
The `-z,text` linker flag causes the linker to reject shared libraries
and PIE executables that have textrels. Our code mostly did not use
these except in one place in LibC, which is changed in this commit.
This makes GNU ld match LLD's behavior, which has this option enabled by
default.

TEXTRELs pose a security risk, as performing these relocations require
executable pages to be written to by the dynamic linker. This can
significantly weaken W^X hardening mitigations.

Note that after this change, TEXTRELs can still be used in ports, as the
dynamic loader code is not changed. There are also uses of it in the
kernel, removing which are outside the scope of this PR. To allow those,
`-z,notext` is added.
2021-08-18 18:01:22 +02:00
Gunnar Beutner
6f19bf8501 Kernel: Fix a crash introduced by my TCP RST fix 2021-08-18 10:38:26 +02:00
Gunnar Beutner
c17f9adb12 Kernel: Don't respond to TCP RST packets
As seen with GitHub Pages and probably lots of other hosts this might
cause a loop if the peer decides to respond to our RST packet again.
2021-08-18 10:26:51 +02:00
Brian Gianforcaro
e9d8f158a1 AK+Kernel: StringView hash map Traits should not set peek type to String
This typo / bug in the Traits<T> implementation for StringView caused
AK::HashMap methods to return a `String` when looking up values out of
a hash map of type HashTable<StringView,StringView>.

This change fixes the typo, and fixes the only consumer, the kernel
Commandline class.
2021-08-18 10:21:19 +02:00
Andreas Kling
c70e2f2519 Kernel: Protect the Custody cache with a mutex instead of a spinlock
We don't need to access the Custody cache in IRQs or anything like that,
so it should be fine to use a regular Mutex (via ProtectedValue.)

This allows threads to block while waiting for the custody cache.
Thanks to Sergey for pointing this out. :^)
2021-08-18 00:44:35 +02:00
Andrew January
f62ac5d81d Kernel/SysFS: Prepend to the custody cache instead of append
Usage patterns mean we are more likely to need a Custody we just cached.
Because lookup walks the list from the beginning, prepending new items
instead of appending means they will be found quicker.

This reduces the number of items in the cache we need to walk by 50% for
boot and application startups.
2021-08-17 16:30:03 +02:00
Timothy Flynn
eedb26110a Kernel: Fix compilation with Clang
Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
2021-08-17 13:31:43 +01:00
Andreas Kling
1ea604adb1 Kernel/ProcFS: Avoid String allocation when traversing /proc/PID/fd/ 2021-08-17 01:21:47 +02:00
Andreas Kling
45a5cf95f9 Kernel: Add a little explainer comment to ListedRefCounted 2021-08-17 01:21:47 +02:00
Andreas Kling
ea09294351 Kernel: Port Inode to ListedRefCounted
This consolidates the lock+list combo into a SpinLockProtectedValue
and closes yet another unref() race. :^)
2021-08-17 01:21:47 +02:00
Andreas Kling
b70b3a9b92 Kernel: Remove unused VMObject::is_contiguous() 2021-08-17 01:21:47 +02:00
Andreas Kling
4226b662cd Kernel+Userland: Remove global futexes
We only ever use private futexes, so it doesn't make sense to carry
around all the complexity required for global (cross-process) futexes.
2021-08-17 01:21:47 +02:00
Andreas Kling
7979b5a8bb Kernel: Port VMObject to ListedRefCounted
The VMObject class now manages its own instance list (it was previously
a member of MemoryManager.) Removal from the list is done safely on the
last unref(), closing a race window in the previous implementation.

Note that VMObject::all_instances() now has its own lock instead of
using the global MM lock.
2021-08-17 01:21:47 +02:00
Andreas Kling
3a2d888913 Kernel/DevPtsFS: Avoid String allocation during directory traversal
Use a StringBuilder to generate a temporary string buffer with the
slave PTY names. (This works because StringBuilder has 128 bytes of
inline stack capacity before it does any heap allocations.)
2021-08-17 01:21:47 +02:00
Andreas Kling
0de8c95d49 Kernel: Convert SlavePTY all-instances HashTable to an IntrusiveList
This simplifies the DevPtsFS implementation somewhat, as it no longer
requires SlavePTY to register itself with it, since it can now simply
use the list of SlavePTY instances.
2021-08-17 01:21:47 +02:00
Andreas Kling
62719b85e0 Kernel: Port Thread to ListedRefCounted 2021-08-17 01:21:47 +02:00
Andreas Kling
c410f08c2b Kernel: Add ListedRefCounted<T> template class
This class implements the emerging "ref-counted object that participates
in a lock-protected list that requires safe removal on unref()" pattern
as a base class that can be inherited in place of RefCounted<T>.
2021-08-17 01:21:47 +02:00
Andreas Kling
641083f3b8 Kernel: Customize File::unref() and make it virtual
Make File inherit from RefCountedBase and provide a custom unref()
implementation. This will allow subclasses that participate in lists to
remove themselves in a safe way when being destroyed.
2021-08-17 01:21:47 +02:00
Andreas Kling
fc0cd8317a Kernel/DevPtsFS: Add tightly typed DevPtsFSInode::fs() 2021-08-17 01:21:47 +02:00
Itamar
f3aa87c436 Kernel: Make makedev()/minor()/major() static
This was originally done in 7274037 and for some reason reverted in
740140a.

This avoids "multiple definitions" link errors and fixes the libuv port.
2021-08-16 23:26:52 +02:00
Ali Mohammad Pur
64bc5f668d Kernel: Avoid enumerating all the fds to find a specific one in procfs 2021-08-16 21:16:14 +02:00
Ali Mohammad Pur
e9feced041 Kernel: Add a Process::FileDescriptions::get_if_valid(index) API
Note that this is not the same as ::at(index), which has a different
precondition ("allocated" vs "valid").
2021-08-16 21:16:14 +02:00
Andreas Kling
c1c12497b5 Kernel: Don't hold thread list lock while invoking ~Thread()
There is no need for this, and it can cause deadlocks if ~Thread()
ends up doing something else that requires a lock (e.g ~Process())
2021-08-16 02:22:42 +02:00
Andreas Kling
29a58459ab Kernel: Use ProtectedValue for VirtualFileSystem::m_mounts
This is what VirtualFileSystem::m_lock was actually guarding, and
wrapping it in a ProtectedValue makes it so much more obvious how it
all works. :^)
2021-08-16 01:41:26 +02:00
Andreas Kling
d6d7d11590 Kernel: Cache Custody objects (weakly) to avoid expensive reconstruction
This patch adds a (spinlock-protected) custody cache. It's a simple
intrusive list containing all currently live custody objects.

This allows us to re-use existing custodies instead of creating them
again and again.

This gives a pretty decent performance improvement on "find /" :^)
2021-08-15 23:30:52 +02:00
Andreas Kling
a412fd2ed8 Kernel/ProcFS: Avoid two unnecessary number-to-string conversions
We don't need to create a new string from a number in order to compare
an existing string to that number. Converting the existing string to a
number is much cheaper, since it does not require any heap allocations.

Ran into this while profiling "find /" :^)
2021-08-15 23:30:52 +02:00
Andreas Kling
9c373b646b Kernel/SysFS: Don't compute exact size of PCI files
There's no need for generated files in SysFS to tell you their precise
file size when you stat() them.

I noticed when profiling "find /" that we were spending a chunk of time
generating and throwing away SysFS content just so we could tell you
exactly how large it would be. :^)
2021-08-15 23:30:52 +02:00
brapru
f633ef2af7 Kernel: Move ARP debug information to ARP_DEBUG
Previously when trying to debug the system's routing, the ARP
information would clutter the output and make it difficult to focus on
the routing decisions. It would be better to specify these
debug messages under ARP_DEBUG.
2021-08-15 21:53:29 +02:00
Andreas Kling
cda69704dc Kernel: Remove unused FIFO::all_fifos() table 2021-08-15 16:53:03 +02:00
Andreas Kling
6a20733fcd Kernel: Convert TCP retransmit queue from HashTable to IntrusiveList 2021-08-15 16:53:03 +02:00
Andreas Kling
7063303022 Kernel: Convert IPv4 socket list from HashTable to IntrusiveList
There was no reason whatsoever to use a HashTable here. IntrusiveList
removes all the heap allocations and does everything more efficiently.
2021-08-15 16:53:03 +02:00
Andreas Kling
a154faebb7 Kernel: Use NonnullRefPtr<PhysicalPage> in PageDirectory::m_page_tables
We don't care to store null page pointers in the page table map.
2021-08-15 16:53:03 +02:00
sin-ack
0a18425cbb Kernel: Make Memory::Region allocation functions return KResultOr
This makes for some nicer handling of errors compared to checking an
OwnPtr for null state.
2021-08-15 15:41:02 +02:00
sin-ack
4bfd6e41b9 Kernel: Make Kernel::VMObject allocation functions return KResultOr
This makes for nicer handling of errors compared to checking whether a
RefPtr is null. Additionally, this will give way to return different
types of errors in the future.
2021-08-15 15:41:02 +02:00