use openvm_platform::{fileno::*, memory::sys_alloc_aligned, rust_rt::terminate, WORD_SIZE};
use openvm_rv32im_guest::{hint_buffer_chunked, hint_random, raw_print_str_from_bytes};
const DIGEST_WORDS: usize = 8;
pub mod exit_code {
pub const SUCCESS: u8 = 0;
pub const PANIC: u8 = 1;
pub const UNIMP: u8 = 2;
pub const HALT: u8 = 4;
pub const PAUSE: u8 = 5;
}
#[inline(never)]
#[no_mangle]
pub extern "C" fn sys_halt(_user_exit: u8, _out_state: *const [u32; DIGEST_WORDS]) -> ! {
terminate::<{ exit_code::HALT }>();
unreachable!()
}
#[no_mangle]
pub extern "C" fn sys_output(_output_id: u32, _output_value: u32) {
terminate::<{ exit_code::UNIMP }>();
}
#[inline(always)]
#[no_mangle]
pub unsafe extern "C" fn sys_sha_compress(
_out_state: *mut [u32; DIGEST_WORDS],
_in_state: *const [u32; DIGEST_WORDS],
_block1_ptr: *const [u32; DIGEST_WORDS],
_block2_ptr: *const [u32; DIGEST_WORDS],
) {
unreachable!("sha_compress should not be part of PAL")
}
#[inline(always)]
#[no_mangle]
pub unsafe extern "C" fn sys_sha_buffer(
_out_state: *mut [u32; DIGEST_WORDS],
_in_state: *const [u32; DIGEST_WORDS],
_buf: *const u8,
_count: u32,
) {
unreachable!("sha_buffer should not be part of PAL")
}
#[no_mangle]
pub unsafe extern "C" fn sys_rand(recv_buf: *mut u32, words: usize) {
hint_random(words);
hint_buffer_chunked(recv_buf as *mut u8, words);
}
#[no_mangle]
unsafe extern "C" fn sys_panic(msg_ptr: *const u8, len: usize) -> ! {
raw_print_str_from_bytes(msg_ptr, len);
terminate::<{ exit_code::PANIC }>();
unreachable!()
}
#[no_mangle]
pub unsafe extern "C" fn sys_log(msg_ptr: *const u8, len: usize) {
raw_print_str_from_bytes(msg_ptr, len);
}
#[no_mangle]
pub extern "C" fn sys_cycle_count() -> u64 {
crate::io::println("TODO");
terminate::<{ exit_code::UNIMP }>();
0u64
}
#[no_mangle]
pub unsafe extern "C" fn sys_read(_fd: u32, _recv_ptr: *mut u8, _nread: usize) -> usize {
crate::io::println("sys_read is todo");
terminate::<{ exit_code::UNIMP }>();
0
}
#[no_mangle]
pub unsafe extern "C" fn sys_read_words(_fd: u32, _recv_ptr: *mut u32, _nwords: usize) -> usize {
crate::io::println("sys_read_words is todo");
terminate::<{ exit_code::UNIMP }>();
0
}
#[no_mangle]
pub unsafe extern "C" fn sys_write(fd: u32, write_ptr: *const u8, nbytes: usize) {
if fd == STDOUT || fd == STDERR {
raw_print_str_from_bytes(write_ptr, nbytes);
} else {
use core::fmt::Write;
let mut writer = crate::io::Writer;
let _ = write!(writer, "sys_write to fd={fd} not supported.\n");
terminate::<{ exit_code::UNIMP }>();
}
}
#[no_mangle]
pub unsafe extern "C" fn sys_getenv(
_out_words: *mut u32,
_out_nwords: usize,
_varname: *const u8,
_varname_len: usize,
) -> usize {
crate::io::println("sys_getenv is todo; returning 0");
0
}
#[no_mangle]
pub extern "C" fn sys_argc() -> usize {
crate::io::println("sys_argc is todo; returning 0");
0
}
#[no_mangle]
pub unsafe extern "C" fn sys_argv(
_out_words: *mut u32,
_out_nwords: usize,
_arg_index: usize,
) -> usize {
crate::io::println("sys_argv is todo; returning 0");
0
}
#[no_mangle]
#[deprecated]
pub extern "C" fn sys_alloc_words(nwords: usize) -> *mut u32 {
unsafe { sys_alloc_aligned(WORD_SIZE * nwords, WORD_SIZE) as *mut u32 }
}