hostmem-epc.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * QEMU host SGX EPC memory backend
  3. *
  4. * Copyright (C) 2019 Intel Corporation
  5. *
  6. * Authors:
  7. * Sean Christopherson <sean.j.christopherson@intel.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include <sys/ioctl.h>
  14. #include "qom/object_interfaces.h"
  15. #include "qapi/error.h"
  16. #include "system/hostmem.h"
  17. #include "hw/i386/hostmem-epc.h"
  18. static bool
  19. sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
  20. {
  21. g_autofree char *name = NULL;
  22. uint32_t ram_flags;
  23. int fd;
  24. if (!backend->size) {
  25. error_setg(errp, "can't create backend with size 0");
  26. return false;
  27. }
  28. fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
  29. if (fd < 0) {
  30. return false;
  31. }
  32. backend->aligned = true;
  33. name = object_get_canonical_path(OBJECT(backend));
  34. ram_flags = (backend->share ? RAM_SHARED : RAM_PRIVATE) | RAM_PROTECTED;
  35. return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
  36. backend->size, ram_flags, fd, 0, errp);
  37. }
  38. static void sgx_epc_backend_instance_init(Object *obj)
  39. {
  40. HostMemoryBackend *m = MEMORY_BACKEND(obj);
  41. m->share = true;
  42. m->merge = false;
  43. m->dump = false;
  44. }
  45. static void sgx_epc_backend_class_init(ObjectClass *oc, void *data)
  46. {
  47. HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
  48. bc->alloc = sgx_epc_backend_memory_alloc;
  49. }
  50. static const TypeInfo sgx_epc_backed_info = {
  51. .name = TYPE_MEMORY_BACKEND_EPC,
  52. .parent = TYPE_MEMORY_BACKEND,
  53. .instance_init = sgx_epc_backend_instance_init,
  54. .class_init = sgx_epc_backend_class_init,
  55. .instance_size = sizeof(HostMemoryBackendEpc),
  56. };
  57. static void register_types(void)
  58. {
  59. int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
  60. if (fd >= 0) {
  61. close(fd);
  62. type_register_static(&sgx_epc_backed_info);
  63. }
  64. }
  65. type_init(register_types);