Function rustsbi::ecall[][src]

pub fn ecall(extension: usize, function: usize, param: [usize; 5]) -> SbiRet
Expand description

Supervisor environment call handler function

This function is used by platform runtime to handle environment call ecall instruction.

You should call this function in your runtime’s exception handler. If the incoming exception is caused by supervisor ecall, call this function with parameters extracted from trap frame. After this function returns, store the return value into a0 and a1 parameters.

This function also adapts to the legacy functions. If the supervisor called any of legacy function, the a0 and a1 parameter is transferred to SbiRet’s error and value respectively. So you should store the result into a0 and a1 in any function calls including legacy functions.

Example

A typical usage:

#[exception]
fn handle_exception(ctx: &mut TrapFrame) {
    if mcause::read().cause() == Trap::Exception(Exception::SupervisorEnvCall) {
        let params = [ctx.a0, ctx.a1, ctx.a2, ctx.a3, ctx.a4];
        let ans = rustsbi::ecall(ctx.a7, ctx.a6, params);
        ctx.a0 = ans.error;
        ctx.a1 = ans.value;
        mepc::write(mepc::read().wrapping_add(4));
    }
    // other conditions..
}

Do not forget to advance mepc by 4 after an ecall is handled. This skips the ecall instruction itself which is 4-byte long in all conditions.