2023-05-18 16:03:09 -07:00
|
|
|
#include "syscall/syscall.h"
|
|
|
|
|
|
|
|
#include <stdint.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-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-20 14:26:06 -07:00
|
|
|
#include "syscall/address_space.h"
|
2023-06-20 14:41:44 -07:00
|
|
|
#include "syscall/channel.h"
|
2023-06-20 14:26:06 -07:00
|
|
|
#include "syscall/memory_object.h"
|
2023-06-20 14:01:43 -07:00
|
|
|
#include "syscall/process.h"
|
2023-06-20 14:10:28 -07:00
|
|
|
#include "syscall/thread.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-06-12 20:56:25 -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-16 14:53:57 -07:00
|
|
|
z_err_t ValidateCap(const RefPtr<Capability>& cap, 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-17 01:24:07 -07:00
|
|
|
// FIXME: Check capability type before permissions, otherwise you can
|
|
|
|
// get a confusing error.
|
2023-06-07 08:50:08 -07:00
|
|
|
if (!cap->HasPermissions(permissions)) {
|
2023-06-17 01:24:07 -07:00
|
|
|
dbgln("PERM, has %x needs %x", cap->permissions(), permissions);
|
2023-06-07 08:50:08 -07:00
|
|
|
return Z_ERR_CAP_DENIED;
|
2023-05-30 23:55:42 -07:00
|
|
|
}
|
2023-06-07 08:50:08 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-17 00:31:02 -07:00
|
|
|
z_err_t PortCreate(ZPortCreateResp* resp) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port = MakeRefCounted<Port>();
|
2023-06-17 01:05:51 -07:00
|
|
|
resp->port_cap = proc.AddNewCapability(port, ZC_WRITE | ZC_READ);
|
|
|
|
return Z_OK;
|
2023-06-17 00:31:02 -07:00
|
|
|
}
|
|
|
|
|
2023-06-17 01:05:10 -07:00
|
|
|
z_err_t PortSend(ZPortSendReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
|
|
|
RET_ERR(ValidateCap(port_cap, ZC_WRITE));
|
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
|
|
|
RET_IF_NULL(port);
|
|
|
|
return port->Write(req->message);
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
z_err_t PortRecv(ZPortRecvReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
2023-06-16 14:53:57 -07:00
|
|
|
RET_ERR(ValidateCap(port_cap, ZC_READ));
|
2023-06-12 19:20:51 -07:00
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
2023-06-16 14:53:57 -07:00
|
|
|
RET_IF_NULL(port);
|
2023-06-12 19:20:51 -07:00
|
|
|
return port->Read(req->message);
|
|
|
|
}
|
|
|
|
|
2023-06-16 23:15:28 -07:00
|
|
|
z_err_t PortPoll(ZPortRecvReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
|
|
|
RET_ERR(ValidateCap(port_cap, ZC_READ));
|
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
|
|
|
RET_IF_NULL(port);
|
|
|
|
if (!port->HasMessages()) {
|
|
|
|
return Z_ERR_EMPTY;
|
|
|
|
}
|
|
|
|
return port->Read(req->message);
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
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.
|
|
|
|
return Z_ERR_UNIMPLEMENTED;
|
|
|
|
}
|
|
|
|
RefPtr<Port> port = MakeRefCounted<Port>();
|
2023-06-16 15:04:33 -07:00
|
|
|
resp->port_cap = proc.AddNewCapability(port, ZC_READ | ZC_WRITE);
|
2023-06-12 19:20:51 -07:00
|
|
|
RegisterPciPort(port);
|
2023-06-07 08:24:10 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-17 00:59:44 -07:00
|
|
|
z_err_t CapDuplicate(ZCapDuplicateReq* req, ZCapDuplicateResp* resp) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto cap = proc.GetCapability(req->cap);
|
|
|
|
if (!cap) {
|
|
|
|
return Z_ERR_CAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
resp->cap = proc.AddExistingCapability(cap);
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-20 13:50:18 -07:00
|
|
|
#define CASE(name) \
|
|
|
|
case kZion##name: \
|
|
|
|
return name(reinterpret_cast<Z##name##Req*>(req));
|
|
|
|
|
2023-06-07 08:50:08 -07:00
|
|
|
extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
2023-06-12 20:56:25 -07:00
|
|
|
RefPtr<Thread> thread = gScheduler->CurrentThread();
|
2023-05-29 13:06:43 -07:00
|
|
|
switch (call_id) {
|
2023-06-20 14:01:43 -07:00
|
|
|
// syscall/process.h
|
2023-06-20 13:50:18 -07:00
|
|
|
CASE(ProcessExit);
|
2023-06-20 14:01:43 -07:00
|
|
|
CASE(ProcessSpawn);
|
2023-06-20 14:10:28 -07:00
|
|
|
// syscall/thread.h
|
2023-06-20 13:50:18 -07:00
|
|
|
CASE(ThreadCreate);
|
2023-06-20 14:10:28 -07:00
|
|
|
CASE(ThreadStart);
|
|
|
|
CASE(ThreadExit);
|
2023-06-20 14:26:06 -07:00
|
|
|
// syscall/address_space.h
|
|
|
|
CASE(AddressSpaceMap);
|
|
|
|
// syscall/memory_object.h
|
|
|
|
CASE(MemoryObjectCreate);
|
|
|
|
CASE(MemoryObjectCreatePhysical);
|
|
|
|
CASE(MemoryObjectCreateContiguous);
|
|
|
|
CASE(TempPcieConfigObjectCreate);
|
2023-06-20 14:41:44 -07:00
|
|
|
// syscall/channel.h
|
|
|
|
CASE(ChannelCreate);
|
|
|
|
CASE(ChannelSend);
|
|
|
|
CASE(ChannelRecv);
|
2023-06-17 00:31:02 -07:00
|
|
|
case Z_PORT_CREATE:
|
|
|
|
return PortCreate(reinterpret_cast<ZPortCreateResp*>(resp));
|
2023-06-17 01:05:10 -07:00
|
|
|
case Z_PORT_SEND:
|
|
|
|
return PortSend(reinterpret_cast<ZPortSendReq*>(req));
|
2023-06-12 19:20:51 -07:00
|
|
|
case Z_PORT_RECV:
|
|
|
|
return PortRecv(reinterpret_cast<ZPortRecvReq*>(req));
|
2023-06-16 23:15:28 -07:00
|
|
|
case Z_PORT_POLL:
|
|
|
|
return PortPoll(reinterpret_cast<ZPortRecvReq*>(req));
|
2023-06-12 19:20:51 -07:00
|
|
|
case Z_IRQ_REGISTER:
|
|
|
|
return IrqRegister(reinterpret_cast<ZIrqRegisterReq*>(req),
|
|
|
|
reinterpret_cast<ZIrqRegisterResp*>(resp));
|
2023-06-17 00:59:44 -07:00
|
|
|
case Z_CAP_DUPLICATE:
|
|
|
|
return CapDuplicate(reinterpret_cast<ZCapDuplicateReq*>(req),
|
|
|
|
reinterpret_cast<ZCapDuplicateResp*>(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
|
|
|
}
|