target/arm: Implement ARMv8.0-PredInv
[qemu/ar7.git] / scripts / qemu.py
blobf7269eefbb118d59e5399c540d721ed92121a363
1 # QEMU library
3 # Copyright (C) 2015-2016 Red Hat Inc.
4 # Copyright (C) 2012 IBM Corp.
6 # Authors:
7 # Fam Zheng <famz@redhat.com>
9 # This work is licensed under the terms of the GNU GPL, version 2. See
10 # the COPYING file in the top-level directory.
12 # Based on qmp.py.
15 import errno
16 import logging
17 import os
18 import subprocess
19 import qmp.qmp
20 import re
21 import shutil
22 import socket
23 import tempfile
26 LOG = logging.getLogger(__name__)
28 # Mapping host architecture to any additional architectures it can
29 # support which often includes its 32 bit cousin.
30 ADDITIONAL_ARCHES = {
31 "x86_64" : "i386",
32 "aarch64" : "armhf"
35 def kvm_available(target_arch=None):
36 host_arch = os.uname()[4]
37 if target_arch and target_arch != host_arch:
38 if target_arch != ADDITIONAL_ARCHES.get(host_arch):
39 return False
40 return os.access("/dev/kvm", os.R_OK | os.W_OK)
43 #: Maps machine types to the preferred console device types
44 CONSOLE_DEV_TYPES = {
45 r'^clipper$': 'isa-serial',
46 r'^malta': 'isa-serial',
47 r'^(pc.*|q35.*|isapc)$': 'isa-serial',
48 r'^(40p|powernv|prep)$': 'isa-serial',
49 r'^pseries.*': 'spapr-vty',
50 r'^s390-ccw-virtio.*': 'sclpconsole',
54 class QEMUMachineError(Exception):
55 """
56 Exception called when an error in QEMUMachine happens.
57 """
60 class QEMUMachineAddDeviceError(QEMUMachineError):
61 """
62 Exception raised when a request to add a device can not be fulfilled
64 The failures are caused by limitations, lack of information or conflicting
65 requests on the QEMUMachine methods. This exception does not represent
66 failures reported by the QEMU binary itself.
67 """
69 class MonitorResponseError(qmp.qmp.QMPError):
70 """
71 Represents erroneous QMP monitor reply
72 """
73 def __init__(self, reply):
74 try:
75 desc = reply["error"]["desc"]
76 except KeyError:
77 desc = reply
78 super(MonitorResponseError, self).__init__(desc)
79 self.reply = reply
82 class QEMUMachine(object):
83 """
84 A QEMU VM
86 Use this object as a context manager to ensure the QEMU process terminates::
88 with VM(binary) as vm:
89 ...
90 # vm is guaranteed to be shut down here
91 """
93 def __init__(self, binary, args=None, wrapper=None, name=None,
94 test_dir="/var/tmp", monitor_address=None,
95 socket_scm_helper=None):
96 '''
97 Initialize a QEMUMachine
99 @param binary: path to the qemu binary
100 @param args: list of extra arguments
101 @param wrapper: list of arguments used as prefix to qemu binary
102 @param name: prefix for socket and log file names (default: qemu-PID)
103 @param test_dir: where to create socket and log file
104 @param monitor_address: address for QMP monitor
105 @param socket_scm_helper: helper program, required for send_fd_scm()
106 @note: Qemu process is not started until launch() is used.
108 if args is None:
109 args = []
110 if wrapper is None:
111 wrapper = []
112 if name is None:
113 name = "qemu-%d" % os.getpid()
114 self._name = name
115 self._monitor_address = monitor_address
116 self._vm_monitor = None
117 self._qemu_log_path = None
118 self._qemu_log_file = None
119 self._popen = None
120 self._binary = binary
121 self._args = list(args) # Force copy args in case we modify them
122 self._wrapper = wrapper
123 self._events = []
124 self._iolog = None
125 self._socket_scm_helper = socket_scm_helper
126 self._qmp = None
127 self._qemu_full_args = None
128 self._test_dir = test_dir
129 self._temp_dir = None
130 self._launched = False
131 self._machine = None
132 self._console_device_type = None
133 self._console_address = None
134 self._console_socket = None
136 # just in case logging wasn't configured by the main script:
137 logging.basicConfig()
139 def __enter__(self):
140 return self
142 def __exit__(self, exc_type, exc_val, exc_tb):
143 self.shutdown()
144 return False
146 # This can be used to add an unused monitor instance.
147 def add_monitor_null(self):
148 self._args.append('-monitor')
149 self._args.append('null')
151 def add_fd(self, fd, fdset, opaque, opts=''):
153 Pass a file descriptor to the VM
155 options = ['fd=%d' % fd,
156 'set=%d' % fdset,
157 'opaque=%s' % opaque]
158 if opts:
159 options.append(opts)
161 # This did not exist before 3.4, but since then it is
162 # mandatory for our purpose
163 if hasattr(os, 'set_inheritable'):
164 os.set_inheritable(fd, True)
166 self._args.append('-add-fd')
167 self._args.append(','.join(options))
168 return self
170 # Exactly one of fd and file_path must be given.
171 # (If it is file_path, the helper will open that file and pass its
172 # own fd)
173 def send_fd_scm(self, fd=None, file_path=None):
174 # In iotest.py, the qmp should always use unix socket.
175 assert self._qmp.is_scm_available()
176 if self._socket_scm_helper is None:
177 raise QEMUMachineError("No path to socket_scm_helper set")
178 if not os.path.exists(self._socket_scm_helper):
179 raise QEMUMachineError("%s does not exist" %
180 self._socket_scm_helper)
182 # This did not exist before 3.4, but since then it is
183 # mandatory for our purpose
184 if hasattr(os, 'set_inheritable'):
185 os.set_inheritable(self._qmp.get_sock_fd(), True)
186 if fd is not None:
187 os.set_inheritable(fd, True)
189 fd_param = ["%s" % self._socket_scm_helper,
190 "%d" % self._qmp.get_sock_fd()]
192 if file_path is not None:
193 assert fd is None
194 fd_param.append(file_path)
195 else:
196 assert fd is not None
197 fd_param.append(str(fd))
199 devnull = open(os.path.devnull, 'rb')
200 proc = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
201 stderr=subprocess.STDOUT, close_fds=False)
202 output = proc.communicate()[0]
203 if output:
204 LOG.debug(output)
206 return proc.returncode
208 @staticmethod
209 def _remove_if_exists(path):
211 Remove file object at path if it exists
213 try:
214 os.remove(path)
215 except OSError as exception:
216 if exception.errno == errno.ENOENT:
217 return
218 raise
220 def is_running(self):
221 return self._popen is not None and self._popen.poll() is None
223 def exitcode(self):
224 if self._popen is None:
225 return None
226 return self._popen.poll()
228 def get_pid(self):
229 if not self.is_running():
230 return None
231 return self._popen.pid
233 def _load_io_log(self):
234 if self._qemu_log_path is not None:
235 with open(self._qemu_log_path, "r") as iolog:
236 self._iolog = iolog.read()
238 def _base_args(self):
239 if isinstance(self._monitor_address, tuple):
240 moncdev = "socket,id=mon,host=%s,port=%s" % (
241 self._monitor_address[0],
242 self._monitor_address[1])
243 else:
244 moncdev = 'socket,id=mon,path=%s' % self._vm_monitor
245 args = ['-chardev', moncdev,
246 '-mon', 'chardev=mon,mode=control',
247 '-display', 'none', '-vga', 'none']
248 if self._machine is not None:
249 args.extend(['-machine', self._machine])
250 if self._console_device_type is not None:
251 self._console_address = os.path.join(self._temp_dir,
252 self._name + "-console.sock")
253 chardev = ('socket,id=console,path=%s,server,nowait' %
254 self._console_address)
255 device = '%s,chardev=console' % self._console_device_type
256 args.extend(['-chardev', chardev, '-device', device])
257 return args
259 def _pre_launch(self):
260 self._temp_dir = tempfile.mkdtemp(dir=self._test_dir)
261 if self._monitor_address is not None:
262 self._vm_monitor = self._monitor_address
263 else:
264 self._vm_monitor = os.path.join(self._temp_dir,
265 self._name + "-monitor.sock")
266 self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log")
267 self._qemu_log_file = open(self._qemu_log_path, 'wb')
269 self._qmp = qmp.qmp.QEMUMonitorProtocol(self._vm_monitor,
270 server=True)
272 def _post_launch(self):
273 self._qmp.accept()
275 def _post_shutdown(self):
276 if self._qemu_log_file is not None:
277 self._qemu_log_file.close()
278 self._qemu_log_file = None
280 self._qemu_log_path = None
282 if self._console_socket is not None:
283 self._console_socket.close()
284 self._console_socket = None
286 if self._temp_dir is not None:
287 shutil.rmtree(self._temp_dir)
288 self._temp_dir = None
290 def launch(self):
292 Launch the VM and make sure we cleanup and expose the
293 command line/output in case of exception
296 if self._launched:
297 raise QEMUMachineError('VM already launched')
299 self._iolog = None
300 self._qemu_full_args = None
301 try:
302 self._launch()
303 self._launched = True
304 except:
305 self.shutdown()
307 LOG.debug('Error launching VM')
308 if self._qemu_full_args:
309 LOG.debug('Command: %r', ' '.join(self._qemu_full_args))
310 if self._iolog:
311 LOG.debug('Output: %r', self._iolog)
312 raise
314 def _launch(self):
316 Launch the VM and establish a QMP connection
318 devnull = open(os.path.devnull, 'rb')
319 self._pre_launch()
320 self._qemu_full_args = (self._wrapper + [self._binary] +
321 self._base_args() + self._args)
322 self._popen = subprocess.Popen(self._qemu_full_args,
323 stdin=devnull,
324 stdout=self._qemu_log_file,
325 stderr=subprocess.STDOUT,
326 shell=False,
327 close_fds=False)
328 self._post_launch()
330 def wait(self):
332 Wait for the VM to power off
334 self._popen.wait()
335 self._qmp.close()
336 self._load_io_log()
337 self._post_shutdown()
339 def shutdown(self):
341 Terminate the VM and clean up
343 if self.is_running():
344 try:
345 self._qmp.cmd('quit')
346 self._qmp.close()
347 except:
348 self._popen.kill()
349 self._popen.wait()
351 self._load_io_log()
352 self._post_shutdown()
354 exitcode = self.exitcode()
355 if exitcode is not None and exitcode < 0:
356 msg = 'qemu received signal %i: %s'
357 if self._qemu_full_args:
358 command = ' '.join(self._qemu_full_args)
359 else:
360 command = ''
361 LOG.warn(msg, -exitcode, command)
363 self._launched = False
365 def qmp(self, cmd, conv_keys=True, **args):
367 Invoke a QMP command and return the response dict
369 qmp_args = dict()
370 for key, value in args.items():
371 if conv_keys:
372 qmp_args[key.replace('_', '-')] = value
373 else:
374 qmp_args[key] = value
376 return self._qmp.cmd(cmd, args=qmp_args)
378 def command(self, cmd, conv_keys=True, **args):
380 Invoke a QMP command.
381 On success return the response dict.
382 On failure raise an exception.
384 reply = self.qmp(cmd, conv_keys, **args)
385 if reply is None:
386 raise qmp.qmp.QMPError("Monitor is closed")
387 if "error" in reply:
388 raise MonitorResponseError(reply)
389 return reply["return"]
391 def get_qmp_event(self, wait=False):
393 Poll for one queued QMP events and return it
395 if len(self._events) > 0:
396 return self._events.pop(0)
397 return self._qmp.pull_event(wait=wait)
399 def get_qmp_events(self, wait=False):
401 Poll for queued QMP events and return a list of dicts
403 events = self._qmp.get_events(wait=wait)
404 events.extend(self._events)
405 del self._events[:]
406 self._qmp.clear_events()
407 return events
409 def event_wait(self, name, timeout=60.0, match=None):
411 Wait for specified timeout on named event in QMP; optionally filter
412 results by match.
414 The 'match' is checked to be a recursive subset of the 'event'; skips
415 branch processing on match's value None
416 {"foo": {"bar": 1}} matches {"foo": None}
417 {"foo": {"bar": 1}} does not matches {"foo": {"baz": None}}
419 def event_match(event, match=None):
420 if match is None:
421 return True
423 for key in match:
424 if key in event:
425 if isinstance(event[key], dict):
426 if not event_match(event[key], match[key]):
427 return False
428 elif event[key] != match[key]:
429 return False
430 else:
431 return False
433 return True
435 # Search cached events
436 for event in self._events:
437 if (event['event'] == name) and event_match(event, match):
438 self._events.remove(event)
439 return event
441 # Poll for new events
442 while True:
443 event = self._qmp.pull_event(wait=timeout)
444 if (event['event'] == name) and event_match(event, match):
445 return event
446 self._events.append(event)
448 return None
450 def get_log(self):
452 After self.shutdown or failed qemu execution, this returns the output
453 of the qemu process.
455 return self._iolog
457 def add_args(self, *args):
459 Adds to the list of extra arguments to be given to the QEMU binary
461 self._args.extend(args)
463 def set_machine(self, machine_type):
465 Sets the machine type
467 If set, the machine type will be added to the base arguments
468 of the resulting QEMU command line.
470 self._machine = machine_type
472 def set_console(self, device_type=None):
474 Sets the device type for a console device
476 If set, the console device and a backing character device will
477 be added to the base arguments of the resulting QEMU command
478 line.
480 This is a convenience method that will either use the provided
481 device type, of if not given, it will used the device type set
482 on CONSOLE_DEV_TYPES.
484 The actual setting of command line arguments will be be done at
485 machine launch time, as it depends on the temporary directory
486 to be created.
488 @param device_type: the device type, such as "isa-serial"
489 @raises: QEMUMachineAddDeviceError if the device type is not given
490 and can not be determined.
492 if device_type is None:
493 if self._machine is None:
494 raise QEMUMachineAddDeviceError("Can not add a console device:"
495 " QEMU instance without a "
496 "defined machine type")
497 for regex, device in CONSOLE_DEV_TYPES.items():
498 if re.match(regex, self._machine):
499 device_type = device
500 break
501 if device_type is None:
502 raise QEMUMachineAddDeviceError("Can not add a console device:"
503 " no matching console device "
504 "type definition")
505 self._console_device_type = device_type
507 @property
508 def console_socket(self):
510 Returns a socket connected to the console
512 if self._console_socket is None:
513 self._console_socket = socket.socket(socket.AF_UNIX,
514 socket.SOCK_STREAM)
515 self._console_socket.connect(self._console_address)
516 return self._console_socket