qualcomm/qbox
Loading...
Searching...
No Matches
cpu.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2021
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef _LIBQBOX_COMPONENTS_CPU_CPU_H
10#define _LIBQBOX_COMPONENTS_CPU_CPU_H
11
12#include <sstream>
13#include <mutex>
14#include <condition_variable>
15#include <atomic>
16#include <chrono>
17
18#include <tlm>
19#include <tlm_utils/simple_initiator_socket.h>
20#include <tlm_utils/simple_target_socket.h>
21#include <cci_configuration>
22
23#include <libgssync.h>
24
25#include "device.h"
26#include "ports/initiator.h"
27#include "tlm-extensions/qemu-cpu-hint.h"
28#include "ports/qemu-target-signal-socket.h"
29
30class QemuCpu;
31
32/*
33 * QEMU<->SystemC time synchronization strategy.
34 *
35 * QemuCpu owns one strategy, chosen at construction, and calls a hook at each
36 * point where time synchronization is involved (lifecycle, halt/reset, CPU
37 * start, TLM local-time). The quantum-keeper machinery itself (deadline timer,
38 * kick/end-of-loop callbacks, the CPU run loop, MTTCG signalling) lives on
39 * QemuCpu; a hook either drives it or does nothing.
40 *
41 * - QuantumKeeperSync : forwards every hook to the matching QemuCpu method
42 * (quantum-keeper time synchronization).
43 *
44 * - McipsSync : time is driven by the MCIPS plugin, so almost every hook is a
45 * no-op. It overrides only on_end_of_elaboration() to register the CPU's
46 * instructions-per-second with the plugin. The hooks it does NOT override
47 * document precisely how much of the CPU lifecycle MCIPS participates in.
48 *
49 * Strategies hold no state of their own; they reach QemuCpu through m_qemu_cpu
50 * (a back-reference). The two concrete strategies are nested classes of
51 * QemuCpu, which grants them access to its private/protected members.
52 */
53class CpuTimeSyncStrategy
54{
55public:
56 explicit CpuTimeSyncStrategy(QemuCpu& qemu_cpu): m_qemu_cpu(qemu_cpu) {}
57 virtual ~CpuTimeSyncStrategy() = default;
58
59 /* Lifecycle hooks, called from the matching QemuCpu method. */
60 virtual void on_construct() {}
61 /* before_end_of_elaboration: after_cpu_created runs right after m_cpu is
62 * built (before socket.init); the main hook runs after set_soft_stopped. */
63 virtual void on_after_cpu_created() {}
64 virtual void on_before_end_of_elaboration() {}
65 virtual void on_end_of_elaboration() {}
66 virtual void on_end_of_simulation() {}
67 virtual void on_destroy() {}
68
69 /* halt_cb: pre runs before the QEMU halt, post after it. */
70 virtual void on_halt_pre(bool val) {}
71 virtual void on_halt_post() {}
72
73 /* reset_cb: finish runs at end-of-reset, notify at the very end. */
74 virtual void on_reset_finish() {}
75 virtual void on_kick_notify() {}
76
77 /* start_of_simulation: (idempotent) quantum-keeper start + deadline arm. */
78 virtual void on_qk_start() {}
79 virtual void on_arm_deadline() {}
80
81 /* TLM initiator local-time hooks. */
82 virtual sc_core::sc_time get_local_time(int64_t vclock_now, sc_core::sc_time sc_t) { return sc_core::SC_ZERO_TIME; }
83 virtual void set_local_time(const sc_core::sc_time& t) {}
84
85protected:
86 QemuCpu& m_qemu_cpu;
87 SCP_LOGGER();
88};
89
90class QemuCpu : public QemuDevice, public QemuInitiatorIface
91{
92 /*
93 * The concrete time-sync strategies are nested classes so they can reach
94 * QemuCpu's private/protected members directly.
95 */
96
97 /*
98 * Traditional quantum-keeper time synchronization. Every hook forwards to
99 * the matching QemuCpu method.
100 */
101 class QuantumKeeperSync : public CpuTimeSyncStrategy
102 {
103 public:
104 using CpuTimeSyncStrategy::CpuTimeSyncStrategy;
105
106 void on_construct() override;
107 void on_after_cpu_created() override;
108 void on_before_end_of_elaboration() override;
109 void on_end_of_simulation() override;
110 void on_destroy() override;
111 void on_halt_pre(bool val) override;
112 void on_halt_post() override;
113 void on_reset_finish() override;
114 void on_kick_notify() override;
115 void on_qk_start() override;
116 void on_arm_deadline() override;
117 sc_core::sc_time get_local_time(int64_t vclock_now, sc_core::sc_time sc_t) override;
118 void set_local_time(const sc_core::sc_time& t) override;
119 };
120
121 /*
122 * MCIPS time synchronization. Time is driven by the MCIPS plugin, so the
123 * CPU does not run a quantum keeper at all. The only thing it contributes
124 * to the CPU lifecycle is registering its instructions-per-second rate with
125 * the plugin at end of elaboration.
126 */
127 class McipsSync : public CpuTimeSyncStrategy
128 {
129 public:
130 using CpuTimeSyncStrategy::CpuTimeSyncStrategy;
131
132 void on_end_of_elaboration() override;
133 void on_destroy() override;
134 };
135
136private:
137 inline bool mcips_enabled() const { return m_inst.is_mcips_enabled(); } // mcips: multi core instructions per second
138
139protected:
140 /*
141 * We have a unique copy per CPU of this extension, which is not dynamically allocated.
142 * We really don't want the default implementation to call delete on it...
143 */
144 class QemuCpuHintTlmExtension : public ::QemuCpuHintTlmExtension
145 {
146 public:
147 void free() override { /* leave my extension alone, TLM */ }
148 };
149
150 gs::runonsysc m_on_sysc;
151 std::shared_ptr<qemu::Timer> m_deadline_timer;
152 bool m_coroutines;
153
154 qemu::Cpu m_cpu;
155 std::string m_cpu_type;
156
157 gs::async_event m_qemu_kick_ev;
158 sc_core::sc_event_or_list m_external_ev;
159 sc_core::sc_process_handle m_sc_thread; // used for co-routines
160
161 std::atomic<bool> m_signaled;
162 std::mutex m_signaled_lock;
163 std::condition_variable m_signaled_cond;
164
165 std::shared_ptr<gs::tlm_quantumkeeper_extended> m_qk;
166 std::atomic<bool> m_finished = false;
167 std::atomic<bool> m_started = false;
168 enum { none, start_reset, hold_reset, finish_reset } m_resetting = none;
169 gs::async_event m_start_reset_done_ev;
170
171 std::mutex m_can_delete;
172 QemuCpuHintTlmExtension m_cpu_hint_ext;
173 cci::cci_param<uint64_t> m_insn_per_second;
174
175 uint64_t m_quantum_ns; // For convenience
176
177 /* Time synchronization strategy (quantum keeper or MCIPS), chosen at construction. */
178 std::unique_ptr<CpuTimeSyncStrategy> m_time_sync;
179
180 /*
181 * Outstanding async work tracking.
182 *
183 * When a job is queued via async_run or async_safe_run we increment this
184 * counter. The wrapper decrements it (and signals the condvar) once the
185 * job has finished executing. The destructor waits for the counter to
186 * reach zero before tearing down the object so that any in-flight job
187 * cannot call back into a destroyed member (e.g. m_start_reset_done_ev).
188 *
189 * A timeout is used as a safety valve: if the simulation is exiting
190 * because the async job itself faulted and will never complete, we do not
191 * want to hang forever.
192 */
193 std::atomic<int> m_async_work_outstanding{ 0 };
194 std::mutex m_async_work_mutex;
195 std::condition_variable m_async_work_cv;
196 static constexpr int ASYNC_WORK_TIMEOUT_MS = 500;
197
198 /*
199 * Wrap @job so that m_async_work_outstanding is incremented before the
200 * job is queued and decremented (with a condvar notification) after it
201 * returns. Must only be called when !m_finished so that the counter
202 * cannot be incremented after the destructor has started waiting.
203 */
204 qemu::Cpu::AsyncJobFn make_tracked_async_job(qemu::Cpu::AsyncJobFn job)
205 {
206 {
207 std::lock_guard<std::mutex> lock(m_async_work_mutex);
208 m_async_work_outstanding++;
209 }
210 if (m_finished) return {}; // already shutting down
211 return [this, job = std::move(job)]() mutable {
212 /*
213 * If the CPU is already shutting down (m_finished set by
214 * end_of_simulation()), skip the job body entirely. The job
215 * will never be able to complete safely anyway (the CPU is being
216 * halted/unplugged), and skipping lets the destructor's wait_for
217 * exit immediately rather than burning the full timeout per CPU.
218 */
219 if (!m_finished) {
220 job();
221 }
222 {
223 std::lock_guard<std::mutex> lock(m_async_work_mutex);
224 m_async_work_outstanding--;
225 }
226 m_async_work_cv.notify_all();
227 };
228 }
229
230 /*
231 * Request quantum keeper from instance
232 */
233 void create_quantum_keeper()
234 {
235 m_qk = m_inst.create_quantum_keeper();
236
237 if (!m_qk) {
238 SCP_FATAL(()) << "qbox : Sync policy unknown";
239 }
240
241 m_qk->reset();
242 }
243
244 /*
245 * Given the quantum keeper nature (synchronous or asynchronous) and the
246 * p_icount parameter, we can configure the QEMU instance accordingly.
247 */
248 void set_coroutine_mode()
249 {
250 switch (m_qk->get_thread_type()) {
251 case gs::SyncPolicy::SYSTEMC_THREAD:
252 m_coroutines = true;
253 break;
254
255 case gs::SyncPolicy::OS_THREAD:
256 m_coroutines = false;
257 break;
258 }
259 }
260
261 /*
262 * ---- CPU loop related methods ----
263 */
264
265 /*
266 * Called by watch_external_ev and kick_cb in MTTCG mode. This keeps track
267 * of an external event in case the CPU thread just released the iothread
268 * and is going to call wait_for_work. This is needed to avoid missing an
269 * event and going to sleep while we should effectively wake-up.
270 *
271 * The coroutine mode does not use this method and use the SystemC kernel
272 * as a mean of synchronization. If an asynchronous event is triggered
273 * while the CPU thread go to sleep, the fact that the CPU thread is also
274 * the SystemC thread will ensure correct ordering of the events.
275 */
276 void set_signaled()
277 {
278 assert(!m_coroutines);
279 if (m_inst.get_tcg_mode() != QemuInstance::TCG_SINGLE) {
280 std::lock_guard<std::mutex> lock(m_signaled_lock);
281 m_signaled = true;
282 m_signaled_cond.notify_all();
283 } else {
284 std::lock_guard<std::mutex> lock(m_inst.g_signaled_lock);
285 m_inst.g_signaled = true;
286 m_inst.g_signaled_cond.notify_all();
287 }
288 }
289
290 /*
291 * SystemC thread watching the m_external_ev event list. Only used in MTTCG
292 * mode.
293 */
294 void watch_external_ev()
295 {
296 for (;;) {
297 wait(m_external_ev);
298 set_signaled();
299 }
300 }
301
302 /*
303 * Called when the CPU is kicked. We notify the corresponding async event
304 * to wake the CPU up if it was sleeping waiting for work.
305 */
306 void kick_cb()
307 {
308 SCP_TRACE(())("QEMU deadline KICK callback");
309 if (m_coroutines) {
310 if (!m_finished) m_qemu_kick_ev.async_notify();
311 } else {
312 set_signaled();
313 }
314 }
315
316 /*
317 * Called by the QEMU iothread when the deadline timer expires. We kick the
318 * CPU out of its execution loop for it to call the end_of_loop_cb callback.
319 * However, we should also handle the case that qemu is currently in 'sync'
320 * - by setting the time here, we will nudge the sync thread.
321 */
322 void deadline_timer_cb()
323 {
324 SCP_TRACE(())("QEMU deadline timer callback");
325 // All syncing will be done in end_of_loop_cb
326 m_cpu.kick();
327 // Rearm timer for next time ....
328 if (!m_finished) {
329 rearm_deadline_timer();
330
331 /* Take this opportunity to set the time */
332 int64_t now = m_inst.get().get_virtual_clock();
333 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
334 if (sc_core::sc_time(now, sc_core::SC_NS) > sc_t) {
335 m_qk->set(sc_core::sc_time(now, sc_core::SC_NS) - sc_t);
336 }
337 }
338 }
339
340 /*
341 * The CPU does not have work anymore. Pause the CPU thread until we have
342 * some work to do.
343 *
344 * - In coroutine mode, this method runs a wait on the SystemC kernel,
345 * waiting for the m_external_ev list.
346 * - In MTTCG mode, we wait on the m_signaled_cond condition, signaled when
347 * set_signaled is called.
348 */
349 void wait_for_work()
350 {
351 SCP_TRACE(())("Wait for work");
352 m_qk->stop();
353 if (m_finished) return;
354
355 if (m_coroutines) {
356 m_on_sysc.run_on_sysc([this]() { wait(m_external_ev); });
357 } else {
358 if (m_inst.get_tcg_mode() != QemuInstance::TCG_SINGLE) {
359 std::unique_lock<std::mutex> lock(m_signaled_lock);
360 m_signaled_cond.wait(lock, [this] { return m_signaled || m_finished; });
361 m_signaled = false;
362 } else {
363 std::unique_lock<std::mutex> lock(m_inst.g_signaled_lock);
364 m_inst.g_signaled_cond.wait(lock, [this] { return m_inst.g_signaled || m_finished; });
365 m_inst.g_signaled = false;
366 }
367 }
368 if (m_finished) return;
369 SCP_TRACE(())("Have work, running CPU");
370 m_qk->start();
371 }
372
373 /*
374 * Set the deadline timer to trigger at the end of the time budget
375 */
376 void rearm_deadline_timer()
377 {
378 // This is a simple "every quantum" tick. Whether the QK makes use of it or not
379 // is down to the sync policy
380 m_deadline_timer->mod(m_inst.get().get_virtual_clock() + m_quantum_ns);
381 }
382
383 /*
384 * Called before running the CPU. Lock the BQL and set the deadline timer
385 * to not run beyond the time budget.
386 */
387 void prepare_run_cpu()
388 {
389 /*
390 * The QEMU CPU loop expect us to enter it with the iothread mutex locked.
391 * It is then unlocked when we come back from the CPU loop, in
392 * sync_with_kernel().
393 */
394
395 SCP_TRACE(())("Prepare run");
396 if (m_inst.get_tcg_mode() == QemuInstance::TCG_SINGLE) {
397 while (!m_inst.can_run() && !m_finished) {
398 wait_for_work();
399 }
400 } else {
401 while (!m_cpu.can_run() && !m_finished) {
402 if (!m_coroutines && !m_inst.is_tcg_enabled()) {
403 // For hardware accelerators (KVM/HVF), break back into the
404 // QEMU loop so the vCPU thread can handle signals delivered
405 // via ioctl. TCG does not need this.
406 SCP_TRACE(())("Stopping QK (accelerator)");
407 m_qk->stop();
408 break;
409 }
410 wait_for_work();
411 }
412 }
413
414 if (m_started) {
415 m_cpu.set_soft_stopped(false);
416 }
417 /*
418 * The QEMU CPU loop expect us to enter it with the iothread mutex locked.
419 * It is then unlocked when we come back from the CPU loop, in
420 * sync_with_kernel().
421 */
422 m_inst.get().lock_iothread();
423 }
424
425 /*
426 * Run the CPU loop. Only used in coroutine mode.
427 */
428 void run_cpu_loop()
429 {
430 auto last_vclock = m_inst.get().get_virtual_clock();
431 m_cpu.loop();
432 /*
433 * Workaround in icount mode: sometimes, the CPU does not execute
434 * on the first call of run_loop(). Give it a second chance.
435 */
436 for (int i = 0; i < m_inst.number_devices(); i++) {
437 if ((m_inst.get().get_virtual_clock() == last_vclock) && (m_cpu.can_run())) {
438 m_cpu.loop();
439 } else
440 break;
441 }
442 }
443
444 /*
445 * Called after a CPU loop run. It synchronizes with the kernel.
446 */
447 void sync_with_kernel()
448 {
449 int64_t now = m_inst.get().get_virtual_clock();
450
451 m_cpu.set_soft_stopped(true);
452
453 m_inst.get().unlock_iothread();
454 if (m_finished) return;
455 if (!m_coroutines) {
456 m_qk->start(); // we may have switched the QK off, so switch it on before setting
457 }
458 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
459 if (sc_core::sc_time(now, sc_core::SC_NS) > sc_t) {
460 m_qk->set(sc_core::sc_time(now, sc_core::SC_NS) - sc_t);
461 }
462 // Important to allow QK to notify itself if it's waiting.
463 m_qk->sync();
464 }
465
466 /*
467 * Callback called when the CPU exits its execution loop. In coroutine
468 * mode, we yield here to come back to run_cpu_loop(). In TCG thread mode,
469 * we use this hook to synchronize with the kernel.
470 */
471 void end_of_loop_cb()
472 {
473 SCP_TRACE(())("End of loop");
474 if (m_finished) return;
475 /*
476 * In MTTCG mode, vCPU threads are created during elaboration and can
477 * call this callback before start_of_simulation() has completed.
478 * Skip sync_with_kernel/prepare_run_cpu until fully initialized.
479 */
480 if (!m_started) return;
481
482 if (m_coroutines) {
483 m_inst.get().coroutine_yield();
484 } else {
485 std::lock_guard<std::mutex> lock(m_can_delete);
486 sync_with_kernel();
487 prepare_run_cpu();
488 }
489 }
490
491 /*
492 * SystemC thread entry when running in coroutine mode.
493 */
494 void mainloop_thread_coroutine()
495 {
496 m_cpu.register_thread();
497
498 for (; !m_finished;) {
499 prepare_run_cpu();
500 run_cpu_loop();
501 sync_with_kernel();
502 }
503 }
504
505public:
506 cci::cci_param<unsigned int> p_gdb_port;
507
508 /* The default memory socket. Mapped to the default CPU address space in QEMU */
509 QemuInitiatorSocket<> socket;
510 TargetSignalSocket<bool> halt;
511 TargetSignalSocket<bool> reset;
512
513 QemuCpu(const sc_core::sc_module_name& name, QemuInstance& inst, const std::string& type_name,
514 const char* cpu_type = nullptr)
515 : QemuDevice(name, inst, cpu_type == nullptr ? (type_name + "-cpu").c_str() : type_name.c_str())
516 , m_cpu_type(cpu_type == nullptr ? "" : cpu_type)
517 , halt("halt")
518 , reset("reset")
519 , m_qemu_kick_ev(false)
520 , m_signaled(false)
521 , p_gdb_port("gdb_port", 0, "Wait for gdb connection on TCP port <gdb_port>")
522 , socket("mem", *this, inst)
523 , m_insn_per_second("insn_per_second", 1'000'000'000, "number of instructions per second in mcips mode")
524 , m_coroutines(false)
525 {
526 using namespace std::placeholders;
527
528 if (mcips_enabled()) {
529 m_time_sync = std::make_unique<McipsSync>(*this);
530 } else {
531 m_time_sync = std::make_unique<QuantumKeeperSync>(*this);
532 }
533
534 auto haltcb = std::bind(&QemuCpu::halt_cb, this, _1);
535 halt.register_value_changed_cb(haltcb);
536 auto resetcb = std::bind(&QemuCpu::reset_cb, this, _1);
537 reset.register_value_changed_cb(resetcb);
538
539 m_time_sync->on_construct();
540 m_inst.add_dev(this);
541
542 m_start_reset_done_ev.async_detach_suspending();
543 }
544
545 virtual ~QemuCpu()
546 {
547 end_of_simulation(); // catch the case we exited abnormally
548
549 /*
550 * Wait for any jobs that were already queued via async_run /
551 * async_safe_run to finish executing before we destroy the object.
552 * Those jobs might hold captured references.
553 */
554 {
555 std::unique_lock<std::mutex> lock(m_async_work_mutex);
556 if (!m_async_work_cv.wait_for(lock, std::chrono::milliseconds(ASYNC_WORK_TIMEOUT_MS),
557 [this] { return m_async_work_outstanding == 0; })) {
558 SCP_WARN(()) << "Timeout waiting for " << m_async_work_outstanding.load()
559 << " outstanding async work(s) to complete during destruction";
560 // We may arrive here if the QEMU thread never actually started, there was queue'd work waiting for it,
561 // but the simulation has been terminated.
562 }
563 }
564
565 m_time_sync->on_destroy();
566 m_inst.del_dev(this);
567 }
568
569 // Process shutting down the CPU's at end of simulation, check this was done on destruction.
570 // This gives time for QEMU to exit etc.
571 void end_of_simulation() override
572 {
573 if (m_finished) return;
574 m_finished = true; // assert before taking lock (for co-routines too)
575
576 if (!m_cpu.valid()) {
577 /* CPU hasn't been created yet */
578 return;
579 }
580
581 if (!m_realized) {
582 return;
583 }
584
585 /*
586 * If start_of_simulation() was never called (e.g. the simulation
587 * aborted during elaboration) then finish_qemu_init() was never
588 * called either, and the QEMU iothread is still in its startup
589 * wait. Calling lock_iothread() in that state blocks forever
590 * inside wait_for_iothread_startup. Nothing useful can be done
591 * without a running iothread, so bail out early.
592 */
593 if (!m_started) {
594 return;
595 }
596
597 m_inst.get().lock_iothread();
598 /* Make sure QEMU won't call us anymore */
599 m_cpu.clear_callbacks();
600
601 if (m_coroutines) {
602 // can't join or wait for sc_event
603 m_inst.get().unlock_iothread();
604 return;
605 }
606
607 /* Unblock it if it's waiting for run budget, and unblock the CPU thread if it's sleeping */
608 m_time_sync->on_end_of_simulation();
609
610 /* Wait for QEMU to terminate the CPU thread */
611 /*
612 * Theoretically we should m_cpu.remove_sync(); here, however if QEMU is in the process of an io operation or an
613 * exclusive cpu region, it will end up waiting for the io operation to finish (effectively waiting for the
614 * SystemC thread, or potentially another CPU that wont get the chance to exit)
615 */
616 m_cpu.halt(true);
617
618 m_inst.get().unlock_iothread();
619 m_cpu.kick(); // Just in case the CPU is currently in the big lock waiting
620 m_cpu.set_unplug(true);
621 }
622
623 /* NB this is usd to determin if this cpu can run in SINGLE mode
624 * for the m_inst.can_run calculation
625 */
626 bool can_run() override { return m_cpu.can_run(); }
627
628 void retrieve_qemu_cpu()
629 {
630 if (m_cpu_type.empty()) {
631 m_cpu = qemu::Cpu(m_dev);
632 } else {
633 // Cortex-Mxx case, retrieve cpu from 'armv7m' device,
634 // must be done after the device is realized by Qemu
635 // (so after SystemC QemuDevice::end_of_elaboration())
636 m_cpu = qemu::Cpu(m_dev.get_prop_link("cpu"));
637 }
638 m_time_sync->on_after_cpu_created();
639
640 m_cpu.set_soft_stopped(true);
641 m_time_sync->on_before_end_of_elaboration();
642
643 m_cpu_hint_ext.set_cpu(m_cpu);
644 }
645
646 void before_end_of_elaboration() override
647 {
648 QemuDevice::before_end_of_elaboration();
649
650 if (m_cpu_type.empty()) {
651 retrieve_qemu_cpu();
652 }
653 socket.init(m_dev, "memory");
654 }
655
656 void halt_cb(const bool& val)
657 {
658 SCP_TRACE(())("Halt : {}", val);
659 if (!m_finished) {
660 m_time_sync->on_halt_pre(val);
661 m_inst.get().lock_iothread();
662 m_cpu.halt(val);
663 m_inst.get().unlock_iothread();
664 m_time_sync->on_halt_post();
665 }
666 }
667
668 /* NB _MUST_ be called from an SC_THREAD */
669 void reset_cb(const bool& val)
670 {
671 /* Assume this is on the SystemC thread, so no race condition issues */
672 if (m_finished) return;
673
674 if (val) {
675 if (m_resetting != none) return; // dont double reset!
676 SCP_WARN(())("Start reset");
677 m_resetting = start_reset;
678 m_cpu.async_safe_run(make_tracked_async_job([this] {
679 m_cpu.reset(true);
680 m_resetting = hold_reset;
681 m_start_reset_done_ev.async_notify();
682 })); // start the reset (which will pause the CPU)
683 } else {
684 if (m_resetting == none) return; // dont finish a finished reset!
685 while (m_resetting == start_reset) {
686 SCP_WARN(())("Hold reset");
687 /*
688 * Kick under the BQL. qemu_cpu_kick() broadcasts the vCPU's
689 * halt_cond without the BQL, so a bare kick can be lost against a
690 * vCPU entering qemu_cond_wait(halt_cond) (e.g. sleeping in WFI)
691 * and the queued reset(true) job is never run. Holding the BQL
692 * serialises against that. Release it before waiting, the vCPU
693 * needs it to run the job.
694 */
695 m_inst.get().lock_iothread();
696 m_cpu.kick(); // without this kick, async_safe_run may not be called (QEMU race)
697 m_inst.get().unlock_iothread();
698 sc_core::wait(m_start_reset_done_ev);
699 }
700 m_inst.get().lock_iothread();
701 socket.reset(); // remove DMI's (needs BQL for memory region updates)
702 m_inst.get().unlock_iothread();
703 m_cpu.reset(false); // call the end-of-reset (which will unpause the CPU)
704 m_time_sync->on_reset_finish();
705 SCP_WARN(())("Finished reset");
706 m_resetting = none;
707 }
708 m_time_sync->on_kick_notify();
709 }
710 virtual void end_of_elaboration() override
711 {
712 QemuDevice::end_of_elaboration();
713
714 if (!m_cpu_type.empty()) {
715 retrieve_qemu_cpu();
716 }
717
718 m_time_sync->on_end_of_elaboration();
719 if (!p_gdb_port.is_default_value()) {
720 std::stringstream ss;
721 SCP_INFO(()) << "Starting gdb server on TCP port " << p_gdb_port;
722 ss << "tcp::" << p_gdb_port;
723 m_inst.get().start_gdb_server(ss.str());
724 }
725 }
726
727 virtual void start_of_simulation() override
728 {
729 m_quantum_ns = int64_t(tlm_utils::tlm_quantumkeeper::get_global_quantum().to_seconds() * 1e9);
730
731 QemuDevice::start_of_simulation();
732 if (m_inst.get_tcg_mode() == QemuInstance::TCG_SINGLE) {
733 if (m_inst.can_run()) {
734 m_time_sync->on_qk_start();
735 }
736 } else if (!m_coroutines) {
737 /*
738 * In MTTCG mode, start the QK to register a suspending channel
739 * with the SystemC kernel. Without this, async_suspend() returns
740 * true (exit) whenever there are no pending events, which can
741 * happen in the gap between MMIO transactions processed by
742 * run_on_sysc(). The QK will be stopped later in wait_for_work()
743 * when the CPU halts (e.g. WFI), allowing normal starvation exit.
744 */
745 m_time_sync->on_qk_start();
746 }
747
748 m_started = true;
749 if (!m_coroutines) {
750 /*
751 * Start the quantum keeper before kicking the CPU to ensure
752 * its tick event is attached as suspending. Without this, a
753 * fast CPU could complete and stop its QK before a slow CPU
754 * ever calls sync_with_kernel() (where start() was previously
755 * first called), leaving no suspending events and causing
756 * premature simulation exit due to starvation.
757 */
758 m_time_sync->on_qk_start();
759
760 /* Prepare the CPU for its first run and release it
761 * Hold BQL to synchronize with the vCPU thread's idle-wait loop
762 * in qemu_process_cpu_events(). That loop checks cpu_thread_is_idle()
763 * (which reads soft_stopped) under BQL, then enters
764 * qemu_cond_wait(halt_cond, &bql) which atomically releases BQL.
765 * Without BQL here, the kick (broadcast on halt_cond) can be lost
766 * if the vCPU thread is between the idle check and the cond_wait.
767 */
768 m_inst.get().lock_iothread();
769 m_cpu.set_soft_stopped(false);
770 m_time_sync->on_arm_deadline();
771 m_cpu.kick();
772 m_inst.get().unlock_iothread();
773 }
774
775 // Have not managed to figure out the root cause of the issue, but the
776 // PC is not properly set before running KVM, or it is possibly reset to
777 // 0 by some routine. By setting the vcpu as dirty, we trigger pushing
778 // registers to KVM just before running it.
779 m_cpu.set_vcpu_dirty(true);
780 }
781
782 /* QemuInitiatorIface */
783 virtual void initiator_customize_tlm_payload(TlmPayload& payload) override
784 {
785 /* Signal the other end we are a CPU */
786 payload.set_extension(&m_cpu_hint_ext);
787 }
788
789 virtual void initiator_tidy_tlm_payload(TlmPayload& payload) override { payload.clear_extension(&m_cpu_hint_ext); }
790
791 /*
792 * Called by the initiator socket just before a memory transaction.
793 * We update our current view of the local time and return it.
794 */
795 virtual sc_core::sc_time initiator_get_local_time() override
796 {
797 if (m_finished) return sc_core::SC_ZERO_TIME;
798
799 int64_t vclock_now = m_inst.get().get_virtual_clock();
800 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
801 return m_time_sync->get_local_time(vclock_now, sc_t);
802 }
803
804 /*
805 * Called after the transaction. We must update our local time view to
806 * match t.
807 */
808 virtual void initiator_set_local_time(const sc_core::sc_time& t) override
809 {
810 if (m_finished) return;
811 m_time_sync->set_local_time(t);
812 }
813
814 /* expose async run interface for DMI invalidation */
815 virtual void initiator_async_run(qemu::Cpu::AsyncJobFn job) override
816 {
817 if (!m_finished) m_cpu.async_run(make_tracked_async_job(std::move(job)));
818 }
819};
820
821/*
822 * ---- QuantumKeeperSync out-of-line definitions ----
823 *
824 * Defined after QemuCpu so they can reach its members and methods (as a nested
825 * class) through m_qemu_cpu. Each hook simply forwards to the matching
826 * QemuCpu method.
827 */
828
829inline void QemuCpu::QuantumKeeperSync::on_construct()
830{
831 m_qemu_cpu.m_external_ev |= m_qemu_cpu.m_qemu_kick_ev;
832
833 m_qemu_cpu.create_quantum_keeper();
834 m_qemu_cpu.set_coroutine_mode();
835 if (!m_qemu_cpu.m_coroutines) {
836 sc_core::sc_spawn(std::bind(&QemuCpu::watch_external_ev, &m_qemu_cpu), "watch_external_ev");
837 }
838}
839
840inline void QemuCpu::QuantumKeeperSync::on_after_cpu_created()
841{
842 if (m_qemu_cpu.m_coroutines) {
843 m_qemu_cpu.m_sc_thread = sc_core::sc_spawn(std::bind(&QemuCpu::mainloop_thread_coroutine, &m_qemu_cpu));
844 }
845}
846
847inline void QemuCpu::QuantumKeeperSync::on_before_end_of_elaboration()
848{
849 m_qemu_cpu.m_cpu.set_end_of_loop_callback(std::bind(&QemuCpu::end_of_loop_cb, &m_qemu_cpu));
850 m_qemu_cpu.m_cpu.set_kick_callback(std::bind(&QemuCpu::kick_cb, &m_qemu_cpu));
851 m_qemu_cpu.m_deadline_timer = m_qemu_cpu.m_inst.get().timer_new();
852 m_qemu_cpu.m_deadline_timer->set_callback(std::bind(&QemuCpu::deadline_timer_cb, &m_qemu_cpu));
853}
854
855inline void QemuCpu::QuantumKeeperSync::on_end_of_simulation()
856{
857 m_qemu_cpu.m_deadline_timer->del();
858 m_qemu_cpu.m_qk->stop();
859 /* Unblock the CPU thread if it's sleeping */
860 m_qemu_cpu.set_signaled();
861}
862
863inline void QemuCpu::QuantumKeeperSync::on_destroy()
864{
865 while (!m_qemu_cpu.m_can_delete.try_lock()) {
866 m_qemu_cpu.m_qk->stop();
867 }
868}
869
870inline void QemuCpu::QuantumKeeperSync::on_halt_pre(bool val)
871{
872 if (val) {
873 m_qemu_cpu.m_deadline_timer->del();
874 m_qemu_cpu.m_qk->stop();
875 } else {
876 m_qemu_cpu.m_qk->start();
877 m_qemu_cpu.rearm_deadline_timer();
878 }
879}
880
881inline void QemuCpu::QuantumKeeperSync::on_halt_post()
882{
883 m_qemu_cpu.m_qemu_kick_ev.async_notify(); // notify the other thread so that the CPU is allowed to continue
884}
885
886inline void QemuCpu::QuantumKeeperSync::on_reset_finish()
887{
888 m_qemu_cpu.m_qk->start(); // restart the QK if it's stopped
889 m_qemu_cpu.m_qk->reset();
890 m_qemu_cpu.m_qemu_kick_ev.async_notify(); // notify the other thread so that the CPU is allowed to continue
891}
892
893inline void QemuCpu::QuantumKeeperSync::on_kick_notify()
894{
895 m_qemu_cpu.m_qemu_kick_ev
896 .async_notify(); // notify the other thread so that the CPU is allowed to process if required
897}
898
899inline void QemuCpu::QuantumKeeperSync::on_qk_start() { m_qemu_cpu.m_qk->start(); }
900
901inline void QemuCpu::QuantumKeeperSync::on_arm_deadline() { m_qemu_cpu.rearm_deadline_timer(); }
902
903inline sc_core::sc_time QemuCpu::QuantumKeeperSync::get_local_time(int64_t vclock_now, sc_core::sc_time sc_t)
904{
905 using sc_core::sc_time;
906 using sc_core::SC_NS;
907
908 if (sc_time(vclock_now, SC_NS) > sc_t) {
909 m_qemu_cpu.m_qk->set(sc_time(vclock_now, SC_NS) - sc_t);
910 return m_qemu_cpu.m_qk->get_local_time();
911 }
912 return sc_core::SC_ZERO_TIME;
913}
914
915inline void QemuCpu::QuantumKeeperSync::set_local_time(const sc_core::sc_time& t)
916{
917 m_qemu_cpu.m_qk->set(t);
918
919 if (m_qemu_cpu.m_qk->need_sync()) {
920 /*
921 * Kick the CPU out of its execution loop so that we can sync with
922 * the kernel.
923 */
924 m_qemu_cpu.m_cpu.kick();
925 }
926}
927
928/*
929 * ---- McipsSync out-of-line definitions ----
930 */
931
932inline void QemuCpu::McipsSync::on_end_of_elaboration()
933{
934 if (!m_qemu_cpu.m_inst.get_mcips_plugin().set_vcpu_insn_per_second(m_qemu_cpu.m_cpu.get_index(),
935 m_qemu_cpu.m_insn_per_second)) {
936 SCP_FATAL(()) << "Failed to set insn_per_second for cpu_" << m_qemu_cpu.m_cpu.get_index();
937 sc_assert(false);
938 }
939}
940
941inline void QemuCpu::McipsSync::on_destroy()
942{
943 /*
944 * In MCIPS mode the vCPU is a free-running MTTCG thread with no QK/deadline
945 * timer gating it to SystemC. end_of_simulation() only requests halt+unplug
946 * asynchronously, so after sc_stop() the thread can still be in tcg_cpu_exec()
947 * and touch the QemuInstance after it is freed -> use-after-free. By now the
948 * simulation has stopped and runonsysc jobs are cancelled, so it is safe to
949 * synchronously join the vCPU thread before the QemuInstance is destroyed.
950 */
951 if (!m_qemu_cpu.m_started || m_qemu_cpu.m_coroutines || !m_qemu_cpu.m_cpu.valid()) {
952 return;
953 }
954
955 m_qemu_cpu.m_inst.get().lock_iothread();
956 m_qemu_cpu.m_cpu.remove_sync(); // stop + unplug + join the vCPU thread
957 m_qemu_cpu.m_inst.get().unlock_iothread();
958}
959
960#endif
Definition cpu.h:54
Definition qemu-cpu-hint.h:17
Definition cpu.h:91
QemuDevice(const sc_core::sc_module_name &name, QemuInstance &inst, const char *qom_type)
Construct a QEMU device.
Definition device.h:86
Definition initiator.h:38
Definition async_event.h:22
Definition runonsysc.h:23
Definition libqemu-cxx.h:725