Commit Graph

375 Commits

Author SHA1 Message Date
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
Daniel Bertalan
2a5d9a6944 Kernel: Fix adopt_ref_if_nonnull(new T).release_nonnull() pattern
This does the exact thing as `adopt_ref`, which is a recent addition to
AK.

Note that pointers returned by a bare new (without `nothrow`) are
guaranteed not to return null, so they can safely be converted into
references.
2021-06-29 22:57:52 +04:30
Sahan Fernando
ae5d961d97 Kernel: Pass TTY-switch keyboard combo to userspace 2021-06-25 19:26:30 +02:00
Daniel Bertalan
f820917a76 Everywhere: Use nothrow new with adopt_{ref,own}_if_nonnull
This commit converts naked `new`s to `AK::try_make` and `AK::try_create`
wherever possible. If the called constructor is private, this can not be
done, so we instead now use the standard-defined and compiler-agnostic
`new (nothrow)`.
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
Hendiadyoin1
7ca3d413f7 Kernel: Pull apart CPU.h
This does not add any functional changes
2021-06-24 00:38:23 +02:00
Liav A
29f9a38f76 Kernel: Don't use naked new statements in init process
Instead, try to create the device objects in separate static methods,
and if we fail for some odd reason to allocate memory for such devices,
just panic with that reason.
2021-06-19 09:30:43 +02:00
Jesse Buhagiar
71c9572e74 USB: Store device descriptor on enumeration
We now store the device descriptor obtained from the device
during enumeration in the device's object in memory instead
of exposing all of the different members contained within it.
2021-06-18 17:04:57 +04:30
Jesse Buhagiar
7b42146f33 USB: Store devices in globally accessible array
USB Devices are now stored so that they may be later retrieved and
operated on (i.e, fetching their assigned device address via
ProcFS)
2021-06-18 17:04:57 +04:30
Gunnar Beutner
9b14a8605a Kernel: Add a VERIFY() to make sure our DMA address is valid
This checks whether the address we're trying to use for DMA is low
enough so as not to overflow the I/O register.
2021-06-17 19:52:13 +02:00
Liav A
b91df26d4a Kernel/Interrupts: Return boolean on whether we handled the interrupt
If we are in a shared interrupt handler, the called handlers might
indicate it was not their interrupt, so we should not increment the
call counter of these handlers.
2021-06-17 16:53:25 +02:00
Liav A
c802b1a363 Kernel/Interrupts: Add sensible purposes to VirtIO and USB devices
When we enumerate the interrupt handlers, it's a good idea to show a
meaningful name to the user instead of "IRQ Handler".
2021-06-17 16:53:25 +02:00
Gunnar Beutner
bc3076f894 Kernel: Remove various other uses of ssize_t 2021-06-16 21:29:36 +02:00
Jesse Buhagiar
06f1edb516 USB: Further Implement USB Structures
These are the actual structures that allow USB to work (i.e the ones
actually defined in the specification). This should provide us enough
of a baseline implementation that we can build on to support
different types of USB device.
2021-06-12 18:17:25 +04:30
Brian Gianforcaro
f124affc8e Kernel: Mark PCISerialDevice::detect() as UNMAP_AFTER_INIT 2021-06-09 10:57:51 +02:00
Sebastian Zaha
77044dd383
Kernel: Fix crash when switching to console 5 & 6
The changes in commit 20743e8 removed the s_max_virtual_consoles
constant and hardcoded the number of consoles to 4. But in
PS2KeyboardDevice the keyboard shortcuts for switching to consoles were
hardcoded to 6.

I reintroduced the constant and added it in both places.
2021-05-31 17:42:21 +01:00
Gunnar Beutner
1ce32ef675 Kernel: Let the user read/write more than one page from/to dev files
Previously reads and writes to /dev/zero, /dev/full, /dev/null and
/dev/random were limited to 4096 bytes.

This removes that restriction so that users can enjoy more zero bytes
in their buffers.
2021-05-27 09:30:19 +02:00
Liav A
38ccdb02ce Kernel: Process request to change virtual console from the IO Work queue
Instead of processing the input after receiving an IRQ, we shift the
responsibility to the io work queue to handle this for us, so if a page
fault occurs when trying to switch the VirtualConsole, the kernel can
handle that.
2021-05-21 08:08:33 +02:00
Idan Horowitz
df7d938808 Kernel: Stop allocating the PS2KeyboardDevice in the eternal heap
The PS2KeyboardDevice can be free'd in try_to_initialize if the
initialization failed, resulting in an assertion.
2021-05-20 18:40:02 +02:00
Lenny Maiorani
5751327195 Kernel: static vs non-static constexpr variables
Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
2021-05-19 21:21:47 +01:00
Sahan Fernando
d0f314b23c Kernel: Fix subtle race condition in sys$write implementation
There is a slight race condition in our implementation of write().
We call File::can_write() before attempting to write to it (blocking if
it returns false). If it returns true, we assume that we can write to
the file, and our code assumes that File::write() cannot possibly fail
by being blocked. There is, however, the rare case where another process
writes to the file and prevents further writes in between the call to
Files::can_write() and File::write() in the first process. This would
result in the first process calling File::write() when it cannot be
written to.

