Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
program_syscalls.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cstdint>
12#include <vector>
13
14#include "contur/core/types.h"
15
16#include "contur/arch/block.h"
20#include "contur/io/io_types.h"
23
24namespace contur {
25
36 inline void emitOpen(std::vector<Block> &code, RegisterValue resourceId, IoResourceKind kind, OpenMode mode)
37 {
38 constexpr auto kSyscall = SYSCALL_ID_REGISTER;
39 constexpr auto kArg0 = SYSCALL_ARG_REGISTERS[0];
40 constexpr auto kArg1 = SYSCALL_ARG_REGISTERS[1];
41 constexpr auto kArg2 = SYSCALL_ARG_REGISTERS[2];
42 constexpr auto kArg3 = SYSCALL_ARG_REGISTERS[3];
43
44 code.push_back(movImm(kSyscall, static_cast<RegisterValue>(SyscallId::Open)));
45 code.push_back(movImm(kArg0, resourceId));
46 code.push_back(movImm(kArg1, static_cast<RegisterValue>(kind)));
47 code.push_back(movImm(kArg2, static_cast<RegisterValue>(mode)));
48 code.push_back(movImm(kArg3, 0));
49 code.push_back(interrupt(Interrupt::SystemCall));
50 }
51
59 inline void emitRead(std::vector<Block> &code, std::uint8_t fdReg)
60 {
61 constexpr auto kSyscall = SYSCALL_ID_REGISTER;
62 constexpr auto kArg0 = SYSCALL_ARG_REGISTERS[0];
63
64 code.push_back(movImm(kSyscall, static_cast<RegisterValue>(SyscallId::Read)));
65 code.push_back(movReg(kArg0, fdReg));
66 code.push_back(interrupt(Interrupt::SystemCall));
67 }
68
76 inline void emitWrite(std::vector<Block> &code, std::uint8_t fdReg, std::uint8_t valReg)
77 {
78 constexpr auto kSyscall = SYSCALL_ID_REGISTER;
79 constexpr auto kArg0 = SYSCALL_ARG_REGISTERS[0];
80 constexpr auto kArg1 = SYSCALL_ARG_REGISTERS[1];
81
82 code.push_back(movImm(kSyscall, static_cast<RegisterValue>(SyscallId::Write)));
83 code.push_back(movReg(kArg0, fdReg));
84 code.push_back(movReg(kArg1, valReg));
85 code.push_back(interrupt(Interrupt::SystemCall));
86 }
87
92 inline void emitClose(std::vector<Block> &code, std::uint8_t fdReg)
93 {
94 constexpr auto kSyscall = SYSCALL_ID_REGISTER;
95 constexpr auto kArg0 = SYSCALL_ARG_REGISTERS[0];
96
97 code.push_back(movImm(kSyscall, static_cast<RegisterValue>(SyscallId::Close)));
98 code.push_back(movReg(kArg0, fdReg));
99 code.push_back(interrupt(Interrupt::SystemCall));
100 }
101
102} // namespace contur
Block — the fundamental unit of simulated memory.
File descriptor types and descriptor-table abstraction.
Interrupt enum class — hardware and software interrupt codes.
Common I/O descriptor and resource-type definitions.
Definition block.h:15
@ SystemCall
Software interrupt — syscall entry.
Definition interrupt.h:21
Block interrupt(Interrupt code) noexcept
Raises a software interrupt.
Block movReg(std::uint8_t dst, std::uint8_t src) noexcept
Copies the value of one register into another.
void emitClose(std::vector< Block > &code, std::uint8_t fdReg)
Emits instructions to close a kernel I/O descriptor.
constexpr std::uint8_t SYSCALL_ID_REGISTER
Register used to pass syscall id and receive return value.
void emitRead(std::vector< Block > &code, std::uint8_t fdReg)
Emits instructions to read one value from a kernel I/O descriptor.
Block movImm(std::uint8_t reg, RegisterValue value) noexcept
Loads an immediate value into a register.
OpenMode
Open mode bit flags.
@ Write
Write bytes to a kernel-managed sink.
Definition syscall_ids.h:16
@ Read
Read bytes from a kernel-managed source.
Definition syscall_ids.h:18
@ Open
Open file/device.
Definition syscall_ids.h:26
@ Close
Close file/device.
Definition syscall_ids.h:28
void emitWrite(std::vector< Block > &code, std::uint8_t fdReg, std::uint8_t valReg)
Emits instructions to write one value to a kernel I/O descriptor.
IoResourceKind
Resource kinds supported by the kernel I/O layer.
Definition io_types.h:31
constexpr std::array< std::uint8_t, 4 > SYSCALL_ARG_REGISTERS
Registers used to pass syscall arguments.
std::int32_t RegisterValue
Value stored in a CPU register.
Definition types.h:21
void emitOpen(std::vector< Block > &code, RegisterValue resourceId, IoResourceKind kind, OpenMode mode)
Emits instructions to open a kernel I/O resource.
Inline Block construction helpers for assembling bytecode programs.
Register conventions for system call dispatch.
Syscall IDs for the user-kernel boundary.
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...