qualcomm/qbox
Loading...
Searching...
No Matches
libqemu-cxx.h
1/*
2 * This file is part of libqemu-cxx
3 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2015-2019
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#pragma once
10
11#include <string>
12#include <string_view>
13#include <memory>
14#include <limits>
15#include <functional>
16#include <set>
17#include <vector>
18#include <map>
19#include <atomic>
20
21#include <libqemu-cxx/target_info.h>
22#include <libqemu-cxx/exceptions.h>
23#include <dynlib_loader.h>
24
25/* Brings in the generated LibQemuExports struct and all plugin types
26 * (qemu_plugin_id_t, qemu_plugin_tb, qemu_plugin_cb_flags, callbacks, ...)
27 */
28#include <libqemu/libqemu.h>
29
30#include <scp/report.h>
31
32/* libqemu types forward declaration */
33struct LibQemuExports;
34struct QemuObject;
35struct ObjectClass;
36struct DisplayGLCtxOps;
37struct DisplayGLCtx;
38struct QemuConsole;
39struct DisplaySurface;
40struct DisplayOptions;
41struct QEMUCursor;
42struct SDL_Window;
43struct sdl2_console;
44struct DisplayChangeListenerOps;
45struct DisplayChangeListener;
46struct MemTxAttrs;
47struct QemuMemoryRegion;
48struct QemuMemoryRegionOps;
49struct QemuIOMMUMemoryRegion;
50struct QemuAddressSpace;
51struct QemuMemoryListener;
52struct QemuTimer;
53struct FWCfgState;
54typedef void* QEMUGLContext;
55struct QEMUGLParams;
56typedef void (*LibQemuGfxUpdateFn)(DisplayChangeListener*, int, int, int, int);
57typedef void (*LibQemuGfxSwitchFn)(DisplayChangeListener*, DisplaySurface*);
58typedef void (*LibQemuRefreshFn)(DisplayChangeListener*);
59typedef void (*LibQemuWindowCreateFn)(DisplayChangeListener*);
60typedef void (*LibQemuWindowDestroyFn)(DisplayChangeListener*);
61typedef void (*LibQemuWindowResizeFn)(DisplayChangeListener*);
62typedef void (*LibQemuPollEventsFn)(DisplayChangeListener*);
63typedef void (*LibQemuMouseSetFn)(DisplayChangeListener*, int, int, int);
64typedef void (*LibQemuCursorDefineFn)(DisplayChangeListener*, QEMUCursor*);
65typedef void (*LibQemuGLScanoutDisableFn)(DisplayChangeListener*);
66typedef void (*LibQemuGLScanoutTextureFn)(DisplayChangeListener*, uint32_t, bool, uint32_t, uint32_t, uint32_t,
67 uint32_t, uint32_t, uint32_t);
68typedef void (*LibQemuGLUpdateFn)(DisplayChangeListener*, uint32_t, uint32_t, uint32_t, uint32_t);
69
70typedef bool (*LibQemuIsCompatibleDclFn)(DisplayGLCtx*, DisplayChangeListener*);
71
72namespace qemu {
73
75class Object;
76class MemoryRegion;
77class MemoryRegionOps;
79class AddressSpace;
80class MemoryListener;
81class Gpio;
82class Timer;
83class Bus;
84class Chardev;
85class Clock;
86class DisplayOptions;
87class DisplayGLCtxOps;
88class Console;
89class SDL2Console;
90class Dcl;
91class DclOps;
92class RcuReadLock;
93
94class LibQemu
95{
96private:
97 std::shared_ptr<LibQemuInternals> m_int;
98 LibraryLoaderIface& m_library_loader;
99 const char* m_lib_path;
100 Target m_target;
101 bool m_auto_start;
102
103 std::vector<char*> m_qemu_argv;
104
105 LibraryLoaderIface::LibraryIfacePtr m_lib;
106
107 QemuObject* object_new_unparented(const char* type_name);
108 QemuObject* object_new_internal(const char* type_name, const char* id);
109
110 void check_cast(Object& o, const char* type);
111
112 void init_callbacks();
113
114 void rcu_read_lock();
115 void rcu_read_unlock();
116
117public:
118 LibQemu(LibraryLoaderIface& library_loader, const char* lib_path);
119 LibQemu(LibraryLoaderIface& library_loader, Target t);
120 ~LibQemu();
121
122 /* TCG plugin API: returns the libqemu-exports table. */
123 const LibQemuExports& plugin_api() const;
124
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; }
128
129 void init();
130 bool is_inited() const { return m_lib != nullptr; }
131
132 /* QEMU GDB stub
133 * @port: port the gdb server will be listening on. (ex: "tcp::1234") */
134 void start_gdb_server(std::string port);
135 void vm_start();
136 void vm_stop_paused();
137
138 void lock_iothread();
139 void unlock_iothread();
140 bool is_iothread_locked() const;
141
142 RcuReadLock rcu_read_lock_new();
143
144 void finish_qemu_init();
145 Bus sysbus_get_default();
146
147 void coroutine_yield();
148
149 void system_reset();
150
151 template <class T>
152 T object_new()
153 {
154 T o(Object(object_new_internal(T::TYPE, NULL), m_int));
155 check_cast(o, T::TYPE);
156
157 return o;
158 }
159
160 template <class T>
161 T object_new_unparented()
162 {
163 T o(Object(object_new_unparented(T::TYPE), m_int));
164 check_cast(o, T::TYPE);
165
166 return o;
167 }
168 int64_t get_virtual_clock();
169
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();
176 Gpio gpio_new();
177
178 std::shared_ptr<Timer> timer_new();
179
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);
186
187 void tb_invalidate_phys_range(uint64_t start, uint64_t end);
188
189 void enable_opengl();
190 DisplayOptions display_options_new();
191 std::vector<Console> get_all_consoles();
192 Console console_lookup_by_index(int index);
193 DisplayGLCtxOps display_gl_ctx_ops_new(LibQemuIsCompatibleDclFn);
194 Dcl dcl_new(DisplayChangeListener* dcl);
195 DclOps dcl_ops_new();
196
197 int sdl2_init() const;
198 const char* sdl2_get_error() const;
199
200 std::vector<SDL2Console> sdl2_create_consoles(int num);
201 void sdl2_cleanup();
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);
205
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);
209
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);
214
215 void dcl_dpy_gfx_replace_surface(DisplayChangeListener* dcl, DisplaySurface* new_surface);
216
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);
220
221 bool virgl_has_blob() const;
222
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);
228};
229
230class RcuReadLock
231{
232private:
233 std::shared_ptr<LibQemuInternals> m_int;
234 RcuReadLock() = default;
235
236public:
237 RcuReadLock(std::shared_ptr<LibQemuInternals> internals);
238 ~RcuReadLock();
239
240 RcuReadLock(const RcuReadLock&) = delete;
241 RcuReadLock& operator=(const RcuReadLock&) = delete;
242 RcuReadLock(RcuReadLock&&);
243 RcuReadLock& operator=(RcuReadLock&&);
244};
245
246class Object
247{
248protected:
249 QemuObject* m_obj = nullptr;
250 std::shared_ptr<LibQemuInternals> m_int;
251
252 bool check_cast_by_type(const char* type_name) const { return true; }
253
254public:
255 Object() = default;
256 Object(QemuObject* obj, std::shared_ptr<LibQemuInternals>& internals);
257 Object(const Object& o);
258 Object(Object&& o);
259
260 Object& operator=(Object o);
261
262 virtual ~Object();
263
264 bool valid() const { return m_obj != nullptr; }
265
266 void set_prop_bool(const char* name, bool val);
267 void set_prop_int(const char* name, int64_t val);
268 void set_prop_uint(const char* name, uint64_t val);
269 void set_prop_str(const char* name, const char* val);
270 void set_prop_link(const char* name, const Object& link);
271 void set_prop_parse(const char* name, const char* value);
272
273 Object get_prop_link(const char* name);
274
275 QemuObject* get_qemu_obj() const { return m_obj; }
276
277 LibQemu& get_inst();
278 uintptr_t get_inst_id() const { return reinterpret_cast<uintptr_t>(m_int.get()); }
279 bool same_inst_as(const Object& o) const { return get_inst_id() == o.get_inst_id(); }
280
281 template <class T>
282 bool check_cast() const
283 {
284 return check_cast_by_type(T::TYPE);
285 }
286
287 void clear_callbacks();
288};
289
290class Gpio : public Object
291{
292public:
293 typedef std::function<void(bool)> GpioEventFn;
294
296 {
297 protected:
298 bool m_prev_valid = false;
299 bool m_prev;
300 GpioEventFn m_cb;
301 std::atomic<bool> m_cb_set{ false };
302
303 public:
304 void event(bool level)
305 {
306 if (!m_prev_valid || (level != m_prev)) {
307 if (m_cb_set.load(std::memory_order_acquire)) m_cb(level);
308 }
309
310 m_prev_valid = true;
311 m_prev = level;
312 }
313
314 void set_callback(GpioEventFn cb)
315 {
316 if (cb) {
317 m_cb = std::move(cb);
318 m_cb_set.store(true, std::memory_order_release);
319 } else {
320 m_cb_set.store(false, std::memory_order_release);
321 }
322 }
323 };
324
325private:
326 std::shared_ptr<GpioProxy> m_proxy;
327
328public:
329 static constexpr const char* const TYPE = "irq";
330
331 Gpio() = default;
332 Gpio(const Gpio& o) = default;
333 Gpio(const Object& o): Object(o) {}
334
335 void set(bool lvl);
336
337 void set_proxy(std::shared_ptr<GpioProxy> proxy) { m_proxy = proxy; }
338
339 void set_event_callback(GpioEventFn cb)
340 {
341 if (m_proxy) {
342 m_proxy->set_callback(cb);
343 }
344 }
345};
346
347class MemoryRegionOps
348{
349public:
350 enum MemTxResult { MemTxOK, MemTxError, MemTxDecodeError, MemTxOKExitTB };
351
352 struct MemTxAttrs {
353 bool secure = false;
354 uint8_t space = 0;
355 bool user = false;
356 bool memory = false;
357 bool debug = false;
358 uint16_t requester_id = 0;
359 uint8_t pid = 0;
360 bool address_type = false;
361 bool unspecified = false;
362
363 MemTxAttrs() = default;
364 MemTxAttrs(const ::MemTxAttrs& qemu_attrs);
365 };
366
367 typedef std::function<MemTxResult(uint64_t, uint64_t*, unsigned int, MemTxAttrs)> ReadCallback;
368
369 typedef std::function<MemTxResult(uint64_t, uint64_t, unsigned int, MemTxAttrs)> WriteCallback;
370
371private:
372 QemuMemoryRegionOps* m_ops;
373 std::shared_ptr<LibQemuInternals> m_int;
374
375 ReadCallback m_read_cb;
376 WriteCallback m_write_cb;
377
378public:
379 MemoryRegionOps(QemuMemoryRegionOps* ops, std::shared_ptr<LibQemuInternals> internals);
380 ~MemoryRegionOps();
381
382 void set_read_callback(ReadCallback cb);
383 void set_write_callback(WriteCallback cb);
384
385 void set_max_access_size(unsigned size);
386
387 ReadCallback get_read_callback() { return m_read_cb; }
388 WriteCallback get_write_callback() { return m_write_cb; }
389
390 QemuMemoryRegionOps* get_qemu_mr_ops() { return m_ops; }
391};
392
393typedef std::shared_ptr<MemoryRegionOps> MemoryRegionOpsPtr;
394
395class MemoryRegion : public Object
396{
397private:
398 MemoryRegionOpsPtr m_ops;
399 std::set<MemoryRegion> m_subregions;
400 int m_priority = 0;
401
402 void internal_del_subregion(const MemoryRegion& mr);
403
404public:
405 using MemTxResult = MemoryRegionOps::MemTxResult;
406 using MemTxAttrs = MemoryRegionOps::MemTxAttrs;
407
408 static constexpr const char* const TYPE = "memory-region";
409
410 MemoryRegion* container = nullptr;
411
412 MemoryRegion() = default;
413 MemoryRegion(const MemoryRegion&) = default;
414 MemoryRegion(const Object& o): Object(o) {}
415 MemoryRegion(QemuMemoryRegion* mr, std::shared_ptr<LibQemuInternals> internals);
416
417 ~MemoryRegion();
418 void removeSubRegions();
419
420 uint64_t get_size();
421 int get_priority() const { return m_priority; }
422 /* Make sure to set the priority before calling `add_subregion_overlap()` */
423 void set_priority(int priority) { m_priority = priority; }
424
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);
429
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);
433
434 MemTxResult dispatch_read(uint64_t addr, uint64_t* data, uint64_t size, MemTxAttrs attrs);
435
436 MemTxResult dispatch_write(uint64_t addr, uint64_t data, uint64_t size, MemTxAttrs attrs);
437
438 void set_ops(const MemoryRegionOpsPtr ops);
439
440 bool operator<(const MemoryRegion& mr) const { return m_obj < mr.m_obj; }
441};
442
443class AddressSpace
444{
445private:
446 QemuAddressSpace* m_as;
447 std::shared_ptr<LibQemuInternals> m_int;
448
449 bool m_inited = false;
450 bool m_global = false;
451
452public:
453 using MemTxResult = MemoryRegionOps::MemTxResult;
454 using MemTxAttrs = MemoryRegionOps::MemTxAttrs;
455
456 AddressSpace(QemuAddressSpace* as, std::shared_ptr<LibQemuInternals> internals);
457 AddressSpace(const AddressSpace&) = delete;
458
459 ~AddressSpace();
460
461 QemuAddressSpace* get_ptr() { return m_as; }
462
463 void init(MemoryRegion mr, const char* name, bool global = false);
464
465 MemTxResult read(uint64_t addr, void* data, size_t size, MemTxAttrs attrs);
466 MemTxResult write(uint64_t addr, const void* data, size_t size, MemTxAttrs attrs);
467
468 void update_topology();
469};
470
471/* this is simply used to hold shared_ptr's for memory management and
472 * to check if the DMIRegions exist.
473 */
475};
476class IOMMUMemoryRegion : public MemoryRegion
477{
478public:
479 static constexpr const char* const TYPE = "libqemu-iommu-memory-region";
480
481 qemu::MemoryRegion m_root_io;
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;
485
486 IOMMUMemoryRegion(const Object& o)
487 : MemoryRegion(o)
488 , m_root_io(get_inst().object_new_unparented<qemu::MemoryRegion>())
489 , m_as_io(get_inst().address_space_new())
490 {
491 }
492
493 typedef enum {
494 IOMMU_NONE = 0,
495 IOMMU_RO = 1,
496 IOMMU_WO = 2,
497 IOMMU_RW = 3,
498 } IOMMUAccessFlags;
499
501 QemuAddressSpace* target_as;
502 uint64_t iova;
503 uint64_t translated_addr;
504 uint64_t addr_mask;
505 IOMMUAccessFlags perm;
506 };
507
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;
513
514 explicit TranslationContext(LibQemu& inst)
515 : root(inst.object_new_unparented<qemu::MemoryRegion>()), as(inst.address_space_new())
516 {
517 }
518 };
519
520 using IOMMUTranslateCallbackFn = std::function<void(IOMMUTLBEntry*, uint64_t, IOMMUAccessFlags, int)>;
521 using IOMMUAttrsToIndexCallbackFn = std::function<int(MemoryRegionOps::MemTxAttrs)>;
522 using IOMMUNumIndexesCallbackFn = std::function<int()>;
523 using IOMMUIndexToAttrsCallbackFn = std::function<MemoryRegionOps::MemTxAttrs(int)>;
524
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
529 {
530 return m_index_to_attrs_cb ? m_index_to_attrs_cb(idx) : MemoryRegionOps::MemTxAttrs{};
531 }
532 TranslationContext& translation_context(int idx)
533 {
534 auto& context = m_translation_contexts[idx];
535 if (!context) {
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");
539 }
540 return *context;
541 }
542 std::map<int, std::unique_ptr<TranslationContext>>& translation_contexts() { return m_translation_contexts; }
543 void iommu_unmap(IOMMUTLBEntry*);
544
545private:
546 IOMMUIndexToAttrsCallbackFn m_index_to_attrs_cb;
547 std::map<int, std::unique_ptr<TranslationContext>> m_translation_contexts;
548};
549
550class MemoryListener
551{
552public:
553 typedef std::function<void(MemoryListener&, uint64_t addr, uint64_t len)> MapCallback;
554
555private:
556 QemuMemoryListener* m_ml;
557 std::shared_ptr<LibQemuInternals> m_int;
558 std::shared_ptr<AddressSpace> m_as;
559
560 MapCallback m_map_cb;
561
562public:
563 MemoryListener(std::shared_ptr<LibQemuInternals> internals);
564 MemoryListener(const MemoryListener&) = delete;
565 ~MemoryListener();
566
567 void set_ml(QemuMemoryListener* ml);
568
569 void set_map_callback(MapCallback cb);
570 MapCallback& get_map_callback() { return m_map_cb; }
571
572 void register_as(std::shared_ptr<AddressSpace> as);
573};
574
575class DisplayOptions
576{
577public:
578 ::DisplayOptions* m_opts;
579 std::shared_ptr<LibQemuInternals> m_int;
580
581 DisplayOptions() = default;
582 DisplayOptions(::DisplayOptions* opts, std::shared_ptr<LibQemuInternals>& internals);
583 DisplayOptions(const DisplayOptions&) = default;
584};
585
586class DisplayGLCtxOps
587{
588public:
589 ::DisplayGLCtxOps* m_ops;
590 std::shared_ptr<LibQemuInternals> m_int;
591
592 DisplayGLCtxOps() = default;
593 DisplayGLCtxOps(::DisplayGLCtxOps* ops, std::shared_ptr<LibQemuInternals>& internals);
594 DisplayGLCtxOps(const DisplayGLCtxOps&) = default;
595};
596
597class Console
598{
599public:
600 QemuConsole* m_cons;
601 std::shared_ptr<LibQemuInternals> m_int;
602
603 Console() = default;
604 Console(QemuConsole* cons, std::shared_ptr<LibQemuInternals>& internals);
605 Console(const Console&) = default;
606
607 int get_index() const;
608 bool is_graphic() const;
609 void set_display_gl_ctx(DisplayGLCtx*);
610 void set_window_id(int id);
611};
612
613class SDL2Console
614{
615public:
616 struct sdl2_console* m_cons;
617 std::shared_ptr<LibQemuInternals> m_int;
618
619 SDL2Console() = default;
620 SDL2Console(struct sdl2_console* cons, std::shared_ptr<LibQemuInternals>& internals);
621 SDL2Console(const SDL2Console&) = default;
622
623 void init(Console& con, void* user_data);
624 void set_hidden(bool hidden);
625 void set_idx(int idx);
626 void set_opts(DisplayOptions& opts);
627 void set_opengl(bool opengl);
628 void set_dcl_ops(DclOps& dcl_ops);
629 void set_dgc_ops(DisplayGLCtxOps& dgc_ops);
630
631 SDL_Window* get_real_window() const;
632 DisplayChangeListener* get_dcl() const;
633 DisplayGLCtx* get_dgc() const;
634
635 void register_dcl() const;
636
637 void set_window_id(Console& con) const;
638};
639
640class Dcl
641{
642private:
643 DisplayChangeListener* m_dcl;
644 std::shared_ptr<LibQemuInternals> m_int;
645
646public:
647 Dcl(DisplayChangeListener* dcl, std::shared_ptr<LibQemuInternals>& internals);
648
649 void* get_user_data();
650};
651
652class DclOps
653{
654public:
655 DisplayChangeListenerOps* m_ops;
656 std::shared_ptr<LibQemuInternals> m_int;
657
658 DclOps() = default;
659 DclOps(DisplayChangeListenerOps* ops, std::shared_ptr<LibQemuInternals>& internals);
660 DclOps(const DclOps&) = default;
661
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);
667
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);
672};
673
674class Device : public Object
675{
676public:
677 static constexpr const char* const TYPE = "device";
678
679 Device() = default;
680 Device(const Device&) = default;
681 Device(const Object& o): Object(o) {}
682
683 void connect_gpio_out(int idx, Gpio gpio);
684 void connect_gpio_out_named(const char* name, int idx, Gpio gpio);
685
686 Gpio get_gpio_in(int idx);
687 Gpio get_gpio_in_named(const char* name, int idx);
688
689 Bus get_child_bus(const char* name);
690 void set_parent_bus(Bus bus);
691
692 void set_prop_chardev(const char* name, Chardev chr);
693 void set_prop_uint_array(const char* name, std::vector<unsigned int> vec);
694 void set_prop_string(const char* name, const char* value);
695 void set_prop_string(std::string_view name, std::string_view value);
696};
697
698class SysBusDevice : public Device
699{
700public:
701 static constexpr const char* const TYPE = "sys-bus-device";
702
703 SysBusDevice() = default;
704 SysBusDevice(const SysBusDevice&) = default;
705 SysBusDevice(const Object& o): Device(o) {}
706
707 MemoryRegion mmio_get_region(int id);
708
709 void connect_gpio_out(int idx, Gpio gpio);
710};
711
712class GpexHost : public SysBusDevice
713{
714public:
715 static constexpr const char* const TYPE = "gpex-pcihost";
716
717 GpexHost() = default;
718 GpexHost(const GpexHost&) = default;
719 GpexHost(const Object& o): SysBusDevice(o) {}
720
721 void set_irq_num(int idx, int gic_irq);
722};
723
724class Cpu : public Device
725{
726public:
727 static constexpr const char* const TYPE = "cpu";
728
729 using EndOfLoopCallbackFn = std::function<void()>;
730 using CpuKickCallbackFn = std::function<void()>;
731 using AsyncJobFn = std::function<void()>;
732
733 Cpu() = default;
734 Cpu(const Cpu&) = default;
735 Cpu(const Object& o): Device(o) {}
736
737 int get_index() const;
738
739 void loop();
740 bool loop_is_busy();
741
742 bool can_run();
743
744 void set_soft_stopped(bool stopped);
745 void halt(bool halted);
746
747 void reset(bool reset); /* Reset: on true, enter reset, call cpu_pause and cpu_reset
748 * on false exit reset and call cpu_resume */
749
750 void set_unplug(bool unplug);
751 void remove_sync();
752
753 void register_thread();
754
755 Cpu set_as_current();
756
757 void kick();
758
759 [[noreturn]] void exit_loop_from_io();
760
761 void async_run(AsyncJobFn job);
762 void async_safe_run(AsyncJobFn job);
763
764 void set_end_of_loop_callback(EndOfLoopCallbackFn cb);
765 void set_kick_callback(CpuKickCallbackFn cb);
766
767 bool is_in_exclusive_context() const;
768
769 void set_vcpu_dirty(bool dirty) const;
770};
771
772class Timer
773{
774public:
775 typedef std::function<void()> TimerCallbackFn;
776
777private:
778 std::shared_ptr<LibQemuInternals> m_int;
779 QemuTimer* m_timer = nullptr;
780 TimerCallbackFn m_cb;
781
782public:
783 Timer(std::shared_ptr<LibQemuInternals> internals);
784 ~Timer();
785
786 void set_callback(TimerCallbackFn cb);
787
788 void mod(int64_t deadline);
789 void del();
790};
791
792class Bus : public Object
793{
794public:
795 static constexpr const char* const TYPE = "bus";
796
797 Bus() = default;
798 Bus(const Bus& o) = default;
799 Bus(const Object& o): Object(o) {}
800};
801
802class Chardev : public Object
803{
804public:
805 static constexpr const char* const TYPE = "chardev";
806
807 Chardev() = default;
808 Chardev(const Chardev& o) = default;
809 Chardev(const Object& o): Object(o) {}
810};
811
812class Clock : public Object
813{
814public:
815 static constexpr const char* const TYPE = "clock";
816
817 Clock() = default;
818 Clock(const Clock&) = default;
819 Clock(const Object& o): Object(o) {}
820};
821
822}; /* namespace qemu */
Definition libqemu-cxx.h:444
Definition libqemu-cxx.h:793
Definition libqemu-cxx.h:803
Definition libqemu-cxx.h:813
Definition libqemu-cxx.h:598
Definition libqemu-cxx.h:653
Definition libqemu-cxx.h:641
Definition libqemu-cxx.h:587
Definition libqemu-cxx.h:576
Definition libqemu-cxx.h:296
Definition libqemu-cxx.h:291
Definition libqemu-cxx.h:477
Definition internals.h:70
Definition libqemu-cxx.h:95
Definition dynlib_loader.h:23
Definition libqemu-cxx.h:551
Definition libqemu-cxx.h:348
Definition libqemu-cxx.h:396
Definition libqemu-cxx.h:247
Definition libqemu-cxx.h:231
Definition libqemu-cxx.h:614
Definition libqemu-cxx.h:773
Definition libqemu-cxx.h:474
Definition libqemu-cxx.h:500
Definition libqemu-cxx.h:352