xref: /openbmc/linux/Documentation/arch/arm/mem_alignment.rst (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1*e790a4ceSJonathan Corbet================
2*e790a4ceSJonathan CorbetMemory alignment
3*e790a4ceSJonathan Corbet================
4*e790a4ceSJonathan Corbet
5*e790a4ceSJonathan CorbetToo many problems popped up because of unnoticed misaligned memory access in
6*e790a4ceSJonathan Corbetkernel code lately.  Therefore the alignment fixup is now unconditionally
7*e790a4ceSJonathan Corbetconfigured in for SA11x0 based targets.  According to Alan Cox, this is a
8*e790a4ceSJonathan Corbetbad idea to configure it out, but Russell King has some good reasons for
9*e790a4ceSJonathan Corbetdoing so on some f***ed up ARM architectures like the EBSA110.  However
10*e790a4ceSJonathan Corbetthis is not the case on many design I'm aware of, like all SA11x0 based
11*e790a4ceSJonathan Corbetones.
12*e790a4ceSJonathan Corbet
13*e790a4ceSJonathan CorbetOf course this is a bad idea to rely on the alignment trap to perform
14*e790a4ceSJonathan Corbetunaligned memory access in general.  If those access are predictable, you
15*e790a4ceSJonathan Corbetare better to use the macros provided by include/asm/unaligned.h.  The
16*e790a4ceSJonathan Corbetalignment trap can fixup misaligned access for the exception cases, but at
17*e790a4ceSJonathan Corbeta high performance cost.  It better be rare.
18*e790a4ceSJonathan Corbet
19*e790a4ceSJonathan CorbetNow for user space applications, it is possible to configure the alignment
20*e790a4ceSJonathan Corbettrap to SIGBUS any code performing unaligned access (good for debugging bad
21*e790a4ceSJonathan Corbetcode), or even fixup the access by software like for kernel code.  The later
22*e790a4ceSJonathan Corbetmode isn't recommended for performance reasons (just think about the
23*e790a4ceSJonathan Corbetfloating point emulation that works about the same way).  Fix your code
24*e790a4ceSJonathan Corbetinstead!
25*e790a4ceSJonathan Corbet
26*e790a4ceSJonathan CorbetPlease note that randomly changing the behaviour without good thought is
27*e790a4ceSJonathan Corbetreal bad - it changes the behaviour of all unaligned instructions in user
28*e790a4ceSJonathan Corbetspace, and might cause programs to fail unexpectedly.
29*e790a4ceSJonathan Corbet
30*e790a4ceSJonathan CorbetTo change the alignment trap behavior, simply echo a number into
31*e790a4ceSJonathan Corbet/proc/cpu/alignment.  The number is made up from various bits:
32*e790a4ceSJonathan Corbet
33*e790a4ceSJonathan Corbet===		========================================================
34*e790a4ceSJonathan Corbetbit		behavior when set
35*e790a4ceSJonathan Corbet===		========================================================
36*e790a4ceSJonathan Corbet0		A user process performing an unaligned memory access
37*e790a4ceSJonathan Corbet		will cause the kernel to print a message indicating
38*e790a4ceSJonathan Corbet		process name, pid, pc, instruction, address, and the
39*e790a4ceSJonathan Corbet		fault code.
40*e790a4ceSJonathan Corbet
41*e790a4ceSJonathan Corbet1		The kernel will attempt to fix up the user process
42*e790a4ceSJonathan Corbet		performing the unaligned access.  This is of course
43*e790a4ceSJonathan Corbet		slow (think about the floating point emulator) and
44*e790a4ceSJonathan Corbet		not recommended for production use.
45*e790a4ceSJonathan Corbet
46*e790a4ceSJonathan Corbet2		The kernel will send a SIGBUS signal to the user process
47*e790a4ceSJonathan Corbet		performing the unaligned access.
48*e790a4ceSJonathan Corbet===		========================================================
49*e790a4ceSJonathan Corbet
50*e790a4ceSJonathan CorbetNote that not all combinations are supported - only values 0 through 5.
51*e790a4ceSJonathan Corbet(6 and 7 don't make sense).
52*e790a4ceSJonathan Corbet
53*e790a4ceSJonathan CorbetFor example, the following will turn on the warnings, but without
54*e790a4ceSJonathan Corbetfixing up or sending SIGBUS signals::
55*e790a4ceSJonathan Corbet
56*e790a4ceSJonathan Corbet	echo 1 > /proc/cpu/alignment
57*e790a4ceSJonathan Corbet
58*e790a4ceSJonathan CorbetYou can also read the content of the same file to get statistical
59*e790a4ceSJonathan Corbetinformation on unaligned access occurrences plus the current mode of
60*e790a4ceSJonathan Corbetoperation for user space code.
61*e790a4ceSJonathan Corbet
62*e790a4ceSJonathan Corbet
63*e790a4ceSJonathan CorbetNicolas Pitre, Mar 13, 2001.  Modified Russell King, Nov 30, 2001.
64