From: Konrad Rzeszutek Wilk Subject: Re: memop struct packing, 32/64 bits Date: Sun, 22 Jan 2012 16:37:14 -0400 Message-ID: <20120122203714.GB30288@andromeda.dapyr.net> References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="KsGdsel6WgEHnImy" Return-path: Content-Disposition: inline In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Keir Fraser Cc: ian.jackson@citrix.com, xen-devel@lists.xensource.com, andres@lagarcavilla.org, ian.campbell@citrix.com List-Id: xen-devel@lists.xenproject.org --KsGdsel6WgEHnImy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline > > luck? Am I missing something? > > Where older structs were not 32/64-bit invariant, compat shims were > implemented. See common/compat/memory.c, for example. Well worth avoiding > that! So for the "Fun" of it I tried to see if the 'struct xen_processor_performance' has some 32/64-bit issues and was surprised to find they do. What I am more surprised to find is that nobody seems to have had any troubles with this as it seems to have been there since it was initially implemented. The major issue would have been with the 'shared_type', 'domain_info' and the pointer to the 'states' being at different offsets (So when running a 32-bit dom0 with a 64-bit hypervisor). The attached little C program has the identified issues and the // FIX is my attempt at making the structs of the same size on 32 and 64 bit builds. Right now when built as 32-bit the struct is 96 bytes, while on 64-bit it is 104. Was wondering what is the right fix? The thoughts I had was to either leave them as be and the domain would have to figure out whether the hypervisor is 32-bit or 64-bit and provide the _right_ structure. Or perhaps fix it and provide a "version" hypercall, but that would not be backwards compatible. --KsGdsel6WgEHnImy Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="psd.c" #include #include #include /* Turn this on/off to see how it would look like on 32 or 64 bit */ #define CONFIG_X86_64 #ifdef CONFIG_X86_64 #else #pragma pack(4) #endif #define __DEFINE_GUEST_HANDLE(name, type) \ typedef struct { type *p; } __guest_handle_ ## name #define DEFINE_GUEST_HANDLE_STRUCT(name) \ __DEFINE_GUEST_HANDLE(name, struct name) #define DEFINE_GUEST_HANDLE(name) __DEFINE_GUEST_HANDLE(name, name) #define GUEST_HANDLE(name) __guest_handle_ ## name struct xen_power_register { uint32_t space_id; uint32_t bit_width; uint32_t bit_offset; uint32_t access_size; uint64_t address; }; struct xen_processor_csd { uint32_t domain; /* domain number of one dependent group */ uint32_t coord_type; /* coordination type */ uint32_t num; /* number of processors in same domain */ }; DEFINE_GUEST_HANDLE_STRUCT(xen_processor_csd); struct xen_processor_cx { struct xen_power_register reg; /* GAS for Cx trigger register */ uint8_t type; /* cstate value, c0: 0, c1: 1, ... */ // FIX: Mega pad. No 64/32 bit problems. uint8_t _pad1[3]; uint32_t latency; /* worst latency (ms) to enter/exit this cstate */ uint32_t power; /* average power consumption(mW) */ uint32_t dpcnt; /* number of dependency entries */ GUEST_HANDLE(xen_processor_csd) dp; /* NULL if no dependency */ }; DEFINE_GUEST_HANDLE_STRUCT(xen_processor_cx); struct xen_processor_flags { uint32_t bm_control:1; uint32_t bm_check:1; uint32_t has_cst:1; uint32_t power_setup_done:1; uint32_t bm_rld_set:1; }; struct xen_processor_power { uint32_t count; /* number of C state entries in array below */ struct xen_processor_flags flags; /* global flags of this processor */ GUEST_HANDLE(xen_processor_cx) states; /* supported c states */ }; struct xen_pct_register { uint8_t descriptor; // FIX: Do not memcpy the (acpi_pct_register) as its packing is different */ uint8_t _pad1; /* ARGH */ uint16_t length; uint8_t space_id; uint8_t bit_width; uint8_t bit_offset; uint8_t reserved; uint64_t address; } __attribute__((__packed__)); struct xen_processor_px { uint64_t core_frequency; /* megahertz */ uint64_t power; /* milliWatts */ uint64_t transition_latency; /* microseconds */ uint64_t bus_master_latency; /* microseconds */ uint64_t control; /* control value */ uint64_t status; /* success indicator */ }; DEFINE_GUEST_HANDLE_STRUCT(xen_processor_px); struct xen_psd_package { uint64_t num_entries; uint64_t revision; uint64_t domain; uint64_t coord_type; uint64_t num_processors; } struct xen_processor_performance { uint32_t flags; /* flag for Px sub info type */ uint32_t platform_limit; /* Platform limitation on freq usage */ struct xen_pct_register control_register; struct xen_pct_register status_register; uint32_t state_count; /* total available performance states */ #ifndef CONFIG_X86_64 // FIX uint32_t pad1; #endif GUEST_HANDLE(xen_processor_px) states; struct xen_psd_package domain_info; uint32_t shared_type; /* coordination type of this processor */ #ifndef CONFIG_X86_64 // FIX uint32_t pad2; #endif }; int main(void) { printf("%d==104|96 %d==16 %d==40 %d==16 %d==48\n", sizeof(struct xen_processor_performance), /* 104 when 64, 96 when 32 */ sizeof(struct xen_pct_register), sizeof(struct xen_psd_package), sizeof(struct xen_processor_power), sizeof(struct xen_processor_cx)); printf("%d==0, %d==4, %d==10,%d==26 %d==40\n", offsetof(struct xen_processor_performance, flags), offsetof(struct xen_processor_performance, platform_limit), offsetof(struct xen_processor_performance, control_register.length), offsetof(struct xen_processor_performance, status_register.length), offsetof(struct xen_processor_performance, state_count)); printf("%d==48|44, %d==56|52 %d==96|92\n", offsetof(struct xen_processor_performance, states), offsetof(struct xen_processor_performance, domain_info), offsetof(struct xen_processor_performance, shared_type)); printf("%d==4, %d==8\n", offsetof(struct xen_processor_power, flags), offsetof(struct xen_processor_power, states)); printf("%d==0 %d==0 %d==4 %d==16 %d==24 %d==28 %d==32 %d==36\n", offsetof(struct xen_processor_cx, reg), offsetof(struct xen_processor_cx, reg.space_id), offsetof(struct xen_processor_cx, reg.bit_width), offsetof(struct xen_processor_cx, reg.address), offsetof(struct xen_processor_cx, type), // 16 offsetof(struct xen_processor_cx, latency), // 24 offsetof(struct xen_processor_cx, power), // 28 offsetof(struct xen_processor_cx, dpcnt), //32 offsetof(struct xen_processor_cx, dp)); //36 return 0; } --KsGdsel6WgEHnImy Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --KsGdsel6WgEHnImy--