All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Vivier <laurent@vivier.eu>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Riku Voipio <riku.voipio@iki.fi>,
	QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH] linux-user: manage SOCK_PACKET socket type.
Date: Tue, 27 Oct 2015 12:54:29 +0100	[thread overview]
Message-ID: <562F65F5.7040106@vivier.eu> (raw)
In-Reply-To: <CAFEAcA-CyEoLC_C4yDE9-Sh29oL9x+EoxSHn5A3Lv1-qgZQoVg@mail.gmail.com>



Le 27/10/2015 12:35, Peter Maydell a écrit :
> On 27 October 2015 at 10:47, Laurent Vivier <laurent@vivier.eu> wrote:
>> And for the socketcall part, we need the tswap16():
>>
>> for instance,
>>
>>     int a = htons(0x0003);
>>
>> On a LE host:
>>
>>     a = 0x00000300
>>
>> On a BE host:
>>
>>     a = 0x00000003
>>
>> If the guest is BE, it will put in memory:
>>
>>     0x00 0x00 0x00 0x03
>>
>> Then a LE host, will read:
>>
>>     int b = 0x03000000
>>
>> but get_user_ual() in do_socketcall() will byte-swap it and put
>> 0x00000003 in a[2].
>>
>> so without the byte-swap, we call do_socket(..., 0x0003),
>> whereas the syscall is waiting for htons(0x0003) -> 0x0300 as we are on
>> LE host.
> 
> So, I thought through this this morning, and I think the swapping
> issues here are not specific to socketcall. If the socket syscall
> ABI requires an argument of "htons(3)", then this is actually
> a *different* ABI for BE vs LE systems. On a BE system this is
> asking for "3", but on LE it is asking for "0x300". (Argument
> is generally passed in a register.) So we need to be able to tell
> when the host kernel wants this sort of difference and fix it up.
> 
> For socketcall, the current swapping we have will correctly pass
> the value the user wrote into the array-of-longs into the syscall,
> because if the value to be passed is 0x11223344 (assume 32-bit long),
> for BE guest LE host we have:
>  in register 0x11223344
>  in memory 0x11 0x22 0x33 0x44
>  byteswapped back by get_user_ual: 0x11223344
> and for LE guest LE host:
>  in register 0x11223344
>  in memory 0x44 0x33 0x22 0x11
>  read back by get_user_ual: 0x11223344
> But we still have the same issue that if the guest believes the
> kernel wants a value of 0x3 but in fact it wants 0x300 we need to
> fix things up.
> 
> So the fix needs to go into do_socket(), and it needs to be
> specific to the PF*/SOCK* values that indicate socket types
> that want a network-order-16-bit value, which I think is
>  (domain == AF_PACKET || (domain == AF_INET && type == SOCK_PACKET))

OK, I will try with my use case.

> 
> (this is pretty close to what your patch had to start with,
> so apologies for taking a while to work through it. Endianness
> always confuses me...)

No problem, It tooks me 3 years to explain that correctly :) ...

> Still thinking about the other part of your patch, because
> "does this start with 'eth'" is not very pretty...

I agree but I didn't find a better way...

Laurent

  parent reply	other threads:[~2015-10-27 11:54 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-06 17:11 [Qemu-devel] [PATCH] linux-user: manage SOCK_PACKET socket type Laurent Vivier
2015-10-26 14:40 ` Peter Maydell
2015-10-27  3:09   ` Laurent Vivier
2015-10-27 10:47     ` Laurent Vivier
2015-10-27 11:35       ` Peter Maydell
2015-10-27 11:39         ` Peter Maydell
2015-10-27 11:49           ` Laurent Vivier
2015-10-27 11:52             ` Peter Maydell
2015-10-27 11:56               ` Laurent Vivier
2015-10-27 11:54         ` Laurent Vivier [this message]
2015-10-27 11:50     ` Peter Maydell
2015-10-27 11:54       ` Laurent Vivier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=562F65F5.7040106@vivier.eu \
    --to=laurent@vivier.eu \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=riku.voipio@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.