diff mbox series

[08/13] hw/xtensa/xtfpga: Replace memcpy()+tswap32() by stl_endian_p()

Message ID 20240930073450.33195-9-philmd@linaro.org (mailing list archive)
State New, archived
Headers show
Series hw: Add ld/st_endian() APIs | expand

Commit Message

Philippe Mathieu-Daudé Sept. 30, 2024, 7:34 a.m. UTC
Replace a pair of memcpy() + tswap32() by stl_endian_p(),
which also swap the value using target endianness.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/xtensa/xtfpga.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Thomas Huth Sept. 30, 2024, 2:32 p.m. UTC | #1
On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
> which also swap the value using target endianness.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/xtensa/xtfpga.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
> index 228f00b045..521fe84b01 100644
> --- a/hw/xtensa/xtfpga.c
> +++ b/hw/xtensa/xtfpga.c
> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc *board, MachineState *machine)
>               const size_t boot_sz = TARGET_BIG_ENDIAN ? sizeof(boot_be)
>                                                        : sizeof(boot_le);
>               uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
> -            uint32_t entry_pc = tswap32(entry_point);
> -            uint32_t entry_a2 = tswap32(tagptr);
>   
> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);

Why don't you simply use stl_p() here?

  Thomas
Philippe Mathieu-Daudé Oct. 3, 2024, 4:02 p.m. UTC | #2
On 30/9/24 16:32, Thomas Huth wrote:
> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>> which also swap the value using target endianness.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   hw/xtensa/xtfpga.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>> index 228f00b045..521fe84b01 100644
>> --- a/hw/xtensa/xtfpga.c
>> +++ b/hw/xtensa/xtfpga.c
>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc 
>> *board, MachineState *machine)
>>               const size_t boot_sz = TARGET_BIG_ENDIAN ? sizeof(boot_be)
>>                                                        : sizeof(boot_le);
>>               uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>> -            uint32_t entry_pc = tswap32(entry_point);
>> -            uint32_t entry_a2 = tswap32(tagptr);
>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
> 
> Why don't you simply use stl_p() here?

We want to remove the tswap32() calls...
Pierrick Bouvier Oct. 3, 2024, 4:04 p.m. UTC | #3
On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
> On 30/9/24 16:32, Thomas Huth wrote:
>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>> which also swap the value using target endianness.
>>>
>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>> ---
>>>    hw/xtensa/xtfpga.c | 6 ++----
>>>    1 file changed, 2 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>> index 228f00b045..521fe84b01 100644
>>> --- a/hw/xtensa/xtfpga.c
>>> +++ b/hw/xtensa/xtfpga.c
>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>> *board, MachineState *machine)
>>>                const size_t boot_sz = TARGET_BIG_ENDIAN ? sizeof(boot_be)
>>>                                                         : sizeof(boot_le);
>>>                uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>>> -            uint32_t entry_pc = tswap32(entry_point);
>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>
>> Why don't you simply use stl_p() here?
> 
> We want to remove the tswap32() calls...
> 

I think is point is that you could directly use stl_be_p, instead of 
stl_endian_p(TARGET_BIT_ENDIAN, ...).
I don't know if your intent is to make be/le variant "private" and 
relies only on endian_p though.
Philippe Mathieu-Daudé Oct. 3, 2024, 8:48 p.m. UTC | #4
On 3/10/24 18:04, Pierrick Bouvier wrote:
> On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
>> On 30/9/24 16:32, Thomas Huth wrote:
>>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>>> which also swap the value using target endianness.
>>>>
>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>> ---
>>>>    hw/xtensa/xtfpga.c | 6 ++----
>>>>    1 file changed, 2 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>>> index 228f00b045..521fe84b01 100644
>>>> --- a/hw/xtensa/xtfpga.c
>>>> +++ b/hw/xtensa/xtfpga.c
>>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>>> *board, MachineState *machine)
>>>>                const size_t boot_sz = TARGET_BIG_ENDIAN ? 
>>>> sizeof(boot_be)
>>>>                                                         : 
>>>> sizeof(boot_le);
>>>>                uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>>>> -            uint32_t entry_pc = tswap32(entry_point);
>>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>>
>>> Why don't you simply use stl_p() here?
>>
>> We want to remove the tswap32() calls...
>>
> 
> I think is point is that you could directly use stl_be_p, instead of 
> stl_endian_p(TARGET_BIT_ENDIAN, ...).

