3 # Copyright (c) 2013 Jakub Jermar
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
32 Emulator wrapper for running HelenOS
46 if str in overrides
.keys():
50 def 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
]
56 return emulators
[platform
][machine
][processor
]
59 for termemu
in ['xfce4-terminal', 'xterm']:
61 subprocess
.check_output('which ' + termemu
, shell
= True)
66 def run_in_console(cmd
, title
):
67 ecmd
= cmd
.replace('"', '\\"')
68 cmdline
= termemu_detect() + ' -T ' + '"' + title
+ '"' + ' -e "' + ecmd
+ '"'
70 if not is_override('dryrun'):
71 subprocess
.call(cmdline
, shell
= True)
73 def get_host_native_width():
74 return int(platform
.architecture()[0].strip('bit'))
76 def pc_options(guest_width
):
79 # Do not enable KVM if running 64 bits HelenOS
81 host_width
= get_host_native_width()
82 if guest_width
<= host_width
and not is_override('nokvm'):
83 opts
= opts
+ ' -enable-kvm'
85 # Remove the leading space
91 def 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':
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/'
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']
120 print("Cannot find OpenSPARC binary images!")
121 print("Either set OPENSPARC_BINARIES environment variable accordingly or place the images in %s." % (default_path
))
124 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins
)
128 if not os
.path
.exists('hdisk.img'):
129 subprocess
.call('tools/mkfat.py 1048576 uspace/dist/data hdisk.img', shell
= True)
131 def qemu_bd_options():
132 if is_override('nohdd'):
137 return ' -drive file=hdisk.img,index=0,media=disk,format=raw'
139 def qemu_nic_ne2k_options():
140 return ' -device ne2k_isa,irq=5,netdev=n1'
142 def qemu_nic_e1k_options():
143 return ' -device e1000,netdev=n1'
145 def qemu_nic_rtl8139_options():
146 return ' -device rtl8139,netdev=n1'
148 def qemu_nic_virtio_options():
149 return ' -device virtio-net,netdev=n1'
151 def qemu_net_options():
152 if is_override('nonet'):
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()
166 # Use the default NIC
167 nic_options
+= qemu_nic_e1k_options()
169 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'
171 def qemu_usb_options():
172 if is_override('nousb'):
176 def qemu_xhci_options():
177 if is_override('noxhci'):
179 return ' -device nec-usb-xhci,id=xhci'
181 def qemu_tablet_options():
182 if is_override('notablet') or (is_override('nousb') and is_override('noxhci')):
184 return ' -device usb-tablet'
186 def qemu_audio_options():
187 if is_override('nosnd'):
189 return ' -device intel-hda -device hda-duplex'
191 def 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
197 if 'qemu_path' in overrides
.keys():
198 cmdline
= overrides
['qemu_path'] + cmd
201 cmdline
+= ' ' + options
203 cmdline
+= qemu_bd_options()
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()
216 console
= ('console' in cfg
.keys() and cfg
['console'])
218 if (is_override('nographic')):
219 cmdline
+= ' -nographic'
221 if (not console
and (not is_override('nographic')) and not is_override('noserial')):
222 cmdline
+= ' -serial stdio'
224 if (is_override('bigmem')):
227 if cfg
['image'] == 'image.iso':
228 cmdline
+= ' -boot d -cdrom image.iso'
229 elif cfg
['image'] == 'image.boot':
230 cmdline
+= ' -kernel image.boot'
232 cmdline
+= ' ' + cfg
['image']
235 cmdline
+= ' -nographic'
237 title
= 'HelenOS/' + platform
239 title
+= ' on ' + machine
240 if 'expect' in cfg
.keys():
241 cmdline
= 'expect -c \'spawn %s; expect "%s" { send "%s" } timeout exp_continue; interact\'' % (cmdline
, cfg
['expect']['src'], cfg
['expect']['dst'])
242 run_in_console(cmdline
, title
)
245 if not is_override('dryrun'):
246 subprocess
.call(cmdline
, shell
= True)
248 def ski_run(platform
, machine
, processor
):
249 run_in_console('ski -i tools/conf/ski.conf', 'HelenOS/ia64 on ski')
251 def msim_run(platform
, machine
, processor
):
253 run_in_console('msim -c tools/conf/msim.conf', 'HelenOS/mips32 on msim')
255 def spike_run(platform
, machine
, processor
):
256 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
261 'image' : 'image.iso'
266 'image' : 'image.boot',
275 'image' : 'image.iso'
288 'image' : 'image.boot',
293 'image' : 'image.boot',
299 'image' : 'image.iso',
304 'image' : 'image.boot'
310 'image' : 'image.iso',
320 'image' : '-drive if=pflash,readonly=on,file=image.iso',
337 print("%s - emulator wrapper for running HelenOS\n" % os
.path
.basename(sys
.argv
[0]))
338 print("%s [-d] [-h] [-net e1k|rtl8139|ne2k|virtio-net] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb] [-noxhci] [-notablet]\n" %
339 os
.path
.basename(sys
.argv
[0]))
340 print("-d\tDry run: do not run the emulation, just print the command line.")
341 print("-h\tPrint the usage information and exit.")
342 print("-nohdd\tDisable hard disk, if applicable.")
343 print("-nokvm\tDisable KVM, if applicable.")
344 print("-nonet\tDisable networking support, if applicable.")
345 print("-nosnd\tDisable sound, if applicable.")
346 print("-nousb\tDisable USB support, if applicable.")
347 print("-noxhci\tDisable XHCI support, if applicable.")
348 print("-notablet\tDisable USB tablet (use only relative-position PS/2 mouse instead), if applicable.")
349 print("-nographic\tDisable graphical output. Serial port output must be enabled for this to be useful.")
350 print("-noserial\tDisable serial port output in the terminal.")
351 print("-bigmem\tSets maximum RAM size to 4GB.")
353 def fail(platform
, machine
):
354 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform
, machine
))
361 for i
in range(1, len(sys
.argv
)):
365 if not 'net' in overrides
.keys():
366 overrides
['net'] = {}
367 if sys
.argv
[i
] == 'e1k':
368 overrides
['net']['e1k'] = True
369 elif sys
.argv
[i
] == 'rtl8139':
370 overrides
['net']['rtl8139'] = True
371 elif sys
.argv
[i
] == 'ne2k':
372 overrides
['net']['ne2k'] = True
373 elif sys
.argv
[i
] == 'virtio-net':
374 overrides
['net']['virtio-net'] = True
382 overrides
['qemu_path'] = sys
.argv
[i
]
384 elif sys
.argv
[i
] == '-h':
387 elif sys
.argv
[i
] == '-d':
388 overrides
['dryrun'] = True
389 elif sys
.argv
[i
] == '-net' and i
< len(sys
.argv
) - 1:
391 elif sys
.argv
[i
] == '-nohdd':
392 overrides
['nohdd'] = True
393 elif sys
.argv
[i
] == '-nokvm':
394 overrides
['nokvm'] = True
395 elif sys
.argv
[i
] == '-nonet':
396 overrides
['nonet'] = True
397 elif sys
.argv
[i
] == '-nosnd':
398 overrides
['nosnd'] = True
399 elif sys
.argv
[i
] == '-nousb':
400 overrides
['nousb'] = True
401 elif sys
.argv
[i
] == '-noxhci':
402 overrides
['noxhci'] = True
403 elif sys
.argv
[i
] == '-notablet':
404 overrides
['notablet'] = True
405 elif sys
.argv
[i
] == '-nographic':
406 overrides
['nographic'] = True
407 elif sys
.argv
[i
] == '-bigmem':
408 overrides
['bigmem'] = True
409 elif sys
.argv
[i
] == '-noserial':
410 overrides
['noserial'] = True
411 elif sys
.argv
[i
] == '-qemu_path' and i
< len(sys
.argv
) - 1:
418 autotool
.read_config(autotool
.CONFIG
, config
)
420 if 'PLATFORM' in config
.keys():
421 platform
= config
['PLATFORM']
425 if 'MACHINE' in config
.keys():
426 mach
= config
['MACHINE']
430 if 'PROCESSOR' in config
.keys():
431 processor
= config
['PROCESSOR']
436 emu_run
= cfg_get(platform
, mach
, processor
)['run']
437 emu_run(platform
, mach
, processor
)