Commit graph

6451 commits

Author SHA1 Message Date
Daniel Bertalan
182016d7c0 Kernel+LibC+LibCore+UE: Implement fchmodat(2)
This function is an extended version of `chmod(2)` that lets one control
whether to dereference symlinks, and specify a file descriptor to a
directory that will be used as the base for relative paths.
2022-01-12 14:54:12 +01:00
Andreas Kling
5f71925aa4 Kernel: Actually clear page slots in Region::clear_to_zero()
We were copying the RefPtr<PhysicalPage> and zeroing the copy instead
of zeroing the slot itself.
2022-01-12 14:52:47 +01:00
Andreas Kling
d8206c1059 Kernel: Don't release/relock spinlocks repeatedly during space teardown
Grab the page directory and MM locks once at the start of address space
teardown, then hold onto them across all the region unmapping work.
2022-01-12 14:52:47 +01:00
Andreas Kling
2323cdd914 Kernel: Do less unnecessary work when tearing down process address space
When deleting an entire AddressSpace, we don't need to do TLB flushes
at all (since the entire page directory is going away anyway).

We also don't need to deallocate VM ranges one by one, since the entire
VM range allocator will be deleted anyway.
2022-01-12 14:52:47 +01:00
Brian Gianforcaro
9592ba3548 Kernel: Avoid potential allocation when enumerating cpu features
We can use `StringView::for_each_split_view` here to avoid the potential
allocation of `Vector<StringView>` elements we would get from the normal
Split view functions.
2022-01-12 13:03:44 +01:00
Brian Gianforcaro
faf1fa0a84 Kernel: Convert Processor::features_string() API to KString 2022-01-12 13:03:44 +01:00
Brian Gianforcaro
f8ad3cc6a0 Kernel: Harden DevPtsFSInode::traverse_as_directory against OOM
Use the try variants of AK::StringBuilder append APIs to harden this
function against OOM.
2022-01-12 13:03:44 +01:00
Andreas Kling
05ed8d1738 Kernel: Wait for the ATA busy bit to clear after switching channels
This is a speculative fix for a flaky boot crash that shows up every now
and then on CI.

Fixes #10177. Hopefully.
2022-01-12 02:08:58 +01:00
Andreas Kling
8177e7eb22 Kernel: Clarify IDEChannel function that switches current channel
Rename wait_until_not_busy() => select_device_and_wait_until_not_busy()
to make it more obvious what this thing is doing.
2022-01-12 01:57:38 +01:00
Andreas Kling
a4b4b358ff AK+Kernel: Remove one_ref_left() footgun
This mechanism was unsafe to use in any multithreaded context, since
the hook function was invoked on a raw pointer *after* decrementing
the local ref count.

Since we don't use it for anything anymore, let's just get rid of it.
2022-01-11 01:12:16 +01:00
Andreas Kling
08e927f084 Kernel: Synchronize removals from TmpFS inode map
Previously we were uncaching inodes from TmpFSInode::one_ref_left().
This was not safe, since one_ref_left() was effectively being called
on a raw pointer after decrementing the local ref count and observing
it become 1. There was a race here where someone else could trigger
the destructor by unreffing to 0 before one_ref_left() got called,
causing us to call one_ref_left() on a deleted inode.

We fix this by using the new remove_from_secondary_lists() mechanism
in ListedRefCounted and synchronizing all access to the TmpFS inode
map with the main Inode::all_instances() lock.

There's probably a nicer way to solve this.
2022-01-11 01:12:16 +01:00
Andreas Kling
3550f12543 Kernel: Make ListedRefCounted::unref() call optional list removal helper
Look for remove_from_secondary_lists() and call it on the ref-counting
target if present *while the lock is held*.

