Skip to content

Commit c68fdc7

Browse files
committed
feat(syscall): implement exit
1 parent 95cc6a3 commit c68fdc7

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

sim/include/syscall.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SYSCALL_H
2+
#define SYSCALL_H
3+
4+
#include <decode.h>
5+
6+
#define SYSCALL_EXIT 93
7+
#define SYSCALL_WRITE 64
8+
9+
void handle_syscall(Decode *s);
10+
11+
#endif

sim/src/syscall.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <cpu.h>
2-
#include <trap.h>
2+
#include <include/syscall.h>
33
#include <decode.h>
4+
#include <stdio.h>
45

56
extern CPU_state cpu;
67

78
void handle_syscall(Decode *s) {
89
uint64_t syscall_no = cpu.reg[17]; // a7 register
910
switch (syscall_no) {
1011
case SYSCALL_EXIT:
11-
Log("Syscall: exit with code %lu", cpu.reg[10]); // a0 register
12+
printf("Syscall: exit with code %lu\n", cpu.reg[10]); // a0 register
1213
halt_trap(s->pc, cpu.reg[10]);
1314
break;
1415

@@ -23,14 +24,14 @@ void handle_syscall(Decode *s) {
2324
}
2425
cpu.reg[10] = count; // return value in a0
2526
} else {
26-
Log("Syscall: write to unsupported fd %lu", fd);
27+
printf("Syscall: write to unsupported fd %lu\n", fd);
2728
cpu.reg[10] = -1; // error
2829
}
2930
}
3031
break;
3132

3233
default:
33-
Log("Unknown syscall: %lu", syscall_no);
34+
printf("Unknown syscall: %lu\n", syscall_no);
3435
halt_trap(s->pc, -1);
3536
break;
3637
}

test/include/syscall.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef SYSCALL_H
2+
#define SYSCALL_H
3+
4+
#include <stdint.h>
5+
6+
#define SYSCALL_EXIT 93
7+
#define SYSCALL_WRITE 64
8+
9+
static inline int64_t syscall(int64_t no, int64_t a0, int64_t a1, int64_t a2) {
10+
register int64_t a7 asm("a7") = no;
11+
register int64_t a0_ asm("a0") = a0;
12+
register int64_t a1_ asm("a1") = a1;
13+
register int64_t a2_ asm("a2") = a2;
14+
asm volatile("ecall"
15+
: "+r"(a0_)
16+
: "r"(a7), "r"(a1_), "r"(a2_)
17+
: "memory");
18+
return a0_;
19+
}
20+
21+
static inline void exit(int64_t code) {
22+
syscall(SYSCALL_EXIT, code, 0, 0);
23+
}
24+
25+
static inline int64_t write(int64_t fd, const void *buf, int64_t count) {
26+
return syscall(SYSCALL_WRITE, fd, (int64_t)buf, count);
27+
}
28+
29+
30+
#endif

test/src/exit.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <include/syscall.h>
2+
3+
int main() {
4+
exit(0);
5+
}

0 commit comments

Comments
 (0)