qualcomm/qbox
Loading...
Searching...
No Matches
qemu-instance.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2021
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef LIBQBOX_QEMU_INSTANCE_H_
10#define LIBQBOX_QEMU_INSTANCE_H_
11
12#include <cassert>
13#include <cstring>
14#include <sstream>
15#include <systemc>
16
17#include <cci_configuration>
18#include <vector>
19
20#include <cciutils.h>
21#include <report.h>
22#include <libgssync.h>
23
24#include <libqemu-cxx/libqemu-cxx.h>
25
26#include <dmi-manager.h>
27#include <exceptions.h>
28
29#include <scp/report.h>
30
31#include "ports/qemu-target-signal-socket.h"
32#include "mcips-plugin.h"
33#include "qemu-instance-order.h"
34
36{
37public:
38 virtual bool can_run() { return true; }
39 SCP_LOGGER();
40};
41
51class QemuInstanceManager : public sc_core::sc_module
52{
53public:
54 using Target = qemu::Target;
55 using LibLoader = qemu::LibraryLoaderIface;
56
57protected:
58 LibLoader& m_loader = qemu::get_default_lib_loader();
59
60public:
65 QemuInstanceManager(const sc_core::sc_module_name& n = "QemuInstanceManager"): sc_core::sc_module(n) {}
66
72 QemuInstanceManager(const sc_core::sc_module_name& n, LibLoader& loader): sc_core::sc_module(n), m_loader(loader) {}
73
74 LibLoader& get_loader() { return m_loader; }
75
76 /* Destructor should only be called at the end of the program, if it is called before, then all
77 * Qemu instances that it manages will, of course, be destroyed too
78 */
79 virtual ~QemuInstanceManager() {}
80};
81
88class QemuInstance : public sc_core::sc_module, private qemu_instance_order::Entry
89{
90private:
91 std::shared_ptr<gs::tlm_quantumkeeper_extended> m_first_qk = NULL;
92 std::mutex m_lock;
93 std::list<QemuDeviceBaseIF*> devices;
94 cci::cci_broker_handle m_conf_broker;
95
96 bool m_running = false;
97 SCP_LOGGER();
98
99public:
101
102 // these will be used by wait_for_work when it needs a global lock
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;
107
108 void add_dev(QemuDeviceBaseIF* d)
109 {
110 std::lock_guard<std::mutex> lock(m_lock);
111 devices.push_back(d);
112 }
113 void del_dev(QemuDeviceBaseIF* d)
114 {
115 if (m_running) {
116 std::lock_guard<std::mutex> lock(m_lock);
117 devices.remove(d);
118 }
119 }
120 bool can_run()
121 {
122 if (!m_running) return false;
123 std::lock_guard<std::mutex> lock(m_lock);
124 bool can_run = false;
125 // In SINGLE mode, check if another CPU could run
126 for (auto const& i : devices) {
127 if (i->can_run()) {
128 can_run = true;
129 break;
130 }
131 }
132 return can_run;
133 }
134 using Target = qemu::Target;
135 using LibLoader = qemu::LibraryLoaderIface;
136
137 enum TcgMode {
138 TCG_UNSPECIFIED,
139 TCG_SINGLE,
140 TCG_COROUTINE,
141 TCG_MULTI,
142 };
143 TcgMode StringToTcgMode(std::string s)
144 {
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;
150 }
151
152 enum TimeSyncStrategy {
153 TIME_SYNC_QUANTUM_KEEPER,
154 TIME_SYNC_MCIPS,
155 };
156 TimeSyncStrategy StringToTimeSyncStrategy(std::string s)
157 {
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\")";
161 sc_assert(false);
162 return TIME_SYNC_QUANTUM_KEEPER;
163 }
164
165protected:
166 cci::cci_param<std::string> p_time_sync_strategy;
167 std::unique_ptr<McipsPlugin> m_mcips_plugin;
168
169 qemu::LibQemu m_inst;
170 QemuInstanceDmiManager m_dmi_mgr;
171
172 cci::cci_param<std::string> p_tcg_mode;
173 cci::cci_param<std::string> p_sync_policy;
174 TcgMode m_tcg_mode;
175
176 cci::cci_param<bool> p_icount;
177 cci::cci_param<int> p_icount_mips;
178
179 cci::cci_param<std::string> p_args;
180 std::string m_display_argument;
181
182 cci::cci_param<std::string> p_accel;
183 cci::cci_param<std::string> p_whpx_args;
184
185 void push_default_args()
186 {
187 const size_t l = strlen(name()) + 1;
188
189 m_inst.push_qemu_arg("libqbox"); /* argv[0] */
190 m_inst.push_qemu_arg({
191 "-M", "none", /* no machine */
192 "-m", "0", /* Guest memory is managed by gs_memory module */
193 "-monitor", "null", /* no monitor */
194 "-serial", "null", /* no serial backend */
195 });
196
197 const char* args = "qemu_args."; /* Update documentations because it's not anymore 'args.' it's '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;
200 })) {
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() });
206 } else {
207 SCP_FATAL(()) << "The value of the argument is not a string";
208 }
209 }
210
211 std::stringstream ss_args(p_args);
212 std::string arg;
213 while (ss_args >> arg) {
214 m_inst.push_qemu_arg(arg.c_str());
215 }
216 }
217
218 void push_icount_mode_args()
219 {
220 std::ostringstream ss;
221 if (p_icount_mips > 0) {
222 p_icount = true;
223 }
224 p_icount.lock();
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);
230 }
231 m_inst.push_qemu_arg("-icount");
232
233 ss << p_icount_mips << ",sleep=off";
234 m_inst.push_qemu_arg(ss.str().c_str());
235 }
236
237 void push_tcg_mode_args()
238 {
239 switch (m_tcg_mode) {
240 case TCG_SINGLE:
241 m_inst.push_qemu_arg("tcg,thread=single");
242 break;
243
244 case TCG_COROUTINE:
245 m_inst.push_qemu_arg("tcg,thread=single,coroutine=on");
246 break;
247
248 case TCG_MULTI:
249 m_inst.push_qemu_arg("tcg,thread=multi");
250 if (p_icount) {
251 SCP_FATAL(()) << "MULTI threading can not be used with icount";
252 assert(!p_icount);
253 }
254 break;
255
256 default:
257 assert(false);
258 }
259 }
260
261 void push_accelerator_args()
262 {
263 m_inst.push_qemu_arg("-accel");
264
265 auto& accel = p_accel.get_value();
266
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");
275 } else {
276 std::string full_arg = "whpx," + whpx_args_str;
277 m_inst.push_qemu_arg(full_arg.c_str());
278 }
279 } else {
280 SCP_FATAL(()) << "Invalid accel property '" << accel << "'";
281 }
282 }
283
284 LibLoader& get_loader(sc_core::sc_object* o)
285 {
286 QemuInstanceManager* inst_mgr = dynamic_cast<QemuInstanceManager*>(o);
287 if (!inst_mgr) {
288 SCP_FATAL(SCMOD) << "Object is not a QemuInstanceManager";
289 }
290 return inst_mgr->get_loader();
291 }
292
293 Target strtotarget(std::string s)
294 {
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;
303 } else {
304 SCP_FATAL(()) << "Unable to find QEMU target container";
305 }
306 __builtin_unreachable();
307 }
308
309public:
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))
312 {
313 }
314 QemuInstance(const sc_core::sc_module_name& n, sc_core::sc_object* o, Target t): QemuInstance(n, get_loader(o), t)
315 {
316 }
317
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())
321 , m_inst(loader, t)
322 , m_dmi_mgr(m_inst)
323 , reset("reset")
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.")
336 {
337 SCP_DEBUG(()) << "Libqbox QemuInstance constructor";
338
339 auto resetcb = std::bind(&QemuInstance::reset_cb, this, sc_unnamed::_1);
340 reset.register_value_changed_cb(resetcb);
341
342 m_running = true;
343 p_tcg_mode.lock();
344 push_default_args();
345 if (StringToTimeSyncStrategy(p_time_sync_strategy) == TIME_SYNC_MCIPS) {
346 m_mcips_plugin = std::make_unique<McipsPlugin>("mcips_plugin", m_inst);
347 /* Plugin filename per platform: meson shared_module() defaults */
348#if defined(_WIN32)
349 m_mcips_plugin->push_plugin_args("libidlinker.dll");
350#elif defined(__APPLE__)
351 m_mcips_plugin->push_plugin_args("libidlinker.dylib");
352#else /* Linux */
353 m_mcips_plugin->push_plugin_args("libidlinker.so");
354#endif
355 }
356
357 /* Last: the destructor, which unregisters, only runs for a fully constructed object */
358 qemu_instance_order::register_instance(this);
359 }
360
361 QemuInstance(const QemuInstance&) = delete;
362 QemuInstance(QemuInstance&&) = delete;
363
364 virtual ~QemuInstance()
365 {
366 qemu_instance_order::unregister_instance(this);
367 m_running = false;
368 if (m_mcips_plugin) {
369 m_mcips_plugin.reset();
370 }
371 }
372
373 bool operator==(const QemuInstance& b) const { return this == &b; }
374
375 bool operator!=(const QemuInstance& b) const { return this != &b; }
376
382 void add_arg(const char* arg) { m_inst.push_qemu_arg(arg); }
383
390 void set_display_arg(const std::string& arg)
391 {
392 // If already set to the same value, nothing to do
393 if (arg == m_display_argument) {
394 return;
395 }
396
397 // If already set to a different value, it's an error
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).";
402 }
403
404 // Set the display argument for the first time
405 m_display_argument = arg;
406 m_inst.push_qemu_arg("-display");
407 m_inst.push_qemu_arg(m_display_argument.c_str());
408 }
409
415 void set_vnc_args(const std::string& vnc_options)
416 {
417 m_inst.push_qemu_arg("-vnc");
418 m_inst.push_qemu_arg(vnc_options.c_str());
419 }
420
427 TcgMode get_tcg_mode() { return m_tcg_mode; }
428
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"; }
433
440 std::shared_ptr<gs::tlm_quantumkeeper_extended> create_quantum_keeper()
441 {
442 std::shared_ptr<gs::tlm_quantumkeeper_extended> qk;
443 /* only multi-mode sync should have separate QK's per CPU */
444 if (m_first_qk && m_tcg_mode != TCG_MULTI) {
445 qk = m_first_qk;
446 } else {
447 qk = gs::tlm_quantumkeeper_factory(p_sync_policy);
448 }
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);
453 }
454 /* The p_sync_policy parameter should not be modified anymore */
455 p_sync_policy.lock();
456 return qk;
457 }
458
468 void init()
469 {
470 assert(!is_inited());
471
472 init_vnc_instance_first();
473
474 if (m_tcg_mode == TCG_UNSPECIFIED) {
475 SCP_FATAL(()) << "Unknown tcg mode : " << std::string(p_tcg_mode);
476 }
477 // By now, if there is a CPU, it would be loaded into QEMU, and we would have a QK
478 if (m_first_qk) {
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";
482 }
483 m_tcg_mode = TCG_COROUTINE;
484 } else {
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";
488 }
489 }
490 }
491
492 push_accelerator_args();
493 push_icount_mode_args();
494
495 if (m_display_argument.empty()) {
496 m_inst.push_qemu_arg({
497 "-display", "none", /* no GUI */
498 });
499 }
500
501 bool trace = (SCP_LOGGER_NAME().level >= sc_core::SC_FULL);
502 if (trace) {
503 SCP_WARN(())("Enabling QEMU debug logging");
504 m_inst.push_qemu_arg({ "-d", "in_asm,int,mmu,unimp,guest_errors" });
505 }
506
507 SCP_INFO(()) << "Initializing QEMU instance with args:";
508
509 for (const char* arg : m_inst.get_qemu_args()) {
510 SCP_INFO(()) << arg;
511 }
512
513 m_inst.init();
514 m_dmi_mgr.init();
515 }
516
517private:
518 /*
519 * All the QEMU instances of the process share the GLib process default
520 * main context, on which QEMU implicitly attaches a lot of its sources,
521 * and in particular the ones of the VNC server. libqemu hands that
522 * context to the first instance being initialized, whose main loop then
523 * dispatches those sources under its own BQL. If that is not the instance
524 * running VNC, the VNC callbacks run on another instance's thread,
525 * without the BQL of the instance they belong to.
526 *
527 * Instances are initialized lazily, in an order resulting from the module
528 * construction order. So when the first one is about to be initialized
529 * (all the modules are constructed by then, hence -vnc is known), the
530 * instance with VNC enabled, if any, is initialized first.
531 *
532 * As a consequence, an instance may be initialized before the elaboration
533 * callbacks of its own devices run: QEMU arguments must be pushed from
534 * module constructors only.
535 */
536 void init_vnc_instance_first()
537 {
538 std::vector<qemu_instance_order::Entry*> instances = qemu_instance_order::claim_first_init();
539 qemu_instance_order::Entry* vnc_inst = nullptr;
540
541 for (auto* i : instances) {
542 if (!i->instance_has_vnc()) {
543 continue;
544 }
545 if (vnc_inst) {
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";
548 }
549 vnc_inst = i;
550 }
551
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();
556 }
557 }
558
559 /* VNC enabled with -vnc, however it was passed (vnc module, qemu_args, ...) */
560 bool instance_has_vnc() const override
561 {
562 const std::vector<char*>& args = m_inst.get_qemu_args();
563
564 /*
565 * QEMU treats --opt as -opt. Each -vnc creates its own display, and
566 * "-vnc none" only disables that one.
567 */
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) {
571 return true;
572 }
573 }
574 }
575 return false;
576 }
577
578 const char* instance_name() const override { return name(); }
579 void instance_init_early() override
580 {
581 if (!is_inited()) {
582 init();
583 }
584 }
585
586public:
590 bool is_inited() const { return m_inst.is_inited(); }
591
600 {
601 if (!is_inited()) {
602 init();
603 }
604
605 return m_inst;
606 }
607
614 {
615 sc_assert(m_mcips_plugin);
616 return *m_mcips_plugin;
617 }
618
624 bool is_mcips_enabled() const noexcept { return static_cast<bool>(m_mcips_plugin); }
625
634
635 int number_devices() { return devices.size(); }
636
637private:
638 void before_end_of_elaboration() override
639 {
640 if (!is_inited()) {
641 init(); // dlsyms libqemu_init; plugin functions are part of the returned LibQemuExports table.
642 }
643 }
644 void start_of_simulation(void) override { get().finish_qemu_init(); }
645
646 void reset_cb(const bool val)
647 {
648 if (val == 1) {
649 m_inst.system_reset();
650 }
651 }
652};
653
654GSC_MODULE_REGISTER(QemuInstanceManager);
655GSC_MODULE_REGISTER(QemuInstance, sc_core::sc_object*, std::string);
656
657#endif
Definition mcips-plugin.h:21
Definition qemu-instance.h:36
Handles the DMI regions at the QEMU instance level.
Definition dmi-manager.h:47
QEMU instance manager class.
Definition qemu-instance.h:52
QemuInstanceManager(const sc_core::sc_module_name &n="QemuInstanceManager")
Construct a QemuInstanceManager. The manager will use the default library loader provided by libqemu-...
Definition qemu-instance.h:65
QemuInstanceManager(const sc_core::sc_module_name &n, LibLoader &loader)
Construct a QemuInstanceManager by providing a custom library loader.
Definition qemu-instance.h:72
This class encapsulates a libqemu-cxx qemu::LibQemu instance. It handles QEMU parameters and instance...
Definition qemu-instance.h:89
void set_vnc_args(const std::string &vnc_options)
Add the VNC command line argument to the qemu instance.
Definition qemu-instance.h:415
bool is_mcips_enabled() const noexcept
Check if multi core instructions per second plugin (mcips) is enabled.
Definition qemu-instance.h:624
qemu::LibQemu & get()
Returns the underlying qemu::LibQemu instance.
Definition qemu-instance.h:599
TcgMode get_tcg_mode()
Get the TCG mode for this instance.
Definition qemu-instance.h:427
std::shared_ptr< gs::tlm_quantumkeeper_extended > create_quantum_keeper()
Get the TCG mode for this instance.
Definition qemu-instance.h:440
McipsPlugin & get_mcips_plugin()
Returns the mcips_plugin.
Definition qemu-instance.h:613
void set_display_arg(const std::string &arg)
Add a the display command line argument to the qemu instance.
Definition qemu-instance.h:390
bool is_inited() const
Returns true if the instance is initialized.
Definition qemu-instance.h:590
QemuInstanceDmiManager & get_dmi_manager()
Returns the locked QemuInstanceDmiManager instance.
Definition qemu-instance.h:633
void add_arg(const char *arg)
Add a command line argument to the qemu instance.
Definition qemu-instance.h:382
void init()
Initialize the QEMU instance.
Definition qemu-instance.h:468
Definition target-signal-socket.h:163
Definition libqemu-cxx.h:95
Definition dynlib_loader.h:23
Definition qemu-instance-order.h:19