Contents

PerlOS runs a Perl interpreter directly on a small bare-metal system, providing a serial console and files embedded in the executable. Build it with QEMU and trace the path from CPU reset to the Perl prompt. Setup and compilation times vary, so treat one hour as an estimate.

Key Points#

  • Start with QEMU’s Cortex-M33 emulation to test startup code and UART output without physical hardware.
  • Before adding Perl, implement the C runtime features needed to initialize memory, output text, and allocate heap space.
  • Use PerlOS release v0.1.1, as specified in the tutorial. It ports microperl from Perl 5.12.5.
  • Get the shell running in QEMU before tackling the Raspberry Pi Pico 2, which requires additional work to address memory constraints.

Prepare the Build Tools#

Install Make, a host Perl interpreter, curl, an Arm bare-metal compiler, and QEMU’s Arm system emulator. The package installation commands for Ubuntu or Debian are:

snippet.txt
sudo apt update
sudo apt install make perl curl tar gcc-arm-none-eabi qemu-system-arm ffmpeg

arm-none-eabi-gcc --version
qemu-system-arm --version
perl --version

mkdir perlos
cd perlos

ffplay, included with FFmpeg, is needed only for the optional RAM viewer. On macOS, use Homebrew, but first check the latest package information and tap instructions for arm-none-eabi-gcc.

Use tutorial files from tag v0.1.1 throughout. The scripts, patches, and build rules must be used together. Perl 5.12.5 is an old dependency chosen for this exercise, not the latest Perl release.

Boot and Print a Line#

Follow the accompanying exercise to create boot.S, kernel.c, qemu.ld, and run.sh. The linker script places code at 0x10000000 and RAM at 0x80000000 for QEMU’s mps2-an505 board. These addresses are specific to this target.

On reset, the Cortex-M33 reads the initial stack pointer and reset handler address from the vector table. The handler calls kernel_main(). The vector entry for the handler must indicate Thumb state in its least significant bit. Assembler symbol attributes affect address encoding, so inspect the linked vector table if boot fails.

Next, add UART output. This board’s UART registers start at 0x50200000, with DATA at offset 0, STATE at 4, and CTRL at 8. While bit 0 of STATE is 1, the transmit buffer is full; wait, then write one character to DATA. Use volatile accesses so the compiler does not omit hardware reads or writes. Run bash run.sh, confirm that Hello, kernel! appears, then press Ctrl+A followed by X to exit QEMU.

Prepare the C Runtime#

Complete startup initialization before adding the interpreter. Copy .data from its load address into RAM and clear .bss to zero. C code assumes these conditions even when no OS launches the program.

Follow the exercise to implement character input, a small printf(), and heap allocation. The initial formatter supports only %s, %c, %d, %x, and %%. The initial allocator reserves a 1 MiB heap and advances a pointer; it cannot reclaim memory. Perl needs memory allocation, deallocation, and resizing, so replace it with the supplied allocator before running Perl.

Use the get.sh helper from the tutorial’s specified version to fetch libc/, script/, root/lib/, and the ROM filesystem sources. Examine libc/stdio.c and libc/heap.c to understand the runtime services that ordinary C applications receive from existing libraries.

Embed Perl and Files#

Fetch the supplied Makefile, AN505 I/O backend, and Perl embedding files. Replace the exercise versions of kernel.c and main.c with the completed versions. Initialize timing and I/O, mount the embedded filesystem, then call perlos_repl().

Create root/boot.pl to print a welcome message. Add the empty root/dev/null file required by this port, and include root/lib/ and root/timethis.pl. ROMFS2 packages these files into C arrays and links them into the executable. Any changes under root/ require rebuilding the image.

Run make run. The first build downloads Perl 5.12.5, applies the supplied bare-metal patches, and compiles the interpreter. At the prompt, enter 1 + 2 and confirm that it returns 3, then try do "timethis.pl". Use :quit to exit. This completes the path: reset handler → C startup → filesystem initialization → interpreter → expression evaluation.

Review the Limitations#

This system provides a Perl shell, a small runtime, and direct hardware access. It does not provide process isolation or general-purpose services like a desktop OS. Verify unfamiliar memory addresses before using PerlOS::load(). The UART’s STATE value changes with device activity.

To inspect memory, use make ramview, which uses QMP and a host-side viewer. In the supplied release, improve portability by changing the dump path from /dev/shm/perlos-ram.bin to /tmp/perlos-ram.bin and adding the connection retry loop described in the documentation. A graphical desktop environment is required. Treat benchmark results as measurements for that emulator configuration.

Support for the Raspberry Pi Pico 2 is a separate hardware target. It has the same Cortex-M33 core but requires its own startup code, memory layout, and USB console backend. This port uses NV=float to match the single-precision arithmetic hardware and improves heap reuse to fit within the limited RAM. However, the accompanying materials do not provide complete, verified build instructions for physical hardware.

Next Steps#

Build the first QEMU exercise and confirm that Hello, kernel! appears before adding the C runtime and microperl.

Source: https://zenn.dev/takesako/articles/e9b10360d889ca

Contents