-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhello_world_lib.c
60 lines (52 loc) · 1.33 KB
/
hello_world_lib.c
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
// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
#include "hello_world_lib.h"
#include <asm/unistd.h>
#include <stdlib.h>
#include <unistd.h>
/*
Local system call implementations
*/
// int close(int fd)
int sys_close(int fd) {
int r;
asm volatile("syscall"
: "=a"(r)
: "0"(__NR_close), "D"(fd)
: "rcx", "r11", "memory");
return r;
}
// void exit(int status)
void sys_exit(int status) {
int r;
asm volatile("syscall"
: "=a"(r)
: "0"(__NR_exit), "D"(status)
: "rcx", "r11", "memory");
}
// int open(const char *pathname, int flags, mode_t mode)
int sys_open(const char *pathname, int flags, mode_t mode) {
int r;
asm volatile("syscall"
: "=a"(r)
: "0"(__NR_open), "D"(pathname), "S"(flags), "d"(mode)
: "rcx", "r11", "memory");
return r;
}
// ssize_t read(int fd, void *buf, size_t count)
ssize_t sys_read(int fd, void *buf, size_t count) {
ssize_t r;
asm volatile("syscall"
: "=a"(r)
: "0"(__NR_read), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11", "memory");
return r;
}
// ssize_t write(int fd, const void *buf, size_t count)
ssize_t sys_write(int fd, const void *buf, size_t count) {
ssize_t r;
asm volatile("syscall"
: "=a"(r)
: "0"(__NR_write), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11", "memory");
return r;
}