hw/arm: add Xunlong Orange Pi PC machine
[qemu/ar7.git] / python / qemu / accel.py
blob36ae85791eed8b905567d028fa3db8d0ab3bc0cc
1 """
2 QEMU accel module:
4 This module provides utilities for discover and check the availability of
5 accelerators.
6 """
7 # Copyright (C) 2015-2016 Red Hat Inc.
8 # Copyright (C) 2012 IBM Corp.
10 # Authors:
11 # Fam Zheng <famz@redhat.com>
13 # This work is licensed under the terms of the GNU GPL, version 2. See
14 # the COPYING file in the top-level directory.
17 import logging
18 import os
19 import subprocess
21 LOG = logging.getLogger(__name__)
23 # Mapping host architecture to any additional architectures it can
24 # support which often includes its 32 bit cousin.
25 ADDITIONAL_ARCHES = {
26 "x86_64" : "i386",
27 "aarch64" : "armhf",
28 "ppc64le" : "ppc64",
31 def list_accel(qemu_bin):
32 """
33 List accelerators enabled in the QEMU binary.
35 @param qemu_bin (str): path to the QEMU binary.
36 @raise Exception: if failed to run `qemu -accel help`
37 @return a list of accelerator names.
38 """
39 if not qemu_bin:
40 return []
41 try:
42 out = subprocess.check_output([qemu_bin, '-accel', 'help'],
43 universal_newlines=True)
44 except:
45 LOG.debug("Failed to get the list of accelerators in %s", qemu_bin)
46 raise
47 # Skip the first line which is the header.
48 return [acc.strip() for acc in out.splitlines()[1:]]
50 def kvm_available(target_arch=None, qemu_bin=None):
51 """
52 Check if KVM is available using the following heuristic:
53 - Kernel module is present in the host;
54 - Target and host arches don't mismatch;
55 - KVM is enabled in the QEMU binary.
57 @param target_arch (str): target architecture
58 @param qemu_bin (str): path to the QEMU binary
59 @return True if kvm is available, otherwise False.
60 """
61 if not os.access("/dev/kvm", os.R_OK | os.W_OK):
62 return False
63 if target_arch:
64 host_arch = os.uname()[4]
65 if target_arch != host_arch:
66 if target_arch != ADDITIONAL_ARCHES.get(host_arch):
67 return False
68 if qemu_bin and "kvm" not in list_accel(qemu_bin):
69 return False
70 return True
72 def tcg_available(qemu_bin):
73 """
74 Check if TCG is available.
76 @param qemu_bin (str): path to the QEMU binary
77 """
78 return 'tcg' in list_accel(qemu_bin)