Add a kernel ELF module and load it in a new process.
Don't yet jump to userspace.
This commit is contained in:
parent
f86bbe6ea9
commit
aefb4f082b
3
.gdbinit
3
.gdbinit
|
@ -1,3 +1,4 @@
|
||||||
target remote localhost:1234
|
source ~/.gdbinit-gef.py
|
||||||
|
gef-remote --qemu-user --qemu-binary=builddbg/zion/zion localhost 1234
|
||||||
file builddbg/zion/zion
|
file builddbg/zion/zion
|
||||||
break zion
|
break zion
|
||||||
|
|
|
@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS True)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS True)
|
||||||
|
|
||||||
add_subdirectory(zion)
|
add_subdirectory(zion)
|
||||||
|
add_subdirectory(sys)
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT disk.img
|
OUTPUT disk.img
|
||||||
|
|
|
@ -18,6 +18,7 @@ echo "Loopback device: ${dev}"
|
||||||
cleanup() {
|
cleanup() {
|
||||||
umount efi
|
umount efi
|
||||||
rm -rf efi
|
rm -rf efi
|
||||||
|
losetup -d $dev
|
||||||
}
|
}
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
@ -35,5 +36,7 @@ cp /usr/share/limine/BOOTX64.EFI efi/EFI/BOOT
|
||||||
cp /usr/share/limine/limine.sys efi/
|
cp /usr/share/limine/limine.sys efi/
|
||||||
cp ../zion/boot/limine.cfg efi/
|
cp ../zion/boot/limine.cfg efi/
|
||||||
cp zion/zion efi/
|
cp zion/zion efi/
|
||||||
|
mkdir -p efi/sys
|
||||||
|
cp sys/test efi/sys/test
|
||||||
|
|
||||||
chown drew:drew $1
|
chown drew:drew $1
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
set(_COMPILE_FLAGS "-ffreestanding -mgeneral-regs-only")
|
||||||
|
set(_LINK_FLAGS "-nostdlib")
|
||||||
|
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||||||
|
|
||||||
|
add_executable(test
|
||||||
|
test.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(test
|
||||||
|
zion_lib)
|
||||||
|
|
||||||
|
set_target_properties(test
|
||||||
|
PROPERTIES
|
||||||
|
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${_COMPILE_FLAGS}"
|
||||||
|
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${_LINK_FLAGS}"
|
||||||
|
)
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
#include "zcall.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ZDebug("Testing");
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -5,6 +5,8 @@ add_executable(zion
|
||||||
debug/debug.cpp
|
debug/debug.cpp
|
||||||
interrupt/interrupt.cpp
|
interrupt/interrupt.cpp
|
||||||
interrupt/interrupt_enter.s
|
interrupt/interrupt_enter.s
|
||||||
|
loader/elf_loader.cpp
|
||||||
|
loader/init_loader.cpp
|
||||||
memory/kernel_heap.cpp
|
memory/kernel_heap.cpp
|
||||||
memory/paging_util.cpp
|
memory/paging_util.cpp
|
||||||
memory/physical_memory.cpp
|
memory/physical_memory.cpp
|
||||||
|
@ -50,3 +52,17 @@ set_target_properties(zion
|
||||||
COMPILE_FLAGS "${_Z_COMPILE_FLAGS}"
|
COMPILE_FLAGS "${_Z_COMPILE_FLAGS}"
|
||||||
LINK_FLAGS "${_Z_LINK_FLAGS}"
|
LINK_FLAGS "${_Z_LINK_FLAGS}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
add_library(zion_lib STATIC
|
||||||
|
usr/crt0.s
|
||||||
|
usr/zcall.cpp)
|
||||||
|
|
||||||
|
target_include_directories(zion_lib
|
||||||
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
set_target_properties(zion_lib
|
||||||
|
PROPERTIES
|
||||||
|
COMPILE_FLAGS "${_Z_COMPILE_FLAGS}")
|
||||||
|
|
|
@ -25,4 +25,15 @@ uint64_t GetHigherHalfDirectMap() {
|
||||||
return gHhdmRequest.response->offset;
|
return gHhdmRequest.response->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static volatile struct limine_module_request gModuleRequest {
|
||||||
|
.id = LIMINE_MODULE_REQUEST, .revision = 0, .response = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const limine_module_response& GetModules() {
|
||||||
|
if (!gModuleRequest.response) {
|
||||||
|
panic("No module response from limine");
|
||||||
|
}
|
||||||
|
return *gModuleRequest.response;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace boot
|
} // namespace boot
|
||||||
|
|
|
@ -7,4 +7,6 @@ namespace boot {
|
||||||
const limine_memmap_response& GetMemoryMap();
|
const limine_memmap_response& GetMemoryMap();
|
||||||
uint64_t GetHigherHalfDirectMap();
|
uint64_t GetHigherHalfDirectMap();
|
||||||
|
|
||||||
|
const limine_module_response& GetModules();
|
||||||
|
|
||||||
} // namespace boot
|
} // namespace boot
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
TIMEOUT=0
|
TIMEOUT=0
|
||||||
|
|
||||||
:AcadiaOS
|
:AcadiaOS
|
||||||
|
PROTOCOL=limine
|
||||||
|
|
||||||
PROTOCOL=limine
|
KERNEL_PATH=boot:///zion
|
||||||
|
MODULE_PATH=boot:///sys/test
|
||||||
KERNEL_PATH=boot:///zion
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define Z_DEBUG_PRINT 100
|
||||||
|
|
||||||
|
uint64_t ZDebug(const char* message);
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
#include "loader/elf_loader.h"
|
||||||
|
|
||||||
|
#include "debug/debug.h"
|
||||||
|
#include "memory/paging_util.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char ident[16];
|
||||||
|
uint16_t type;
|
||||||
|
uint16_t machine;
|
||||||
|
uint32_t version;
|
||||||
|
uint64_t entry;
|
||||||
|
uint64_t phoff;
|
||||||
|
uint64_t shoff;
|
||||||
|
uint32_t flags;
|
||||||
|
uint16_t ehsize;
|
||||||
|
uint16_t phentsize;
|
||||||
|
uint16_t phnum;
|
||||||
|
uint16_t shentsize;
|
||||||
|
uint16_t shnum;
|
||||||
|
uint16_t shstrndx;
|
||||||
|
} Elf64Header;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t name;
|
||||||
|
uint32_t type;
|
||||||
|
uint64_t flags;
|
||||||
|
uint64_t addr;
|
||||||
|
uint64_t offset;
|
||||||
|
uint64_t size;
|
||||||
|
uint32_t link;
|
||||||
|
uint32_t info;
|
||||||
|
uint64_t addralign;
|
||||||
|
uint64_t entsize;
|
||||||
|
} Elf64SectionHeader;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t type;
|
||||||
|
uint32_t flags;
|
||||||
|
uint64_t offset;
|
||||||
|
uint64_t vaddr;
|
||||||
|
uint64_t paddr;
|
||||||
|
uint64_t filesz;
|
||||||
|
uint64_t memsz;
|
||||||
|
uint64_t align;
|
||||||
|
} Elf64ProgramHeader;
|
||||||
|
|
||||||
|
void badmemcpy(uint64_t base, uint64_t offset, uint64_t dest) {
|
||||||
|
uint8_t* ptr = reinterpret_cast<uint8_t*>(base);
|
||||||
|
uint8_t* dest_ptr = reinterpret_cast<uint8_t*>(dest);
|
||||||
|
for (uint64_t i = 0; i < offset; i++) {
|
||||||
|
dest_ptr[i] = ptr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void LoadElfProgram(uint64_t base, uint64_t offset) {
|
||||||
|
Elf64Header* header = reinterpret_cast<Elf64Header*>(base);
|
||||||
|
dbgln("phoff: %u phnum: %u", header->phoff, header->phnum);
|
||||||
|
Elf64ProgramHeader* programs =
|
||||||
|
reinterpret_cast<Elf64ProgramHeader*>(base + header->phoff);
|
||||||
|
for (uint64_t i = 0; i < header->phnum; i++) {
|
||||||
|
Elf64ProgramHeader& program = programs[i];
|
||||||
|
dbgln(
|
||||||
|
"prog: type: %u, flags: %u, offset: %u\n vaddr: %m, paddr: %m\n "
|
||||||
|
"filesz: %u, memsz: %u, align: %u",
|
||||||
|
program.type, program.flags, program.offset, program.vaddr,
|
||||||
|
program.paddr, program.filesz, program.memsz, program.align);
|
||||||
|
EnsureResident(program.vaddr, program.memsz);
|
||||||
|
badmemcpy(base + program.offset, program.filesz, program.vaddr);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void LoadElfProgram(uint64_t base, uint64_t length);
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "loader/init_loader.h"
|
||||||
|
|
||||||
|
#include "boot/boot_info.h"
|
||||||
|
#include "debug/debug.h"
|
||||||
|
#include "loader/elf_loader.h"
|
||||||
|
#include "scheduler/process.h"
|
||||||
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const limine_file& GetInitProgram() {
|
||||||
|
const limine_module_response& resp = boot::GetModules();
|
||||||
|
dbgln("Dumping modules");
|
||||||
|
for (uint64_t i = 0; i < resp.module_count; i++) {
|
||||||
|
const limine_file& file = *resp.modules[i];
|
||||||
|
dbgln("%s,%m,%x", file.path, file.address, file.size);
|
||||||
|
// TODO eventually compare this file path.
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
panic("No init program found");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void LoadInitProgram() {
|
||||||
|
const limine_file& init_prog = GetInitProgram();
|
||||||
|
|
||||||
|
sched::InsertProcess(
|
||||||
|
new Process(reinterpret_cast<uint64_t>(init_prog.address)));
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void LoadInitProgram();
|
|
@ -25,14 +25,14 @@ Process* Process::RootProcess() {
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
Process::Process() : id_(gNextId++) {
|
Process::Process(uint64_t elf_ptr) : id_(gNextId++) {
|
||||||
cr3_ = phys_mem::AllocatePage();
|
cr3_ = phys_mem::AllocatePage();
|
||||||
InitializePml4(cr3_);
|
InitializePml4(cr3_);
|
||||||
CreateThread();
|
CreateThread(elf_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Process::CreateThread() {
|
void Process::CreateThread(uint64_t elf_ptr) {
|
||||||
Thread* thread = new Thread(this, next_thread_id_++);
|
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
|
||||||
ThreadEntry* tentry = new ThreadEntry{
|
ThreadEntry* tentry = new ThreadEntry{
|
||||||
.thread = thread,
|
.thread = thread,
|
||||||
.next = nullptr,
|
.next = nullptr,
|
||||||
|
|
|
@ -9,12 +9,12 @@ class Process {
|
||||||
public:
|
public:
|
||||||
// Caller takes ownership of returned process.
|
// Caller takes ownership of returned process.
|
||||||
static Process* RootProcess();
|
static Process* RootProcess();
|
||||||
Process();
|
Process(uint64_t elf_ptr);
|
||||||
|
|
||||||
uint64_t id() { return id_; }
|
uint64_t id() { return id_; }
|
||||||
uint64_t cr3() { return cr3_; }
|
uint64_t cr3() { return cr3_; }
|
||||||
|
|
||||||
void CreateThread();
|
void CreateThread(uint64_t elf_ptr);
|
||||||
Thread* GetThread(uint64_t tid);
|
Thread* GetThread(uint64_t tid);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -55,6 +55,7 @@ class Scheduler {
|
||||||
Process& CurrentProcess() { return current_thread_->process(); }
|
Process& CurrentProcess() { return current_thread_->process(); }
|
||||||
Thread& CurrentThread() { return *current_thread_; }
|
Thread& CurrentThread() { return *current_thread_; }
|
||||||
|
|
||||||
|
void InsertProcess(Process* process) { proc_list_.InsertProcess(process); }
|
||||||
void Enqueue(Thread* thread) {
|
void Enqueue(Thread* thread) {
|
||||||
Thread* back = current_thread_;
|
Thread* back = current_thread_;
|
||||||
while (back->next_thread_ != nullptr) {
|
while (back->next_thread_ != nullptr) {
|
||||||
|
@ -108,6 +109,7 @@ void EnableScheduler() { GetScheduler().Enable(); }
|
||||||
|
|
||||||
void Yield() { GetScheduler().Yield(); }
|
void Yield() { GetScheduler().Yield(); }
|
||||||
|
|
||||||
|
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
||||||
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
|
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
|
||||||
|
|
||||||
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }
|
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "scheduler/thread.h"
|
#include "scheduler/thread.h"
|
||||||
|
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
|
#include "loader/elf_loader.h"
|
||||||
#include "scheduler/process.h"
|
#include "scheduler/process.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
|
@ -16,7 +17,8 @@ extern "C" void thread_init() {
|
||||||
|
|
||||||
Thread* Thread::RootThread(Process* root_proc) { return new Thread(root_proc); }
|
Thread* Thread::RootThread(Process* root_proc) { return new Thread(root_proc); }
|
||||||
|
|
||||||
Thread::Thread(Process* proc, uint64_t tid) : process_(proc), id_(tid) {
|
Thread::Thread(Process* proc, uint64_t tid, uint64_t elf_ptr)
|
||||||
|
: process_(proc), id_(tid), elf_ptr_(elf_ptr) {
|
||||||
uint64_t* stack = new uint64_t[512];
|
uint64_t* stack = new uint64_t[512];
|
||||||
uint64_t* stack_ptr = stack + 511;
|
uint64_t* stack_ptr = stack + 511;
|
||||||
// 0: rip
|
// 0: rip
|
||||||
|
@ -34,6 +36,7 @@ Thread::Thread(Process* proc, uint64_t tid) : process_(proc), id_(tid) {
|
||||||
uint64_t Thread::pid() { return process_->id(); }
|
uint64_t Thread::pid() { return process_->id(); }
|
||||||
|
|
||||||
void Thread::Init() {
|
void Thread::Init() {
|
||||||
|
LoadElfProgram(elf_ptr_, 0);
|
||||||
while (true) {
|
while (true) {
|
||||||
dbgln("[%u.%u]", pid(), id_);
|
dbgln("[%u.%u]", pid(), id_);
|
||||||
sched::Yield();
|
sched::Yield();
|
||||||
|
|
|
@ -9,7 +9,7 @@ class Thread {
|
||||||
public:
|
public:
|
||||||
static Thread* RootThread(Process* root_proc);
|
static Thread* RootThread(Process* root_proc);
|
||||||
|
|
||||||
explicit Thread(Process* proc, uint64_t tid);
|
explicit Thread(Process* proc, uint64_t tid, uint64_t elf_ptr);
|
||||||
|
|
||||||
uint64_t tid() { return id_; };
|
uint64_t tid() { return id_; };
|
||||||
uint64_t pid();
|
uint64_t pid();
|
||||||
|
@ -31,6 +31,8 @@ class Thread {
|
||||||
Process* process_;
|
Process* process_;
|
||||||
uint64_t id_;
|
uint64_t id_;
|
||||||
|
|
||||||
|
uint64_t elf_ptr_;
|
||||||
|
|
||||||
// Stack pointer to take on resume.
|
// Stack pointer to take on resume.
|
||||||
// Stack will contain the full thread context.
|
// Stack will contain the full thread context.
|
||||||
uint64_t rsp0_;
|
uint64_t rsp0_;
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
.text
|
||||||
|
|
||||||
|
.global _start
|
||||||
|
_start:
|
||||||
|
call main
|
||||||
|
call _exit
|
||||||
|
|
||||||
|
_exit:
|
||||||
|
mov $1, %rdi
|
||||||
|
syscall
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "include/zcall.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
uint64_t SysCall1(uint64_t number, const void* first) {
|
||||||
|
uint64_t return_code;
|
||||||
|
asm("syscall" : "=a"(return_code) : "D"(number), "S"(first) : "rcx", "r11");
|
||||||
|
return return_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t ZDebug(const char* message) {
|
||||||
|
return SysCall1(Z_DEBUG_PRINT, message);
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
#include "common/gdt.h"
|
#include "common/gdt.h"
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
#include "interrupt/interrupt.h"
|
#include "interrupt/interrupt.h"
|
||||||
|
#include "loader/init_loader.h"
|
||||||
#include "memory/kernel_heap.h"
|
#include "memory/kernel_heap.h"
|
||||||
#include "memory/paging_util.h"
|
#include "memory/paging_util.h"
|
||||||
#include "memory/physical_memory.h"
|
#include "memory/physical_memory.h"
|
||||||
|
@ -21,11 +22,9 @@ extern "C" void zion() {
|
||||||
InitSyscall();
|
InitSyscall();
|
||||||
|
|
||||||
sched::InitScheduler();
|
sched::InitScheduler();
|
||||||
Process p1;
|
|
||||||
p1.CreateThread();
|
|
||||||
Process p2;
|
|
||||||
p2.CreateThread();
|
|
||||||
sched::EnableScheduler();
|
sched::EnableScheduler();
|
||||||
|
|
||||||
|
LoadInitProgram();
|
||||||
sched::Yield();
|
sched::Yield();
|
||||||
|
|
||||||
dbgln("Sleeping!");
|
dbgln("Sleeping!");
|
||||||
|
|
Loading…
Reference in New Issue