0

Since (from what I have read) ARM9 platform may fail to correctly load data at an unaligned memory address, let's assume unaligned meaning that the address value is not multiple of 2 (i.e. not aligned on 16-bit), then how would one access say, fourth character on a string of characters pointed to by a properly aligned pointer?

char buf[] = "Hello world.";

buf[3]; // (buf + 3) is unaligned here,  is it not?

Does compiler generate extra code, as opposed to the case when buf + 3 is properly aligned? Or will the last statement in the example above have undesired results at runtime - yielding something else than the fourth character, the second l in Hello?

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
  • 1
    I have never heard such a thing about ARM9 - where did you read that? – Carl Norum Jun 29 '13 at 17:42
  • 1
    Byte accesses are always aligned, naturally. Hence a UsageFault will never occur. – Wiz Jun 29 '13 at 17:42
  • I can't find the link that tipped me off originally. I hope I did not make the wrong conclusion. My point is, will the above always work, can I trust it? What if I try to "unpack" an element in a data stream using the following macro `#define unpacked(type, ptr, offset) *((type*)((uint8_t*)ptr + offset))` - will that work on any system, provided each element in the stream is in host byte order (native endianness)? – Armen Michaeli Jun 29 '13 at 17:48
  • Found where I picked the ARM9 behavior from - the answer by gentleman named Potzon mentions it - http://stackoverflow.com/questions/1496848/does-unaligned-memory-access-always-cause-bus-errors – Armen Michaeli Jun 29 '13 at 17:50

3 Answers3

4

Byte accesses don't have to be aligned. The compiler will generate a ldrb instruction, which does not need any sort of alignment.

