Skip to content

Commit

Permalink
feat(build): Migrate to mips64r2 target for cannon (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored Jan 24, 2025
1 parent a59f643 commit 4b00849
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ see the [SDK section of the book](https://op-rs.github.io/kona/sdk/intro.html).

**Build Pipelines**

- [`cannon`](./build/cannon): Docker image for compiling to the bare-metal `mips-unknown-none` target.
- [`cannon`](./build/cannon): Docker image for compiling to the bare-metal `mips64-unknown-none` target.
- [`asterisc`](./build/asterisc): Docker image for compiling to the bare-metal `riscv64imac-unknown-none-elf` target.

**Protocol**
Expand Down
2 changes: 1 addition & 1 deletion bin/client/src/kona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]
#![cfg_attr(any(target_arch = "mips", target_arch = "riscv64"), no_main)]
#![cfg_attr(any(target_arch = "mips64", target_arch = "riscv64"), no_main)]

extern crate alloc;

Expand Down
2 changes: 1 addition & 1 deletion bin/client/src/kona_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]
#![cfg_attr(any(target_arch = "mips", target_arch = "riscv64"), no_main)]
#![cfg_attr(any(target_arch = "mips64", target_arch = "riscv64"), no_main)]

extern crate alloc;

Expand Down
4 changes: 2 additions & 2 deletions book/src/fpp-dev/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ programs to directly invoke a select few syscalls:

[asterisc-syscalls]: https://github.com/ethereum-optimism/asterisc/blob/master/docs/golang.md#linux-syscalls-used-by-go

## Cannon (MIPS32r2)
## Cannon (MIPS64r2)

