Eric Biederman | 9008960 | 2004-05-28 14:11:54 +0000 | [diff] [blame] | 1 | struct syscall_result { |
| 2 | long val; |
| 3 | int errno; |
| 4 | }; |
| 5 | |
| 6 | static struct syscall_result syscall_return(long result) |
| 7 | { |
| 8 | struct syscall_result res; |
| 9 | if (((unsigned long)result) >= ((unsigned long)-125)) { |
| 10 | res.errno = - result; |
| 11 | res.val = -1; |
| 12 | } else { |
| 13 | res.errno = 0; |
| 14 | res.val = result; |
| 15 | } |
| 16 | return res; |
| 17 | } |
| 18 | |
| 19 | static struct syscall_result syscall1(unsigned long nr, unsigned long arg1) |
| 20 | { |
| 21 | long res; |
| 22 | asm volatile( |
| 23 | "int $0x80" |
| 24 | : "=a" (res) |
| 25 | : "a" (nr), "b" (arg1)); |
| 26 | return syscall_return(res); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 27 | |
Eric Biederman | 9008960 | 2004-05-28 14:11:54 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | |
| 31 | static struct syscall_result syscall3(unsigned long nr, unsigned long arg1, unsigned long arg2, |
| 32 | unsigned long arg3) |
| 33 | { |
| 34 | long res; |
| 35 | asm volatile( |
| 36 | "int $0x80" |
| 37 | : "=a" (res) |
| 38 | : "a" (nr), "b" (arg1), "c" (arg2), "d" (arg3)); |
| 39 | return syscall_return(res); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 40 | |
Eric Biederman | 9008960 | 2004-05-28 14:11:54 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | #define NR_exit 1 |
| 44 | #define NR_write 4 |
| 45 | |
| 46 | /* Standard file descriptors */ |
| 47 | #define STDIN_FILENO 0 /* Standard input */ |
| 48 | #define STDOUT_FILENO 1 /* Standard output */ |
| 49 | #define STDERR_FILENO 2 /* Standard error output */ |
| 50 | |
| 51 | typedef long ssize_t; |
| 52 | typedef unsigned long size_t; |
| 53 | |
| 54 | static ssize_t write(int fd, const void *buf, size_t count) |
| 55 | { |
| 56 | struct syscall_result res; |
| 57 | res = syscall3(NR_write, fd, (unsigned long)buf, count); |
| 58 | return res.val; |
| 59 | } |
| 60 | |
| 61 | static void _exit(int status) |
| 62 | { |
| 63 | struct syscall_result res; |
| 64 | res = syscall1(NR_exit, status); |
| 65 | } |
| 66 | |
| 67 | static void console_tx_string(const char *str) |
| 68 | { |
| 69 | unsigned char ch; |
| 70 | while(1) { |
| 71 | |
| 72 | } |
| 73 | for(;1;) { |
| 74 | } |
| 75 | do { |
| 76 | } while(1); |
| 77 | if (1) { |
| 78 | }else { |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void main(void) |
| 84 | { |
| 85 | static const char msg[] = "hello world\r\n"; |
| 86 | write(STDOUT_FILENO, msg, sizeof(msg)); |
| 87 | _exit(0); |
| 88 | } |