hw/grackle: fix PCI bus initialization
[qemu/kevin.git] / tests / libqos / libqos.c
blobc7ba441d0bb80f226f4e8a10f64c34fab9b57e15
1 #include "qemu/osdep.h"
2 #include <sys/wait.h>
4 #include "libqtest.h"
5 #include "libqos/libqos.h"
6 #include "libqos/pci.h"
8 /*** Test Setup & Teardown ***/
10 /**
11 * Launch QEMU with the given command line,
12 * and then set up interrupts and our guest malloc interface.
14 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
16 char *cmdline;
18 struct QOSState *qs = g_malloc(sizeof(QOSState));
20 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
21 qs->qts = qtest_start(cmdline);
22 qs->ops = ops;
23 qtest_irq_intercept_in(global_qtest, "ioapic");
24 if (ops && ops->init_allocator) {
25 qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
28 g_free(cmdline);
29 return qs;
32 /**
33 * Launch QEMU with the given command line,
34 * and then set up interrupts and our guest malloc interface.
36 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
38 QOSState *qs;
39 va_list ap;
41 va_start(ap, cmdline_fmt);
42 qs = qtest_vboot(ops, cmdline_fmt, ap);
43 va_end(ap);
45 return qs;
48 /**
49 * Tear down the QEMU instance.
51 void qtest_shutdown(QOSState *qs)
53 if (qs->alloc && qs->ops && qs->ops->uninit_allocator) {
54 qs->ops->uninit_allocator(qs->alloc);
55 qs->alloc = NULL;
57 qtest_quit(qs->qts);
58 g_free(qs);
61 void set_context(QOSState *s)
63 global_qtest = s->qts;
66 static QDict *qmp_execute(const char *command)
68 char *fmt;
69 QDict *rsp;
71 fmt = g_strdup_printf("{ 'execute': '%s' }", command);
72 rsp = qmp(fmt);
73 g_free(fmt);
75 return rsp;
78 void migrate(QOSState *from, QOSState *to, const char *uri)
80 const char *st;
81 char *s;
82 QDict *rsp, *sub;
83 bool running;
85 set_context(from);
87 /* Is the machine currently running? */
88 rsp = qmp_execute("query-status");
89 g_assert(qdict_haskey(rsp, "return"));
90 sub = qdict_get_qdict(rsp, "return");
91 g_assert(qdict_haskey(sub, "running"));
92 running = qdict_get_bool(sub, "running");
93 QDECREF(rsp);
95 /* Issue the migrate command. */
96 s = g_strdup_printf("{ 'execute': 'migrate',"
97 "'arguments': { 'uri': '%s' } }",
98 uri);
99 rsp = qmp(s);
100 g_free(s);
101 g_assert(qdict_haskey(rsp, "return"));
102 QDECREF(rsp);
104 /* Wait for STOP event, but only if we were running: */
105 if (running) {
106 qmp_eventwait("STOP");
109 /* If we were running, we can wait for an event. */
110 if (running) {
111 migrate_allocator(from->alloc, to->alloc);
112 set_context(to);
113 qmp_eventwait("RESUME");
114 return;
117 /* Otherwise, we need to wait: poll until migration is completed. */
118 while (1) {
119 rsp = qmp_execute("query-migrate");
120 g_assert(qdict_haskey(rsp, "return"));
121 sub = qdict_get_qdict(rsp, "return");
122 g_assert(qdict_haskey(sub, "status"));
123 st = qdict_get_str(sub, "status");
125 /* "setup", "active", "completed", "failed", "cancelled" */
126 if (strcmp(st, "completed") == 0) {
127 QDECREF(rsp);
128 break;
131 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
132 QDECREF(rsp);
133 g_usleep(5000);
134 continue;
137 fprintf(stderr, "Migration did not complete, status: %s\n", st);
138 g_assert_not_reached();
141 migrate_allocator(from->alloc, to->alloc);
142 set_context(to);
145 bool have_qemu_img(void)
147 char *rpath;
148 const char *path = getenv("QTEST_QEMU_IMG");
149 if (!path) {
150 return false;
153 rpath = realpath(path, NULL);
154 if (!rpath) {
155 return false;
156 } else {
157 free(rpath);
158 return true;
162 void mkimg(const char *file, const char *fmt, unsigned size_mb)
164 gchar *cli;
165 bool ret;
166 int rc;
167 GError *err = NULL;
168 char *qemu_img_path;
169 gchar *out, *out2;
170 char *qemu_img_abs_path;
172 qemu_img_path = getenv("QTEST_QEMU_IMG");
173 g_assert(qemu_img_path);
174 qemu_img_abs_path = realpath(qemu_img_path, NULL);
175 g_assert(qemu_img_abs_path);
177 cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
178 fmt, file, size_mb);
179 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
180 if (err) {
181 fprintf(stderr, "%s\n", err->message);
182 g_error_free(err);
184 g_assert(ret && !err);
186 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
187 * glib 2.43.91 implementation assumes that any non-zero is an error for
188 * windows, but uses extra precautions for Linux. However,
189 * 0 is only possible if the program exited normally, so that should be
190 * sufficient for our purposes on all platforms, here. */
191 if (rc) {
192 fprintf(stderr, "qemu-img returned status code %d\n", rc);
194 g_assert(!rc);
196 g_free(out);
197 g_free(out2);
198 g_free(cli);
199 free(qemu_img_abs_path);
202 void mkqcow2(const char *file, unsigned size_mb)
204 return mkimg(file, "qcow2", size_mb);
207 void prepare_blkdebug_script(const char *debug_fn, const char *event)
209 FILE *debug_file = fopen(debug_fn, "w");
210 int ret;
212 fprintf(debug_file, "[inject-error]\n");
213 fprintf(debug_file, "event = \"%s\"\n", event);
214 fprintf(debug_file, "errno = \"5\"\n");
215 fprintf(debug_file, "state = \"1\"\n");
216 fprintf(debug_file, "immediately = \"off\"\n");
217 fprintf(debug_file, "once = \"on\"\n");
219 fprintf(debug_file, "[set-state]\n");
220 fprintf(debug_file, "event = \"%s\"\n", event);
221 fprintf(debug_file, "new_state = \"2\"\n");
222 fflush(debug_file);
223 g_assert(!ferror(debug_file));
225 ret = fclose(debug_file);
226 g_assert(ret == 0);
229 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
231 int i, j;
232 unsigned char *tx = (unsigned char *)buffer;
233 unsigned char p;
234 size_t *sx;
236 /* Write an indicative pattern that varies and is unique per-cycle */
237 p = rand() % 256;
238 for (i = 0; i < len; i++) {
239 tx[i] = p++ % 256;
240 if (i % cycle_len == 0) {
241 p = rand() % 256;
245 /* force uniqueness by writing an id per-cycle */
246 for (i = 0; i < len / cycle_len; i++) {
247 j = i * cycle_len;
248 if (j + sizeof(*sx) <= len) {
249 sx = (size_t *)&tx[j];
250 *sx = i;