source: mainline/tools/ew.py@ 3ae7827

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3ae7827 was 63d46341, checked in by Martin Decky <martin@…>, 7 years ago

move emulator configurations from contrib/ to tools/

Since the emulator configurations are used by tools/ew.py, they should
be in tools/ rather than contrib/.

  • Property mode set to 100755
File size: 11.9 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2013 Jakub Jermar
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
35import os
36import sys
37import subprocess
38import autotool
39import platform
40import thread
41import time
42
43overrides = {}
44
45def is_override(str):
46 if str in overrides.keys():
47 return overrides[str]
48 return False
49
50def cfg_get(platform, machine, processor):
51 if machine == "" or emulators[platform].has_key("run"):
52 return emulators[platform]
53 elif processor == "" or emulators[platform][machine].has_key("run"):
54 return emulators[platform][machine]
55 else:
56 return emulators[platform][machine][processor]
57
58def 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
66def run_in_console(cmd, title):
67 ecmd = cmd.replace('"', '\\"')
68 cmdline = termemu_detect() + ' -T ' + '"' + title + '"' + ' -e "' + ecmd + '"'
69 print(cmdline)
70 if not is_override('dryrun'):
71 subprocess.call(cmdline, shell = True)
72
73def get_host_native_width():
74 return int(platform.architecture()[0].strip('bit'))
75
76def pc_options(guest_width):
77 opts = ''
78
79 # Do not enable KVM if running 64 bits HelenOS
80 # on 32 bits host
81 host_width = get_host_native_width()
82 if guest_width <= host_width and not is_override('nokvm'):
83 opts = opts + ' -enable-kvm'
84
85 # Remove the leading space
86 return opts[1:]
87
88def malta_options():
89 return '-cpu 4Kc'
90
91def platform_to_qemu_options(platform, machine, processor):
92 if platform == 'amd64':
93 return 'system-x86_64', pc_options(64)
94 elif platform == 'arm32':
95 return 'system-arm', '-M integratorcp'
96 elif platform == 'ia32':
97 return 'system-i386', pc_options(32)
98 elif platform == 'mips32':
99 if machine == 'lmalta':
100 return 'system-mipsel', malta_options()
101 elif machine == 'bmalta':
102 return 'system-mips', malta_options()
103 elif platform == 'ppc32':
104 return 'system-ppc', '-m 256'
105 elif platform == 'sparc64':
106 if machine != 'generic':
107 raise Exception
108 if processor == 'us':
109 return 'system-sparc64', '-M sun4u --prom-env boot-args="console=devices/\\hw\\pci0\\01:01.0\\com1\\a"'
110 elif processor == 'sun4v':
111 default_path = '/usr/local/opensparc/image/'
112 try:
113 if os.path.exists(default_path):
114 opensparc_bins = default_path
115 elif os.path.exists(os.environ['OPENSPARC_BINARIES']):
116 opensparc_bins = os.environ['OPENSPARC_BINARIES']
117 else:
118 raise Exception
119 except:
120 print("Cannot find OpenSPARC binary images!")
121 print("Either set OPENSPARC_BINARIES environment variable accordingly or place the images in %s." % (default_path))
122 raise Exception
123
124 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins)
125
126
127def hdisk_mk():
128 if not os.path.exists('hdisk.img'):
129 subprocess.call('tools/mkfat.py 1048576 uspace/dist/data hdisk.img', shell = True)
130
131def qemu_bd_options():
132 if is_override('nohdd'):
133 return ''
134
135 hdisk_mk()
136
137 return ' -drive file=hdisk.img,index=0,media=disk,format=raw'
138
139def qemu_nic_ne2k_options():
140 return ' -device ne2k_isa,irq=5,vlan=0'
141
142def qemu_nic_e1k_options():
143 return ' -device e1000,vlan=0'
144
145def qemu_nic_rtl8139_options():
146 return ' -device rtl8139,vlan=0'
147
148def qemu_nic_virtio_options():
149 return ' -device virtio-net,vlan=0'
150
151def qemu_net_options():
152 if is_override('nonet'):
153 return ''
154
155 nic_options = ''
156 if 'net' in overrides.keys():
157 if 'e1k' in overrides['net'].keys():
158 nic_options += qemu_nic_e1k_options()
159 if 'rtl8139' in overrides['net'].keys():
160 nic_options += qemu_nic_rtl8139_options()
161 if 'ne2k' in overrides['net'].keys():
162 nic_options += qemu_nic_ne2k_options()
163 if 'virtio-net' in overrides['net'].keys():
164 nic_options += qemu_nic_virtio_options()
165 else:
166 # Use the default NIC
167 nic_options += qemu_nic_e1k_options()
168
169 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'
170
171def qemu_usb_options():
172 if is_override('nousb'):
173 return ''
174 return ' -usb'
175
176def qemu_xhci_options():
177 if is_override('noxhci'):
178 return ''
179 return ' -device nec-usb-xhci,id=xhci'
180
181def qemu_tablet_options():
182 if is_override('notablet') or (is_override('nousb') and is_override('noxhci')):
183 return ''
184 return ' -device usb-tablet'
185
186def qemu_audio_options():
187 if is_override('nosnd'):
188 return ''
189 return ' -device intel-hda -device hda-duplex'
190
191def qemu_run(platform, machine, processor):
192 cfg = cfg_get(platform, machine, processor)
193 suffix, options = platform_to_qemu_options(platform, machine, processor)
194 cmd = 'qemu-' + suffix
195
196 cmdline = cmd
197 if 'qemu_path' in overrides.keys():
198 cmdline = overrides['qemu_path'] + cmd
199
200 if options != '':
201 cmdline += ' ' + options
202
203 cmdline += qemu_bd_options()
204
205 if (not 'net' in cfg.keys()) or cfg['net']:
206 cmdline += qemu_net_options()
207 if (not 'usb' in cfg.keys()) or cfg['usb']:
208 cmdline += qemu_usb_options()
209 if (not 'xhci' in cfg.keys()) or cfg['xhci']:
210 cmdline += qemu_xhci_options()
211 if (not 'tablet' in cfg.keys()) or cfg['tablet']:
212 cmdline += qemu_tablet_options()
213 if (not 'audio' in cfg.keys()) or cfg['audio']:
214 cmdline += qemu_audio_options()
215
216 if (is_override('nographic')):
217 cmdline += ' -nographic'
218
219 if ((not is_override('nographic')) and not is_override('noserial')):
220 cmdline += ' -serial stdio'
221
222 if (is_override('bigmem')):
223 cmdline += ' -m 4G'
224
225 if cfg['image'] == 'image.iso':
226 cmdline += ' -boot d -cdrom image.iso'
227 elif cfg['image'] == 'image.boot':
228 cmdline += ' -kernel image.boot'
229 else:
230 cmdline += ' ' + cfg['image']
231
232 if ('console' in cfg.keys()) and not cfg['console']:
233 cmdline += ' -nographic'
234
235 title = 'HelenOS/' + platform
236 if machine != '':
237 title += ' on ' + machine
238 if 'expect' in cfg.keys():
239 cmdline = 'expect -c \'spawn %s; expect "%s" { send "%s" } timeout exp_continue; interact\'' % (cmdline, cfg['expect']['src'], cfg['expect']['dst'])
240 run_in_console(cmdline, title)
241 else:
242 print(cmdline)
243 if not is_override('dryrun'):
244 subprocess.call(cmdline, shell = True)
245
246def ski_run(platform, machine, processor):
247 run_in_console('ski -i tools/conf/ski.conf', 'HelenOS/ia64 on ski')
248
249def msim_run(platform, machine, processor):
250 hdisk_mk()
251 run_in_console('msim -c tools/conf/msim.conf', 'HelenOS/mips32 on msim')
252
253def spike_run(platform, machine, processor):
254 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
255
256emulators = {
257 'amd64' : {
258 'run' : qemu_run,
259 'image' : 'image.iso'
260 },
261 'arm32' : {
262 'integratorcp' : {
263 'run' : qemu_run,
264 'image' : 'image.boot',
265 'net' : False,
266 'audio' : False,
267 'xhci' : False,
268 'tablet' : False
269 }
270 },
271 'ia32' : {
272 'run' : qemu_run,
273 'image' : 'image.iso'
274 },
275 'ia64' : {
276 'ski' : {
277 'run' : ski_run
278 }
279 },
280 'mips32' : {
281 'msim' : {
282 'run' : msim_run
283 },
284 'lmalta' : {
285 'run' : qemu_run,
286 'image' : 'image.boot',
287 'console' : False
288 },
289 'bmalta' : {
290 'run' : qemu_run,
291 'image' : 'image.boot',
292 'console' : False
293 },
294 },
295 'ppc32' : {
296 'run' : qemu_run,
297 'image' : 'image.iso',
298 'audio' : False
299 },
300 'riscv64' : {
301 'run' : spike_run,
302 'image' : 'image.boot'
303 },
304 'sparc64' : {
305 'generic' : {
306 'us' : {
307 'run' : qemu_run,
308 'image' : 'image.iso',
309 'audio' : False,
310 'console' : False,
311 'net' : False,
312 'usb' : False,
313 'xhci' : False,
314 'tablet' : False
315 },
316 'sun4v' : {
317 'run' : qemu_run,
318 'image' : '-drive if=pflash,readonly=on,file=image.iso',
319 'audio' : False,
320 'console' : False,
321 'net' : False,
322 'usb' : False,
323 'xhci' : False,
324 'tablet' : False,
325 'expect' : {
326 'src' : 'ok ',
327 'dst' : 'boot\n'
328 },
329 }
330 }
331 },
332}
333
334def usage():
335 print("%s - emulator wrapper for running HelenOS\n" % os.path.basename(sys.argv[0]))
336 print("%s [-d] [-h] [-net e1k|rtl8139|ne2k|virtio-net] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb] [-noxhci] [-notablet]\n" %
337 os.path.basename(sys.argv[0]))
338 print("-d\tDry run: do not run the emulation, just print the command line.")
339 print("-h\tPrint the usage information and exit.")
340 print("-nohdd\tDisable hard disk, if applicable.")
341 print("-nokvm\tDisable KVM, if applicable.")
342 print("-nonet\tDisable networking support, if applicable.")
343 print("-nosnd\tDisable sound, if applicable.")
344 print("-nousb\tDisable USB support, if applicable.")
345 print("-noxhci\tDisable XHCI support, if applicable.")
346 print("-notablet\tDisable USB tablet (use only relative-position PS/2 mouse instead), if applicable.")
347 print("-nographic\tDisable graphical output. Serial port output must be enabled for this to be useful.")
348 print("-noserial\tDisable serial port output in the terminal.")
349 print("-bigmem\tSets maximum RAM size to 4GB.")
350
351def fail(platform, machine):
352 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform, machine))
353
354
355def run():
356 expect_nic = False
357 expect_qemu = False
358
359 for i in range(1, len(sys.argv)):
360
361 if expect_nic:
362 expect_nic = False
363 if not 'net' in overrides.keys():
364 overrides['net'] = {}
365 if sys.argv[i] == 'e1k':
366 overrides['net']['e1k'] = True
367 elif sys.argv[i] == 'rtl8139':
368 overrides['net']['rtl8139'] = True
369 elif sys.argv[i] == 'ne2k':
370 overrides['net']['ne2k'] = True
371 elif sys.argv[i] == 'virtio-net':
372 overrides['net']['virtio-net'] = True
373 else:
374 usage()
375 exit()
376 continue
377
378 if expect_qemu:
379 expect_qemu = False
380 overrides['qemu_path'] = sys.argv[i]
381
382 elif sys.argv[i] == '-h':
383 usage()
384 exit()
385 elif sys.argv[i] == '-d':
386 overrides['dryrun'] = True
387 elif sys.argv[i] == '-net' and i < len(sys.argv) - 1:
388 expect_nic = True
389 elif sys.argv[i] == '-nohdd':
390 overrides['nohdd'] = True
391 elif sys.argv[i] == '-nokvm':
392 overrides['nokvm'] = True
393 elif sys.argv[i] == '-nonet':
394 overrides['nonet'] = True
395 elif sys.argv[i] == '-nosnd':
396 overrides['nosnd'] = True
397 elif sys.argv[i] == '-nousb':
398 overrides['nousb'] = True
399 elif sys.argv[i] == '-noxhci':
400 overrides['noxhci'] = True
401 elif sys.argv[i] == '-notablet':
402 overrides['notablet'] = True
403 elif sys.argv[i] == '-nographic':
404 overrides['nographic'] = True
405 elif sys.argv[i] == '-bigmem':
406 overrides['bigmem'] = True
407 elif sys.argv[i] == '-noserial':
408 overrides['noserial'] = True
409 elif sys.argv[i] == '-qemu_path' and i < len(sys.argv) - 1:
410 expect_qemu = True
411 else:
412 usage()
413 exit()
414
415 config = {}
416 autotool.read_config(autotool.CONFIG, config)
417
418 if 'PLATFORM' in config.keys():
419 platform = config['PLATFORM']
420 else:
421 platform = ''
422
423 if 'MACHINE' in config.keys():
424 mach = config['MACHINE']
425 else:
426 mach = ''
427
428 if 'PROCESSOR' in config.keys():
429 processor = config['PROCESSOR']
430 else:
431 processor = ''
432
433 try:
434 emu_run = cfg_get(platform, mach, processor)['run']
435 emu_run(platform, mach, processor)
436 except:
437 fail(platform, mach)
438 return
439
440run()
Note: See TracBrowser for help on using the repository browser.