18c2ecf20Sopenharmony_ci================ 28c2ecf20Sopenharmony_ciMemory alignment 38c2ecf20Sopenharmony_ci================ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciToo many problems popped up because of unnoticed misaligned memory access in 68c2ecf20Sopenharmony_cikernel code lately. Therefore the alignment fixup is now unconditionally 78c2ecf20Sopenharmony_ciconfigured in for SA11x0 based targets. According to Alan Cox, this is a 88c2ecf20Sopenharmony_cibad idea to configure it out, but Russell King has some good reasons for 98c2ecf20Sopenharmony_cidoing so on some f***ed up ARM architectures like the EBSA110. However 108c2ecf20Sopenharmony_cithis is not the case on many design I'm aware of, like all SA11x0 based 118c2ecf20Sopenharmony_ciones. 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciOf course this is a bad idea to rely on the alignment trap to perform 148c2ecf20Sopenharmony_ciunaligned memory access in general. If those access are predictable, you 158c2ecf20Sopenharmony_ciare better to use the macros provided by include/asm/unaligned.h. The 168c2ecf20Sopenharmony_cialignment trap can fixup misaligned access for the exception cases, but at 178c2ecf20Sopenharmony_cia high performance cost. It better be rare. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciNow for user space applications, it is possible to configure the alignment 208c2ecf20Sopenharmony_citrap to SIGBUS any code performing unaligned access (good for debugging bad 218c2ecf20Sopenharmony_cicode), or even fixup the access by software like for kernel code. The later 228c2ecf20Sopenharmony_cimode isn't recommended for performance reasons (just think about the 238c2ecf20Sopenharmony_cifloating point emulation that works about the same way). Fix your code 248c2ecf20Sopenharmony_ciinstead! 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciPlease note that randomly changing the behaviour without good thought is 278c2ecf20Sopenharmony_cireal bad - it changes the behaviour of all unaligned instructions in user 288c2ecf20Sopenharmony_cispace, and might cause programs to fail unexpectedly. 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciTo change the alignment trap behavior, simply echo a number into 318c2ecf20Sopenharmony_ci/proc/cpu/alignment. The number is made up from various bits: 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci=== ======================================================== 348c2ecf20Sopenharmony_cibit behavior when set 358c2ecf20Sopenharmony_ci=== ======================================================== 368c2ecf20Sopenharmony_ci0 A user process performing an unaligned memory access 378c2ecf20Sopenharmony_ci will cause the kernel to print a message indicating 388c2ecf20Sopenharmony_ci process name, pid, pc, instruction, address, and the 398c2ecf20Sopenharmony_ci fault code. 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci1 The kernel will attempt to fix up the user process 428c2ecf20Sopenharmony_ci performing the unaligned access. This is of course 438c2ecf20Sopenharmony_ci slow (think about the floating point emulator) and 448c2ecf20Sopenharmony_ci not recommended for production use. 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci2 The kernel will send a SIGBUS signal to the user process 478c2ecf20Sopenharmony_ci performing the unaligned access. 488c2ecf20Sopenharmony_ci=== ======================================================== 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ciNote that not all combinations are supported - only values 0 through 5. 518c2ecf20Sopenharmony_ci(6 and 7 don't make sense). 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciFor example, the following will turn on the warnings, but without 548c2ecf20Sopenharmony_cifixing up or sending SIGBUS signals:: 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci echo 1 > /proc/cpu/alignment 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ciYou can also read the content of the same file to get statistical 598c2ecf20Sopenharmony_ciinformation on unaligned access occurrences plus the current mode of 608c2ecf20Sopenharmony_cioperation for user space code. 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciNicolas Pitre, Mar 13, 2001. Modified Russell King, Nov 30, 2001. 64