linux-embedded.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Alignment on XScale ARM
@ 2008-11-07 22:52 Marcus Tangermann
  2008-11-08 14:22 ` Will Newton
  2008-11-08 15:37 ` Nicolas Pitre
  0 siblings, 2 replies; 5+ messages in thread
From: Marcus Tangermann @ 2008-11-07 22:52 UTC (permalink / raw)
  To: linux-embedded

Hi,

we use an XScale 422 (ARM) with a big endian Linux system based on the buildroot environment. It seems there is an alignment problem we have. I've tracked down the problem to the following. 
1. The data received via a netlink communication from the kernel is stored into a buffer buf. The data is correct.
2. The address of the buffer is converted to a pointer to nlmsghdr:
         struct nlmsghdr *h;
         h = (struct nlmsghdr*) buf; 
         printf("IPSECTOOLS: Length  %04d\n", h->nlmsg_len);
         printf("IPSECTOOLS: Type %02d\n", h->nlmsg_type);
   The results of printf are nonsens. The first fields seem to have an offset of 2 bytes, at the end there also seem to be swapped bytes.
3. To test what might happen, I've created an own struct
       struct aligntest{
                __u32 first;
                __u32 int second;
                __u32 third;
                __u32 fourth;
       };
     When you now set a point to a buffer, again the values are wrong:
         s1 = (struct aligntest*) buf;
         printf("1: %08X\n", s1->first);
         printf("2: %08X\n", s1->second);
         printf("3: %08X\n", s1->third);
         printf("4: %08X\n", s1->fourth);

It seems, that there is a problem with the unsigned int values. When you add the attribute "packed" to the definition of struct aligntest ( __attribute__(__packed__) ), all values are displayed correctly. 
So, any hints what the problem can be?

Regards
Marcus
____________________________________________________________________
Psssst! Schon vom neuen WEB.DE MultiMessenger gehört? 
Der kann`s mit allen: http://www.produkte.web.de/messenger/?did=3123

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alignment on XScale ARM
  2008-11-07 22:52 Marcus Tangermann
@ 2008-11-08 14:22 ` Will Newton
  2008-11-08 15:37 ` Nicolas Pitre
  1 sibling, 0 replies; 5+ messages in thread
From: Will Newton @ 2008-11-08 14:22 UTC (permalink / raw)
  To: Marcus Tangermann; +Cc: linux-embedded

On Fri, Nov 7, 2008 at 10:52 PM, Marcus Tangermann
<Marcus.Tangermann@web.de> wrote:
> Hi,
>
> we use an XScale 422 (ARM) with a big endian Linux system based on the buildroot environment. It seems there is an alignment problem we have. I've tracked down the problem to the following.
> 1. The data received via a netlink communication from the kernel is stored into a buffer buf. The data is correct.
> 2. The address of the buffer is converted to a pointer to nlmsghdr:
>         struct nlmsghdr *h;
>         h = (struct nlmsghdr*) buf;
>         printf("IPSECTOOLS: Length  %04d\n", h->nlmsg_len);
>         printf("IPSECTOOLS: Type %02d\n", h->nlmsg_type);
>   The results of printf are nonsens. The first fields seem to have an offset of 2 bytes, at the end there also seem to be swapped bytes.
> 3. To test what might happen, I've created an own struct
>       struct aligntest{
>                __u32 first;
>                __u32 int second;
>                __u32 third;
>                __u32 fourth;
>       };
>     When you now set a point to a buffer, again the values are wrong:
>         s1 = (struct aligntest*) buf;
>         printf("1: %08X\n", s1->first);
>         printf("2: %08X\n", s1->second);
>         printf("3: %08X\n", s1->third);
>         printf("4: %08X\n", s1->fourth);
>
> It seems, that there is a problem with the unsigned int values. When you add the attribute "packed" to the definition of struct aligntest ( __attribute__(__packed__) ), all values are displayed correctly.
> So, any hints what the problem can be?

I'm not sure what the problem is? You can't generally misalign a
struct pointer safely.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alignment on XScale ARM
  2008-11-07 22:52 Marcus Tangermann
  2008-11-08 14:22 ` Will Newton
@ 2008-11-08 15:37 ` Nicolas Pitre
  2008-11-08 18:02   ` David Brownell
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Pitre @ 2008-11-08 15:37 UTC (permalink / raw)
  To: Marcus Tangermann; +Cc: linux-embedded

On Fri, 7 Nov 2008, Marcus Tangermann wrote:

> Hi,
> 
> we use an XScale 422 (ARM) with a big endian Linux system based on the 
> buildroot environment. It seems there is an alignment problem we have. 
> I've tracked down the problem to the following. 1. The data received 
> via a netlink communication from the kernel is stored into a buffer 
> buf. The data is correct. 2. The address of the buffer is converted to 
> a pointer to nlmsghdr:
>          struct nlmsghdr *h;
>          h = (struct nlmsghdr*) buf; 
>          printf("IPSECTOOLS: Length  %04d\n", h->nlmsg_len);
>          printf("IPSECTOOLS: Type %02d\n", h->nlmsg_type);
> The results of printf are nonsens. The first fields seem to have an 
> offset of 2 bytes, at the end there also seem to be swapped bytes. 3. 
> To test what might happen, I've created an own struct
>        struct aligntest{
>                 __u32 first;
>                 __u32 int second;
>                 __u32 third;
>                 __u32 fourth;
>        };
> When you now set a point to a buffer, again the values are wrong:
>          s1 = (struct aligntest*) buf;
>          printf("1: %08X\n", s1->first);
>          printf("2: %08X\n", s1->second);
>          printf("3: %08X\n", s1->third);
>          printf("4: %08X\n", s1->fourth);
> 
> It seems, that there is a problem with the unsigned int values. When 
> you add the attribute "packed" to the definition of struct aligntest ( 
> __attribute__(__packed__) ), all values are displayed correctly. So, 
> any hints what the problem can be?

