Skip to content

Commit

Permalink
Kernel: Invoke heap constructors separately early on
Browse files Browse the repository at this point in the history
By having a separate list of constructors for the kernel heap
code, we can properly use constructors without re-running them
after the heap was already initialized. This solves some problems
where values were wiped out because they were overwritten by
running their constructors later in the initialization process.
  • Loading branch information
tomuta authored and awesomekling committed Aug 10, 2020
1 parent de5e542 commit 08ff25f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
12 changes: 8 additions & 4 deletions Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
set(KERNEL_HEAP_SOURCES
Heap/SlabAllocator.cpp
Heap/kmalloc.cpp
)

set(KERNEL_SOURCES
ACPI/DynamicParser.cpp
ACPI/Initialize.cpp
Expand Down Expand Up @@ -47,8 +52,6 @@ set(KERNEL_SOURCES
FileSystem/ProcFS.cpp
FileSystem/TmpFS.cpp
FileSystem/VirtualFileSystem.cpp
Heap/SlabAllocator.cpp
Heap/kmalloc.cpp
Interrupts/APIC.cpp
Interrupts/GenericInterruptHandler.cpp
Interrupts/IOAPIC.cpp
Expand Down Expand Up @@ -244,6 +247,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib -nostdinc -nostdinc++")
add_link_options(LINKER:-T ${CMAKE_CURRENT_BINARY_DIR}/linker.ld -nostdlib)

add_library(boot OBJECT Arch/i386/Boot/boot.S)
add_library(kernel_heap STATIC ${KERNEL_HEAP_SOURCES})
file(GENERATE OUTPUT linker.ld INPUT linker.ld)

if (${CMAKE_HOST_SYSTEM_NAME} MATCHES SerenityOS)
Expand All @@ -255,8 +259,8 @@ else()
endif()

add_executable(Kernel ${SOURCES})
target_link_libraries(Kernel gcc stdc++)
add_dependencies(Kernel boot)
target_link_libraries(Kernel kernel_heap gcc stdc++)
add_dependencies(Kernel boot kernel_heap)
install(TARGETS Kernel RUNTIME DESTINATION boot)

add_custom_command(
Expand Down
7 changes: 3 additions & 4 deletions Kernel/Heap/SlabAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ class SlabAllocator {
char padding[templated_slab_size - sizeof(FreeSlab*)];
};

// NOTE: These are not default-initialized to prevent an init-time constructor from overwriting them
FreeSlab* m_freelist;
FreeSlab* m_freelist { nullptr };
Atomic<size_t> m_num_allocated;
Atomic<size_t> m_num_free;
void* m_base;
void* m_end;
void* m_base { nullptr };
void* m_end { nullptr };
SpinLock<u32> m_lock;

static_assert(sizeof(FreeSlab) == templated_slab_size);
Expand Down
5 changes: 5 additions & 0 deletions Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

// Defined in the linker script
typedef void (*ctor_func_t)();
extern ctor_func_t start_heap_ctors;
extern ctor_func_t end_heap_ctors;
extern ctor_func_t start_ctors;
extern ctor_func_t end_ctors;

Expand Down Expand Up @@ -107,6 +109,9 @@ extern "C" [[noreturn]] void init()

s_bsp_processor.early_initialize(0);

// Invoke the constructors needed for the kernel heap
for (ctor_func_t* ctor = &start_heap_ctors; ctor < &end_heap_ctors; ctor++)
(*ctor)();
kmalloc_init();
slab_alloc_init();

Expand Down
4 changes: 4 additions & 0 deletions Kernel/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ SECTIONS

.rodata ALIGN(4K) : AT (ADDR(.rodata) - 0xc0000000)
{
start_heap_ctors = .;
*libkernel_heap.a:*(.ctors)
end_heap_ctors = .;

start_ctors = .;
*(.ctors)
end_ctors = .;
Expand Down

0 comments on commit 08ff25f

Please sign in to comment.