qemu-s390x
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [qemu-s390x] [PATCH 2/3] hw/s390x/css: Remove QEMU_PACKED from struc


From: Peter Maydell
Subject: Re: [qemu-s390x] [PATCH 2/3] hw/s390x/css: Remove QEMU_PACKED from struct SenseId
Date: Tue, 25 Sep 2018 17:25:45 +0100

On 25 September 2018 at 17:14, Cornelia Huck <address@hidden> wrote:
> On Tue, 25 Sep 2018 17:20:08 +0200
> Thomas Huth <address@hidden> wrote:
>
>> The uint16_t member cu_type of struct SenseId is not naturally aligned,
>> and since the struct is marked with QEMU_PACKED, this can lead to
>> unaligned memory accesses - which does not work on architectures like
>> Sparc. Thus remove the QEMU_PACKED here and rather copy the struct
>> byte by byte when we do copy_sense_id_to_guest().
>
> Hm... would it look nicer if we chopped up {cu,dev}_type into a hi/lo
> byte and keep the QEMU_PACKED? (The ciws should be ok, I think.)

>> -static void copy_sense_id_to_guest(SenseId *dest, SenseId *src)
>> +static void copy_sense_id_to_guest(uint8_t *dest, SenseId *src)
>>  {
>>      int i;
>>
>> -    dest->reserved = src->reserved;
>> -    dest->cu_type = cpu_to_be16(src->cu_type);
>> -    dest->cu_model = src->cu_model;
>> -    dest->dev_type = cpu_to_be16(src->dev_type);
>> -    dest->dev_model = src->dev_model;
>> -    dest->unused = src->unused;
>> -    for (i = 0; i < ARRAY_SIZE(dest->ciw); i++) {
>> -        dest->ciw[i].type = src->ciw[i].type;
>> -        dest->ciw[i].command = src->ciw[i].command;
>> -        dest->ciw[i].count = cpu_to_be16(src->ciw[i].count);
>> +    dest[0] = src->reserved;
>> +    dest[1] = src->cu_type >> 8;
>> +    dest[2] = src->cu_type & 0xff;
>> +    dest[3] = src->cu_model;
>> +    dest[4] = src->dev_type >> 8;
>> +    dest[5] = src->dev_type & 0xff;
>> +    dest[6] = src->dev_model;
>> +    dest[7] = src->unused;
>> +    for (i = 0; i < ARRAY_SIZE(src->ciw); i++) {
>> +        dest[8 + i * 4] = src->ciw[i].type;
>> +        dest[9 + i * 4] = src->ciw[i].command;
>> +        dest[10 + i * 4] = src->ciw[i].count >> 8;
>> +        dest[11 + i * 4] = src->ciw[i].count & 0xff;
>
> /me gets a headache while trying to understand the endianness here...

Possibly easier to understand written as:
    stw_be_p(dest + 10 + i * 4, src->ciw[i].count);

(There is also an stb_p(ptr, value) if you want all the filling
in of the dest buffer here to look consistent, though it doesn't
do anything more magical than *(uint8_t *)ptr = value.)

thanks
-- PMM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]