We fix this by adding a mechanism for File::can_write() to signal that
it was blocked, making it the responsibilty of File::write() to check
whether it can write and then finally making sys$write() check if the
write failed due to it being blocked.
2021-05-18 16:33:15 +02:00
Idan Horowitz
208cfcb0a5 Kernel: Add support for multiple serial ports per device
This commit adds support for initializing multiple serial ports per
PCI board, as well as initializing multiple different pci serial boards

Currently we just choose the first PCI serial port seen as the debug
port, but this should probably be made configurable some how in the
future.
2021-05-18 16:31:39 +02:00
Idan Horowitz
79d3910145 Kernel: Stop overriding built-in serial port with PCI serial port
On a second thought, theres nothing stopping us from allowing poeple to
use both if they want to :^)
2021-05-17 19:45:35 +01:00
Idan Horowitz
51e9fdebea Kernel: Add support for QEMU's emulated pci serial (-pci-serial option) 2021-05-17 19:45:35 +01:00
Idan Horowitz
ba9b3dc656 Kernel: Implement a PCI Serial Device driver
This simple driver simply finds a device in a device definitions list
and then sets up a SerialDevice instance based on the definition.

The driver currently only supports "WCH CH382 2S" pci serial boards,
as that is the only device available for me to test with, but most
other pci serial devices should be as easily addable as adding a
board_definitions entry.
2021-05-17 18:15:25 +02:00
Idan Horowitz
62f69cc50f Kernel: Use IOAddress instead of direct IO calls in SerialDevice 2021-05-17 18:15:25 +02:00
Idan Horowitz
a5699a141d Kernel: Add a put_char(char) method to SerialDevice
This can be used to print a single char to the serial port the
SerialDevice instance handles.
2021-05-17 18:15:25 +02:00
Idan Horowitz
c75ca4ea8f Kernel: Bit mask line control options in SerialDevice::set_line_control
The line control option bits (parity, stop bits, word length) were
masked and then combined incorrectly, resulting in them not being set
when requested.
2021-05-17 18:15:25 +02:00
Idan Horowitz
be57c424f3 Kernel: Swap baud rate divisor registers in SerialDevice::set_baud
These were accidentally the wrong way around (LSB part of the divisor
into the MSB register, MSB part of the divisor into the LSB register)
as can be seen in the specification (and in the comments themselves)
2021-05-17 18:15:25 +02:00
Idan Horowitz
0e5aba16ef Kernel: Use unsigned instead of signed types in SerialDevice
Addresses are unsigned by definition, and the conversion from signed
to unsigned and back in SerialDevice looked a bit dubious.
2021-05-17 18:15:25 +02:00
Liav A
99eab4667a Kernel: Print scheduler state to the display console 2021-05-16 19:58:33 +02:00
Liav A
20743e8aed Kernel/Graphics + SystemServer: Support text mode properly
As we removed the support of VBE modesetting that was done by GRUB early
on boot, we need to determine if we can modeset the resolution with our
drivers, and if not, we should enable text mode and ensure that
SystemServer knows about it too.

