xref: /openbsd-src/regress/sys/kern/unalign/unalign.c (revision 78389f1c2826b8ac396ff8e81ca7b8408b1df3ea)
1*78389f1cSmiod /*	$OpenBSD: unalign.c,v 1.2 2008/07/26 10:25:04 miod Exp $	*/
215b2b61eSmiod /* Written by Miod Vallat, 2004 AD -- this file is in the public domain */
315b2b61eSmiod 
415b2b61eSmiod /*
515b2b61eSmiod  * This test checks for the ability, for 32 bit systems, to correctly
615b2b61eSmiod  * access a long long (64 bit) value aligned on a 32 bit boundary, but not
715b2b61eSmiod  * on a 64 bit boundary.
815b2b61eSmiod  *
915b2b61eSmiod  * All architectures should pass this test; on m88k this requires assistance
1015b2b61eSmiod  * from the kernel to recover from the misaligned operand exception: see
1115b2b61eSmiod  * double_reg_fixup() in arch/m88k/m88k/trap.c for details.
1215b2b61eSmiod  */
1315b2b61eSmiod 
1415b2b61eSmiod #include <stdio.h>
1515b2b61eSmiod #include <sys/types.h>
1615b2b61eSmiod 
17*78389f1cSmiod uint32_t array[5];
1815b2b61eSmiod 
19*78389f1cSmiod int
unalign_read(uint64_t * addr)20*78389f1cSmiod unalign_read(uint64_t *addr)
21*78389f1cSmiod {
22*78389f1cSmiod 	uint64_t t;
23*78389f1cSmiod 
24*78389f1cSmiod 	t = *addr;
2515b2b61eSmiod #if BYTE_ORDER == BIG_ENDIAN
2615b2b61eSmiod 	if (t != 0x13579aceffffabcdULL)
2715b2b61eSmiod #else
2815b2b61eSmiod 	if (t != 0xffffabcd13579aceULL)
2915b2b61eSmiod #endif
3015b2b61eSmiod 		return (1);
3115b2b61eSmiod 
32*78389f1cSmiod 	return (0);
33*78389f1cSmiod }
3415b2b61eSmiod 
35*78389f1cSmiod void
unalign_write(uint64_t * addr)36*78389f1cSmiod unalign_write(uint64_t *addr)
37*78389f1cSmiod {
38*78389f1cSmiod 	uint64_t t;
39*78389f1cSmiod 
40*78389f1cSmiod 	t = 0xdeadbeaffadebabeULL;
41*78389f1cSmiod 	*addr = t;
42*78389f1cSmiod }
43*78389f1cSmiod 
44*78389f1cSmiod int
main(int argc,char * argv[])45*78389f1cSmiod main(int argc, char *argv[])
46*78389f1cSmiod {
47*78389f1cSmiod #if !defined(__LP64__)
48*78389f1cSmiod 	uint32_t *addr = array;
49*78389f1cSmiod 
50*78389f1cSmiod 	/* align on a 64 bit boundary */
51*78389f1cSmiod 	if (((uint32_t)addr & 7) != 0)
52*78389f1cSmiod 		addr++;
53*78389f1cSmiod 
54*78389f1cSmiod 	addr[0] = 0x12345678;
55*78389f1cSmiod 	addr[1] = 0x13579ace;
56*78389f1cSmiod 	addr[2] = 0xffffabcd;
57*78389f1cSmiod 	addr[3] = 0x2468fedc;
58*78389f1cSmiod 
59*78389f1cSmiod 	if (unalign_read((uint64_t *)(addr + 1)))
6015b2b61eSmiod 		return (1);
6115b2b61eSmiod 
62*78389f1cSmiod 	unalign_write((uint64_t *)(addr + 1));
63*78389f1cSmiod 
64*78389f1cSmiod #if BYTE_ORDER == BIG_ENDIAN
65*78389f1cSmiod 	if (addr[1] != 0xdeadbeaf || addr[2] != 0xfadebabe)
66*78389f1cSmiod #else
67*78389f1cSmiod 	if (addr[1] != 0xfadebabe || addr[2] != 0xdeadbeaf)
68*78389f1cSmiod #endif
69*78389f1cSmiod 		return (1);
7015b2b61eSmiod #endif	/* __LP64__ */
71*78389f1cSmiod 
7215b2b61eSmiod 	return (0);
7315b2b61eSmiod }
74