Closed deepsrc closed 6 years ago
I think your code contains a small error. The input constraints you are giving to the asm statement are :[result] "=&r" (cpu_id), [inp] "=r" (stack_sz)
which means
So gcc is right in not setting stack_sz as it is never read anywhere. There are two options to fix this:
:[result] "=&r" (cpu_id), [inp] "+r" (stack_sz)
(not the + instead of the =)
uint32_t get_cpu_id_mc()
{
uint32_t cpu_id, stack_sz = KERNEL_PARAMS.STACK_SIZE;
asm("addi %[result], %[inp], -1 \n\t"
"not %[result], %[result] \n\t"
"and %[result], %[result], sp \n\t"
"add %[result], %[result], %[inp] \n\t"
"lw %[result], -8(%[result]) \n\t"
:[result] "=&r" (cpu_id) : [inp] "r" (stack_sz));
return cpu_id;
}
Number 2 is of course the better option and also the more correct and elegant one
Did it fix your issue ? If not could you try with the new toolchain here: https://github.com/pulp-platform/pulp-riscv-gnu-toolchain If not please reopen the issue in the new toolchain trackers.
We have a C function [given below] which has an inline assembly code.
At higher levels of optimization [O3] the disassembly of this function looks like this -
The values being passed to the inline assembly are not initialized correctly resulting into data corruption and unexpected code execution.
Without optimization the code disassembles like this -