Skip to contentSkip to conversation

Xen/ARM - Query about a data abort seen while reading GICD registers

DISCUSSION Ayan Kumar Halder 4 participants 9 messages Nov 16, 2021 – Nov 19, 2021

lore
Share & tools
lore
#0 lore raw

Hi Xen/Arm experts,

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...

    /* All the instructions used on emulated MMIO region should be valid */
    if ( !dabt.valid ) {

        return IO_ABORT;
    }

...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#1 lore raw

Hi Ayan,

Quoted 9 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

Could you try with:
add x1, x1, #4
ldr w2, [x1]

Regards
Bertrand

Quoted 41 lines

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...
/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid ) {

       return IO_ABORT;

}
...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#2 lore raw

Hi Bertrand,

Thanks for looking into it.

Quoted 6 lines · Bertrand Marquis

Hi Ayan,

Quoted 4 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

Quoted 7 lines
Quoted 5 lines

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

Can you point me in source where this limitation is set ?

Quoted 4 lines

Could you try with:
add x1, x1, #4
ldr w2, [x1]

This worked fine.

-Ayan

Quoted 46 lines

Regards
Bertrand

Quoted 41 lines

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...

    /* All the instructions used on emulated MMIO region should be valid */
    if ( !dabt.valid ) {

        return IO_ABORT;
    }

...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#3 lore raw

Hi Ayan,

Quoted 12 lines · Ayan Kumar Halder

Hi Bertrand,

Thanks for looking into it.

Quoted 5 lines · Bertrand Marquis

Hi Ayan,

Quoted 4 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value. There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

Quoted 2 lines

Can you point me in source where this limitation is set ?

In this case the isv field is set to 0 when the exception is received by Xen which means the decoding part of HSR is invalid so Xen is not handling the emulation of the access.

@julien and Stefano: I have been going around and around the code but I cannot find where the valid bit of hsr_dabt is checked, could you help me here ?

Quoted 5 lines
Quoted 3 lines

Could you try with:
add x1, x1, #4
ldr w2, [x1]

This worked fine.

Good :-)

Bertrand

Quoted 45 lines

-Ayan

Quoted 43 lines

Regards
Bertrand

Quoted 41 lines

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...
/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid ) {

       return IO_ABORT;

}
...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#4 lore raw

Hi Bertrand,

Many thanks for your response.

Quoted 16 lines · Bertrand Marquis

Hi Ayan,

Quoted 14 lines · Ayan Kumar Halder

Hi Bertrand,

Thanks for looking into it.

Quoted 7 lines · Bertrand Marquis

Hi Ayan,

Quoted 6 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value.

This is very interesting. Is this being done for any of the other instructions in Xen ?

Quoted 1 line

There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

I am actually trying to understand where I will need to make the changes if I have to add support for the decoder. The reason being this issue is being faced by one of our customer application. Besides changing the instruction to prevent post increment, is there any other mitigation ?

The reason being I don't see a way to instruct the compiler to not generate the post indexing instructions.

Quoted 7 lines
Quoted 3 lines

Can you point me in source where this limitation is set ?

In this case the isv field is set to 0 when the exception is received by Xen which means the decoding part of HSR is invalid so Xen is not handling the emulation of the access.

@julien and Stefano: I have been going around and around the code but I cannot find where the valid bit of hsr_dabt is checked, could you help me here ?

By any chance, are you referring to https://github.com/Xilinx/xen/blob/4bd2da58b5b008f77429007a307b658db9c0f636/xen/arch/arm/io.c#L129 ?

Also, post indexing works for some other registers (eg GICC). So, I am guessing that the behaviour is specific for GICD or random ?

- Ayan

Quoted 57 lines
Quoted 6 lines
Quoted 4 lines

Could you try with:
add x1, x1, #4
ldr w2, [x1]

This worked fine.

Good :-)

Bertrand

Quoted 45 lines

-Ayan

Quoted 43 lines

Regards
Bertrand

Quoted 41 lines

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...

    /* All the instructions used on emulated MMIO region should be valid */
    if ( !dabt.valid ) {

        return IO_ABORT;
    }

...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#5 lore raw

Hi Ayan,

Quoted 25 lines · Ayan Kumar Halder

Hi Bertrand,

Many thanks for your response.

Quoted 18 lines · Bertrand Marquis

Hi Ayan,

Quoted 17 lines · Ayan Kumar Halder

Hi Bertrand,

Thanks for looking into it.

Quoted 10 lines · Bertrand Marquis

Hi Ayan,

Quoted 9 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value.

