-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: import code from zeam-runtime
- Loading branch information
Showing
17 changed files
with
566 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
MEMORY | ||
{ | ||
RAM : ORIGIN = 0x80000000, LENGTH = 1024M | ||
ROM : ORIGIN = 0x20000000, LENGTH = 16M | ||
HINTS: ORIGIN = 0x40000000, LENGTH = 1024M | ||
} | ||
|
||
REGION_ALIAS("REGION_TEXT", ROM); | ||
REGION_ALIAS("REGION_RODATA", ROM); | ||
REGION_ALIAS("REGION_DATA", RAM); | ||
REGION_ALIAS("REGION_BSS", RAM); | ||
REGION_ALIAS("REGION_HEAP", RAM); | ||
REGION_ALIAS("REGION_STACK", RAM); | ||
|
||
REGION_ALIAS("REGION_HINTS", HINTS); | ||
|
||
_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK); | ||
_hints_start = ORIGIN(REGION_HINTS); | ||
_hints_length = LENGTH(REGION_HINTS); | ||
_lengths_of_hints_start = ORIGIN(REGION_HINTS); | ||
|
||
SECTIONS | ||
{ | ||
.text : | ||
{ | ||
KEEP(*(.init)); | ||
. = ALIGN(4); | ||
*(.text .text.*); | ||
} > ROM | ||
|
||
.rodata : ALIGN(4) | ||
{ | ||
*(.srodata .srodata.*); | ||
*(.rodata .rodata.*); | ||
} > ROM | ||
|
||
.data : ALIGN(4) | ||
{ | ||
/* Must be called __global_pointer$ for linker relaxations to work. */ | ||
PROVIDE(__global_pointer$ = . + 0x800); | ||
|
||
*(.sdata .sdata.*); | ||
*(.sdata2 .sdata2.*); | ||
*(.data .data.*); | ||
} > RAM | ||
|
||
.bss (NOLOAD) : ALIGN(4) | ||
{ | ||
*(.sbss .sbss.*); | ||
*(.bss .bss.*); | ||
|
||
. = ALIGN(4); | ||
_sheap = .; | ||
} > RAM | ||
|
||
/* Define a section for runtime-populated EEPROM-like HINTS data */ | ||
.hints (NOLOAD) : ALIGN(4) | ||
{ | ||
*(.hints .hints.*); | ||
} > HINTS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const std = @import("std"); | ||
|
||
var info_out: [*]volatile u32 = @ptrFromInt(0x8010_0000); | ||
|
||
var cursor: usize = 0; | ||
|
||
fn alloc(msg_len: usize) []volatile u32 { | ||
// This isn't thread-safe, but it doesn't matter right now | ||
// as there are no threads in this environment. | ||
const old_cursor = cursor; | ||
cursor += (msg_len + 3) & 0xFFFFFFFC; // word-align | ||
return info_out[old_cursor..cursor]; | ||
} | ||
|
||
pub fn print_str(str: []const u8) void { | ||
var buf = alloc(str.len); | ||
// @memcpy seems to greatly increase the heap size, which takes | ||
// the prover down. Do it manyally for now. | ||
// @memcpy(buf[0..], std.mem.bytesAsSlice(u32, buf)); | ||
const as_u32 = std.mem.bytesAsSlice(u32, buf); | ||
for (as_u32, 0..) |word, i| { | ||
if (i < str.len / 4) { | ||
buf[i] = word; | ||
} else { | ||
const mask: u32 = @as(u32, 0xFFFFFFFF) >> @truncate(8 * (4 * i - str.len)); | ||
buf[i] = word & mask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const std = @import("std"); | ||
const syscalls = @import("./syscalls.zig"); | ||
pub const io = @import("./io.zig"); | ||
|
||
pub fn halt(exit_code: u32) noreturn { | ||
asm volatile ("ecall" | ||
: | ||
: [exit_code] "{a0}" (exit_code), | ||
[arg] "{t0}" (syscalls.halt_call), | ||
); | ||
unreachable; | ||
} | ||
|
||
pub fn keccack_permute(state: []u64) !void { | ||
if (state.length != 25) { | ||
return error.InvalidKeccakInputSize; | ||
} | ||
|
||
asm volatile ("ecall" | ||
: | ||
: [scallnum] "{t0}" (syscalls.keccack_permute), | ||
[buf] "{a0}" (&state), | ||
[arg1] "{a1}" (0), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.section .init | ||
.global _start | ||
_start: | ||
.option push | ||
.option norelax | ||
la gp, __global_pointer$ | ||
.option pop | ||
|
||
la sp, _stack_start | ||
mv fp, sp | ||
|
||
call main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub const halt_call = 0; | ||
pub const keccack_permute = 0x10109; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const syscalls = @import("./syscalls.zig").syscalls; | ||
|
||
pub fn print_str(str: []const u8) void { | ||
for (str) |c| { | ||
asm volatile ("ecall" | ||
: | ||
: [scallnum] "{t0}" (@intFromEnum(syscalls.output)), | ||
[subcommand] "{a0}" (0), | ||
[arg1] "{a1}" (c), | ||
: "memory" | ||
); | ||
} | ||
} | ||
|
||
pub fn read_u32(idx: u32) u32 { | ||
return asm volatile ("ecall" | ||
: [ret] "={a0}" (-> u32), | ||
: [scallnum] "{t0}" (@intFromEnum(syscalls.input)), | ||
[fd] "{a0}" (0), // fd = 0 == stdin | ||
[idx] "{a1}" (idx + 1), | ||
: "memory" | ||
); | ||
} | ||
|
||
pub fn read_data_len(fd: u32) usize { | ||
return asm volatile ("ecall" | ||
: [ret] "={a0}" (-> usize), | ||
: [scallnum] "{t0}" (@intFromEnum(syscalls.input)), | ||
[subcommand] "{a0}" (fd), | ||
[idx] "{a1}" (0), | ||
: "memory" | ||
); | ||
} | ||
|
||
pub fn read_slice(fd: u32, data: []u32) void { | ||
for (data, 0..) |*d, idx| { | ||
var item: u32 = undefined; | ||
asm volatile ("ecall" | ||
: [ret] "={a0}" (item), | ||
: [scallnum] "{t0}" (@intFromEnum(syscalls.input)), | ||
[subcommand] "{a0}" (fd), | ||
[idx] "{a1}" (idx + 1), | ||
: "memory" | ||
); | ||
d.* = item; | ||
} | ||
} | ||
|
||
pub fn write_u8(fd: u32, byte: u8) void { | ||
asm volatile ("ecall" | ||
: | ||
: [scallnum] "{t0}" (@intFromEnum(syscalls.output)), | ||
[fd] "{a0}" (fd), | ||
[arg1] "{a1}" (byte), | ||
: "memory" | ||
); | ||
} | ||
|
||
pub fn write_slice(fd: u32, data: []const u8) void { | ||
for (data) |c| { | ||
write_u8(fd, c); | ||
} | ||
} |
Oops, something went wrong.