summaryrefslogtreecommitdiff
path: root/src/arch/x86/syscall.h
blob: 2fb3552dacd9f4bf55a036978719c72dd7f438bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* SPDX-License-Identifier: MIT */

#ifndef LIBURING_ARCH_X86_SYSCALL_H
#define LIBURING_ARCH_X86_SYSCALL_H

#ifndef LIBURING_SYSCALL_H
#  error "This file should be included from src/syscall.h (liburing)"
#endif

#if defined(__x86_64__)
/**
 * Note for syscall registers usage (x86-64):
 *   - %rax is the syscall number.
 *   - %rax is also the return value.
 *   - %rdi is the 1st argument.
 *   - %rsi is the 2nd argument.
 *   - %rdx is the 3rd argument.
 *   - %r10 is the 4th argument (**yes it's %r10, not %rcx!**).
 *   - %r8  is the 5th argument.
 *   - %r9  is the 6th argument.
 *
 * `syscall` instruction will clobber %r11 and %rcx.
 *
 * After the syscall returns to userspace:
 *   - %r11 will contain %rflags.
 *   - %rcx will contain the return address.
 *
 * IOW, after the syscall returns to userspace:
 *   %r11 == %rflags and %rcx == %rip.
 */

static inline void *__arch_impl_mmap(void *addr, size_t length, int prot,
				     int flags, int fd, off_t offset)
{
	void *rax;
	register int r10 __asm__("r10") = flags;
	register int r8 __asm__("r8") = fd;
	register off_t r9 __asm__("r9") = offset;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_mmap),	/* %rax */
		  "D"(addr),		/* %rdi */
		  "S"(length),		/* %rsi */
		  "d"(prot),		/* %rdx */
		  "r"(r10),		/* %r10 */
		  "r"(r8),		/* %r8  */
		  "r"(r9)		/* %r9  */
		: "memory", "rcx", "r11"
	);
	return rax;
}

static inline int __arch_impl_munmap(void *addr, size_t length)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_munmap),	/* %rax */
		  "D"(addr),		/* %rdi */
		  "S"(length)		/* %rsi */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_madvise(void *addr, size_t length, int advice)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_madvise),	/* %rax */
		  "D"(addr),		/* %rdi */
		  "S"(length),		/* %rsi */
		  "d"(advice)		/* %rdx */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_getrlimit(int resource, struct rlimit *rlim)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_getrlimit),	/* %rax */
		  "D"(resource),	/* %rdi */
		  "S"(rlim)		/* %rsi */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_setrlimit(int resource, const struct rlimit *rlim)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_setrlimit),	/* %rax */
		  "D"(resource),	/* %rdi */
		  "S"(rlim)		/* %rsi */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_close(int fd)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)		/* %rax */
		: "a"(__NR_close),	/* %rax */
		  "D"(fd)		/* %rdi */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_io_uring_register(int fd, unsigned opcode,
						const void *arg,
						unsigned nr_args)
{
	long rax;
	register unsigned r10 __asm__("r10") = nr_args;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)			/* %rax */
		: "a"(__NR_io_uring_register),	/* %rax */
		  "D"(fd),			/* %rdi */
		  "S"(opcode),			/* %rsi */
		  "d"(arg),			/* %rdx */
		  "r"(r10)			/* %r10 */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_io_uring_setup(unsigned entries,
					     struct io_uring_params *p)
{
	long rax;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)			/* %rax */
		: "a"(__NR_io_uring_setup),	/* %rax */
		  "D"(entries),			/* %rdi */
		  "S"(p)			/* %rsi */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

static inline int __arch_impl_io_uring_enter(int fd, unsigned to_submit,
					     unsigned min_complete,
					     unsigned flags, sigset_t *sig,
					     int sz)
{
	long rax;
	register unsigned r10 __asm__("r10") = flags;
	register sigset_t *r8 __asm__("r8") = sig;
	register int r9 __asm__("r9") = sz;

	__asm__ volatile(
		"syscall"
		: "=a"(rax)			/* %rax */
		: "a"(__NR_io_uring_enter),	/* %rax */
		  "D"(fd),			/* %rdi */
		  "S"(to_submit),		/* %rsi */
		  "d"(min_complete),		/* %rdx */
		  "r"(r10),			/* %r10 */
		  "r"(r8),			/* %r8  */
		  "r"(r9)			/* %r9  */
		: "memory", "rcx", "r11"
	);
	return (int) rax;
}

#else /* #if defined(__x86_64__) */

/*
 * TODO: Add x86 (32-bit) support here.
 */
#error "x86 (32-bit) is currently not supported for nolibc builds"

#endif /* #if defined(__x86_64__) */

#endif /* #ifndef LIBURING_ARCH_X86_SYSCALL_H */