Kernel: Slap UNMAP_AFTER_INIT on a whole bunch of functions

There's no real system here, I just added it to various functions
that I don't believe we ever want to call after initialization
has finished.

With these changes, we're able to unmap 60 KiB of kernel text
after init. :^)
This commit is contained in:
Andreas Kling 2021-02-19 18:41:50 +01:00
parent 32e93c8808
commit fdf03852c9
Notes: sideshowbarker 2024-07-18 22:08:51 +09:00
22 changed files with 69 additions and 68 deletions

View file

@ -454,24 +454,24 @@ void unregister_generic_interrupt_handler(u8 interrupt_number, GenericInterruptH
ASSERT_NOT_REACHED();
}
void register_interrupt_handler(u8 index, void (*f)())
UNMAP_AFTER_INIT void register_interrupt_handler(u8 index, void (*f)())
{
s_idt[index].low = 0x00080000 | LSW((f));
s_idt[index].high = ((u32)(f)&0xffff0000) | 0x8e00;
}
void register_user_callable_interrupt_handler(u8 index, void (*f)())
UNMAP_AFTER_INIT void register_user_callable_interrupt_handler(u8 index, void (*f)())
{
s_idt[index].low = 0x00080000 | LSW((f));
s_idt[index].high = ((u32)(f)&0xffff0000) | 0xef00;
}
void flush_idt()
UNMAP_AFTER_INIT void flush_idt()
{
asm("lidt %0" ::"m"(s_idtr));
}
static void idt_init()
UNMAP_AFTER_INIT static void idt_init()
{
s_idtr.address = s_idt;
s_idtr.limit = 256 * 8 - 1;
@ -815,7 +815,7 @@ Processor& Processor::by_id(u32 cpu)
}
}
void Processor::cpu_detect()
UNMAP_AFTER_INIT void Processor::cpu_detect()
{
// NOTE: This is called during Processor::early_initialize, we cannot
// safely log at this point because we don't have kmalloc
@ -900,7 +900,7 @@ void Processor::cpu_detect()
set_feature(CPUFeature::RDSEED);
}
void Processor::cpu_setup()
UNMAP_AFTER_INIT void Processor::cpu_setup()
{
// NOTE: This is called during Processor::early_initialize, we cannot
// safely log at this point because we don't have kmalloc
@ -1013,7 +1013,7 @@ String Processor::features_string() const
return builder.build();
}
void Processor::early_initialize(u32 cpu)
UNMAP_AFTER_INIT void Processor::early_initialize(u32 cpu)
{
m_self = this;
@ -1048,7 +1048,7 @@ void Processor::early_initialize(u32 cpu)
ASSERT(&current() == this); // sanity check
}
void Processor::initialize(u32 cpu)
UNMAP_AFTER_INIT void Processor::initialize(u32 cpu)
{
ASSERT(m_self == this);
ASSERT(&current() == this); // sanity check
@ -1774,7 +1774,7 @@ u32 Processor::smp_wake_n_idle_processors(u32 wake_count)
return did_wake_count;
}
void Processor::smp_enable()
UNMAP_AFTER_INIT void Processor::smp_enable()
{
size_t msg_pool_size = Processor::count() * 100u;
size_t msg_entries_cnt = Processor::count();
@ -2167,7 +2167,7 @@ void Processor::deferred_call_queue(void (*callback)(void*), void* data, void (*
cur_proc.deferred_call_queue_entry(entry);
}
void Processor::gdt_init()
UNMAP_AFTER_INIT void Processor::gdt_init()
{
m_gdt_length = 0;
m_gdtr.address = nullptr;

View file

@ -36,7 +36,7 @@
static AK::Singleton<Console> s_the;
static Kernel::SpinLock g_console_lock;
void Console::initialize()
UNMAP_AFTER_INIT void Console::initialize()
{
s_the.ensure_instance();
}
@ -51,7 +51,7 @@ bool Console::is_initialized()
return s_the.is_initialized();
}
Console::Console()
UNMAP_AFTER_INIT Console::Console()
: CharacterDevice(5, 1)
{
}

View file

@ -32,12 +32,12 @@
namespace Kernel {
FullDevice::FullDevice()
UNMAP_AFTER_INIT FullDevice::FullDevice()
: CharacterDevice(1, 7)
{
}
FullDevice::~FullDevice()
UNMAP_AFTER_INIT FullDevice::~FullDevice()
{
}

View file

@ -33,7 +33,7 @@ namespace Kernel {
static I8042Controller* s_the;
void I8042Controller::initialize()
UNMAP_AFTER_INIT void I8042Controller::initialize()
{
if (ACPI::Parser::the()->have_8042())
new I8042Controller;
@ -45,7 +45,7 @@ I8042Controller& I8042Controller::the()
return *s_the;
}
I8042Controller::I8042Controller()
UNMAP_AFTER_INIT I8042Controller::I8042Controller()
{
ASSERT(!s_the);
s_the = this;

View file

@ -42,12 +42,12 @@ NullDevice& NullDevice::the()
return *s_the;
}
NullDevice::NullDevice()
UNMAP_AFTER_INIT NullDevice::NullDevice()
: CharacterDevice(1, 3)
{
}
NullDevice::~NullDevice()
UNMAP_AFTER_INIT NullDevice::~NullDevice()
{
}

View file

@ -29,12 +29,12 @@
namespace Kernel {
RandomDevice::RandomDevice()
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
: CharacterDevice(1, 8)
{
}
RandomDevice::~RandomDevice()
UNMAP_AFTER_INIT RandomDevice::~RandomDevice()
{
}

View file

@ -30,12 +30,12 @@
namespace Kernel {
ZeroDevice::ZeroDevice()
UNMAP_AFTER_INIT ZeroDevice::ZeroDevice()
: CharacterDevice(1, 5)
{
}
ZeroDevice::~ZeroDevice()
UNMAP_AFTER_INIT ZeroDevice::~ZeroDevice()
{
}

View file

@ -44,7 +44,7 @@ static AK::Singleton<VFS> s_the;
static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;
void VFS::initialize()
UNMAP_AFTER_INIT void VFS::initialize()
{
s_the.ensure_instance();
}
@ -54,14 +54,14 @@ VFS& VFS::the()
return *s_the;
}
VFS::VFS()
UNMAP_AFTER_INIT VFS::VFS()
{
#if VFS_DEBUG
klog() << "VFS: Constructing VFS";
#endif
}
VFS::~VFS()
UNMAP_AFTER_INIT VFS::~VFS()
{
}

View file

@ -141,7 +141,7 @@ void for_each_allocator(Callback callback)
callback(s_slab_allocator_128);
}
void slab_alloc_init()
UNMAP_AFTER_INIT void slab_alloc_init()
{
s_slab_allocator_16.init(128 * KiB);
s_slab_allocator_32.init(128 * KiB);

View file

@ -211,7 +211,7 @@ void kmalloc_enable_expand()
g_kmalloc_global->allocate_backup_memory();
}
void kmalloc_init()
UNMAP_AFTER_INIT void kmalloc_init()
{
// Zero out heap since it's placed after end_of_kernel_bss.
memset(kmalloc_eternal_heap, 0, sizeof(kmalloc_eternal_heap));

View file

@ -146,7 +146,7 @@ APIC& APIC::the()
return *s_apic;
}
void APIC::initialize()
UNMAP_AFTER_INIT void APIC::initialize()
{
ASSERT(!APIC::initialized());
s_apic.ensure_instance();
@ -234,7 +234,7 @@ u8 APIC::spurious_interrupt_vector()
+ reinterpret_cast<ptrdiff_t>(&varname) \
- reinterpret_cast<ptrdiff_t>(&apic_ap_start))
bool APIC::init_bsp()
UNMAP_AFTER_INIT bool APIC::init_bsp()
{
// FIXME: Use the ACPI MADT table
if (!MSR::have())
@ -300,7 +300,7 @@ bool APIC::init_bsp()
return true;
}
void APIC::do_boot_aps()
UNMAP_AFTER_INIT void APIC::do_boot_aps()
{
ASSERT(m_processor_enabled_cnt > 1);
u32 aps_to_enable = m_processor_enabled_cnt - 1;
@ -400,7 +400,7 @@ void APIC::do_boot_aps()
#endif
}
void APIC::boot_aps()
UNMAP_AFTER_INIT void APIC::boot_aps()
{
if (m_processor_enabled_cnt <= 1)
return;
@ -421,7 +421,7 @@ void APIC::boot_aps()
m_apic_ap_continue.store(1, AK::MemoryOrder::memory_order_release);
}
void APIC::enable(u32 cpu)
UNMAP_AFTER_INIT void APIC::enable(u32 cpu)
{
if (cpu >= 8) {
// TODO: x2apic support?
@ -472,7 +472,7 @@ Thread* APIC::get_idle_thread(u32 cpu) const
return m_ap_idle_threads[cpu - 1];
}
void APIC::init_finished(u32 cpu)
UNMAP_AFTER_INIT void APIC::init_finished(u32 cpu)
{
// This method is called once the boot stack is no longer needed
ASSERT(cpu > 0);
@ -525,7 +525,7 @@ void APIC::send_ipi(u32 cpu)
write_icr(ICRReg(IRQ_APIC_IPI + IRQ_VECTOR_BASE, ICRReg::Fixed, ICRReg::Logical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::NoShorthand, cpu));
}
APICTimer* APIC::initialize_timers(HardwareTimerBase& calibration_timer)
UNMAP_AFTER_INIT APICTimer* APIC::initialize_timers(HardwareTimerBase& calibration_timer)
{
if (!m_apic_base)
return nullptr;

View file

@ -56,7 +56,7 @@ InterruptManagement& InterruptManagement::the()
return *s_interrupt_management;
}
void InterruptManagement::initialize()
UNMAP_AFTER_INIT void InterruptManagement::initialize()
{
ASSERT(!InterruptManagement::initialized());
s_interrupt_management = new InterruptManagement();
@ -125,7 +125,7 @@ RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 int
ASSERT_NOT_REACHED();
}
PhysicalAddress InterruptManagement::search_for_madt()
UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt()
{
dbgln("Early access to ACPI tables for interrupt setup");
auto rsdp = ACPI::StaticParsing::find_rsdp();
@ -134,13 +134,13 @@ PhysicalAddress InterruptManagement::search_for_madt()
return ACPI::StaticParsing::find_table(rsdp.value(), "APIC");
}
InterruptManagement::InterruptManagement()
UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
: m_madt(search_for_madt())
{
m_interrupt_controllers.resize(1);
}
void InterruptManagement::switch_to_pic_mode()
UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
{
klog() << "Interrupts: Switch to Legacy PIC mode";
InterruptDisabler disabler;
@ -159,7 +159,7 @@ void InterruptManagement::switch_to_pic_mode()
}
}
void InterruptManagement::switch_to_ioapic_mode()
UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
{
klog() << "Interrupts: Switch to IOAPIC mode";
InterruptDisabler disabler;
@ -196,7 +196,7 @@ void InterruptManagement::switch_to_ioapic_mode()
APIC::the().init_bsp();
}
void InterruptManagement::locate_apic_data()
UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
{
ASSERT(!m_madt.is_null());
auto madt = map_typed<ACPI::Structures::MADT>(m_madt);

View file

@ -78,7 +78,7 @@ ProcessID Process::allocate_pid()
return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
}
void Process::initialize()
UNMAP_AFTER_INIT void Process::initialize()
{
g_modules = new HashMap<String, OwnPtr<Module>>;

View file

@ -168,7 +168,7 @@ void Scheduler::queue_runnable_thread(Thread& thread)
g_ready_queues_mask |= (1u << priority);
}
void Scheduler::start()
UNMAP_AFTER_INIT void Scheduler::start()
{
ASSERT_INTERRUPTS_DISABLED();
@ -488,7 +488,7 @@ Process* Scheduler::colonel()
return s_colonel_process;
}
void Scheduler::initialize()
UNMAP_AFTER_INIT void Scheduler::initialize()
{
ASSERT(&Processor::current() != nullptr); // sanity check

View file

@ -42,7 +42,7 @@ PTYMultiplexer& PTYMultiplexer::the()
return *s_the;
}
PTYMultiplexer::PTYMultiplexer()
UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer()
: CharacterDevice(5, 2)
{
m_freelist.ensure_capacity(s_max_pty_pairs);
@ -50,7 +50,7 @@ PTYMultiplexer::PTYMultiplexer()
m_freelist.unchecked_append(i - 1);
}
PTYMultiplexer::~PTYMultiplexer()
UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
{
}

View file

@ -49,7 +49,7 @@ void VirtualConsole::flush_vga_cursor()
IO::out8(0x3d5, LSB(value));
}
void VirtualConsole::initialize()
UNMAP_AFTER_INIT void VirtualConsole::initialize()
{
s_vga_buffer = (u8*)0xc00b8000;
s_active_console = -1;
@ -63,7 +63,7 @@ void VirtualConsole::set_graphical(bool graphical)
m_graphical = graphical;
}
VirtualConsole::VirtualConsole(const unsigned index)
UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
: TTY(4, index)
, m_index(index)
, m_terminal(*this)
@ -76,7 +76,7 @@ VirtualConsole::VirtualConsole(const unsigned index)
s_consoles[index] = this;
}
VirtualConsole::~VirtualConsole()
UNMAP_AFTER_INIT VirtualConsole::~VirtualConsole()
{
ASSERT_NOT_REACHED();
}

View file

@ -48,7 +48,7 @@ namespace Kernel {
SpinLock<u8> Thread::g_tid_map_lock;
READONLY_AFTER_INIT HashMap<ThreadID, Thread*>* Thread::g_tid_map;
void Thread::initialize()
UNMAP_AFTER_INIT void Thread::initialize()
{
g_tid_map = new HashMap<ThreadID, Thread*>();
}

View file

@ -36,7 +36,7 @@ namespace Kernel {
#define APIC_TIMER_MEASURE_CPU_CLOCK
APICTimer* APICTimer::initialize(u8 interrupt_number, HardwareTimerBase& calibration_source)
UNMAP_AFTER_INIT APICTimer* APICTimer::initialize(u8 interrupt_number, HardwareTimerBase& calibration_source)
{
auto* timer = new APICTimer(interrupt_number, nullptr);
if (!timer->calibrate(calibration_source)) {
@ -46,13 +46,13 @@ APICTimer* APICTimer::initialize(u8 interrupt_number, HardwareTimerBase& calibra
return timer;
}
APICTimer::APICTimer(u8 interrupt_number, Function<void(const RegisterState&)> callback)
UNMAP_AFTER_INIT APICTimer::APICTimer(u8 interrupt_number, Function<void(const RegisterState&)> callback)
: HardwareTimer<GenericInterruptHandler>(interrupt_number, move(callback))
{
disable_remap();
}
bool APICTimer::calibrate(HardwareTimerBase& calibration_source)
UNMAP_AFTER_INIT bool APICTimer::calibrate(HardwareTimerBase& calibration_source)
{
ASSERT_INTERRUPTS_DISABLED();

View file

@ -145,7 +145,7 @@ u64 TimeManagement::uptime_ms() const
return ms;
}
void TimeManagement::initialize(u32 cpu)
UNMAP_AFTER_INIT void TimeManagement::initialize(u32 cpu)
{
if (cpu == 0) {
ASSERT(!s_the.is_initialized());
@ -187,7 +187,7 @@ time_t TimeManagement::boot_time() const
return RTC::boot_time();
}
TimeManagement::TimeManagement()
UNMAP_AFTER_INIT TimeManagement::TimeManagement()
{
bool probe_non_legacy_hardware_timers = !(kernel_command_line().lookup("time").value_or("modern") == "legacy");
if (ACPI::is_enabled()) {
@ -255,7 +255,7 @@ bool TimeManagement::is_hpet_periodic_mode_allowed()
ASSERT_NOT_REACHED();
}
bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
UNMAP_AFTER_INIT bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
{
if (!ACPI::is_enabled())
return false;
@ -309,7 +309,7 @@ bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
return true;
}
bool TimeManagement::probe_and_set_legacy_hardware_timers()
UNMAP_AFTER_INIT bool TimeManagement::probe_and_set_legacy_hardware_timers()
{
if (ACPI::is_enabled()) {
if (ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {

View file

@ -81,7 +81,7 @@ bool MemoryManager::is_initialized()
return s_the != nullptr;
}
MemoryManager::MemoryManager()
UNMAP_AFTER_INIT MemoryManager::MemoryManager()
{
ScopedSpinLock lock(s_mm_lock);
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
@ -104,11 +104,11 @@ MemoryManager::MemoryManager()
m_lazy_committed_page = allocate_committed_user_physical_page();
}
MemoryManager::~MemoryManager()
UNMAP_AFTER_INIT MemoryManager::~MemoryManager()
{
}
void MemoryManager::protect_kernel_image()
UNMAP_AFTER_INIT void MemoryManager::protect_kernel_image()
{
ScopedSpinLock page_lock(kernel_page_directory().get_lock());
// Disable writing to the kernel text and rodata segments.
@ -125,7 +125,7 @@ void MemoryManager::protect_kernel_image()
}
}
void MemoryManager::protect_readonly_after_init_memory()
UNMAP_AFTER_INIT void MemoryManager::protect_readonly_after_init_memory()
{
ScopedSpinLock mm_lock(s_mm_lock);
ScopedSpinLock page_lock(kernel_page_directory().get_lock());
@ -153,9 +153,10 @@ void MemoryManager::unmap_memory_after_init()
}
dmesgln("Unmapped {} KiB of kernel text after init! :^)", (end - start) / KiB);
//Processor::halt();
}
void MemoryManager::register_reserved_ranges()
UNMAP_AFTER_INIT void MemoryManager::register_reserved_ranges()
{
ASSERT(!m_physical_memory_ranges.is_empty());
ContiguousReservedMemoryRange range;
@ -194,7 +195,7 @@ bool MemoryManager::is_allowed_to_mmap_to_userspace(PhysicalAddress start_addres
return false;
}
void MemoryManager::parse_memory_map()
UNMAP_AFTER_INIT void MemoryManager::parse_memory_map()
{
RefPtr<PhysicalRegion> physical_region;
@ -414,7 +415,7 @@ void MemoryManager::release_pte(PageDirectory& page_directory, VirtualAddress va
}
}
void MemoryManager::initialize(u32 cpu)
UNMAP_AFTER_INIT void MemoryManager::initialize(u32 cpu)
{
auto mm_data = new MemoryManagerData;
Processor::current().set_mm_data(*mm_data);

View file

@ -54,7 +54,7 @@ extern "C" PageDirectoryEntry* boot_pdpt[4];
extern "C" PageDirectoryEntry boot_pd0[1024];
extern "C" PageDirectoryEntry boot_pd3[1024];
PageDirectory::PageDirectory()
UNMAP_AFTER_INIT PageDirectory::PageDirectory()
{
m_range_allocator.initialize_with_range(VirtualAddress(0xc0800000), 0x3f000000);
m_identity_range_allocator.initialize_with_range(VirtualAddress(FlatPtr(0x00000000)), 0x00200000);

View file

@ -116,7 +116,7 @@ static Processor s_bsp_processor; // global but let's keep it "private"
// Once multi-tasking is ready, we spawn a new thread that starts in the
// init_stage2() function. Initialization continues there.
extern "C" [[noreturn]] void init()
extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
{
setup_serial_debug();
@ -196,7 +196,7 @@ extern "C" [[noreturn]] void init()
//
// The purpose of init_ap() is to initialize APs for multi-tasking.
//
extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
extern "C" UNMAP_AFTER_INIT [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
{
processor_info->early_initialize(cpu);
@ -213,7 +213,7 @@ extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
// This method is called once a CPU enters the scheduler and its idle thread
// At this point the initial boot stack can be freed
//
extern "C" void init_finished(u32 cpu)
extern "C" UNMAP_AFTER_INIT void init_finished(u32 cpu)
{
if (cpu == 0) {
// TODO: we can reuse the boot stack, maybe for kmalloc()?
@ -323,7 +323,7 @@ void init_stage2(void*)
ASSERT_NOT_REACHED();
}
void setup_serial_debug()
UNMAP_AFTER_INIT void setup_serial_debug()
{
// serial_debug will output all the klog() and dbgln() data to COM1 at
// 8-N-1 57600 baud. this is particularly useful for debugging the boot