This is very interesting. Is this being done for any of the other instructions in Xen ?

No Xen is not trying to decode any instructions.
If you look at Linux source code, this is the kind of stuff that the kernel is delegating to user application (qemu) to do as it can be quite complex.

Quoted 4 lines
Quoted 1 line

There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

I am actually trying to understand where I will need to make the changes if I have to add support for the decoder. The reason being this issue is being faced by one of our customer application. Besides changing the instruction to prevent post increment, is there any other mitigation ?

Not that I know of.

Quoted 2 lines

The reason being I don't see a way to instruct the compiler to not generate the post indexing instructions.

You can define io access functions instead of letting the compiler generate the read/write functions. Look at arch/arm64/include/asm-arm/io.h in linux for example.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Can you point me in source where this limitation is set ?

In this case the isv field is set to 0 when the exception is received by Xen which means the decoding part of HSR is invalid so Xen is not handling the emulation of the access. @julien and Stefano: I have been going around and around the code but I cannot find where the valid bit of hsr_dabt is checked, could you help me here ?

By any chance, are you referring to https://github.com/Xilinx/xen/blob/4bd2da58b5b008f77429007a307b658db9c0f636/xen/arch/arm/io.c#L129 ?

Yes thanks :-) I was stuck in traps.c but it is actually done later.

Quoted 2 lines

Also, post indexing works for some other registers (eg GICC). So, I am guessing that the behaviour is specific for GICD or random ?

Distributor is emulated by Xen.
Cpu part of the GIC is using the hardware so Xen is not emulating io access to this area, the processor is which is why it works in that case.

Bertrand

Quoted 53 lines

- Ayan

Quoted 51 lines
Quoted 5 lines
Quoted 3 lines

Could you try with:
add x1, x1, #4
ldr w2, [x1]

This worked fine.

Good :-)
Bertrand

Quoted 44 lines

-Ayan

Quoted 42 lines

Regards
Bertrand

Quoted 40 lines

And then I get :-
HSR=0x92000005 pc=0x00000040000ca8 gva=0x3001000 gpa=0x00000003001000

This problem occurs only while reading the GICD registers with post indexing. If I read the register with pre-indexing, then I do not see any abort. Alternatively, if I read GICC register with post indexing, I don't see the abort either.

From the HSR value, I interpret it as
EC = 100100b # Data abort from lower exception
IL = 1b # 32 bit instruction trapped
DFSC = 101 # Translation fault level 1

On debugging, I found that the abort is triggered from

try_handle_mmio()
{ ...
/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid ) {

       return IO_ABORT;

}
...
}

From the Arm V8 Arm specs, I understand that dabt.valid is ISV, bit[24] in "ISS encoding for an exception from a Data Abort".

I saw that the caller is

do_trap_guest_sync() "case HSR_EC_DATA_ABORT_LOWER_EL" where dabt.valid is false.
In the success scenario, dabt.valid is true.

I could not find the caller for do_trap_guest_sync()

So, can anyone help me here
1. Who is the caller for do_trap_guest_sync() ?
2. Any idea on what the issue is and how I can debug it further ?

Kind regards,
Ayan

#6 lore raw

Hi,

I will combine my answers for this thread in one single e-mail.

Quoted 25 lines · Bertrand Marquis
Quoted 25 lines · Ayan Kumar Halder

Hi Bertrand,

Many thanks for your response.

Quoted 18 lines · Bertrand Marquis

Hi Ayan,

Quoted 17 lines · Ayan Kumar Halder

Hi Bertrand,

Thanks for looking into it.

Quoted 10 lines · Bertrand Marquis

Hi Ayan,

Quoted 9 lines · Ayan Kumar Halder

Hi Xen/Arm experts,

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :- ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value.

This is very interesting. Is this being done for any of the other instructions in Xen ?

No Xen is not trying to decode any instructions.

We actually decode some instructions (see arch/arm/decode.c). This was necessary because early revision of Cortex-A15 would not properly fill syndrome for Thumb instructions.

decode_instruction() could be extended to handle the specific instruction if needed.

Quoted 8 lines

If you look at Linux source code, this is the kind of stuff that the kernel is delegating to user application (qemu) to do as it can be quite complex.

Quoted 4 lines
Quoted 1 line

There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

I am actually trying to understand where I will need to make the changes if I have to add support for the decoder. The reason being this issue is being faced by one of our customer application. Besides changing the instruction to prevent post increment, is there any other mitigation ?

Not that I know of.

Decoding the instruction is the only solution if you don't want to update the baremetal app.

Quoted 2 lines
Quoted 2 lines

The reason being I don't see a way to instruct the compiler to not generate the post indexing instructions.

In general, it is not safe to let the compiler decide for you how to access the MMIO registers as it can do all sort of optimization behind your back. That's why...

Quoted 3 lines

You can define io access functions instead of letting the compiler generate the read/write functions. Look at arch/arm64/include/asm-arm/io.h in linux for example.

... Linux provides io{read, write} helpers.

@Ayan, is the code written in assembly or C? If the latter, how is it written?

That said, the ldr writeback instructions should be safe to use. It happens that we never had OS using them before.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw
Quoted 78 lines · Julien Grall

I will combine my answers for this thread in one single e-mail.

Quoted 34 lines · Bertrand Marquis
Quoted 34 lines

On 17 Nov 2021, at 16:21, Ayan Kumar Halder <[email protected]> wrote:

Hi Bertrand,

Many thanks for your response.

Quoted 26 lines · Bertrand Marquis

Hi Ayan,

Quoted 25 lines

On 16 Nov 2021, at 16:24, Ayan Kumar Halder
<[email protected]> wrote:

Hi Bertrand,

Thanks for looking into it.

Quoted 6 lines · Bertrand Marquis

Hi Ayan,

Quoted 5 lines

On 16 Nov 2021, at 15:27, Ayan Kumar Halder
<[email protected]> wrote:

Hi Xen/Arm experts,

Quoted 8 lines
Quoted 7 lines

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :-
ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value.

This is very interesting. Is this being done for any of the other instructions in Xen ?

No Xen is not trying to decode any instructions.

We actually decode some instructions (see arch/arm/decode.c). This was necessary because early revision of Cortex-A15 would not properly fill syndrome for Thumb instructions.

decode_instruction() could be extended to handle the specific instruction if needed.

Quoted 6 lines

If you look at Linux source code, this is the kind of stuff that the kernel is delegating to user application (qemu) to do as it can be quite complex.

Quoted 3 lines
Quoted 2 lines

There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

Quoted 7 lines
Quoted 5 lines

I am actually trying to understand where I will need to make the changes if I have to add support for the decoder. The reason being this issue is being faced by one of our customer application.
Besides changing the instruction to prevent post increment, is there any other mitigation ?

Not that I know of.

Decoding the instruction is the only solution if you don't want to update the baremetal app.

Quoted 3 lines
Quoted 3 lines

The reason being I don't see a way to instruct the compiler to not generate the post indexing instructions.

In general, it is not safe to let the compiler decide for you how to access the MMIO registers as it can do all sort of optimization behind your back. That's why...

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

You can define io access functions instead of letting the compiler generate the read/write functions.
Look at arch/arm64/include/asm-arm/io.h in linux for example.

... Linux provides io{read, write} helpers.

@Ayan, is the code written in assembly or C? If the latter, how is it written?

I chatted with Ayan this morning and he will try to contact the original author and get a proper answer, but I am pretty sure that it is written in C. What makes it worse is that I believe the issue only started to appear recently due to updating compiler (hence new compiler optimizations.) Which means that this issue might become more common in the future in other environments too :-(

I believe this is the code (source [1]):

    XScuGic_WriteReg(BaseAddress, XSCUGIC_DIST_EN_OFFSET, 0UL);

which expands to:

    #define XScuGic_WriteReg(BaseAddress, RegOffset, Data) \
    	(Xil_Out32(((BaseAddress) + (RegOffset)), ((u32)(Data))))

Which is:

    static INLINE void Xil_Out32(UINTPTR Addr, u32 Value)
    {
    	/* write 32 bit value to specified address */
    #ifndef ENABLE_SAFETY
    	volatile u32 *LocalAddr = (volatile u32 *)Addr;
    	*LocalAddr = Value;
    #else
    	XStl_RegUpdate(Addr, Value);
    #endif
    }

[1] https://github.com/Xilinx/embeddedsw/tree/master/XilinxProcessorIPLib/drivers/scugic/examples/xscugic_low_level_example.c

So it seems to be a pretty standard volatile write in a static inline function.

Quoted 2 lines

That said, the ldr writeback instructions should be safe to use. It happens that we never had OS using them before.

Yeah I agree... My two cents is that if we can find a way to decode the instruction without a huge amount of code then it would be worth doing it.

#8 lore raw

Hi Stefano/Julien/Bertrand,

Thanks a lot for your inputs.

Quoted 123 lines · Stefano Stabellini
Quoted 78 lines · Julien Grall

I will combine my answers for this thread in one single e-mail.

Quoted 34 lines · Bertrand Marquis
Quoted 34 lines

On 17 Nov 2021, at 16:21, Ayan Kumar Halder <[email protected]> wrote:

Hi Bertrand,

Many thanks for your response.

Quoted 26 lines · Bertrand Marquis

Hi Ayan,

Quoted 25 lines

On 16 Nov 2021, at 16:24, Ayan Kumar Halder
<[email protected]> wrote:

Hi Bertrand,

Thanks for looking into it.

Quoted 6 lines · Bertrand Marquis

Hi Ayan,

Quoted 5 lines

On 16 Nov 2021, at 15:27, Ayan Kumar Halder
<[email protected]> wrote:

Hi Xen/Arm experts,

Quoted 8 lines
Quoted 7 lines

I am facing a very strange issue while running a baremetal application as a DomU guest on arm64 platform.

The baremetal app tries to read the GICD register with post indexing as follows :-
ldr x1, =0x3001000
ldr w2, [x1], #4 <<<------ PC = 0x40000ca8

Increment on on load is not supported by the emulation layer.

That is surprising. The reason being if I try to read the GICC register (0x3002000) with post indexing then it works fine.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

When the ISV bit is not set, Xen would have to decode the instruction to actually emulate the access and properly modify the registers values as long as providing the “emulated” access value.

This is very interesting. Is this being done for any of the other instructions in Xen ?

No Xen is not trying to decode any instructions.

We actually decode some instructions (see arch/arm/decode.c). This was necessary because early revision of Cortex-A15 would not properly fill syndrome for Thumb instructions.

decode_instruction() could be extended to handle the specific instruction if needed.

Quoted 6 lines

If you look at Linux source code, this is the kind of stuff that the kernel is delegating to user application (qemu) to do as it can be quite complex.

Quoted 3 lines
Quoted 2 lines

There is not such a decoder in Xen right now which means those kind of accesses are not supported for emulated mmio accesses.

Quoted 7 lines
Quoted 5 lines

I am actually trying to understand where I will need to make the changes if I have to add support for the decoder. The reason being this issue is being faced by one of our customer application.
Besides changing the instruction to prevent post increment, is there any other mitigation ?

Not that I know of.

Decoding the instruction is the only solution if you don't want to update the baremetal app.

Quoted 3 lines
Quoted 3 lines

The reason being I don't see a way to instruct the compiler to not generate the post indexing instructions.

In general, it is not safe to let the compiler decide for you how to access the MMIO registers as it can do all sort of optimization behind your back. That's why...

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

You can define io access functions instead of letting the compiler generate the read/write functions.
Look at arch/arm64/include/asm-arm/io.h in linux for example.

... Linux provides io{read, write} helpers.

@Ayan, is the code written in assembly or C? If the latter, how is it written?

I chatted with Ayan this morning and he will try to contact the original author and get a proper answer, but I am pretty sure that it is written in C. What makes it worse is that I believe the issue only started to appear recently due to updating compiler (hence new compiler optimizations.) Which means that this issue might become more common in the future in other environments too :-(

I believe this is the code (source [1]):

     XScuGic_WriteReg(BaseAddress, XSCUGIC_DIST_EN_OFFSET, 0UL);

which expands to:

     #define XScuGic_WriteReg(BaseAddress, RegOffset, Data) \
     	(Xil_Out32(((BaseAddress) + (RegOffset)), ((u32)(Data))))

Which is:

     static INLINE void Xil_Out32(UINTPTR Addr, u32 Value)
     {
     	/* write 32 bit value to specified address */
     #ifndef ENABLE_SAFETY
     	volatile u32 *LocalAddr = (volatile u32 *)Addr;
     	*LocalAddr = Value;
     #else
     	XStl_RegUpdate(Addr, Value);
     #endif
     }

[1] https://github.com/Xilinx/embeddedsw/tree/master/XilinxProcessorIPLib/drivers/scugic/examples/xscugic_low_level_example.c

So it seems to be a pretty standard volatile write in a static inline function.

Quoted 7 lines
Quoted 2 lines

That said, the ldr writeback instructions should be safe to use. It happens that we never had OS using them before.

Yeah I agree... My two cents is that if we can find a way to decode the instruction without a huge amount of code then it would be worth doing it.

I have submitted a patch to decode the ldr/str post-indexing instructions. "[RFC PATCH] Added the logic to decode 32 bit ldr/str post-indexing instructions"

Please have a look and let me know your thoughts.
- Ayan