Strata — memory is explicit I/O

10 min read Original article ↗

01The idea

Modern CPUs pretend memory is flat and fast, then spend a third of the chip keeping up the pretence. Strata stops pretending. What software can reach directly is small and fast; everything else is a transfer you ask for, and can overlap with work.

Local Stores, not caches

Loads and stores reach only registers and a 64 KiB data Local Store per core. Instructions come only from a 64 KiB instruction Local Store. L1 is an architectural scratchpad, and software decides what lives there.

Transfers, as if synchronous

tget, tput and tgetx move data between tiers with completion tags. Each takes effect at its issue point; the hardware stalls any access to a range still in flight. Overlap is a performance choice, never a correctness one.

Linear capabilities

Far memory is named only through capabilities. Writable ones can be split, joined, given to another core and taken back — never copied. A data race is not something you can write down. Revocation domains take everything back at once.

No interrupts

A device “interrupts” by completing a transfer. tany waits for any of a set of transfers, or for a capability arriving in the inbox. Ctrl-C is a console read that completes when you press the key. The timer is a read that completes at a deadline.

02The tiers, all visible

Registersx0–x31 · c0–c15

Local Stores64 KiB I-LS + 64 KiB D-LS per core · load/store

T2 cluster store256 KiB · tget / tput

T3 system memory64 MiB · tget / tput / tamo

Devicesconsole · disk · timer · framebuffer

; divide the work by dividing ownership
        csplit c5, c0, s6     ; c0 = my piece, c5 = the rest
        csplit c8, c5, s6     ; c5 = core k's piece
        cgive  c5, s3         ; hand it over (c5 is now empty)
        ...
        ctake  c5, s3         ; take it back from core k only
        cjoin  c0, c5         ; and glue the array together

; stream far data while computing
        tget   tg1, a0, c0, t0, t1   ; ask for the next block
        ...                          ; work on the current one
        twait  tg1                   ; (loads would wait anyway)
A Mandelbrot set rendered by eight Strata cores into the framebuffer
examples/gfx.s — eight cores each own one band of the framebuffer through a capability and render into it. The same frame, byte for byte, on the emulator and the Verilog RTL.

03StrataOS

A microkernel written in Strata assembly, where the explicit machine shows through instead of being hidden. Its architecture, the kernel, and every server's messages →

A kernel that pages itself

A few kilobytes stay resident. The rest of the kernel is code units it loads into its own instruction store on demand — the same xcall / xmiss mechanism programs use.

Servers are processes

proc, dev, fs, tty, pipe and the window server ws run in user mode. A message is a lent buffer capability; programs talk to servers directly, core to core, in hardware.

Context switches you can see

Preemptive multitasking with a configurable time slice (2 ms by default, down to 100 µs). A switch copies the used Local Store out and in — the cache flush other CPUs hide. ps shows each process's switches and kilobytes moved; the window server's core lattice pulses with every one.

strata$ seq 30 | grep 2 | wc      # three processes, two pipes
strata$ -n 3 mandel &             # three threads, time-shared
strata$ as < hello.s > hi && hi   # the assembler, written in Strata
strata$ ps                        # switches and KiB moved, per process
strata$ ws                        # the window server takes the screen

The window server

ws is the only process that touches the framebuffer. It tiles windows on a grid, and makes the machine visible: the strip at the top has one cell per core, lit in the colour of the process running there, pulsing on every context switch; each window's tag shows how many kilobytes its context switches have moved. Clients draw in their own memory and lend each finished band to the server as a capability; only the server writes the screen.

