https://github.com/akkartik/mu/blob/main/linux/000init.subx
 1 # Some OS-specific preliminaries for Linux.
 2 
 3 # Memory layout
 4 #
 5 #          0 - 0x08047ffff - reserved for the kernel
 6 # 0x08048000 - 0xbffffffff - available for user programs
 7 # 0xc0000000 - 0xfffffffff - reserved for the kernel
 8 == code 0x09000000
 9 == data 0x0a000000
10 
11 # Syscalls
12 #
13 # We don't have libc, so we need to know Linux's precise syscall layout.
14 # These are not real functions. Pass arguments in specific registers.
15 == code
16 
17 # http://man7.org/linux/man-pages/man2/exit.2.html
18 syscall_exit:  # status/ebx: int
19     b8/copy-to-eax 1/imm32
20     cd/syscall 0x80/imm8
21 
22 # http://man7.org/linux/man-pages/man2/read.2.html
23 syscall_read:  # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
24     b8/copy-to-eax 3/imm32
25     cd/syscall 0x80/imm8
26     c3/return
27 
28 # http://man7.org/linux/man-pages/man2/write.2.html
29 syscall_write:  # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
30     b8/copy-to-eax 4/imm32
31     cd/syscall 0x80/imm8
32     c3/return
33 
34 # http://man7.org/linux/man-pages/man2/open.2.html
35 syscall_open:  # filename/ebx: (addr kernel-string), flags/ecx: int, dummy=0x180/edx -> fd-or-error/eax: int
36     b8/copy-to-eax 5/imm32
37     cd/syscall 0x80/imm8
38     c3/return
39 
40 # http://man7.org/linux/man-pages/man2/close.2.html
41 syscall_close:  # fd/ebx: int -> status/eax
42     b8/copy-to-eax 6/imm32
43     cd/syscall 0x80/imm8
44     c3/return
45 
46 # http://man7.org/linux/man-pages/man2/lseek.2.html
47 syscall_lseek:  # fd/ebx: int, offset/ecx: int, whence/edx: int
48     b8/copy-to-eax 0x13/imm32
49     cd/syscall 0x80/imm8
50     c3/return
51 
52 # http://man7.org/linux/man-pages/man2/creat.2.html
53 syscall_creat:  # filename/ebx: (addr kernel-string) -> fd-or-error/eax: int
54     b8/copy-to-eax 8/imm32
55     cd/syscall 0x80/imm8
56     c3/return
57 
58 # http://man7.org/linux/man-pages/man2/unlink.2.html
59 syscall_unlink:  # filename/ebx: (addr kernel-string) -> status/eax: int
60     b8/copy-to-eax 0xa/imm32
61     cd/syscall 0x80/imm8
62     c3/return
63 
64 # http://man7.org/linux/man-pages/man2/rename.2.html
65 syscall_rename:  # source/ebx: (addr kernel-string), dest/ecx: (addr kernel-string) -> status/eax: int
66     b8/copy-to-eax 0x26/imm32
67     cd/syscall 0x80/imm8
68     c3/return
69 
70 # https://github.com/torvalds/linux/blob/fa121bb3fed6313b1f0af23952301e06cf6d32ed/mm/nommu.c#L1352
71 syscall_mmap:  # arg/ebx: (addr mmap_arg_struct) -> status/eax: int
72     # the important thing: ebx+4 contains the 32-bit size to be allocated
73     b8/copy-to-eax 0x5a/imm32
74     cd/syscall 0x80/imm8
75     c3/return
76 
77 syscall_ioctl:  # fd/ebx: int, cmd/ecx: int, arg/edx: (addr _)
78     b8/copy-to-eax 0x36/imm32
79     cd/syscall 0x80/imm8
80     c3/return
81 
82 syscall_nanosleep:  # req/ebx: (addr timespec)
83     b8/copy-to-eax 0xa2/imm32  # 162
84     cd/syscall 0x80/imm8
85     c3/return
86 
87 syscall_clock_gettime:  # clock/ebx: int, out/ecx: (addr timespec)
88     b8/copy-to-eax 0x109/imm32  # 265
89     cd/syscall 0x80/imm8
90     c3/return