Cannon is based off of the `mips32r2` target architecture, specified in [_MIPS32™ Architecture For Programmers Volume III: The MIPS32™ Privileged Resource Architecture_](https://www.cs.cornell.edu/courses/cs3410/2013sp/MIPS_Vol3.pdf)
Cannon is based off of the `mips64r2` target architecture, specified in [MIPS® Architecture For Programmers Volume II-A: The MIPS64® Instruction Set Reference Manual](https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MIPS_Architecture_MIPS64_InstructionSet_%20AFP_P_MD00087_06.05.pdf)

### Syscalls

Expand Down
2 changes: 1 addition & 1 deletion book/src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ experience by standardizing and streamlining the process of developing and compi
**4. Performance**

Kona is opinionated in that it favors `no_std` Rust programs for embedded FPVM development, for both performance and portability. In contrast with alternative approaches, such
as the [`op-program`][op-program] using the Golang `MIPS32` target, `no_std` Rust programs produce much smaller binaries, resulting in fewer instructions
as the [`op-program`][op-program] using the Golang `MIPS64` target, `no_std` Rust programs produce much smaller binaries, resulting in fewer instructions
that need to be executed on the FPVM. In addition, this offers developers more low-level control over interactions with the FPVM kernel, which can be useful
for optimizing performance-critical code.

Expand Down
29 changes: 12 additions & 17 deletions build/cannon/cannon.dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
# This image and the `mips-unknown-none` target were derived from `BadBoiLabs`'s
# `cannon-rs` project.
#
# https://github.com/BadBoiLabs/Cannon-rs

FROM --platform=linux/amd64 ubuntu:22.04

ENV SHELL=/bin/bash
ENV DEBIAN_FRONTEND noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# todo: pin `nightly` version
ENV RUST_VERSION nightly
ENV RUST_VERSION=nightly

RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
ca-certificates \
build-essential \
curl \
g++-mips-linux-gnu \
libc6-dev-mips-cross \
binutils-mips-linux-gnu \
g++-mips64-linux-gnuabi64 \
libc6-dev-mips64-cross \
binutils-mips64-linux-gnuabi64 \
llvm \
clang \
make \
cmake \
git
git

# Install Rustup and Rust
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y --default-toolchain ${RUST_VERSION} --component rust-src
ENV PATH="/root/.cargo/bin:${PATH}"

# Add the special cannon build target
COPY ./mips-unknown-none.json .
COPY ./mips64-unknown-none.json .

# Set up the env vars to instruct rustc to use the correct compiler and linker
# and to build correctly to support the Cannon processor
ENV CC_mips_unknown_none=mips-linux-gnu-gcc \
CXX_mips_unknown_none=mips-linux-gnu-g++ \
CARGO_TARGET_MIPS_UNKNOWN_NONE_LINKER=mips-linux-gnu-gcc \
RUSTFLAGS="-Clink-arg=-e_start -C llvm-args=-mno-check-zero-division" \
CARGO_BUILD_TARGET="/mips-unknown-none.json" \
ENV CC_mips64_unknown_none=mips64-linux-gnuabi64-gcc \
CXX_mips64_unknown_none=mips64-linux-gnuabi64-g++ \
CARGO_TARGET_MIPS64_UNKNOWN_NONE_LINKER=mips64-linux-gnuabi64-gcc \
RUSTFLAGS="-Clink-arg=-e_start -Cllvm-args=-mno-check-zero-division" \
CARGO_BUILD_TARGET="/mips64-unknown-none.json" \
RUSTUP_TOOLCHAIN=${RUST_VERSION}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"arch": "mips",
"cpu": "mips32",
"llvm-target": "mips-unknown-none",
"data-layout": "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64",
"arch": "mips64",
"cpu": "mips64",
"llvm-target": "mips64-unknown-none",
"data-layout": "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128",
"target-endian": "big",
"target-pointer-width": "32",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"features": "+soft-float,+mips32,-mips32r2",
"max-atomic-width": "0",
"features": "+soft-float,+mips64r2,+xgot,+noabicalls",
"max-atomic-width": "64",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"executables": true,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-sdk/std-fpvm-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn client_entry(attr: TokenStream, input: TokenStream) -> TokenStream {
}

cfg_if::cfg_if! {
if #[cfg(any(target_arch = "mips", target_arch = "riscv64"))] {
if #[cfg(any(target_arch = "mips64", target_arch = "riscv64"))] {
const HEAP_SIZE: usize = #heap_size;

#[doc = "Program entry point"]
Expand Down
6 changes: 3 additions & 3 deletions crates/proof-sdk/std-fpvm/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{errors::IOResult, BasicKernelInterface, FileDescriptor};
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(target_arch = "mips")] {
#[doc = "Concrete implementation of the [BasicKernelInterface] trait for the `MIPS32rel1` target architecture."]
pub(crate) type ClientIO = crate::mips32::io::Mips32IO;
if #[cfg(target_arch = "mips64")] {
#[doc = "Concrete implementation of the [BasicKernelInterface] trait for the `MIPS64r2` target architecture."]
pub(crate) type ClientIO = crate::mips64::io::Mips64IO;
} else if #[cfg(target_arch = "riscv64")] {
#[doc = "Concrete implementation of the [BasicKernelInterface] trait for the `riscv64` target architecture."]
pub(crate) type ClientIO = crate::riscv64::io::RiscV64IO;
Expand Down
8 changes: 4 additions & 4 deletions crates/proof-sdk/std-fpvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
html_favicon_url = "https://raw.githubusercontent.com/op-rs/kona/main/assets/favicon.ico"
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(target_arch = "mips", feature(asm_experimental_arch))]
#![cfg_attr(any(target_arch = "mips", target_arch = "riscv64"), no_std)]
#![cfg_attr(target_arch = "mips64", feature(asm_experimental_arch))]
#![cfg_attr(any(target_arch = "mips64", target_arch = "riscv64"), no_std)]

extern crate alloc;

Expand All @@ -29,8 +29,8 @@ pub use channel::FileChannel;

pub(crate) mod linux;

#[cfg(target_arch = "mips")]
pub(crate) mod mips32;
#[cfg(target_arch = "mips64")]
pub(crate) mod mips64;