TARGET_BIG_ENDIAN is defined as 0 on little endian, and 1 on big one.

The following change isn't worth it:

   if (TARGET_BIG_ENDIAN) {
     stl_be_p(boot + 8, tagptr);
   } else {
     stl_le_p(boot + 8, tagptr);
   }

Maybe I'm missing Thomas point, as the xtfpga machines are available
for both xtensa-softmmu (LE) and xtensaeb-softmmu (BE).

> I don't know if your intent is to make be/le variant "private" and 
> relies only on endian_p though.

My intent is to enforce endian agnostic API uses when possible, and
use LE/BE specific variant when it is known at build time.
Pierrick Bouvier Oct. 3, 2024, 9:31 p.m. UTC | #5
On 10/3/24 13:48, Philippe Mathieu-Daudé wrote:
> On 3/10/24 18:04, Pierrick Bouvier wrote:
>> On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
>>> On 30/9/24 16:32, Thomas Huth wrote:
>>>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>>>> which also swap the value using target endianness.
>>>>>
>>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>>> ---
>>>>>     hw/xtensa/xtfpga.c | 6 ++----
>>>>>     1 file changed, 2 insertions(+), 4 deletions(-)
>>>>>
>>>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>>>> index 228f00b045..521fe84b01 100644
>>>>> --- a/hw/xtensa/xtfpga.c
>>>>> +++ b/hw/xtensa/xtfpga.c
>>>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>>>> *board, MachineState *machine)
>>>>>                 const size_t boot_sz = TARGET_BIG_ENDIAN ?
>>>>> sizeof(boot_be)
>>>>>                                                          :
>>>>> sizeof(boot_le);
>>>>>                 uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>>>>> -            uint32_t entry_pc = tswap32(entry_point);
>>>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>>>
>>>> Why don't you simply use stl_p() here?
>>>
>>> We want to remove the tswap32() calls...
>>>
>>
>> I think is point is that you could directly use stl_be_p, instead of
>> stl_endian_p(TARGET_BIT_ENDIAN, ...).
> 
> TARGET_BIG_ENDIAN is defined as 0 on little endian, and 1 on big one.
> 
> The following change isn't worth it:
> 
>     if (TARGET_BIG_ENDIAN) {
>       stl_be_p(boot + 8, tagptr);
>     } else {
>       stl_le_p(boot + 8, tagptr);
>     }
> 
> Maybe I'm missing Thomas point, as the xtfpga machines are available
> for both xtensa-softmmu (LE) and xtensaeb-softmmu (BE).
> 
>> I don't know if your intent is to make be/le variant "private" and
>> relies only on endian_p though.
> 
> My intent is to enforce endian agnostic API uses when possible, and
> use LE/BE specific variant when it is known at build time.

Oh ok, it's me who missed your point then.
For some reason, I thought we were always calling big endian variant.

Thus, your implementation makes totally sense.

