1 ##########################################################################
2 # Copyright (c) 2009-2016 ETH Zurich.
5 # This file is distributed under the terms in the attached LICENSE file.
6 # If you do not find this file, copies can be found by writing to:
7 # ETH Zurich D-INFK, Universitaetstr 6, CH-8092 Zurich. Attn: Systems Group.
8 ##########################################################################
10 import os, debug, signal, shutil, time
13 class Machine(object):
16 def validateArgs(cls, kwargs):
20 raise TypeError("Missing key %s" % e.args[0])
22 def __init__(self, options, operations,
29 cores_per_socket=None,
31 serial_binary="serial_kernel",
33 eth0=(0xff, 0xff, 0xff),
39 self._name = "(unknown)"
40 self.options = options
41 self._operations = operations
43 self._bootarch = bootarch
45 self._machine_name = machine_name
47 if buildarchs is None:
48 buildarchs = [bootarch]
49 self._build_archs = buildarchs
50 assert(bootarch in buildarchs)
54 if cores_per_socket is None:
55 cores_per_socket = ncores
56 self._cores_per_socket = cores_per_socket
58 self._kernel_args = kernel_args
60 self._boot_driver = boot_driver
62 self._serial_binary = serial_binary
64 self._boot_timeout = boot_timeout
66 self._platform = platform
68 self._pci_args = pci_args
72 self._perfcount_type = perfcount_type
74 self._tick_rate = tickrate
77 debug.error("Fix machine definition, unknown args: %s" % str(kwargs))
79 def get_machine_name(self):
80 return self._machine_name
82 def get_bootarch(self):
83 """Return the architecture for booting and base system services."""
86 def get_buildarchs(self):
87 """Return the architectures that must be enabled in hake for this machine."""
88 return self._build_archs
90 def get_buildall_target(self):
91 """Return a valid make target to build default set of modules
93 raise NotImplementedError
96 """Returns absolute number of cores."""
99 def get_coreids(self):
100 """Returns the list of core IDs."""
101 return range(0, self.get_ncores()) # default behaviour for x86
103 # XXX: REMOVE ME. used only by lwip_loopback bench
104 def get_cores_per_socket(self):
105 """Returns number of cores per socket."""
106 return self._cores_per_socket
108 def get_tickrate(self):
109 """Returns clock rate in MHz."""
110 raise NotImplementedError
112 def get_perfcount_type(self):
113 """Returns a string ('amd0f', 'amd10', or 'intel'), or None if unknown"""
114 return self._perfcount_type
116 def get_kernel_args(self):
117 """Returns list of machine-specific arguments to add to the kernel command-line"""
118 return self._kernel_args
120 def get_boot_driver(self):
121 """Returns list of machine-specific arguments to add to the kernel command-line"""
122 return self._boot_driver
124 def get_pci_args(self):
125 """Returns list of machine-specific arguments to add to the PCI command-line"""
126 return self._pci_args
128 def get_platform(self):
129 """Returns machine-specific platform specifier"""
130 return self._platform
133 """Returns machine-specific bus:dev:fun for connected network interface of machine"""
134 # 0xff for all three elements should preserve kaluga default behaviour
137 def get_serial_binary(self):
138 """Returns a machine-specific binary name for the serial driver
139 (fallback if not implemented is the kernel serial driver)"""
140 return self._serial_binary
142 def get_boot_timeout(self):
143 """Returns a machine-specific timeout (in seconds), or None for the default"""
144 return self._boot_timeout
146 def get_test_timeout(self):
147 """Returns a machine-specific timeout (in seconds), or None for the default"""
150 def get_tftp_dir(self):
151 """Return a unique TFTP boot directory for this machine."""
152 print("DEPRECATED get_tftp_dir")
153 return self._operations.get_tftp_dir()
155 def set_bootmodules(self, modules):
156 """Set the machine to boot from the given module data."""
157 print("DEPRECATED set_bootmodules")
158 return self._operations.set_bootmodules(modules)
161 """Lock the machine to prevent concurrent access."""
162 print("DEPRECATED lock")
163 return self._operations.lock()
166 """Unlock an already-locked machine."""
167 print("DEPRECATED unlock")
168 return self._operations.unlock()
172 """Prepare a locked machine to be booted."""
173 print("DEPRECATED setup")
174 return self._operations.setup()
177 """Reboot (or boot) the machine."""
178 print("DEPRECATED reboot")
179 return self._operations.reboot()
182 """Shut down/switch off the machine."""
183 print("DEPRECATED shutdown")
184 return self._operations.shutdown()
186 def get_output(self):
187 """Returns a file object to the output of a locked machine."""
188 print("DEPRECATED get_output")
189 return self._operations.get_output()
191 def force_write(self, consolectrl):
192 print("DEPRECATED force_write")
193 return self._operations.force_write(consolectrl)
198 def setName(self, name):
201 def default_bootmodules(self):
202 """Returns the default boot module configuration for the given machine."""
203 # FIXME: clean up / separate platform-specific logic
206 a = machine.get_bootarch()
208 # set the kernel: elver on x86_64
211 elif a == "armv7" or a == "armv8":
212 kernel = "cpu_%s" % machine.get_platform()
216 m = barrelfish.BootModules(machine, prefix=("%s/sbin/" % a), kernel=kernel)
217 m.add_kernel_args(machine.get_kernel_args())
218 # default for all barrelfish archs
219 # hack: cpu driver is not called "cpu" for ARMv7 builds
221 m.add_module("cpu_%s" % machine.get_platform(), machine.get_kernel_args())
226 m.set_cpu_driver(kernel, machine.get_kernel_args())
228 m.set_boot_driver(machine.get_boot_driver())
230 m.add_module("cpu", machine.get_kernel_args())
233 m.add_module("mem_serv")
234 m.add_module("monitor")
235 m.add_module("ramfsd", ["boot"])
236 m.add_module("skb", ["boot"])
237 m.add_module("spawnd", ["boot"])
238 m.add_module("startd", ["boot"])
239 m.add_module("/eclipseclp_ramfs.cpio.gz", ["nospawn"])
240 m.add_module("/skb_ramfs.cpio.gz", ["nospawn"])
241 m.add_module("corectrl", ["auto"])
245 m.add_module("acpi", ["boot"])
246 m.add_module("kaluga", ["boot"])
248 # SKB and PCI are x86-only for the moment
249 if a == "x86_64" or a == "x86_32":
250 m.add_module("acpi", ["boot"])
251 m.add_module("routing_setup", ["boot"])
253 # Add pci with machine-specific extra-arguments
254 m.add_module("pci", ["auto"] + machine.get_pci_args())
256 # Add kaluga with machine-specific bus:dev:fun triplet for eth0
258 m.add_module("kaluga",
259 ["boot", "eth0=%d:%d:%d" % machine.get_eth0()])
261 # coreboot should work on armv7 now
263 m.add_module("kaluga", machine.get_kaluga_args())
266 class MachineOperations(object):
268 def __init__(self, machine):
269 self._machine = machine
271 def get_tftp_dir(self):
272 """Return a unique TFTP boot directory for this machine."""
273 raise NotImplementedError
275 def set_bootmodules(self, modules):
276 """Set the machine to boot from the given module data."""
277 raise NotImplementedError
280 """Lock the machine to prevent concurrent access."""
281 raise NotImplementedError
284 """Unlock an already-locked machine."""
285 raise NotImplementedError
288 """Prepare a locked machine to be booted."""
289 raise NotImplementedError
292 """Reboot (or boot) the machine."""
293 raise NotImplementedError
296 """Shut down/switch off the machine."""
297 raise NotImplementedError
299 def get_output(self):
300 """Returns a file object to the output of a locked machine."""
301 raise NotImplementedError
303 def force_write(self, consolectrl):
304 raise NotImplementedError
306 class MachineLockedError(Exception):
307 """May be raised by lock() when the machine is locked by another user."""
310 class ARMMachineBase(Machine):
313 def validateArgs(cls, kwargs):
314 super(ARMMachineBase, cls).validateArgs(kwargs)
317 except KeyError as e:
318 raise TypeError("Missing key %s" % e.args[0])
320 def __init__(self, options, operations, **kwargs):
321 super(ARMMachineBase, self).__init__(options, operations, **kwargs)
324 self.kernel_args = None
325 self.kaluga_args = None
326 self.menulst_template = "menu.lst." + self.get_bootarch() + "_" + \
327 self.get_platform() + ("_%d" % self.get_ncores())
328 self._set_kernel_image()
330 def _get_template_menu_lst(self):
331 """Read menu lst in source tree"""
332 if self.menulst is None:
333 template_menulst = os.path.join(self.options.sourcedir, "hake",
334 self.menulst_template)
335 with open(template_menulst) as f:
336 self.menulst = f.readlines()
340 def _set_kernel_image(self):
341 if self.options.existingbuild:
342 self.kernel_img = os.path.join(self.options.existingbuild, self.imagename)
344 self.kernel_img = os.path.join(self.options.buildbase,
345 self.options.builds[0].name,
348 def get_kernel_args(self):
349 if self.kernel_args is None:
350 for line in self._get_template_menu_lst():
351 if line.startswith("kernel"):
352 _, _, args = line.strip().split(" ", 2)
353 self.kernel_args = args.split(" ")
354 return self.kernel_args
357 """Grab MMAP data from menu lst in source tree"""
358 if self.mmap is None:
360 for line in self._get_template_menu_lst():
361 if line.startswith("mmap"):
362 self.mmap.append(line)
364 debug.debug("got MMAP:\n %s" % " ".join(self.mmap))
367 def get_kaluga_args(self):
368 if self.kaluga_args is None:
369 for line in self._get_template_menu_lst():
371 _,_,args = line.strip().split(' ', 2)
372 self.kaluga_args = args.split(' ')
374 return self.kaluga_args
376 def _write_menu_lst(self, data, path):
377 debug.verbose('writing %s' % path)
381 for line in self._get_mmap():
385 class ARMSimulatorBase(ARMMachineBase):
387 def __init__(self, options, operations,
388 boot_timeout=20, **kwargs):
389 super(ARMSimulatorBase, self).__init__(options, operations,
390 boot_timeout=boot_timeout,
393 def get_tickrate(self):
396 def get_test_timeout(self):
397 """Default test timeout for ARM simulators: 10min"""
400 def get_machine_name(self):
403 class ARMSimulatorOperations(MachineOperations):
405 def __init__(self, machine):
406 super(ARMSimulatorOperations, self).__init__(machine)
410 self.simulator_start_timeout = 5 # seconds
415 def force_write(self, consolectrl):
424 def get_free_port(self):
426 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
428 # extract port from addrinfo
429 self.telnet_port = s.getsockname()[1]
432 def _get_cmdline(self):
433 raise NotImplementedError
435 def _kill_child(self):
436 # terminate child if running
439 os.kill(self.child.pid, signal.SIGTERM)
441 debug.verbose("Caught OSError trying to kill child: %r" % e)
443 debug.verbose("Caught exception trying to kill child: %r" % e)
448 "Caught exception while waiting for child: %r" % e)
452 debug.verbose('Simulator:shutdown requested');
453 debug.verbose('terminating simulator')
454 if not self.child is None:
456 self.child.terminate()
458 debug.verbose("Error when trying to terminate simulator: %r" % e)
459 debug.verbose('closing telnet connection')
460 if self.telnet is not None:
463 # try to cleanup tftp tree if needed
464 if self.tftp_dir and os.path.isdir(self.tftp_dir):
465 shutil.rmtree(self.tftp_dir, ignore_errors=True)
468 def get_output(self):
469 # wait a bit to give the simulator time to listen for a telnet connection
470 if self.child.poll() != None: # Check if child is down
471 print 'Simulator is down, return code is %d' % self.child.returncode
475 self.telnet_connected = False
476 while not self.telnet_connected:
478 self.telnet = telnetlib.Telnet("localhost", self.telnet_port)
479 self.telnet_connected = True
480 self.output = self.telnet.get_socket().makefile()
483 if errno != 111: # connection refused
484 debug.error("telnet: %s [%d]" % (msg, errno))
486 self.telnet_connected = False
487 time.sleep(self.simulator_start_timeout)
491 class MachineFactory:
493 machineFactories = {}
495 def __init__(self, name, machineClass, kwargs):
496 self._class = machineClass
497 self._kwargs = kwargs
501 def addMachine(cls, name, machineClass, **kwargs):
502 cls.machineFactories[name] = MachineFactory(name, machineClass, kwargs)
503 machineClass.validateArgs(kwargs)
506 """Get the name of the machine produced by this factory."""
509 def createMachine(self, options):
510 """Create a new machine instance."""
512 machine = self._class(options, **self._kwargs)
513 except TypeError as e:
514 print("Machine class %s failed to instantiate: %s" % (str(self._class), str(e)))
516 machine.setName(self._name)
520 def createMachineByName(cls, name, options):
521 """Create a new machine instance."""
522 return cls.machineFactories[name].createMachine(options)
524 # Assume that QEMU, FVP, pandaboard and Gem5 work everywhere if invoked
530 # Other site-specific modules will be loaded by the siteconfig module