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 cmdline
= termemu_detect() + ' -T ' + '"' + title
+ '"' + ' -e \'' + cmd
+ '\''
69 if not is_override('dryrun'):
70 subprocess
.call(cmdline
, shell
= True)
72 def get_host_native_width():
73 return int(platform
.architecture()[0].strip('bit'))
75 def pc_options(guest_width
):
78 # Do not enable KVM if running 64 bits HelenOS
80 host_width
= get_host_native_width()
81 if guest_width
<= host_width
and not is_override('nokvm'):
82 opts
= opts
+ ' -enable-kvm'
84 # Remove the leading space
90 def platform_to_qemu_options(platform
, machine
, processor
):
91 if platform
== 'amd64':
92 return 'system-x86_64', pc_options(64)
93 elif platform
== 'arm32':
94 return 'system-arm', '-M integratorcp'
95 elif platform
== 'ia32':
96 return 'system-i386', pc_options(32)
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':
103 return 'system-ppc', '-m 256'
104 elif platform
== 'sparc64':
105 if machine
!= 'generic':
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/'
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']
119 print("Cannot find OpenSPARC binary images!")
120 print("Either set OPENSPARC_BINARIES environment variable accordingly or place the images in %s." % (default_path
))
123 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins
)
127 if not os
.path
.exists('hdisk.img'):
128 subprocess
.call('tools/mkfat.py 1048576 uspace/dist/data hdisk.img', shell
= True)
130 def qemu_bd_options():
131 if is_override('nohdd'):
136 return ' -drive file=hdisk.img,index=0,media=disk,format=raw'
138 def qemu_nic_ne2k_options():
139 return ' -device ne2k_isa,irq=5,vlan=0'
141 def qemu_nic_e1k_options():
142 return ' -device e1000,vlan=0'
144 def qemu_nic_rtl8139_options():
145 return ' -device rtl8139,vlan=0'
147 def qemu_net_options():
148 if is_override('nonet'):
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()
160 # Use the default NIC
161 nic_options
+= qemu_nic_e1k_options()
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'
165 def qemu_usb_options():
166 if is_override('nousb'):
170 def qemu_audio_options():
171 if is_override('nosnd'):
173 return ' -device intel-hda -device hda-duplex'
175 def qemu_run(platform
, machine
, processor
):
176 cfg
= cfg_get(platform
, machine
, processor
)
177 suffix
, options
= platform_to_qemu_options(platform
, machine
, processor
)
178 cmd
= 'qemu-' + suffix
182 cmdline
+= ' ' + options
184 cmdline
+= qemu_bd_options()
186 if (not 'net' in cfg
.keys()) or cfg
['net']:
187 cmdline
+= qemu_net_options()
188 if (not 'usb' in cfg
.keys()) or cfg
['usb']:
189 cmdline
+= qemu_usb_options()
190 if (not 'audio' in cfg
.keys()) or cfg
['audio']:
191 cmdline
+= qemu_audio_options()
193 if cfg
['image'] == 'image.iso':
194 cmdline
+= ' -boot d -cdrom image.iso'
195 elif cfg
['image'] == 'image.boot':
196 cmdline
+= ' -kernel image.boot'
198 cmdline
+= ' ' + cfg
['image']
200 if ('console' in cfg
.keys()) and not cfg
['console']:
201 cmdline
+= ' -nographic'
203 title
= 'HelenOS/' + platform
205 title
+= ' on ' + machine
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'])
208 run_in_console(cmdline
, title
)
211 if not is_override('dryrun'):
212 subprocess
.call(cmdline
, shell
= True)
214 def ski_run(platform
, machine
, processor
):
215 run_in_console('ski -i contrib/conf/ski.conf', 'HelenOS/ia64 on ski')
217 def msim_run(platform
, machine
, processor
):
219 run_in_console('msim -c contrib/conf/msim.conf', 'HelenOS/mips32 on msim')
221 def spike_run(platform
, machine
, processor
):
222 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
227 'image' : 'image.iso'
232 'image' : 'image.boot',
239 'image' : 'image.iso'
252 'image' : 'image.boot',
257 'image' : 'image.boot',
263 'image' : 'image.iso',
268 'image' : 'image.boot'
274 'image' : 'image.iso',
280 'image' : '-drive if=pflash,readonly=on,file=image.iso',
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.")
306 def fail(platform
, machine
):
307 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform
, machine
))
313 for i
in range(1, len(sys
.argv
)):
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
329 elif sys
.argv
[i
] == '-h':
332 elif sys
.argv
[i
] == '-d':
333 overrides
['dryrun'] = True
334 elif sys
.argv
[i
] == '-net' and i
< len(sys
.argv
) - 1:
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
351 autotool
.read_config(autotool
.CONFIG
, config
)
353 if 'PLATFORM' in config
.keys():
354 platform
= config
['PLATFORM']
358 if 'MACHINE' in config
.keys():
359 mach
= config
['MACHINE']
363 if 'PROCESSOR' in config
.keys():
364 processor
= config
['PROCESSOR']
369 emu_run
= cfg_get(platform
, mach
, processor
)['run']
370 emu_run(platform
, mach
, processor
)