aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-04-22 21:29:06 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-04-22 21:29:06 +0200
commit1de9228fda28826c337ac09201586de1f2f248b9 (patch)
tree3db67eb9ec171ba11cb66b2a70db973b8c1c76ff
parent74caba366d3fafd21910f3d95dbc2819267c52bc (diff)
downloadfarga-1de9228fda28826c337ac09201586de1f2f248b9.tar.gz
farga-1de9228fda28826c337ac09201586de1f2f248b9.zip
riscv: user: Force LINUX_HEADERS on hwprobe
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--README.md9
-rw-r--r--arch/riscv/user/basics/README.md2
-rw-r--r--arch/riscv/user/hwprobe/Makefile2
-rw-r--r--arch/riscv/user/hwprobe/README.md36
-rw-r--r--arch/riscv/user/hwprobe/probe.c12
5 files changed, 49 insertions, 12 deletions
diff --git a/README.md b/README.md
index 00e8851..1804b5e 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,13 @@
A bunch of code that messes with low level stuff. That is:
-- `arch`:
+- `arch/riscv/`:
- `machine`: prints "hello, world" on the serial port while running in M mode
on QEMU. Check the [README file](./arch/riscv/machine/README.md) for more
info.
- `supervisor/hsm`: using the HSM extension from SBI to gather info from
harts.
- - `user/`:
- - `basics`: bunch of code checking on basic programming concepts from the
- RISC-V ISA.
- - `hwprobe`: using the `hwprobe` system call from the Linux kernel to check
- on the features from the current machine.
+ - `user/`: user space programs that either delve into RISC-V assembly or that
+ interface the Linux Kernel on the RISC-V architecture.
- `kernel/dtm`: simple kernel module showcasing the API from the `of` driver.
A lot of this is not taken solely from my brain, and I will give credit whenever
diff --git a/arch/riscv/user/basics/README.md b/arch/riscv/user/basics/README.md
new file mode 100644
index 0000000..e938133
--- /dev/null
+++ b/arch/riscv/user/basics/README.md
@@ -0,0 +1,2 @@
+Just random assembly messing with basic programming concepts from the RISC-V
+ISA.
diff --git a/arch/riscv/user/hwprobe/Makefile b/arch/riscv/user/hwprobe/Makefile
index 6c47002..67053a8 100644
--- a/arch/riscv/user/hwprobe/Makefile
+++ b/arch/riscv/user/hwprobe/Makefile
@@ -24,7 +24,7 @@ ISA ?= rv64imafdc_zicntr_zicsr_zifencei_zihpm_zca_zcd_zba_zbb
WARNINGS = -Werror -Wpedantic -Wall -Wextra -Wcast-align -Wcast-qual -Winit-self \
-Wmissing-include-dirs -Wredundant-decls -Wshadow -Wsign-conversion \
-Wswitch-default -Wundef -Wunreachable-code -Wmissing-noreturn
-CCFLAGS = $(WARNINGS) -march=$(ISA) -static
+CCFLAGS = $(WARNINGS) -march=$(ISA) -static -I$(LINUX_HEADERS)
LDFLAGS =
diff --git a/arch/riscv/user/hwprobe/README.md b/arch/riscv/user/hwprobe/README.md
new file mode 100644
index 0000000..58f09c1
--- /dev/null
+++ b/arch/riscv/user/hwprobe/README.md
@@ -0,0 +1,36 @@
+The RISC-V support for the Linux kernel has a new system call which is specific
+to it, called `hwprobe`. With this system call you can check which features,
+extensions, etc. are available on the current machine.
+
+In order to build this example you need Linux headers for the RISC-V
+architecture. You can achieve this in two ways:
+
+1. Simply install the Linux headers from your distribution if you are already
+ using a RISC-V machine.
+2. Build the Linux kernel and use the `LINUX_HEADERS` environment variable to
+ point to the headers directory (i.e. the directory produced by `make
+ headers_install`)
+
+After that, build it and run it. On a QEMU machine I got the following:
+
+```
+Supports base extensions I, M, A? yes
+
+We got back this bit map: 0000001110001101001010000001000100100000000000000000000011111011; which means:
+
+F, D: OK
+C: OK
+ZBA: OK
+ZBB: OK
+ZBS: OK
+ZICBOZ: OK
+ZBC: OK
+ZIHINTNTL: OK
+ZFA: OK
+ZIHINTPAUSE: OK
+ZCA: OK
+ZCD: OK
+ZAWRS: OK
+ZICNTR: OK
+ZIHPM: OK
+```
diff --git a/arch/riscv/user/hwprobe/probe.c b/arch/riscv/user/hwprobe/probe.c
index de2c209..70206f2 100644
--- a/arch/riscv/user/hwprobe/probe.c
+++ b/arch/riscv/user/hwprobe/probe.c
@@ -1,19 +1,21 @@
#define _GNU_SOURCE
-#include <sched.h>
-#include <asm/hwprobe.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
-#define NR_RISCV_HWPROBE 258
+#include <sched.h>
+#include <asm/hwprobe.h>
+#include <asm/unistd.h>
+#ifndef RISCV_HWPROBE_EXT_ZICNTR
// Merged in Linux v6.15.
// See 4458b8f68dc7 ("riscv: hwprobe: export Zicntr and Zihpm extensions").
#define RISCV_HWPROBE_EXT_ZICNTR (1ULL << 50)
#define RISCV_HWPROBE_EXT_ZIHPM (1ULL << 51)
+#endif
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr)[0])
@@ -253,7 +255,7 @@ int main()
pairs = (struct riscv_hwprobe){
.key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR,
};
- rc = syscall(NR_RISCV_HWPROBE, &pairs, 1, 0, NULL, 0);
+ rc = syscall(__NR_riscv_hwprobe, &pairs, 1, 0, NULL, 0);
if (rc) {
perror("could not fetch the base behavior for the system");
exit(EXIT_FAILURE);
@@ -265,7 +267,7 @@ int main()
pairs = (struct riscv_hwprobe){
.key = RISCV_HWPROBE_KEY_IMA_EXT_0,
};
- rc = syscall(NR_RISCV_HWPROBE, &pairs, 1, 0, NULL, 0);
+ rc = syscall(__NR_riscv_hwprobe, &pairs, 1, 0, NULL, 0);
if (rc) {
perror("could not fetch extensions as given by hwprobe");
exit(EXIT_FAILURE);