//file: include/linux/syscalls.h
....
asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,size_t vlen, int behavior, unsigned int flags);
....
+asmlinkage long sys_mytest(int id); //新增系统调用声明
int main(void)
{int id = 0;id = syscall(441, 100);printf("result : %d\n", id);return 0;
}~# ./mytest
result : 100
标准C库
程序中调用的syscall来自标准C库,根据源码可知:应用层系统调用接口是封装的syscall。
当前使用的标准C库(musl)syscall源码如下:
//file: musl-1.2.1/arch/riscv64/syscall_arch.h
...
#define __asm_syscall(...) \__asm__ __volatile__ ("ecall\n\t" \: "=r"(a0) : __VA_ARGS__ : "memory"); \return a0; \static inline long __syscall0(long n)
{register long a7 __asm__("a7") = n;register long a0 __asm__("a0");__asm_syscall("r"(a7))
}
...
static inline long __syscall6(long n, long a, long b, long c, long d, long e, long f)
{register long a7 __asm__("a7") = n;register long a0 __asm__("a0") = a;register long a1 __asm__("a1") = b;register long a2 __asm__("a2") = c;register long a3 __asm__("a3") = d;register long a4 __asm__("a4") = e;register long a5 __asm__("a5") = f;__asm_syscall("r"(a7), "0"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5))
}
#define __asm_syscall(...) do { \__asm__ __volatile__ ( "svc 0" \: "=r"(x0) : __VA_ARGS__ : "memory", "cc"); \return x0; \} while (0)static inline long __syscall0(long n)
{register long x8 __asm__("x8") = n;register long x0 __asm__("x0");__asm_syscall("r"(x8));
}
...
static inline long __syscall6(long n, long a, long b, long c, long d, long e, long f)
{register long x8 __asm__("x8") = n;register long x0 __asm__("x0") = a;register long x1 __asm__("x1") = b;register long x2 __asm__("x2") = c;register long x3 __asm__("x3") = d;register long x4 __asm__("x4") = e;register long x5 __asm__("x5") = f;__asm_syscall("r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5));
}