summaryrefslogtreecommitdiff
path: root/bsp
diff options
context:
space:
mode:
authorTm-C-mT <[email protected]>2025-12-03 18:38:26 +0800
committerR b b666 <[email protected]>2025-12-04 15:42:09 +0800
commitacef64ed2a30bd1b2c832114dde7d9b1f5b9a509 (patch)
tree3c5e5bf7478eb069e39d7042503f9cb28bb60912 /bsp
parentdd19c0eb7205b00a8f5c9a179d95d345b5a0f6d4 (diff)
[libcpu-riscv]: [surpport SMP]: Add SMP support for qemu-virt64-riscv
1.Add the necessary function declarations for SMP enablement and implement the corresponding functionalities, including rt_hw_secondary_cpu_up, secondary_cpu_entry, rt_hw_local_irq_disable, rt_hw_local_irq_enable, rt_hw_secondary_cpu_idle_exec, rt_hw_spin_lock_init, rt_hw_spin_lock, rt_hw_spin_unlock, rt_hw_ipi_send, rt_hw_interrupt_set_priority, rt_hw_interrupt_get_priority, rt_hw_ipi_init, rt_hw_ipi_handler_install, and rt_hw_ipi_handler. 2.In the two functions (rt_hw_context_switch_to and rt_hw_context_switch) in context_gcc.S, add a call to rt_cpus_lock_status_restore to update the scheduler information. 3.If the MMU is enabled, use the .percpu section and record different hartids by configuring special page tables; if the MMU is not enabled, record them directly in the satp register. Additionally, add dynamic startup based on core configuration.The .percpu section is only used when both ARCH_MM_MMU and RT_USING_SMP are enabled. However, there is a certain amount of space waste since no macro guard is added for it in the link script currently. 4.The physical memory of QEMU started in CI is 128MB, so RT_HW_PAGE_END is modified from the original +256MB to +128MB. Modify the SConscript file under the common64 directory to include common/atomic_riscv.c in the compilation process. Signed-off-by: Mengchen Teng <[email protected]>
Diffstat (limited to 'bsp')
-rw-r--r--bsp/qemu-virt64-riscv/SConstruct20
-rw-r--r--bsp/qemu-virt64-riscv/driver/board.c9
-rw-r--r--bsp/qemu-virt64-riscv/driver/board.h2
-rw-r--r--bsp/qemu-virt64-riscv/link.lds28
-rw-r--r--bsp/qemu-virt64-riscv/link_cpus.lds1
-rw-r--r--bsp/qemu-virt64-riscv/link_smart.lds28
-rwxr-xr-xbsp/qemu-virt64-riscv/qemu-dbg.sh16
-rwxr-xr-xbsp/qemu-virt64-riscv/run.sh17
8 files changed, 104 insertions, 17 deletions
diff --git a/bsp/qemu-virt64-riscv/SConstruct b/bsp/qemu-virt64-riscv/SConstruct
index 3387d0e149..ae0e3375e1 100644
--- a/bsp/qemu-virt64-riscv/SConstruct
+++ b/bsp/qemu-virt64-riscv/SConstruct
@@ -38,5 +38,25 @@ if GetDepend('__STACKSIZE__'): stack_size = GetDepend('__STACKSIZE__')
stack_lds.write('__STACKSIZE__ = %d;\n' % stack_size)
stack_lds.close()
+# Obtain the number of harts from rtconfig.h and write
+# it into link_cpus.lds for the linker script
+try:
+ with open('rtconfig.h', 'r') as f:
+ rtconfig_content = f.readlines()
+except FileNotFoundError:
+ cpus_nr = 1
+else:
+ cpus_nr = 1 # default value
+ for line in rtconfig_content:
+ line = line.strip()
+ if line.startswith('#define') and 'RT_CPUS_NR' in line:
+ parts = line.split()
+ if len(parts) >= 3 and parts[2].isdigit():
+ cpus_nr = int(parts[2])
+ break
+
+with open('link_cpus.lds', 'w') as cpus_lds:
+ cpus_lds.write(f'RT_CPUS_NR = {cpus_nr};\n')
+
# make a building
DoBuilding(TARGET, objs)
diff --git a/bsp/qemu-virt64-riscv/driver/board.c b/bsp/qemu-virt64-riscv/driver/board.c
index c5116aad0c..092244278e 100644
--- a/bsp/qemu-virt64-riscv/driver/board.c
+++ b/bsp/qemu-virt64-riscv/driver/board.c
@@ -24,6 +24,10 @@
#include "plic.h"
#include "stack.h"
+#ifdef RT_USING_SMP
+#include "interrupt.h"
+#endif /* RT_USING_SMP */
+
#ifdef RT_USING_SMART
#include "riscv_mmu.h"
#include "mmu.h"
@@ -89,6 +93,11 @@ void rt_hw_board_init(void)
rt_hw_tick_init();
+#ifdef RT_USING_SMP
+ /* ipi init */
+ rt_hw_ipi_init();
+#endif /* RT_USING_SMP */
+
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
diff --git a/bsp/qemu-virt64-riscv/driver/board.h b/bsp/qemu-virt64-riscv/driver/board.h
index 433b1d8f53..9c74ae6b26 100644
--- a/bsp/qemu-virt64-riscv/driver/board.h
+++ b/bsp/qemu-virt64-riscv/driver/board.h
@@ -25,7 +25,7 @@ extern unsigned int __bss_end;
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
#define RT_HW_HEAP_END ((void *)(RT_HW_HEAP_BEGIN + 64 * 1024 * 1024))
#define RT_HW_PAGE_START RT_HW_HEAP_END
-#define RT_HW_PAGE_END ((void *)(KERNEL_VADDR_START + (256 * 1024 * 1024 - VIRT64_SBI_MEMSZ)))
+#define RT_HW_PAGE_END ((void *)(KERNEL_VADDR_START + (128 * 1024 * 1024 - VIRT64_SBI_MEMSZ)))
void rt_hw_board_init(void);
void rt_init_user_mem(struct rt_thread *thread, const char *name,
diff --git a/bsp/qemu-virt64-riscv/link.lds b/bsp/qemu-virt64-riscv/link.lds
index a76fed4fa3..da750aca9b 100644
--- a/bsp/qemu-virt64-riscv/link.lds
+++ b/bsp/qemu-virt64-riscv/link.lds
@@ -9,6 +9,7 @@
*/
INCLUDE "link_stacksize.lds"
+INCLUDE "link_cpus.lds"
OUTPUT_ARCH( "riscv" )
@@ -121,12 +122,9 @@ SECTIONS
{
. = ALIGN(64);
__stack_start__ = .;
-
- . += __STACKSIZE__;
- __stack_cpu0 = .;
-
- . += __STACKSIZE__;
- __stack_cpu1 = .;
+ /* Dynamically allocate stack areas according to RT_CPUS_NR */
+ . += (__STACKSIZE__ * RT_CPUS_NR);
+ __stack_end__ = .;
} > SRAM
.sbss :
@@ -138,6 +136,24 @@ SECTIONS
*(.scommon)
} > SRAM
+ .percpu (NOLOAD) :
+ {
+ /* 2MB Align for MMU early map */
+ . = ALIGN(0x200000);
+ PROVIDE(__percpu_start = .);
+
+ *(.percpu)
+
+ /* 2MB Align for MMU early map */
+ . = ALIGN(0x200000);
+
+ PROVIDE(__percpu_end = .);
+
+ /* Clone the area */
+ . = __percpu_end + (__percpu_end - __percpu_start) * (RT_CPUS_NR - 1);
+ PROVIDE(__percpu_real_end = .);
+ } > SRAM
+
.bss :
{
*(.bss)
diff --git a/bsp/qemu-virt64-riscv/link_cpus.lds b/bsp/qemu-virt64-riscv/link_cpus.lds
new file mode 100644
index 0000000000..2659b2befb
--- /dev/null
+++ b/bsp/qemu-virt64-riscv/link_cpus.lds
@@ -0,0 +1 @@
+RT_CPUS_NR = 8;
diff --git a/bsp/qemu-virt64-riscv/link_smart.lds b/bsp/qemu-virt64-riscv/link_smart.lds
index ddf596630e..29d33fdbb1 100644
--- a/bsp/qemu-virt64-riscv/link_smart.lds
+++ b/bsp/qemu-virt64-riscv/link_smart.lds
@@ -9,6 +9,7 @@
*/
INCLUDE "link_stacksize.lds"
+INCLUDE "link_cpus.lds"
OUTPUT_ARCH( "riscv" )
@@ -122,12 +123,9 @@ SECTIONS
{
. = ALIGN(64);
__stack_start__ = .;
-
- . += __STACKSIZE__;
- __stack_cpu0 = .;
-
- . += __STACKSIZE__;
- __stack_cpu1 = .;
+ /* Dynamically allocate stack areas according to RT_CPUS_NR */
+ . += (__STACKSIZE__ * RT_CPUS_NR);
+ __stack_end__ = .;
} > SRAM
.sbss :
@@ -139,6 +137,24 @@ SECTIONS
*(.scommon)
} > SRAM
+ .percpu (NOLOAD) :
+ {
+ /* 2MB Align for MMU early map */
+ . = ALIGN(0x200000);
+ PROVIDE(__percpu_start = .);
+
+ *(.percpu)
+
+ /* 2MB Align for MMU early map */
+ . = ALIGN(0x200000);
+
+ PROVIDE(__percpu_end = .);
+
+ /* Clone the area */
+ . = __percpu_end + (__percpu_end - __percpu_start) * (RT_CPUS_NR - 1);
+ PROVIDE(__percpu_real_end = .);
+ } > SRAM
+
.bss :
{
*(.bss)
diff --git a/bsp/qemu-virt64-riscv/qemu-dbg.sh b/bsp/qemu-virt64-riscv/qemu-dbg.sh
index a7958ef8e8..69f62e7f6f 100755
--- a/bsp/qemu-virt64-riscv/qemu-dbg.sh
+++ b/bsp/qemu-virt64-riscv/qemu-dbg.sh
@@ -1,4 +1,16 @@
-qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -s -S \
+QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -s -S"
+
+if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
+ hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
+ if [ -z "$hart_num" ]; then
+ hart_num=1
+ fi
+ QEMU_CMD="$QEMU_CMD -smp $hart_num"
+fi
+
+QEMU_CMD="$QEMU_CMD \
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
--device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
+-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0"
+
+eval $QEMU_CMD \ No newline at end of file
diff --git a/bsp/qemu-virt64-riscv/run.sh b/bsp/qemu-virt64-riscv/run.sh
index dd53c95f61..c332915098 100755
--- a/bsp/qemu-virt64-riscv/run.sh
+++ b/bsp/qemu-virt64-riscv/run.sh
@@ -24,7 +24,20 @@ if [ ! -f $path_image ]; then
exit
fi
-qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin \
+QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin"
+
+if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
+ hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds 2>/dev/null | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
+ if [ -z "$hart_num" ] || [ "$hart_num" -lt 1 ]; then
+ echo "Warning: Invalid or missing RT_CPUS_NR, defaulting to 1"
+ hart_num=1
+ fi
+ QEMU_CMD="$QEMU_CMD -smp $hart_num"
+fi
+
+QEMU_CMD="$QEMU_CMD \
-drive if=none,file=$path_image,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
--device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
+-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0"
+
+eval $QEMU_CMD \ No newline at end of file