mirror of
https://github.com/usatiuk/ficus.git
synced 2025-10-28 16:17:51 +01:00
limine module loading as user tasks!
This commit is contained in:
2
run.sh
2
run.sh
@@ -69,5 +69,5 @@ fi
|
||||
|
||||
cmake --build cmake-build-$MODE --target iso
|
||||
|
||||
qemu-system-x86_64 -s $QEMU_OPTS -cdrom cmake-build-relwithdebinfo/src/iso/os2.iso
|
||||
qemu-system-x86_64 -s $QEMU_OPTS -cdrom cmake-build-$MODE/src/iso/os2.iso
|
||||
|
||||
|
||||
@@ -6,5 +6,8 @@ target_compile_options(kernel.elf PUBLIC $<$<COMPILE_LANGUAGE:C>:-fstack-protect
|
||||
|
||||
add_subdirectory(./arch/)
|
||||
add_subdirectory(./kernel/)
|
||||
add_subdirectory(./uapi/)
|
||||
|
||||
add_subdirectory(./test/)
|
||||
|
||||
add_subdirectory(./iso/)
|
||||
|
||||
@@ -23,7 +23,8 @@ target_sources(kernel.elf PRIVATE
|
||||
gdt.cpp
|
||||
misc.cpp
|
||||
syscalls.cpp
|
||||
syscalls.asm)
|
||||
syscalls.asm
|
||||
limine_modules.cpp)
|
||||
|
||||
target_include_directories(kernel.elf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "limine.h"
|
||||
#include "limine_fb.hpp"
|
||||
#include "limine_mm.hpp"
|
||||
#include "limine_modules.hpp"
|
||||
#include "memman.hpp"
|
||||
#include "misc.hpp"
|
||||
#include "paging.hpp"
|
||||
@@ -44,6 +45,7 @@ extern "C" void _start(void) {
|
||||
init_serial();
|
||||
barrier();
|
||||
limine_kern_save_response();
|
||||
limine_modules_save();
|
||||
barrier();
|
||||
map_hddm(get_cr3());
|
||||
barrier();
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
#include "SkipList.hpp"
|
||||
#include "String.hpp"
|
||||
#include "TestTemplates.hpp"
|
||||
#include "VMA.hpp"
|
||||
#include "asserts.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "kmem.hpp"
|
||||
#include "limine_fb.hpp"
|
||||
#include "limine_modules.hpp"
|
||||
#include "memman.hpp"
|
||||
#include "misc.hpp"
|
||||
#include "mutex.hpp"
|
||||
@@ -213,7 +215,17 @@ void ktask_main() {
|
||||
new_ktask(templates_tester, "templates_tester2");
|
||||
new_ktask(stress_tester, "stress_tester");
|
||||
|
||||
new_utask(user_task, "user");
|
||||
for (int i = 0; i < saved_modules_size; i++) {
|
||||
all_tty_putstr("Starting ");
|
||||
all_tty_putstr(saved_modules_names[i]);
|
||||
all_tty_putchar('\n');
|
||||
|
||||
Task *utask = new_utask((void (*)()) 0x00020000, saved_modules_names[i]);
|
||||
assert(saved_modules_size > 0);
|
||||
utask->vma->mmap_phys((void *) 0x00020000, (void *) KERN_V2P(saved_modules_data[i]),
|
||||
max_saved_module_file_size, PAGE_USER | PAGE_RW);
|
||||
start_utask(utask);
|
||||
}
|
||||
|
||||
remove_self();
|
||||
}
|
||||
|
||||
29
src/arch/x86/limine_modules.cpp
Normal file
29
src/arch/x86/limine_modules.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 26.11.2023.
|
||||
//
|
||||
|
||||
#include "limine_modules.hpp"
|
||||
|
||||
#include "asserts.hpp"
|
||||
#include "string.h"
|
||||
|
||||
static volatile struct limine_module_request module_request = {
|
||||
.id = LIMINE_MODULE_REQUEST,
|
||||
.revision = 0};
|
||||
|
||||
|
||||
limine_file saved_modules[max_saved_modules];
|
||||
char saved_modules_data[max_saved_modules][max_saved_module_file_size] __attribute__((aligned(4096)));
|
||||
char saved_modules_names[max_saved_modules][max_saved_module_name] __attribute__((aligned(4096)));
|
||||
unsigned saved_modules_size = 0;
|
||||
|
||||
void limine_modules_save() {
|
||||
for (int i = 0; i < module_request.response->module_count; i++) {
|
||||
assert(i < max_saved_modules);
|
||||
saved_modules_size++;
|
||||
saved_modules[i] = (*module_request.response->modules)[i];
|
||||
assert(saved_modules[i].size < max_saved_module_file_size);
|
||||
memcpy(saved_modules_data[i], saved_modules[i].address, saved_modules[i].size);
|
||||
memcpy(saved_modules_names[i], saved_modules[i].path, max_saved_module_name);
|
||||
}
|
||||
}
|
||||
21
src/arch/x86/limine_modules.hpp
Normal file
21
src/arch/x86/limine_modules.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 26.11.2023.
|
||||
//
|
||||
|
||||
#ifndef OS2_LIMINE_MODULES_HPP
|
||||
#define OS2_LIMINE_MODULES_HPP
|
||||
|
||||
#include "limine.hpp"
|
||||
|
||||
void limine_modules_save();
|
||||
|
||||
static constexpr unsigned max_saved_modules = 2;
|
||||
static constexpr unsigned max_saved_module_file_size = 1024 * 1024;
|
||||
static constexpr unsigned max_saved_module_name = 256;
|
||||
|
||||
extern unsigned saved_modules_size;
|
||||
extern limine_file saved_modules[max_saved_modules];
|
||||
extern char saved_modules_data[max_saved_modules][max_saved_module_file_size] __attribute__((aligned(4096)));
|
||||
extern char saved_modules_names[max_saved_modules][max_saved_module_name] __attribute__((aligned(4096)));
|
||||
|
||||
#endif//OS2_LIMINE_MODULES_HPP
|
||||
@@ -130,14 +130,14 @@ struct Task *new_ktask(void (*fn)(), const char *name) {
|
||||
}
|
||||
return newt;
|
||||
}
|
||||
struct Task *new_utask(void (*fn)(), const char *name) {
|
||||
struct Task *new_utask(void (*entrypoint)(), const char *name) {
|
||||
Task *newt = static_cast<Task *>(kmalloc(sizeof(struct Task)));
|
||||
newt->kstack = static_cast<uint64_t *>(kmalloc(TASK_SS));
|
||||
newt->name = static_cast<char *>(kmalloc(strlen(name) + 1));
|
||||
newt->fxsave = static_cast<char *>(kmalloc(512));
|
||||
strcpy(name, newt->name);
|
||||
|
||||
newt->frame.ip = (uint64_t) fn;
|
||||
newt->frame.ip = (uint64_t) entrypoint;
|
||||
newt->frame.cs = GDTSEL(gdt_code_user) | 0x3;
|
||||
newt->frame.ss = GDTSEL(gdt_data_user) | 0x3;
|
||||
|
||||
@@ -147,7 +147,7 @@ struct Task *new_utask(void (*fn)(), const char *name) {
|
||||
newt->frame.guard = IDT_GUARD;
|
||||
newt->addressSpace = new AddressSpace();
|
||||
newt->vma = new VMA(newt->addressSpace);
|
||||
newt->state = TS_RUNNING;
|
||||
newt->state = TS_BLOCKED;
|
||||
newt->mode = TASKMODE_USER;
|
||||
newt->pid = max_pid.fetch_add(1);
|
||||
newt->used_time = 0;
|
||||
@@ -175,13 +175,6 @@ struct Task *new_utask(void (*fn)(), const char *name) {
|
||||
|
||||
sanity_check_frame(&newt->frame);
|
||||
|
||||
auto new_node = NextTasks.create_node(newt);
|
||||
|
||||
{
|
||||
LockGuard l(NextTasks_lock);
|
||||
NextTasks.emplace_front(new_node);
|
||||
}
|
||||
|
||||
{
|
||||
LockGuard l(AllTasks_lock);
|
||||
AllTasks.add(newt->pid, newt);
|
||||
@@ -189,6 +182,15 @@ struct Task *new_utask(void (*fn)(), const char *name) {
|
||||
return newt;
|
||||
}
|
||||
|
||||
void start_utask(struct Task *task) {
|
||||
task->state = TS_RUNNING;
|
||||
auto new_node = NextTasks.create_node(task);
|
||||
{
|
||||
LockGuard l(NextTasks_lock);
|
||||
NextTasks.emplace_front(new_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void remove_self() {
|
||||
{
|
||||
|
||||
@@ -53,7 +53,8 @@ List<Task *>::Node *extract_running_task_node();
|
||||
|
||||
void init_tasks();
|
||||
struct Task *new_ktask(void (*fn)(), const char *name);
|
||||
struct Task *new_utask(void (*fn)(), const char *name);
|
||||
struct Task *new_utask(void (*entrypoint)(), const char *name);
|
||||
void start_utask(struct Task *task);
|
||||
void remove_self();
|
||||
void sleep_self(uint64_t diff);
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ add_custom_target(iso
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/isodir/
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/limine.cfg ${CMAKE_CURRENT_BINARY_DIR}/isodir/
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:kernel.elf> ${CMAKE_CURRENT_BINARY_DIR}/isodir/os2.elf
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:hello> ${CMAKE_CURRENT_BINARY_DIR}/isodir/hello
|
||||
COMMAND xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table --efi-boot limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label ${CMAKE_CURRENT_BINARY_DIR}/isodir -o ${CMAKE_CURRENT_BINARY_DIR}/os2.iso
|
||||
COMMAND ${tools}/limine/prefix/bin/limine bios-install ${CMAKE_CURRENT_BINARY_DIR}/os2.iso
|
||||
COMMAND ${tools}/limine/prefix/bin/limine bios-install ${CMAKE_CURRENT_BINARY_DIR}/os2.iso
|
||||
VERBATIM
|
||||
DEPENDS kernel.elf
|
||||
DEPENDS hello
|
||||
DEPENDS iso_limine
|
||||
)
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ TIMEOUT=1
|
||||
|
||||
# Path to the kernel to boot. boot:/// represents the partition on which limine.cfg is located.
|
||||
KERNEL_PATH=boot:///os2.elf
|
||||
|
||||
MODULE_PATH=boot:///hello
|
||||
|
||||
# Same thing, but without KASLR.
|
||||
:os2 (KASLR off)
|
||||
@@ -16,4 +18,5 @@ TIMEOUT=1
|
||||
# Disable KASLR (it is enabled by default for relocatable kernels)
|
||||
KASLR=no
|
||||
|
||||
KERNEL_PATH=boot:///os2.elf
|
||||
KERNEL_PATH=boot:///os2.elf
|
||||
MODULE_PATH=boot:///hello
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
target_include_directories(kernel.elf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_sources(kernel.elf PRIVATE mutex.cpp cppsupport.cpp Spinlock.cpp LockGuard.cpp rand.cpp VMA.cpp asserts.cpp syscalls_interface.c)
|
||||
target_sources(kernel.elf PRIVATE mutex.cpp cppsupport.cpp Spinlock.cpp LockGuard.cpp rand.cpp VMA.cpp asserts.cpp)
|
||||
|
||||
add_subdirectory(templates)
|
||||
9
src/test/CMakeLists.txt
Normal file
9
src/test/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
add_executable(hello hello.cpp)
|
||||
|
||||
target_link_libraries(hello syscalls_interface)
|
||||
|
||||
target_include_directories(hello PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_link_options(hello PRIVATE "SHELL:-T${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")
|
||||
set_target_properties(hello PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")
|
||||
|
||||
10
src/test/hello.cpp
Normal file
10
src/test/hello.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "syscalls_interface.h"
|
||||
|
||||
void _start() {
|
||||
while (true) {
|
||||
putchar('h');
|
||||
putchar('i');
|
||||
putchar('\n');
|
||||
sleep(100000);
|
||||
}
|
||||
}
|
||||
27
src/test/linker.ld
Normal file
27
src/test/linker.ld
Normal file
@@ -0,0 +1,27 @@
|
||||
OUTPUT_FORMAT("binary")
|
||||
OUTPUT_ARCH(i386:x86-64)
|
||||
|
||||
ENTRY(start)
|
||||
phys = 0x00020000;
|
||||
SECTIONS
|
||||
{
|
||||
.text phys : AT(phys) {
|
||||
code = .;
|
||||
*(.text)
|
||||
*(.rodata)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
.data : AT(phys + (data - code))
|
||||
{
|
||||
data = .;
|
||||
*(.data)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
.bss : AT(phys + (bss - code))
|
||||
{
|
||||
bss = .;
|
||||
*(.bss)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
end = .;
|
||||
}
|
||||
4
src/uapi/CMakeLists.txt
Normal file
4
src/uapi/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
add_library(syscalls_interface syscalls_interface.c)
|
||||
target_include_directories(syscalls_interface INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_link_libraries(kernel.elf syscalls_interface)
|
||||
Reference in New Issue
Block a user