mu/linux/000init.subx
Kartik K. Agaram 71e4f38129 7842 - new directory organization
Baremetal is now the default build target and therefore has its sources
at the top-level. Baremetal programs build using the phase-2 Mu toolchain
that requires a Linux kernel. This phase-2 codebase which used to be at
the top-level is now under the linux/ directory. Finally, the phase-2 toolchain,
while self-hosting, has a way to bootstrap from a C implementation, which
is now stored in linux/bootstrap. The bootstrap C implementation uses some
literate programming tools that are now in linux/bootstrap/tools.

So the whole thing has gotten inverted. Each directory should build one
artifact and include the main sources (along with standard library). Tools
used for building it are relegated to sub-directories, even though those
tools are often useful in their own right, and have had lots of interesting
programs written using them.

A couple of things have gotten dropped in this process:
  - I had old ways to run on just a Linux kernel, or with a Soso kernel.
    No more.
  - I had some old tooling for running a single test at the cursor. I haven't
    used that lately. Maybe I'll bring it back one day.

The reorg isn't done yet. Still to do:
  - redo documentation everywhere. All the README files, all other markdown,
    particularly vocabulary.md.
  - clean up how-to-run comments at the start of programs everywhere
  - rethink what to do with the html/ directory. Do we even want to keep
    supporting it?

In spite of these shortcomings, all the scripts at the top-level, linux/
and linux/bootstrap are working. The names of the scripts also feel reasonable.
This is a good milestone to take stock at.
2021-03-03 22:21:03 -08:00

91 lines
2.8 KiB
Plaintext

# Some OS-specific preliminaries for Linux.
# Memory layout
#
# 0 - 0x08047ffff - reserved for the kernel
# 0x08048000 - 0xbffffffff - available for user programs
# 0xc0000000 - 0xfffffffff - reserved for the kernel
== code 0x09000000
== data 0x0a000000
# Syscalls
#
# We don't have libc, so we need to know Linux's precise syscall layout.
# These are not real functions. Pass arguments in specific registers.
== code
# http://man7.org/linux/man-pages/man2/exit.2.html
syscall_exit: # status/ebx: int
b8/copy-to-eax 1/imm32
cd/syscall 0x80/imm8
# http://man7.org/linux/man-pages/man2/read.2.html
syscall_read: # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
b8/copy-to-eax 3/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/write.2.html
syscall_write: # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
b8/copy-to-eax 4/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/open.2.html
syscall_open: # filename/ebx: (addr kernel-string), flags/ecx: int, dummy=0x180/edx -> fd-or-error/eax: int
b8/copy-to-eax 5/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/close.2.html
syscall_close: # fd/ebx: int -> status/eax
b8/copy-to-eax 6/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/lseek.2.html
syscall_lseek: # fd/ebx: int, offset/ecx: int, whence/edx: int
b8/copy-to-eax 0x13/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/creat.2.html
syscall_creat: # filename/ebx: (addr kernel-string) -> fd-or-error/eax: int
b8/copy-to-eax 8/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/unlink.2.html
syscall_unlink: # filename/ebx: (addr kernel-string) -> status/eax: int
b8/copy-to-eax 0xa/imm32
cd/syscall 0x80/imm8
c3/return
# http://man7.org/linux/man-pages/man2/rename.2.html
syscall_rename: # source/ebx: (addr kernel-string), dest/ecx: (addr kernel-string) -> status/eax: int
b8/copy-to-eax 0x26/imm32
cd/syscall 0x80/imm8
c3/return
# https://github.com/torvalds/linux/blob/fa121bb3fed6313b1f0af23952301e06cf6d32ed/mm/nommu.c#L1352
syscall_mmap: # arg/ebx: (addr mmap_arg_struct) -> status/eax: int
# the important thing: ebx+4 contains the 32-bit size to be allocated
b8/copy-to-eax 0x5a/imm32
cd/syscall 0x80/imm8
c3/return
syscall_ioctl: # fd/ebx: int, cmd/ecx: int, arg/edx: (addr _)
b8/copy-to-eax 0x36/imm32
cd/syscall 0x80/imm8
c3/return
syscall_nanosleep: # req/ebx: (addr timespec)
b8/copy-to-eax 0xa2/imm32 # 162
cd/syscall 0x80/imm8
c3/return
syscall_clock_gettime: # clock/ebx: int, out/ecx: (addr timespec)
b8/copy-to-eax 0x109/imm32 # 265
cd/syscall 0x80/imm8
c3/return