SPARC assembly, How a C pointer to struct is accessed

1

I have a small piece of SPARC assembly code that I'm trying to understand.

 .section ".text"
 .global tt_0x09
tt_0x09:
 /* Find AHBSTAT */
 set ahbstat, %l3
 ld [%l3], %l3
 /* Get failing address from AHBSTAT faddr. */
 ld [%l3 + 4], %l4

 set 0xDEADBEEF, %l5
 st %l5, [%l4]

 /*st %g0, [%l3]*/
 /* Re-execute trapped instruction. */
 jmp %l1
 rett %l2 

where ahbstat is defined in a C file

#define AHBSTAT_REGS 0x80000f00
struct ahbstat_regs { unsigned int status, faddr; };
...
volatile struct ahbstat_regs *ahbstat = (void *) AHBSTAT_REGS;

For the sake of completeness, the assembly snippet I showed above is the code of a trap which is mapped using a particular library function:

extern void tt_0x09(void); static const int TT_DATA_ACCESS_EXCEPTION = 0x09; ... bcc_set_trap(TT_DATA_ACCESS_EXCEPTION, tt_0x09);

The point which I do not understand is how the struct is accessed in the assembly code. In fact, if l3contains the address of the struct, then using ld [%l3], %l3 I'm loading a word from the memory to l3 it self, So I'm copy the value of the unsigned int status of the struct into l3.

Another problem is in the consecutive lines: It set l5 to 0xDEADBEEF and then store l5 in the memory location pointed to l4. But l4 has been loaded with the value at memory location [%l3+4] which is a non-sense as far as I understand because l3 contains the value of unsigned int status of the struct.

The program should write 0xdeadbeef in the memory location pointed by the failing address, which is the address contained in faddr of the struct.

I'm wrong somewhere, but I think I'm clear with the load instruction: ld [addr], rd -> Load a word from addr into rd.

So I do not think it's clear to me how a C struct pointer is "translated" in assembly.

Thanks in advance for your help, excuse me if something I wrote is not clear.

c
pointers
assembly
struct
sparc
asked on Stack Overflow Jan 30, 2018 by Andak • edited Jan 30, 2018 by Julien Rousé

1 Answer

3

You have almost understood everything correctly except for what ahbstat is.

In the C code, you can see the declaration -

volatile struct ahbstat_regs *ahbstat = (void *) AHBSTAT_REGS;

which means ahbstat is a pointer to the struct. The label ahbstat in assembly thus becomes the address of this pointer. Which makes %l3, the address of the address of the struct.

I think with this correction, you can figure out the rest.


User contributions licensed under CC BY-SA 3.0