Mauro Carvalho Chehab | dc7a12b | 2019-04-14 15:51:10 -0300 | [diff] [blame] | 1 | ================ |
| 2 | Memory alignment |
| 3 | ================ |
| 4 | |
George G. Davis | 462e5a5 | 2019-06-05 16:30:10 -0400 | [diff] [blame] | 5 | Too many problems popped up because of unnoticed misaligned memory access in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | kernel code lately. Therefore the alignment fixup is now unconditionally |
| 7 | configured in for SA11x0 based targets. According to Alan Cox, this is a |
| 8 | bad idea to configure it out, but Russell King has some good reasons for |
| 9 | doing so on some f***ed up ARM architectures like the EBSA110. However |
| 10 | this is not the case on many design I'm aware of, like all SA11x0 based |
| 11 | ones. |
| 12 | |
| 13 | Of course this is a bad idea to rely on the alignment trap to perform |
| 14 | unaligned memory access in general. If those access are predictable, you |
| 15 | are better to use the macros provided by include/asm/unaligned.h. The |
| 16 | alignment trap can fixup misaligned access for the exception cases, but at |
| 17 | a high performance cost. It better be rare. |
| 18 | |
| 19 | Now for user space applications, it is possible to configure the alignment |
| 20 | trap to SIGBUS any code performing unaligned access (good for debugging bad |
| 21 | code), or even fixup the access by software like for kernel code. The later |
| 22 | mode isn't recommended for performance reasons (just think about the |
| 23 | floating point emulation that works about the same way). Fix your code |
| 24 | instead! |
| 25 | |
| 26 | Please note that randomly changing the behaviour without good thought is |
| 27 | real bad - it changes the behaviour of all unaligned instructions in user |
| 28 | space, and might cause programs to fail unexpectedly. |
| 29 | |
| 30 | To change the alignment trap behavior, simply echo a number into |
Nicolas Pitre | 1ada144 | 2008-12-15 03:09:15 +0100 | [diff] [blame] | 31 | /proc/cpu/alignment. The number is made up from various bits: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Mauro Carvalho Chehab | dc7a12b | 2019-04-14 15:51:10 -0300 | [diff] [blame] | 33 | === ======================================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | bit behavior when set |
Mauro Carvalho Chehab | dc7a12b | 2019-04-14 15:51:10 -0300 | [diff] [blame] | 35 | === ======================================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 0 A user process performing an unaligned memory access |
| 37 | will cause the kernel to print a message indicating |
| 38 | process name, pid, pc, instruction, address, and the |
| 39 | fault code. |
| 40 | |
| 41 | 1 The kernel will attempt to fix up the user process |
| 42 | performing the unaligned access. This is of course |
| 43 | slow (think about the floating point emulator) and |
| 44 | not recommended for production use. |
| 45 | |
| 46 | 2 The kernel will send a SIGBUS signal to the user process |
| 47 | performing the unaligned access. |
Mauro Carvalho Chehab | dc7a12b | 2019-04-14 15:51:10 -0300 | [diff] [blame] | 48 | === ======================================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | Note that not all combinations are supported - only values 0 through 5. |
| 51 | (6 and 7 don't make sense). |
| 52 | |
| 53 | For example, the following will turn on the warnings, but without |
Mauro Carvalho Chehab | dc7a12b | 2019-04-14 15:51:10 -0300 | [diff] [blame] | 54 | fixing up or sending SIGBUS signals:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Perr Zhang | 7c2a3e9 | 2017-04-20 12:58:40 +0800 | [diff] [blame] | 56 | echo 1 > /proc/cpu/alignment |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | You can also read the content of the same file to get statistical |
| 59 | information on unaligned access occurrences plus the current mode of |
| 60 | operation for user space code. |
| 61 | |
| 62 | |
| 63 | Nicolas Pitre, Mar 13, 2001. Modified Russell King, Nov 30, 2001. |