Compare commits
5 Commits
b9b45c5e45
...
2eefda6114
Author | SHA1 | Date |
---|---|---|
Drew Galbraith | 2eefda6114 | |
Drew Galbraith | 1db93e5b12 | |
Drew Galbraith | f22dd66c8d | |
Drew Galbraith | 3c3341a90f | |
Drew Galbraith | 5bcf1f4e4a |
|
@ -15,7 +15,7 @@ add_subdirectory(sys)
|
|||
add_custom_command(
|
||||
OUTPUT disk.img
|
||||
COMMAND sudo sh ../scripts/build_image.sh disk.img
|
||||
DEPENDS zion test
|
||||
DEPENDS zion test test2
|
||||
USES_TERMINAL
|
||||
)
|
||||
|
||||
|
|
|
@ -38,5 +38,6 @@ cp ../zion/boot/limine.cfg efi/
|
|||
cp zion/zion efi/
|
||||
mkdir -p efi/sys
|
||||
cp sys/test efi/sys/test
|
||||
cp sys/test2 efi/sys/test2
|
||||
|
||||
chown drew:drew $1
|
||||
|
|
|
@ -15,3 +15,15 @@ set_target_properties(test
|
|||
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${_COMPILE_FLAGS}"
|
||||
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${_LINK_FLAGS}"
|
||||
)
|
||||
|
||||
add_executable(test2
|
||||
test2.cpp)
|
||||
|
||||
target_link_libraries(test2
|
||||
zion_lib)
|
||||
|
||||
set_target_properties(test2
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${_COMPILE_FLAGS}"
|
||||
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${_LINK_FLAGS}"
|
||||
)
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
|
||||
#include "zcall.h"
|
||||
|
||||
constexpr uint64_t prog2 = 0x00000020'00000000;
|
||||
|
||||
int main() {
|
||||
ZDebug("Testing");
|
||||
ZProcessSpawn(prog2, 0x1000);
|
||||
ZDebug("Return");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
#include "zcall.h"
|
||||
|
||||
int main() {
|
||||
ZDebug("Testing");
|
||||
return 0;
|
||||
}
|
|
@ -9,8 +9,11 @@ add_executable(zion
|
|||
loader/elf_loader.cpp
|
||||
loader/init_loader.cpp
|
||||
memory/kernel_heap.cpp
|
||||
memory/kernel_stack_manager.cpp
|
||||
memory/paging_util.cpp
|
||||
memory/physical_memory.cpp
|
||||
memory/user_stack_manager.cpp
|
||||
memory/virtual_memory.cpp
|
||||
scheduler/context_switch.s
|
||||
scheduler/jump_user_space.s
|
||||
scheduler/process.cpp
|
||||
|
|
|
@ -7,3 +7,4 @@ TIMEOUT=0
|
|||
|
||||
KERNEL_PATH=boot:///zion
|
||||
MODULE_PATH=boot:///sys/test
|
||||
MODULE_PATH=boot:///sys/test2
|
||||
|
|
|
@ -3,6 +3,17 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#define Z_THREAD_EXIT 0x01
|
||||
|
||||
#define Z_PROCESS_SPAWN 0x10
|
||||
|
||||
#define Z_DEBUG_PRINT 0x100
|
||||
|
||||
uint64_t ZDebug(const char* message);
|
||||
|
||||
// TODO: Move structs into an internal header.
|
||||
struct ZProcessSpawnReq {
|
||||
uint64_t elf_base;
|
||||
uint64_t elf_size;
|
||||
};
|
||||
|
||||
uint64_t ZProcessSpawn(uint64_t elf_base, uint64_t elf_size);
|
||||
|
|
|
@ -48,7 +48,7 @@ typedef struct {
|
|||
|
||||
} // namespace
|
||||
|
||||
uint64_t LoadElfProgram(uint64_t cr3, uint64_t base, uint64_t offset) {
|
||||
uint64_t LoadElfProgram(Process& dest_proc, uint64_t base, uint64_t offset) {
|
||||
Elf64Header* header = reinterpret_cast<Elf64Header*>(base);
|
||||
dbgln("phoff: %u phnum: %u", header->phoff, header->phnum);
|
||||
Elf64ProgramHeader* programs =
|
||||
|
@ -60,7 +60,7 @@ uint64_t LoadElfProgram(uint64_t cr3, uint64_t base, uint64_t offset) {
|
|||
"filesz: %u, memsz: %u, align: %u",
|
||||
program.type, program.flags, program.offset, program.vaddr,
|
||||
program.paddr, program.filesz, program.memsz, program.align);
|
||||
CopyIntoNonResidentProcess(base + program.offset, program.filesz, cr3,
|
||||
CopyIntoNonResidentProcess(base + program.offset, program.filesz, dest_proc,
|
||||
program.vaddr);
|
||||
}
|
||||
return header->entry;
|
||||
|
|
|
@ -2,5 +2,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "scheduler/process.h"
|
||||
|
||||
// Loads the elf program and returns its entry point.
|
||||
uint64_t LoadElfProgram(uint64_t cr3, uint64_t base, uint64_t length);
|
||||
uint64_t LoadElfProgram(Process& dest_proc, uint64_t base, uint64_t length);
|
||||
|
|
|
@ -3,33 +3,52 @@
|
|||
#include "boot/boot_info.h"
|
||||
#include "debug/debug.h"
|
||||
#include "loader/elf_loader.h"
|
||||
#include "memory/paging_util.h"
|
||||
#include "scheduler/process.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const limine_file& GetInitProgram() {
|
||||
bool streq(const char* a, const char* b) {
|
||||
while (true) {
|
||||
if (*a == '\0' && *b == '\0') return true;
|
||||
if (*a == '\0' || *b == '\0') {
|
||||
return false;
|
||||
}
|
||||
if (*a != *b) {
|
||||
return false;
|
||||
}
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
}
|
||||
|
||||
const limine_file& GetInitProgram(const char* path) {
|
||||
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;
|
||||
if (streq(file.path, path)) return file;
|
||||
}
|
||||
panic("No init program found");
|
||||
panic("Program not found: %s", path);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void LoadInitProgram() {
|
||||
const limine_file& init_prog = GetInitProgram();
|
||||
const limine_file& init_prog = GetInitProgram("/sys/test");
|
||||
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
||||
|
||||
SharedPtr<Process> proc = MakeShared<Process>();
|
||||
gProcMan->InsertProcess(proc);
|
||||
|
||||
uint64_t entry =
|
||||
LoadElfProgram(proc->cr3(), reinterpret_cast<uint64_t>(init_prog.address),
|
||||
init_prog.size);
|
||||
uint64_t entry = LoadElfProgram(
|
||||
*proc, reinterpret_cast<uint64_t>(init_prog.address), init_prog.size);
|
||||
|
||||
CopyIntoNonResidentProcess(reinterpret_cast<uint64_t>(prog2.address),
|
||||
prog2.size, *proc,
|
||||
proc->vmm().GetNextMemMapAddr(prog2.size));
|
||||
|
||||
proc->CreateThread(entry);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
#include "memory/kernel_stack_manager.h"
|
||||
|
||||
#include "debug/debug.h"
|
||||
#include "memory/paging_util.h"
|
||||
|
||||
#define KERNEL_STACK_START 0xFFFFFFFF'90000000
|
||||
#define KERNEL_STACK_LIMIT 0xFFFFFFFF'9FFFFFFF
|
||||
#define KERNEL_STACK_OFFSET 0x4000
|
||||
|
||||
KernelStackManager* gKernelStackManager;
|
||||
|
||||
void KernelStackManager::Init() {
|
||||
gKernelStackManager = new KernelStackManager();
|
||||
}
|
||||
|
||||
KernelStackManager::KernelStackManager()
|
||||
: next_stack_addr_(KERNEL_STACK_START) {}
|
||||
|
||||
uint64_t* KernelStackManager::AllocateKernelStack() {
|
||||
next_stack_addr_ += KERNEL_STACK_OFFSET;
|
||||
if (next_stack_addr_ >= KERNEL_STACK_LIMIT) {
|
||||
panic("No more kernelstack space");
|
||||
}
|
||||
EnsureResident(next_stack_addr_ - 0x3000, 0x3000);
|
||||
return reinterpret_cast<uint64_t*>(next_stack_addr_) - 1;
|
||||
}
|
||||
|
||||
void KernelStackManager::FreeKernelStack(uint64_t stack_base) {
|
||||
freed_stack_cnt_++;
|
||||
dbgln("Freed kernel stacks using %u KiB", freed_stack_cnt_ * 12);
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// KernelStackManager doles out kernel stacks.
|
||||
//
|
||||
// KernelStacks are in the region:
|
||||
// 0xFFFFFFFF 90000000 - 0xFFFFFFFF 9FFFFFFF
|
||||
//
|
||||
// Each kernel stack is 12 KiB with a 4 Kib page boundary.
|
||||
//
|
||||
// It is global object that is only exposed via internal linkage
|
||||
// to the VirtualMemory class. All kernel stacks should be created through that
|
||||
// class.
|
||||
class KernelStackManager {
|
||||
public:
|
||||
static void Init();
|
||||
|
||||
uint64_t* AllocateKernelStack();
|
||||
|
||||
void FreeKernelStack(uint64_t stack_base);
|
||||
|
||||
private:
|
||||
KernelStackManager();
|
||||
uint64_t next_stack_addr_;
|
||||
uint64_t freed_stack_cnt_ = 0;
|
||||
};
|
|
@ -174,15 +174,15 @@ void EnsureResident(uint64_t addr, uint64_t size) {
|
|||
}
|
||||
}
|
||||
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size, uint64_t dest_cr3,
|
||||
uint64_t dest_virt) {
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size,
|
||||
Process& dest_proc, uint64_t dest_virt) {
|
||||
if (size > 0x1000) {
|
||||
panic("Unimplemented NR copy > 1 page");
|
||||
}
|
||||
if (dest_virt & 0xFFF) {
|
||||
panic("Unimplemented NR copy to non page aligned");
|
||||
}
|
||||
uint64_t phys = AllocatePageIfNecessary(dest_virt, dest_cr3);
|
||||
uint64_t phys = AllocatePageIfNecessary(dest_virt, dest_proc.vmm().cr3());
|
||||
uint8_t* src = reinterpret_cast<uint8_t*>(base);
|
||||
uint8_t* dest =
|
||||
reinterpret_cast<uint8_t*>(phys + boot::GetHigherHalfDirectMap());
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "scheduler/process.h"
|
||||
|
||||
void InitializePml4(uint64_t pml4_physical_addr);
|
||||
|
||||
uint64_t AllocatePageIfNecessary(uint64_t addr, uint64_t cr3 = 0);
|
||||
void EnsureResident(uint64_t addr, uint64_t size);
|
||||
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size, uint64_t dest_cr3,
|
||||
uint64_t dest_virt);
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size,
|
||||
Process& dest_proc, uint64_t dest_virt);
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
#include "memory/user_stack_manager.h"
|
||||
|
||||
#include "debug/debug.h"
|
||||
#include "memory/paging_util.h"
|
||||
|
||||
uint64_t UserStackManager::NewUserStack() {
|
||||
uint64_t stack = next_stack_;
|
||||
next_stack_ -= kStackSize;
|
||||
if (stack <= kStackMin) {
|
||||
panic("Out of user stacks!");
|
||||
}
|
||||
if (stack == kStackMax) {
|
||||
// Add a additional page boudary between kernel and user space.
|
||||
stack -= 0x1000;
|
||||
}
|
||||
EnsureResident(stack - 1, 1);
|
||||
return stack;
|
||||
}
|
||||
|
||||
void UserStackManager::FreeUserStack(uint64_t stack_ptr) {
|
||||
freed_stacks_++;
|
||||
dbgln("%u freed user stacks", freed_stacks_);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Per-process class to manage user stacks.
|
||||
//
|
||||
// User stacks live at
|
||||
// 0x00007FF0 00000000 - 0x00007FFF FFFFFFFF
|
||||
// and grow downwards.
|
||||
//
|
||||
// Each user stack gets 1 MiB (0x100000 bytes)
|
||||
// less a page boundary.
|
||||
// with 1 page allocated at a time.
|
||||
// TODO: consider increasing this.
|
||||
class UserStackManager {
|
||||
public:
|
||||
UserStackManager() {}
|
||||
UserStackManager(const UserStackManager&) = delete;
|
||||
|
||||
uint64_t NewUserStack();
|
||||
void FreeUserStack(uint64_t stack_ptr);
|
||||
|
||||
private:
|
||||
const uint64_t kStackMax = 0x00008000'00000000;
|
||||
const uint64_t kStackMin = 0x00007FF0'00000000;
|
||||
const uint64_t kStackSize = 0x100000;
|
||||
|
||||
uint64_t next_stack_ = kStackMax;
|
||||
uint64_t freed_stacks_ = 0;
|
||||
};
|
|
@ -0,0 +1,35 @@
|
|||
#include "memory/virtual_memory.h"
|
||||
|
||||
#include "memory/kernel_stack_manager.h"
|
||||
#include "memory/paging_util.h"
|
||||
#include "memory/physical_memory.h"
|
||||
|
||||
extern KernelStackManager* gKernelStackManager;
|
||||
|
||||
VirtualMemory VirtualMemory::ForRoot() {
|
||||
uint64_t cr3 = 0;
|
||||
asm volatile("mov %%cr3, %0;" : "=r"(cr3));
|
||||
return {cr3};
|
||||
}
|
||||
|
||||
VirtualMemory::VirtualMemory() {
|
||||
cr3_ = phys_mem::AllocatePage();
|
||||
InitializePml4(cr3_);
|
||||
}
|
||||
|
||||
uint64_t VirtualMemory::AllocateUserStack() {
|
||||
return user_stacks_.NewUserStack();
|
||||
}
|
||||
|
||||
uint64_t VirtualMemory::GetNextMemMapAddr(uint64_t size) {
|
||||
uint64_t addr = next_memmap_addr_;
|
||||
next_memmap_addr_ += size;
|
||||
if (next_memmap_addr_ >= 0x30'00000000) {
|
||||
panic("OOM: Memmap");
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
uint64_t* VirtualMemory::AllocateKernelStack() {
|
||||
return gKernelStackManager->AllocateKernelStack();
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "debug/debug.h"
|
||||
#include "memory/user_stack_manager.h"
|
||||
|
||||
// VirtualMemory class holds a memory space for an individual process.
|
||||
//
|
||||
// Memory Regions are predefined for simplicity for now. However, in general
|
||||
// we try not to rely on these regions being static to allow for flexibility in
|
||||
// the future.
|
||||
//
|
||||
// User Regions (Per Process):
|
||||
// 0x00000000 00000000 - 0x0000000F FFFFFFFF : USER_CODE (64 GiB)
|
||||
// 0x00000010 00000000 - 0x0000001F FFFFFFFF : USER_HEAP (64 GiB)
|
||||
// 0x00000020 00000000 - 0x0000002F FFFFFFFF : MEM_MAP (64 GiB)
|
||||
// 0x00000040 00000000 - 0x0000004F FFFFFFFF : IPC_BUF (64 GiB)
|
||||
// 0x00007FF0 00000000 - 0x00007FFF FFFFFFFF : USER_STACK (64 GiB)
|
||||
//
|
||||
// Kernel Regions (Shared across processes):
|
||||
// 0xFFFF8000 00000000 - 0xFFFF800F FFFFFFFF : HHDM (64 GiB)
|
||||
// 0xFFFFFFFF 40000000 - 0xFFFFFFFF 7FFFFFFF : KERNEL_HEAP (1 GiB)
|
||||
// 0xFFFFFFFF 80000000 - 0xFFFFFFFF 80FFFFFF : KERNEL_CODE (16 MiB)
|
||||
// 0xFFFFFFFF 90000000 - 0xFFFFFFFF 9FFFFFFF : KERNEL_STACK (256 MiB)
|
||||
class VirtualMemory {
|
||||
public:
|
||||
enum MemoryType {
|
||||
UNSPECIFIED,
|
||||
UNMAPPED,
|
||||
USER_CODE,
|
||||
USER_HEAP,
|
||||
MEM_MAP,
|
||||
IPC_BUF,
|
||||
USER_STACK,
|
||||
HHDM,
|
||||
KERNEL_HEAP,
|
||||
KERNEL_CODE,
|
||||
KERNEL_STACK,
|
||||
};
|
||||
|
||||
static VirtualMemory ForRoot();
|
||||
|
||||
VirtualMemory();
|
||||
VirtualMemory(const VirtualMemory&) = delete;
|
||||
VirtualMemory(VirtualMemory&&) = delete;
|
||||
|
||||
uint64_t cr3() { return cr3_; }
|
||||
|
||||
// User Mappings.
|
||||
uint64_t AllocateUserStack();
|
||||
uint64_t GetNextMemMapAddr(uint64_t size);
|
||||
|
||||
// Kernel Mappings.
|
||||
uint64_t* AllocateKernelStack();
|
||||
|
||||
private:
|
||||
VirtualMemory(uint64_t cr3) : cr3_(cr3) {}
|
||||
uint64_t cr3_ = 0;
|
||||
|
||||
UserStackManager user_stacks_;
|
||||
uint64_t next_memmap_addr_ = 0x20'00000000;
|
||||
};
|
|
@ -13,22 +13,17 @@ static uint64_t gNextId = 1;
|
|||
}
|
||||
|
||||
SharedPtr<Process> Process::RootProcess() {
|
||||
uint64_t pml4_addr = 0;
|
||||
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
|
||||
SharedPtr<Process> proc(new Process(0, pml4_addr));
|
||||
proc->threads_.PushBack(Thread::RootThread(proc.ptr()));
|
||||
SharedPtr<Process> proc(new Process(0));
|
||||
proc->threads_.PushBack(Thread::RootThread(*proc));
|
||||
proc->next_thread_id_ = 1;
|
||||
|
||||
return proc;
|
||||
}
|
||||
|
||||
Process::Process() : id_(gNextId++), state_(RUNNING) {
|
||||
cr3_ = phys_mem::AllocatePage();
|
||||
InitializePml4(cr3_);
|
||||
}
|
||||
Process::Process() : id_(gNextId++), state_(RUNNING) {}
|
||||
|
||||
void Process::CreateThread(uint64_t entry) {
|
||||
Thread* thread = new Thread(this, next_thread_id_++, entry);
|
||||
Thread* thread = new Thread(*this, next_thread_id_++, entry);
|
||||
threads_.PushBack(thread);
|
||||
gScheduler->Enqueue(thread);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/shared_ptr.h"
|
||||
#include "memory/virtual_memory.h"
|
||||
|
||||
// Forward decl due to cyclic dependency.
|
||||
class Thread;
|
||||
|
@ -20,7 +21,7 @@ class Process {
|
|||
Process();
|
||||
|
||||
uint64_t id() const { return id_; }
|
||||
uint64_t cr3() const { return cr3_; }
|
||||
VirtualMemory& vmm() { return vmm_; }
|
||||
|
||||
void CreateThread(uint64_t entry);
|
||||
SharedPtr<Thread> GetThread(uint64_t tid);
|
||||
|
@ -32,9 +33,9 @@ class Process {
|
|||
State GetState() { return state_; }
|
||||
|
||||
private:
|
||||
Process(uint64_t id, uint64_t cr3) : id_(id), cr3_(cr3) {}
|
||||
Process(uint64_t id) : id_(id), vmm_(VirtualMemory::ForRoot()) {}
|
||||
uint64_t id_;
|
||||
uint64_t cr3_;
|
||||
VirtualMemory vmm_;
|
||||
State state_;
|
||||
|
||||
uint64_t next_thread_id_ = 0;
|
||||
|
|
|
@ -19,14 +19,13 @@ extern "C" void thread_init() {
|
|||
|
||||
} // namespace
|
||||
|
||||
SharedPtr<Thread> Thread::RootThread(Process* root_proc) {
|
||||
SharedPtr<Thread> Thread::RootThread(Process& root_proc) {
|
||||
return new Thread(root_proc);
|
||||
}
|
||||
|
||||
Thread::Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry)
|
||||
Thread::Thread(Process& proc, uint64_t tid, uint64_t entry)
|
||||
: process_(proc), id_(tid), rip_(entry) {
|
||||
uint64_t* stack = new uint64_t[512];
|
||||
uint64_t* stack_ptr = stack + 511;
|
||||
uint64_t* stack_ptr = proc.vmm().AllocateKernelStack();
|
||||
// 0: rip
|
||||
*(stack_ptr) = reinterpret_cast<uint64_t>(thread_init);
|
||||
// 1-4: rax, rcx, rdx, rbx
|
||||
|
@ -34,17 +33,16 @@ Thread::Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry)
|
|||
*(stack_ptr - 5) = reinterpret_cast<uint64_t>(stack_ptr + 1);
|
||||
// 6-15: rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15
|
||||
// 16: cr3
|
||||
*(stack_ptr - 16) = proc->cr3();
|
||||
*(stack_ptr - 16) = proc.vmm().cr3();
|
||||
rsp0_ = reinterpret_cast<uint64_t>(stack_ptr - 16);
|
||||
rsp0_start_ = reinterpret_cast<uint64_t>(stack_ptr);
|
||||
}
|
||||
|
||||
uint64_t Thread::pid() { return process_->id(); }
|
||||
uint64_t Thread::pid() const { return process_.id(); }
|
||||
|
||||
void Thread::Init() {
|
||||
dbgln("[%u.%u] thread start.", pid(), id_);
|
||||
uint64_t rsp = 0x80000000;
|
||||
EnsureResident(rsp - 1, 1);
|
||||
uint64_t rsp = process_.vmm().AllocateUserStack();
|
||||
SetRsp0(rsp0_start_);
|
||||
jump_user_space(rip_, rsp);
|
||||
}
|
||||
|
@ -52,6 +50,6 @@ void Thread::Init() {
|
|||
void Thread::Exit() {
|
||||
dbgln("[%u.%u] Exiting", pid(), id_);
|
||||
state_ = FINISHED;
|
||||
process_->CheckState();
|
||||
process_.CheckState();
|
||||
gScheduler->Yield();
|
||||
}
|
||||
|
|
|
@ -15,14 +15,14 @@ class Thread {
|
|||
RUNNABLE,
|
||||
FINISHED,
|
||||
};
|
||||
static SharedPtr<Thread> RootThread(Process* root_proc);
|
||||
static SharedPtr<Thread> RootThread(Process& root_proc);
|
||||
|
||||
explicit Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry);
|
||||
Thread(Process& proc, uint64_t tid, uint64_t entry);
|
||||
|
||||
uint64_t tid() { return id_; };
|
||||
uint64_t pid();
|
||||
uint64_t tid() const { return id_; };
|
||||
uint64_t pid() const;
|
||||
|
||||
Process& process() { return *process_; }
|
||||
Process& process() { return process_; }
|
||||
|
||||
uint64_t* Rsp0Ptr() { return &rsp0_; }
|
||||
uint64_t Rsp0Start() { return rsp0_start_; }
|
||||
|
@ -37,8 +37,8 @@ class Thread {
|
|||
|
||||
private:
|
||||
// Special constructor for the root thread only.
|
||||
Thread(Process* proc) : process_(proc), id_(0) {}
|
||||
SharedPtr<Process> process_;
|
||||
Thread(Process& proc) : process_(proc), id_(0) {}
|
||||
Process& process_;
|
||||
uint64_t id_;
|
||||
State state_ = RUNNABLE;
|
||||
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
|
||||
#include "debug/debug.h"
|
||||
#include "include/zcall.h"
|
||||
#include "loader/elf_loader.h"
|
||||
#include "scheduler/process.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
#define EFER 0xC0000080
|
||||
|
@ -53,7 +56,16 @@ void InitSyscall() {
|
|||
SetMSR(LSTAR, reinterpret_cast<uint64_t>(syscall_enter));
|
||||
}
|
||||
|
||||
extern "C" void SyscallHandler(uint64_t call_id, char* message) {
|
||||
uint64_t ProcessSpawn(ZProcessSpawnReq* req) {
|
||||
dbgln("Proc spawn: %u:%u", req->elf_base, req->elf_size);
|
||||
SharedPtr<Process> proc = MakeShared<Process>();
|
||||
gProcMan->InsertProcess(proc);
|
||||
uint64_t entry = LoadElfProgram(*proc, req->elf_base, req->elf_size);
|
||||
proc->CreateThread(entry);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" uint64_t SyscallHandler(uint64_t call_id, char* message) {
|
||||
Thread& thread = gScheduler->CurrentThread();
|
||||
switch (call_id) {
|
||||
case Z_THREAD_EXIT:
|
||||
|
@ -63,7 +75,10 @@ extern "C" void SyscallHandler(uint64_t call_id, char* message) {
|
|||
case Z_DEBUG_PRINT:
|
||||
dbgln("[%u.%u] [Debug] %s", thread.pid(), thread.tid(), message);
|
||||
break;
|
||||
case Z_PROCESS_SPAWN:
|
||||
return ProcessSpawn(reinterpret_cast<ZProcessSpawnReq*>(message));
|
||||
default:
|
||||
panic("Unhandled syscall number: %u", call_id);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -11,3 +11,11 @@ uint64_t SysCall1(uint64_t number, const void* first) {
|
|||
uint64_t ZDebug(const char* message) {
|
||||
return SysCall1(Z_DEBUG_PRINT, message);
|
||||
}
|
||||
|
||||
uint64_t ZProcessSpawn(uint64_t elf_base, uint64_t elf_size) {
|
||||
ZProcessSpawnReq req{
|
||||
.elf_base = elf_base,
|
||||
.elf_size = elf_size,
|
||||
};
|
||||
return SysCall1(Z_PROCESS_SPAWN, &req);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "interrupt/timer.h"
|
||||
#include "loader/init_loader.h"
|
||||
#include "memory/kernel_heap.h"
|
||||
#include "memory/kernel_stack_manager.h"
|
||||
#include "memory/paging_util.h"
|
||||
#include "memory/physical_memory.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
|
@ -22,6 +23,11 @@ extern "C" void zion() {
|
|||
KernelHeap heap(0xFFFFFFFF'40000000, 0xFFFFFFFF'80000000);
|
||||
phys_mem::InitPhysicalMemoryManager();
|
||||
|
||||
dbgln("[boot] Memory allocations available now.");
|
||||
|
||||
dbgln("[boot] Init Kernel Stack Manager.");
|
||||
KernelStackManager::Init();
|
||||
|
||||
dbgln("[boot] Init syscalls.");
|
||||
InitSyscall();
|
||||
|
||||
|
|
Loading…
Reference in New Issue