The ARM processor traditionally cannot access misaligned short and int 
values in memory.  By using the packed attribute you tell the compiler 
that the structure should not have inserted padding to align its 
members, and that accesses are to be performed wth byte accesses and the 
value reconstructed that way.


Nicolas

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alignment on XScale ARM
  2008-11-08 15:37 ` Nicolas Pitre
@ 2008-11-08 18:02   ` David Brownell
  0 siblings, 0 replies; 5+ messages in thread
From: David Brownell @ 2008-11-08 18:02 UTC (permalink / raw)
  To: Nicolas Pitre, Marcus Tangermann; +Cc: linux-embedded

On Saturday 08 November 2008, Nicolas Pitre wrote:
> The ARM processor traditionally cannot access misaligned short and int 
> values in memory.  By using the packed attribute you tell the compiler 
> that the structure should not have inserted padding to align its 
> members, and that accesses are to be performed wth byte accesses and the 
> value reconstructed that way.

... but then when you pass "&misaligned_ptr->member" to some other
function, that other function generally won't know it needs to use
byte access.  So, be careful.  The "packed" struct attribute isn't
a panacea.

General Linux kernel policy is to use <asm/unaligned.h> accessors,
but obviously "packed" has advantages when you've got to pull
structures out of protocol buffers.  The userspace headers don't
seem to support such accessors though.

Also, the network stack is known for making assumptions about its
data being properly aligned.  See how NET_IP_ALIGN is used inside
the kernel; you might be happier aligning your RX buffers before
reading data into them from the kernel.

- Dave

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alignment on XScale ARM
@ 2008-11-08 18:37 Marcus Tangermann
  0 siblings, 0 replies; 5+ messages in thread
From: Marcus Tangermann @ 2008-11-08 18:37 UTC (permalink / raw)
  To: linux-embedded

Hello,

due to your hints I've found a solution. The problem was simple: The buffer was not word aligned as the Xscale requires. 
With
char buffer[8192] __attribute__((aligned(4)));
things are working.  Sometimes you don't see the wood for the trees.
So thanks to all for the good hints!

Marcus

> -----Ursprüngliche Nachricht-----
> Von: "Marcus Tangermann" <Marcus.Tangermann@web.de>
> Gesendet: 07.11.08 23:52:43
> An: linux-embedded@vger.kernel.org
> Betreff: Alignment on XScale ARM

> Hi,
> 
> we use an XScale 422 (ARM) with a big endian Linux system based on the buildroot environment. It seems there is an alignment problem we have. I've tracked down the problem to the following. 
> 1. The data received via a netlink communication from the kernel is stored into a buffer buf. The data is correct.
> 2. The address of the buffer is converted to a pointer to nlmsghdr:
>          struct nlmsghdr *h;
>          h = (struct nlmsghdr*) buf; 
>          printf("IPSECTOOLS: Length  %04d\n", h->nlmsg_len);
>          printf("IPSECTOOLS: Type %02d\n", h->nlmsg_type);
>    The results of printf are nonsens. The first fields seem to have an offset of 2 bytes, at the end there also seem to be swapped bytes.
> 3. To test what might happen, I've created an own struct
>        struct aligntest{
>                 __u32 first;
>                 __u32 int second;
>                 __u32 third;
>                 __u32 fourth;
>        };
>      When you now set a point to a buffer, again the values are wrong:
>          s1 = (struct aligntest*) buf;
>          printf("1: %08X\n", s1->first);
>          printf("2: %08X\n", s1->second);
>          printf("3: %08X\n", s1->third);
>          printf("4: %08X\n", s1->fourth);
> 
> It seems, that there is a problem with the unsigned int values. When you add the attribute "packed" to the definition of struct aligntest ( __attribute__(__packed__) ), all values are displayed correctly. 
> So, any hints what the problem can be?
> 
> Regards
> Marcus
> ____________________________________________________________________
> Psssst! Schon vom neuen WEB.DE MultiMessenger gehört? 
> Der kann`s mit allen: http://www.produkte.web.de/messenger/?did=3123
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-embedded" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


__________________________________________________________________
"Run, Fatboy, Run" sowie "Rails & Ties" kostenlos anschauen!
Blockbuster-Gutscheine sichern unter http://www.blockbuster.web.de

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-11-08 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-08 18:37 Alignment on XScale ARM Marcus Tangermann
  -- strict thread matches above, loose matches on Subject: below --
2008-11-07 22:52 Marcus Tangermann
2008-11-08 14:22 ` Will Newton
2008-11-08 15:37 ` Nicolas Pitre
2008-11-08 18:02   ` David Brownell

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).