This allows listed-ref-counted objects to be present in multiple lists
and still have synchronized removal on final unref.
2022-01-11 01:12:16 +01:00
Andreas Kling
a4b3bf1d63 Kernel: Remove empty Ext2FSInode::one_ref_left() 2022-01-11 01:12:16 +01:00
Andreas Kling
b66bb11ed1 Kernel: Remove empty ISO9660Inode::one_ref_left() 2022-01-11 01:12:16 +01:00
kleines Filmröllchen
e2c9578390 Kernel: Allow preventing kmalloc and kfree
For "destructive" disallowance of allocations throughout the system,
Thread gains a member that controls whether allocations are currently
allowed or not. kmalloc checks this member on both allocations and
deallocations (with the exception of early boot) and panics the kernel
if allocations are disabled. This will allow for critical sections that
can't be allowed to allocate to fail-fast, making for easier debugging.

PS: My first proper Kernel commit :^)
2022-01-11 00:08:58 +01:00
Linus Groh
49471b63f8 Kernel: Fix indentation in CMakeLists.txt 2022-01-10 19:10:02 +01:00
Andreas Kling
24ecf1d021 Kernel: Remove redundant hash map of page tables in PageDirectory
The purpose of the PageDirectory::m_page_tables map was really just
to act as ref-counting storage for PhysicalPage objects that were
being used for the directory's page tables.

However, this was basically redundant, since we can find the physical
address of each page table from the page directory, and we can find the
PhysicalPage object from MemoryManager::get_physical_page_entry().
So if we just manually ref() and unref() the pages when they go in and
out of the directory, we no longer need PageDirectory::m_page_tables!

Not only does this remove a bunch of kmalloc() traffic, it also solves
a race condition that would occur when lazily adding a new page table
to a directory:

Previously, when MemoryManager::ensure_pte() would call HashMap::set()
to insert the new page table into m_page_tables, if the HashMap had to
grow its internal storage, it would call kmalloc(). If that kmalloc()
would need to perform heap expansion, it would end up calling
ensure_pte() again, which would clobber the page directory mapping used
by the outer invocation of ensure_pte().

The net result of the above bug would be that any invocation of
MemoryManager::ensure_pte() could erroneously return a pointer into
a kernel page table instead of the correct one!

This whole problem goes away when we remove the HashMap, as ensure_pte()
no longer does anything that allocates from the heap.
2022-01-10 16:22:37 +01:00
Andreas Kling
bdbff9df24 Kernel: Don't relock MM lock for every page when remapping region
Make sure that callers already hold the MM lock, and we don't have to
worry about reacquiring it every time.
2022-01-10 16:22:37 +01:00
Hendiadyoin1
1cdace7898 Kernel: Add implied auto qualifiers in Memory 2022-01-09 23:29:57 -08:00
Pankaj Raghav
9ae2285675 Kernel: Make enumerate_disk_partitions function not const
The enumerate_disk_partitions function doesn't need to be const. Remove
the constness and use the newly added `add_partition` function.
2022-01-09 20:18:37 -08:00
Pankaj Raghav
2c810332b6 Kernel: Add add_partition function
Until now partitions were added directly by accessing the private member
of the StorageDevice class.

Add a new member function to add partition.
2022-01-09 20:18:37 -08:00
Jelle Raaijmakers
864f842dfe Kernel: Page-align AC'97 audio buffer descriptor list
This was broken in commit 0a1b34c753 / PR #11687 since the buffer
descriptor list size was not page-aligned, and the new
`MM.allocate_dma_buffer_pages` expects a page-aligned size.
2022-01-09 19:11:29 +02:00
Brian Gianforcaro
ccbc3f8975 Kernel: Harden Socket::pseudo_path(..) implementations against OOM
Use the try variants of AK::StringBuilder append APIs to harden these
functions against OOM.
2022-01-09 12:22:52 +01:00
Michel Hermier
69cabb3ead Everywhere: Add serenity_dev_{makedev,major,minor}
Add them in `<Kernel/API/Device.h>` and use these to provides
`{makedev,major,minor}` in `<sys/sysmacros.h>`. It aims to be more in
line with other Unix implementations and avoid code duplication in user
land.
2022-01-09 00:58:44 +01:00
Pankaj Raghav
0a1b34c753 Kernel: Use DMA helper everywhere
Port UCHI, AC97, SB16, BMIDEChannel and AHCIPort to use the helper to
allocate DMA buffers.
2022-01-09 00:45:38 +01:00
Pankaj Raghav
59da9bd0bd Kernel: Overload DMA helper without Physical Page output parameter
Not all drivers need the PhysicalPage output parameter while creating
a DMA buffer. This overload will avoid creating a temporary variable
for the caller
2022-01-09 00:45:38 +01:00
Pankaj Raghav
e79f94f998 Kernel: Set Cacheable parameter to NO explicitly in DMA helpers
The cacheable parameter to allocate_kernel_region should be explicitly
set to No as this region is used to do physical memory transfers. Even
though most architectures ignore this even if it is set, it is better
to make this explicit.
2022-01-09 00:45:38 +01:00
Liav A
ac2c01320b Kernel/PCI: Split host bridge code from the Access singleton
Two classes are added - HostBridge and MemoryBackedHostBridge, which
both derive from HostController class. This allows the kernel to map
different busses from different PCI domains in the same time. Each
HostController implementation doesn't take the Address object to address
PCI devices but instead we take distinct numbers of the PCI bus, device
and function as it allows us to specify arbitrary PCI domains in the
Address structure and still to get the correct PCI devices. This also
matches the hardware behavior of PCI domains - the host bridge merely
takes memory operations or IO operations and translates them to
addressing of three components - PCI bus, device and function.

