Skip to contentSkip to conversation

kqemu.c should check return value for ioctl(KQEMU_EXEC)

PATCH New Juergen Keil 1 participant 1 message Jun 17, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kqemu.c should check return value for ioctl(KQEMU_EXEC)

When kqemu is enabled, the ioctl(KQEMU_EXEC) could fail for several reasons (e.g. with Linux kqemu-1.4.0pre1 it can fail with EIO or FAULT; and on OpenSolaris I just have a case where it's failing with EINVAL).

Problem is that in qemu's file kqemu.c function kqemu_cpu_exec() the return value from the ioctl(KQEMU_EXEC) is ignored and the code continues with the uninitialized kenv->retval.

Depending on the uninitialized kenv->retval, you may or may not get a Qemu abort with a register dump and an "Unsupported return value" error message. And there is no indication that the root cause was a failed ioctl. Like this:

% qemu -m 512 -localtime -hda /files2/qemu/sol10u4.img -cdrom /files2/media/sol-10-u4-ga-x86-dvd.iso -boot d EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000 ESI=00000000 EDI=00000000 EBP=00000000 ESP=08047f58 EIP=d27cb7b6 EFL=00000202 [-------] CPL=3 II=0 A20=1 SMM=0 HLT=0 ES =0173 00000000 ffffffff 00cff300
CS =016b 00000000 ffffffff 00cffb00
SS =0173 00000000 ffffffff 00cff300
DS =0173 00000000 ffffffff 00cff300
FS =0000 00000000 00000000 00000000
GS =01c3 d27fb400 ffffffff d2cff37f
LDT=0000 00000000 00000000 00008200
TR =0150 fec21a50 00000067 00008900
GDT= fec01000 000002cf
IDT= fec20da0 000007ff
CR0=8005003b CR2=00000000 CR3=1e0d8000 CR4=00000698 Unsupported return value: 0xfffffd7f

kqemu_cpu_exec() should check the return value from ioctl(KQEMU_EXEC) and report some error when the ioctl failed. And it should stop execution in some deterministic way.

Patch is attached.

kqemu.c +6 -2
Index: kqemu.c
===================================================================
@@ -771,8 +771,12 @@
         ret = -1;
     }
 #else
-    ioctl(kqemu_fd, KQEMU_EXEC, kenv);
-    ret = kenv->retval;
+    if (ioctl(kqemu_fd, KQEMU_EXEC, kenv) < 0) {
+        fprintf(stderr, "Error while running code in QEMU acceleration layer: %s\n", strerror(errno));
+        ret = -1;
+    } else {
+        ret = kenv->retval;
+    }
 #endif
     if (env->cpuid_features & CPUID_FXSR)
         save_native_fp_fxsave(env);