2

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.

Julien Rousé
  • 1,115
  • 1
  • 15
  • 30
Andak
  • 124
  • 12

1 Answers1

4

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.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • I understood that `ahbstat` was a pointer to the struct. I did not know instead how the label `ahbstat` would become in assembly. With your correction I have a clear vision of the total snippet. Thank you Ajay – Andak Jan 30 '18 at 08:30
  • @Andak: C global / static variable names always become asm label names that refer to the static storage where the variable's value is stored. – Peter Cordes Jan 30 '18 at 12:29