[NSWI004] getting stack pointer value

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


Hi,

sorry, I forgot: the GCC documentation also states elsewhere that "The asm keyword is a GNU extension. When writing code that can be compiled with -ansi and the various -std options, use __asm__ instead of asm (see Alternate Keywords)."

Petr


On 08/10/2020 20:16, Georgii Ekserdzhian wrote:
> Good evening,
> 
> I've also tried the "register" keyword route.
> 
> register int *foo asm ("sp");
> 
> this line produces error:
> In file included from src/debug/code.c:4:
> include/debug/code.h: In function 'debug_get_stack_pointer':
> include/debug/code.h:11:23: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'asm'
>    11 |     register int *foo asm ("sp");
>       |                       ^~~
> include/debug/code.h:11:23: error: implicit declaration of function 'asm' [-Werror=implicit-function-declaration]
> cc1: all warnings being treated as errors
> 
> 
> Georgii Ekserdzhian
> 
> 
> чт, 8 окт. 2020 г. в 20:06, Petr Tůma <petr.tuma at d3s.mff.cuni.cz <mailto:petr.tuma at d3s.mff.cuni.cz>>:
> 
>     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 <mailto: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 <mailto:NSWI004 at d3s.mff.cuni.cz>
>     >> https://d3s.mff.cuni.cz/mailman/listinfo/nswi004
>     >
>     > _______________________________________________
>     > NSWI004 mailing list
>     > NSWI004 at d3s.mff.cuni.cz <mailto:NSWI004 at d3s.mff.cuni.cz>
>     > https://d3s.mff.cuni.cz/mailman/listinfo/nswi004
>     >
>     _______________________________________________
>     NSWI004 mailing list
>     NSWI004 at d3s.mff.cuni.cz <mailto: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