From: Andrew Jones <andrew.jones@linux.dev>
To: kvm-riscv@lists.infradead.org
Subject: [kvm-unit-tests PATCH 17/24] riscv: Populate memregions and switch to page allocator
Date: Wed, 24 Jan 2024 08:18:33 +0100 [thread overview]
Message-ID: <20240124071815.6898-43-andrew.jones@linux.dev> (raw)
In-Reply-To: <20240124071815.6898-26-andrew.jones@linux.dev>
Get the memory regions from the DT rather than just assuming we have
enough. Getting memory regions and setting their flags is also a
first step to enabling the MMU. Also switch to the page allocator,
which is a second step to enabling the MMU.
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
lib/riscv/asm/memory_areas.h | 1 +
lib/riscv/setup.c | 55 ++++++++++++++++++++++++++++++++++--
riscv/Makefile | 2 ++
3 files changed, 55 insertions(+), 3 deletions(-)
create mode 100644 lib/riscv/asm/memory_areas.h
diff --git a/lib/riscv/asm/memory_areas.h b/lib/riscv/asm/memory_areas.h
new file mode 100644
index 000000000000..2b34e63502dd
--- /dev/null
+++ b/lib/riscv/asm/memory_areas.h
@@ -0,0 +1 @@
+#include <asm-generic/memory_areas.h>
diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c
index 9ff446b5e171..848ec8e83496 100644
--- a/lib/riscv/setup.c
+++ b/lib/riscv/setup.c
@@ -6,22 +6,31 @@
*/
#include <libcflat.h>
#include <alloc.h>
+#include <alloc_page.h>
#include <alloc_phys.h>
#include <argv.h>
#include <cpumask.h>
#include <devicetree.h>
+#include <memregions.h>
#include <on-cpus.h>
#include <asm/csr.h>
#include <asm/page.h>
#include <asm/processor.h>
#include <asm/setup.h>
+#define VA_BASE ((phys_addr_t)3 * SZ_1G)
+
+#define MAX_DT_MEM_REGIONS 16
+#define NR_MEM_REGIONS (MAX_DT_MEM_REGIONS + 16)
+
char *initrd;
u32 initrd_size;
struct thread_info cpus[NR_CPUS];
int nr_cpus;
+static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1];
+
int hartid_to_cpu(unsigned long hartid)
{
int cpu;
@@ -64,10 +73,50 @@ static void cpu_init(void)
cpu0_calls_idle = true;
}
+extern unsigned long _etext;
+
static void mem_init(phys_addr_t freemem_start)
{
- //TODO - for now just assume we've got some memory available
- phys_alloc_init(freemem_start, 16 * SZ_1M);
+ struct mem_region *freemem, *code, *data;
+ phys_addr_t freemem_end, base, top;
+
+ memregions_init(riscv_mem_regions, NR_MEM_REGIONS);
+ memregions_add_dt_regions(MAX_DT_MEM_REGIONS);
+
+ /* Split the region with the code into two regions; code and data */
+ memregions_split((unsigned long)&_etext, &code, &data);
+ assert(code);
+ code->flags |= MR_F_CODE;
+
+ freemem = memregions_find(freemem_start);
+ assert(freemem && !(freemem->flags & (MR_F_IO | MR_F_CODE)));
+
+ freemem_end = freemem->end & PAGE_MASK;
+
+ /*
+ * The assert below is mostly checking that the free memory doesn't
+ * start in the 3G-4G range, which is reserved for virtual addresses,
+ * but it also confirms that there is some free memory (the amount
+ * is arbitrarily selected, but should be sufficient for a unit test)
+ *
+ * TODO: Allow the VA range to shrink and move.
+ */
+ if (freemem_end > VA_BASE)
+ freemem_end = VA_BASE;
+ assert(freemem_end - freemem_start >= SZ_1M * 16);
+
+ /*
+ * TODO: Remove the need for this phys allocator dance, since, as we
+ * can see with the assert, we could have gone straight to the page
+ * allocator.
+ */
+ phys_alloc_init(freemem_start, freemem_end - freemem_start);
+ phys_alloc_set_minimum_alignment(PAGE_SIZE);
+ phys_alloc_get_unused(&base, &top);
+ assert(base == freemem_start && top == freemem_end);
+
+ page_alloc_init_area(0, freemem_start >> PAGE_SHIFT, freemem_end >> PAGE_SHIFT);
+ page_alloc_ops_enable();
}
static void banner(void)
@@ -86,7 +135,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
u32 fdt_size;
int ret;
- assert(sizeof(long) == 8 || freemem_start < (3ul << 30));
+ assert(sizeof(long) == 8 || freemem_start < VA_BASE);
freemem = (void *)(unsigned long)freemem_start;
/* Move the FDT to the base of free memory */
diff --git a/riscv/Makefile b/riscv/Makefile
index 932f3378264c..ed1a14025ed2 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -22,8 +22,10 @@ $(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1
cstart.o = $(TEST_DIR)/cstart.o
cflatobjs += lib/alloc.o
+cflatobjs += lib/alloc_page.o
cflatobjs += lib/alloc_phys.o
cflatobjs += lib/devicetree.o
+cflatobjs += lib/memregions.o
cflatobjs += lib/on-cpus.o
cflatobjs += lib/riscv/bitops.o
cflatobjs += lib/riscv/io.o
--
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 17/24] riscv: Populate memregions and switch to page allocator
Date: Wed, 24 Jan 2024 08:18:33 +0100 [thread overview]
Message-ID: <20240124071815.6898-43-andrew.jones@linux.dev> (raw)
In-Reply-To: <20240124071815.6898-26-andrew.jones@linux.dev>
Get the memory regions from the DT rather than just assuming we have
enough. Getting memory regions and setting their flags is also a
first step to enabling the MMU. Also switch to the page allocator,
which is a second step to enabling the MMU.
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
lib/riscv/asm/memory_areas.h | 1 +
lib/riscv/setup.c | 55 ++++++++++++++++++++++++++++++++++--
riscv/Makefile | 2 ++
3 files changed, 55 insertions(+), 3 deletions(-)
create mode 100644 lib/riscv/asm/memory_areas.h
diff --git a/lib/riscv/asm/memory_areas.h b/lib/riscv/asm/memory_areas.h
new file mode 100644
index 000000000000..2b34e63502dd
--- /dev/null
+++ b/lib/riscv/asm/memory_areas.h
@@ -0,0 +1 @@
+#include <asm-generic/memory_areas.h>
diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c
index 9ff446b5e171..848ec8e83496 100644
--- a/lib/riscv/setup.c
+++ b/lib/riscv/setup.c
@@ -6,22 +6,31 @@
*/
#include <libcflat.h>
#include <alloc.h>
+#include <alloc_page.h>
#include <alloc_phys.h>
#include <argv.h>
#include <cpumask.h>
#include <devicetree.h>
+#include <memregions.h>
#include <on-cpus.h>
#include <asm/csr.h>
#include <asm/page.h>
#include <asm/processor.h>
#include <asm/setup.h>
+#define VA_BASE ((phys_addr_t)3 * SZ_1G)
+
+#define MAX_DT_MEM_REGIONS 16
+#define NR_MEM_REGIONS (MAX_DT_MEM_REGIONS + 16)
+
char *initrd;
u32 initrd_size;
struct thread_info cpus[NR_CPUS];
int nr_cpus;
+static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1];
+
int hartid_to_cpu(unsigned long hartid)
{
int cpu;
@@ -64,10 +73,50 @@ static void cpu_init(void)
cpu0_calls_idle = true;
}
+extern unsigned long _etext;
+
static void mem_init(phys_addr_t freemem_start)
{
- //TODO - for now just assume we've got some memory available
- phys_alloc_init(freemem_start, 16 * SZ_1M);
+ struct mem_region *freemem, *code, *data;
+ phys_addr_t freemem_end, base, top;
+
+ memregions_init(riscv_mem_regions, NR_MEM_REGIONS);
+ memregions_add_dt_regions(MAX_DT_MEM_REGIONS);
+
+ /* Split the region with the code into two regions; code and data */
+ memregions_split((unsigned long)&_etext, &code, &data);
+ assert(code);
+ code->flags |= MR_F_CODE;
+
+ freemem = memregions_find(freemem_start);
+ assert(freemem && !(freemem->flags & (MR_F_IO | MR_F_CODE)));
+
+ freemem_end = freemem->end & PAGE_MASK;
+
+ /*
+ * The assert below is mostly checking that the free memory doesn't
+ * start in the 3G-4G range, which is reserved for virtual addresses,
+ * but it also confirms that there is some free memory (the amount
+ * is arbitrarily selected, but should be sufficient for a unit test)
+ *
+ * TODO: Allow the VA range to shrink and move.
+ */
+ if (freemem_end > VA_BASE)
+ freemem_end = VA_BASE;
+ assert(freemem_end - freemem_start >= SZ_1M * 16);
+
+ /*
+ * TODO: Remove the need for this phys allocator dance, since, as we
+ * can see with the assert, we could have gone straight to the page
+ * allocator.
+ */
+ phys_alloc_init(freemem_start, freemem_end - freemem_start);
+ phys_alloc_set_minimum_alignment(PAGE_SIZE);
+ phys_alloc_get_unused(&base, &top);
+ assert(base == freemem_start && top == freemem_end);
+
+ page_alloc_init_area(0, freemem_start >> PAGE_SHIFT, freemem_end >> PAGE_SHIFT);
+ page_alloc_ops_enable();
}
static void banner(void)
@@ -86,7 +135,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
u32 fdt_size;
int ret;
- assert(sizeof(long) == 8 || freemem_start < (3ul << 30));
+ assert(sizeof(long) == 8 || freemem_start < VA_BASE);
freemem = (void *)(unsigned long)freemem_start;
/* Move the FDT to the base of free memory */
diff --git a/riscv/Makefile b/riscv/Makefile
index 932f3378264c..ed1a14025ed2 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -22,8 +22,10 @@ $(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1
cstart.o = $(TEST_DIR)/cstart.o
cflatobjs += lib/alloc.o
+cflatobjs += lib/alloc_page.o
cflatobjs += lib/alloc_phys.o
cflatobjs += lib/devicetree.o
+cflatobjs += lib/memregions.o
cflatobjs += lib/on-cpus.o
cflatobjs += lib/riscv/bitops.o
cflatobjs += lib/riscv/io.o
--
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 ` [kvm-unit-tests PATCH 08/24] riscv: Add riscv32 support Andrew Jones
2024-01-24 7:18 ` 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 ` Andrew Jones [this message]
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 ` [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-43-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.