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