These changes also greatly simplify how enumeration of Host Bridges work
now - scanning of the hardware depends on what the Host bridges can do
for us, so in case we have multiple host bridges that expose a memory
mapped region or IO ports to access PCI configuration space, we simply
let the code of the host bridge to figure out how to fetch data for us.

Another semantical change is that a PCI domain structure is no longer
attached to a PhysicalAddress, so even in the case that the machine
doesn't implement PCI domains, we still treat that machine to contain 1
PCI domain to treat that one host bridge in the same way, like with a
machine with one or more PCI domains.
2022-01-08 23:49:26 +01:00
creator1creeper1
3c05261611 AK+Everywhere: Make FixedArray OOM-safe
FixedArray now doesn't expose any infallible constructors anymore.
Rather, it exposes fallible methods. Therefore, it can be used for
OOM-safe code.
This commit also converts the rest of the system to use the new API.
However, as an example, VMObject can't take advantage of this yet,
as we would have to endow VMObject with a fallible static
construction method, which would require a very fundamental change
to VMObject's whole inheritance hierarchy.
2022-01-08 22:54:05 +01:00
Andreas Kling
a7b70c62d7 Kernel: Fix broken disk cache behavior with O_DIRECT
When writing into a block via an O_DIRECT open file description, we
would first flush every dirty block *except* the one we're about to
write into.

The purpose of flushing is to ensure coherency when mixing direct and
indirect accesses to the same file. This patch fixes the issue by only
flushing the affected block.

Our behavior was totally backwards and could end up doing a lot of
unnecessary work while avoiding the work that actually mattered.
2022-01-08 20:15:25 +01:00
Andreas Kling
95df924e0e Kernel: Remove EWHYTHO error code :^)
This error code was a hack for catching error handling mistakes in the
kernel. It's no longer used anywhere.
2022-01-08 20:10:58 +01:00
Andreas Kling
065f79990e Kernel: Rename DiskCache::get() to ensure()
Since this function always returns a CacheEntry& (after potentially
evicting someone else to make room), let's call it "ensure" instead of
"get" to match how we usually use these terms.
2022-01-08 19:57:33 +01:00
Andreas Kling
01823746e3 Kernel: Remove redundant disk cache dirty flag in BlockBasedFileSystem
The disk cache is dirty if the dirty list has entries. No need to track
this in a separate flag.
2022-01-08 19:38:00 +01:00
Andreas Kling
5871072ed3 Kernel: Unbreak ref counting hooks in ListedRefCounted & RefCounted
We have to mind the constness of the pointer when using "requires" to
check if a member function can be invoked.

I regressed this in c4a0f01b02.
2022-01-08 17:25:37 +01:00
Andreas Kling
e53571ef59 Kernel: Dump memory regions when crashing in ring 0 2022-01-08 16:31:14 +01:00
Andreas Kling
b86443f0e1 Kernel: Lock weak pointer revocation during listed-ref-counted unref
When doing the last unref() on a listed-ref-counted object, we keep
the list locked while mutating the ref count. The destructor itself
is invoked after unlocking the list.