If you're curious as to why, this is because ARM will load the entire aligned word that contains the target byte, and then simply select that byte out of the four it just loaded.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
  • Thank you. I wonder, how would this relate to the answer provided by Potzon on this page http://stackoverflow.com/questions/1496848/does-unaligned-memory-access-always-cause-bus-errors - he does something similar to what I did in my example, namely dereferencing unaligned address. – Armen Michaeli Jun 29 '13 at 17:54
  • Assuming there's a way to disable the segfault behavior, yes. – Drew McGowen Jun 29 '13 at 17:56
  • I don't understand, what do you mean by 'yes'? Sorry for being thick here. In general, how do systems that don't allow unaligned addresses, access string characters arbitrarily? Do the compilers do the dirty work? – Armen Michaeli Jun 29 '13 at 18:02
  • 1
    Oh sorry, I thought you were asking DOES it relate. Looking back at the answer he provided, notice how despite the fact that he's effectively accessing 4 bytes starting at offset 1, it still reads the first 4 bytes? And notice how they're not in the expected order? That's because the byte offset 1 translates to a word offset 0 (1/4=0 remainder 1), so the first word (first 4 bytes) are accessed. The remainder of 1 from above is used to select the correct byte. However, since he's doing a word access, it turns into a rotate of 1 byte, aka 8 bits (I guess that's just how the ARM mem system works) – Drew McGowen Jun 29 '13 at 18:16
  • 1
    newer arms allow unaligned access. Older arms like the arm7 and arm9 would rotate the data within the word. But this is all for accesses larger than a byte, you cant address smaller than a byte so you cant have an unaligned byte access, only halfwords and words. On all the arm platforms you can enable or disable exceptions on an unaligned access, the different cores/implementations power up with it on or off. – old_timer Jun 29 '13 at 18:21
  • @dwelch I thought it can be problematic to access a single byte at an unaligned address? - some older ARM systems only allowed 16-bit aligned addressing - that would make for a problem accessing every odd byte, wouldn't it? I am much less interested in exceptions etc - I am interested in portability from C standpoint - how would one approach memory accessing in a C program, knowing it has to compile and function on an ARM platform (or other more esoteric systems with a C compiler). The answer to the question I linked in my comment earlier, raises my eyebrow, at least. – Armen Michaeli Jun 30 '13 at 10:32
  • From the arm4t to the present you CAN do unaligned accesses, the behavior is deterministic (the ARM ARM wanted you to believe otherwise) but the behavior changed around armv6. Also which cores had an exception enabled by default vs ones that didnt changed over time as well. You need to discard whatever information is leading to you believe that you cannot address odd numbered bytes or whatever,that is bogus information. On systems where the address is a byte based address (most/all in use today) there is no such thing as an unaligned byte transaction. arm or other. – old_timer Jun 30 '13 at 14:33
  • Ok, thanks, you offer very extensive information to me, which I will have to take some time to really digest. On a side note, and related to all of this - would you know where I can dig up whether any C standard prohibits the "rotated" result when accessing unaligned data? I mean, does it define result of `*((int*)(buf + 3))`? – Armen Michaeli Jun 30 '13 at 16:13
2

The concept to remember is that the compiler will try to optimize access based on the type in order to get the most efficiency of your processor. So when accessing ints, it'll want to use things like the ldr instruction which will fault if it's an unaligned access. For something link a char access, the compiler will work some of the details for you. Where you have to be concerned are things like:

  • Casting pointers. If you cast a char * to an int * and the pointer is not aligned correctly, you'll get an alignment trap. In general, it's okay to cast down (from an int to a char), but not the other way around. You would not want to do this:

    char buf[] = "12345678";
    int *p = &buf[1];
    printf("0x%08X\n", *p);  // *p is badness here!
    
  • Trying to pull data off the wire with structures. I've seen this done a lot, and it's just plain bad practice. Endianess issues aside, you can cause an alignment trap if the elements aren't aligned correctly for the platform.

FWIW, casting pointers is probably the number one issue I've seen in practice.

There's a great book called Write Portable Code which goes over quite a few details about writing code for multiple platforms. The sample chapter on the linked site actually contains a section talking about alignment.

There's a little more that's going on too. Memory functions, like malloc, also give you back aligned blocks (generally on a double-word boundary) so that you can write in data and not hit an alignment fault.

One last bit, while newer ARMs can cope with unaligned accesses better, that does not mean they're performant. It just means they're tolerant. The same can be said for the X86 processors too. They'll do the unaligned access, but you're forcing extra memory fetches by doing so.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
1

Most systems use byte based addressing. The address 0x1234 is in terms of bytes for example. Assume that I mean 8 bit bytes for this answer.

The definition of unaligned as to do with the size of the transfer. A 32 bit transfer for example is 4 bytes. 4 is 2 to the power 2 so if the lower 2 bits of the address are anything other than zeros then that address is an unaligned 32 bit transfer.

So using a table like this or just understanding powers of 2

8   1  0 []
16  2  1 [0]
32  4  2 [1:0]
64  8  3 [2:0]
128 16 4 [3:0]

the first column is the number of bits in the transfer. the second is the number of bytes that represents, the third is the number of bits at the bottom of the address that have to be zero to make it an aligned transfer, and the last column describes those bits.

It is not possible to have an unaligned 8 bit transfer. Not on arm, not on any system. Please understand that.

16 bit transfers. Once we get into transfers larger than 16 bits then you can START to talk about being unaligned. Then problem with unaligned transfers has to do with the number of bus cycles. Say you are doing 16 bit transfers on a system with a 16 bit wide bus and 16 bit wide memories. That means that we have items at memory at these addresses for example, address on left, data on right:

0x0100 : 0x1234
0x0102 : 0x5678

If you want to do a 16 bit transfer that is aligned the lsbit of your address must be zero, 0x100, 0x102, 0x104, etc. Unaligned transfers would be at addresses with the lsbit set, 0x101, 0x103, 0x105, etc. Why are they a problem? In this hypothetical (there were and are still real systems like this) system in order to get two bytes at address 0x0100 we only need to access the memory one time and take all 16 bits from that one address resulting in 0x1234. But if we want 16 bits starting at address 0x0101. We have to do two memory transactions 0x0100 and 0x0102 and take one byte from each combine those to get the result which little endian is 0x7812. That takes more clock cycles, more logic, etc. Inefficient and costly. Intel x86 and other systems from that era which were 8 or 16 bit processors but used 8 bit memory, everything larger than an 8 bit transfer was multiple clock cycles, instructions themselves took multiple clock cycles to execute before the next one could start, burning clock cycles and complication in the logic was not of interest (they saved themselves from pain in other ways).

The older arms may or may not have been from that era, but post acorn, the armv4 to the present is a 32 bit system from a perspective of the size of the general purpose registers, the data bus is 32 or 64 bits (the newest arms have 64 bit registers and I would assume if not already 128 bit busses) depending on your system. The core that put ARM on the map the ARM7TDMI which is an ARMv4T, I assume is a 32 bit data bus. The ARM7 and ARM9 ARM ARM (ARM Architectural Reference Manual) changed its language on each revision (I have several revisions going back to the paper only ones) with respect to words like UNPREDICTABLE RESULTS. When and where they would list something as bad or broken. Some of this was legal, understand ARM does not make chips, they sell IP, back then it was masks for a particular foundry today you get the source code to their core and you deal with it. So to survive you need a good legal defense, your secrets are exposed to customers, some of these items that were claimed not to be supported actually have deterministic results, if ARM were to find a clone (which is yet another legal discussion) with these unpredictable results being predictable and matching what arms logic does you have to be pretty good at explaining why. The clones have been crushed when they have tried (that or legally become licensed arm cores) so some of this is just interesting history. Another arm manual described quite clearly what happens when you do an unaligned transfer on the older ARM7 systems. And it is a bit of a duh moment when you see it, quite obvious what was going on (just plain keep it simple stupid logic).

The byte lanes rotated. On a 32 bit bus somewhere in the system, likely not on the amba/axi bus but inside the memory controller you would effectively get this:

0x0100 : 0x12345678
0x0101 : 0x78123456
0x0102 : 0x56781234
0x0103 : 0x34567812

address on the left resulting data on the right. Now why is that obvious you ask and what is the size of that transfer? The size of the transfer is irrelevant, doesnt matter, look at that address/data this way:

0x0100 : 0x12345678
0x0101 : 0xxx123456
0x0102 : 0xxxxx1234
0x0103 : 0xxxxxxx12

Using aligned transfers, 0x0100 is legal for 32, 16, and 8 bit and look at the lower 8, 16, or 32 bits you get the right answer with the data as shown. For address 0x0101 only an 8 bit transfer is legal, and the lower 8 bits of that data is in the lower 8 bits, just copy those over to the registers lower 8 bits. for address 0x0102 8 and 16 are legal, unaligned, transfers and 0x1234 is the right answer for 16 bit and 0x34 for 8. lastly 0x0103 8 bit is the only transfer size without alignment issues and 0x12 is the right answer.

This above information is all from publicly available documents, no secrets here or special insider knowledge, just generic programming experience.

ARM put an exception in, data abort or prefetch abort (thumb is a separate topic) to discourage the use of unaligned transfers as do other architectures. Unfortunately x86 has lead people to be very lazy and also not care about the performance hit that they incur when doing such a thing on an x86, which allows the transfer at the price of extra cycles and extra logic. The prefetch abort if I remember was not on by default on the ARM7 platforms I used, but was on by default on the ARM9 platforms I used, my memory could be wrong and since I dont know how the defaults worked that could have been a strap option on the core so it could have varied from chip to chip, vendor to vendor. You could disable it and do unaligned transfers so long as you understood what happened with the data (rotate not spill over into the next word).

More modern ARM processors do support unaligned transfers and they are as one would expect, I wont use 64 bit examples here to save typing and space but go back to that 16 bit example to paint the picture

0x0100: 0x1234
0x0102: 0x5678

With a 16 bit wide system, memory and bus, little endian, if you did a 16 bit unaligned transfer at address 0x0101 you would expect to see 0x7812 and that is what you get now on the modern arm systems. But it is still a software controlled feature, you can enable exceptions on unaligned transfers and you will get a data abort instead of a completed transfer.

As far as your question goes look at the ldrb instruction, that instruction does an 8 bit read from memory, being 8 bit there is no such thing as unaligned all addresses are valid, if buf[] happened to live at address 0x1234 then buf[3] is at address 0x1237 and that is a perfectly valid address for an 8 bit read. No alignment issues of any kind, no exceptions will fire. Where you would get into trouble is if you do one of these very ugly programming hacks:

char buf[]="hello world";
short *sptr;
int *iptr;

    sptr=(short *)&buf[3];
    iptr=(int *)&buf[3];
    ...
    something=*sptr;
    something=*iptr;
    ...
    short_something=*(short *)&buf[3];
    int_something=*(int *)&buf[3];

And then yes you would need to worry about unaligned transfers as well as hoping that you dont have any compiler optimization issues making the code not work as you had thought it would. +1 to jszakmeister for already covering this sub topic.

short answer:

char buf[]="hello world";

char is generally assumed to mean an 8 bit byte so this is a quantity of 8 bit items. certainly compiled for ARM that is what you will get (or mips or x86 or power pc, etc). So accessing buf[X] for any X within that string, cannot be unaligned because

something = buf[X];

Is an 8 bit transfer and you cant have unaligned 8 bit transfers. If you were to do this

short buf[]={1,2,1,2,3,2,1};

short is assumed but not always the case, to be 16, bit, for the arm compilers I know it is 16 bit. but that doesnt matter buf[X] here also cannot be unaligned because the compiler computes the offset for you. As follows address of buf[X] is base_address_of_buf + (X<<1). And the compiler and/or linker will insure, on ARM, MIPS, and other systems that buf is placed on a 16 bit aligned address so that math will always result in an aligned address.

old_timer
  • 69,149
  • 8
  • 89
  • 168