You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
riscv-pke/user/user_lib.c

52 lines
1.3 KiB

/*
* The supporting library for applications.
* Actually, supporting routines for applications are catalogued as the user
* library. we don't do that in PKE to make the relationship between application
* and user library more straightforward.
*/
#include "user_lib.h"
#include "util/types.h"
#include "util/snprintf.h"
#include "kernel/syscall.h"
int do_user_call(uint64 sysnum, uint64 a1, uint64 a2, uint64 a3, uint64 a4, uint64 a5, uint64 a6,
uint64 a7) {
int ret;
// before invoking the syscall, arguments of do_user_call are already loaded into the argument
// registers (a0-a7) of our (emulated) risc-v machine.
asm volatile(
"ecall\n"
"sw a0, %0" // returns a 32-bit value
: "=m"(ret)
:
: "memory");
return ret;
}
//
// printu() supports user/lab1_1_helloworld.c
//
int printu(const char* s, ...) {
va_list vl;
va_start(vl, s);
char out[256]; // fixed buffer size.
int res = vsnprintf(out, sizeof(out), s, vl);
va_end(vl);
const char* buf = out;
size_t n = res < sizeof(out) ? res : sizeof(out);
// make a syscall to implement the required functionality.
return do_user_call(SYS_user_print, (uint64)buf, n, 0, 0, 0, 0, 0);
}
//
// applications need to call exit to quit execution.
//
int exit(int code) {
return do_user_call(SYS_user_exit, code, 0, 0, 0, 0, 0, 0);
}