From 204d2c9a5bfdc8892a7cba42760df4948704b879 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Mon, 18 Apr 2022 20:25:35 +0200 Subject: [PATCH] Syscalls: cleaned up formatting and added instructions for extensions --- docs/syscalls.md | 3 +++ riscemu/syscall.py | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/syscalls.md b/docs/syscalls.md index e415f09..9cf3e64 100644 --- a/docs/syscalls.md +++ b/docs/syscalls.md @@ -45,3 +45,6 @@ Requires flag `--scall-fs` to be set to True * `a0`: file descriptor to close * `return in a0`: 0 if closed correctly or -1 +# Extending these syscalls + +You can implement your own syscall by adding its code to the `SYSCALLS` dict in the [riscemu/syscalls.py](./riscemu/syscalls.py) file, creating a mapping of a syscall code to a name, and then implementing that syscall name in the SyscallInterface class further down that same file. Each syscall method should have the same signature: `read(self, scall: Syscall)`. The `Syscall` object gives you access to the cpu, through which you can access registers and memory. You can look at the `read` or `write` syscalls for further examples. diff --git a/riscemu/syscall.py b/riscemu/syscall.py index c7a1c3c..7ac95d6 100644 --- a/riscemu/syscall.py +++ b/riscemu/syscall.py @@ -8,10 +8,9 @@ import sys from dataclasses import dataclass from typing import Dict, IO -from .helpers import * - -if typing.TYPE_CHECKING: - from riscemu.CPU import UserModeCPU +from .colors import FMT_SYSCALL, FMT_NONE +from .types import Int32, CPU +from .types.exceptions import InvalidSyscallException SYSCALLS = { 63: 'read', @@ -20,7 +19,13 @@ SYSCALLS = { 1024: 'open', 1025: 'close', } -"""All available syscalls (mapped id->name)""" +""" +This dict contains a mapping for all available syscalls (code->name) + +If you wish to add a syscall to the built-in system, you can extend this +dictionary and implement a method with the same name on the SyscallInterface +class. +""" OPEN_MODES = { 0: 'rb', @@ -39,7 +44,7 @@ class Syscall: """ id: int """The syscall number (e.g. 64 - write)""" - cpu: 'UserModeCPU' + cpu: CPU """The CPU object that created the syscall""" @property @@ -95,7 +100,7 @@ class SyscallInterface: addr = scall.cpu.regs.get('a1').unsigned_value size = scall.cpu.regs.get('a2').unsigned_value if fileno not in self.open_files: - scall.cpu.regs.set('a0', -1) + scall.ret(-1) return chars = self.open_files[fileno].readline(size) @@ -142,7 +147,7 @@ class SyscallInterface: Requires running with flag scall-fs """ - # FIXME: this should be toggleable in a global setting or somethign + # FIXME: this should be toggleable in a global setting or something if True: print(FMT_SYSCALL + '[Syscall] open: opening files not supported without scall-fs flag!' + FMT_NONE) return scall.ret(-1)