32 bit alignment
Hi,
I have a strange problem with the U-Boot network code. This is how some piece of memory (network buffer) looks like:
0005 5ddd 81aa 0005 5df6 026c 0800 4510
014c 0000 0000 1011 de4a d5fc 8fa5 d5fc
8fa8 0043 0044 0138 76b9 0201 0600 5de8
^^^^
283b 0000 0000 0000 0000 d5fc 8fa8 d5fc
8fa5 0000 0000 0005 5ddd 81aa 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
The DHCP payload starts at the marked address, which is not 32 bit aligned. Now I want to access the 32 bit value at the marked address and the following one by using a pointer (u32*)bp which points to the marked address. The result is that I get the value from the address _before_ the marked address (which belongs to the same 32 bit aligned dword). This makes the whole network code break which assumes that it can lay arbitrary data structures over the piece of memory.
Any idea what I'm missing here?
Robert
Signature · 5 lines
Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
Pengutronix - Linux Solutions for Science and Industry
Braunschweiger Str. 79, 31134 Hildesheim, Germany
Handelsregister: Amtsgericht Hildesheim, HRA 2686
Phone: +49-5121-28619-0 | Fax: +49-5121-28619-4Quoted 7 lines · Robert Schwebel
The DHCP payload starts at the marked address, which is not 32 bit aligned. Now I want to access the 32 bit value at the marked address and the following one by using a pointer (u32*)bp which points to the marked address. The result is that I get the value from the address _before_ the marked address (which belongs to the same 32 bit aligned dword). This makes the whole network code break which assumes that it can lay arbitrary data structures over the piece of memory.
This is standard behaviour on ARMs, if the alignment exception is configured to be ignored. The EA is internally simply anded with ~3. There are 2 ways to handle this:
1) Implement an alignment exception handler (have fun)
2) Don't access unaligned data. Use byte accesses instead.
Regards,
Marius
-----------------------------------------------------------------------------
Marius Groeger SYSGO Real-Time Solutions AG mgroeger at sysgo.de Software Engineering Embedded and Real-Time Software www.sysgo.de Voice: +49-6136-9948-0 Am Pfaffenstein 14 www.osek.de FAX: +49-6136-9948-10 55270 Klein-Winternheim, Germany www.elinos.com
Quoted 4 lines · Marius Groeger
There are 2 ways to handle this:
1) Implement an alignment exception handler (have fun)
2) Don't access unaligned data. Use byte accesses instead.
The problem is that the whole network code in uboot depends on this; the IP packet is copied into some memory and a struct IP_t layed over it, pointing to the right address.
Robert
Signature · 5 lines
Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
Pengutronix - Linux Solutions for Science and Industry
Braunschweiger Str. 79, 31134 Hildesheim, Germany
Handelsregister: Amtsgericht Hildesheim, HRA 2686
Phone: +49-5121-28619-0 | Fax: +49-5121-28619-4Quoted 7 lines · In message <[email protected]> you wrote:
Quoted 3 lines
1) Implement an alignment exception handler (have fun)
2) Don't access unaligned data. Use byte accesses instead.The problem is that the whole network code in uboot depends on this; the IP packet is copied into some memory and a struct
IP_tlayed over it, pointing to the right address.
Please point out a specific place in the code where (you think) this happens. We encountered the problem before on other ARM systems, and Robert Kaiser fixed most of these places. We found some more, but now the code is working fine on the ARM systems I have here.
I guess you are using something that we did not test yet or not thoroughly enough. But I cannot accept your statement that "whole network code" breaks. It runs fine here ;-)
Best regards,
Wolfgang Denk
Signature · 4 lines
Software Engineering: Embedded and Realtime Systems, Embedded Linux Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd at denx.de You can fool some of the people all of the time, and You can fool all of the people some of the time, but You can't fool mom.
Hi Wolfgang,
Quoted 8 lines · Wolfgang Denk
Please point out a specific place in the code where (you think) this happens. We encountered the problem before on other ARM systems, and Robert Kaiser fixed most of these places. We found some more, but now the code is working fine on the ARM systems I have here.
I guess you are using something that we did not test yet or not thoroughly enough. But I cannot accept your statement that "whole network code" breaks. It runs fine here ;-)
Hmm, perhaps I'm missing something, isn't too improbable ;-) Here's what I've found out until now:
- The board is a PXA250 based custom board with a SMSC91111 LAN chip
- I tried to use the DHCP command. Switched it on in the configuration, configured the driver to find the correct base address etc.
- When I start the "dhcp" command I see this on the server:
----------8<----------8<----------8<----------
DHCPDISCOVER from 00:05:5d:dd:81:aa via eth0
DHCPOFFER on 213.252.143.168 to 00:05:5d:dd:81:aa via eth0
DHCPREQUEST for 213.252.0.0 (213.252.143.165) from 00:05:5d:dd:81:aa via eth0: wrong network. DHCPNAK on 213.252.0.0 to 00:05:5d:dd:81:aa via eth0 ----------8<----------8<----------8<----------
- "no network" is correct seen from the server, as the DHCPREQUEST is for 213.252.0.0 which is incorrect. 213.252.143.168 would be correct.
- I tracked this down by switching on several DEBUG defines in net/bootp.c, drivers/smsc91111.c and net/net.c.
- Result: this is how the DHCPOFFER packet looks like, taken from the smc91111.c:print_packet() output:
----------8<----------8<----------8<----------
Receiving Packet
Packet of length 348
0005 5ddd 81aa 0005 5df6 026c 0800 4510
^^1^
014c 0000 0000 1011 de4a d5fc 8fa5 d5fc
8fa8 0043 0044 0138 76b9 0201 0600 5de8
283b 0000 0000 0000 0000 d5fc 8fa8 d5fc
^^^^^^^2^ ^^^^ 8fa5 0000 0000 0005 5ddd 81aa 0000 0000
^^3^
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 6382 5363 3501 0236 04d5
fc8f a533 0400 001c 2001 04ff ffff f003
04d5 fc8f a111 222f 686f 6d65 2f65 6d62
6564 6465 642f 7074 7864 6973 742d 302e
332e 3135 2f72 6f6f 74ff 7440
----------8<----------8<----------8<----------
^^1^: start of IP header
^^2^: Your IP Address
^^3^: Server IP Address
- Further analysis showed that when u-boot prepares the DHCPREQUEST packet which finally fails it takes the "Your IP Address" from the packet shown below. This is done by using a pointer to the DHCP payload (bp* in net/bootp.c:DhcpHandler()).
- bp->bp_yiaddr does now point to the beginning of the ^^^^2^ marked block, starting with "d5". Unfortunately, the 32 bit aligned longword at this address is not equivalent to the "d5fc 8fa8" part, but to the "0000 d5fc" sequence (using the 16 bits _before_ the place the pointer points to).
- The result is that the next packet (DHCPREQUEST) asks for an address which ends in .0.0.
Robert
Signature · 5 lines
Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
Pengutronix - Linux Solutions for Science and Industry
Braunschweiger Str. 79, 31134 Hildesheim, Germany
Handelsregister: Amtsgericht Hildesheim, HRA 2686
Phone: +49-5121-28619-0 | Fax: +49-5121-28619-4Quoted 6 lines · In message <[email protected]> you wrote:
Quoted 5 lines
address. The result is that I get the value from the address _before_ the marked address (which belongs to the same 32 bit aligned dword). This makes the whole network code break which assumes that it can lay arbitrary data structures over the piece of memory.
Quoted 6 lines
This is standard behaviour on ARMs, if the alignment exception is configured to be ignored. The EA is internally simply anded with ~3. There are 2 ways to handle this:
1) Implement an alignment exception handler (have fun)
2) Don't access unaligned data. Use byte accesses instead.
And 2) is what's being used in U-Boot so far. Obviously Robert stumbles over a piece of code that escaped me so far...
Best regards,
Wolfgang Denk
Signature · 5 lines
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd at denx.de
Command, n.:
Statement presented by a human and accepted by a computer
in such a manner as to make the human feel as if he is in control.Quoted 8 lines · In message <[email protected]> you wrote:
The DHCP payload starts at the marked address, which is not 32 bit aligned. Now I want to access the 32 bit value at the marked address and the following one by using a pointer (u32*)bp which points to the marked address. The result is that I get the value from the address _before_ the marked address (which belongs to the same 32 bit aligned dword). This makes the whole network code break which assumes that it can lay arbitrary data structures over the piece of memory.
Which exact piece of code (file name, source line number) are you talking about?
Best regards,
Wolfgang Denk
Signature · 5 lines
Software Engineering: Embedded and Realtime Systems, Embedded Linux Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd at denx.de Es sind ?berhaupt nur die Dummk?pfe, die sich den Befehlen der M?ch- tigen widersetzen. Um sie zu ruinieren ist es genug, ihre Befehle treu zu erf?llen. - Peter Hacks: "Die sch?ne Helena"
Quoted 2 lines · Wolfgang Denk
Which exact piece of code (file name, source line number) are you talking about?
It's the network code:
net/bootp.c
net/net.c
drivers/smc91111.c
Robert
Signature · 5 lines
Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
Pengutronix - Linux Solutions for Science and Industry
Braunschweiger Str. 79, 31134 Hildesheim, Germany
Handelsregister: Amtsgericht Hildesheim, HRA 2686
Phone: +49-5121-28619-0 | Fax: +49-5121-28619-4Quoted 9 lines · In message <[email protected]> you wrote:
Quoted 3 lines
Which exact piece of code (file name, source line number) are you talking about?
It's the network code:
net/bootp.c
net/net.c
drivers/smc91111.c
Grrrrgh... This is 3000+ lines of code. Can you please be a _bit_ more specific? And please exclude the smc91111 driver for now, since I don't feel responsible for it.
Best regards,
Wolfgang Denk
Signature · 3 lines
Software Engineering: Embedded and Realtime Systems, Embedded Linux Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd at denx.de How does a project get to be a year late? ... One day at a time.
Quoted 6 lines · Wolfgang Denk
Quoted 3 lines
net/bootp.c
net/net.c
drivers/smc91111.cGrrrrgh... This is 3000+ lines of code. Can you please be a _bit_ more specific?
See my other post, I hope it can clarify things a little bit. The problem is that the whole thing is one large state machine...
Quoted 2 lines
And please exclude the smc91111 driver for now, since I don't feel responsible for it.
It basically works - the problems appear later when the contents of the ethernet packet are in memory.
By the way: I'm sometimes see strange memory reservation errors when the code is trying to get mem from the SMSC MMU. It can be fixed by adding random debug("foo, bar, baz\n"); function calls, but you have to add exactly the right number of calls :-) Has somebody else seen this behaviour?
Robert
Signature · 5 lines
Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
Pengutronix - Linux Solutions for Science and Industry
Braunschweiger Str. 79, 31134 Hildesheim, Germany
Handelsregister: Amtsgericht Hildesheim, HRA 2686
Phone: +49-5121-28619-0 | Fax: +49-5121-28619-4