SchOS/lib/start.S
Nexoniarz 3f13989883 unix: a bound socket is a file
Gate 1, item 3, and the last of foundations.md.

bind() creates a T_SOCK node at the path; connect() resolves the path and looks
for the socket bound to **that inode**. Matching on the inode rather than on
the path *string* is the part that matters: the name used to live only in the
socket table, so two spellings of one path missed each other, a relative path
never matched an absolute one, and there was nothing to stat() or unlink().
An inode is the thing a path resolves *to*, so connect() can walk the path
however it likes and arrive at the same socket.

    # ls -l /tmp
    srwxr-xr-x 1 root root 0 2026-09-22 08:04 socktest

The node is a marker, not the binding. It holds no data and the live
association stays in the socket table, so a socket file left by a crash is
what Linux leaves behind -- a dead file that connect() refuses until something
binds again -- and unlink()ing a live socket's path does not disturb anyone
already connected.

EADDRINUSE comes from the *directory* rather than from a scan of the table,
deliberately: a stale socket file has to refuse a rebind for the same reason a
running server's does, or two servers both believe they own it. The creation
lives in sys_bind() and not in unix.c, because that is the layer with both --
kernel/net/unix.c knows about sockets and has no business calling create().

T_SOCK is a compat addition to SchFS by the rule in schfs.h: a driver that does
not know SCHFS_T_SOCK maps it to a zero-length regular file, so nothing goes
stale and nothing becomes unreadable.

cmd/socktest section 15 asserts the plan's own wording -- bind, stat, see an
`s`, unlink -- plus the two a name table could never fail: a second bind to a
live path is refused, and connecting to a path that is a *regular file* is
refused rather than matched by name.

**And the semantics being real broke socktest on its second run**, correctly:
the socket files persisted and every bind() hit EADDRINUSE. Every real server
unlinks before binding; socktest now does too, which is the change the new
behaviour demands rather than a workaround for it.

Chasing that turned up something worse. `socktest` reported rc=1 with every
check passing, and so did every other first-party program:

  - **lib/start.S called main and threw %eax away.** sys_exit() reads the
    status from %rdi and nothing put it there, so every program exited with
    whatever that register happened to hold. Every test suite ends in
    `return failures ? 1 : 0` and none of it reached the shell.
  - **exit(void) passed no status either**, for the same reason: the
    declaration takes no argument, so %rdi was the caller's litter.

`if socktest; then` works now, and a failing command reports 1 -- checked both
ways, because a fix that makes everything return 0 would look identical from
the success side.

Also factored: unlink_path() out of sys_unlink, so a failed bind can take its
own node back out without going through the syscall entry, and create_sock()
as a named door rather than exporting create() with an arbitrary type.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 10:06:10 +02:00

56 lines
2.4 KiB
ArmAsm

/* 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