1、sbi_ecall函数功能
(1)sbi_ecall函数是内核调用SBI的接口,在RISC-V架构中定义了SBI规范,内核通过ecall指令来调用SBI接口进而操作硬件;
(2)SBI规范参考官网文档《riscv-sbi.pdf》;
2、sbi_ecall函数源码
struct sbiret
{long error;long value;
};//ext对应SBI规范的拓展EID,fid对应SBI规范的功能FID,arg0-arg5是传参
struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,unsigned long arg1, unsigned long arg2,unsigned long arg3, unsigned long arg4,unsigned long arg5)
{struct sbiret ret;register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);asm volatile ("ecall": "+r" (a0), "+r" (a1): "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7): "memory");ret.error = a0;ret.value = a1;return ret;
}
函数代码摘抄自内核源码:
arch/riscv/kernel/sbi.c
3、ecall指令传参分析
(1)ecall通过寄存器a0-a7传递参数,其中a6传递功能ID,a7传递拓展ID;
(2)其中a0和a1不仅做传入参数,还需要做传出参数,a0传递错误码,a1传递返回值;
补充:要看懂需要了解内嵌汇编,参考博客:《RISC-V架构学习——C语言内嵌汇编总结》;
4、调用ecall指令后跳转执行什么代码?
(1)在S模式或者U模式调用ecall指令,会陷入到M模式,执行M模式的异常处理函数,异常处理函数的地址在启动阶段需要设置到mtvec寄存器中,硬件会自动跳转;
(2)要明白上述的代码,需要了解RISC-V的不同模式下的寄存器、中断机制、SBI规范;