This was racy with weakable classes, since their weak pointer factory
still pointed to the object after we'd decided to destroy it. That
opened a small time window where someone could try to strong-ref a weak
pointer to an object after it was removed from the list, but just before
the destructor got invoked.

This patch closes the race window by explicitly revoking all weak
pointers while the list is locked.
2022-01-08 16:31:14 +01:00
Andreas Kling
c4a0f01b02 AK+Kernel: Use requires expression when invoking ref counting hooks
Replace some old-school template trickery with C++20 requires. :^)
2022-01-08 16:31:14 +01:00
Liav A
f7d1b8cd0c Kernel: Avoid potential memory info leak when doing mmap on /dev/mem
Although we can still consider this impossible to happen now, because
the mmap syscall entry code verifies that specified offset must be page
aligned, it's still a good practice to VERIFY we actually take a start
address as page-aligned in case of doing mmap on /dev/mem.

As for read(2) on /dev/mem, we don't map anything to userspace so it's
safe to read from whatever offset userspace specified as long as it does
not break the original rules of reading physical memory from /dev/mem.
2022-01-08 13:21:16 +02:00
Liav A
ca254699ec Kernel: Implement read functionality for MemoryDevice
So far we only had mmap(2) functionality on the /dev/mem device, but now
we can also do read(2) on it.

The test unit was updated to check we are doing it safely.
2022-01-08 13:21:16 +02:00
Liav A
6feb07fe43 Kernel: Remove needless did_seek method override in MemoryDevice class 2022-01-08 13:21:16 +02:00
Liav A
876559d283 Kernel: Change method name to clarify physical memory mmap validation 2022-01-08 13:21:16 +02:00
Liav A
3e066d380d Kernel/Memory: Remove needless VERIFY in /dev/mem mmap validation method
As it was pointed by Idan Horowitz, the rest of the method doesn't
assume we have any reserved ranges to allow mmap(2) to work on them, so
the VERIFY is not needed at all.
2022-01-07 19:13:27 +02:00
mjz19910
10ec98dd38 Everywhere: Fix spelling mistakes 2022-01-07 15:44:42 +01:00
mjz19910
3102d8e160 Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
Andreas Kling
626507943c Kernel: Remove redundant socket_by_tuple removal in ~TCPSocket() 2022-01-06 22:51:28 +01:00
Tom
6a4d06e739 Kernel: Only use multiboot framebuffer details if present
We should only look at the framebuffer structure members if the
MULTIBOOT_INFO_FRAMEBUFFER_INFO bit is set in the flags field.

Also add some logging if we ignored the fbdev command line argument
due to either not having a framebuffer provided by the bootloader, or
because we don't support the framebuffer format.
2022-01-06 22:34:11 +01:00
Tom
785c10fda9 Kernel: Add option to force using only the bootloader framebuffer
This allows forcing the use of only the framebuffer set up by the
bootloader and skips instantiating devices for any other graphics
cards that may be present.
2022-01-06 22:34:11 +01:00
Andreas Kling
01b3666894 Kernel: Lock TCPSocket lookup table across destruction
Use the same trick as SlavePTY and override unref() to provide safe
removal from the sockets_by_tuple table when destroying a TCPSocket.

This should fix the TCPSocket::from_tuple() flake seen on CI.
2022-01-06 22:30:40 +01:00
Andreas Kling
bcc0186bbc Kernel/Ext2FS: Use HashMap::remove_all_matching() in Ext2FS
This makes the inode cache eviction mechanism quite a bit easier
to understand, thanks to the new expressive API. :^)
2022-01-05 18:57:14 +01:00
Brian Gianforcaro
4b4cf06069 Kernel: Remove temporary Vector from Ext2FS::flush_writes()
Previously we were using this vector to store the inodes as we iterated.
However, we don't need to store all of them, just the previous inode, as
we know it will be safe to remove it once we've iterated past that
element.
2022-01-05 14:04:18 +01:00