CTSS - Multics - Unix - BSD/Linux - Android/iOS
is there a program that doesn’t make any system calls?
try strace with C & Java hello world
#include <stdio.h>
int main(void)
{
printf("hello world!\n");
return 0;
}
class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
see also: Linux system calls
example: write
see fs/read_write.c
int 0x80
, CSE 351, lec 9, “Processes”syscall
instruction
write
is 1write(fd, buf, count)
excerpts from AMD64 ABI, A.2.1
The kernel interface uses %rdi
, %rsi
, %rdx
, %r10
, %r8
and %r9
.
A system-call is done via the syscall
instruction. The kernel destroys registers %rcx
and %r11
.
The number of the syscall has to be passed in register %rax.
System-calls are limited to six arguments, no argument is passed directly on the stack.
Returning from the syscall, register %rax
contains the result of the system-call. A value in the range between -4095
and -1
indicates an error, it is -errno
.
write(1, "hello world!\n", 13);
_exit(0);
syscall | register | description | value |
---|---|---|---|
write |
%rax |
syscall number | 1 (__NR_write ) |
%rdi |
1st parameter | 1 (stdout) | |
%rsi |
2nd parameter | address of “hello world!\n” | |
%rdx |
3rd parameter | 13 (size of above) | |
_exit |
%rax |
syscall number | 60 (__NR_exit ) |
%rdi |
1st parameter | 0 |
#include <sys/syscall.h>
#include <unistd.h>
void _start(void) {
int fd = 1;
char buf[] = "hello world!\n";
size_t count = sizeof(buf) - 1;
asm volatile ("syscall"
: /* ignore output */
: "a"(__NR_write), "D"(fd), "S"(buf), "d"(count)
: "cc", "rcx", "r11", "memory"
);
asm volatile ("syscall"
: /* no output */
: "a"(__NR_exit), "D"(0)
);
}
compile using “gcc -nostdlib
” and try strace
manually invoking syscalls is tedious & non-portable
ssize_t write(int fd, const void *buf, size_t count);
man
/google/next lecture:
see also: creat
libc provides higher-level library functions
printf
will call write
internallyint printf(const char *restrict format, ...);
q1: how about Java’s System.out.println
?
q2: how about Unix commands ls
, cat
, or sed
?