66typedef void (*LibQemuGLScanoutTextureFn)(DisplayChangeListener*, uint32_t, bool, uint32_t, uint32_t, uint32_t,
97 std::shared_ptr<LibQemuInternals> m_int;
99 const char* m_lib_path;
103 std::vector<char*> m_qemu_argv;
105 LibraryLoaderIface::LibraryIfacePtr m_lib;
107 QemuObject* object_new_unparented(
const char* type_name);
108 QemuObject* object_new_internal(
const char* type_name,
const char*
id);
110 void check_cast(
Object& o,
const char* type);
112 void init_callbacks();
114 void rcu_read_lock();
115 void rcu_read_unlock();
123 const LibQemuExports& plugin_api()
const;
125 void push_qemu_arg(
const char* arg);
126 void push_qemu_arg(std::initializer_list<const char*> args);
127 const std::vector<char*>& get_qemu_args()
const {
return m_qemu_argv; }
130 bool is_inited()
const {
return m_lib !=
nullptr; }
134 void start_gdb_server(std::string port);
136 void vm_stop_paused();
138 void lock_iothread();
139 void unlock_iothread();
140 bool is_iothread_locked()
const;
144 void finish_qemu_init();
145 Bus sysbus_get_default();
147 void coroutine_yield();
154 T o(
Object(object_new_internal(T::TYPE, NULL), m_int));
155 check_cast(o, T::TYPE);
161 T object_new_unparented()
163 T o(
Object(object_new_unparented(T::TYPE), m_int));
164 check_cast(o, T::TYPE);
168 int64_t get_virtual_clock();
170 Object object_new(
const char* type_name,
const char*
id);
171 std::shared_ptr<MemoryRegionOps> memory_region_ops_new();
172 std::shared_ptr<AddressSpace> address_space_new();
173 std::shared_ptr<AddressSpace> address_space_get_system_memory();
174 std::shared_ptr<MemoryRegion> get_system_memory();
175 std::shared_ptr<MemoryListener> memory_listener_new();
178 std::shared_ptr<Timer> timer_new();
180 Chardev chardev_new(
const char* label,
const char* type);
181 Chardev chardev_new(std::string_view label, std::string_view type);
182 Clock clock_new(QemuObject* o,
const char* label);
183 Clock clock_new(QemuObject* o, std::string_view label);
184 bool clock_set_hz(
Clock& clock, uint64_t hz);
185 void qdev_connect_clock_in(QemuObject* o,
const char* name,
Clock& clk);
187 void tb_invalidate_phys_range(uint64_t start, uint64_t end);
189 void enable_opengl();
191 std::vector<Console> get_all_consoles();
192 Console console_lookup_by_index(
int index);
194 Dcl dcl_new(DisplayChangeListener* dcl);
197 int sdl2_init()
const;
198 const char* sdl2_get_error()
const;
200 std::vector<SDL2Console> sdl2_create_consoles(
int num);
202 void sdl2_2d_update(DisplayChangeListener* dcl,
int x,
int y,
int w,
int h);
203 void sdl2_2d_switch(DisplayChangeListener* dcl, DisplaySurface* new_surface);
204 void sdl2_2d_refresh(DisplayChangeListener* dcl);
206 void sdl2_gl_update(DisplayChangeListener* dcl,
int x,
int y,
int w,
int h);
207 void sdl2_gl_switch(DisplayChangeListener* dcl, DisplaySurface* new_surface);
208 void sdl2_gl_refresh(DisplayChangeListener* dcl);
210 void sdl2_window_create(DisplayChangeListener* dcl);
211 void sdl2_window_destroy(DisplayChangeListener* dcl);
212 void sdl2_window_resize(DisplayChangeListener* dcl);
213 void sdl2_poll_events(DisplayChangeListener* dcl);
215 void dcl_dpy_gfx_replace_surface(DisplayChangeListener* dcl, DisplaySurface* new_surface);
217 QEMUGLContext sdl2_gl_create_context(DisplayGLCtx* dgc, QEMUGLParams* p);
218 void sdl2_gl_destroy_context(DisplayGLCtx* dgc, QEMUGLContext gl_ctx);
219 int sdl2_gl_make_context_current(DisplayGLCtx* dgc, QEMUGLContext gl_ctx);
221 bool virgl_has_blob()
const;
223 FWCfgState* fw_cfg_find();
224 void fw_cfg_set_dma_as(FWCfgState* s);
225 void fw_cfg_add_i16(FWCfgState* s, uint16_t key, uint16_t value);
226 void fw_cfg_add_file(FWCfgState* s,
const char* filename,
void* data,
size_t len);
227 void* fw_cfg_modify_file(FWCfgState* s,
const char* filename,
void* data,
size_t len);
350 enum MemTxResult { MemTxOK, MemTxError, MemTxDecodeError, MemTxOKExitTB };
358 uint16_t requester_id = 0;
360 bool address_type =
false;
361 bool unspecified =
false;
363 MemTxAttrs() =
default;
364 MemTxAttrs(const ::MemTxAttrs& qemu_attrs);
367 typedef std::function<MemTxResult(uint64_t, uint64_t*,
unsigned int,
MemTxAttrs)> ReadCallback;
369 typedef std::function<MemTxResult(uint64_t, uint64_t,
unsigned int,
MemTxAttrs)> WriteCallback;
372 QemuMemoryRegionOps* m_ops;
373 std::shared_ptr<LibQemuInternals> m_int;
375 ReadCallback m_read_cb;
376 WriteCallback m_write_cb;
379 MemoryRegionOps(QemuMemoryRegionOps* ops, std::shared_ptr<LibQemuInternals> internals);
382 void set_read_callback(ReadCallback cb);
383 void set_write_callback(WriteCallback cb);
385 void set_max_access_size(
unsigned size);
387 ReadCallback get_read_callback() {
return m_read_cb; }
388 WriteCallback get_write_callback() {
return m_write_cb; }
390 QemuMemoryRegionOps* get_qemu_mr_ops() {
return m_ops; }
395class MemoryRegion :
public Object
398 MemoryRegionOpsPtr m_ops;
399 std::set<MemoryRegion> m_subregions;
402 void internal_del_subregion(
const MemoryRegion& mr);
405 using MemTxResult = MemoryRegionOps::MemTxResult;
408 static constexpr const char*
const TYPE =
"memory-region";
410 MemoryRegion* container =
nullptr;
412 MemoryRegion() =
default;
413 MemoryRegion(
const MemoryRegion&) =
default;
414 MemoryRegion(
const Object& o): Object(o) {}
415 MemoryRegion(QemuMemoryRegion* mr, std::shared_ptr<LibQemuInternals> internals);
418 void removeSubRegions();
421 int get_priority()
const {
return m_priority; }
423 void set_priority(
int priority) { m_priority = priority; }
425 void init(
const Object& owner,
const char* name, uint64_t size);
426 void init_io(Object owner,
const char* name, uint64_t size, MemoryRegionOpsPtr ops);
427 void init_ram_ptr(Object owner,
const char* name, uint64_t size,
void* ptr,
int fd = -1);
428 void init_alias(Object owner,
const char* name,
const MemoryRegion& root, uint64_t offset, uint64_t size);
430 void add_subregion(MemoryRegion& mr, uint64_t offset);
431 void add_subregion_overlap(MemoryRegion& mr, uint64_t offset);
432 void del_subregion(
const MemoryRegion& mr);
434 MemTxResult dispatch_read(uint64_t addr, uint64_t* data, uint64_t size, MemTxAttrs attrs);
436 MemTxResult dispatch_write(uint64_t addr, uint64_t data, uint64_t size, MemTxAttrs attrs);
438 void set_ops(
const MemoryRegionOpsPtr ops);
440 bool operator<(
const MemoryRegion& mr)
const {
return m_obj < mr.m_obj; }
476class IOMMUMemoryRegion :
public MemoryRegion
479 static constexpr const char*
const TYPE =
"libqemu-iommu-memory-region";
482 std::shared_ptr<qemu::AddressSpace> m_as_io;
483 std::map<uint64_t, std::shared_ptr<DmiRegionBase>> m_dmi_aliases_io;
484 uint64_t min_page_sz;
486 IOMMUMemoryRegion(
const Object& o)
488 , m_root_io(get_inst().object_new_unparented<qemu::MemoryRegion>())
489 , m_as_io(get_inst().address_space_new())
501 QemuAddressSpace* target_as;
503 uint64_t translated_addr;
505 IOMMUAccessFlags perm;
508 struct TranslationContext {
510 std::shared_ptr<qemu::AddressSpace> as;
511 std::map<uint64_t, std::shared_ptr<DmiRegionBase>> dmi_aliases;
512 std::map<uint64_t, IOMMUTLBEntry> mappings;
514 explicit TranslationContext(
LibQemu& inst)
515 : root(inst.object_new_unparented<
qemu::MemoryRegion>()), as(inst.address_space_new())
520 using IOMMUTranslateCallbackFn = std::function<void(
IOMMUTLBEntry*, uint64_t, IOMMUAccessFlags,
int)>;
522 using IOMMUNumIndexesCallbackFn = std::function<int()>;
525 void init(
const Object& owner,
const char* name, uint64_t size, MemoryRegionOpsPtr ops, IOMMUTranslateCallbackFn cb,
526 IOMMUAttrsToIndexCallbackFn attrs_to_index_cb = {}, IOMMUNumIndexesCallbackFn num_indexes_cb = {},
527 IOMMUIndexToAttrsCallbackFn index_to_attrs_cb = {});
528 MemoryRegionOps::MemTxAttrs attrs_for_index(
int idx)
const
530 return m_index_to_attrs_cb ? m_index_to_attrs_cb(idx) : MemoryRegionOps::MemTxAttrs{};
534 auto& context = m_translation_contexts[idx];
536 context = std::make_unique<TranslationContext>(get_inst());
537 context->root.init(*
this,
"IOMMU translated root", std::numeric_limits<uint64_t>::max());
538 context->as->init(context->root,
"IOMMU translated AddressSpace");
542 std::map<int, std::unique_ptr<TranslationContext>>& translation_contexts() {
return m_translation_contexts; }
546 IOMMUIndexToAttrsCallbackFn m_index_to_attrs_cb;
547 std::map<int, std::unique_ptr<TranslationContext>> m_translation_contexts;
553 typedef std::function<void(MemoryListener&, uint64_t addr, uint64_t len)> MapCallback;
556 QemuMemoryListener* m_ml;
557 std::shared_ptr<LibQemuInternals> m_int;
558 std::shared_ptr<AddressSpace> m_as;
560 MapCallback m_map_cb;
563 MemoryListener(std::shared_ptr<LibQemuInternals> internals);
564 MemoryListener(
const MemoryListener&) =
delete;
567 void set_ml(QemuMemoryListener* ml);
569 void set_map_callback(MapCallback cb);
570 MapCallback& get_map_callback() {
return m_map_cb; }
572 void register_as(std::shared_ptr<AddressSpace> as);
655 DisplayChangeListenerOps* m_ops;
656 std::shared_ptr<LibQemuInternals> m_int;
659 DclOps(DisplayChangeListenerOps* ops, std::shared_ptr<LibQemuInternals>& internals);
660 DclOps(
const DclOps&) =
default;
662 void set_name(
const char* name);
663 bool is_used_by(DisplayChangeListener* dcl)
const;
664 void set_gfx_update(LibQemuGfxUpdateFn gfx_update_fn);
665 void set_gfx_switch(LibQemuGfxSwitchFn gfx_switch_fn);
666 void set_refresh(LibQemuRefreshFn refresh_fn);
668 void set_window_create(LibQemuWindowCreateFn window_create_fn);
669 void set_window_destroy(LibQemuWindowDestroyFn window_destroy_fn);
670 void set_window_resize(LibQemuWindowResizeFn window_resize_fn);
671 void set_poll_events(LibQemuPollEventsFn poll_events_fn);
724class Cpu :
public Device
727 static constexpr const char*
const TYPE =
"cpu";
729 using EndOfLoopCallbackFn = std::function<void()>;
730 using CpuKickCallbackFn = std::function<void()>;
731 using AsyncJobFn = std::function<void()>;
734 Cpu(
const Cpu&) =
default;
735 Cpu(
const Object& o): Device(o) {}
737 int get_index()
const;
744 void set_soft_stopped(
bool stopped);
745 void halt(
bool halted);
747 void reset(
bool reset);
750 void set_unplug(
bool unplug);
753 void register_thread();
755 Cpu set_as_current();
759 [[noreturn]]
void exit_loop_from_io();
761 void async_run(AsyncJobFn job);
762 void async_safe_run(AsyncJobFn job);
764 void set_end_of_loop_callback(EndOfLoopCallbackFn cb);
765 void set_kick_callback(CpuKickCallbackFn cb);
767 bool is_in_exclusive_context()
const;
769 void set_vcpu_dirty(
bool dirty)
const;