Memory Management
Syscalls Overview
Syscalls in the ThruVM provide programs with access to runtime services and system resources. They enable programs to manage memory, manipulate accounts, transfer funds, and interact with the broader system.
Syscall Listing
Section titled “Syscall Listing”Account Management
tsys_set_account_data_writable
Code 0x02 - Marks account data writable by program
tsys_account_transfer
Code 0x03 - Transfers balance between accounts
tsys_account_create
Code 0x04 - Creates permanent account with proof
tsys_account_create_ephemeral
Code 0x05 - Creates temporary account
tsys_account_delete
Code 0x06 - Marks account as deleted
tsys_account_resize
Code 0x07 - Changes account data size
tsys_account_set_flags
Code 0x0E - Modifies account flags
Compression & Storage
Program Control
Logging & Events
Calling Convention
Section titled “Calling Convention”ThruVM syscalls follow a standardized calling convention using RISC-V registers:
- Syscall number: Placed in register
a7 - Arguments: Passed in registers
a0througha6(up to 7 arguments) - Return value: Placed in register
a0 - Invocation: Use the
ecallinstruction to trigger the syscall
C SDK Interface
Section titled “C SDK Interface”The C SDK provides convenient wrapper functions for all syscalls. Include <thru-sdk/c/tn_sdk_syscall.h> to access these functions:
#include <thru-sdk/c/tn_sdk_syscall.h>
// Example: Transfer funds between accountsulong result = tsys_account_transfer(from_account, to_account, amount);if (result == TN_VM_SYSCALL_SUCCESS) { // Transfer successful}Raw Assembly Template
Section titled “Raw Assembly Template”For direct assembly usage:
# Set arguments in a0-a6li a0, account_idxli a1, amount# Set syscall number in a7li a7, TN_SYSCALL_CODE_ACCOUNT_TRANSFER# Invoke syscallecall# Check return value in a0Compute Units
Section titled “Compute Units”Most syscalls consume compute units:
- Typical base cost:
TN_VM_SYSCALL_BASE_COST(512 units) - Additional costs: Vary by syscall complexity and data size
- Current exceptions:
exitandlogcurrently apply zero CU charge in implementation
Error Handling
Section titled “Error Handling”Syscalls return standardized error codes:
TN_VM_SYSCALL_SUCCESS(0) - Operation successful- Negative values indicate specific error conditions
- Programs should always check return values
Usage Patterns
Section titled “Usage Patterns”Memory Management
Section titled “Memory Management”Use memory syscalls to allocate and manage anonymous data segments for program scratch space and dynamic data structures.
Account Operations
Section titled “Account Operations”Account syscalls enable creating, modifying, and managing accounts within transactions. Always verify account ownership and permissions.
Cross-Program Calls
Section titled “Cross-Program Calls”Use tsys_invoke and tsys_exit syscalls to build modular programs that can call each other while maintaining security boundaries.
State Management
Section titled “State Management”Compression syscalls allow efficient storage of large account states with cryptographic proofs for verification.
Debugging & Monitoring
Section titled “Debugging & Monitoring”Logging and event syscalls provide visibility into program execution and enable external monitoring.