Also, SystemServer should first check if there's a framebuffer device
node, which is an indication that text mode was not even if it was
requested. Then, if it doesn't find it, it should check what boot_mode
argument the user specified (in case it's self-test). This way if we
try to use bochs-display device (which is not VGA compatible) and
request a text mode, it will not honor the request and will continue
with graphical mode.

Also try to print critical messages with mininum memory allocations
possible.

In LibVT, We make the implementation flexible for kernel-specific
methods that are implemented in ConsoleImpl class.
2021-05-16 19:58:33 +02:00
Liav A
6a728e2d76 Kernel: Introduce a new graphics subsystem
This new subsystem is replacing the old code that was used to
create device nodes of framebuffer devices in /dev.

This subsystem includes for now 3 roles:
1. GraphicsManagement singleton object that is used in the boot
process to enumerate and initialize display devices.
2. GraphicsDevice(s) that are used to control the display adapter.
3. FramebufferDevice(s) that are used to control the device node in
/dev.

For now, we support the Bochs display adapter and any other
generic VGA compatible adapter that was configured by the boot
loader to a known and fixed resolution.

Two improvements in the Bochs display adapter code are that
we can support native bochs-display device (this device doesn't
expose any VGA capabilities) and also that we use the MMIO region,
to configure the device, instead of setting IO ports for such tasks.
2021-05-16 19:58:33 +02:00
Liav A
86be477da0 Kernel: Support the bochs-display device
This device is a graphics display device that is not supporting
VGA functionality.
Therefore, it exposes a MMIO region to configure it, so we use that
region to set the framebuffer resolution.
2021-05-16 19:58:33 +02:00
Sahan Fernando
e4f61c6f28 Kernel: Fix return values of BXVGADevice::read/write 2021-05-15 17:43:45 +01:00
Brian Gianforcaro
d76dedb381 Kernel: Fix UHCIController singleton startup null-deref race condition.
The following KUBSAN crash on startup was reported on discord:

```
UHCI: Started
KUBSAN: reference binding to null pointer of type struct UHCIController
KUBSAN: at ../../Kernel/Devices/USB/UHCIController.cpp, line 67
```

After inspecting the code, it became clear that there's a window of time
where the kernel task which monitors the UHCI port can startup and start
executing before the UHCIController constructor completes. This leaves
the singleton pointing to nullptr, thus in the duration of this race
window the "UHCI port proc" thread will go an and de-reference the null
pointer when trying to read for status changes on the UHCI root ports.

Reported-by: @stelar7
Reported-by: @bcoles

Fixes: #6154
2021-05-15 09:46:41 +02:00
Brian Gianforcaro
a324d4d6a3 Kernel: Make AnonymousVMObject physical page APIs OOM safe
AnonymousVMObject::create_with_physical_page(s) can't be NonnullRefPtr
as it allocates internally. Fixing the API then surfaced an issue in
ScatterGatherList, where the code was attempting to create an
AnonymousVMObject in the constructor which will not be observable
during OOM.

Fix all of these issues and start propagating errors at the callers
of the AnonymousVMObject and ScatterGatherList APis.
2021-05-15 09:01:32 +02:00
Andreas Kling
e46343bf9a Kernel: Make UserOrKernelBuffer R/W helpers return KResultOr<size_t>
This makes error propagation less cumbersome (and also exposed some
places where we were not doing it.)
2021-05-13 23:28:40 +02:00
Liav A
f7b5352af0 Kernel/HID: Don't assume that ACPI is initialized 2021-05-08 19:15:54 +02:00
Spencer Dixon
0f89e47a1a
Kernel: Allow remapping Caps Lock to Control (#6883)
We use a global setting to determine if Caps Lock should be remapped to
Control because we don't care how keyboard events come in, just that they
should be massaged into different scan codes.

The `proc` filesystem is able to manipulate this global variable using
the `sysctl` utility like so:

```
# sysctl caps_lock_to_ctrl=1
```
2021-05-05 23:10:56 +02:00
Brian Gianforcaro
4bf9b399f7 Kernel: Remove unused header includes from various files.
Found while browsing code with CLion.
2021-05-03 16:03:17 +02:00
Brian Gianforcaro
65138171f9 Kernel: Mark AsyncBlockDeviceRequest + AnonymousVMObject as final
Mark final to aid in de-virtualization since they are not currently
derived from.
2021-05-03 16:03:17 +02:00
Gunnar Beutner
6cf59b6ae9 Everywhere: Turn #if *_DEBUG into dbgln_if/if constexpr 2021-05-01 21:25:06 +02:00
Idan Horowitz
8293b22361 Kernel: Handle both shift keys being pressed and then released
Our current implementation does not work in the special case in which
both shift keys are pressed, and then only one of the keys is released,
as this would result in writing lower case letters, instead of the
expected upper case letters.

This commit fixes that by keeping track of the amount of shift keys
that are pressed (instead of if any are at all), and only switching to
the unshifted keymap once all of them are released.
2021-05-01 21:08:23 +02:00
Valtteri Koskivuori
370231c05c Kernel: Expose minor device numbers for keyboard and mouse
A fix for two FIXMEs, and paving the way for multi-keyboard/mouse
support, I guess.
2021-05-01 20:07:11 +02:00
Linus Groh
649d2faeab Everywhere: Use "the SerenityOS developers." in copyright headers
We had some inconsistencies before:

- Sometimes "The", sometimes "the"
- Sometimes trailing ".", sometimes no trailing "."

I picked the most common one (lowecase "the", trailing ".") and applied
it to all copyright headers.

By using the exact same string everywhere we can ensure nothing gets
missed during a global search (and replace), and that these
inconsistencies are not spread any further (as copyright headers are
commonly copied to new files).
2021-04-29 00:59:26 +02:00
Linus Groh
dbe72fd962 Everywhere: Remove empty line after function body opening curly brace 2021-04-25 20:20:00 +02:00
Andreas Kling
b91c49364d AK: Rename adopt() to adopt_ref()
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
2021-04-23 16:46:57 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Brian Gianforcaro
ce5a308f24 Kernel: Annotate more AsyncDeviceRequest API's with [[nodiscard]] 2021-04-21 19:31:49 +02:00