accel-common.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * QEMU accel class, components common to system emulation and user mode
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2014 Red Hat Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/accel.h"
  27. #include "cpu.h"
  28. #include "hw/core/accel-cpu.h"
  29. #ifndef CONFIG_USER_ONLY
  30. #include "accel-softmmu.h"
  31. #endif /* !CONFIG_USER_ONLY */
  32. static const TypeInfo accel_type = {
  33. .name = TYPE_ACCEL,
  34. .parent = TYPE_OBJECT,
  35. .class_size = sizeof(AccelClass),
  36. .instance_size = sizeof(AccelState),
  37. };
  38. /* Lookup AccelClass from opt_name. Returns NULL if not found */
  39. AccelClass *accel_find(const char *opt_name)
  40. {
  41. char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
  42. AccelClass *ac = ACCEL_CLASS(module_object_class_by_name(class_name));
  43. g_free(class_name);
  44. return ac;
  45. }
  46. /* Return the name of the current accelerator */
  47. const char *current_accel_name(void)
  48. {
  49. AccelClass *ac = ACCEL_GET_CLASS(current_accel());
  50. return ac->name;
  51. }
  52. static void accel_init_cpu_int_aux(ObjectClass *klass, void *opaque)
  53. {
  54. CPUClass *cc = CPU_CLASS(klass);
  55. AccelCPUClass *accel_cpu = opaque;
  56. /*
  57. * The first callback allows accel-cpu to run initializations
  58. * for the CPU, customizing CPU behavior according to the accelerator.
  59. *
  60. * The second one allows the CPU to customize the accel-cpu
  61. * behavior according to the CPU.
  62. *
  63. * The second is currently only used by TCG, to specialize the
  64. * TCGCPUOps depending on the CPU type.
  65. */
  66. cc->accel_cpu = accel_cpu;
  67. if (accel_cpu->cpu_class_init) {
  68. accel_cpu->cpu_class_init(cc);
  69. }
  70. if (cc->init_accel_cpu) {
  71. cc->init_accel_cpu(accel_cpu, cc);
  72. }
  73. }
  74. /* initialize the arch-specific accel CpuClass interfaces */
  75. static void accel_init_cpu_interfaces(AccelClass *ac)
  76. {
  77. const char *ac_name; /* AccelClass name */
  78. char *acc_name; /* AccelCPUClass name */
  79. ObjectClass *acc; /* AccelCPUClass */
  80. ac_name = object_class_get_name(OBJECT_CLASS(ac));
  81. g_assert(ac_name != NULL);
  82. acc_name = g_strdup_printf("%s-%s", ac_name, CPU_RESOLVING_TYPE);
  83. acc = object_class_by_name(acc_name);
  84. g_free(acc_name);
  85. if (acc) {
  86. object_class_foreach(accel_init_cpu_int_aux,
  87. CPU_RESOLVING_TYPE, false, acc);
  88. }
  89. }
  90. void accel_init_interfaces(AccelClass *ac)
  91. {
  92. #ifndef CONFIG_USER_ONLY
  93. accel_init_ops_interfaces(ac);
  94. #endif /* !CONFIG_USER_ONLY */
  95. accel_init_cpu_interfaces(ac);
  96. }
  97. void accel_cpu_instance_init(CPUState *cpu)
  98. {
  99. CPUClass *cc = CPU_GET_CLASS(cpu);
  100. if (cc->accel_cpu && cc->accel_cpu->cpu_instance_init) {
  101. cc->accel_cpu->cpu_instance_init(cpu);
  102. }
  103. }
  104. bool accel_cpu_realizefn(CPUState *cpu, Error **errp)
  105. {
  106. CPUClass *cc = CPU_GET_CLASS(cpu);
  107. if (cc->accel_cpu && cc->accel_cpu->cpu_realizefn) {
  108. return cc->accel_cpu->cpu_realizefn(cpu, errp);
  109. }
  110. return true;
  111. }
  112. int accel_supported_gdbstub_sstep_flags(void)
  113. {
  114. AccelState *accel = current_accel();
  115. AccelClass *acc = ACCEL_GET_CLASS(accel);
  116. if (acc->gdbstub_supported_sstep_flags) {
  117. return acc->gdbstub_supported_sstep_flags();
  118. }
  119. return 0;
  120. }
  121. static const TypeInfo accel_cpu_type = {
  122. .name = TYPE_ACCEL_CPU,
  123. .parent = TYPE_OBJECT,
  124. .abstract = true,
  125. .class_size = sizeof(AccelCPUClass),
  126. };
  127. static void register_accel_types(void)
  128. {
  129. type_register_static(&accel_type);
  130. type_register_static(&accel_cpu_type);
  131. }
  132. type_init(register_accel_types);