mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 17:52:26 -05:00
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.
This commit is contained in:
parent
fba3c77a04
commit
29f9a38f76
Notes:
sideshowbarker
2024-07-18 12:02:51 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/29f9a38f767 Pull-request: https://github.com/SerenityOS/serenity/pull/8129
11 changed files with 74 additions and 18 deletions
|
@ -8,10 +8,16 @@
|
|||
#include <AK/Memory.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <Kernel/Arch/x86/CPU.h>
|
||||
#include <Kernel/Panic.h>
|
||||
#include <LibC/errno_numbers.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<FullDevice> FullDevice::must_create()
|
||||
{
|
||||
return adopt_ref_if_nonnull(new FullDevice).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT FullDevice::FullDevice()
|
||||
: CharacterDevice(1, 7)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
|||
class FullDevice final : public CharacterDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
FullDevice();
|
||||
static NonnullRefPtr<FullDevice> must_create();
|
||||
virtual ~FullDevice() override;
|
||||
|
||||
// ^Device
|
||||
|
@ -21,6 +21,8 @@ public:
|
|||
virtual String device_name() const override { return "full"; }
|
||||
|
||||
private:
|
||||
FullDevice();
|
||||
|
||||
// ^CharacterDevice
|
||||
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
||||
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
|
||||
|
|
|
@ -8,11 +8,17 @@
|
|||
#include <AK/Memory.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <Kernel/Arch/PC/BIOS.h>
|
||||
#include <Kernel/Panic.h>
|
||||
#include <Kernel/VM/AnonymousVMObject.h>
|
||||
#include <Kernel/VM/TypedMapping.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<MemoryDevice> MemoryDevice::must_create()
|
||||
{
|
||||
return adopt_ref_if_nonnull(new MemoryDevice).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT MemoryDevice::MemoryDevice()
|
||||
: CharacterDevice(1, 1)
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Kernel {
|
|||
class MemoryDevice final : public CharacterDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
MemoryDevice();
|
||||
static NonnullRefPtr<MemoryDevice> must_create();
|
||||
~MemoryDevice();
|
||||
|
||||
virtual KResultOr<Region*> mmap(Process&, FileDescription&, const Range&, u64 offset, int prot, bool shared) override;
|
||||
|
@ -26,6 +26,7 @@ public:
|
|||
virtual String device_name() const override { return "mem"; };
|
||||
|
||||
private:
|
||||
MemoryDevice();
|
||||
virtual const char* class_name() const override { return "MemoryDevice"; }
|
||||
virtual bool can_read(const FileDescription&, size_t) const override { return true; }
|
||||
virtual bool can_write(const FileDescription&, size_t) const override { return false; }
|
||||
|
|
|
@ -5,10 +5,16 @@
|
|||
*/
|
||||
|
||||
#include <Kernel/Devices/RandomDevice.h>
|
||||
#include <Kernel/Panic.h>
|
||||
#include <Kernel/Random.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<RandomDevice> RandomDevice::must_create()
|
||||
{
|
||||
return adopt_ref_if_nonnull(new RandomDevice).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
|
||||
: CharacterDevice(1, 8)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
|||
class RandomDevice final : public CharacterDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
RandomDevice();
|
||||
static NonnullRefPtr<RandomDevice> must_create();
|
||||
virtual ~RandomDevice() override;
|
||||
|
||||
// ^Device
|
||||
|
@ -21,6 +21,8 @@ public:
|
|||
virtual String device_name() const override { return "random"; }
|
||||
|
||||
private:
|
||||
RandomDevice();
|
||||
|
||||
// ^CharacterDevice
|
||||
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
||||
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
|
||||
|
|
|
@ -7,9 +7,37 @@
|
|||
|
||||
#include <Kernel/Devices/SerialDevice.h>
|
||||
#include <Kernel/IO.h>
|
||||
#include <Kernel/Panic.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
#define SERIAL_COM1_ADDR 0x3F8
|
||||
#define SERIAL_COM2_ADDR 0x2F8
|
||||
#define SERIAL_COM3_ADDR 0x3E8
|
||||
#define SERIAL_COM4_ADDR 0x2E8
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<SerialDevice> SerialDevice::must_create(size_t com_number)
|
||||
{
|
||||
SerialDevice* device = nullptr;
|
||||
switch (com_number) {
|
||||
case 0:
|
||||
device = new SerialDevice(IOAddress(SERIAL_COM1_ADDR), 64);
|
||||
break;
|
||||
case 1:
|
||||
device = new SerialDevice(IOAddress(SERIAL_COM2_ADDR), 65);
|
||||
break;
|
||||
case 2:
|
||||
device = new SerialDevice(IOAddress(SERIAL_COM3_ADDR), 66);
|
||||
break;
|
||||
case 3:
|
||||
device = new SerialDevice(IOAddress(SERIAL_COM4_ADDR), 67);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return adopt_ref_if_nonnull(device).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT SerialDevice::SerialDevice(IOAddress base_addr, unsigned minor)
|
||||
: CharacterDevice(4, minor)
|
||||
, m_base_addr(base_addr)
|
||||
|
|
|
@ -11,15 +11,11 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
#define SERIAL_COM1_ADDR 0x3F8
|
||||
#define SERIAL_COM2_ADDR 0x2F8
|
||||
#define SERIAL_COM3_ADDR 0x3E8
|
||||
#define SERIAL_COM4_ADDR 0x2E8
|
||||
|
||||
class SerialDevice final : public CharacterDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
SerialDevice(IOAddress base_addr, unsigned minor);
|
||||
static NonnullRefPtr<SerialDevice> must_create(size_t com_number);
|
||||
|
||||
virtual ~SerialDevice() override;
|
||||
|
||||
// ^CharacterDevice
|
||||
|
@ -113,6 +109,8 @@ public:
|
|||
private:
|
||||
friend class PCISerialDevice;
|
||||
|
||||
SerialDevice(IOAddress base_addr, unsigned minor);
|
||||
|
||||
// ^CharacterDevice
|
||||
virtual const char* class_name() const override { return "SerialDevice"; }
|
||||
|
||||
|
|
|
@ -7,9 +7,15 @@
|
|||
#include "ZeroDevice.h"
|
||||
#include <AK/Memory.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <Kernel/Panic.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
UNMAP_AFTER_INIT NonnullRefPtr<ZeroDevice> ZeroDevice::must_create()
|
||||
{
|
||||
return adopt_ref_if_nonnull(new ZeroDevice).release_nonnull();
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT ZeroDevice::ZeroDevice()
|
||||
: CharacterDevice(1, 5)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
|||
class ZeroDevice final : public CharacterDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
ZeroDevice();
|
||||
static NonnullRefPtr<ZeroDevice> must_create();
|
||||
virtual ~ZeroDevice() override;
|
||||
|
||||
// ^Device
|
||||
|
@ -21,6 +21,7 @@ public:
|
|||
virtual String device_name() const override { return "zero"; }
|
||||
|
||||
private:
|
||||
ZeroDevice();
|
||||
// ^CharacterDevice
|
||||
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
||||
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
|
||||
|
|
|
@ -158,10 +158,10 @@ extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
|
|||
|
||||
NullDevice::initialize();
|
||||
if (!get_serial_debug())
|
||||
new SerialDevice(IOAddress(SERIAL_COM1_ADDR), 64);
|
||||
new SerialDevice(IOAddress(SERIAL_COM2_ADDR), 65);
|
||||
new SerialDevice(IOAddress(SERIAL_COM3_ADDR), 66);
|
||||
new SerialDevice(IOAddress(SERIAL_COM4_ADDR), 67);
|
||||
(void)SerialDevice::must_create(0).leak_ref();
|
||||
(void)SerialDevice::must_create(1).leak_ref();
|
||||
(void)SerialDevice::must_create(2).leak_ref();
|
||||
(void)SerialDevice::must_create(3).leak_ref();
|
||||
|
||||
VMWareBackdoor::the(); // don't wait until first mouse packet
|
||||
HIDManagement::initialize();
|
||||
|
@ -243,10 +243,10 @@ void init_stage2(void*)
|
|||
NetworkingManagement::the().initialize();
|
||||
Syscall::initialize();
|
||||
|
||||
new MemoryDevice;
|
||||
new ZeroDevice;
|
||||
new FullDevice;
|
||||
new RandomDevice;
|
||||
(void)MemoryDevice::must_create().leak_ref();
|
||||
(void)ZeroDevice::must_create().leak_ref();
|
||||
(void)FullDevice::must_create().leak_ref();
|
||||
(void)RandomDevice::must_create().leak_ref();
|
||||
PTYMultiplexer::initialize();
|
||||
SB16::detect();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue