Skip to content

Commit 7e36c00

Browse files
committed
use an enum for syscalls
1 parent ba9900a commit 7e36c00

2 files changed

Lines changed: 72 additions & 62 deletions

File tree

include/cascade/syscall.h

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,87 @@
66

77
#include <cascade/internal/common.h>
88

9-
typedef uint64_t cascade_syscall;
10-
11-
/**
12-
* Exits the current thread.
13-
*
14-
* ### Arguments
15-
* none
16-
*
17-
* ### Errors
18-
* none
19-
*
20-
* ### Return
21-
* never
22-
*/
23-
#define CASCADE_SYSCALL_THREAD_EXIT_CURRENT ((cascade_syscall)0)
24-
25-
/**
26-
* Output a debug message.
27-
*
28-
* This is not intended to be used by normal userspace programs and instead is intended for logs from system libraries like libc.
29-
*
30-
* The message is assumed to be UTF-8 encoded.
31-
*
32-
* If the message does not end with a newline, one will be appended.
33-
*
34-
* No guarantees are made about the destination of the message, the implementation may choose to discard it or send it to any number of
35-
* destinations.
36-
*
37-
* Any errors encountered while writing the message are ignored and may cause the message to be truncated.
38-
*
39-
* ### Arguments
40-
* - `arg1`: length of the message
41-
* - `arg2`: pointer to the message
42-
*
43-
* ### Errors
44-
* none
45-
*
46-
* ### Return
47-
* undefined
48-
*/
49-
#define CASCADE_SYSCALL_DEBUG_PRINT ((cascade_syscall)1)
50-
519
#ifdef __cplusplus
5210
extern "C" {
5311
#endif
5412

13+
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201103L)
14+
typedef enum cascade_syscall_e : uint64_t {
15+
#else
16+
typedef enum cascade_syscall_e {
17+
#endif
18+
/**
19+
* Exits the current thread.
20+
*
21+
* ### Arguments
22+
* none
23+
*
24+
* ### Errors
25+
* none
26+
*
27+
* ### Return
28+
* never
29+
*/
30+
CASCADE_SYSCALL_THREAD_EXIT_CURRENT = 0,
31+
32+
/**
33+
* Output a debug message.
34+
*
35+
* This is not intended to be used by normal userspace programs and instead is intended for logs from system libraries like libc.
36+
*
37+
* The message is assumed to be UTF-8 encoded.
38+
*
39+
* If the message does not end with a newline, one will be appended.
40+
*
41+
* No guarantees are made about the destination of the message, the implementation may choose to discard it or send it to any number
42+
of
43+
* destinations.
44+
*
45+
* Any errors encountered while writing the message are ignored and may cause the message to be truncated.
46+
*
47+
* ### Arguments
48+
* - `arg1`: length of the message
49+
* - `arg2`: pointer to the message
50+
*
51+
* ### Errors
52+
* none
53+
*
54+
* ### Return
55+
* undefined
56+
*/
57+
CASCADE_SYSCALL_DEBUG_PRINT = 1,
58+
} cascade_syscall;
59+
5560
#if defined(__x86_64__)
5661

5762
_CASCADE_INLINE int64_t cascade_syscall0(cascade_syscall syscall) {
5863
int64_t ret;
59-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall) : "memory", "rcx", "r11");
64+
__asm__ volatile("syscall" : "=a"(ret) : "a"((uint64_t)syscall) : "memory", "rcx", "r11");
6065
return ret;
6166
}
6267

6368
_CASCADE_INLINE int64_t cascade_syscall1(cascade_syscall syscall, uint64_t arg1) {
6469
int64_t ret;
65-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall), "D"(arg1) : "memory", "rcx", "r11");
70+
__asm__ volatile("syscall" : "=a"(ret) : "a"((uint64_t)syscall), "D"(arg1) : "memory", "rcx", "r11");
6671
return ret;
6772
}
6873

6974
_CASCADE_INLINE int64_t cascade_syscall2(cascade_syscall syscall, uint64_t arg1, uint64_t arg2) {
7075
int64_t ret;
71-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall), "D"(arg1), "S"(arg2) : "memory", "rcx", "r11");
76+
__asm__ volatile("syscall" : "=a"(ret) : "a"((uint64_t)syscall), "D"(arg1), "S"(arg2) : "memory", "rcx", "r11");
7277
return ret;
7378
}
7479

7580
_CASCADE_INLINE int64_t cascade_syscall3(cascade_syscall syscall, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
7681
int64_t ret;
77-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3) : "memory", "rcx", "r11");
82+
__asm__ volatile("syscall" : "=a"(ret) : "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3) : "memory", "rcx", "r11");
7883
return ret;
7984
}
8085