Let's see if Thomas meant something different.
Else,
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Pierrick Bouvier Oct. 3, 2024, 9:34 p.m. UTC | #6
On 10/3/24 14:31, Pierrick Bouvier wrote:
> On 10/3/24 13:48, Philippe Mathieu-Daudé wrote:
>> On 3/10/24 18:04, Pierrick Bouvier wrote:
>>> On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
>>>> On 30/9/24 16:32, Thomas Huth wrote:
>>>>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>>>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>>>>> which also swap the value using target endianness.
>>>>>>
>>>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>>>> ---
>>>>>>      hw/xtensa/xtfpga.c | 6 ++----
>>>>>>      1 file changed, 2 insertions(+), 4 deletions(-)
>>>>>>
>>>>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>>>>> index 228f00b045..521fe84b01 100644
>>>>>> --- a/hw/xtensa/xtfpga.c
>>>>>> +++ b/hw/xtensa/xtfpga.c
>>>>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>>>>> *board, MachineState *machine)
>>>>>>                  const size_t boot_sz = TARGET_BIG_ENDIAN ?
>>>>>> sizeof(boot_be)
>>>>>>                                                           :
>>>>>> sizeof(boot_le);
>>>>>>                  uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>>>>>> -            uint32_t entry_pc = tswap32(entry_point);
>>>>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>>>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>>>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>>>>
>>>>> Why don't you simply use stl_p() here?
>>>>
>>>> We want to remove the tswap32() calls...
>>>>
>>>
>>> I think is point is that you could directly use stl_be_p, instead of
>>> stl_endian_p(TARGET_BIT_ENDIAN, ...).
>>
>> TARGET_BIG_ENDIAN is defined as 0 on little endian, and 1 on big one.
>>
>> The following change isn't worth it:
>>
>>      if (TARGET_BIG_ENDIAN) {
>>        stl_be_p(boot + 8, tagptr);
>>      } else {
>>        stl_le_p(boot + 8, tagptr);
>>      }
>>
>> Maybe I'm missing Thomas point, as the xtfpga machines are available
>> for both xtensa-softmmu (LE) and xtensaeb-softmmu (BE).
>>
>>> I don't know if your intent is to make be/le variant "private" and
>>> relies only on endian_p though.
>>
>> My intent is to enforce endian agnostic API uses when possible, and
>> use LE/BE specific variant when it is known at build time.
> 
> Oh ok, it's me who missed your point then.
> For some reason, I thought we were always calling big endian variant.
> 
> Thus, your implementation makes totally sense.
> 
> Let's see if Thomas meant something different.
> Else,
> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

Looking more closely,
stl_p is already correctly defined when you know at compile time your 
target endianness. So Thomas was referring to this.

https://gitlab.com/qemu-project/qemu/-/blame/master/include/exec/cpu-all.h?ref_type=heads#L49
Philippe Mathieu-Daudé Oct. 3, 2024, 9:40 p.m. UTC | #7
On 3/10/24 23:34, Pierrick Bouvier wrote:
> On 10/3/24 14:31, Pierrick Bouvier wrote:
>> On 10/3/24 13:48, Philippe Mathieu-Daudé wrote:
>>> On 3/10/24 18:04, Pierrick Bouvier wrote:
>>>> On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
>>>>> On 30/9/24 16:32, Thomas Huth wrote:
>>>>>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>>>>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>>>>>> which also swap the value using target endianness.
>>>>>>>
>>>>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>>>>> ---
>>>>>>>      hw/xtensa/xtfpga.c | 6 ++----
>>>>>>>      1 file changed, 2 insertions(+), 4 deletions(-)
>>>>>>>
>>>>>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>>>>>> index 228f00b045..521fe84b01 100644
>>>>>>> --- a/hw/xtensa/xtfpga.c
>>>>>>> +++ b/hw/xtensa/xtfpga.c
>>>>>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>>>>>> *board, MachineState *machine)
>>>>>>>                  const size_t boot_sz = TARGET_BIG_ENDIAN ?
>>>>>>> sizeof(boot_be)
>>>>>>>                                                           :
>>>>>>> sizeof(boot_le);
>>>>>>>                  uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : 
>>>>>>> boot_le;
>>>>>>> -            uint32_t entry_pc = tswap32(entry_point);
>>>>>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>>>>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>>>>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>>>>>
>>>>>> Why don't you simply use stl_p() here?
>>>>>
>>>>> We want to remove the tswap32() calls...
>>>>>
>>>>
>>>> I think is point is that you could directly use stl_be_p, instead of
>>>> stl_endian_p(TARGET_BIT_ENDIAN, ...).
>>>
>>> TARGET_BIG_ENDIAN is defined as 0 on little endian, and 1 on big one.
>>>
>>> The following change isn't worth it:
>>>
>>>      if (TARGET_BIG_ENDIAN) {
>>>        stl_be_p(boot + 8, tagptr);
>>>      } else {
>>>        stl_le_p(boot + 8, tagptr);
>>>      }
>>>
>>> Maybe I'm missing Thomas point, as the xtfpga machines are available
>>> for both xtensa-softmmu (LE) and xtensaeb-softmmu (BE).
>>>
>>>> I don't know if your intent is to make be/le variant "private" and
>>>> relies only on endian_p though.
>>>
>>> My intent is to enforce endian agnostic API uses when possible, and
>>> use LE/BE specific variant when it is known at build time.
>>
>> Oh ok, it's me who missed your point then.
>> For some reason, I thought we were always calling big endian variant.
>>
>> Thus, your implementation makes totally sense.
>>
>> Let's see if Thomas meant something different.
>> Else,
>> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> 
> Looking more closely,
> stl_p is already correctly defined when you know at compile time your 
> target endianness. So Thomas was referring to this.
> 
> https://gitlab.com/qemu-project/qemu/-/blame/master/include/exec/cpu-all.h?ref_type=heads#L49

