kvmxx.hh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef KVMXX_H
  2. #define KVMXX_H
  3. #include <string>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <vector>
  7. #include <errno.h>
  8. #include <linux/kvm.h>
  9. #include <stdint.h>
  10. namespace kvm {
  11. class system;
  12. class vm;
  13. class vcpu;
  14. class fd;
  15. class fd {
  16. public:
  17. explicit fd(int n);
  18. explicit fd(std::string path, int flags);
  19. fd(const fd& other);
  20. ~fd() { ::close(_fd); }
  21. int get() { return _fd; }
  22. long ioctl(unsigned nr, long arg);
  23. long ioctlp(unsigned nr, void *arg) {
  24. return ioctl(nr, reinterpret_cast<long>(arg));
  25. }
  26. private:
  27. int _fd;
  28. };
  29. class vcpu {
  30. public:
  31. vcpu(vm& vm, int fd);
  32. ~vcpu();
  33. void run();
  34. kvm_run *shared();
  35. kvm_regs regs();
  36. void set_regs(const kvm_regs& regs);
  37. kvm_sregs sregs();
  38. void set_sregs(const kvm_sregs& sregs);
  39. std::vector<kvm_msr_entry> msrs(std::vector<uint32_t> indices);
  40. void set_msrs(const std::vector<kvm_msr_entry>& msrs);
  41. void set_debug(uint64_t dr[8], bool enabled, bool singlestep);
  42. private:
  43. class kvm_msrs_ptr;
  44. private:
  45. vm& _vm;
  46. fd _fd;
  47. kvm_run *_shared;
  48. unsigned _mmap_size;
  49. friend class vm;
  50. };
  51. class vm {
  52. public:
  53. explicit vm(system& system);
  54. void set_memory_region(int slot, void *addr, uint64_t gpa, size_t len,
  55. uint32_t flags = 0);
  56. void get_dirty_log(int slot, void *log);
  57. void set_tss_addr(uint32_t addr);
  58. void set_ept_identity_map_addr(uint64_t addr);
  59. system& sys() { return _system; }
  60. private:
  61. system& _system;
  62. fd _fd;
  63. friend class system;
  64. friend class vcpu;
  65. };
  66. class system {
  67. public:
  68. explicit system(std::string device_node = "/dev/kvm");
  69. bool check_extension(int extension);
  70. int get_extension_int(int extension);
  71. private:
  72. fd _fd;
  73. friend class vcpu;
  74. friend class vm;
  75. };
  76. };
  77. #endif