[BUG] hartid and cpuid mismatch for RISCV when CONFIG_ARCH_RV_CPUID_MAP is configured
- Dominant language
- C
- Stars
- 4k
- Forks
- 1.7k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 237
Description
### Description / Steps to reproduce the issue
CONFIG_ARCH_RV_CPUID_MAP is configured
1. bellow code using g_cpux_idlestack to get stack but without translate cpuid to hartid
```
arch/risc-v/src/common/riscv_cpuidlestack.c
//will read stack by cpuid
stack_alloc = (uintptr_t)g_cpux_idlestack(cpu);
```
```
arch/risc-v/src/common/riscv_initialstate.c
// will read stack by cpuid
tcb->stack_alloc_ptr = (void *)g_cpux_idlestack(this_cpu());
```
```
arch/risc-v/src/common/riscv_common_memorymap.h
//using cpuid to read stack
#define g_cpux_idlestack(cpuid) \
(g_idle_topstack - SMP_STACK_SIZE * (CONFIG_SMP_NCPUS - (cpuid)))
```
2. if using customized cpuid to hartid mapping like bellow, stack is wrong.
```
int riscv_hartid_to_cpuid(int hart)
{
#ifdef CONFIG_SMP
int cpu_id = -1;
switch(hart) {
case 0: cpu_id = 0; break;
case 1: cpu_id = 2; break;
case 2: cpu_id = 1; break;
default :
DEBUGASSERT(0);
break;
}
return cpu_id;
#else
return 0;
#endif
}
int riscv_cpuid_to_hartid(int cpu)
{
#ifdef CONFIG_SMP
int hart_id = -1;
switch(cpu) {
case 0: hart_id = 0; break;
case 1: hart_id = 2; break;
case 2: hart_id = 1; break;
default :
DEBUGASSERT(0);
break;
}
return hart_id;
#else
return up_cpu_index();
#endif
}
```
### On which OS does this issue occur?
[OS: Linux]
### What is the version of your OS?
Ubuntu 22.04.4 LTS
### NuttX Version
96f440f0c068f278cc11a956c2c6cf52ff79cf01
### Issue Architecture
[Arch: risc-v]
### Issue Area
[Area: Kernel]
### Host information
qemu to run the rv64
qemu-system-riscv64 --version
QEMU emulator version 10.0.0
Copyright (c) 2003-2025 Fabrice Bellard and the QEMU Project developers
### Verification
- [x] I have verified before submitting the report.
Contributor guide
Research direction
Start with arch/risc-v/src/common/riscv_cpuidlestack.c, arch/risc-v/src/common/riscv_initialstate.c, and arch/risc-v/src/common/riscv_common_memorymap.h, then inspect the cpuid-to-hartid mapping functions shown in the report. Reproduce the configured mapping under QEMU and verify that idle-stack selection remains correct for each CPU; done means the reported mismatch no longer selects the wrong stack.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot, operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100