xref: /openbsd/regress/sys/kern/unalign/unalign.c (revision 7b36286a)
1 /*	$OpenBSD: unalign.c,v 1.2 2008/07/26 10:25:04 miod Exp $	*/
2 /* Written by Miod Vallat, 2004 AD -- this file is in the public domain */
3 
4 /*
5  * This test checks for the ability, for 32 bit systems, to correctly
6  * access a long long (64 bit) value aligned on a 32 bit boundary, but not
7  * on a 64 bit boundary.
8  *
9  * All architectures should pass this test; on m88k this requires assistance
10  * from the kernel to recover from the misaligned operand exception: see
11  * double_reg_fixup() in arch/m88k/m88k/trap.c for details.
12  */
13 
14 #include <stdio.h>
15 #include <sys/types.h>
16 
17 uint32_t array[5];
18 
19 int
20 unalign_read(uint64_t *addr)
21 {
22 	uint64_t t;
23 
24 	t = *addr;
25 #if BYTE_ORDER == BIG_ENDIAN
26 	if (t != 0x13579aceffffabcdULL)
27 #else
28 	if (t != 0xffffabcd13579aceULL)
29 #endif
30 		return (1);
31 
32 	return (0);
33 }
34 
35 void
36 unalign_write(uint64_t *addr)
37 {
38 	uint64_t t;
39 
40 	t = 0xdeadbeaffadebabeULL;
41 	*addr = t;
42 }
43 
44 int
45 main(int argc, char *argv[])
46 {
47 #if !defined(__LP64__)
48 	uint32_t *addr = array;
49 
50 	/* align on a 64 bit boundary */
51 	if (((uint32_t)addr & 7) != 0)
52 		addr++;
53 
54 	addr[0] = 0x12345678;
55 	addr[1] = 0x13579ace;
56 	addr[2] = 0xffffabcd;
57 	addr[3] = 0x2468fedc;
58 
59 	if (unalign_read((uint64_t *)(addr + 1)))
60 		return (1);
61 
62 	unalign_write((uint64_t *)(addr + 1));
63 
64 #if BYTE_ORDER == BIG_ENDIAN
65 	if (addr[1] != 0xdeadbeaf || addr[2] != 0xfadebabe)
66 #else
67 	if (addr[1] != 0xfadebabe || addr[2] != 0xdeadbeaf)
68 #endif
69 		return (1);
70 #endif	/* __LP64__ */
71 
72 	return (0);
73 }
74