Skip to content

Commit 7a770ce

Browse files
committed
[libcpu-riscv]: [surpport SMP]: Add dynamic startup based on core configuration.
Add dynamic startup based on core configuration. It should be noted that to pass the SMP Utest, the maximum priority needs to be configured to 256. Signed-off-by: Mengchen Teng <teng_mengchen@163.com>
1 parent 8386d29 commit 7a770ce

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

bsp/qemu-virt64-riscv/SConstruct

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,25 @@ if GetDepend('__STACKSIZE__'): stack_size = GetDepend('__STACKSIZE__')
3838
stack_lds.write('__STACKSIZE__ = %d;\n' % stack_size)
3939
stack_lds.close()
4040

41+
# Obtain the number of harts from rtconfig.h and write
42+
# it into link_cpus.lds for the linker script
43+
try:
44+
with open('rtconfig.h', 'r') as f:
45+
rtconfig_content = f.readlines()
46+
except FileNotFoundError:
47+
cpus_nr = 1
48+
else:
49+
cpus_nr = 1 # default value
50+
for line in rtconfig_content:
51+
line = line.strip()
52+
if line.startswith('#define') and 'RT_CPUS_NR' in line:
53+
parts = line.split()
54+
if len(parts) >= 3 and parts[2].isdigit():
55+
cpus_nr = int(parts[2])
56+
break
57+
58+
with open('link_cpus.lds', 'w') as cpus_lds:
59+
cpus_lds.write(f'RT_CPUS_NR = {cpus_nr};\n')
60+
4161
# make a building
4262
DoBuilding(TARGET, objs)

bsp/qemu-virt64-riscv/link.lds

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
INCLUDE "link_stacksize.lds"
12+
INCLUDE "link_cpus.lds"
1213

1314
OUTPUT_ARCH( "riscv" )
1415

@@ -121,12 +122,9 @@ SECTIONS
121122
{
122123
. = ALIGN(64);
123124
__stack_start__ = .;
124-
125-
. += __STACKSIZE__;
126-
__stack_cpu0 = .;
127-
128-
. += __STACKSIZE__;
129-
__stack_cpu1 = .;
125+
/* Dynamically allocate stack areas according to RT_CPUS_NR */
126+
. += (__STACKSIZE__ * RT_CPUS_NR);
127+
__stack_end__ = .;
130128
} > SRAM
131129

132130
.sbss :
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RT_CPUS_NR = 8;

bsp/qemu-virt64-riscv/qemu-dbg.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -s -S \
1+
QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -s -S"
2+
3+
if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
4+
hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
5+
if [ -z "$hart_num" ]; then
6+
hart_num=1
7+
fi
8+
QEMU_CMD="$QEMU_CMD -smp $hart_num"
9+
fi
10+
11+
QEMU_CMD="$QEMU_CMD \
212
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
313
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
4-
-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
14+
-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"
15+
16+
eval $QEMU_CMD

bsp/qemu-virt64-riscv/run.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,19 @@ if [ ! -f $path_image ]; then
2424
exit
2525
fi
2626

27-
qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -smp 2 \
27+
QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin"
28+
29+
if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
30+
hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
31+
if [ -z "$hart_num" ]; then
32+
hart_num=1
33+
fi
34+
QEMU_CMD="$QEMU_CMD -smp $hart_num"
35+
fi
36+
37+
QEMU_CMD="$QEMU_CMD \
2838
-drive if=none,file=$path_image,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
2939
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
30-
-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
40+
-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"
41+
42+
eval $QEMU_CMD

0 commit comments

Comments
 (0)