[df64dbc] | 1 | #!/usr/bin/env python
|
---|
| 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 | """
|
---|
| 32 | Emulator wrapper for running HelenOS
|
---|
| 33 | """
|
---|
| 34 |
|
---|
[1c24c7c] | 35 | import os
|
---|
[f5ceb18] | 36 | import sys
|
---|
[1c24c7c] | 37 | import subprocess
|
---|
[df64dbc] | 38 | import autotool
|
---|
[8a26f82] | 39 | import platform
|
---|
[df425da] | 40 | import thread
|
---|
| 41 | import time
|
---|
[df64dbc] | 42 |
|
---|
[f5ceb18] | 43 | overrides = {}
|
---|
| 44 |
|
---|
| 45 | def is_override(str):
|
---|
| 46 | if str in overrides.keys():
|
---|
| 47 | return overrides[str]
|
---|
| 48 | return False
|
---|
| 49 |
|
---|
[df425da] | 50 | def cfg_get(platform, machine, processor):
|
---|
| 51 | if machine == "" or emulators[platform].has_key("run"):
|
---|
[f5ceb18] | 52 | return emulators[platform]
|
---|
[df425da] | 53 | elif processor == "" or emulators[platform][machine].has_key("run"):
|
---|
[f5ceb18] | 54 | return emulators[platform][machine]
|
---|
[df425da] | 55 | else:
|
---|
| 56 | return emulators[platform][machine][processor]
|
---|
[f5ceb18] | 57 |
|
---|
[e4c8e3cf] | 58 | def termemu_detect():
|
---|
| 59 | for termemu in ['xfce4-terminal', 'xterm']:
|
---|
| 60 | try:
|
---|
| 61 | subprocess.check_output('which ' + termemu, shell = True)
|
---|
| 62 | return termemu
|
---|
| 63 | except:
|
---|
| 64 | pass
|
---|
| 65 |
|
---|
[df64dbc] | 66 | def run_in_console(cmd, title):
|
---|
[e4c8e3cf] | 67 | cmdline = termemu_detect() + ' -T ' + '"' + title + '"' + ' -e \'' + cmd + '\''
|
---|
[e4a1497] | 68 | print(cmdline)
|
---|
[f5ceb18] | 69 | if not is_override('dryrun'):
|
---|
[e4c8e3cf] | 70 | subprocess.call(cmdline, shell = True)
|
---|
[df64dbc] | 71 |
|
---|
[8a26f82] | 72 | def get_host_native_width():
|
---|
| 73 | return int(platform.architecture()[0].strip('bit'))
|
---|
| 74 |
|
---|
| 75 | def pc_options(guest_width):
|
---|
| 76 | opts = ''
|
---|
| 77 |
|
---|
| 78 | # Do not enable KVM if running 64 bits HelenOS
|
---|
| 79 | # on 32 bits host
|
---|
| 80 | host_width = get_host_native_width()
|
---|
[f5ceb18] | 81 | if guest_width <= host_width and not is_override('nokvm'):
|
---|
[8a26f82] | 82 | opts = opts + ' -enable-kvm'
|
---|
| 83 |
|
---|
| 84 | # Remove the leading space
|
---|
| 85 | return opts[1:]
|
---|
[df64dbc] | 86 |
|
---|
| 87 | def malta_options():
|
---|
| 88 | return '-cpu 4Kc'
|
---|
| 89 |
|
---|
[9185e42] | 90 | def platform_to_qemu_options(platform, machine, processor):
|
---|
[df64dbc] | 91 | if platform == 'amd64':
|
---|
[8a26f82] | 92 | return 'system-x86_64', pc_options(64)
|
---|
[df64dbc] | 93 | elif platform == 'arm32':
|
---|
[83b01c2] | 94 | return 'system-arm', '-M integratorcp'
|
---|
[df64dbc] | 95 | elif platform == 'ia32':
|
---|
[8a26f82] | 96 | return 'system-i386', pc_options(32)
|
---|
[df64dbc] | 97 | elif platform == 'mips32':
|
---|
| 98 | if machine == 'lmalta':
|
---|
| 99 | return 'system-mipsel', malta_options()
|
---|
| 100 | elif machine == 'bmalta':
|
---|
| 101 | return 'system-mips', malta_options()
|
---|
| 102 | elif platform == 'ppc32':
|
---|
[644352c] | 103 | return 'system-ppc', '-m 256'
|
---|
[df64dbc] | 104 | elif platform == 'sparc64':
|
---|
[9185e42] | 105 | if machine != 'generic':
|
---|
| 106 | raise Exception
|
---|
| 107 | if processor == 'us':
|
---|
| 108 | return 'system-sparc64', '-M sun4u --prom-env boot-args="console=devices/\\hw\\pci0\\00:03.0\\com1\\a"'
|
---|
| 109 | elif processor == 'sun4v':
|
---|
| 110 | default_path = '/usr/local/opensparc/image/'
|
---|
| 111 | try:
|
---|
| 112 | if os.path.exists(default_path):
|
---|
| 113 | opensparc_bins = default_path
|
---|
| 114 | elif os.path.exists(os.environ['OPENSPARC_BINARIES']):
|
---|
| 115 | opensparc_bins = os.environ['OPENSPARC_BINARIES']
|
---|
| 116 | else:
|
---|
| 117 | raise Exception
|
---|
| 118 | except:
|
---|
| 119 | print("Cannot find OpenSPARC binary images!")
|
---|
| 120 | print("Either set OPENSPARC_BINARIES environment variable accordingly or place the images in %s." % (default_path))
|
---|
| 121 | raise Exception
|
---|
| 122 |
|
---|
| 123 | return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins)
|
---|
| 124 |
|
---|
[df64dbc] | 125 |
|
---|
[129b92c6] | 126 | def hdisk_mk():
|
---|
[df64dbc] | 127 | if not os.path.exists('hdisk.img'):
|
---|
| 128 | subprocess.call('tools/mkfat.py 1048576 uspace/dist/data hdisk.img', shell = True)
|
---|
[f5ceb18] | 129 |
|
---|
[129b92c6] | 130 | def qemu_bd_options():
|
---|
| 131 | if is_override('nohdd'):
|
---|
| 132 | return ''
|
---|
| 133 |
|
---|
| 134 | hdisk_mk()
|
---|
| 135 |
|
---|
[5fdad22] | 136 | return ' -drive file=hdisk.img,index=0,media=disk,format=raw'
|
---|
[df64dbc] | 137 |
|
---|
| 138 | def qemu_nic_ne2k_options():
|
---|
| 139 | return ' -device ne2k_isa,irq=5,vlan=0'
|
---|
| 140 |
|
---|
| 141 | def qemu_nic_e1k_options():
|
---|
| 142 | return ' -device e1000,vlan=0'
|
---|
| 143 |
|
---|
| 144 | def qemu_nic_rtl8139_options():
|
---|
| 145 | return ' -device rtl8139,vlan=0'
|
---|
| 146 |
|
---|
| 147 | def qemu_net_options():
|
---|
[f5ceb18] | 148 | if is_override('nonet'):
|
---|
| 149 | return ''
|
---|
| 150 |
|
---|
| 151 | nic_options = ''
|
---|
| 152 | if 'net' in overrides.keys():
|
---|
| 153 | if 'e1k' in overrides['net'].keys():
|
---|
| 154 | nic_options += qemu_nic_e1k_options()
|
---|
| 155 | if 'rtl8139' in overrides['net'].keys():
|
---|
| 156 | nic_options += qemu_nic_rtl8139_options()
|
---|
| 157 | if 'ne2k' in overrides['net'].keys():
|
---|
| 158 | nic_options += qemu_nic_ne2k_options()
|
---|
| 159 | else:
|
---|
| 160 | # Use the default NIC
|
---|
| 161 | nic_options += qemu_nic_e1k_options()
|
---|
| 162 |
|
---|
[362654a] | 163 | return nic_options + ' -net user,hostfwd=udp::8080-:8080,hostfwd=udp::8081-:8081,hostfwd=tcp::8080-:8080,hostfwd=tcp::8081-:8081,hostfwd=tcp::2223-:2223'
|
---|
[df64dbc] | 164 |
|
---|
| 165 | def qemu_usb_options():
|
---|
[f5ceb18] | 166 | if is_override('nousb'):
|
---|
| 167 | return ''
|
---|
| 168 | return ' -usb'
|
---|
[df64dbc] | 169 |
|
---|
[f5ceb18] | 170 | def qemu_audio_options():
|
---|
| 171 | if is_override('nosnd'):
|
---|
| 172 | return ''
|
---|
[089901e] | 173 | return ' -device intel-hda -device hda-duplex'
|
---|
[f5ceb18] | 174 |
|
---|
[df425da] | 175 | def qemu_run(platform, machine, processor):
|
---|
| 176 | cfg = cfg_get(platform, machine, processor)
|
---|
[9185e42] | 177 | suffix, options = platform_to_qemu_options(platform, machine, processor)
|
---|
[df64dbc] | 178 | cmd = 'qemu-' + suffix
|
---|
| 179 |
|
---|
| 180 | cmdline = cmd
|
---|
| 181 | if options != '':
|
---|
| 182 | cmdline += ' ' + options
|
---|
| 183 |
|
---|
[f5ceb18] | 184 | cmdline += qemu_bd_options()
|
---|
| 185 |
|
---|
| 186 | if (not 'net' in cfg.keys()) or cfg['net']:
|
---|
[df64dbc] | 187 | cmdline += qemu_net_options()
|
---|
[f5ceb18] | 188 | if (not 'usb' in cfg.keys()) or cfg['usb']:
|
---|
[df64dbc] | 189 | cmdline += qemu_usb_options()
|
---|
[f5ceb18] | 190 | if (not 'audio' in cfg.keys()) or cfg['audio']:
|
---|
| 191 | cmdline += qemu_audio_options()
|
---|
[df64dbc] | 192 |
|
---|
[f5ceb18] | 193 | if cfg['image'] == 'image.iso':
|
---|
[df64dbc] | 194 | cmdline += ' -boot d -cdrom image.iso'
|
---|
[f5ceb18] | 195 | elif cfg['image'] == 'image.boot':
|
---|
[df64dbc] | 196 | cmdline += ' -kernel image.boot'
|
---|
[9185e42] | 197 | else:
|
---|
| 198 | cmdline += ' ' + cfg['image']
|
---|
[df64dbc] | 199 |
|
---|
[f5ceb18] | 200 | if ('console' in cfg.keys()) and not cfg['console']:
|
---|
[df64dbc] | 201 | cmdline += ' -nographic'
|
---|
| 202 |
|
---|
| 203 | title = 'HelenOS/' + platform
|
---|
| 204 | if machine != '':
|
---|
| 205 | title += ' on ' + machine
|
---|
[9185e42] | 206 | if 'expect' in cfg.keys():
|
---|
| 207 | cmdline = 'expect -c \'spawn %s; expect "%s" { send "%s" } timeout exp_continue; interact\'' % (cmdline, cfg['expect']['src'], cfg['expect']['dst'])
|
---|
[df64dbc] | 208 | run_in_console(cmdline, title)
|
---|
| 209 | else:
|
---|
[e4a1497] | 210 | print(cmdline)
|
---|
[f5ceb18] | 211 | if not is_override('dryrun'):
|
---|
| 212 | subprocess.call(cmdline, shell = True)
|
---|
[3f4c537a] | 213 |
|
---|
[df425da] | 214 | def ski_run(platform, machine, processor):
|
---|
[df64dbc] | 215 | run_in_console('ski -i contrib/conf/ski.conf', 'HelenOS/ia64 on ski')
|
---|
| 216 |
|
---|
[df425da] | 217 | def msim_run(platform, machine, processor):
|
---|
[129b92c6] | 218 | hdisk_mk()
|
---|
[df64dbc] | 219 | run_in_console('msim -c contrib/conf/msim.conf', 'HelenOS/mips32 on msim')
|
---|
| 220 |
|
---|
[3f4c537a] | 221 | def spike_run(platform, machine, processor):
|
---|
[ae8d7b0] | 222 | run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
|
---|
[3f4c537a] | 223 |
|
---|
[f5ceb18] | 224 | emulators = {
|
---|
| 225 | 'amd64' : {
|
---|
| 226 | 'run' : qemu_run,
|
---|
| 227 | 'image' : 'image.iso'
|
---|
| 228 | },
|
---|
| 229 | 'arm32' : {
|
---|
| 230 | 'integratorcp' : {
|
---|
| 231 | 'run' : qemu_run,
|
---|
| 232 | 'image' : 'image.boot',
|
---|
| 233 | 'net' : False,
|
---|
| 234 | 'audio' : False
|
---|
| 235 | }
|
---|
| 236 | },
|
---|
| 237 | 'ia32' : {
|
---|
| 238 | 'run' : qemu_run,
|
---|
| 239 | 'image' : 'image.iso'
|
---|
| 240 | },
|
---|
| 241 | 'ia64' : {
|
---|
| 242 | 'ski' : {
|
---|
| 243 | 'run' : ski_run
|
---|
| 244 | }
|
---|
| 245 | },
|
---|
| 246 | 'mips32' : {
|
---|
| 247 | 'msim' : {
|
---|
| 248 | 'run' : msim_run
|
---|
| 249 | },
|
---|
| 250 | 'lmalta' : {
|
---|
| 251 | 'run' : qemu_run,
|
---|
| 252 | 'image' : 'image.boot',
|
---|
| 253 | 'console' : False
|
---|
| 254 | },
|
---|
| 255 | 'bmalta' : {
|
---|
| 256 | 'run' : qemu_run,
|
---|
| 257 | 'image' : 'image.boot',
|
---|
| 258 | 'console' : False
|
---|
| 259 | },
|
---|
| 260 | },
|
---|
| 261 | 'ppc32' : {
|
---|
| 262 | 'run' : qemu_run,
|
---|
| 263 | 'image' : 'image.iso',
|
---|
| 264 | 'audio' : False
|
---|
| 265 | },
|
---|
[3f4c537a] | 266 | 'riscv64' : {
|
---|
| 267 | 'run' : spike_run,
|
---|
| 268 | 'image' : 'image.boot'
|
---|
| 269 | },
|
---|
[f5ceb18] | 270 | 'sparc64' : {
|
---|
| 271 | 'generic' : {
|
---|
[df425da] | 272 | 'us' : {
|
---|
| 273 | 'run' : qemu_run,
|
---|
| 274 | 'image' : 'image.iso',
|
---|
[0195374] | 275 | 'audio' : False,
|
---|
| 276 | 'console' : False,
|
---|
[df425da] | 277 | },
|
---|
| 278 | 'sun4v' : {
|
---|
[9185e42] | 279 | 'run' : qemu_run,
|
---|
| 280 | 'image' : '-drive if=pflash,readonly=on,file=image.iso',
|
---|
| 281 | 'audio' : False,
|
---|
| 282 | 'console' : False,
|
---|
| 283 | 'net' : False,
|
---|
| 284 | 'usb' : False,
|
---|
| 285 | 'expect' : {
|
---|
| 286 | 'src' : 'ok ',
|
---|
| 287 | 'dst' : 'boot\n'
|
---|
| 288 | },
|
---|
[df425da] | 289 | }
|
---|
[f5ceb18] | 290 | }
|
---|
| 291 | },
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | def usage():
|
---|
| 295 | print("%s - emulator wrapper for running HelenOS\n" % os.path.basename(sys.argv[0]))
|
---|
| 296 | print("%s [-d] [-h] [-net e1k|rtl8139|ne2k] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb]\n" %
|
---|
| 297 | os.path.basename(sys.argv[0]))
|
---|
| 298 | print("-d\tDry run: do not run the emulation, just print the command line.")
|
---|
| 299 | print("-h\tPrint the usage information and exit.")
|
---|
| 300 | print("-nohdd\tDisable hard disk, if applicable.")
|
---|
| 301 | print("-nokvm\tDisable KVM, if applicable.")
|
---|
| 302 | print("-nonet\tDisable networking support, if applicable.")
|
---|
| 303 | print("-nosnd\tDisable sound, if applicable.")
|
---|
| 304 | print("-nousb\tDisable USB support, if applicable.")
|
---|
[df64dbc] | 305 |
|
---|
[df425da] | 306 | def fail(platform, machine):
|
---|
| 307 | print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform, machine))
|
---|
| 308 |
|
---|
| 309 |
|
---|
[df64dbc] | 310 | def run():
|
---|
[f5ceb18] | 311 | expect_nic = False
|
---|
| 312 |
|
---|
| 313 | for i in range(1, len(sys.argv)):
|
---|
| 314 |
|
---|
| 315 | if expect_nic:
|
---|
| 316 | expect_nic = False
|
---|
| 317 | if not 'net' in overrides.keys():
|
---|
| 318 | overrides['net'] = {}
|
---|
| 319 | if sys.argv[i] == 'e1k':
|
---|
| 320 | overrides['net']['e1k'] = True
|
---|
| 321 | elif sys.argv[i] == 'rtl8139':
|
---|
| 322 | overrides['net']['rtl8139'] = True
|
---|
| 323 | elif sys.argv[i] == 'ne2k':
|
---|
| 324 | overrides['net']['ne2k'] = True
|
---|
| 325 | else:
|
---|
| 326 | usage()
|
---|
| 327 | exit()
|
---|
| 328 |
|
---|
| 329 | elif sys.argv[i] == '-h':
|
---|
| 330 | usage()
|
---|
| 331 | exit()
|
---|
| 332 | elif sys.argv[i] == '-d':
|
---|
| 333 | overrides['dryrun'] = True
|
---|
| 334 | elif sys.argv[i] == '-net' and i < len(sys.argv) - 1:
|
---|
| 335 | expect_nic = True
|
---|
| 336 | elif sys.argv[i] == '-nohdd':
|
---|
| 337 | overrides['nohdd'] = True
|
---|
| 338 | elif sys.argv[i] == '-nokvm':
|
---|
| 339 | overrides['nokvm'] = True
|
---|
| 340 | elif sys.argv[i] == '-nonet':
|
---|
| 341 | overrides['nonet'] = True
|
---|
| 342 | elif sys.argv[i] == '-nosnd':
|
---|
| 343 | overrides['nosnd'] = True
|
---|
| 344 | elif sys.argv[i] == '-nousb':
|
---|
| 345 | overrides['nousb'] = True
|
---|
| 346 | else:
|
---|
| 347 | usage()
|
---|
| 348 | exit()
|
---|
| 349 |
|
---|
[df64dbc] | 350 | config = {}
|
---|
| 351 | autotool.read_config(autotool.CONFIG, config)
|
---|
| 352 |
|
---|
[f5ceb18] | 353 | if 'PLATFORM' in config.keys():
|
---|
[df64dbc] | 354 | platform = config['PLATFORM']
|
---|
[f5ceb18] | 355 | else:
|
---|
[df64dbc] | 356 | platform = ''
|
---|
| 357 |
|
---|
[f5ceb18] | 358 | if 'MACHINE' in config.keys():
|
---|
[df64dbc] | 359 | mach = config['MACHINE']
|
---|
[f5ceb18] | 360 | else:
|
---|
[df64dbc] | 361 | mach = ''
|
---|
| 362 |
|
---|
[df425da] | 363 | if 'PROCESSOR' in config.keys():
|
---|
| 364 | processor = config['PROCESSOR']
|
---|
| 365 | else:
|
---|
| 366 | processor = ''
|
---|
| 367 |
|
---|
[df64dbc] | 368 | try:
|
---|
[df425da] | 369 | emu_run = cfg_get(platform, mach, processor)['run']
|
---|
| 370 | emu_run(platform, mach, processor)
|
---|
[df64dbc] | 371 | except:
|
---|
[df425da] | 372 | fail(platform, mach)
|
---|
[df64dbc] | 373 | return
|
---|
| 374 |
|
---|
| 375 | run()
|
---|