Computer Architecture
In line 11
and line 29
, I call clock
function and try to caculate the time between my testcase. However, I got an error message when I use rv32emu to run the .elf file which is compiled by The xPack GNU RISC-V Embedded GCC
gettimeofday
After reading file unistd.h
, which define the system call number under RISC-V architecture
If compile C file by riscv-none-elf-gcc -march=rv32i -mabi=ilp32
, which will means __BITS_PER_LONG = 32
, and it will never define __NR_gettimeof day 169
, so if compile C file with gnu toolchain, the elf file cannot execute well on rv32emu and get the error message
Because in rv32emu, only implemented system call gettimeofday
with number 169
, I think it can be use if write the assembly code directly.
__ARCH_WANT_TIME32_SYSCALLS
, which can optional define in /(arch-type)/unistd.h
, but we use The xPack GNU RISC-V Embedded GCC, which riscv-none-elf-gcc
is already an executable code.So I try to implement a system call clock_gettime
with number 403
in rv32emu in order to run the .elf file which is compiled by riscv-none-elf-gcc
directly.
Try object dump the executable file to figure out what operation actually done by clock
function associate with system call.
Disassembly
line 7
, I notice that when I call clock
function, it actually use system call number 403
which is clock_gettime
, mentioned in Juraj's Blog-RISC-V Linux syscall tableline 16
to line 23
, there is some operation on a4
, which is original store tv_nsec
in timespec. After operation, a5
will store a4
* 1000a5
= \((((a_4 \times 32)-a_4)\times4)+a_4)\times8 = a_4 \times 1000\)So, the value should be write back in syscall clock_gettime
should be
tv_sec
which is original defined in timespec
tv_msec
which is time_nsec
/ 1000000, this will cause the resolution will reduce to millisecond.syscall.c
Now it can use run clock
function in rv32emu which is compiled by riscv-none-elf-gcc
Generate random string between a - z, the code with SWAR technique.
Test code takes 2 same input strings, and the algorithm will go through whole string.
Now can use clock
function in C code and compile with command riscv-none-elf-gcc -march=rv32i -mabi=ilp32
, and directly run on rv32emu.
Trick
There is a problem if I don't allocate char* str
and the associate code is below.
0x10000
which means 65536 bytes space. So I malloc(system call brk
I think) it.TODO: clarify newlib internals