Compare commits

...

4 Commits

Author SHA1 Message Date
Drew Galbraith 69b5cd001f Add a skeleton framework for capabilities.
Use the first capability to spawn a child process.
2023-05-30 23:55:42 -07:00
Drew Galbraith 09b8136ef9 Fix formatting on panic 2023-05-30 23:52:50 -07:00
Drew Galbraith 69c73a9386 Add a qemu mode that dumps interrupt info. 2023-05-30 23:22:52 -07:00
Drew Galbraith 9e3df0ccd0 Add pid/tid to all log messages 2023-05-30 23:22:29 -07:00
13 changed files with 133 additions and 14 deletions

View File

@ -12,6 +12,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS True)
add_subdirectory(zion)
add_subdirectory(sys)
set(QEMU_CMD qemu-system-x86_64 -d guest_errors -m 1G -serial stdio -hda disk.img)
add_custom_command(
OUTPUT disk.img
COMMAND sudo sh ../scripts/build_image.sh disk.img
@ -20,11 +22,16 @@ add_custom_command(
)
add_custom_target(qemu
COMMAND qemu-system-x86_64 -d guest_errors -m 1G -serial stdio -hda disk.img
COMMAND ${QEMU_CMD}
DEPENDS disk.img
USES_TERMINAL)
add_custom_target(qemu-dbg
COMMAND qemu-system-x86_64 -d guest_errors -m 1G -serial stdio -hda disk.img -S -s
COMMAND ${QEMU_CMD} -S -s
DEPENDS disk.img
USES_TERMINAL)
add_custom_target(qemu-int
COMMAND ${QEMU_CMD} -d int
DEPENDS disk.img
USES_TERMINAL)

View File

@ -1,11 +1,16 @@
#include "zcall.h"
#include "zerrors.h"
constexpr uint64_t prog2 = 0x00000020'00000000;
int main() {
ZDebug("Testing");
ZProcessSpawn(prog2, 0x1000);
ZDebug("Return");
uint64_t err = ZProcessSpawn(0x100, prog2, 0x1000);
if (err != Z_OK) {
ZDebug("Error");
} else {
ZDebug("Return");
}
return 0;
}

View File

@ -0,0 +1,44 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
class Process;
class Capability {
public:
enum Type {
UNDEFINED,
PROCESS,
};
Capability(void* obj, Type type, uint64_t id, uint64_t permissions)
: obj_(obj), type_(type), id_(id), permissions_(permissions) {}
template <typename T>
T& obj();
uint64_t id() { return id_; }
bool CheckType(Type type) { return type_ == type; }
uint64_t permissions() { return permissions_; }
bool HasPermissions(uint64_t requested) {
return (permissions_ & requested) == requested;
}
private:
// FIXME: This should somehow be a shared ptr to keep the object alive.
void* obj_;
Type type_;
uint64_t id_;
uint64_t permissions_;
};
template <class Process>
Process& Capability::obj() {
if (type_ != PROCESS) {
panic("Accessing %u cap as object.", type_);
}
return *static_cast<Process*>(obj_);
}

View File

@ -1,6 +1,7 @@
#include "debug/debug.h"
#include "common/port.h"
#include "scheduler/scheduler.h"
#define COM1 0x3f8
@ -73,7 +74,14 @@ void MemToStr(uint64_t u, char* str) {
str[18] = '\0';
}
void dbgln_internal(const char* fmt, va_list args) {
void AddProcPrefix() {
if (gScheduler != nullptr) {
auto& t = gScheduler->CurrentThread();
dbg("[%u.%u] ", t.pid(), t.tid());
}
}
void dbg_internal(const char* fmt, va_list args) {
for (; *fmt != '\0'; fmt++) {
if (*fmt != '%') {
dbgputchar(*fmt);
@ -120,24 +128,34 @@ void dbgln_internal(const char* fmt, va_list args) {
}
}
}
dbgputchar('\n');
}
} // namespace
void dbgln(const char* fmt, ...) {
void dbg(const char* fmt, ...) {
va_list arg;
va_start(arg, fmt);
dbgln_internal(fmt, arg);
dbg_internal(fmt, arg);
va_end(arg);
}
void dbgln(const char* fmt, ...) {
AddProcPrefix();
va_list arg;
va_start(arg, fmt);
dbg_internal(fmt, arg);
va_end(arg);
dbgputchar('\n');
}
void panic(const char* fmt, ...) {
asm volatile("cli");
AddProcPrefix();
va_list arg;
va_start(arg, fmt);
dbgln_internal(fmt, arg);
dbg_internal(fmt, arg);
va_end(arg);
dbgputchar('\n');
dbgln("PANIC");
while (1)
;

View File

@ -2,5 +2,6 @@
#include <stdarg.h>
void dbg(const char* fmt, ...);
void dbgln(const char* str, ...);
void panic(const char* str, ...);

3
zion/include/cap_types.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#define ZC_PROC_SPAWN_CHILD 0x1

View File

@ -12,8 +12,9 @@ uint64_t ZDebug(const char* message);
// TODO: Move structs into an internal header.
struct ZProcessSpawnReq {
uint64_t cap_id;
uint64_t elf_base;
uint64_t elf_size;
};
uint64_t ZProcessSpawn(uint64_t elf_base, uint64_t elf_size);
uint64_t ZProcessSpawn(uint64_t cap_id, uint64_t elf_base, uint64_t elf_size);

6
zion/include/zerrors.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#define Z_OK 0x0
#define ZE_NOT_FOUND 0x1
#define ZE_INVALID 0x2
#define ZE_DENIED 0x4

View File

@ -1,6 +1,7 @@
#include "scheduler/process.h"
#include "debug/debug.h"
#include "include/cap_types.h"
#include "memory/paging_util.h"
#include "memory/physical_memory.h"
#include "scheduler/scheduler.h"
@ -25,6 +26,8 @@ Process::Process() : id_(gNextId++), state_(RUNNING) {}
void Process::CreateThread(uint64_t entry) {
Thread* thread = new Thread(*this, next_thread_id_++, entry);
threads_.PushBack(thread);
caps_.PushBack(new Capability(this, Capability::PROCESS, next_cap_id_++,
ZC_PROC_SPAWN_CHILD));
gScheduler->Enqueue(thread);
}
@ -50,3 +53,15 @@ void Process::CheckState() {
}
state_ = FINISHED;
}
SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
auto iter = caps_.begin();
while (iter != caps_.end()) {
if (iter->id() == cid) {
return *iter;
}
++iter;
}
dbgln("Bad cap access");
return {};
}

View File

@ -2,6 +2,7 @@
#include <stdint.h>
#include "capability/capability.h"
#include "lib/linked_list.h"
#include "lib/shared_ptr.h"
#include "memory/virtual_memory.h"
@ -26,6 +27,7 @@ class Process {
void CreateThread(uint64_t entry);
SharedPtr<Thread> GetThread(uint64_t tid);
SharedPtr<Capability> GetCapability(uint64_t cid);
// Checks the state of all child threads and transitions to
// finished if all have finished.
void CheckState();
@ -39,6 +41,8 @@ class Process {
State state_;
uint64_t next_thread_id_ = 0;
uint64_t next_cap_id_ = 0x100;
LinkedList<SharedPtr<Thread>> threads_;
LinkedList<SharedPtr<Capability>> caps_;
};

View File

@ -41,14 +41,14 @@ Thread::Thread(Process& proc, uint64_t tid, uint64_t entry)
uint64_t Thread::pid() const { return process_.id(); }
void Thread::Init() {
dbgln("[%u.%u] thread start.", pid(), id_);
dbgln("Thread start.", pid(), id_);
uint64_t rsp = process_.vmm().AllocateUserStack();
SetRsp0(rsp0_start_);
jump_user_space(rip_, rsp);
}
void Thread::Exit() {
dbgln("[%u.%u] Exiting", pid(), id_);
dbgln("Exiting", pid(), id_);
state_ = FINISHED;
process_.CheckState();
gScheduler->Yield();

View File

@ -3,7 +3,9 @@
#include <stdint.h>
#include "debug/debug.h"
#include "include/cap_types.h"
#include "include/zcall.h"
#include "include/zerrors.h"
#include "loader/elf_loader.h"
#include "scheduler/process.h"
#include "scheduler/process_manager.h"
@ -57,6 +59,18 @@ void InitSyscall() {
}
uint64_t ProcessSpawn(ZProcessSpawnReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->cap_id);
if (cap.empty()) {
return ZE_NOT_FOUND;
}
if (!cap->CheckType(Capability::PROCESS)) {
return ZE_INVALID;
}
if (!cap->HasPermissions(ZC_PROC_SPAWN_CHILD)) {
return ZE_DENIED;
}
dbgln("Proc spawn: %u:%u", req->elf_base, req->elf_size);
SharedPtr<Process> proc = MakeShared<Process>();
gProcMan->InsertProcess(proc);
@ -73,7 +87,7 @@ extern "C" uint64_t SyscallHandler(uint64_t call_id, char* message) {
panic("Returned from thread exit");
break;
case Z_DEBUG_PRINT:
dbgln("[%u.%u] [Debug] %s", thread.pid(), thread.tid(), message);
dbgln("[Debug] %s", message);
break;
case Z_PROCESS_SPAWN:
return ProcessSpawn(reinterpret_cast<ZProcessSpawnReq*>(message));

View File

@ -12,8 +12,9 @@ uint64_t ZDebug(const char* message) {
return SysCall1(Z_DEBUG_PRINT, message);
}
uint64_t ZProcessSpawn(uint64_t elf_base, uint64_t elf_size) {
uint64_t ZProcessSpawn(uint64_t cap_id, uint64_t elf_base, uint64_t elf_size) {
ZProcessSpawnReq req{
.cap_id = cap_id,
.elf_base = elf_base,
.elf_size = elf_size,
};