2023-05-18 16:03:09 -07:00
|
|
|
#include "syscall/syscall.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-06-07 22:45:42 -07:00
|
|
|
#include "boot/acpi.h"
|
2023-06-07 13:40:36 -07:00
|
|
|
#include "common/msr.h"
|
2023-05-18 16:03:09 -07:00
|
|
|
#include "debug/debug.h"
|
2023-05-29 13:06:43 -07:00
|
|
|
#include "include/zcall.h"
|
2023-05-30 23:55:42 -07:00
|
|
|
#include "include/zerrors.h"
|
2023-06-12 19:20:51 -07:00
|
|
|
#include "interrupt/interrupt.h"
|
|
|
|
#include "memory/physical_memory.h"
|
2023-06-07 08:24:10 -07:00
|
|
|
#include "object/channel.h"
|
2023-06-12 19:20:51 -07:00
|
|
|
#include "object/port.h"
|
2023-06-06 20:18:53 -07:00
|
|
|
#include "object/process.h"
|
2023-05-30 20:55:03 -07:00
|
|
|
#include "scheduler/process_manager.h"
|
2023-05-18 16:03:09 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
2023-06-07 00:04:53 -07:00
|
|
|
#include "usr/zcall_internal.h"
|
2023-05-18 16:03:09 -07:00
|
|
|
|
|
|
|
#define EFER 0xC0000080
|
|
|
|
#define STAR 0xC0000081
|
|
|
|
#define LSTAR 0xC0000082
|
|
|
|
|
|
|
|
extern "C" void syscall_enter();
|
|
|
|
|
|
|
|
// Used by syscall_enter.s
|
|
|
|
extern "C" uint64_t GetKernelRsp() {
|
2023-05-29 23:48:32 -07:00
|
|
|
return gScheduler->CurrentThread().Rsp0Start();
|
2023-05-18 16:03:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitSyscall() {
|
|
|
|
uint64_t efer_val = GetMSR(EFER);
|
|
|
|
efer_val |= 1;
|
|
|
|
SetMSR(EFER, efer_val);
|
|
|
|
if (GetMSR(EFER) != efer_val) {
|
|
|
|
panic("Failed to set EFER MSR");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t star_val = GetMSR(STAR);
|
|
|
|
uint64_t kernel_cs = 0x8;
|
2023-06-07 00:04:53 -07:00
|
|
|
// Due to the ability to jump from a 64 bit kernel into compatibility mode,
|
|
|
|
// this will actually use CS 0x20 (and SS 0x18).
|
|
|
|
// See AMD Manual 3.4 instruction SYSRET for more info.
|
|
|
|
uint64_t user_cs = 0x18;
|
2023-05-18 16:03:09 -07:00
|
|
|
star_val |= (kernel_cs << 32) | (user_cs << 48);
|
|
|
|
SetMSR(STAR, star_val);
|
|
|
|
SetMSR(LSTAR, reinterpret_cast<uint64_t>(syscall_enter));
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ValidateCap(const RefPtr<Capability>& cap, Capability::Type type,
|
|
|
|
uint64_t permissions) {
|
2023-06-07 06:21:36 -07:00
|
|
|
if (!cap) {
|
2023-06-07 08:50:08 -07:00
|
|
|
return Z_ERR_CAP_NOT_FOUND;
|
2023-05-30 23:55:42 -07:00
|
|
|
}
|
2023-06-07 08:50:08 -07:00
|
|
|
if (!cap->CheckType(type)) {
|
|
|
|
return Z_ERR_CAP_TYPE;
|
2023-05-30 23:55:42 -07:00
|
|
|
}
|
2023-06-07 08:50:08 -07:00
|
|
|
if (!cap->HasPermissions(permissions)) {
|
|
|
|
return Z_ERR_CAP_DENIED;
|
2023-05-30 23:55:42 -07:00
|
|
|
}
|
2023-06-07 08:50:08 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
z_err_t ProcessSpawn(ZProcessSpawnReq* req, ZProcessSpawnResp* resp) {
|
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
|
|
|
auto cap = curr_proc.GetCapability(req->proc_cap);
|
|
|
|
RET_ERR(ValidateCap(cap, Capability::PROCESS, ZC_PROC_SPAWN_PROC));
|
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
RefPtr<Process> proc = Process::Create();
|
2023-05-30 20:55:03 -07:00
|
|
|
gProcMan->InsertProcess(proc);
|
2023-06-07 00:04:53 -07:00
|
|
|
|
|
|
|
resp->proc_cap = curr_proc.AddCapability(proc);
|
2023-06-07 00:30:26 -07:00
|
|
|
resp->vmas_cap = curr_proc.AddCapability(proc->vmas());
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-07 08:24:10 -07:00
|
|
|
if (req->bootstrap_cap != 0) {
|
|
|
|
auto cap = curr_proc.ReleaseCapability(req->bootstrap_cap);
|
|
|
|
if (!cap) {
|
2023-06-07 08:50:08 -07:00
|
|
|
return Z_ERR_CAP_NOT_FOUND;
|
2023-06-07 08:24:10 -07:00
|
|
|
}
|
|
|
|
// FIXME: Check permissions.
|
|
|
|
resp->bootstrap_cap = proc->AddCapability(cap);
|
|
|
|
}
|
|
|
|
|
2023-06-07 00:04:53 -07:00
|
|
|
return Z_OK;
|
2023-05-30 20:55:03 -07:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) {
|
2023-06-06 16:24:03 -07:00
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
|
|
|
auto cap = curr_proc.GetCapability(req->proc_cap);
|
2023-06-07 08:50:08 -07:00
|
|
|
RET_ERR(ValidateCap(cap, Capability::PROCESS, ZC_PROC_SPAWN_THREAD));
|
2023-06-06 16:24:03 -07:00
|
|
|
|
2023-06-07 00:04:53 -07:00
|
|
|
auto parent_proc = cap->obj<Process>();
|
|
|
|
auto thread = parent_proc->CreateThread();
|
2023-06-06 16:24:03 -07:00
|
|
|
resp->thread_cap = curr_proc.AddCapability(thread);
|
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ThreadStart(ZThreadStartReq* req) {
|
2023-06-06 16:24:03 -07:00
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
|
|
|
auto cap = curr_proc.GetCapability(req->thread_cap);
|
2023-06-07 08:50:08 -07:00
|
|
|
RET_ERR(ValidateCap(cap, Capability::THREAD, ZC_WRITE));
|
2023-06-06 16:24:03 -07:00
|
|
|
|
2023-06-07 00:04:53 -07:00
|
|
|
auto thread = cap->obj<Thread>();
|
2023-06-06 16:24:03 -07:00
|
|
|
// FIXME: validate entry point is in user space.
|
2023-06-07 00:04:53 -07:00
|
|
|
thread->Start(req->entry, req->arg1, req->arg2);
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) {
|
2023-06-07 00:04:53 -07:00
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
2023-06-07 00:30:26 -07:00
|
|
|
auto vmas_cap = curr_proc.GetCapability(req->vmas_cap);
|
|
|
|
auto vmmo_cap = curr_proc.GetCapability(req->vmmo_cap);
|
2023-06-07 08:50:08 -07:00
|
|
|
RET_ERR(ValidateCap(vmas_cap, Capability::ADDRESS_SPACE, ZC_WRITE));
|
|
|
|
RET_ERR(ValidateCap(vmmo_cap, Capability::MEMORY_OBJECT, ZC_WRITE));
|
|
|
|
|
2023-06-07 00:30:26 -07:00
|
|
|
auto vmas = vmas_cap->obj<AddressSpace>();
|
|
|
|
auto vmmo = vmmo_cap->obj<MemoryObject>();
|
2023-06-07 00:04:53 -07:00
|
|
|
// FIXME: Validation necessary.
|
2023-06-07 00:30:26 -07:00
|
|
|
if (req->vmas_offset != 0) {
|
|
|
|
vmas->MapInMemoryObject(req->vmas_offset, vmmo);
|
|
|
|
resp->vaddr = req->vmas_offset;
|
2023-06-07 00:04:53 -07:00
|
|
|
} else {
|
2023-06-07 00:30:26 -07:00
|
|
|
resp->vaddr = vmas->MapInMemoryObject(vmmo);
|
2023-06-07 00:04:53 -07:00
|
|
|
}
|
2023-06-07 07:15:25 -07:00
|
|
|
return Z_OK;
|
2023-06-07 00:04:53 -07:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t MemoryObjectCreate(ZMemoryObjectCreateReq* req,
|
|
|
|
ZMemoryObjectCreateResp* resp) {
|
2023-06-07 00:04:53 -07:00
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
2023-06-07 00:30:26 -07:00
|
|
|
resp->vmmo_cap =
|
2023-06-07 00:04:53 -07:00
|
|
|
curr_proc.AddCapability(MakeRefCounted<MemoryObject>(req->size));
|
|
|
|
return Z_OK;
|
2023-06-06 16:24:03 -07:00
|
|
|
}
|
|
|
|
|
2023-06-08 02:36:47 -07:00
|
|
|
z_err_t MemoryObjectCreatePhysical(ZMemoryObjectCreatePhysicalReq* req,
|
2023-06-12 19:20:51 -07:00
|
|
|
ZMemoryObjectCreatePhysicalResp* resp) {
|
2023-06-08 02:36:47 -07:00
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
2023-06-12 19:20:51 -07:00
|
|
|
uint64_t paddr = req->paddr;
|
|
|
|
if (paddr == 0) {
|
2023-06-12 20:55:53 -07:00
|
|
|
paddr = phys_mem::AllocateContinuous(((req->size - 1) / 0x1000) + 1);
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
|
|
|
auto vmmo_ref = MakeRefCounted<FixedMemoryObject>(paddr, req->size);
|
2023-06-08 02:36:47 -07:00
|
|
|
resp->vmmo_cap =
|
|
|
|
curr_proc.AddCapability(StaticCastRefPtr<MemoryObject>(vmmo_ref));
|
2023-06-12 19:20:51 -07:00
|
|
|
resp->paddr = paddr;
|
2023-06-08 02:36:47 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:45:42 -07:00
|
|
|
z_err_t TempPcieConfigObjectCreate(ZTempPcieConfigObjectCreateResp* resp) {
|
|
|
|
auto& curr_proc = gScheduler->CurrentProcess();
|
|
|
|
uint64_t pci_base, pci_size;
|
|
|
|
RET_ERR(GetPciExtendedConfiguration(&pci_base, &pci_size));
|
|
|
|
auto vmmo_ref = MakeRefCounted<FixedMemoryObject>(pci_base, pci_size);
|
|
|
|
resp->vmmo_cap =
|
|
|
|
curr_proc.AddCapability(StaticCastRefPtr<MemoryObject>(vmmo_ref));
|
|
|
|
resp->vmmo_size = pci_size;
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ChannelCreate(ZChannelCreateResp* resp) {
|
2023-06-07 08:24:10 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_pair = Channel::CreateChannelPair();
|
|
|
|
resp->chan_cap1 = proc.AddCapability(chan_pair.first());
|
|
|
|
resp->chan_cap2 = proc.AddCapability(chan_pair.second());
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ChannelSend(ZChannelSendReq* req) {
|
2023-06-07 08:24:10 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_cap = proc.GetCapability(req->chan_cap);
|
2023-06-07 08:50:08 -07:00
|
|
|
RET_ERR(ValidateCap(chan_cap, Capability::CHANNEL, ZC_WRITE));
|
|
|
|
|
2023-06-07 08:24:10 -07:00
|
|
|
auto chan = chan_cap->obj<Channel>();
|
|
|
|
chan->Write(req->message);
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
z_err_t ChannelRecv(ZChannelRecvReq* req) {
|
2023-06-07 08:24:10 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_cap = proc.GetCapability(req->chan_cap);
|
2023-06-07 08:50:08 -07:00
|
|
|
RET_ERR(ValidateCap(chan_cap, Capability::CHANNEL, ZC_READ));
|
|
|
|
|
2023-06-07 08:24:10 -07:00
|
|
|
auto chan = chan_cap->obj<Channel>();
|
2023-06-12 19:20:51 -07:00
|
|
|
return chan->Read(req->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
z_err_t PortRecv(ZPortRecvReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
dbgln("Port cap %u", req->port_cap);
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
|
|
|
RET_ERR(ValidateCap(port_cap, Capability::PORT, ZC_READ));
|
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
|
|
|
return port->Read(req->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
z_err_t IrqRegister(ZIrqRegisterReq* req, ZIrqRegisterResp* resp) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
if (req->irq_num != Z_IRQ_PCI_BASE) {
|
|
|
|
// FIXME: Don't hardcode this nonsense.
|
|
|
|
dbgln("Irq %x", req->irq_num);
|
|
|
|
return Z_ERR_UNIMPLEMENTED;
|
|
|
|
}
|
|
|
|
RefPtr<Port> port = MakeRefCounted<Port>();
|
|
|
|
resp->port_cap = proc.AddCapability(port);
|
|
|
|
dbgln("Port cap %u", resp->port_cap);
|
|
|
|
RegisterPciPort(port);
|
2023-06-07 08:24:10 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
2023-05-29 23:48:32 -07:00
|
|
|
Thread& thread = gScheduler->CurrentThread();
|
2023-05-29 13:06:43 -07:00
|
|
|
switch (call_id) {
|
2023-06-06 15:01:31 -07:00
|
|
|
case Z_PROCESS_EXIT:
|
|
|
|
// FIXME: kill process here.
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Exit code: %u", req);
|
2023-05-29 13:51:00 -07:00
|
|
|
thread.Exit();
|
|
|
|
panic("Returned from thread exit");
|
|
|
|
break;
|
2023-05-30 20:55:03 -07:00
|
|
|
case Z_PROCESS_SPAWN:
|
2023-06-07 00:04:53 -07:00
|
|
|
return ProcessSpawn(reinterpret_cast<ZProcessSpawnReq*>(req),
|
|
|
|
reinterpret_cast<ZProcessSpawnResp*>(resp));
|
2023-06-06 16:24:03 -07:00
|
|
|
case Z_THREAD_CREATE:
|
|
|
|
return ThreadCreate(reinterpret_cast<ZThreadCreateReq*>(req),
|
|
|
|
reinterpret_cast<ZThreadCreateResp*>(resp));
|
|
|
|
case Z_THREAD_START:
|
|
|
|
return ThreadStart(reinterpret_cast<ZThreadStartReq*>(req));
|
|
|
|
case Z_THREAD_EXIT:
|
|
|
|
thread.Exit();
|
|
|
|
panic("Returned from thread exit");
|
|
|
|
break;
|
2023-06-07 00:04:53 -07:00
|
|
|
|
|
|
|
case Z_ADDRESS_SPACE_MAP:
|
|
|
|
return AddressSpaceMap(reinterpret_cast<ZAddressSpaceMapReq*>(req),
|
|
|
|
reinterpret_cast<ZAddressSpaceMapResp*>(resp));
|
|
|
|
case Z_MEMORY_OBJECT_CREATE:
|
|
|
|
return MemoryObjectCreate(
|
|
|
|
reinterpret_cast<ZMemoryObjectCreateReq*>(req),
|
|
|
|
reinterpret_cast<ZMemoryObjectCreateResp*>(resp));
|
2023-06-08 02:36:47 -07:00
|
|
|
case Z_MEMORY_OBJECT_CREATE_PHYSICAL:
|
|
|
|
return MemoryObjectCreatePhysical(
|
|
|
|
reinterpret_cast<ZMemoryObjectCreatePhysicalReq*>(req),
|
2023-06-12 19:20:51 -07:00
|
|
|
reinterpret_cast<ZMemoryObjectCreatePhysicalResp*>(resp));
|
2023-06-07 22:45:42 -07:00
|
|
|
case Z_TEMP_PCIE_CONFIG_OBJECT_CREATE:
|
|
|
|
return TempPcieConfigObjectCreate(
|
|
|
|
reinterpret_cast<ZTempPcieConfigObjectCreateResp*>(resp));
|
2023-06-07 08:24:10 -07:00
|
|
|
case Z_CHANNEL_CREATE:
|
|
|
|
return ChannelCreate(reinterpret_cast<ZChannelCreateResp*>(resp));
|
|
|
|
case Z_CHANNEL_SEND:
|
|
|
|
return ChannelSend(reinterpret_cast<ZChannelSendReq*>(req));
|
|
|
|
case Z_CHANNEL_RECV:
|
|
|
|
return ChannelRecv(reinterpret_cast<ZChannelRecvReq*>(req));
|
2023-06-12 19:20:51 -07:00
|
|
|
case Z_PORT_RECV:
|
|
|
|
return PortRecv(reinterpret_cast<ZPortRecvReq*>(req));
|
|
|
|
case Z_IRQ_REGISTER:
|
|
|
|
return IrqRegister(reinterpret_cast<ZIrqRegisterReq*>(req),
|
|
|
|
reinterpret_cast<ZIrqRegisterResp*>(resp));
|
2023-06-07 00:04:53 -07:00
|
|
|
case Z_DEBUG_PRINT:
|
|
|
|
dbgln("[Debug] %s", req);
|
2023-06-07 07:15:25 -07:00
|
|
|
return Z_OK;
|
2023-06-07 00:04:53 -07:00
|
|
|
break;
|
2023-05-29 13:06:43 -07:00
|
|
|
default:
|
2023-06-06 16:24:03 -07:00
|
|
|
panic("Unhandled syscall number: %x", call_id);
|
2023-05-29 13:06:43 -07:00
|
|
|
}
|
2023-06-07 08:50:08 -07:00
|
|
|
UNREACHABLE
|
2023-05-18 16:03:09 -07:00
|
|
|
}
|