source: mainline/tools/ew.py@ cd0a38e

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd0a38e was 0436aec, checked in by Jakub Jermář <jakub@…>, 3 years ago

tools/ew.py: Allow to run sun4u with serial console only

  • Property mode set to 100755
File size: 15.1 KB
RevLine 
[edb57bc6]1#!/usr/bin/env python3
[df64dbc]2#
[3f4c537a]3# Copyright (c) 2013 Jakub Jermar
[df64dbc]4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30
31"""
32Emulator wrapper for running HelenOS
33"""
34
[4b65f9a]35import inspect
[1c24c7c]36import os
[8a26f82]37import platform
[1783f75]38import re
39import subprocess
40import sys
[663f445f]41import _thread
[df425da]42import time
[df64dbc]43
[f5ceb18]44overrides = {}
45
[1783f75]46CONFIG = 'Makefile.config'
47
[4b65f9a]48TOOLS_DIR = os.path.dirname(inspect.getabsfile(inspect.currentframe()))
49
[1783f75]50def read_config():
51 "Read HelenOS build configuration"
52
53 inf = open(CONFIG, 'r')
54 config = {}
55
56 for line in inf:
57 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
58 if (res):
59 config[res.group(1)] = res.group(2)
60
61 inf.close()
62 return config
63
[f5ceb18]64def is_override(str):
65 if str in overrides.keys():
66 return overrides[str]
67 return False
68
[df425da]69def cfg_get(platform, machine, processor):
[663f445f]70 if machine == "" or "run" in emulators[platform]:
[f5ceb18]71 return emulators[platform]
[663f445f]72 elif processor == "" or "run" in emulators[platform][machine]:
[f5ceb18]73 return emulators[platform][machine]
[df425da]74 else:
75 return emulators[platform][machine][processor]
[f5ceb18]76
[e4c8e3cf]77def termemu_detect():
[ac49d23c]78 emus = ['gnome-terminal', 'xfce4-terminal', 'xterm']
79 for termemu in emus:
[e4c8e3cf]80 try:
81 subprocess.check_output('which ' + termemu, shell = True)
82 return termemu
83 except:
84 pass
85
[ac49d23c]86 print('Could not find any of the terminal emulators %s.'%(emus))
87 sys.exit(1)
88
[df64dbc]89def run_in_console(cmd, title):
[ac49d23c]90 temu = termemu_detect()
91 if temu == 'gnome-terminal':
[ecf0a04b]92 cmdline = temu + ' -- ' + cmd
[ac49d23c]93 else:
[ecf0a04b]94 ecmd = cmd.replace('"', '\\"')
[ac49d23c]95 cmdline = temu + ' -T ' + '"' + title + '"' + ' -e "' + ecmd + '"'
96
[e4a1497]97 print(cmdline)
[f5ceb18]98 if not is_override('dryrun'):
[e4c8e3cf]99 subprocess.call(cmdline, shell = True)
[df64dbc]100
[8a26f82]101def get_host_native_width():
102 return int(platform.architecture()[0].strip('bit'))
103
104def pc_options(guest_width):
105 opts = ''
[a35b458]106
[8a26f82]107 # Do not enable KVM if running 64 bits HelenOS
108 # on 32 bits host
109 host_width = get_host_native_width()
[f5ceb18]110 if guest_width <= host_width and not is_override('nokvm'):
[8a26f82]111 opts = opts + ' -enable-kvm'
[a35b458]112
[8a26f82]113 # Remove the leading space
114 return opts[1:]
[df64dbc]115
116def malta_options():
[dabaa83]117 return '-cpu 4Kc -append "console=devices/\\hw\\pci0\\00:0a.0\\com1\\a"'
[df64dbc]118
[739bc43]119def find_firmware(name, environ_var, default_paths, extra_info=None):
120 """Find firmware image(s)."""
121
122 if environ_var in os.environ:
123 return os.environ[environ_var]
124
125 for path in default_paths:
126 if os.path.exists(path):
127 return path
128
129 sys.stderr.write("Cannot find %s binary image(s)!\n" % name)
130 sys.stderr.write(
131 "Either set %s environment variable accordingly or place the image(s) in one of the default locations: %s.\n" %
132 (environ_var, ", ".join(default_paths)))
133 if extra_info is not None:
134 sys.stderr.write(extra_info)
135 return None
136
[9185e42]137def platform_to_qemu_options(platform, machine, processor):
[df64dbc]138 if platform == 'amd64':
[8a26f82]139 return 'system-x86_64', pc_options(64)
[df64dbc]140 elif platform == 'arm32':
[e9f7778]141 if machine == 'integratorcp':
142 return 'system-arm', '-M integratorcp'
143 elif machine == 'raspberrypi':
144 return 'system-arm', '-M raspi1ap'
[84176f3]145 elif platform == 'arm64':
[06f10ac]146 if machine == 'virt':
147 # Search for the EDK2 firmware image
148 default_paths = (
149 '/usr/local/qemu-efi-aarch64/QEMU_EFI.fd', # Custom
150 '/usr/share/edk2/aarch64/QEMU_EFI.fd', # Fedora
151 '/usr/share/qemu-efi-aarch64/QEMU_EFI.fd', # Ubuntu
152 )
153 extra_info = ("Pre-compiled binary can be obtained from "
154 "http://snapshots.linaro.org/components/kernel/leg-virt-tianocore-edk2-upstream/latest/QEMU-AARCH64/RELEASE_GCC5/QEMU_EFI.fd.\n")
155 efi_path = find_firmware(
156 "EDK2", 'EW_QEMU_EFI_AARCH64', default_paths, extra_info)
157 if efi_path is None:
158 raise Exception
159
160 return 'system-aarch64', \
161 '-M virt -cpu cortex-a57 -m 1024 -bios %s' % efi_path
[df64dbc]162 elif platform == 'ia32':
[8a26f82]163 return 'system-i386', pc_options(32)
[df64dbc]164 elif platform == 'mips32':
165 if machine == 'lmalta':
166 return 'system-mipsel', malta_options()
167 elif machine == 'bmalta':
168 return 'system-mips', malta_options()
169 elif platform == 'ppc32':
[644352c]170 return 'system-ppc', '-m 256'
[df64dbc]171 elif platform == 'sparc64':
[9185e42]172 if machine != 'generic':
173 raise Exception
174 if processor == 'us':
[0436aec]175 cmdline = '-M sun4u'
176 if is_override('nographic'):
177 cmdline += ' --prom-env boot-args="console=devices/\\hw\\pci0\\01:01.0\\com1\\a"'
178 return 'system-sparc64', cmdline
[739bc43]179
180 # processor = 'sun4v'
181 opensparc_bins = find_firmware(
182 "OpenSPARC", 'OPENSPARC_BINARIES',
183 ('/usr/local/opensparc/image/', ))
184 if opensparc_bins is None:
[9185e42]185 raise Exception
186
187 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins)
188
[df64dbc]189
[129b92c6]190def hdisk_mk():
[df64dbc]191 if not os.path.exists('hdisk.img'):
[4b65f9a]192 subprocess.call(TOOLS_DIR + '/mkfat.py 1048576 dist/data hdisk.img', shell = True)
[f5ceb18]193
[129b92c6]194def qemu_bd_options():
195 if is_override('nohdd'):
196 return ''
[a35b458]197
[129b92c6]198 hdisk_mk()
[a35b458]199
[13eecc4]200 hdd_options = ''
201 if 'hdd' in overrides.keys():
202 if 'ata' in overrides['hdd'].keys():
203 hdd_options += ''
204 elif 'virtio-blk' in overrides['hdd'].keys():
205 hdd_options += ',if=virtio'
206
207 return ' -drive file=hdisk.img,index=0,media=disk,format=raw' + hdd_options
[df64dbc]208
209def qemu_nic_ne2k_options():
[d4b7b29]210 return ' -device ne2k_isa,irq=5,netdev=n1'
[df64dbc]211
212def qemu_nic_e1k_options():
[d4b7b29]213 return ' -device e1000,netdev=n1'
[df64dbc]214
215def qemu_nic_rtl8139_options():
[d4b7b29]216 return ' -device rtl8139,netdev=n1'
[df64dbc]217
[7bf16b7e]218def qemu_nic_virtio_options():
[d4b7b29]219 return ' -device virtio-net,netdev=n1'
[7bf16b7e]220
[df64dbc]221def qemu_net_options():
[f5ceb18]222 if is_override('nonet'):
223 return ''
224
225 nic_options = ''
226 if 'net' in overrides.keys():
227 if 'e1k' in overrides['net'].keys():
228 nic_options += qemu_nic_e1k_options()
229 if 'rtl8139' in overrides['net'].keys():
230 nic_options += qemu_nic_rtl8139_options()
231 if 'ne2k' in overrides['net'].keys():
232 nic_options += qemu_nic_ne2k_options()
[7bf16b7e]233 if 'virtio-net' in overrides['net'].keys():
234 nic_options += qemu_nic_virtio_options()
[f5ceb18]235 else:
236 # Use the default NIC
237 nic_options += qemu_nic_e1k_options()
238
[d4b7b29]239 return nic_options + ' -netdev user,id=n1,hostfwd=udp::8080-:8080,hostfwd=udp::8081-:8081,hostfwd=tcp::8080-:8080,hostfwd=tcp::8081-:8081,hostfwd=tcp::2223-:2223'
[df64dbc]240
241def qemu_usb_options():
[f5ceb18]242 if is_override('nousb'):
243 return ''
244 return ' -usb'
[df64dbc]245
[5119d34]246def qemu_xhci_options():
247 if is_override('noxhci'):
248 return ''
249 return ' -device nec-usb-xhci,id=xhci'
250
[27de618]251def qemu_tablet_options():
252 if is_override('notablet') or (is_override('nousb') and is_override('noxhci')):
253 return ''
254 return ' -device usb-tablet'
255
[f5ceb18]256def qemu_audio_options():
257 if is_override('nosnd'):
258 return ''
[089901e]259 return ' -device intel-hda -device hda-duplex'
[f5ceb18]260
[df425da]261def qemu_run(platform, machine, processor):
262 cfg = cfg_get(platform, machine, processor)
[9185e42]263 suffix, options = platform_to_qemu_options(platform, machine, processor)
[df64dbc]264 cmd = 'qemu-' + suffix
265
266 cmdline = cmd
[3692678]267 if 'qemu_path' in overrides.keys():
268 cmdline = overrides['qemu_path'] + cmd
269
[df64dbc]270 if options != '':
271 cmdline += ' ' + options
272
[84176f3]273 if (not 'hdd' in cfg.keys() or cfg['hdd']):
274 cmdline += qemu_bd_options()
[f5ceb18]275 if (not 'net' in cfg.keys()) or cfg['net']:
[df64dbc]276 cmdline += qemu_net_options()
[f5ceb18]277 if (not 'usb' in cfg.keys()) or cfg['usb']:
[df64dbc]278 cmdline += qemu_usb_options()
[5119d34]279 if (not 'xhci' in cfg.keys()) or cfg['xhci']:
280 cmdline += qemu_xhci_options()
[27de618]281 if (not 'tablet' in cfg.keys()) or cfg['tablet']:
282 cmdline += qemu_tablet_options()
[f5ceb18]283 if (not 'audio' in cfg.keys()) or cfg['audio']:
284 cmdline += qemu_audio_options()
[a35b458]285
[868d75c]286 console = ('console' in cfg.keys() and cfg['console'])
287
[0ceeac3]288 if (is_override('nographic')):
289 cmdline += ' -nographic'
290
[868d75c]291 if (not console and (not is_override('nographic')) and not is_override('noserial')):
[01552e3]292 cmdline += ' -serial stdio'
[2fc9bfd]293
[abf8bd8]294 if (is_override('bigmem')):
295 cmdline += ' -m 4G'
296
[f5ceb18]297 if cfg['image'] == 'image.iso':
[4b65f9a]298 cmdline += ' -boot d -cdrom image.iso'
[84176f3]299 elif cfg['image'] == 'image.iso@arm64':
300 # Define image.iso cdrom backend.
[4b65f9a]301 cmdline += ' -drive if=none,file=image.iso,id=cdrom,media=cdrom'
[84176f3]302 # Define scsi bus.
303 cmdline += ' -device virtio-scsi-device'
304 # Define cdrom frontend connected to this scsi bus.
305 cmdline += ' -device scsi-cd,drive=cdrom'
[f5ceb18]306 elif cfg['image'] == 'image.boot':
[4b65f9a]307 cmdline += ' -kernel image.boot'
[e9f7778]308 elif cfg['image'] == 'kernel.img@rpi':
309 cmdline += ' -bios boot/image.boot.bin'
[9185e42]310 else:
311 cmdline += ' ' + cfg['image']
[df64dbc]312
[868d75c]313 if console:
[df64dbc]314 cmdline += ' -nographic'
315
316 title = 'HelenOS/' + platform
317 if machine != '':
318 title += ' on ' + machine
[9185e42]319 if 'expect' in cfg.keys():
320 cmdline = 'expect -c \'spawn %s; expect "%s" { send "%s" } timeout exp_continue; interact\'' % (cmdline, cfg['expect']['src'], cfg['expect']['dst'])
[df64dbc]321 run_in_console(cmdline, title)
322 else:
[e4a1497]323 print(cmdline)
[f5ceb18]324 if not is_override('dryrun'):
325 subprocess.call(cmdline, shell = True)
[3f4c537a]326
[df425da]327def ski_run(platform, machine, processor):
[4b65f9a]328 run_in_console('ski -i ' + TOOLS_DIR + '/conf/ski.conf', 'HelenOS/ia64 on ski')
[df64dbc]329
[df425da]330def msim_run(platform, machine, processor):
[129b92c6]331 hdisk_mk()
[4b65f9a]332 run_in_console('msim -c ' + TOOLS_DIR + '/conf/msim.conf', 'HelenOS/mips32 on msim')
[df64dbc]333
[3f4c537a]334def spike_run(platform, machine, processor):
[4b65f9a]335 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
[3f4c537a]336
[f5ceb18]337emulators = {
338 'amd64' : {
339 'run' : qemu_run,
340 'image' : 'image.iso'
341 },
342 'arm32' : {
343 'integratorcp' : {
344 'run' : qemu_run,
345 'image' : 'image.boot',
346 'net' : False,
[a1a81f69]347 'audio' : False,
348 'xhci' : False,
349 'tablet' : False
[e9f7778]350 },
351 'raspberrypi' : {
352 'run' : qemu_run,
353 'image' : 'kernel.img@rpi',
354 'audio' : False,
355 'console' : True,
356 'hdd' : False,
357 'net' : False,
358 'tablet' : False,
359 'usb' : False,
360 'xhci' : False
361 },
[f5ceb18]362 },
[84176f3]363 'arm64' : {
364 'virt' : {
365 'run' : qemu_run,
366 'image' : 'image.iso@arm64',
367 'audio' : False,
[01552e3]368 'console' : True,
[84176f3]369 'hdd' : False,
370 'net' : False,
371 'tablet' : False,
372 'usb' : False,
373 'xhci' : False
374 }
375 },
[f5ceb18]376 'ia32' : {
377 'run' : qemu_run,
378 'image' : 'image.iso'
379 },
380 'ia64' : {
381 'ski' : {
382 'run' : ski_run
383 }
384 },
385 'mips32' : {
386 'msim' : {
387 'run' : msim_run
388 },
389 'lmalta' : {
390 'run' : qemu_run,
391 'image' : 'image.boot',
[868d75c]392 'console' : True
[f5ceb18]393 },
394 'bmalta' : {
395 'run' : qemu_run,
396 'image' : 'image.boot',
[868d75c]397 'console' : True
[f5ceb18]398 },
399 },
400 'ppc32' : {
401 'run' : qemu_run,
402 'image' : 'image.iso',
403 'audio' : False
404 },
[3f4c537a]405 'riscv64' : {
406 'run' : spike_run,
407 'image' : 'image.boot'
408 },
[f5ceb18]409 'sparc64' : {
410 'generic' : {
[df425da]411 'us' : {
412 'run' : qemu_run,
413 'image' : 'image.iso',
[0195374]414 'audio' : False,
[0436aec]415 'console' : is_override('nographic'),
[fd57cf17]416 'net' : False,
417 'usb' : False,
418 'xhci' : False,
419 'tablet' : False
[df425da]420 },
421 'sun4v' : {
[9185e42]422 'run' : qemu_run,
[4b65f9a]423 'image' : '-drive if=pflash,readonly=on,file=image.iso',
[9185e42]424 'audio' : False,
[868d75c]425 'console' : True,
[9185e42]426 'net' : False,
427 'usb' : False,
[fd57cf17]428 'xhci' : False,
429 'tablet' : False,
[9185e42]430 'expect' : {
431 'src' : 'ok ',
432 'dst' : 'boot\n'
433 },
[df425da]434 }
[f5ceb18]435 }
436 },
437}
438
439def usage():
440 print("%s - emulator wrapper for running HelenOS\n" % os.path.basename(sys.argv[0]))
[13eecc4]441 print("%s [-d] [-h] [-net e1k|rtl8139|ne2k|virtio-net] [-hdd ata|virtio-blk] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb] [-noxhci] [-notablet]\n" %
[f5ceb18]442 os.path.basename(sys.argv[0]))
443 print("-d\tDry run: do not run the emulation, just print the command line.")
444 print("-h\tPrint the usage information and exit.")
445 print("-nohdd\tDisable hard disk, if applicable.")
446 print("-nokvm\tDisable KVM, if applicable.")
447 print("-nonet\tDisable networking support, if applicable.")
448 print("-nosnd\tDisable sound, if applicable.")
449 print("-nousb\tDisable USB support, if applicable.")
[5119d34]450 print("-noxhci\tDisable XHCI support, if applicable.")
[27de618]451 print("-notablet\tDisable USB tablet (use only relative-position PS/2 mouse instead), if applicable.")
[abf8bd8]452 print("-nographic\tDisable graphical output. Serial port output must be enabled for this to be useful.")
[2fc9bfd]453 print("-noserial\tDisable serial port output in the terminal.")
[abf8bd8]454 print("-bigmem\tSets maximum RAM size to 4GB.")
[df64dbc]455
[df425da]456def fail(platform, machine):
457 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform, machine))
[a35b458]458
[df425da]459
[df64dbc]460def run():
[f5ceb18]461 expect_nic = False
[13eecc4]462 expect_hdd = False
[3692678]463 expect_qemu = False
[f5ceb18]464
465 for i in range(1, len(sys.argv)):
466
467 if expect_nic:
468 expect_nic = False
469 if not 'net' in overrides.keys():
470 overrides['net'] = {}
471 if sys.argv[i] == 'e1k':
472 overrides['net']['e1k'] = True
473 elif sys.argv[i] == 'rtl8139':
474 overrides['net']['rtl8139'] = True
475 elif sys.argv[i] == 'ne2k':
476 overrides['net']['ne2k'] = True
[7bf16b7e]477 elif sys.argv[i] == 'virtio-net':
478 overrides['net']['virtio-net'] = True
[f5ceb18]479 else:
480 usage()
481 exit()
[f134413]482 continue
[f5ceb18]483
[13eecc4]484 if expect_hdd:
485 expect_hdd = False
486 if not 'hdd' in overrides.keys():
487 overrides['hdd'] = {}
488 if sys.argv[i] == 'ata':
489 overrides['hdd']['ata'] = True
490 elif sys.argv[i] == 'virtio-blk':
491 overrides['hdd']['virtio-blk'] = True
492 else:
493 usage()
494 exit()
495 continue
496
[3692678]497 if expect_qemu:
498 expect_qemu = False
499 overrides['qemu_path'] = sys.argv[i]
500
[f5ceb18]501 elif sys.argv[i] == '-h':
502 usage()
503 exit()
504 elif sys.argv[i] == '-d':
505 overrides['dryrun'] = True
506 elif sys.argv[i] == '-net' and i < len(sys.argv) - 1:
507 expect_nic = True
[13eecc4]508 elif sys.argv[i] == '-hdd' and i < len(sys.argv) - 1:
509 expect_hdd = True
[f5ceb18]510 elif sys.argv[i] == '-nohdd':
511 overrides['nohdd'] = True
512 elif sys.argv[i] == '-nokvm':
513 overrides['nokvm'] = True
514 elif sys.argv[i] == '-nonet':
515 overrides['nonet'] = True
516 elif sys.argv[i] == '-nosnd':
517 overrides['nosnd'] = True
518 elif sys.argv[i] == '-nousb':
519 overrides['nousb'] = True
[5119d34]520 elif sys.argv[i] == '-noxhci':
521 overrides['noxhci'] = True
[27de618]522 elif sys.argv[i] == '-notablet':
523 overrides['notablet'] = True
[0ceeac3]524 elif sys.argv[i] == '-nographic':
525 overrides['nographic'] = True
[abf8bd8]526 elif sys.argv[i] == '-bigmem':
527 overrides['bigmem'] = True
[2fc9bfd]528 elif sys.argv[i] == '-noserial':
529 overrides['noserial'] = True
[3692678]530 elif sys.argv[i] == '-qemu_path' and i < len(sys.argv) - 1:
531 expect_qemu = True
[f5ceb18]532 else:
533 usage()
534 exit()
535
[1783f75]536 config = read_config()
[df64dbc]537
[f5ceb18]538 if 'PLATFORM' in config.keys():
[df64dbc]539 platform = config['PLATFORM']
[f5ceb18]540 else:
[df64dbc]541 platform = ''
542
[f5ceb18]543 if 'MACHINE' in config.keys():
[df64dbc]544 mach = config['MACHINE']
[f5ceb18]545 else:
[df64dbc]546 mach = ''
547
[df425da]548 if 'PROCESSOR' in config.keys():
549 processor = config['PROCESSOR']
550 else:
551 processor = ''
552
[df64dbc]553 try:
[df425da]554 emu_run = cfg_get(platform, mach, processor)['run']
555 emu_run(platform, mach, processor)
[df64dbc]556 except:
[df425da]557 fail(platform, mach)
[df64dbc]558 return
559
560run()
Note: See TracBrowser for help on using the repository browser.