OK I guess I'm seeing Thomas point now; this series cover was not clear
enough. The goal is to remove TARGET_BIG_ENDIAN so we can build half
objects and do a little step toward the single binary.

Maybe I need to kill the stl_p & Co functions first, or do it while
suggesting this target agnostic replacement API.
Thomas Huth Oct. 4, 2024, 6:44 a.m. UTC | #8
On 03/10/2024 23.40, Philippe Mathieu-Daudé wrote:
> On 3/10/24 23:34, Pierrick Bouvier wrote:
>> On 10/3/24 14:31, Pierrick Bouvier wrote:
>>> On 10/3/24 13:48, Philippe Mathieu-Daudé wrote:
>>>> On 3/10/24 18:04, Pierrick Bouvier wrote:
>>>>> On 10/3/24 09:02, Philippe Mathieu-Daudé wrote:
>>>>>> On 30/9/24 16:32, Thomas Huth wrote:
>>>>>>> On 30/09/2024 09.34, Philippe Mathieu-Daudé wrote:
>>>>>>>> Replace a pair of memcpy() + tswap32() by stl_endian_p(),
>>>>>>>> which also swap the value using target endianness.
>>>>>>>>
>>>>>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>>>>>> ---
>>>>>>>>      hw/xtensa/xtfpga.c | 6 ++----
>>>>>>>>      1 file changed, 2 insertions(+), 4 deletions(-)
>>>>>>>>
>>>>>>>> diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
>>>>>>>> index 228f00b045..521fe84b01 100644
>>>>>>>> --- a/hw/xtensa/xtfpga.c
>>>>>>>> +++ b/hw/xtensa/xtfpga.c
>>>>>>>> @@ -438,11 +438,9 @@ static void xtfpga_init(const XtfpgaBoardDesc
>>>>>>>> *board, MachineState *machine)
>>>>>>>>                  const size_t boot_sz = TARGET_BIG_ENDIAN ?
>>>>>>>> sizeof(boot_be)
>>>>>>>>                                                           :
>>>>>>>> sizeof(boot_le);
>>>>>>>>                  uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
>>>>>>>> -            uint32_t entry_pc = tswap32(entry_point);
>>>>>>>> -            uint32_t entry_a2 = tswap32(tagptr);
>>>>>>>> -            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
>>>>>>>> -            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
>>>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
>>>>>>>> +            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
>>>>>>>
>>>>>>> Why don't you simply use stl_p() here?
>>>>>>
>>>>>> We want to remove the tswap32() calls...
>>>>>>
>>>>>
>>>>> I think is point is that you could directly use stl_be_p, instead of
>>>>> stl_endian_p(TARGET_BIT_ENDIAN, ...).
>>>>
>>>> TARGET_BIG_ENDIAN is defined as 0 on little endian, and 1 on big one.
>>>>
>>>> The following change isn't worth it:
>>>>
>>>>      if (TARGET_BIG_ENDIAN) {
>>>>        stl_be_p(boot + 8, tagptr);
>>>>      } else {
>>>>        stl_le_p(boot + 8, tagptr);
>>>>      }
>>>>
>>>> Maybe I'm missing Thomas point, as the xtfpga machines are available
>>>> for both xtensa-softmmu (LE) and xtensaeb-softmmu (BE).
>>>>
>>>>> I don't know if your intent is to make be/le variant "private" and
>>>>> relies only on endian_p though.
>>>>
>>>> My intent is to enforce endian agnostic API uses when possible, and
>>>> use LE/BE specific variant when it is known at build time.
>>>
>>> Oh ok, it's me who missed your point then.
>>> For some reason, I thought we were always calling big endian variant.
>>>
>>> Thus, your implementation makes totally sense.
>>>
>>> Let's see if Thomas meant something different.
>>> Else,
>>> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>
>> Looking more closely,
>> stl_p is already correctly defined when you know at compile time your 
>> target endianness. So Thomas was referring to this.
>>
>> https://gitlab.com/qemu-project/qemu/-/blame/master/include/exec/cpu- 
>> all.h?ref_type=heads#L49
> 
> OK I guess I'm seeing Thomas point now; this series cover was not clear
> enough. The goal is to remove TARGET_BIG_ENDIAN so we can build half
> objects and do a little step toward the single binary.

