blob: 5e24828f3f33340757bed3be7eb89f857e0649d5 [file] [log] [blame] [view]
Rupert Swarbrick9a47a362020-08-07 12:23:23 +01001---
2title: "Writing and building software for OTBN"
3---
4
5OTBN is the OpenTitan Big Number accelerator and this document describes how to write and build software for it.
6The [OTBN reference manual]({{< relref "hw/ip/otbn/doc" >}}) describes the hardware and associated ISA.
7
8Since OTBN is designed for cryptographic offload, not general computation, it has no C compiler.
9However, it does have the usual binutils programs: an assembler, linker and disassembler.
10The core of OTBN's instruction set is based on RV32I, the RISC-V base integer instruction set.
11As such, we implement the toolchain as a thin wrapper around RISC-V binutils.
12
Philipp Wagnerc120b182020-09-17 11:53:03 +010013## Assembly format and examples
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010014
15The OTBN ISA and programmer model are described in the [OTBN reference manual]({{< relref "hw/ip/otbn/doc" >}}).
16In particular, note that the register `x1` has special stack-like behaviour.
Philipp Wagner0f643e12020-08-17 16:50:05 +010017There are some example programs at `sw/otbn/code-snippets`.
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010018These range from simple examples of how to use pseudo-operations up to example cryptographic primitives.
19
Philipp Wagnerc120b182020-09-17 11:53:03 +010020## The tools
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010021
Philipp Wagnerc120b182020-09-17 11:53:03 +010022### Assembler
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010023
24The OTBN assembler is called `otbn-as` and can be found at `hw/ip/otbn/util/otbn-as`.
25This has the same command line interface as `riscv32-unknown-elf-as` (indeed, it's a wrapper around that program).
26The only difference in default flags is that `otbn-as` passes `-mno-relax`, telling the assembler not to request linker relaxation.
27This is needed because one of these relaxations generates GP-relative loads, which assume `x3` is treated as a global pointer (not true for OTBN code).
28
Philipp Wagnerb5b8c7f2020-10-27 10:44:27 +000029To assemble some code in `foo.s` to an ELF object called `foo.o`, run:
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010030```shell
Philipp Wagnerb5b8c7f2020-10-27 10:44:27 +000031hw/ip/otbn/util/otbn-as -o foo.o foo.s
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010032```
33
Philipp Wagnerc120b182020-09-17 11:53:03 +010034### Linker
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010035
36The OTBN linker is called `otbn-ld` and can be found at `hw/ip/otbn/util/otbn-ld`.
37This is a thin wrapper around `riscv32-unknown-elf-ld`, but supplies a default linker script that matches the OTBN memory layout.
38This linker script creates a `.text` and a `.data` section.
39Since OTBN has a strict Harvard architecture (instructions and data both starting at address zero), the linker script places them both at VMA zero.
40The instruction and data segments have distinct LMAs (for addresses, see the IMEM and DMEM windows at `hw/ip/otbn/data/otbn.hjson`).
41
42To link ELF object files to an OTBN ELF binary, run
43```shell
44hw/ip/otbn/util/otbn-ld -o foo foo0.o foo1.o foo2.o
45```
46
Philipp Wagnerc120b182020-09-17 11:53:03 +010047### Objdump
Rupert Swarbrick9a47a362020-08-07 12:23:23 +010048
49To disassemble OTBN code, use `otbn-objdump`, which can be found at `hw/ip/otbn/util/otbn-objdump`.
50This wraps `riscv32-unknown-elf-objdump`, but correctly disassembles OTBN instructions when run with the `-d` flag.
51
52To disassemble the ELF binary linked in the previous section, run
53```shell
54hw/ip/otbn/util/otbn-objdump -d foo
55```