ARM_Linlon-V5

Public RASC Toolchain (rasc-elf) and ABI Contract

This document explains how to compile freestanding C and Assembly firmware for the SpacemiT K1/K3 RASC CPUs using exclusively ARM’s public open-source toolchain, details the compiler-generated ABI, and documents known practical toolchain limitations.


1. Public Toolchain Source ([P])

ARM publicly provides the complete toolchain for the rasc-elf target:


2. Building the Toolchain on Modern Linux (x86-64) ([P][O])

The repository’s original makefile assumes gcc -m32, newlib, and svn (not present in the repository). Direct manual compilation on a 64-bit host has been successfully validated ([O]):

2.1 Compatibility Wrapper for Modern Host GCC

Modern compilers (GCC 14–16) treat implicit declarations and -fno-common as fatal errors in 2008-era code. Create a cc-gnu89 wrapper:

#!/bin/sh
exec gcc -std=gnu89 -fcommon -Wno-error \
     -Wno-implicit-function-declaration \
     -Wno-builtin-declaration-mismatch "$@"

2.2 Building binutils (rasc-elf)

mkdir -p build-binutils && cd build-binutils
CC=$PWD/../cc-gnu89 ../rasc-devkit/binutils/configure \
    --target=rasc-elf \
    --prefix=$PWD/../out \
    --program-prefix=rasc-elf- \
    --disable-nls --disable-werror \
    --disable-gdb --disable-sim --disable-gprof
make -j$(nproc)
make install

2.3 Building gcc (rasc-elf-gcc, freestanding C)

Add out/bin to PATH before compiling GCC (required so libgcc.a finds rasc-elf-ar and rasc-elf-ranlib):

export PATH=$PWD/out/bin:$PATH
mkdir -p build-gcc && cd build-gcc
CC=$PWD/../cc-gnu89 ../rasc-devkit/gcc/configure \
    --target=rasc-elf \
    --prefix=$PWD/../out \
    --program-prefix=rasc-elf- \
    --enable-languages=c \
    --disable-nls --disable-bootstrap \
    --without-headers --with-newlib \
    --disable-libssp --disable-libgomp --disable-libmudflap \
    --disable-threads --disable-shared --disable-multilib \
    --with-as=$PWD/../out/bin/rasc-elf-as \
    --with-ld=$PWD/../out/bin/rasc-elf-ld
make -j$(nproc) all-gcc
make install-gcc

Environment tip: If /tmp has limited space, set TMPDIR to a directory with sufficient free space before running make and rasc-elf-gcc.


3. Calling Convention (rasc-elf-gcc ABI) ([P][O])

Extracted from gcc/config/rasc/rasc.h/rasc.c and empirically confirmed on real K1 hardware:

Element Convention
Function arguments First 5 words (32-bit) in r0 – r4; additional arguments (6th onward) passed on the stack ([sp, offset])
Return value (32-bit) r0
Return value (64-bit or 2-word struct) r0:r1 (r0 = low word, r1 = high word). Measured note: returning a struct of two u32s uses r0:r1 with no extra packing instructions
Caller-saved (volatile) r0 – r4
Callee-saved (preserved) r5 – r13, plus r14 (lr) and r15 (sp)
Frame pointer (when used) r13
Link register r14 (lr)
Stack pointer r15 (sp), grows toward decreasing addresses
Typical prologue push rN-r14 (saves lowest used callee-saved register through lr), sub sp, #locals
Typical epilogue add sp, #locals, popr rN-r14 (restores rN..r14 and returns); leaf functions return with jmp lr (pseudo-mnemonic rts)
Call / Tail call bsr symbol / jsr rN; tail calls use br symbol / jmp rN
Stack order (push rN-r14) rN at the lowest address, r14 (lr) at the highest address
Access to globals / strings Automatically emitted via lpc rX, symbol (PC-relative)

4. Clean-Room .fwb Firmware Build Pipeline ([P][O])

Because rasc-devkit includes neither newlib nor crt0, the clean-room firmware must be 100% freestanding (-ffreestanding -nostdlib):

# 1. Compile assembly startup and C sources (C89 / GNU89)
rasc-elf-gcc -std=gnu89 -Os -ffreestanding -Wall -Wextra -c start.S -o start.o
rasc-elf-gcc -std=gnu89 -Os -ffreestanding -Wall -Wextra -c main.c -o main.o

# 2. Link with a linker script that places _start after the 180-byte header (e.g., 0xB4 or 0xC0)
rasc-elf-ld -T firmware.ld -o firmware.elf start.o main.o

# 3. Extract flat image (code + rodata)
rasc-elf-objcopy -O binary firmware.elf payload.bin

# 4. Pack into the .fwb container:
#    - 4-byte `br _start` trampoline at offset 0x00 (rasc_jmp)
#    - Remaining 176 bytes of struct mvx_fw_header (protocol_major=3, protocol_minor=5,
#      text_length = payload_offset + len(payload.bin), bss_start_address, bss_bitmap, master_rw)
#    - padding up to payload_offset + payload.bin

Linker Script Requirements (firmware.ld)

  1. Place .text.start at the exact file offset targeted by the br trampoline at 0x00 (e.g., 0xB4 or 0xC0, corresponding to VA 0x10B4 or 0x10C0 in instance 0).
  2. Align .bss to the bss_start_address declared in the .fwb header (multiple of 0x1000, above the end of .text/.rodata).
  3. Align the shared .master_rw section to master_rw_start_address (multiple of 0x1000), reserving pages in bss_bitmap_size.
  4. Define the stack_top symbol at the top of the stack area inside .bss for loading via lpc sp, stack_top in start.S.
  5. If there is non-zero initialized data (.data), use AT(...) in .rodata (LMA) with a copy in start.S to VMA in .bss using PC-relative addressing (lpc), because the .fwb file only loads the text segment (read-only/executable).

5. Known Toolchain Pitfalls and Limitations ([O])

  1. Assembler (rasc-elf-as 2.18) segfault on raw numeric addresses:
    • Writing bsr 0x1234 or lpc r2, 0x44000 with a numeric literal causes a segfault in as 2.18.
    • Solution: Always use labels/symbols (bsr my_function, lpc r2, my_symbol). The GCC compiler always emits symbols, so only hand-written .S assembly needs to observe this rule.
  2. C dialect (GCC 4.2.3, 2008):
    • Use -std=gnu89 or -std=gnu99. C11 features (_Static_assert, _Atomic, _Generic) do not exist.
    • Use volatile and compiler barriers (__asm__ volatile ("" ::: "memory")) on all MMIO accesses (0xFF00_0000+) and shared queues (0x1007_9000+).
  3. Accessing CRs in inline assembly:
    • The control register index in mtcr/mfcr is an instruction immediate, not a register. Therefore, C helper functions must use macros or a switch over compile-time constants:
      #define RASC_MTCR(cr_num, val) \
          __asm__ volatile ("mtcr %0, %1" :: "r"((unsigned int)(val)), "i"(cr_num) : "memory")
      #define RASC_MFCR(cr_num, out) \
          __asm__ volatile ("mfcr %0, %1" : "=r"(out) : "i"(cr_num))
      
  4. Pseudo-mnemonic rts:
    • The assembler accepts rts as a synonym for jmp lr; objdump prints jmp lr.
  5. K1 and K3 compatibility:
    • The same rasc-elf-gcc 4.2.3 generates valid code for both K1 (2 cores) and K3 (4 cores), as both share the same scalar RASC ISA/ABI.

License: CC-BY-SA-4.0 — Creative Commons Attribution-ShareAlike 4.0 International.