Linksys WRT54G experiments - unaligned data access
Published on: 2005-12-26
Linksys WRT54G experiments - unaligned data access
2005-12-26T22:34:00
Read my old entry on the Linux based WRT54G here. IBM DeveloperWorks has an interesting article about data alignment here The article says that MIPS h/w doesn't support unaligned data access. I wrote a program to test this out:
#include <sys/sysmips.h> #include <signal.h> void handler(int n) { printf("caught %d\n", n); exit(1); } main() { int a[100]; int *p; signal(SIGBUS, handler); p = (char*)a + 1; *p = 1; printf("after unaligned access ...\n"); }No problem at all ... the print statement after `*p = 1' seems to work perfectly! A few moments spent digging around the kernel source identified the reason - it seems the kernel catches the exception which the hardware generates and `emulates' the unaligned access in software (arch/mips/kernel/unaligned.c)! This `emulation' can be turned off by calling: sysmips(MIPS_FIXADE, 0); It may be instructive to demonstrate the necessity for structure padding by turning off padding (use -fpack-struct in gcc) and generating unaligned accesses. The effect on MIPS is much more dramatic than on x86 CPU's which can perform unaligned access.
Eric Smith
Fri Nov 13 08:36:01 2009
MIPS also has unaligned load/store instructions (which were patented, but the patent has now expired). These instructions are useful when you know that an access will be unaligned; it takes two instructions to do the access, but it won't trap. I doubt that the C compiler is smart enough to ever infer that they should be used.