accel-target.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "accel/accel-cpu-target.h"
  29. #ifndef CONFIG_USER_ONLY
  30. #include "accel-system.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. .abstract = true,
  38. };
  39. /* Lookup AccelClass from opt_name. Returns NULL if not found */
  40. AccelClass *accel_find(const char *opt_name)
  41. {
  42. char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
  43. AccelClass *ac = ACCEL_CLASS(module_object_class_by_name(class_name));
  44. g_free(class_name);
  45. return ac;
  46. }
  47. /* Return the name of the current accelerator */
  48. const char *current_accel_name(void)
  49. {
  50. AccelClass *ac = ACCEL_GET_CLASS(current_accel());
  51. return ac->name;
  52. }
  53. static void accel_init_cpu_int_aux(ObjectClass *klass, void *opaque)
  54. {
  55. CPUClass *cc = CPU_CLASS(klass);
  56. AccelCPUClass *accel_cpu = opaque;
  57. /*
  58. * The first callback allows accel-cpu to run initializations
  59. * for the CPU, customizing CPU behavior according to the accelerator.
  60. *
  61. * The second one allows the CPU to customize the accel-cpu
  62. * behavior according to the CPU.
  63. *
  64. * The second is currently only used by TCG, to specialize the
  65. * TCGCPUOps depending on the CPU type.
  66. */
  67. cc->accel_cpu = accel_cpu;
  68. if (accel_cpu->cpu_class_init) {
  69. accel_cpu->cpu_class_init(cc);
  70. }
  71. if (cc->init_accel_cpu) {
  72. cc->init_accel_cpu(accel_cpu, cc);
  73. }
  74. }
  75. /* initialize the arch-specific accel CpuClass interfaces */
  76. static void accel_init_cpu_interfaces(AccelClass *ac)
  77. {
  78. const char *ac_name; /* AccelClass name */
  79. char *acc_name; /* AccelCPUClass name */
  80. ObjectClass *acc; /* AccelCPUClass */
  81. ac_name = object_class_get_name(OBJECT_CLASS(ac));
  82. g_assert(ac_name != NULL);
  83. acc_name = g_strdup_printf("%s-%s", ac_name, CPU_RESOLVING_TYPE);
  84. acc = object_class_by_name(acc_name);
  85. g_free(acc_name);
  86. if (acc) {
  87. object_class_foreach(accel_init_cpu_int_aux,
  88. CPU_RESOLVING_TYPE, false, acc);
  89. }
  90. }
  91. void accel_init_interfaces(AccelClass *ac)
  92. {
  93. #ifndef CONFIG_USER_ONLY
  94. accel_system_init_ops_interfaces(ac);
  95. #endif /* !CONFIG_USER_ONLY */
  96. accel_init_cpu_interfaces(ac);
  97. }
  98. void accel_cpu_instance_init(CPUState *cpu)
  99. {
  100. if (cpu->cc->accel_cpu && cpu->cc->accel_cpu->cpu_instance_init) {
  101. cpu->cc->accel_cpu->cpu_instance_init(cpu);
  102. }
  103. }
  104. bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
  105. {
  106. AccelState *accel = current_accel();
  107. AccelClass *acc = ACCEL_GET_CLASS(accel);
  108. /* target specific realization */
  109. if (cpu->cc->accel_cpu
  110. && cpu->cc->accel_cpu->cpu_target_realize
  111. && !cpu->cc->accel_cpu->cpu_target_realize(cpu, errp)) {
  112. return false;
  113. }
  114. /* generic realization */
  115. if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. void accel_cpu_common_unrealize(CPUState *cpu)
  121. {
  122. AccelState *accel = current_accel();
  123. AccelClass *acc = ACCEL_GET_CLASS(accel);
  124. /* generic unrealization */
  125. if (acc->cpu_common_unrealize) {
  126. acc->cpu_common_unrealize(cpu);
  127. }
  128. }
  129. int accel_supported_gdbstub_sstep_flags(void)
  130. {
  131. AccelState *accel = current_accel();
  132. AccelClass *acc = ACCEL_GET_CLASS(accel);
  133. if (acc->gdbstub_supported_sstep_flags) {
  134. return acc->gdbstub_supported_sstep_flags();
  135. }
  136. return 0;
  137. }
  138. static const TypeInfo accel_cpu_type = {
  139. .name = TYPE_ACCEL_CPU,
  140. .parent = TYPE_OBJECT,
  141. .abstract = true,
  142. .class_size = sizeof(AccelCPUClass),
  143. };
  144. static void register_accel_types(void)
  145. {
  146. type_register_static(&accel_type);
  147. type_register_static(&accel_cpu_type);
  148. }
  149. type_init(register_accel_types);