-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for riscv64? #247
Comments
Since riscv cpu identification instructions can only be performed in M-mode. Machine level is the highest level and as far as I understand we can't even run it in userland so the best approach is to parse proc/cpuinfo see Linux implementation https://github.com/google/cpu_features/blob/main/src/impl_x86_linux_or_android.c and the same approach we can use for FreeBSD https://github.com/google/cpu_features/blob/main/src/impl_x86_freebsd.c. if I'm not mistaken macOS and Windows don't support riscv |
@yuzibo Which board have you used? I would need this information to add a test |
Hi, @DaniAffCH Linux unmatched 5.18.0-2-riscv64 #1 SMP Debian 5.18.5-1 (2022-06-16) riscv64 GNU/Linux So how can I help you to do test? |
More info about Unmatched board is here: |
Thanks a lot! @yuzibo |
Hi @yuzibo, I'm wondering if we can run M-mode code in userspace as is the case with aarch64 for Linux. Could you run the code under Linux riscv to see if you are getting an illegal instruction or some result, please? #include <stdio.h>
static unsigned long cpuid()
{
unsigned long res;
asm ("csrr %0, mcpuid" : "=r"(res));
return res;
}
int main() {
unsigned long mcpuid = cpuid();
printf("mcpuid: %lu", mcpuid);
return 0;
} |
Oops: vimer@unmatched:~/build/cpu$ gcc -g get_cpuid.c -o test
get_cpuid.c: Assembler messages:
get_cpuid.c:12: Error: unknown CSR `mcpuid' |
@yuzibo, probably mcpuid is deprecated, could you replace |
It can be compiled but fail on runtime: vimer@unmatched:~/build/cpu$ gcc -g get_cpuid.c -o test
vimer@unmatched:~/build/cpu$ ./test
Illegal instruction
vimer@unmatched:~/build/cpu$ cat get_cpuid.c
#include <stdio.h>
static unsigned long cpuid()
{
unsigned long res;
__asm__ volatile ("csrr %0, misa" : "=r"(res));
return res;
}
int main() {
unsigned long misa = cpuid();
printf("misa: %lu", misa);
return 0;
} |
I think the right way to do this is that the kernel reads the FDT from the boot rom, and uses that to work out which extensions the CPU supports. Then userspace parses /proc/cpuinfo or uses hwcap.h. There are extensions that aren't in CSR's, and have to be determined from the FDT. |
@yuzibo thank you! This is the expected result, but it was necessary to check |
@michael-roe, what advantages of using fdt compared with |
User space programs (which includes cpu_features) ought to use proc/cpuinfo and/or hwcap. But the part of the Linux kernel that creates /proc/cpuinfo (and, probably, hwcap) looks at the FDT to work out which options the CPU supports. Some of the information can't be derived just from cpu status registers. So the basic flow is: a) kernel reads the FDT from the boot rom, works out what options are supported and saves this info somewhere |
See What I think is happening here is that it's getting the information from the FDT. |
Hi,
Do we have a plan to support riscv64?
I am willing to do it but maybe need your help.
I am stuck on how to define RiscvFeatures struct:
I have a real riscv64 hardware by hand:)
The text was updated successfully, but these errors were encountered: