qualcomm/qbox
Loading...
Searching...
No Matches
mcips-plugin.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause-Clear
6 */
7
8#ifndef _LIBQBOX_COMPONENTS_MCIPS_PLUGIN_H
9#define _LIBQBOX_COMPONENTS_MCIPS_PLUGIN_H
10
11#include "libqemu-plugin.h"
12#include <sync_window.h>
13#include <async_event.h>
14#include <atomic>
15#include <chrono>
16#include <mutex>
17#include <thread>
18#include <sstream>
19
20class McipsPlugin : public LibQemuPlugin
21{
22 SCP_LOGGER();
23 SC_HAS_PROCESS(McipsPlugin);
24
25private:
26 enum vCPUTimeStatus { IDLE, PAUSED, RUNNING };
27
28 struct vCPUTime {
29 uint64_t index;
30 uint64_t insn_per_second;
31 uint64_t delta_insn;
32 sc_core::sc_time cpu_time;
33 vCPUTimeStatus cpu_execution_status;
34 };
35
36 uint64_t m_global_quantum; // quantum in nanoseconds; used as the instruction count limit
37 int m_num_vcpus{ 0 }; // cached CPU count (set in end_of_elaboration, may grow in vcpu_init)
38 void* m_time_handle; // handle returned by QEMU to let us control time
41 sc_current_window; // last window received from SystemC
42 sc_core::sc_time m_quantum; // copy of the TLM global quantum
43 sc_core::sc_time m_qemu_time; // main QEMU simulation time (only changed while holding m_mcips_mutex)
44 /* CPU currently driving m_qemu_time. Read lock-free by get_qemu_clock() (acquire/release). */
45 std::atomic<vCPUTime*> m_active_vcpu;
46 qemu_plugin_scoreboard* m_vcpus_scoreboard; // one vCPUTime entry per CPU index
47
48 /* Nanosecond shadow of m_qemu_time; sc_time is not thread-safe. */
49 std::atomic<int64_t> m_qemu_time_ns{ 0 };
50
51 /* Shutdown flag; all entry points return immediately when set. */
52 std::atomic<bool> m_shutdown{ false };
53
54 /* Counts in-flight receive_window_cb() calls; shutdown waits for zero. */
55 std::atomic<int> m_inflight_cb{ 0 };
56
57 /* Protects shared state across vCPU and SystemC threads. */
58 std::mutex m_mcips_mutex;
59
60 /* Fires a SystemC event to keep time moving when all CPUs are idle (the "idle pump"),
61 * and doubles as the iothread-livelock watchdog kick. Notified by vcpu_idle() and
62 * get_qemu_clock(); vcpu_resume() lets it lapse once a CPU is active again. */
63 gs::async_event m_idle_tick;
64
65 /* False until the first vCPU is initialized; while false, get_qemu_clock() kicks the
66 * idle pump so time advances during startup before any vCPU exists. */
67 std::atomic<bool> m_first_vcpu_initialized{ false };
68
69 /* Iothread-livelock watchdog state. Wall-clock gated (1s) so sub-second
70 * unit tests never trip it. */
71 std::atomic<int64_t> m_wd_last_clock_ns{ 0 };
72 std::atomic<int64_t> m_wd_freeze_start_ns{ 0 };
73 sc_core::sc_time m_wd_last_tick_qemu_time{ sc_core::SC_ZERO_TIME }; // SC-side, under m_mcips_mutex
74 static constexpr int64_t kWdFreezeThresholdNs = 1'000'000'000LL;
75
76public:
77 McipsPlugin(const sc_core::sc_module_name& nm, qemu::LibQemu& inst)
78 : LibQemuPlugin(nm, inst)
79 , m_sync_sc("m_sync_sc")
80 , m_global_quantum(0)
81 , m_time_handle(nullptr)
82 , sc_current_window(sc_core::sc_sync_window<sc_core::sc_sync_policy_tlm_quantum>::zero_window)
83 , m_quantum(sc_core::SC_ZERO_TIME)
84 , m_qemu_time(sc_core::SC_ZERO_TIME)
85 , m_active_vcpu(nullptr)
86 , m_vcpus_scoreboard(nullptr)
87 , m_idle_tick(false)
88 {
89 SC_METHOD(idle_tick_method);
90 dont_initialize();
91 sensitive << m_idle_tick;
92 }
93
100 {
101 std::unique_lock<std::mutex> lock(m_mcips_mutex);
102
103 // Watchdog recovery: kicked by get_qemu_clock when m_qemu_time has been frozen >1s wall.
104 // If qemu_time hasn't advanced since the last tick, bump it by one quantum so the iothread's
105 // next gt_recalc_timer reads a fresh deadline and its ppoll goes back to sleep.
106 auto* active = m_active_vcpu.load(std::memory_order_relaxed);
107 if (active) {
108 const sc_core::sc_time current_qemu_time = qemu_time_now(active);
109 if (current_qemu_time == m_wd_last_tick_qemu_time && m_quantum > sc_core::SC_ZERO_TIME) {
110 m_qemu_time += m_quantum;
112 active->cpu_time += m_quantum;
114 m_idle_tick.notify(m_quantum);
115 }
116 m_wd_last_tick_qemu_time = current_qemu_time;
117 }
118 }
119
128 {
129 if (m_shutdown.load(std::memory_order_seq_cst)) {
130 return; // already shut down
131 }
132
133 m_shutdown.store(true, std::memory_order_seq_cst); // must be seq_cst before inflight drain
134 m_active_vcpu.store(nullptr, std::memory_order_release);
135
137
138 /* Wait for any in-flight receive_window_cb() to finish. */
139 while (m_inflight_cb.load(std::memory_order_seq_cst) > 0) {
140 std::this_thread::yield();
141 }
142
143 detach_sync_window();
144 }
145
147
149 void end_of_simulation() override { shutdown_cleanup(); }
150
152 void end_of_elaboration() override
153 {
154 LibQemuPlugin::end_of_elaboration();
155 m_vcpus_scoreboard = m_inst.plugin_api().qemu_plugin_scoreboard_new(sizeof(vCPUTime));
156 sc_assert(m_vcpus_scoreboard);
157
158 m_time_handle = const_cast<void*>(m_inst.plugin_api().qemu_plugin_request_time_control(m_id));
159 sc_assert(m_time_handle);
160
161 m_quantum = tlm_utils::tlm_quantumkeeper::get_global_quantum();
162 m_global_quantum = static_cast<uint64_t>(std::floor(m_quantum.to_seconds() * NSEC_IN_ONE_SEC));
163
164 sc_current_window = { sc_core::SC_ZERO_TIME, m_quantum };
165 if (m_sync_sc.is_attached()) {
166 m_sync_sc.async_set_window(sc_current_window);
167 } else {
168 SCP_FATAL(()) << "Window must be attached before calling async_set_window()";
169 sc_assert(false);
170 }
171 m_num_vcpus = m_inst.plugin_api().qemu_plugin_num_vcpus();
172 m_sync_sc.register_sync_cb(std::bind(&McipsPlugin::receive_window_cb, this, std::placeholders::_1));
173
174 /* Every callback is registered directly with the plugin API as a
175 * capture-less lambda (which decays to the C function pointer the API
176 * expects). Each forwards through LibQemuPlugin::dispatch_userdata for
177 * shutdown synchronization against the leaked PluginHandle, a raw
178 * `this` would dangle once this SystemC module is destroyed while QEMU
179 * threads still have callbacks in flight. */
180 m_inst.plugin_api().qemu_plugin_register_vcpu_tb_trans_cb(
181 m_id,
182 [](qemu_plugin_tb* tb, void* userdata) {
184 userdata, [&](LibQemuPlugin* p) { static_cast<McipsPlugin*>(p)->vcpu_tb_trans(tb); });
185 },
186 handle_as_userdata());
187 m_inst.plugin_api().qemu_plugin_register_vcpu_init_cb(
188 m_id,
189 [](unsigned int cpu_index, void* userdata) {
191 userdata, [&](LibQemuPlugin* p) { static_cast<McipsPlugin*>(p)->vcpu_init(cpu_index); });
192 },
193 handle_as_userdata());
194 m_inst.plugin_api().qemu_plugin_register_vcpu_resume_cb(
195 m_id,
196 [](unsigned int cpu_index, void* userdata) {
198 userdata, [&](LibQemuPlugin* p) { static_cast<McipsPlugin*>(p)->vcpu_resume(cpu_index); });
199 },
200 handle_as_userdata());
201 m_inst.plugin_api().qemu_plugin_register_vcpu_idle_cb(
202 m_id,
203 [](unsigned int cpu_index, void* userdata) {
205 userdata, [&](LibQemuPlugin* p) { static_cast<McipsPlugin*>(p)->vcpu_idle(cpu_index); });
206 },
207 handle_as_userdata());
208 m_inst.plugin_api().qemu_plugin_register_time_cb(
209 m_time_handle,
210 [](void* userdata) -> int64_t {
211 int64_t result = static_cast<int64_t>(sc_core::sc_time_stamp().to_seconds() * NSEC_IN_ONE_SEC);
212 LibQemuPlugin::dispatch_userdata(userdata, [&](LibQemuPlugin* p) {
213 result = static_cast<McipsPlugin*>(p)->get_qemu_clock(nullptr);
214 });
215 return result;
216 },
217 handle_as_userdata());
218 }
219
221 vCPUTime* get_vcpu(unsigned int cpu_index)
222 {
223 return reinterpret_cast<vCPUTime*>(
224 m_inst.plugin_api().qemu_plugin_scoreboard_find(m_vcpus_scoreboard, cpu_index));
225 }
226
227 void attach_sync_window()
228 {
229 if (!m_sync_sc.is_attached()) m_sync_sc.attach();
230 }
231
232 void detach_sync_window()
233 {
234 if (m_sync_sc.is_attached()) m_sync_sc.detach(m_qemu_time);
235 }
236
243 bool set_vcpu_insn_per_second(unsigned int cpu_index, uint64_t insn_per_second)
244 {
245 if (insn_per_second == 0) {
246 SCP_FATAL(()) << "insn_per_second must be > 0 (cpu_" << cpu_index << ")";
247 return false;
248 }
249
250 const unsigned int n = static_cast<unsigned int>(m_inst.plugin_api().qemu_plugin_num_vcpus());
251 if (cpu_index >= n) {
252 return false;
253 }
254 if (n > static_cast<unsigned int>(m_num_vcpus)) m_num_vcpus = static_cast<int>(n);
255
256 vCPUTime* vcpu = get_vcpu(cpu_index);
257 if (!vcpu) {
258 return false;
259 }
260
261 vcpu->insn_per_second = insn_per_second;
262 return true;
263 }
264
267 {
268 m_qemu_time_ns.store(static_cast<int64_t>(m_qemu_time.to_seconds() * NSEC_IN_ONE_SEC),
269 std::memory_order_release);
270 }
271
273 static sc_core::sc_time cpu_delta_time(const vCPUTime* vcpu)
274 {
275 sc_assert(vcpu && "cpu_delta_time called with null vCPU");
276 return sc_core::sc_time(static_cast<double>(vcpu->delta_insn) / vcpu->insn_per_second, sc_core::SC_SEC);
277 }
278
280 sc_core::sc_time cpu_time_now(const vCPUTime* vcpu)
281 {
282 sc_assert(vcpu && "cpu_time_now called with null vCPU");
283 if (vcpu->cpu_execution_status == IDLE) {
284 return vcpu->cpu_time;
285 }
286 return vcpu->cpu_time + cpu_delta_time(vcpu);
287 }
288
295 sc_core::sc_time qemu_time_now() { return qemu_time_now(m_active_vcpu.load(std::memory_order_acquire)); }
296
298 sc_core::sc_time qemu_time_now(const vCPUTime* active)
299 {
300 if (!active) return m_qemu_time;
301 return m_qemu_time + cpu_delta_time(active);
302 }
303
306 {
307 vCPUTime* slowest_cpu = nullptr;
308 sc_core::sc_time min_time = sc_core::SC_ZERO_TIME;
309
310 for (int i = 0; i < m_num_vcpus; i++) {
311 auto* vcpu = get_vcpu(i);
312 if (vcpu->cpu_execution_status == IDLE) continue;
313
314 const sc_core::sc_time current_cpu_time = cpu_time_now(vcpu);
315 if (!slowest_cpu || current_cpu_time < min_time) {
316 min_time = current_cpu_time;
317 slowest_cpu = vcpu;
318 }
319 }
320 return slowest_cpu;
321 }
322
329 {
330 for (int i = 0; i < m_num_vcpus; i++) {
331 vCPUTime* vcpu = get_vcpu(i);
332 if (vcpu->cpu_execution_status != IDLE) {
333 m_active_vcpu.store(vcpu, std::memory_order_release);
334 SCP_DEBUG(()) << "cpu_" << vcpu->index << " is the new active cpu";
335 return vcpu;
336 }
337 }
338 m_active_vcpu.store(nullptr, std::memory_order_release);
339 return nullptr;
340 }
341
343 void pause_if_ahead(vCPUTime* vcpu, const vCPUTime* active, sc_core::sc_time current_qemu_time,
344 sc_core::sc_time threshold)
345 {
346 if (vcpu->cpu_execution_status != RUNNING) return;
347
348 const sc_core::sc_time vcpu_time = cpu_time_now(vcpu);
349 if ((vcpu == active && current_qemu_time > threshold) || (vcpu != active && vcpu_time > current_qemu_time)) {
350 vcpu->cpu_execution_status = PAUSED;
351 m_inst.plugin_api().qemu_plugin_cpu_request_pause(static_cast<unsigned int>(vcpu->index));
352 SCP_DEBUG(()) << "cpu_" << vcpu->index << " paused, time=" << vcpu_time;
353 }
354 }
355
357 void resume_if_behind(const vCPUTime* active, sc_core::sc_time current_qemu_time, sc_core::sc_time threshold)
358 {
359 for (int i = 0; i < m_num_vcpus; i++) {
360 auto* vcpu = get_vcpu(i);
361 if (vcpu->cpu_execution_status != PAUSED) continue;
362 const sc_core::sc_time vcpu_time = cpu_time_now(vcpu);
363 if ((vcpu == active && current_qemu_time <= threshold) ||
364 (vcpu != active && vcpu_time <= current_qemu_time)) {
365 vcpu->cpu_execution_status = RUNNING;
366 m_inst.plugin_api().qemu_plugin_cpu_resume(static_cast<unsigned int>(vcpu->index));
367 SCP_DEBUG(()) << "cpu_" << vcpu->index << " resumed, time=" << vcpu_time;
368 }
369 }
370 }
371
381 sc_core::sc_time compute_threshold()
382 {
383 const vCPUTime* slowest_cpu = slowest_none_idle_cpu();
384 if (!slowest_cpu) return sc_core::SC_ZERO_TIME;
385 return std::min(sc_current_window.to, cpu_time_now(slowest_cpu) + m_quantum);
386 }
387
395 void rebalance_and_sync(vCPUTime* vcpu = nullptr)
396 {
397 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
398
399 if (!active) {
400 SCP_DEBUG(()) << "rebalance_and_sync(no-active): all-idle, detaching window";
401 detach_sync_window();
402 return;
403 }
404
405 SCP_DEBUG(()) << "rebalance_and_sync: active cpu present";
406 const sc_core::sc_time threshold = compute_threshold();
407 if (threshold == sc_core::SC_ZERO_TIME) {
408 SCP_DEBUG(()) << "rebalance_and_sync: threshold=0 with active cpu, skipping";
409 return;
410 }
411
412 const sc_core::sc_time current_qemu_time = qemu_time_now(active);
413 if (vcpu) {
414 pause_if_ahead(vcpu, active, current_qemu_time, threshold);
415 }
416 resume_if_behind(active, current_qemu_time, threshold);
418 }
419
426 {
427 if (m_sync_sc.is_attached()) {
428 if (custom_window) {
429 SCP_DEBUG(()) << "set_systemc_window::custom_window.from= " << custom_window->from
430 << ", custom_window.to= " << custom_window->to;
431 m_sync_sc.async_set_window(*custom_window);
432 } else {
433 sc_core::sc_time current_qemu_time = qemu_time_now();
434 SCP_DEBUG(()) << "set_systemc_window::qemu_cpu_time_now = " << current_qemu_time
435 << ", sc_current_window.from= " << sc_current_window.from
436 << ", sc_current_window.to= " << sc_current_window.to;
437 m_sync_sc.async_set_window({ current_qemu_time, (current_qemu_time + m_quantum) });
438 }
439 } else {
440 SCP_INFO(()) << "set_systemc_window: window not attached, skipping async_set_window()";
441 }
442 }
443
452 {
453 if (m_shutdown.load(std::memory_order_seq_cst)) return;
454
455 m_inflight_cb.fetch_add(1, std::memory_order_seq_cst);
456 if (m_shutdown.load(std::memory_order_seq_cst)) {
457 m_inflight_cb.fetch_sub(1, std::memory_order_seq_cst);
458 return;
459 }
460
461 std::lock_guard<std::mutex> lock(m_mcips_mutex);
462 sc_current_window = sc_w;
463
464 auto* active = m_active_vcpu.load(std::memory_order_relaxed);
465 SCP_DEBUG(()) << "receive_window_cb: qemu_time=" << qemu_time_now(active) << ", sc_window=["
466 << sc_current_window.from << ", " << sc_current_window.to << "]";
467
468 if (!active) {
469 SCP_DEBUG(()) << "receive_window_cb(no-active): calling rebalance_and_sync";
471 m_inflight_cb.fetch_sub(1, std::memory_order_seq_cst);
472 return;
473 }
474
475 if (active->cpu_execution_status == PAUSED) {
476 const sc_core::sc_time threshold = compute_threshold();
477 if (threshold != sc_core::SC_ZERO_TIME && qemu_time_now(active) <= threshold) {
478 active->cpu_execution_status = RUNNING;
479 m_inst.plugin_api().qemu_plugin_cpu_resume(static_cast<unsigned int>(active->index));
480 SCP_DEBUG(()) << "receive_window_cb: resumed active cpu";
481 }
482 }
483
485 m_inflight_cb.fetch_sub(1, std::memory_order_seq_cst);
486 }
487
489 void vcpu_init(unsigned int cpu_index)
490 {
491 if (m_shutdown.load(std::memory_order_acquire)) return;
492
493 const int current = m_inst.plugin_api().qemu_plugin_num_vcpus();
494 if (current > m_num_vcpus) m_num_vcpus = current;
495
496 vCPUTime* vcpu = get_vcpu(cpu_index);
497 vcpu->index = cpu_index;
498 if (vcpu->insn_per_second == 0) {
499 vcpu->insn_per_second = 1'000'000'000;
500 }
501 vcpu->delta_insn = 0;
502 vcpu->cpu_time = sc_core::SC_ZERO_TIME;
503 vcpu->cpu_execution_status = RUNNING;
504
505 if (m_active_vcpu.load(std::memory_order_relaxed) == nullptr) {
506 m_active_vcpu.store(vcpu, std::memory_order_release);
507 }
508
509 m_first_vcpu_initialized.store(true, std::memory_order_release);
510 }
511
517 void cpu_end_delta_quota(vCPUTime* vcpu)
518 {
519 std::lock_guard<std::mutex> lock(m_mcips_mutex);
520
521 if (vcpu->cpu_execution_status != RUNNING) {
522 return;
523 }
524
525 const sc_core::sc_time delta_time = cpu_delta_time(vcpu);
526 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
527 sc_assert(active && "cpu_end_delta_quota called but no active CPU");
528
529 const sc_core::sc_time old_qemu_time = m_qemu_time;
530 if (active == vcpu) {
531 m_qemu_time += delta_time;
533 SCP_DEBUG(()) << "cpu_" << vcpu->index << " completed quantum, qemu_time: " << old_qemu_time << " -> "
534 << m_qemu_time << ", delta=" << delta_time << ", sc_time=" << sc_core::sc_time_stamp();
535 }
536 vcpu->cpu_time += delta_time;
537 vcpu->delta_insn = 0;
538
539 rebalance_and_sync(vcpu);
540 }
541
543 void vcpu_tb_exec_cond(unsigned int cpu_index, void* /*udata*/)
544 {
545 if (m_shutdown.load(std::memory_order_acquire)) return;
546
547 vCPUTime* vcpu = get_vcpu(cpu_index);
548 if (vcpu->cpu_execution_status != RUNNING) {
549 return;
550 }
551
552 sc_assert(vcpu->delta_insn >= m_global_quantum && "TB exec condition fired but quota not met");
554 }
555
557 void vcpu_tb_trans(qemu_plugin_tb* tb)
558 {
559 const size_t n_insns = m_inst.plugin_api().qemu_plugin_tb_n_insns(tb);
560 qemu_plugin_u64 delta_insn = qemu_plugin_scoreboard_u64_in_struct(m_vcpus_scoreboard, vCPUTime, delta_insn);
561
562 m_inst.plugin_api().qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(tb, QEMU_PLUGIN_INLINE_ADD_U64,
563 delta_insn, n_insns);
564
565 m_inst.plugin_api().qemu_plugin_register_vcpu_tb_exec_cond_cb(
566 tb,
567 /* Registered per translated block (not in end_of_elaboration), but
568 * still a capture-less lambda for consistency with the others. */
569 [](unsigned int cpu_index, void* userdata) {
570 LibQemuPlugin::dispatch_userdata(userdata, [&](LibQemuPlugin* p) {
571 static_cast<McipsPlugin*>(p)->vcpu_tb_exec_cond(cpu_index, userdata);
572 });
573 },
574 QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE, delta_insn, m_global_quantum, handle_as_userdata());
575 }
576
583 void vcpu_idle(unsigned int cpu_index)
584 {
585 if (m_shutdown.load(std::memory_order_acquire)) return;
586
587 std::lock_guard<std::mutex> lock(m_mcips_mutex);
588 SCP_DEBUG(()) << "vcpu_idle callback for cpu_" << cpu_index;
589 vCPUTime* vcpu = get_vcpu(cpu_index);
590
591 switch (vcpu->cpu_execution_status) {
592 case RUNNING: {
593 SCP_DEBUG(()) << "cpu_" << cpu_index << " RUNNING → IDLE";
594 vcpu->cpu_execution_status = IDLE;
595
596 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
597 if (vcpu == active) {
598 SCP_DEBUG(()) << "cpu_" << cpu_index << " was active, looking for another cpu to take over";
599
600 const sc_core::sc_time delta = cpu_delta_time(vcpu);
601 m_qemu_time += delta;
603 vcpu->cpu_time += delta;
604
605 vCPUTime* new_active = select_active_vcpu();
606
607 if (!new_active) {
608 if (!m_shutdown.load(std::memory_order_acquire)) {
609 m_idle_tick.notify(m_quantum);
610 SCP_DEBUG(()) << "[idle_pump] NOTIFY from vcpu_idle (cpu_" << cpu_index << ")";
611 }
612 detach_sync_window();
613 } else {
614 new_active->cpu_time = m_qemu_time;
615 new_active->delta_insn = 0;
616
617 if (new_active->cpu_execution_status == PAUSED) {
618 new_active->cpu_execution_status = RUNNING;
619 m_inst.plugin_api().qemu_plugin_cpu_resume(static_cast<unsigned int>(new_active->index));
620 }
621 }
622 }
623 vcpu->delta_insn = 0;
625 break;
626 }
627 case PAUSED:
628 SCP_DEBUG(()) << "vcpu_idle: cpu_" << cpu_index << " already PAUSED";
629 break;
630
631 case IDLE:
632 SCP_DEBUG(()) << "vcpu_idle: cpu_" << cpu_index << " already IDLE";
633 break;
634
635 default:
636 SCP_FATAL(()) << "vcpu_idle: invalid execution status for cpu_" << cpu_index;
637 sc_assert(false);
638 break;
639 }
640 }
641
650 void vcpu_resume(unsigned int cpu_index)
651 {
652 if (m_shutdown.load(std::memory_order_acquire)) return;
653
654 std::lock_guard<std::mutex> lock(m_mcips_mutex);
655 SCP_DEBUG(()) << "vcpu_resume callback for cpu_" << cpu_index;
656 vCPUTime* vcpu = get_vcpu(cpu_index);
657
658 switch (vcpu->cpu_execution_status) {
659 case IDLE: {
660 SCP_DEBUG(()) << "cpu_" << cpu_index << " IDLE → RUNNING";
661 vcpu->cpu_execution_status = RUNNING;
662 vcpu->cpu_time = qemu_time_now();
663
664 if (m_active_vcpu.load(std::memory_order_relaxed) == nullptr) {
665 const sc_core::sc_time sc_now = sc_core::sc_time(sc_core::sc_time_stamp().to_seconds(),
666 sc_core::SC_SEC);
667 if (sc_now > m_qemu_time) {
668 m_qemu_time = sc_now;
670 }
671
672 vcpu->cpu_time = m_qemu_time;
673 m_active_vcpu.store(vcpu, std::memory_order_release);
674
675 attach_sync_window();
677 SCP_DEBUG(()) << "[idle_pump] STOP vcpu_resume: cpu_" << cpu_index << " first to resume";
678 }
679 break;
680 }
681 case PAUSED: {
682 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
683 const sc_core::sc_time threshold = compute_threshold();
684 const sc_core::sc_time current_qemu_time = qemu_time_now(active);
685 const sc_core::sc_time vcpu_time = cpu_time_now(vcpu);
686
687 if (threshold != sc_core::SC_ZERO_TIME && ((vcpu == active && current_qemu_time <= threshold) ||
688 (vcpu != active && vcpu_time <= current_qemu_time))) {
689 vcpu->cpu_execution_status = RUNNING;
690 m_inst.plugin_api().qemu_plugin_cpu_resume(static_cast<unsigned int>(vcpu->index));
691 SCP_DEBUG(()) << "vcpu_resume: cpu_" << vcpu->index << " PAUSED → RUNNING";
692 } else {
693 m_inst.plugin_api().qemu_plugin_cpu_request_pause(static_cast<unsigned int>(vcpu->index));
694 SCP_DEBUG(()) << "vcpu_resume: cpu_" << vcpu->index << " CAN NOT BE RESUMED";
695 }
696 break;
697 }
698 case RUNNING:
699 SCP_DEBUG(()) << "vcpu_resume: cpu_" << cpu_index << " already RUNNING";
700 break;
701
702 default:
703 SCP_FATAL(()) << "vcpu_resume: invalid execution status for cpu_" << cpu_index;
704 sc_assert(false);
705 break;
706 }
707 }
708
720 int64_t get_qemu_clock(void* /*userdata*/)
721 {
722 if (m_shutdown.load(std::memory_order_acquire)) {
723 return static_cast<int64_t>(sc_core::sc_time_stamp().to_seconds() * NSEC_IN_ONE_SEC);
724 }
725
726 /* Lock-free: read the base time from the atomic ns shadow, never from the
727 * non-atomic m_qemu_time (vCPU threads mutate that under m_mcips_mutex, so
728 * reading it here would be a torn read). */
729 auto active = m_active_vcpu.load(std::memory_order_acquire);
730 int64_t qemu_time = m_qemu_time_ns.load(std::memory_order_acquire);
731
732 if (!active) {
733 // Clamp qemu_time to SystemC time to prevent drift at startup.
734 if (qemu_time > 0) {
735 int64_t systemc_time_now = static_cast<int64_t>(sc_core::sc_time_stamp().to_seconds() *
736 NSEC_IN_ONE_SEC);
737 if (systemc_time_now > qemu_time) {
738 qemu_time = systemc_time_now;
739 }
740 }
741
742 if (!m_first_vcpu_initialized.load(std::memory_order_acquire)) {
743 m_idle_tick.notify(m_quantum);
744 SCP_DEBUG(()) << "[idle_pump] NOTIFY from get_qemu_clock (startup, no vcpu yet)";
745 }
746 } else {
747 // Add the active CPU's in-flight delta (delta_insn / insn_per_second are race-free).
748 qemu_time += static_cast<int64_t>(cpu_delta_time(active).to_seconds() * NSEC_IN_ONE_SEC);
749
750 // active CPU drives the clock; watch for it getting stuck (iothread livelock)
751 int64_t prev = m_wd_last_clock_ns.load(std::memory_order_relaxed);
752 if (prev != qemu_time) {
753 // A: clock advanced since last observation.
754 m_wd_last_clock_ns.store(qemu_time, std::memory_order_relaxed);
755 m_wd_freeze_start_ns.store(0, std::memory_order_relaxed);
756 } else {
757 const int64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
758 std::chrono::steady_clock::now().time_since_epoch())
759 .count();
760 int64_t start = m_wd_freeze_start_ns.load(std::memory_order_relaxed);
761 if (start == 0) {
762 // B: first call to notice the freeze; start the wall-clock stopwatch.
763 m_wd_freeze_start_ns.compare_exchange_strong(start, now_ns, std::memory_order_relaxed);
764 } else if (now_ns - start > kWdFreezeThresholdNs) {
765 // D: frozen for more than the threshold -> fire the watchdog.
766 m_idle_tick.notify(sc_core::SC_ZERO_TIME);
767 m_wd_freeze_start_ns.store(now_ns, std::memory_order_relaxed); // throttle re-kicks
768 }
769 // C: frozen but under threshold -> keep waiting (no-op).
770 }
771 }
772
773 return qemu_time;
774 }
775
781 {
782 auto active = m_active_vcpu.load(std::memory_order_relaxed);
783 std::ostringstream os;
784
785 const uint64_t qemu_time = static_cast<uint64_t>(qemu_time_now().to_seconds() * NSEC_IN_ONE_SEC);
786
787 os << "{" << "\"name\":\"" << name() << "\"," << "\"qemu_time\":\"" << qemu_time << " ns\","
788 << "\"n_cpus\":" << m_num_vcpus << ","
789 << "\"active_vcpu_index\":" << (active ? static_cast<int64_t>(active->index) : -1) << "," << "\"vcpus\":[";
790
791 bool first = true;
792 for (int i = 0; i < m_num_vcpus; i++) {
793 auto* vcpu = get_vcpu(i);
794 if (!vcpu) continue;
795
796 if (!first) os << ",";
797 first = false;
798
799 const uint64_t ns = static_cast<uint64_t>(vcpu->cpu_time.to_seconds() * NSEC_IN_ONE_SEC);
800
801 os << "{" << "\"index\":" << vcpu->index << "," << "\"insn_per_second\":\"" << vcpu->insn_per_second
802 << "\"," << "\"delta_insn\":\"" << vcpu->delta_insn << "\"," << "\"cpu_time_ns\":\"" << ns << "\","
803 << "\"cpu_execution_status\":" << static_cast<int>(vcpu->cpu_execution_status) << "}";
804 }
805 os << "]}";
806
807 return os.str();
808 }
809};
810
811#endif //_LIBQBOX_COMPONENTS_MCIPS_PLUGIN_H
static void dispatch_userdata(void *userdata, Body body)
Run body on the live plugin instance, holding a refcount for the duration. No-op when the handle is n...
Definition libqemu-plugin.h:127
void shutdown_bridge()
Stop bridge dispatch and drain in-flight callbacks.
Definition libqemu-plugin.h:98
Definition mcips-plugin.h:21
void vcpu_tb_exec_cond(unsigned int cpu_index, void *)
Called when a CPU has run at least global_quantum instructions.
Definition mcips-plugin.h:543
void resume_if_behind(const vCPUTime *active, sc_core::sc_time current_qemu_time, sc_core::sc_time threshold)
Resume paused CPUs that are still within the allowed time limit. m_mcips_mutex must be held.
Definition mcips-plugin.h:357
vCPUTime * slowest_none_idle_cpu()
Find the CPU that is furthest behind in time.
Definition mcips-plugin.h:305
vCPUTime * select_active_vcpu()
Pick the first non-idle CPU and make it the active one.
Definition mcips-plugin.h:328
void vcpu_idle(unsigned int cpu_index)
Called when a CPU goes idle (QEMU holds BQL; we additionally take m_mcips_mutex).
Definition mcips-plugin.h:583
std::string get_mcips_status_json()
Returns a JSON string with the current state of all CPUs (for debugging). Reads without m_mcips_mutex...
Definition mcips-plugin.h:780
void rebalance_and_sync(vCPUTime *vcpu=nullptr)
Rebalance CPUs and notify SystemC. m_mcips_mutex must be held.
Definition mcips-plugin.h:395
void idle_tick_method()
SC_METHOD woken by m_idle_tick. When a CPU is active it does iothread-livelock recovery (bumps qemu_t...
Definition mcips-plugin.h:99
void vcpu_init(unsigned int cpu_index)
Called under BQL before simulation starts; does not hold m_mcips_mutex.
Definition mcips-plugin.h:489
sc_core::sc_time qemu_time_now(const vCPUTime *active)
Overload for callers that already hold m_mcips_mutex and have the active pointer.
Definition mcips-plugin.h:298
static sc_core::sc_time cpu_delta_time(const vCPUTime *vcpu)
Time represented by the in-flight instructions in delta_insn.
Definition mcips-plugin.h:273
void shutdown_cleanup()
Shared shutdown logic used by both the destructor and end_of_simulation().
Definition mcips-plugin.h:127
void end_of_elaboration() override
Set up the plugin, register QEMU callbacks, and send the first sync window.
Definition mcips-plugin.h:152
sc_core::sc_time qemu_time_now()
Current QEMU time (base + active CPU's in-flight delta).
Definition mcips-plugin.h:295
void sync_qemu_time_ns()
Copy m_qemu_time into the atomic nanosecond field. Must hold m_mcips_mutex when calling.
Definition mcips-plugin.h:266
int64_t get_qemu_clock(void *)
Returns the current simulation time in nanoseconds to QEMU's timer thread.
Definition mcips-plugin.h:720
void pause_if_ahead(vCPUTime *vcpu, const vCPUTime *active, sc_core::sc_time current_qemu_time, sc_core::sc_time threshold)
Pause the CPU if it has run too far ahead of the allowed limit. m_mcips_mutex must be held.
Definition mcips-plugin.h:343
void vcpu_resume(unsigned int cpu_index)
Called when a CPU wakes up (QEMU holds BQL; we additionally take m_mcips_mutex).
Definition mcips-plugin.h:650
void receive_window_cb(const sc_core::sc_sync_window< sc_core::sc_sync_policy_tlm_quantum >::window &sc_w)
Called by SystemC when a new time window is ready.
Definition mcips-plugin.h:451
vCPUTime * get_vcpu(unsigned int cpu_index)
Helper function to get vCPU time structure from scoreboard.
Definition mcips-plugin.h:221
bool set_vcpu_insn_per_second(unsigned int cpu_index, uint64_t insn_per_second)
Set the instructions per second for a specific vCPU.
Definition mcips-plugin.h:243
sc_core::sc_time compute_threshold()
Compute the time threshold that determines which CPUs may run.
Definition mcips-plugin.h:381
void vcpu_tb_trans(qemu_plugin_tb *tb)
Called when QEMU translates a block, adds instruction counting and quota check.
Definition mcips-plugin.h:557
void set_systemc_window(const sc_core::sc_sync_window< sc_core::sc_sync_policy_tlm_quantum >::window *custom_window=nullptr)
Set the SystemC synchronization window.
Definition mcips-plugin.h:424
void cpu_end_delta_quota(vCPUTime *vcpu)
Called when a CPU has run its full instruction quota. Adds the time used, pauses/resumes CPUs as need...
Definition mcips-plugin.h:517
void end_of_simulation() override
Called by SystemC at end of simulation; stops all QEMU callbacks.
Definition mcips-plugin.h:149
sc_core::sc_time cpu_time_now(const vCPUTime *vcpu)
Current time for a specific CPU (base + in-flight delta). Must hold m_mcips_mutex when calling.
Definition mcips-plugin.h:280
Definition async_event.h:22
Definition libqemu-cxx.h:95
windowed synchronizer, template SYNC_POLICY gives the quantum (dynamically) and specifies if the wind...
Definition sync_window.h:53
void attach()
Attach to the primitive channel's suspend/resume mechanism for time synchronization.
Definition sync_window.h:183
bool is_attached() const
Check if the sync_window is currently attached.
Definition sync_window.h:176
Definition sync_window.h:65