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:
Liav A 2021-06-18 11:37:26 +03:00 committed by Andreas Kling
parent fba3c77a04
commit 29f9a38f76
Notes: sideshowbarker 2024-07-18 12:02:51 +09:00
11 changed files with 74 additions and 18 deletions

View file

@ -8,10 +8,16 @@
#include <AK/Memory.h> #include <AK/Memory.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <Kernel/Arch/x86/CPU.h> #include <Kernel/Arch/x86/CPU.h>
#include <Kernel/Panic.h>
#include <LibC/errno_numbers.h> #include <LibC/errno_numbers.h>
namespace Kernel { namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<FullDevice> FullDevice::must_create()
{
return adopt_ref_if_nonnull(new FullDevice).release_nonnull();
}
UNMAP_AFTER_INIT FullDevice::FullDevice() UNMAP_AFTER_INIT FullDevice::FullDevice()
: CharacterDevice(1, 7) : CharacterDevice(1, 7)
{ {

View file

@ -13,7 +13,7 @@ namespace Kernel {
class FullDevice final : public CharacterDevice { class FullDevice final : public CharacterDevice {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
FullDevice(); static NonnullRefPtr<FullDevice> must_create();
virtual ~FullDevice() override; virtual ~FullDevice() override;
// ^Device // ^Device
@ -21,6 +21,8 @@ public:
virtual String device_name() const override { return "full"; } virtual String device_name() const override { return "full"; }
private: private:
FullDevice();
// ^CharacterDevice // ^CharacterDevice
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -8,11 +8,17 @@
#include <AK/Memory.h> #include <AK/Memory.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <Kernel/Arch/PC/BIOS.h> #include <Kernel/Arch/PC/BIOS.h>
#include <Kernel/Panic.h>
#include <Kernel/VM/AnonymousVMObject.h> #include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/TypedMapping.h> #include <Kernel/VM/TypedMapping.h>
namespace Kernel { namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<MemoryDevice> MemoryDevice::must_create()
{
return adopt_ref_if_nonnull(new MemoryDevice).release_nonnull();
}
UNMAP_AFTER_INIT MemoryDevice::MemoryDevice() UNMAP_AFTER_INIT MemoryDevice::MemoryDevice()
: CharacterDevice(1, 1) : CharacterDevice(1, 1)
{ {

View file

@ -16,7 +16,7 @@ namespace Kernel {
class MemoryDevice final : public CharacterDevice { class MemoryDevice final : public CharacterDevice {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
MemoryDevice(); static NonnullRefPtr<MemoryDevice> must_create();
~MemoryDevice(); ~MemoryDevice();
virtual KResultOr<Region*> mmap(Process&, FileDescription&, const Range&, u64 offset, int prot, bool shared) override; 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"; }; virtual String device_name() const override { return "mem"; };
private: private:
MemoryDevice();
virtual const char* class_name() const override { return "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_read(const FileDescription&, size_t) const override { return true; }
virtual bool can_write(const FileDescription&, size_t) const override { return false; } virtual bool can_write(const FileDescription&, size_t) const override { return false; }

View file

@ -5,10 +5,16 @@
*/ */
#include <Kernel/Devices/RandomDevice.h> #include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Panic.h>
#include <Kernel/Random.h> #include <Kernel/Random.h>
namespace Kernel { namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<RandomDevice> RandomDevice::must_create()
{
return adopt_ref_if_nonnull(new RandomDevice).release_nonnull();
}
UNMAP_AFTER_INIT RandomDevice::RandomDevice() UNMAP_AFTER_INIT RandomDevice::RandomDevice()
: CharacterDevice(1, 8) : CharacterDevice(1, 8)
{ {

View file

@ -13,7 +13,7 @@ namespace Kernel {
class RandomDevice final : public CharacterDevice { class RandomDevice final : public CharacterDevice {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
RandomDevice(); static NonnullRefPtr<RandomDevice> must_create();
virtual ~RandomDevice() override; virtual ~RandomDevice() override;
// ^Device // ^Device
@ -21,6 +21,8 @@ public:
virtual String device_name() const override { return "random"; } virtual String device_name() const override { return "random"; }
private: private:
RandomDevice();
// ^CharacterDevice // ^CharacterDevice
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -7,9 +7,37 @@
#include <Kernel/Devices/SerialDevice.h> #include <Kernel/Devices/SerialDevice.h>
#include <Kernel/IO.h> #include <Kernel/IO.h>
#include <Kernel/Panic.h>
namespace Kernel { 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) UNMAP_AFTER_INIT SerialDevice::SerialDevice(IOAddress base_addr, unsigned minor)
: CharacterDevice(4, minor) : CharacterDevice(4, minor)
, m_base_addr(base_addr) , m_base_addr(base_addr)

View file

@ -11,15 +11,11 @@
namespace Kernel { 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 { class SerialDevice final : public CharacterDevice {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
SerialDevice(IOAddress base_addr, unsigned minor); static NonnullRefPtr<SerialDevice> must_create(size_t com_number);
virtual ~SerialDevice() override; virtual ~SerialDevice() override;
// ^CharacterDevice // ^CharacterDevice
@ -113,6 +109,8 @@ public:
private: private:
friend class PCISerialDevice; friend class PCISerialDevice;
SerialDevice(IOAddress base_addr, unsigned minor);
// ^CharacterDevice // ^CharacterDevice
virtual const char* class_name() const override { return "SerialDevice"; } virtual const char* class_name() const override { return "SerialDevice"; }

View file

@ -7,9 +7,15 @@
#include "ZeroDevice.h" #include "ZeroDevice.h"
#include <AK/Memory.h> #include <AK/Memory.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <Kernel/Panic.h>
namespace Kernel { namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<ZeroDevice> ZeroDevice::must_create()
{
return adopt_ref_if_nonnull(new ZeroDevice).release_nonnull();
}
UNMAP_AFTER_INIT ZeroDevice::ZeroDevice() UNMAP_AFTER_INIT ZeroDevice::ZeroDevice()
: CharacterDevice(1, 5) : CharacterDevice(1, 5)
{ {

View file

@ -13,7 +13,7 @@ namespace Kernel {
class ZeroDevice final : public CharacterDevice { class ZeroDevice final : public CharacterDevice {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
ZeroDevice(); static NonnullRefPtr<ZeroDevice> must_create();
virtual ~ZeroDevice() override; virtual ~ZeroDevice() override;
// ^Device // ^Device
@ -21,6 +21,7 @@ public:
virtual String device_name() const override { return "zero"; } virtual String device_name() const override { return "zero"; }
private: private:
ZeroDevice();
// ^CharacterDevice // ^CharacterDevice
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -158,10 +158,10 @@ extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
NullDevice::initialize(); NullDevice::initialize();
if (!get_serial_debug()) if (!get_serial_debug())
new SerialDevice(IOAddress(SERIAL_COM1_ADDR), 64); (void)SerialDevice::must_create(0).leak_ref();
new SerialDevice(IOAddress(SERIAL_COM2_ADDR), 65); (void)SerialDevice::must_create(1).leak_ref();
new SerialDevice(IOAddress(SERIAL_COM3_ADDR), 66); (void)SerialDevice::must_create(2).leak_ref();
new SerialDevice(IOAddress(SERIAL_COM4_ADDR), 67); (void)SerialDevice::must_create(3).leak_ref();
VMWareBackdoor::the(); // don't wait until first mouse packet VMWareBackdoor::the(); // don't wait until first mouse packet
HIDManagement::initialize(); HIDManagement::initialize();
@ -243,10 +243,10 @@ void init_stage2(void*)
NetworkingManagement::the().initialize(); NetworkingManagement::the().initialize();
Syscall::initialize(); Syscall::initialize();
new MemoryDevice; (void)MemoryDevice::must_create().leak_ref();
new ZeroDevice; (void)ZeroDevice::must_create().leak_ref();
new FullDevice; (void)FullDevice::must_create().leak_ref();
new RandomDevice; (void)RandomDevice::must_create().leak_ref();
PTYMultiplexer::initialize(); PTYMultiplexer::initialize();
SB16::detect(); SB16::detect();