#[cfg(target_arch = "riscv64")]
pub(crate) mod riscv64;
8 changes: 4 additions & 4 deletions crates/proof-sdk/std-fpvm/src/malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! well-known and widely used allocator software such as OS Kernels.
/// The global allocator for the program in embedded environments.
#[cfg(any(target_arch = "mips", target_arch = "riscv64"))]
#[cfg(any(target_arch = "mips64", target_arch = "riscv64"))]
pub mod global_allocator {
use linked_list_allocator::LockedHeap;

Expand All @@ -32,17 +32,17 @@ pub mod global_allocator {
///
/// # Safety
#[cfg_attr(
any(target_arch = "mips", target_arch = "riscv64"),
any(target_arch = "mips64", target_arch = "riscv64"),
doc = "See [global_allocator::init_allocator] safety comment."
)]
#[cfg_attr(
not(any(target_arch = "mips", target_arch = "riscv64")),
not(any(target_arch = "mips64", target_arch = "riscv64")),
doc = "This macro is entirely safe to invoke in non-MIPS and non-RISC-V64 profiles, and functions as a no-op."
)]
#[macro_export]
macro_rules! alloc_heap {
($size:expr) => {{
#[cfg(any(target_arch = "mips", target_arch = "riscv64"))]
#[cfg(any(target_arch = "mips64", target_arch = "riscv64"))]
{
use $crate::malloc::global_allocator::init_allocator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{errors::IOResult, mips32::syscall, BasicKernelInterface, FileDescriptor};
use crate::{errors::IOResult, mips64::syscall, BasicKernelInterface, FileDescriptor};

/// Concrete implementation of the [BasicKernelInterface] trait for the `MIPS32rel1` target
/// Concrete implementation of the [BasicKernelInterface] trait for the `MIPS64r2` target
/// architecture. Exposes a safe interface for performing IO operations within the kernel.
#[derive(Debug)]
pub(crate) struct Mips32IO;
pub(crate) struct Mips64IO;

/// Relevant system call numbers for the `MIPS32rel1` target architecture.
/// Relevant system call numbers for the `MIPS64r2` target architecture.
///
/// See [Cannon System Call Specification](https://specs.optimism.io/experimental/fault-proof/cannon-fault-proof-vm.html#syscalls)
///
Expand All @@ -16,14 +16,14 @@ pub(crate) struct Mips32IO;
#[repr(usize)]
pub(crate) enum SyscallNumber {
/// Sets the Exited and ExitCode states to true and $a0 respectively.
Exit = 4246,
Exit = 5205,
/// Similar behavior as Linux/MIPS with support for unaligned reads.
Read = 4003,
Read = 5000,
/// Similar behavior as Linux/MIPS with support for unaligned writes.
Write = 4004,
Write = 5001,
}

impl BasicKernelInterface for Mips32IO {
impl BasicKernelInterface for Mips64IO {
fn write(fd: FileDescriptor, buf: &[u8]) -> IOResult<usize> {
unsafe {
crate::linux::from_ret(syscall::syscall3(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module contains raw syscall bindings for the `MIPS32r2` target architecture, as well as a
//! This module contains raw syscall bindings for the `MIPS64r2` target architecture, as well as a
//! high-level implementation of the [crate::BasicKernelInterface] trait for the `Cannon` kernel.
pub(crate) mod io;
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ lint-cannon:
--platform linux/amd64 \
-v `pwd`/:/workdir \
-w="/workdir" \
ghcr.io/op-rs/kona/cannon-builder:main cargo +nightly clippy -p kona-std-fpvm --all-features --target /mips-unknown-none.json -Zbuild-std=core,alloc -- -D warnings
ghcr.io/op-rs/kona/cannon-builder:main cargo +nightly clippy -p kona-std-fpvm --all-features -Zbuild-std=core,alloc -- -D warnings

# Lint the workspace (risc-v arch). Currently, only the `kona-std-fpvm` crate is linted for the `asterisc` target, as it is the only crate with architecture-specific code.
lint-asterisc:
Expand All @@ -81,7 +81,7 @@ lint-asterisc:
--platform linux/amd64 \
-v `pwd`/:/workdir \
-w="/workdir" \
ghcr.io/op-rs/kona/asterisc-builder:main cargo +nightly clippy -p kona-std-fpvm --all-features --target riscv64imac-unknown-none-elf -Zbuild-std=core,alloc -- -D warnings
ghcr.io/op-rs/kona/asterisc-builder:main cargo +nightly clippy -p kona-std-fpvm --all-features -Zbuild-std=core,alloc -- -D warnings

# Lint the Rust documentation
lint-docs:
Expand Down

0 comments on commit 4b00849

Please sign in to comment.