SchOS/lib/start.S
Nexoniarz 26bca020b6 licence: SchOS is GPL-2.0-or-later, no longer Apache-2.0
Nexoniarz is the sole author and SchOS was never published under
Apache-2.0, so this is a straight relicence. LICENSE is the FSF GPLv2 text,
every first-party SPDX header moves to GPL-2.0-or-later, and NOTICE,
README.md, docs/licensing.md and the site say so. projects/schfs-linux
follows the rest of the tree.

The GPL gives copyleft: a fork has to ship its source. It also lifts the
old "Linux drivers, never" rule, since GPL-2.0-only code combines with
GPL-2.0-or-later. "Or later" leaves room for GPLv3, but one binary still
has to pick one version, so docs/licensing.md now keeps Apache-2.0 and
GPLv3 code out of the kernel to keep Linux code possible there. BSD
4-clause code goes from "avoid" to "no", because the advertising clause
is GPL-incompatible.

LDLS still ships no .ko. That boundary is now project policy rather than
a legal requirement, and ldls.md, devices.md and licensing.md section 6
say so. refmod declares license=GPL, and ldls-check passes (27 checks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 08:48:45 +02:00

56 lines
2.4 KiB
ArmAsm

/* SPDX-License-Identifier: GPL-2.0-or-later
* 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