Add RISC-V Linux APE loader by luojia65 · Pull Request #1515 · jart/cosmopolitan · GitHub
Skip to content

Add RISC-V Linux APE loader - #1515

Open
luojia65 wants to merge 2 commits into
jart:masterfrom
rustsbi:feat/riscv
Open

luojia65 wants to merge 2 commits into
jart:masterfrom
rustsbi:feat/riscv

Conversation

@luojia65

Copy link
Copy Markdown

This adds a Linux RV64 APE loader and teaches apelink to select it when uname -m reports riscv64. The loader uses the Linux generic syscall ABI and currently targets static RV64IMAC/LP64 ELF executables.

This is loader support only. It does not attempt to port Cosmopolitan libc or the compiler runtime to RISC-V (will be implemented in the future).

For a minimal test, save the following as /tmp/hello-riscv64.S:

	.section .text
	.globl _start
	.type _start, @function
_start:
	li a0, 1
	la a1, message
	li a2, 19
	li a7, 64
	ecall

	li a0, 0
	li a7, 93
	ecall
	.size _start, . - _start

	.section .rodata
message:
	.ascii "hello from riscv64\n"

It uses the Linux write and exit syscalls directly and does not depend on libc. Compile it with:

riscv64-linux-gnu-gcc \
  -nostdlib -static \
  -march=rv64imac -mabi=lp64 \
  -Wl,-e,_start \
  -Wl,--build-id=none \
  -Wl,-Ttext-segment=0x400000 \
  -o /tmp/hello-riscv64.elf \
  /tmp/hello-riscv64.S

The explicit text address keeps the ELF load segments above the 2 MiB userspace limit enforced by apelink.

Build apelink and the three loaders with:

make -j16 o//tool/build/apelink
make -j16 o//ape/ape.elf
make -j16 MODE=aarch64 o/aarch64/ape/ape.elf
make -j16 MODE=riscv64 ARCH=riscv64 \
  TOOLCHAIN=riscv64-linux-gnu- \
  o/riscv64/ape/ape.elf

The existing example ELFs can be used as the x86-64 and AArch64 payloads:

make -j16 o//examples/hello
make -j16 MODE=aarch64 o/aarch64/examples/hello

o//tool/build/apelink \
  -l o//ape/ape.elf \
  -l o/aarch64/ape/ape.elf \
  -l o/riscv64/ape/ape.elf \
  -o /tmp/hello.com \
  o//examples/hello.dbg \
  o/aarch64/examples/hello.dbg \
  /tmp/hello-riscv64.elf

Running the resulting file on x86-64 prints hello world. Running the same file on a Linux machine where uname -m reports riscv64 prints:

hello from riscv64

The RISC-V path was tested on real hardware, including repeated runs through the extracted loader cache. The existing x86-64 and AArch64 loader builds also continue to pass. (AArch64 execution was not tested on our hardware.)

Teach apelink to select EM_RISCV inputs when uname -m reports
riscv64. This enables fat APE binaries containing a RISC-V ELF
payload to assimilate and run on RISC-V Linux systems.

Signed-off-by: Zhouqi Jiang <jiangzhouqi25@mails.ucas.ac.cn>
Teach ape to build and run its loader on riscv64 Linux. Add the
RISC-V startup, launch, syscall, and frame macros, use the generic
Linux syscall table, and let apelink select bundled RISC-V loaders.

Signed-off-by: Zhouqi Jiang <jiangzhouqi25@mails.ucas.ac.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant