sim: riscv: Make stack 16-byte aligned

Message ID PAXP193MB1296432A56306213F013C1CDE4062@PAXP193MB1296.EURP193.PROD.OUTLOOK.COM
State New
Headers
Series sim: riscv: Make stack 16-byte aligned |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Bernd Edlinger April 10, 2024, 12:09 p.m. UTC
  Various gcc test cases fail due to the stack
alignment of 16 bytes is expected by gcc,
causing issues mostly with vararg functinos;
e.g.

FAIL: gcc.c-torture/execute/nest-align-1.c   -O0  execution test
FAIL: gcc.c-torture/execute/nest-stdar-1.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-12.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-15.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-16.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-17.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-20.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-26.c   -O0  execution test
...
---
 sim/riscv/sim-main.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Andrew Burgess April 12, 2024, 10:32 a.m. UTC | #1
Bernd Edlinger <bernd.edlinger@hotmail.de> writes:

> Various gcc test cases fail due to the stack
> alignment of 16 bytes is expected by gcc,

And indeed by the RISC-V ABI specification I believe.

> causing issues mostly with vararg functinos;

Typo: functions.

> e.g.
>
> FAIL: gcc.c-torture/execute/nest-align-1.c   -O0  execution test
> FAIL: gcc.c-torture/execute/nest-stdar-1.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-12.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-15.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-16.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-17.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-20.c   -O0  execution test
> FAIL: gcc.c-torture/execute/va-arg-26.c   -O0  execution test
> ...
> ---
>  sim/riscv/sim-main.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/sim/riscv/sim-main.c b/sim/riscv/sim-main.c
> index 4e3672505c6..0876d455570 100644
> --- a/sim/riscv/sim-main.c
> +++ b/sim/riscv/sim-main.c
> @@ -1586,6 +1586,8 @@ initialize_env (SIM_DESC sd, const char * const *argv, const char * const *env)
>    sp = sp_flat - ((argc + 1 + envc + 1) * sizeof (address_word));
>    /* Then the argc.  */
>    sp -= sizeof (unsigned_word);
> +  /* Align to 16 bytes.  */
> +  sp &= ~(address_word)15;

I think you should use 'align_down' from common/sim-bits.h, as:

  /* Align to 16 bytes.  */
  sp = align_down (sp, 15);

Assuming that works then:

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew

>  
>    /* Set up the regs the libgloss crt0 expects.  */
>    riscv_cpu->a0 = argc;
> -- 
> 2.25.1
  
Bernd Edlinger April 15, 2024, 8:27 a.m. UTC | #2
On 4/12/24 12:32, Andrew Burgess wrote:
> Bernd Edlinger <bernd.edlinger@hotmail.de> writes:
> 
>> Various gcc test cases fail due to the stack
>> alignment of 16 bytes is expected by gcc,
> 
> And indeed by the RISC-V ABI specification I believe.
> 
>> causing issues mostly with vararg functinos;
> 
> Typo: functions.
> 
>> e.g.
>>
>> FAIL: gcc.c-torture/execute/nest-align-1.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/nest-stdar-1.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-12.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-15.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-16.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-17.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-20.c   -O0  execution test
>> FAIL: gcc.c-torture/execute/va-arg-26.c   -O0  execution test
>> ...
>> ---
>>  sim/riscv/sim-main.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/sim/riscv/sim-main.c b/sim/riscv/sim-main.c
>> index 4e3672505c6..0876d455570 100644
>> --- a/sim/riscv/sim-main.c
>> +++ b/sim/riscv/sim-main.c
>> @@ -1586,6 +1586,8 @@ initialize_env (SIM_DESC sd, const char * const *argv, const char * const *env)
>>    sp = sp_flat - ((argc + 1 + envc + 1) * sizeof (address_word));
>>    /* Then the argc.  */
>>    sp -= sizeof (unsigned_word);
>> +  /* Align to 16 bytes.  */
>> +  sp &= ~(address_word)15;
> 
> I think you should use 'align_down' from common/sim-bits.h, as:
> 
>   /* Align to 16 bytes.  */
>   sp = align_down (sp, 15);
> 
> Assuming that works then:
> 
> Approved-By: Andrew Burgess <aburgess@redhat.com>
> 
> Thanks,
> Andrew
> 

Okay, thanks for the hint about align_down.
So I changed that to:

sp = align_down (sp, 16);

and it worked...
Pushed.

Thanks
Bernd.
  

Patch

diff --git a/sim/riscv/sim-main.c b/sim/riscv/sim-main.c
index 4e3672505c6..0876d455570 100644
--- a/sim/riscv/sim-main.c
+++ b/sim/riscv/sim-main.c
@@ -1586,6 +1586,8 @@  initialize_env (SIM_DESC sd, const char * const *argv, const char * const *env)
   sp = sp_flat - ((argc + 1 + envc + 1) * sizeof (address_word));
   /* Then the argc.  */
   sp -= sizeof (unsigned_word);
+  /* Align to 16 bytes.  */
+  sp &= ~(address_word)15;
 
   /* Set up the regs the libgloss crt0 expects.  */
   riscv_cpu->a0 = argc;