aboutsummaryrefslogtreecommitdiffstats
path: root/kvm-android.c
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@android.com>2011-05-20 01:18:01 +0200
committerDavid 'Digit' Turner <digit@android.com>2011-05-26 01:41:57 +0200
commit36597756e589622ee6c6628efb47c1b130d5ee85 (patch)
treef62f0a9d22a15afa3166912a9eb6a371d67d3845 /kvm-android.c
parent21ca493a04b74f35d3fd263afb369f7a1eda7aae (diff)
downloadexternal_qemu-36597756e589622ee6c6628efb47c1b130d5ee85.zip
external_qemu-36597756e589622ee6c6628efb47c1b130d5ee85.tar.gz
external_qemu-36597756e589622ee6c6628efb47c1b130d5ee85.tar.bz2
x86: kvm: fix KVM build + enable auto-detection.
This patch fixes the build of KVM support in the x86 emulator by copying official Ubuntu Lucid KVM headers into android/config/linux-*/ This removes the need to rely on the build machine's versions of these headers, which caused various issues. Also, by default, the emulator will now probe the system to see if it can start in KVM mode automatically. See android-kvm.c for details. You can see the result of the probing with the -verbose option. IMPORTANT NOTE: It looks like there is a bug in the KVM code when the emulator is built as a 32-bit binary, running on a 64-bit kernel, so we explicitely disable KVM when we detect this case. It's hard to tell whether this is a bug in QEMU or some versions of the KVM driver. As such, KVM only works when building the emulator as a 64-bit program. For now, this is only possible with "android-configure.sh --try-64", not the Android build system. + Add a new QEMU option (-disable-kvm) to explicitely disable KVM if needed. This is an addition to -enable-kvm which already exists (and forces usage of KVM). Change-Id: I6a397cae29ab62b1c56fce95c1ee75a4664d6688
Diffstat (limited to 'kvm-android.c')
-rw-r--r--kvm-android.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/kvm-android.c b/kvm-android.c
new file mode 100644
index 0000000..55293fa
--- /dev/null
+++ b/kvm-android.c
@@ -0,0 +1,62 @@
+#include <unistd.h>
+#include <string.h>
+#include <sys/utsname.h>
+#include "android/utils/debug.h"
+
+#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)
+
+/* A simple routine used to check that we can run the program under KVM.
+ * We simply want to ensure that the emulator binary and the kernel have the
+ * same endianess.
+ *
+ * A 32-bit executable cannot use the 64-bit KVM interface, even to run a 32-bit guest.
+ */
+
+#ifndef __linux__
+#error "This file should only be compiled under linux"
+#endif
+
+int
+kvm_check_allowed(void)
+{
+ /* sizeof(void*) is enough to give us the program's bitness */
+ const int isProgram64bits = (sizeof(void*) == 8);
+ int isKernel64bits = 0;
+ struct utsname utn;
+
+ /* Is there a /dev/kvm device file here? */
+ if (access("/dev/kvm",F_OK)) {
+ /* no need to print a warning here */
+ D("No kvm device file detected");
+ return 0;
+ }
+
+ /* Can we access it? */
+ if (access("/dev/kvm",R_OK)) {
+ D("KVM device file is not readable for this user.");
+ return 0;
+ }
+
+ /* Determine kernel endianess */
+ memset(&utn,0,sizeof(utn));
+ uname(&utn);
+ D("Kernel machine type: %s", utn.machine);
+ if (strcmp(utn.machine,"x86_64") == 0)
+ isKernel64bits = 1;
+ else if (strcmp(utn.machine,"amd64") == 0)
+ isKernel64bits = 1;
+
+
+ if (isProgram64bits != isKernel64bits) {
+ if (isProgram64bits) {
+ D("kvm disabled (64-bit emulator and 32-bit kernel)");
+ } else {
+ D("kvm disabled (32-bit emulator and 64-bit kernel)");
+ }
+ return 0;
+ }
+
+ D("KVM mode auto-enabled!");
+ return 1;
+}
+