[NSWI004] getting stack pointer value

Petr Tůma petr.tuma at d3s.mff.cuni.cz
Thu Oct 8 20:06:44 CEST 2020


Hi,

inline assembly is one option, but you could also try the "register" keyword, as suggested by the README file. For exact syntax, see https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html#Local-Register-Variables, this makes it possible to access specific register values directly from C (simply by defining a variable that resides in that particular register and then reading it).

Many compiler optimizations make naive solutions for accessing stack location fail, so the above solution might really be better than trying without "register".

Petr


On 08/10/2020 19:11, Peter G wrote:
> Hi,
> 
> I have encountered similar behaviour. When solving the problem without inline assembly I got this output
> 
> sp1 = 800003e0
> sp2 = 800003e0
> 
> tests/basic/stack_pointer/test.c:43: Kernel test assertion failed: sp1 == sp2
> tests/basic/stack_pointer/test.c:43: $sp unexpectedly changed (0x800003e0 => 0x800003e0)
> 
> I could not figure out why this happen, but after I added these two lines the test passes:
> 
> printk("&sp1 = %p\n", &sp1);
> printk("&sp2 = %p\n", &sp2);
> I think that after adding these two lines compiler saves sp1 and sp2 on the stack, otherwise they are kept in the registers. I tried to find where the problem lies in the disassembly but I have not figured it out.
> 
> However, solution using inline assembly worked right away.
> 
> Best regards,
> Peter Grajcar
> 
>> On 8 Oct 2020, at 17:13, Georgii Ekserdzhian <gxrjan at gmail.com> wrote:
>>
>> Good afternoon,
>>
>> I've been thinking on debug_get_stack_pointer function for quite a while now.
>> I pushed my code into my repo. 
>> Also I attached my log. 
>> For some reason the test that asserts that sp1 == sp2 fails, but the log shows that they are the same.
>>
>> Also question. You said that we should try first without using inline assembly. 
>> I tried to use asm() function and the compiler doesn't recognize this function call.
>> Is there another way to use assembly in C?
>>
>> Georgii Ekserdzhian
>> <log>_______________________________________________
>> NSWI004 mailing list
>> NSWI004 at d3s.mff.cuni.cz
>> https://d3s.mff.cuni.cz/mailman/listinfo/nswi004
> 
> _______________________________________________
> NSWI004 mailing list
> NSWI004 at d3s.mff.cuni.cz
> https://d3s.mff.cuni.cz/mailman/listinfo/nswi004
> 


More information about the NSWI004 mailing list