91 std::shared_ptr<gs::tlm_quantumkeeper_extended> m_first_qk = NULL;
93 std::list<QemuDeviceBaseIF*> devices;
94 cci::cci_broker_handle m_conf_broker;
96 bool m_running =
false;
103 std::mutex g_signaled_lock;
104 std::condition_variable g_signaled_cond;
105 bool g_signaled =
false;
106 std::recursive_mutex g_rec_qemu_io_lock;
110 std::lock_guard<std::mutex> lock(m_lock);
111 devices.push_back(d);
116 std::lock_guard<std::mutex> lock(m_lock);
122 if (!m_running)
return false;
123 std::lock_guard<std::mutex> lock(m_lock);
124 bool can_run =
false;
126 for (
auto const& i : devices) {
134 using Target = qemu::Target;
143 TcgMode StringToTcgMode(std::string s)
145 if (s ==
"SINGLE")
return TCG_SINGLE;
146 if (s ==
"COROUTINE")
return TCG_COROUTINE;
147 if (s ==
"MULTI")
return TCG_MULTI;
148 SCP_WARN(()) <<
"Unknown TCG mode " << s;
149 return TCG_UNSPECIFIED;
152 enum TimeSyncStrategy {
153 TIME_SYNC_QUANTUM_KEEPER,
156 TimeSyncStrategy StringToTimeSyncStrategy(std::string s)
158 if (s ==
"quantum_keeper")
return TIME_SYNC_QUANTUM_KEEPER;
159 if (s ==
"mcips")
return TIME_SYNC_MCIPS;
160 SCP_FATAL(()) <<
"Unknown time_sync_strategy '" << s <<
"' (expected \"quantum_keeper\" or \"mcips\")";
162 return TIME_SYNC_QUANTUM_KEEPER;
166 cci::cci_param<std::string> p_time_sync_strategy;
167 std::unique_ptr<McipsPlugin> m_mcips_plugin;
172 cci::cci_param<std::string> p_tcg_mode;
173 cci::cci_param<std::string> p_sync_policy;
176 cci::cci_param<bool> p_icount;
177 cci::cci_param<int> p_icount_mips;
179 cci::cci_param<std::string> p_args;
180 std::string m_display_argument;
182 cci::cci_param<std::string> p_accel;
183 cci::cci_param<std::string> p_whpx_args;
185 void push_default_args()
187 const size_t l = strlen(name()) + 1;
189 m_inst.push_qemu_arg(
"libqbox");
190 m_inst.push_qemu_arg({
197 const char* args =
"qemu_args.";
198 for (
auto p : m_conf_broker.get_unconsumed_preset_values([&](
const std::pair<std::string, cci::cci_value>& iv) {
199 return iv.first.find(std::string(name()) +
"." + args) == 0;
201 if (p.second.get_string().is_string()) {
202 const std::string arg_name = p.first.substr(l + strlen(args));
203 const std::string arg_value = p.second.get_string();
204 SCP_INFO(()) <<
"Added QEMU argument : " << arg_name <<
" " << arg_value;
205 m_inst.push_qemu_arg({ arg_name.c_str(), arg_value.c_str() });
207 SCP_FATAL(()) <<
"The value of the argument is not a string";
211 std::stringstream ss_args(p_args);
213 while (ss_args >> arg) {
214 m_inst.push_qemu_arg(arg.c_str());
218 void push_icount_mode_args()
220 std::ostringstream ss;
221 if (p_icount_mips > 0) {
225 p_icount_mips.lock();
226 if (!p_icount)
return;
227 if (m_tcg_mode == TCG_MULTI) {
228 SCP_FATAL(()) <<
"MULTI threading can not be used with icount";
229 assert(m_tcg_mode != TCG_MULTI);
231 m_inst.push_qemu_arg(
"-icount");
233 ss << p_icount_mips <<
",sleep=off";
234 m_inst.push_qemu_arg(ss.str().c_str());
237 void push_tcg_mode_args()
239 switch (m_tcg_mode) {
241 m_inst.push_qemu_arg(
"tcg,thread=single");
245 m_inst.push_qemu_arg(
"tcg,thread=single,coroutine=on");
249 m_inst.push_qemu_arg(
"tcg,thread=multi");
251 SCP_FATAL(()) <<
"MULTI threading can not be used with icount";
261 void push_accelerator_args()
263 m_inst.push_qemu_arg(
"-accel");
265 auto& accel = p_accel.get_value();
267 if (accel ==
"tcg") {
268 push_tcg_mode_args();
269 }
else if (accel ==
"hvf" || accel ==
"kvm") {
270 m_inst.push_qemu_arg(accel.c_str());
271 }
else if (accel ==
"whpx") {
272 auto& whpx_args_str = p_whpx_args.get_value();
273 if (whpx_args_str.empty()) {
274 m_inst.push_qemu_arg(
"whpx");
276 std::string full_arg =
"whpx," + whpx_args_str;
277 m_inst.push_qemu_arg(full_arg.c_str());
280 SCP_FATAL(()) <<
"Invalid accel property '" << accel <<
"'";
284 LibLoader& get_loader(sc_core::sc_object* o)
288 SCP_FATAL(SCMOD) <<
"Object is not a QemuInstanceManager";
290 return inst_mgr->get_loader();
293 Target strtotarget(std::string s)
295 if (s ==
"AARCH64") {
296 return QemuInstance::Target::AARCH64;
297 }
else if (s ==
"RISCV64") {
298 return QemuInstance::Target::RISCV64;
299 }
else if (s ==
"RISCV32") {
300 return QemuInstance::Target::RISCV32;
301 }
else if (s ==
"HEXAGON") {
302 return QemuInstance::Target::HEXAGON;
304 SCP_FATAL(()) <<
"Unable to find QEMU target container";
306 __builtin_unreachable();
310 QemuInstance(
const sc_core::sc_module_name& n, sc_core::sc_object* o, std::string arch)
311 : QemuInstance(n, get_loader(o), strtotarget(arch))
314 QemuInstance(
const sc_core::sc_module_name& n, sc_core::sc_object* o, Target t): QemuInstance(n, get_loader(o), t)
318 QemuInstance(
const sc_core::sc_module_name& n, LibLoader& loader, Target t)
319 : sc_core::sc_module(n)
320 , m_conf_broker(cci::cci_get_broker())
324 , p_tcg_mode(
"tcg_mode",
"MULTI",
"The TCG mode required, SINGLE, COROUTINE or MULTI")
325 , p_sync_policy(
"sync_policy",
"multithread-quantum",
"Synchronization Policy to use")
326 , m_tcg_mode(StringToTcgMode(p_tcg_mode))
327 , p_icount(
"icount",
false,
"Enable virtual instruction counter")
328 , p_icount_mips(
"icount_mips_shift", 0,
"The MIPS shift value for icount mode (1 insn = 2^(mips) ns)")
329 , p_args(
"qemu_args",
"",
"additional space separated arguments")
330 , p_accel(
"accel",
"tcg",
"Virtualization accelerator")
331 , p_whpx_args(
"whpx_args",
"",
"Additional WHPX accelerator properties (e.g. gicd-base-address=0x17000000)")
332 , p_time_sync_strategy(
"time_sync_strategy",
"quantum_keeper",
333 "QEMU<->SystemC time synchronization strategy: \"quantum_keeper\" (default) uses the "
334 "traditional quantum keeper; \"mcips\" syncs time based on the number of instructions "
335 "via the multi core instructions per second plugin.")
337 SCP_DEBUG(()) <<
"Libqbox QemuInstance constructor";
339 auto resetcb = std::bind(&QemuInstance::reset_cb,
this, sc_unnamed::_1);
340 reset.register_value_changed_cb(resetcb);
345 if (StringToTimeSyncStrategy(p_time_sync_strategy) == TIME_SYNC_MCIPS) {
346 m_mcips_plugin = std::make_unique<McipsPlugin>(
"mcips_plugin", m_inst);
349 m_mcips_plugin->push_plugin_args(
"libidlinker.dll");
350#elif defined(__APPLE__)
351 m_mcips_plugin->push_plugin_args(
"libidlinker.dylib");
353 m_mcips_plugin->push_plugin_args(
"libidlinker.so");
358 qemu_instance_order::register_instance(
this);
361 QemuInstance(
const QemuInstance&) =
delete;
362 QemuInstance(QemuInstance&&) =
delete;
364 virtual ~QemuInstance()
366 qemu_instance_order::unregister_instance(
this);
368 if (m_mcips_plugin) {
369 m_mcips_plugin.reset();
373 bool operator==(
const QemuInstance& b)
const {
return this == &b; }
375 bool operator!=(
const QemuInstance& b)
const {
return this != &b; }
382 void add_arg(
const char* arg) { m_inst.push_qemu_arg(arg); }
393 if (arg == m_display_argument) {
398 if (!m_display_argument.empty()) {
399 SCP_FATAL(()) <<
"Display argument has already been set to '" << m_display_argument
400 <<
"' and cannot be changed to '" << arg
401 <<
"'. This may occur when mixed display modules are created (display/vnc).";
405 m_display_argument = arg;
406 m_inst.push_qemu_arg(
"-display");
407 m_inst.push_qemu_arg(m_display_argument.c_str());
417 m_inst.push_qemu_arg(
"-vnc");
418 m_inst.push_qemu_arg(vnc_options.c_str());
429 bool is_kvm_enabled()
const {
return p_accel.get_value() ==
"kvm"; }
430 bool is_hvf_enabled()
const {
return p_accel.get_value() ==
"hvf"; }
431 bool is_whpx_enabled()
const {
return p_accel.get_value() ==
"whpx"; }
432 bool is_tcg_enabled()
const {
return p_accel.get_value() ==
"tcg"; }
442 std::shared_ptr<gs::tlm_quantumkeeper_extended> qk;
444 if (m_first_qk && m_tcg_mode != TCG_MULTI) {
447 qk = gs::tlm_quantumkeeper_factory(p_sync_policy);
449 if (!qk) SCP_FATAL(())(
"No quantum keeper found with name : {}", p_sync_policy.get_value());
450 if (!m_first_qk) m_first_qk = qk;
451 if (qk->get_thread_type() == gs::SyncPolicy::SYSTEMC_THREAD) {
452 assert(m_tcg_mode == TCG_COROUTINE);
455 p_sync_policy.lock();
472 init_vnc_instance_first();
474 if (m_tcg_mode == TCG_UNSPECIFIED) {
475 SCP_FATAL(()) <<
"Unknown tcg mode : " << std::string(p_tcg_mode);
479 if (m_first_qk->get_thread_type() == gs::SyncPolicy::SYSTEMC_THREAD) {
480 if (p_tcg_mode.is_preset_value() && m_tcg_mode != TCG_COROUTINE) {
481 SCP_WARN(()) <<
"This quantum keeper can only be used with TCG_COROUTINES";
483 m_tcg_mode = TCG_COROUTINE;
485 if (m_tcg_mode == TCG_COROUTINE) {
486 SCP_FATAL(()) <<
"Please select a suitable threading mode for this quantum "
487 "keeper, it can't be used with COROUTINES";
492 push_accelerator_args();
493 push_icount_mode_args();
495 if (m_display_argument.empty()) {
496 m_inst.push_qemu_arg({
501 bool trace = (SCP_LOGGER_NAME().level >= sc_core::SC_FULL);
503 SCP_WARN(())(
"Enabling QEMU debug logging");
504 m_inst.push_qemu_arg({
"-d",
"in_asm,int,mmu,unimp,guest_errors" });
507 SCP_INFO(()) <<
"Initializing QEMU instance with args:";
509 for (
const char* arg : m_inst.get_qemu_args()) {
536 void init_vnc_instance_first()
538 std::vector<qemu_instance_order::Entry*> instances = qemu_instance_order::claim_first_init();
541 for (
auto* i : instances) {
542 if (!i->instance_has_vnc()) {
546 SCP_FATAL(()) <<
"VNC is enabled on several QEMU instances ('" << vnc_inst->instance_name() <<
"' and '"
547 << i->instance_name() <<
"): only one instance can own the GLib default main context";
552 if (vnc_inst && vnc_inst !=
this) {
553 SCP_INFO(()) <<
"Initializing the VNC QEMU instance '" << vnc_inst->instance_name()
554 <<
"' first, as it owns the GLib default main context";
555 vnc_inst->instance_init_early();
560 bool instance_has_vnc()
const override
562 const std::vector<char*>& args = m_inst.get_qemu_args();
568 for (
size_t i = 0; i + 1 < args.size(); i++) {
569 if (std::strcmp(args[i],
"-vnc") == 0 || std::strcmp(args[i],
"--vnc") == 0) {
570 if (std::strcmp(args[++i],
"none") != 0) {
578 const char* instance_name()
const override {
return name(); }
579 void instance_init_early()
override
615 sc_assert(m_mcips_plugin);
616 return *m_mcips_plugin;
635 int number_devices() {
return devices.size(); }
638 void before_end_of_elaboration()
override
644 void start_of_simulation(
void)
override {
get().finish_qemu_init(); }
646 void reset_cb(
const bool val)
649 m_inst.system_reset();