8#ifndef _LIBQBOX_COMPONENTS_MCIPS_PLUGIN_H
9#define _LIBQBOX_COMPONENTS_MCIPS_PLUGIN_H
11#include "libqemu-plugin.h"
12#include <sync_window.h>
13#include <async_event.h>
20class McipsPlugin :
public LibQemuPlugin
23 SC_HAS_PROCESS(McipsPlugin);
26 enum vCPUTimeStatus { IDLE, PAUSED, RUNNING };
30 uint64_t insn_per_second;
32 sc_core::sc_time cpu_time;
33 vCPUTimeStatus cpu_execution_status;
36 uint64_t m_global_quantum;
42 sc_core::sc_time m_quantum;
43 sc_core::sc_time m_qemu_time;
45 std::atomic<vCPUTime*> m_active_vcpu;
46 qemu_plugin_scoreboard* m_vcpus_scoreboard;
49 std::atomic<int64_t> m_qemu_time_ns{ 0 };
52 std::atomic<bool> m_shutdown{
false };
55 std::atomic<int> m_inflight_cb{ 0 };
58 std::mutex m_mcips_mutex;
67 std::atomic<bool> m_first_vcpu_initialized{
false };
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 };
74 static constexpr int64_t kWdFreezeThresholdNs = 1'000'000'000LL;
77 McipsPlugin(
const sc_core::sc_module_name& nm,
qemu::LibQemu& inst)
78 : LibQemuPlugin(nm, inst)
79 , m_sync_sc(
"m_sync_sc")
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)
91 sensitive << m_idle_tick;
101 std::unique_lock<std::mutex> lock(m_mcips_mutex);
106 auto* active = m_active_vcpu.load(std::memory_order_relaxed);
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);
116 m_wd_last_tick_qemu_time = current_qemu_time;
129 if (m_shutdown.load(std::memory_order_seq_cst)) {
133 m_shutdown.store(
true, std::memory_order_seq_cst);
134 m_active_vcpu.store(
nullptr, std::memory_order_release);
139 while (m_inflight_cb.load(std::memory_order_seq_cst) > 0) {
140 std::this_thread::yield();
143 detach_sync_window();
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);
158 m_time_handle =
const_cast<void*
>(m_inst.plugin_api().qemu_plugin_request_time_control(m_id));
159 sc_assert(m_time_handle);
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));
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);
168 SCP_FATAL(()) <<
"Window must be attached before calling async_set_window()";
171 m_num_vcpus = m_inst.plugin_api().qemu_plugin_num_vcpus();
180 m_inst.plugin_api().qemu_plugin_register_vcpu_tb_trans_cb(
182 [](qemu_plugin_tb* tb,
void* userdata) {
184 userdata, [&](LibQemuPlugin* p) {
static_cast<McipsPlugin*
>(p)->
vcpu_tb_trans(tb); });
186 handle_as_userdata());
187 m_inst.plugin_api().qemu_plugin_register_vcpu_init_cb(
189 [](
unsigned int cpu_index,
void* userdata) {
191 userdata, [&](LibQemuPlugin* p) {
static_cast<McipsPlugin*
>(p)->
vcpu_init(cpu_index); });
193 handle_as_userdata());
194 m_inst.plugin_api().qemu_plugin_register_vcpu_resume_cb(
196 [](
unsigned int cpu_index,
void* userdata) {
198 userdata, [&](LibQemuPlugin* p) {
static_cast<McipsPlugin*
>(p)->
vcpu_resume(cpu_index); });
200 handle_as_userdata());
201 m_inst.plugin_api().qemu_plugin_register_vcpu_idle_cb(
203 [](
unsigned int cpu_index,
void* userdata) {
205 userdata, [&](LibQemuPlugin* p) {
static_cast<McipsPlugin*
>(p)->
vcpu_idle(cpu_index); });
207 handle_as_userdata());
208 m_inst.plugin_api().qemu_plugin_register_time_cb(
210 [](
void* userdata) -> int64_t {
211 int64_t result =
static_cast<int64_t
>(sc_core::sc_time_stamp().to_seconds() * NSEC_IN_ONE_SEC);
217 handle_as_userdata());
223 return reinterpret_cast<vCPUTime*
>(
224 m_inst.plugin_api().qemu_plugin_scoreboard_find(m_vcpus_scoreboard, cpu_index));
227 void attach_sync_window()
232 void detach_sync_window()
234 if (m_sync_sc.is_attached()) m_sync_sc.detach(m_qemu_time);
245 if (insn_per_second == 0) {
246 SCP_FATAL(()) <<
"insn_per_second must be > 0 (cpu_" << cpu_index <<
")";
250 const unsigned int n =
static_cast<unsigned int>(m_inst.plugin_api().qemu_plugin_num_vcpus());
251 if (cpu_index >= n) {
254 if (n >
static_cast<unsigned int>(m_num_vcpus)) m_num_vcpus =
static_cast<int>(n);
256 vCPUTime* vcpu =
get_vcpu(cpu_index);
261 vcpu->insn_per_second = insn_per_second;
268 m_qemu_time_ns.store(
static_cast<int64_t
>(m_qemu_time.to_seconds() * NSEC_IN_ONE_SEC),
269 std::memory_order_release);
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);
282 sc_assert(vcpu &&
"cpu_time_now called with null vCPU");
283 if (vcpu->cpu_execution_status == IDLE) {
284 return vcpu->cpu_time;
300 if (!active)
return m_qemu_time;
307 vCPUTime* slowest_cpu =
nullptr;
308 sc_core::sc_time min_time = sc_core::SC_ZERO_TIME;
310 for (
int i = 0; i < m_num_vcpus; i++) {
312 if (vcpu->cpu_execution_status == IDLE)
continue;
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;
330 for (
int i = 0; i < m_num_vcpus; 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";
338 m_active_vcpu.store(
nullptr, std::memory_order_release);
343 void pause_if_ahead(vCPUTime* vcpu,
const vCPUTime* active, sc_core::sc_time current_qemu_time,
344 sc_core::sc_time threshold)
346 if (vcpu->cpu_execution_status != RUNNING)
return;
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;
357 void resume_if_behind(
const vCPUTime* active, sc_core::sc_time current_qemu_time, sc_core::sc_time threshold)
359 for (
int i = 0; i < m_num_vcpus; i++) {
361 if (vcpu->cpu_execution_status != PAUSED)
continue;
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;
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);
397 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
400 SCP_DEBUG(()) <<
"rebalance_and_sync(no-active): all-idle, detaching window";
401 detach_sync_window();
405 SCP_DEBUG(()) <<
"rebalance_and_sync: active cpu present";
407 if (threshold == sc_core::SC_ZERO_TIME) {
408 SCP_DEBUG(()) <<
"rebalance_and_sync: threshold=0 with active cpu, skipping";
412 const sc_core::sc_time current_qemu_time =
qemu_time_now(active);
427 if (m_sync_sc.is_attached()) {
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);
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) });
440 SCP_INFO(()) <<
"set_systemc_window: window not attached, skipping async_set_window()";
453 if (m_shutdown.load(std::memory_order_seq_cst))
return;
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);
461 std::lock_guard<std::mutex> lock(m_mcips_mutex);
462 sc_current_window = sc_w;
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 <<
"]";
469 SCP_DEBUG(()) <<
"receive_window_cb(no-active): calling rebalance_and_sync";
471 m_inflight_cb.fetch_sub(1, std::memory_order_seq_cst);
475 if (active->cpu_execution_status == PAUSED) {
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";
485 m_inflight_cb.fetch_sub(1, std::memory_order_seq_cst);
491 if (m_shutdown.load(std::memory_order_acquire))
return;
493 const int current = m_inst.plugin_api().qemu_plugin_num_vcpus();
494 if (current > m_num_vcpus) m_num_vcpus = current;
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;
501 vcpu->delta_insn = 0;
502 vcpu->cpu_time = sc_core::SC_ZERO_TIME;
503 vcpu->cpu_execution_status = RUNNING;
505 if (m_active_vcpu.load(std::memory_order_relaxed) ==
nullptr) {
506 m_active_vcpu.store(vcpu, std::memory_order_release);
509 m_first_vcpu_initialized.store(
true, std::memory_order_release);
519 std::lock_guard<std::mutex> lock(m_mcips_mutex);
521 if (vcpu->cpu_execution_status != RUNNING) {
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");
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();
536 vcpu->cpu_time += delta_time;
537 vcpu->delta_insn = 0;
545 if (m_shutdown.load(std::memory_order_acquire))
return;
547 vCPUTime* vcpu =
get_vcpu(cpu_index);
548 if (vcpu->cpu_execution_status != RUNNING) {
552 sc_assert(vcpu->delta_insn >= m_global_quantum &&
"TB exec condition fired but quota not met");
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);
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);
565 m_inst.plugin_api().qemu_plugin_register_vcpu_tb_exec_cond_cb(
569 [](
unsigned int cpu_index,
void* userdata) {
574 QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE, delta_insn, m_global_quantum, handle_as_userdata());
585 if (m_shutdown.load(std::memory_order_acquire))
return;
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);
591 switch (vcpu->cpu_execution_status) {
593 SCP_DEBUG(()) <<
"cpu_" << cpu_index <<
" RUNNING → IDLE";
594 vcpu->cpu_execution_status = IDLE;
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";
601 m_qemu_time += delta;
603 vcpu->cpu_time += delta;
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 <<
")";
612 detach_sync_window();
614 new_active->cpu_time = m_qemu_time;
615 new_active->delta_insn = 0;
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));
623 vcpu->delta_insn = 0;
628 SCP_DEBUG(()) <<
"vcpu_idle: cpu_" << cpu_index <<
" already PAUSED";
632 SCP_DEBUG(()) <<
"vcpu_idle: cpu_" << cpu_index <<
" already IDLE";
636 SCP_FATAL(()) <<
"vcpu_idle: invalid execution status for cpu_" << cpu_index;
652 if (m_shutdown.load(std::memory_order_acquire))
return;
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);
658 switch (vcpu->cpu_execution_status) {
660 SCP_DEBUG(()) <<
"cpu_" << cpu_index <<
" IDLE → RUNNING";
661 vcpu->cpu_execution_status = RUNNING;
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(),
667 if (sc_now > m_qemu_time) {
668 m_qemu_time = sc_now;
672 vcpu->cpu_time = m_qemu_time;
673 m_active_vcpu.store(vcpu, std::memory_order_release);
675 attach_sync_window();
677 SCP_DEBUG(()) <<
"[idle_pump] STOP vcpu_resume: cpu_" << cpu_index <<
" first to resume";
682 const auto* active = m_active_vcpu.load(std::memory_order_relaxed);
684 const sc_core::sc_time current_qemu_time =
qemu_time_now(active);
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";
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";
699 SCP_DEBUG(()) <<
"vcpu_resume: cpu_" << cpu_index <<
" already RUNNING";
703 SCP_FATAL(()) <<
"vcpu_resume: invalid execution status for cpu_" << cpu_index;
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);
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);
735 int64_t systemc_time_now =
static_cast<int64_t
>(sc_core::sc_time_stamp().to_seconds() *
737 if (systemc_time_now > qemu_time) {
738 qemu_time = systemc_time_now;
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)";
748 qemu_time +=
static_cast<int64_t
>(
cpu_delta_time(active).to_seconds() * NSEC_IN_ONE_SEC);
751 int64_t prev = m_wd_last_clock_ns.load(std::memory_order_relaxed);
752 if (prev != qemu_time) {
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);
757 const int64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
758 std::chrono::steady_clock::now().time_since_epoch())
760 int64_t start = m_wd_freeze_start_ns.load(std::memory_order_relaxed);
763 m_wd_freeze_start_ns.compare_exchange_strong(start, now_ns, std::memory_order_relaxed);
764 }
else if (now_ns - start > kWdFreezeThresholdNs) {
766 m_idle_tick.notify(sc_core::SC_ZERO_TIME);
767 m_wd_freeze_start_ns.store(now_ns, std::memory_order_relaxed);
782 auto active = m_active_vcpu.load(std::memory_order_relaxed);
783 std::ostringstream os;
785 const uint64_t qemu_time =
static_cast<uint64_t
>(
qemu_time_now().to_seconds() * NSEC_IN_ONE_SEC);
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\":[";
792 for (
int i = 0; i < m_num_vcpus; i++) {
796 if (!first) os <<
",";
799 const uint64_t ns =
static_cast<uint64_t
>(vcpu->cpu_time.to_seconds() * NSEC_IN_ONE_SEC);
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) <<
"}";
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