8186
_CASCADE_INLINE int64_t cascade_syscall4(cascade_syscall syscall, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) {
8287
register uint64_t _rbx __asm__("rbx") = arg4;
8388
int64_t ret;
84-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx) : "memory", "rcx", "r11");
89+
__asm__ volatile("syscall" : "=a"(ret) : "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx) : "memory", "rcx", "r11");
8590
return ret;
8691
}
8792

@@ -90,7 +95,10 @@ cascade_syscall5(cascade_syscall syscall, uint64_t arg1, uint64_t arg2, uint64_t
9095
register uint64_t _rbx __asm__("rbx") = arg4;
9196
register uint64_t _r8 __asm__("r8") = arg5;
9297
int64_t ret;
93-
__asm__ volatile("syscall" : "=a"(ret) : "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8) : "memory", "rcx", "r11");
98+
__asm__ volatile("syscall"
99+
: "=a"(ret)
100+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8)
101+
: "memory", "rcx", "r11");
94102
return ret;
95103
}
96104

@@ -102,7 +110,7 @@ cascade_syscall6(cascade_syscall syscall, uint64_t arg1, uint64_t arg2, uint64_t
102110
int64_t ret;
103111
__asm__ volatile("syscall"
104112
: "=a"(ret)
105-
: "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9)
113+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9)
106114
: "memory", "rcx", "r11");
107115
return ret;
108116
}
@@ -117,7 +125,7 @@ _CASCADE_INLINE int64_t cascade_syscall7(
117125
int64_t ret;
118126
__asm__ volatile("syscall"
119127
: "=a"(ret)
120-
: "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10)
128+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10)
121129
: "memory", "rcx", "r11");
122130
return ret;
123131
}
@@ -141,7 +149,7 @@ _CASCADE_INLINE int64_t cascade_syscall8(
141149
int64_t ret;
142150
__asm__ volatile("syscall"
143151
: "=a"(ret)
144-
: "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12)
152+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12)
145153
: "memory", "rcx", "r11");
146154
return ret;
147155
}
@@ -165,10 +173,12 @@ _CASCADE_INLINE int64_t cascade_syscall9(
165173
register uint64_t _r12 __asm__("r12") = arg8;
166174
register uint64_t _r13 __asm__("r13") = arg9;
167175
int64_t ret;
168-
__asm__ volatile("syscall"
169-
: "=a"(ret)
170-
: "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12), "r"(_r13)
171-
: "memory", "rcx", "r11");
176+
__asm__ volatile(
177+
"syscall"
178+
: "=a"(ret)
179+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12), "r"(_r13)
180+
: "memory", "rcx", "r11"
181+
);
172182
return ret;
173183
}
174184

@@ -196,7 +206,7 @@ _CASCADE_INLINE int64_t cascade_syscall10(
196206
__asm__ volatile(
197207
"syscall"
198208
: "=a"(ret)
199-
: "a"(syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12), "r"(_r13), "r"(_r14)
209+
: "a"((uint64_t)syscall), "D"(arg1), "S"(arg2), "d"(arg3), "r"(_rbx), "r"(_r8), "r"(_r9), "r"(_r10), "r"(_r12), "r"(_r13), "r"(_r14)
200210
: "memory", "rcx", "r11"
201211
);
202212
return ret;
@@ -227,7 +237,7 @@ _CASCADE_INLINE int64_t cascade_syscall11(
227237
int64_t ret;
228238
__asm__ volatile("syscall"
229239
: "=a"(ret)
230-
: "a"(syscall),
240+
: "a"((uint64_t)syscall),
231241
"D"(arg1),
232242
"S"(arg2),
233243
"d"(arg3),
@@ -270,7 +280,7 @@ _CASCADE_INLINE int64_t cascade_syscall12(
270280
int64_t ret;
271281
__asm__ volatile("syscall"
272282
: "=a"(ret)
273-
: "a"(syscall),
283+
: "a"((uint64_t)syscall),
274284
"D"(arg1),
275285
"S"(arg2),
276286
"d"(arg3),

include/cascade/thread.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
#include <cascade/internal/common.h>
88

9-
typedef struct {
10-
uint64_t handle;
11-
} cascade_thread_handle;
12-
139
#ifdef __cplusplus
1410
extern "C" {
1511
#endif
1612

13+
typedef struct {
14+
uint64_t handle;
15+
} cascade_thread_handle;
16+
1717
/**
1818
* Exits the current thread.
1919
*/

0 commit comments

Comments
 (0)