Ok, that piece of information was missing in the patch description, indeed.
But the tswap function should already work with common code, see the 
target_needs_bswap() stuff in include/exec/tswap.h, so maybe this change 
here even is not needed at all?

  Thomas
Richard Henderson Oct. 4, 2024, 2:08 p.m. UTC | #9
On 10/3/24 23:44, Thomas Huth wrote:
> On 03/10/2024 23.40, Philippe Mathieu-Daudé wrote:
>> OK I guess I'm seeing Thomas point now; this series cover was not clear
>> enough. The goal is to remove TARGET_BIG_ENDIAN so we can build half
>> objects and do a little step toward the single binary.
> 
> Ok, that piece of information was missing in the patch description, indeed.
> But the tswap function should already work with common code, see the target_needs_bswap() 
> stuff in include/exec/tswap.h, so maybe this change here even is not needed at all?

The current practice of target_needs_bswap() merely restricts the scope of 
TARGET_BIG_ENDIAN, allowing devices to be built once.  It still requires TARGET_BIG_ENDIAN 
to exist.

For the goal of single-binary, target_needs_bswap() cannot exist, since there may be no 
one correct answer across the system.  We will need the sort of infrastructure that Phil 
is adding: endianness being chosen by the machine model and/or the instantiated devices.

What is left is deciding on the exact API to use.  The ld/st_endian_p interface with a 
boolean seems reasonable.  It interacts well with TARGET_BIG_ENDIAN as an intermediate 
step, as well as with the virtio legacy cpu mode dependent callback.


r~
diff mbox series

Patch

diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
index 228f00b045..521fe84b01 100644
--- a/hw/xtensa/xtfpga.c
+++ b/hw/xtensa/xtfpga.c
@@ -438,11 +438,9 @@  static void xtfpga_init(const XtfpgaBoardDesc *board, MachineState *machine)
             const size_t boot_sz = TARGET_BIG_ENDIAN ? sizeof(boot_be)
                                                      : sizeof(boot_le);
             uint8_t *boot = TARGET_BIG_ENDIAN ? boot_be : boot_le;
-            uint32_t entry_pc = tswap32(entry_point);
-            uint32_t entry_a2 = tswap32(tagptr);
 
-            memcpy(boot + 4, &entry_pc, sizeof(entry_pc));
-            memcpy(boot + 8, &entry_a2, sizeof(entry_a2));
+            stl_endian_p(TARGET_BIG_ENDIAN, boot + 4, entry_point);
+            stl_endian_p(TARGET_BIG_ENDIAN, boot + 8, tagptr);
             cpu_physical_memory_write(env->pc, boot, boot_sz);
         }
     } else {