/* SPDX-License-Identifier: Apache-2.0 * SchOS — process entry point. * * This has to be assembly, and the reason is worth recording. * * exec() hands a new process its arguments in registers, not on the stack: * kernel/fs/exec.c sets proc->tf->rdi = argc and proc->tf->rsi = argv. Those * are already the first two SysV argument registers, so _start's entire job * is to call main and then exit — it must not touch %rdi or %rsi at all. * * It used to be written in C, capturing the incoming registers with GCC's * * register int argc_reg asm("rdi"); * * That is not what local register variables mean. Both GCC and clang only * guarantee the binding when the variable is used as an operand of an inline * asm statement; reading one to pick up an incoming argument is undefined. * GCC happened to do the useful thing. clang compiled it to * * movl -0x4(%rbp), %eax # two uninitialised stack slots * movq -0x10(%rbp), %rax * * and every program was handed a garbage argc and argv. Nothing crashed -- * init, shmtest and socktest take no arguments and passed their whole test * suites -- so the only symptom was `inputtest -n 6` quietly ignoring its * limit. A compiler-dependent way to read registers has no business being in * C when four instructions of assembly are exactly right. */ .text .globl _start _start: xorq %rbp, %rbp /* end the frame-pointer chain here */ /* SysV wants %rsp 16-byte aligned immediately before a call, so that the * callee sees %rsp+8 aligned once the return address is pushed. Rounding * down is safe: argc and argv arrive in registers, so nothing below is * needed. */ andq $-16, %rsp callq main /* main(%rdi = argc, %rsi = argv) */ /* main's return value is the exit status, and it used to be thrown away. * * sys_exit() reads the status from %rdi, and nothing put it there -- so * every first-party program exited with whatever happened to be in that * register, and `if socktest; then` could not work for any of them. The * test suites all end in `return failures ? 1 : 0` and none of it reached * the shell. * * %eax, not %rax: the status is the low eight bits, and a `return 0` from * a C function leaves only %eax defined. */ movl %eax, %edi callq exit_group /* noreturn, but be certain */ 1: hlt jmp 1b