From: Andrew Jones <andrew.jones@linux.dev>
To: kvm-riscv@lists.infradead.org
Subject: [kvm-unit-tests PATCH 08/24] riscv: Add riscv32 support
Date: Wed, 24 Jan 2024 08:18:24 +0100 [thread overview]
Message-ID: <20240124071815.6898-34-andrew.jones@linux.dev> (raw)
In-Reply-To: <20240124071815.6898-26-andrew.jones@linux.dev>
Make a few tweaks to allow also building and running riscv32.
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
lib/elf.h | 11 +++++++++++
lib/ldiv32.c | 16 ++++++++++++++++
lib/riscv/asm-offsets.c | 11 +++++++++++
riscv/Makefile | 3 +++
riscv/cstart.S | 35 +++++++++++++++++++++--------------
5 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/lib/elf.h b/lib/elf.h
index abd5cf4beaad..7a7db57774cd 100644
--- a/lib/elf.h
+++ b/lib/elf.h
@@ -8,6 +8,11 @@
#include <libcflat.h>
+/* 32-bit ELF base types. */
+typedef u32 Elf32_Addr;
+typedef u32 Elf32_Xword;
+typedef s32 Elf32_Sxword;
+
/* 64-bit ELF base types. */
typedef u64 Elf64_Addr;
typedef u64 Elf64_Xword;
@@ -26,6 +31,12 @@ typedef struct elf64_rel {
Elf64_Xword r_info; /* index and type of relocation */
} Elf64_Rel;
+typedef struct elf32_rela {
+ Elf32_Addr r_offset; /* Location at which to apply the action */
+ Elf32_Xword r_info; /* index and type of relocation */
+ Elf32_Sxword r_addend; /* Constant addend used to compute value */
+} Elf32_Rela;
+
typedef struct elf64_rela {
Elf64_Addr r_offset; /* Location at which to apply the action */
Elf64_Xword r_info; /* index and type of relocation */
diff --git a/lib/ldiv32.c b/lib/ldiv32.c
index 897a4b9cd39e..9ce2a6a1faf0 100644
--- a/lib/ldiv32.c
+++ b/lib/ldiv32.c
@@ -1,5 +1,21 @@
#include <stdint.h>
+#if __riscv_xlen == 32
+int __clzdi2(unsigned long);
+
+int __clzdi2(unsigned long a)
+{
+ int n = 0;
+
+ while (a) {
+ ++n;
+ a >>= 1;
+ }
+
+ return 32 - n;
+}
+#endif
+
extern uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem);
extern int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem);
extern int64_t __moddi3(int64_t num, int64_t den);
diff --git a/lib/riscv/asm-offsets.c b/lib/riscv/asm-offsets.c
index 4a74df9e4a09..eb337b7547b8 100644
--- a/lib/riscv/asm-offsets.c
+++ b/lib/riscv/asm-offsets.c
@@ -1,6 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-only
+#include <kbuild.h>
+#include <elf.h>
int main(void)
{
+#if __riscv_xlen == 32
+ OFFSET(ELF_RELA_OFFSET, elf32_rela, r_offset);
+ OFFSET(ELF_RELA_ADDEND, elf32_rela, r_addend);
+ DEFINE(ELF_RELA_SIZE, sizeof(struct elf32_rela));
+#elif __riscv_xlen == 64
+ OFFSET(ELF_RELA_OFFSET, elf64_rela, r_offset);
+ OFFSET(ELF_RELA_ADDEND, elf64_rela, r_addend);
+ DEFINE(ELF_RELA_SIZE, sizeof(struct elf64_rela));
+#endif
return 0;
}
diff --git a/riscv/Makefile b/riscv/Makefile
index 4e7fcc538ba1..fb97e678a456 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -29,6 +29,9 @@ cflatobjs += lib/riscv/io.o
cflatobjs += lib/riscv/sbi.o
cflatobjs += lib/riscv/setup.o
cflatobjs += lib/riscv/smp.o
+ifeq ($(ARCH),riscv32)
+cflatobjs += lib/ldiv32.o
+endif
########################################
diff --git a/riscv/cstart.S b/riscv/cstart.S
index a28d75e8021e..6ec2231e5812 100644
--- a/riscv/cstart.S
+++ b/riscv/cstart.S
@@ -4,11 +4,23 @@
*
* Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <ajones@ventanamicro.com>
*/
+#include <asm/asm-offsets.h>
#include <asm/csr.h>
+#if __riscv_xlen == 64
+#define __REG_SEL(a, b) a
+#elif __riscv_xlen == 32
+#define __REG_SEL(a, b) b
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+
+#define REG_L __REG_SEL(ld, lw)
+#define REG_S __REG_SEL(sd, sw)
+
.macro zero_range, tmp1, tmp2
9998: beq \tmp1, \tmp2, 9997f
- sd zero, 0(\tmp1)
+ REG_S zero, 0(\tmp1)
addi \tmp1, \tmp1, 8
j 9998b
9997:
@@ -33,26 +45,20 @@ start:
/*
* Update all R_RISCV_RELATIVE relocations using the table
- * of Elf64_Rela entries between reloc_start/end. The build
- * will not emit other relocation types.
- *
- * struct Elf64_Rela {
- * uint64_t r_offset;
- * uint64_t r_info;
- * int64_t r_addend;
- * }
+ * of Elf32_Rela/Elf64_Rela entries between reloc_start/end.
+ * The build will not emit other relocation types.
*/
la a1, reloc_start
la a2, reloc_end
la a3, start // base
1:
bge a1, a2, 1f
- ld a4, 0(a1) // r_offset
- ld a5, 16(a1) // r_addend
+ REG_L a4, ELF_RELA_OFFSET(a1) // r_offset
+ REG_L a5, ELF_RELA_ADDEND(a1) // r_addend
add a4, a3, a4 // addr = base + r_offset
add a5, a3, a5 // val = base + r_addend
- sd a5, 0(a4) // *addr = val
- addi a1, a1, 24
+ REG_S a5, 0(a4) // *addr = val
+ addi a1, a1, ELF_RELA_SIZE
j 1b
1:
@@ -72,11 +78,12 @@ start:
/* complete setup */
la a1, stacktop // a1 is the base of free memory
+ mv a2, zero // clear a2 for xlen=32
call setup // a0 is the addr of the dtb
/* run the test */
la a0, __argc
- ld a0, 0(a0)
+ REG_L a0, 0(a0)
la a1, __argv
la a2, __environ
call main
--
2.43.0
WARNING: multiple messages have this Message-ID (diff)
From: Andrew Jones <andrew.jones@linux.dev>
To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
kvmarm@lists.linux.dev
Cc: ajones@ventanamicro.com, anup@brainfault.org,
atishp@atishpatra.org, pbonzini@redhat.com, thuth@redhat.com,
alexandru.elisei@arm.com, eric.auger@redhat.com
Subject: [kvm-unit-tests PATCH 08/24] riscv: Add riscv32 support
Date: Wed, 24 Jan 2024 08:18:24 +0100 [thread overview]
Message-ID: <20240124071815.6898-34-andrew.jones@linux.dev> (raw)
In-Reply-To: <20240124071815.6898-26-andrew.jones@linux.dev>
Make a few tweaks to allow also building and running riscv32.
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
lib/elf.h | 11 +++++++++++
lib/ldiv32.c | 16 ++++++++++++++++
lib/riscv/asm-offsets.c | 11 +++++++++++
riscv/Makefile | 3 +++
riscv/cstart.S | 35 +++++++++++++++++++++--------------
5 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/lib/elf.h b/lib/elf.h
index abd5cf4beaad..7a7db57774cd 100644
--- a/lib/elf.h
+++ b/lib/elf.h
@@ -8,6 +8,11 @@
#include <libcflat.h>
+/* 32-bit ELF base types. */
+typedef u32 Elf32_Addr;
+typedef u32 Elf32_Xword;
+typedef s32 Elf32_Sxword;
+
/* 64-bit ELF base types. */
typedef u64 Elf64_Addr;
typedef u64 Elf64_Xword;
@@ -26,6 +31,12 @@ typedef struct elf64_rel {
Elf64_Xword r_info; /* index and type of relocation */
} Elf64_Rel;
+typedef struct elf32_rela {
+ Elf32_Addr r_offset; /* Location at which to apply the action */
+ Elf32_Xword r_info; /* index and type of relocation */
+ Elf32_Sxword r_addend; /* Constant addend used to compute value */
+} Elf32_Rela;
+
typedef struct elf64_rela {
Elf64_Addr r_offset; /* Location at which to apply the action */
Elf64_Xword r_info; /* index and type of relocation */
diff --git a/lib/ldiv32.c b/lib/ldiv32.c
index 897a4b9cd39e..9ce2a6a1faf0 100644
--- a/lib/ldiv32.c
+++ b/lib/ldiv32.c
@@ -1,5 +1,21 @@
#include <stdint.h>
+#if __riscv_xlen == 32
+int __clzdi2(unsigned long);
+
+int __clzdi2(unsigned long a)
+{
+ int n = 0;
+
+ while (a) {
+ ++n;
+ a >>= 1;
+ }
+
+ return 32 - n;
+}
+#endif
+
extern uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem);
extern int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem);
extern int64_t __moddi3(int64_t num, int64_t den);
diff --git a/lib/riscv/asm-offsets.c b/lib/riscv/asm-offsets.c
index 4a74df9e4a09..eb337b7547b8 100644
--- a/lib/riscv/asm-offsets.c
+++ b/lib/riscv/asm-offsets.c
@@ -1,6 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-only
+#include <kbuild.h>
+#include <elf.h>
int main(void)
{
+#if __riscv_xlen == 32
+ OFFSET(ELF_RELA_OFFSET, elf32_rela, r_offset);
+ OFFSET(ELF_RELA_ADDEND, elf32_rela, r_addend);
+ DEFINE(ELF_RELA_SIZE, sizeof(struct elf32_rela));
+#elif __riscv_xlen == 64
+ OFFSET(ELF_RELA_OFFSET, elf64_rela, r_offset);
+ OFFSET(ELF_RELA_ADDEND, elf64_rela, r_addend);
+ DEFINE(ELF_RELA_SIZE, sizeof(struct elf64_rela));
+#endif
return 0;
}
diff --git a/riscv/Makefile b/riscv/Makefile
index 4e7fcc538ba1..fb97e678a456 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -29,6 +29,9 @@ cflatobjs += lib/riscv/io.o
cflatobjs += lib/riscv/sbi.o
cflatobjs += lib/riscv/setup.o
cflatobjs += lib/riscv/smp.o
+ifeq ($(ARCH),riscv32)
+cflatobjs += lib/ldiv32.o
+endif
########################################
diff --git a/riscv/cstart.S b/riscv/cstart.S
index a28d75e8021e..6ec2231e5812 100644
--- a/riscv/cstart.S
+++ b/riscv/cstart.S
@@ -4,11 +4,23 @@
*
* Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <ajones@ventanamicro.com>
*/
+#include <asm/asm-offsets.h>
#include <asm/csr.h>
+#if __riscv_xlen == 64
+#define __REG_SEL(a, b) a
+#elif __riscv_xlen == 32
+#define __REG_SEL(a, b) b
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+
+#define REG_L __REG_SEL(ld, lw)
+#define REG_S __REG_SEL(sd, sw)
+
.macro zero_range, tmp1, tmp2
9998: beq \tmp1, \tmp2, 9997f
- sd zero, 0(\tmp1)
+ REG_S zero, 0(\tmp1)
addi \tmp1, \tmp1, 8
j 9998b
9997:
@@ -33,26 +45,20 @@ start:
/*
* Update all R_RISCV_RELATIVE relocations using the table
- * of Elf64_Rela entries between reloc_start/end. The build
- * will not emit other relocation types.
- *
- * struct Elf64_Rela {
- * uint64_t r_offset;
- * uint64_t r_info;
- * int64_t r_addend;
- * }
+ * of Elf32_Rela/Elf64_Rela entries between reloc_start/end.
+ * The build will not emit other relocation types.
*/
la a1, reloc_start
la a2, reloc_end
la a3, start // base
1:
bge a1, a2, 1f
- ld a4, 0(a1) // r_offset
- ld a5, 16(a1) // r_addend
+ REG_L a4, ELF_RELA_OFFSET(a1) // r_offset
+ REG_L a5, ELF_RELA_ADDEND(a1) // r_addend
add a4, a3, a4 // addr = base + r_offset
add a5, a3, a5 // val = base + r_addend
- sd a5, 0(a4) // *addr = val
- addi a1, a1, 24
+ REG_S a5, 0(a4) // *addr = val
+ addi a1, a1, ELF_RELA_SIZE
j 1b
1:
@@ -72,11 +78,12 @@ start:
/* complete setup */
la a1, stacktop // a1 is the base of free memory
+ mv a2, zero // clear a2 for xlen=32
call setup // a0 is the addr of the dtb
/* run the test */
la a0, __argc
- ld a0, 0(a0)
+ REG_L a0, 0(a0)
la a1, __argv
la a2, __environ
call main
--
2.43.0
next prev parent reply other threads:[~2024-01-24 7:18 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-24 7:18 [kvm-unit-tests PATCH 00/24] Introduce RISC-V Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 01/24] configure: Add ARCH_LIBDIR Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:33 ` Thomas Huth
2024-01-24 9:33 ` Thomas Huth
2024-01-24 7:18 ` [kvm-unit-tests PATCH 02/24] riscv: Initial port, hello world Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 03/24] arm/arm64: Move cpumask.h to common lib Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:52 ` Thomas Huth
2024-01-24 9:52 ` Thomas Huth
2024-01-24 7:18 ` [kvm-unit-tests PATCH 04/24] arm/arm64: Share cpu online, present and idle masks Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:53 ` Thomas Huth
2024-01-24 9:53 ` Thomas Huth
2024-01-24 7:18 ` [kvm-unit-tests PATCH 05/24] riscv: Add DT parsing Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 06/24] riscv: Add initial SBI support Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 07/24] riscv: Add run script and unittests.cfg Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` Andrew Jones [this message]
2024-01-24 7:18 ` [kvm-unit-tests PATCH 08/24] riscv: Add riscv32 support Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 09/24] riscv: Add exception handling Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 10/24] riscv: Add backtrace support Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 11/24] arm/arm64: Generalize wfe/sev names in smp.c Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 12/24] arm/arm64: Remove spinlocks from on_cpu_async Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 13/24] arm/arm64: Share on_cpus Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 14/24] riscv: Compile with march Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 15/24] riscv: Add SMP support Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 16/24] arm/arm64: Share memregions Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 17/24] riscv: Populate memregions and switch to page allocator Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 18/24] riscv: Add MMU support Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 19/24] riscv: Enable the MMU in secondaries Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 20/24] riscv: Enable vmalloc Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 21/24] lib: Add strcasecmp and strncasecmp Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:41 ` Thomas Huth
2024-01-24 9:41 ` Thomas Huth
2024-01-24 7:18 ` [kvm-unit-tests PATCH 22/24] riscv: Add isa string parsing Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 23/24] gitlab-ci: Add riscv64 tests Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:45 ` Thomas Huth
2024-01-24 9:45 ` Thomas Huth
2024-01-24 10:21 ` Andrew Jones
2024-01-24 10:21 ` Andrew Jones
2024-01-24 7:18 ` [kvm-unit-tests PATCH 24/24] MAINTAINERS: Add riscv Andrew Jones
2024-01-24 7:18 ` Andrew Jones
2024-01-24 9:46 ` Thomas Huth
2024-01-24 9:46 ` Thomas Huth
2024-01-24 9:58 ` [kvm-unit-tests PATCH 00/24] Introduce RISC-V Thomas Huth
2024-01-24 9:58 ` Thomas Huth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240124071815.6898-34-andrew.jones@linux.dev \
--to=andrew.jones@linux.dev \
--cc=kvm-riscv@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.