gdb command: qemu handlers
[qemu/ar7.git] / tests / endianness-test.c
blob2054338e1839af08acad75fde81ade4a413fad96
1 /*
2 * QTest testcase for ISA endianness
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include <glib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
20 #include "libqtest.h"
21 #include "qemu/bswap.h"
23 typedef struct TestCase TestCase;
24 struct TestCase {
25 const char *arch;
26 const char *machine;
27 uint64_t isa_base;
28 bool bswap;
29 const char *superio;
32 static const TestCase test_cases[] = {
33 { "i386", "pc", -1 },
34 { "mips", "mips", 0x14000000, .bswap = true },
35 { "mips", "malta", 0x10000000, .bswap = true },
36 { "mips64", "magnum", 0x90000000, .bswap = true },
37 { "mips64", "pica61", 0x90000000, .bswap = true },
38 { "mips64", "mips", 0x14000000, .bswap = true },
39 { "mips64", "malta", 0x10000000, .bswap = true },
40 { "mips64el", "fulong2e", 0x1fd00000 },
41 { "ppc", "g3beige", 0xfe000000, .bswap = true, .superio = "i82378" },
42 { "ppc", "prep", 0x80000000, .bswap = true },
43 { "ppc", "bamboo", 0xe8000000, .bswap = true, .superio = "i82378" },
44 { "ppc64", "mac99", 0xf2000000, .bswap = true, .superio = "i82378" },
45 { "ppc64", "pseries", 0x10080000000ULL,
46 .bswap = true, .superio = "i82378" },
47 { "sh4", "r2d", 0xfe240000, .superio = "i82378" },
48 { "sh4eb", "r2d", 0xfe240000, .bswap = true, .superio = "i82378" },
49 { "sparc64", "sun4u", 0x1fe02000000LL, .bswap = true },
50 { "x86_64", "pc", -1 },
54 static uint8_t isa_inb(const TestCase *test, uint16_t addr)
56 uint8_t value;
57 if (test->isa_base == -1) {
58 value = inb(addr);
59 } else {
60 value = readb(test->isa_base + addr);
62 return value;
65 static uint16_t isa_inw(const TestCase *test, uint16_t addr)
67 uint16_t value;
68 if (test->isa_base == -1) {
69 value = inw(addr);
70 } else {
71 value = readw(test->isa_base + addr);
73 return test->bswap ? bswap16(value) : value;
76 static uint32_t isa_inl(const TestCase *test, uint16_t addr)
78 uint32_t value;
79 if (test->isa_base == -1) {
80 value = inl(addr);
81 } else {
82 value = readl(test->isa_base + addr);
84 return test->bswap ? bswap32(value) : value;
87 static void isa_outb(const TestCase *test, uint16_t addr, uint8_t value)
89 if (test->isa_base == -1) {
90 outb(addr, value);
91 } else {
92 writeb(test->isa_base + addr, value);
96 static void isa_outw(const TestCase *test, uint16_t addr, uint16_t value)
98 value = test->bswap ? bswap16(value) : value;
99 if (test->isa_base == -1) {
100 outw(addr, value);
101 } else {
102 writew(test->isa_base + addr, value);
106 static void isa_outl(const TestCase *test, uint16_t addr, uint32_t value)
108 value = test->bswap ? bswap32(value) : value;
109 if (test->isa_base == -1) {
110 outl(addr, value);
111 } else {
112 writel(test->isa_base + addr, value);
117 static void test_endianness(gconstpointer data)
119 const TestCase *test = data;
120 char *args;
122 args = g_strdup_printf("-M %s%s%s -device pc-testdev",
123 test->machine,
124 test->superio ? " -device " : "",
125 test->superio ?: "");
126 qtest_start(args);
127 isa_outl(test, 0xe0, 0x87654321);
128 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
129 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
130 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
131 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x87);
132 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x65);
133 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x43);
134 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x21);
136 isa_outw(test, 0xe2, 0x8866);
137 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x88664321);
138 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8866);
139 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
140 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x88);
141 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x66);
142 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x43);
143 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x21);
145 isa_outw(test, 0xe0, 0x4422);
146 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x88664422);
147 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8866);
148 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4422);
149 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x88);
150 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x66);
151 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x44);
152 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x22);
154 isa_outb(test, 0xe3, 0x87);
155 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87664422);
156 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8766);
157 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x87);
158 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x66);
159 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x44);
160 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x22);
162 isa_outb(test, 0xe2, 0x65);
163 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654422);
164 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
165 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4422);
166 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x87);
167 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x65);
168 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x44);
169 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x22);
171 isa_outb(test, 0xe1, 0x43);
172 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654322);
173 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
174 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4322);
175 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x87);
176 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x65);
177 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x43);
178 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x22);
180 isa_outb(test, 0xe0, 0x21);
181 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
182 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
183 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
184 g_assert_cmphex(isa_inb(test, 0xe3), ==, 0x87);
185 g_assert_cmphex(isa_inb(test, 0xe2), ==, 0x65);
186 g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x43);
187 g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x21);
188 qtest_quit(global_qtest);
189 g_free(args);
192 static void test_endianness_split(gconstpointer data)
194 const TestCase *test = data;
195 char *args;
197 args = g_strdup_printf("-M %s%s%s -device pc-testdev",
198 test->machine,
199 test->superio ? " -device " : "",
200 test->superio ?: "");
201 qtest_start(args);
202 isa_outl(test, 0xe8, 0x87654321);
203 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
204 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
205 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
207 isa_outw(test, 0xea, 0x8866);
208 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x88664321);
209 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8866);
210 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
212 isa_outw(test, 0xe8, 0x4422);
213 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x88664422);
214 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8866);
215 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4422);
217 isa_outb(test, 0xeb, 0x87);
218 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87664422);
219 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8766);
221 isa_outb(test, 0xea, 0x65);
222 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654422);
223 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
224 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4422);
226 isa_outb(test, 0xe9, 0x43);
227 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654322);
228 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
229 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4322);
231 isa_outb(test, 0xe8, 0x21);
232 g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
233 g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
234 g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
235 qtest_quit(global_qtest);
236 g_free(args);
239 static void test_endianness_combine(gconstpointer data)
241 const TestCase *test = data;
242 char *args;
244 args = g_strdup_printf("-M %s%s%s -device pc-testdev",
245 test->machine,
246 test->superio ? " -device " : "",
247 test->superio ?: "");
248 qtest_start(args);
249 isa_outl(test, 0xe0, 0x87654321);
250 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654321);
251 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
252 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4321);
254 isa_outw(test, 0xe2, 0x8866);
255 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x88664321);
256 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8866);
257 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4321);
259 isa_outw(test, 0xe0, 0x4422);
260 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x88664422);
261 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8866);
262 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4422);
264 isa_outb(test, 0xe3, 0x87);
265 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87664422);
266 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8766);
268 isa_outb(test, 0xe2, 0x65);
269 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654422);
270 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
271 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4422);
273 isa_outb(test, 0xe1, 0x43);
274 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654322);
275 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
276 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4322);
278 isa_outb(test, 0xe0, 0x21);
279 g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654321);
280 g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
281 g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4321);
282 qtest_quit(global_qtest);
283 g_free(args);
286 int main(int argc, char **argv)
288 const char *arch = qtest_get_arch();
289 int ret;
290 int i;
292 g_test_init(&argc, &argv, NULL);
294 for (i = 0; test_cases[i].arch; i++) {
295 gchar *path;
296 if (strcmp(test_cases[i].arch, arch) != 0) {
297 continue;
299 path = g_strdup_printf("endianness/%s",
300 test_cases[i].machine);
301 qtest_add_data_func(path, &test_cases[i], test_endianness);
303 path = g_strdup_printf("endianness/split/%s",
304 test_cases[i].machine);
305 qtest_add_data_func(path, &test_cases[i], test_endianness_split);
307 path = g_strdup_printf("endianness/combine/%s",
308 test_cases[i].machine);
309 qtest_add_data_func(path, &test_cases[i], test_endianness_combine);
312 ret = g_test_run();
314 return ret;