xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Keir Fraser <keir@xen.org>
To: Konrad Rzeszutek Wilk <konrad@darnok.org>,
	Andres Lagar-Cavilla <andres@lagarcavilla.org>
Cc: ian.jackson@citrix.com, xen-devel@lists.xensource.com,
	ian.campbell@citrix.com
Subject: Re: memop struct packing, 32/64 bits
Date: Thu, 19 Jan 2012 21:07:05 +0000	[thread overview]
Message-ID: <CB3E3679.37CC8%keir@xen.org> (raw)
In-Reply-To: <20120119204941.GB3360@andromeda.dapyr.net>

On 19/01/2012 20:49, "Konrad Rzeszutek Wilk" <konrad@darnok.org> wrote:

> On Thu, Jan 19, 2012 at 12:30:21PM -0800, Andres Lagar-Cavilla wrote:
>> Hi,
>> I had the following painful experience. I declared
>> 
>> struct xen_mem_event_op {
>>     uint8_t       op;           /* XENMEM_*_op_* */
>>     domid_t       domain;
>>     uint64_t buffer;
>>     uint64_t gfn;          /* IN:  gfn of page being operated on */
>> };
>> typedef struct xen_mem_event_op xen_mem_event_op_t;
>> 
>> to be passed as the argument of a memory op called form the toolstack. The
>> hypervisor is 64 bits and the toolstack is 32 bits. My toolstack code
>> simply:
>> 
>>     xen_mem_event_op_t meo;
>> ... set fields ...
>>     return do_memory_op(xch, mode, &meo, sizeof(meo));
>> 
>> No joy because 32 bits was packing the struct differently than 64 bits.
>> Namely, both were adding a 1 byte pad between 'op' and 'domain', but when
>> compiled in 64 bits mode for the hypervisor, an additional 4 byte pad was
>> thrown between 'domain' and 'buffer'.
>> 
> 
>> The first question is, what is the preferred way around this. Declare pads
>> inside the struct?
> 
> Yes. And also use __attribute(__packed__);

Not used in any public header files.

>> Exploring the include/public/memory.h declarations and toolstack code, I
>> see that no current declare includes __attribute__((aligned)) or
>> __attribute__((packed)), or explicit pads.
> 
> Sometimes they have #pragma(4), which will make the alignment be
> 32-bit (4-bytes) for 64-bit builds as well.

Not in any public header files. It's used in the auto-generated 32-on-64
compat headers, for the hypervisor's private use.

>> So how come things don't break more often for 32 bit toolstacks? pure
>> luck? Am I missing something?
> 
> If you do not use 'memcpy' you can escape some of these misues.

Mainly you just have to be careful.

 -- Keir

>> 
>> Thanks!
>> Andres
>> 
>> 
>> _______________________________________________
>> Xen-devel mailing list
>> Xen-devel@lists.xensource.com
>> http://lists.xensource.com/xen-devel
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xensource.com
> http://lists.xensource.com/xen-devel

  reply	other threads:[~2012-01-19 21:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-19 20:30 memop struct packing, 32/64 bits Andres Lagar-Cavilla
2012-01-19 20:49 ` Keir Fraser
2012-01-19 20:57   ` Andres Lagar-Cavilla
2012-01-19 21:11     ` Ian Campbell
2012-01-19 21:21       ` Andres Lagar-Cavilla
2012-01-19 21:23       ` Andres Lagar-Cavilla
2012-01-19 21:56         ` Keir Fraser
2012-01-19 22:00           ` Keir Fraser
2012-01-19 22:04             ` Keir Fraser
2012-01-19 22:05             ` Andres Lagar-Cavilla
2012-01-22 20:37   ` Konrad Rzeszutek Wilk
2012-01-22 20:43     ` Keir Fraser
2012-01-23  9:24     ` Jan Beulich
2012-01-23 15:02       ` Konrad Rzeszutek Wilk
2012-01-19 20:49 ` Konrad Rzeszutek Wilk
2012-01-19 21:07   ` Keir Fraser [this message]
2012-01-19 21:12     ` Ian Campbell
2012-01-19 21:56       ` Keir Fraser
2012-01-20 10:12         ` Jan Beulich
2012-01-20 10:32           ` Keir Fraser

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=CB3E3679.37CC8%keir@xen.org \
    --to=keir@xen.org \
    --cc=andres@lagarcavilla.org \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@citrix.com \
    --cc=konrad@darnok.org \
    --cc=xen-devel@lists.xensource.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).