The StrataOS window server: four tiled windows, a terminal showing ps, a Mandelbrot, a clock and a terminal showing cores, under a strip of core cells
StrataOS 0.1 running ws: two terminals with their own shells, wmandel and wclock, each a separate process, at 1280×800. Boot StrataOS in your browser and type ws →  or watch the scripted demo.
A first-person view of a grey hall with a zombie in front of a pistol's muzzle flash and a status bar underneath, in a window of the StrataOS window server
wdoom: a first-person shooter in Strata assembly, one more client of the window server, with art and sounds from Freedoom. A core's data store is smaller than one frame, so the picture is drawn in ten slabs of 32 columns and every texture column is fetched from far memory. Play it in your browser →
Four pictures of a children's game in pastel pixel art: a castle hall seen from above with a little princess and a duckling, a garden party with a duck and a bluebird under the word HOORAY, a garden with flowers in patches of soil, and a colouring page of a castle with one part painted purple
kira: Princess Kira, four gentle games for a small child in Strata assembly: find seven animal friends in a castle and garden that are made anew for every game (seen from above, or through Kira's eyes with the raycaster of wdoom), plant a garden, colour a page, play a xylophone. No enemies, no timer, no way to lose. Play it in your browser →
A city-building game in bright pixel art: a small town by a river, with the town's yearly paper, the Plat Ledger, open over it: a headline about traffic jams, the residents, jobs, treasury and the mayor's approval
plat: Plat, a city-building game in Strata assembly: new land for every game, roads, power lines and plants, zones for homes, shops and industry that grow while they have power, a road and demand; rail, police, fire, parks, solar power; maps of pollution, crime, traffic and land value; a budget, advisers and a yearly paper; five zoom levels, and the town kept in a file with its population as an attribute. Play it in your browser →
A two-panel file navigator in blue and cyan: on the left the results of searching for vi across the whole disk, each file with the directory it is in; on the right a quick view of the selected file, the source of the vi editor
nav: a file navigator in the Norton / Midnight Commander manner for a file system where a directory is the set of its files' tags. Typing anywhere searches the whole disk as you type, through a call the file server answers from its index of names: about fifteen milliseconds to the first results on a disk of ten thousand files. Try it in your browser →

04Strata Forth

A Forth bootstrapped the classic way — a small kernel of primitives in assembly, the rest of the language defined in Forth — with a modern twist: words compile to native Strata code. Because stores cannot write instruction memory, every definition is assembled in data memory, written to far memory and installed with tgetx: a JIT whose install step you can watch.

  • Inlining, constant folding and tail calls
  • Capabilities, transfers and tags as ordinary words
  • far@ / far! through a software cache built on ltag / linst
  • spawn / await across cores
  • Locals, quotations, catch/throw, see

Every word, with its source, and a playground →
Try Forth in your browser →

: sq ( n -- n*n ) dup * ;  ok
7 sq . 49  ok
see sq
: sq  ( xt $8100 , 40 bytes)
  8100:  addi sp, sp, -8
  8104:  sd   ra, 0(sp)
  8108:  addi s0, s0, -8
  810c:  sd   s1, 0(s0)
  8110:  ld   t0, 0(s0)
  8114:  addi s0, s0, 8
  8118:  mul  s1, t0, s1
  811c:  ld   ra, 0(sp)
  8120:  addi sp, sp, 8
  8124:  ret
 ok
: sum-sq ( n -- ) 0 swap 0 do i sq + loop ;  ok
100 sum-sq . 328350  ok
A Mandelbrot set rendered in parallel by Strata Forth
lang/forth/demos/gfx.fs — the framebuffer cut into bands with csplit, rendered by workers spawned from the Forth prompt.

05Stratalk, a Smalltalk

A Smalltalk whose object memory follows the machine. New objects are made near, in a nursery in the core's data store; the survivors are written out to far memory in one transfer. Far objects are read through a software cache on ltag / linst, and every far store is a transfer you can count. Frames page out to a far stack when recursion runs deep, and a JIT written in Stratalk compiles hot methods into code units that StrataOS pages into the instruction store.

  • Pharo's syntax, closures with non-local return, ANSI exceptions, large integers
  • A compiler written in Stratalk, checked byte for byte against the bootstrap one
  • A REPL with completion; files; a workspace, class browser and inspector in the window server
  • Every StrataOS server's messages as classes, generated from their specifications

The classes, the examples and a playground →

strata$ stratalk
st> 100 factorial printString size
158
st> [10 / 0] on: ZeroDivide do: [:e | e messageText]
'division by zero'
st> (1 to: 10) inject: 0 into: [:a :b | a + b]
55
st> Smalltalk vm at: 'scavenges'
32
The Stratalk class browser in a window of the StrataOS window server
stratalk -b: the class browser, a client of the window server written in Stratalk; the source comes from the kernel's files on the disk. Try Stratalk in your browser →

06Stralog, a Prolog

A Prolog in Strata assembly whose terms all live in far memory, reached only by transfers. The data Local Store holds windows onto the tops of the heap, the trail and the choicepoint stack, and a small write-back cache for everything else; the engine slides a window or spills it with an explicit tget or tput, and statistics counts every byte that moved.

  • ISO core Prolog: cut, if-then-else, call/N, catch/throw with ISO error terms
  • Assert and retract, findall, bagof/setof, DCGs, op/3
  • 584 of 596 ISO conformance vectors pass
  • An engine bigger than the instruction store, in code units StrataOS pages in

Every predicate, the examples and a playground →
Try Stralog in your browser →

strata$ -m 8 stralog
?- append(X, Y, [1, 2]).
X = [], Y = [1,2] ;
X = [1], Y = [2] ;
X = [1,2], Y = [] ;
false.
?- [queens].
true.
?- count_queens(8, N).
N = 92.
?- [family].
true.
?- findall(D, ancestor(tom, D), Ds).
Ds = [bob,liz,ann,pat,jim].
?- statistics.
calls: 134488
heap: 4112 of 6899712 bytes used; window: 485 spills, 1857 slides, 2090432 bytes moved
garbage collections: 0, 0 bytes given back
trail: 16 bytes used; window: 1 spills, 1 slides, 512 bytes moved
choicepoints: 4 frames; window: 0 spills, 0 slides, 0 bytes moved
far cache: 74618 misses, 5583 write-backs (64-byte lines)
atoms: 16616 bytes, clauses: 90352 bytes, findall: 0 bytes
true.

07C, compiled on the machine

cc is a C99 compiler that runs inside StrataOS and writes program images directly: one pass, no assembler, no linker. It is written in C and compiles itself; the first compiler, cc0, is the same language in Strata assembly, and compiling the compiler with itself twice gives the same bytes as the cc on the disk.

  • Both compilers pass 521 tests from the suites of two other C compilers
  • asm("...") and intrinsics for what only Strata has: transfers, capabilities
  • 106 KiB of compiler in a 32 KiB instruction store: it pages itself
  • The same source builds as a cross compiler on your machine

The bootstrap, the machine from C, the tests →
Compile C on Strata in your browser →

strata$ cc /src/hi.c
strata$ hi there
Hello from C on Strata, there!
strata$ cc0 -p -h 0 -o cc1 /src/cc.c   # assembly compiles the compiler
strata$ cc1 -p -h 0 -o c2 /src/cc.c    # which compiles itself
strata$ cmp c2 /bin/cc
c2 /bin/cc: same

08Try it in your browser

The reference emulator, compiled to WebAssembly. Boot StrataOS on 12 cores and type at its shell, watch eight cores draw a Mandelbrot set into the framebuffer, or play the games. Each language has its own start: Forth, whose words run as native Strata code; Stratalk's REPL; Stralog's top level; C, compiled by the compiler on the disk. No install.

Run Strata in your browser

09Run it

Needs a C compiler and Verilator 5; SDL2 is optional (for the window). Or read it first: the source browser has every assembly, Forth and C program, syntax-coloured.

makeassembler, emulator and the Verilated RTL

make testevery example and an OS boot on both machines, compared instruction by instruction

make show-gfxopen the framebuffer window and watch eight cores draw

make bootboot StrataOS on the emulator

make fortha Forth prompt on bare metal