From 2ea1f906906259dea0704cffe527df8982d9db0a Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sat, 17 Jun 2023 00:07:58 -0700 Subject: [PATCH] [zion] Add a direct port write for the kernel --- zion/loader/init_loader.cpp | 18 ++---------------- zion/object/port.cpp | 17 +++++++++++++++++ zion/object/port.h | 2 ++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/zion/loader/init_loader.cpp b/zion/loader/init_loader.cpp index 1e3dcc8..cc57b7b 100644 --- a/zion/loader/init_loader.cpp +++ b/zion/loader/init_loader.cpp @@ -130,25 +130,11 @@ void LoadInitProgram() { prog2_vmmo->CopyBytesToObject(reinterpret_cast(prog2.address), prog2.size); - // TODO: Probably add a way for the kernel to write caps directly rather than - // by installing them first. - uint64_t vmmo_cap = - gScheduler->CurrentProcess().AddNewCapability(prog2_vmmo, ZC_WRITE); - auto port = MakeRefCounted(); uint64_t port_cap = proc->AddNewCapability(port, ZC_READ | ZC_WRITE); - uint64_t vmmo_id = Z_BOOT_DENALI_VMMO; - ZMessage vmmo_msg{ - .type = 0, - .num_bytes = 8, - .bytes = reinterpret_cast(&vmmo_id), - .num_caps = 1, - .caps = &vmmo_cap, - }; - if (port->Write(vmmo_msg) != Z_OK) { - panic("Failed to write cap"); - } + port->WriteKernel(Z_BOOT_DENALI_VMMO, + MakeRefCounted(prog2_vmmo, ZC_READ | ZC_WRITE)); proc->CreateThread()->Start(entry, port_cap, 0); } diff --git a/zion/object/port.cpp b/zion/object/port.cpp index 25d53fd..e649bca 100644 --- a/zion/object/port.cpp +++ b/zion/object/port.cpp @@ -74,6 +74,23 @@ z_err_t Port::Read(ZMessage& msg) { return Z_OK; } +void Port::WriteKernel(uint64_t init, RefPtr cap) { + MutexHolder h(mutex_); + + auto msg = MakeShared(); + msg->type = 0; + msg->bytes = new uint8_t[8]; + msg->num_bytes = sizeof(init); + + uint8_t* data = reinterpret_cast(&init); + for (uint8_t i = 0; i < sizeof(init); i++) { + msg->bytes[i] = data[i]; + } + msg->caps.PushBack(cap); + + pending_messages_.PushBack(msg); +} + bool Port::HasMessages() { MutexHolder h(mutex_); return pending_messages_.size() != 0; diff --git a/zion/object/port.h b/zion/object/port.h index 78fc3e7..baff201 100644 --- a/zion/object/port.h +++ b/zion/object/port.h @@ -24,6 +24,8 @@ class Port : public KernelObject { z_err_t Write(const ZMessage& msg); z_err_t Read(ZMessage& msg); + void WriteKernel(uint64_t init, RefPtr cap); + bool HasMessages(); private: