apic_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * APIC support - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2004-2005 Fabrice Bellard
  5. * Copyright (c) 2011 Jan Kiszka, Siemens AG
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/error-report.h"
  22. #include "qemu/module.h"
  23. #include "qapi/error.h"
  24. #include "cpu.h"
  25. #include "qapi/visitor.h"
  26. #include "hw/i386/apic.h"
  27. #include "hw/i386/apic_internal.h"
  28. #include "trace.h"
  29. #include "sysemu/hax.h"
  30. #include "sysemu/kvm.h"
  31. #include "hw/qdev-properties.h"
  32. #include "hw/sysbus.h"
  33. #include "migration/vmstate.h"
  34. static int apic_irq_delivered;
  35. bool apic_report_tpr_access;
  36. void cpu_set_apic_base(DeviceState *dev, uint64_t val)
  37. {
  38. trace_cpu_set_apic_base(val);
  39. if (dev) {
  40. APICCommonState *s = APIC_COMMON(dev);
  41. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  42. /* switching to x2APIC, reset possibly modified xAPIC ID */
  43. if (!(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  44. (val & MSR_IA32_APICBASE_EXTD)) {
  45. s->id = s->initial_apic_id;
  46. }
  47. info->set_base(s, val);
  48. }
  49. }
  50. uint64_t cpu_get_apic_base(DeviceState *dev)
  51. {
  52. if (dev) {
  53. APICCommonState *s = APIC_COMMON(dev);
  54. trace_cpu_get_apic_base((uint64_t)s->apicbase);
  55. return s->apicbase;
  56. } else {
  57. trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
  58. return MSR_IA32_APICBASE_BSP;
  59. }
  60. }
  61. void cpu_set_apic_tpr(DeviceState *dev, uint8_t val)
  62. {
  63. APICCommonState *s;
  64. APICCommonClass *info;
  65. if (!dev) {
  66. return;
  67. }
  68. s = APIC_COMMON(dev);
  69. info = APIC_COMMON_GET_CLASS(s);
  70. info->set_tpr(s, val);
  71. }
  72. uint8_t cpu_get_apic_tpr(DeviceState *dev)
  73. {
  74. APICCommonState *s;
  75. APICCommonClass *info;
  76. if (!dev) {
  77. return 0;
  78. }
  79. s = APIC_COMMON(dev);
  80. info = APIC_COMMON_GET_CLASS(s);
  81. return info->get_tpr(s);
  82. }
  83. void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable)
  84. {
  85. APICCommonState *s = APIC_COMMON(dev);
  86. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  87. apic_report_tpr_access = enable;
  88. if (info->enable_tpr_reporting) {
  89. info->enable_tpr_reporting(s, enable);
  90. }
  91. }
  92. void apic_enable_vapic(DeviceState *dev, hwaddr paddr)
  93. {
  94. APICCommonState *s = APIC_COMMON(dev);
  95. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  96. s->vapic_paddr = paddr;
  97. info->vapic_base_update(s);
  98. }
  99. void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip,
  100. TPRAccess access)
  101. {
  102. APICCommonState *s = APIC_COMMON(dev);
  103. vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
  104. }
  105. void apic_report_irq_delivered(int delivered)
  106. {
  107. apic_irq_delivered += delivered;
  108. trace_apic_report_irq_delivered(apic_irq_delivered);
  109. }
  110. void apic_reset_irq_delivered(void)
  111. {
  112. /* Copy this into a local variable to encourage gcc to emit a plain
  113. * register for a sys/sdt.h marker. For details on this workaround, see:
  114. * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
  115. */
  116. volatile int a_i_d = apic_irq_delivered;
  117. trace_apic_reset_irq_delivered(a_i_d);
  118. apic_irq_delivered = 0;
  119. }
  120. int apic_get_irq_delivered(void)
  121. {
  122. trace_apic_get_irq_delivered(apic_irq_delivered);
  123. return apic_irq_delivered;
  124. }
  125. void apic_deliver_nmi(DeviceState *dev)
  126. {
  127. APICCommonState *s = APIC_COMMON(dev);
  128. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  129. info->external_nmi(s);
  130. }
  131. bool apic_next_timer(APICCommonState *s, int64_t current_time)
  132. {
  133. int64_t d;
  134. /* We need to store the timer state separately to support APIC
  135. * implementations that maintain a non-QEMU timer, e.g. inside the
  136. * host kernel. This open-coded state allows us to migrate between
  137. * both models. */
  138. s->timer_expiry = -1;
  139. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
  140. return false;
  141. }
  142. d = (current_time - s->initial_count_load_time) >> s->count_shift;
  143. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  144. if (!s->initial_count) {
  145. return false;
  146. }
  147. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
  148. ((uint64_t)s->initial_count + 1);
  149. } else {
  150. if (d >= s->initial_count) {
  151. return false;
  152. }
  153. d = (uint64_t)s->initial_count + 1;
  154. }
  155. s->next_time = s->initial_count_load_time + (d << s->count_shift);
  156. s->timer_expiry = s->next_time;
  157. return true;
  158. }
  159. void apic_init_reset(DeviceState *dev)
  160. {
  161. APICCommonState *s;
  162. APICCommonClass *info;
  163. int i;
  164. if (!dev) {
  165. return;
  166. }
  167. s = APIC_COMMON(dev);
  168. s->tpr = 0;
  169. s->spurious_vec = 0xff;
  170. s->log_dest = 0;
  171. s->dest_mode = 0xf;
  172. memset(s->isr, 0, sizeof(s->isr));
  173. memset(s->tmr, 0, sizeof(s->tmr));
  174. memset(s->irr, 0, sizeof(s->irr));
  175. for (i = 0; i < APIC_LVT_NB; i++) {
  176. s->lvt[i] = APIC_LVT_MASKED;
  177. }
  178. s->esr = 0;
  179. memset(s->icr, 0, sizeof(s->icr));
  180. s->divide_conf = 0;
  181. s->count_shift = 0;
  182. s->initial_count = 0;
  183. s->initial_count_load_time = 0;
  184. s->next_time = 0;
  185. s->wait_for_sipi = !cpu_is_bsp(s->cpu);
  186. if (s->timer) {
  187. timer_del(s->timer);
  188. }
  189. s->timer_expiry = -1;
  190. info = APIC_COMMON_GET_CLASS(s);
  191. if (info->reset) {
  192. info->reset(s);
  193. }
  194. }
  195. void apic_designate_bsp(DeviceState *dev, bool bsp)
  196. {
  197. if (dev == NULL) {
  198. return;
  199. }
  200. APICCommonState *s = APIC_COMMON(dev);
  201. if (bsp) {
  202. s->apicbase |= MSR_IA32_APICBASE_BSP;
  203. } else {
  204. s->apicbase &= ~MSR_IA32_APICBASE_BSP;
  205. }
  206. }
  207. static void apic_reset_common(DeviceState *dev)
  208. {
  209. APICCommonState *s = APIC_COMMON(dev);
  210. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  211. uint32_t bsp;
  212. bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
  213. s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
  214. s->id = s->initial_apic_id;
  215. apic_reset_irq_delivered();
  216. s->vapic_paddr = 0;
  217. info->vapic_base_update(s);
  218. apic_init_reset(dev);
  219. }
  220. static const VMStateDescription vmstate_apic_common;
  221. static void apic_common_realize(DeviceState *dev, Error **errp)
  222. {
  223. APICCommonState *s = APIC_COMMON(dev);
  224. APICCommonClass *info;
  225. static DeviceState *vapic;
  226. int instance_id = s->id;
  227. info = APIC_COMMON_GET_CLASS(s);
  228. info->realize(dev, errp);
  229. /* Note: We need at least 1M to map the VAPIC option ROM */
  230. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  231. !hax_enabled() && ram_size >= 1024 * 1024) {
  232. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  233. }
  234. s->vapic = vapic;
  235. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  236. info->enable_tpr_reporting(s, true);
  237. }
  238. if (s->legacy_instance_id) {
  239. instance_id = -1;
  240. }
  241. vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
  242. s, -1, 0, NULL);
  243. }
  244. static void apic_common_unrealize(DeviceState *dev, Error **errp)
  245. {
  246. APICCommonState *s = APIC_COMMON(dev);
  247. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  248. vmstate_unregister(NULL, &vmstate_apic_common, s);
  249. info->unrealize(dev, errp);
  250. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  251. info->enable_tpr_reporting(s, false);
  252. }
  253. }
  254. static int apic_pre_load(void *opaque)
  255. {
  256. APICCommonState *s = APIC_COMMON(opaque);
  257. /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
  258. * so that's what apic_common_sipi_needed checks for. Reset to
  259. * the value that is assumed when the apic_sipi subsection is
  260. * absent.
  261. */
  262. s->wait_for_sipi = 0;
  263. return 0;
  264. }
  265. static int apic_dispatch_pre_save(void *opaque)
  266. {
  267. APICCommonState *s = APIC_COMMON(opaque);
  268. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  269. if (info->pre_save) {
  270. info->pre_save(s);
  271. }
  272. return 0;
  273. }
  274. static int apic_dispatch_post_load(void *opaque, int version_id)
  275. {
  276. APICCommonState *s = APIC_COMMON(opaque);
  277. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  278. if (info->post_load) {
  279. info->post_load(s);
  280. }
  281. return 0;
  282. }
  283. static bool apic_common_sipi_needed(void *opaque)
  284. {
  285. APICCommonState *s = APIC_COMMON(opaque);
  286. return s->wait_for_sipi != 0;
  287. }
  288. static const VMStateDescription vmstate_apic_common_sipi = {
  289. .name = "apic_sipi",
  290. .version_id = 1,
  291. .minimum_version_id = 1,
  292. .needed = apic_common_sipi_needed,
  293. .fields = (VMStateField[]) {
  294. VMSTATE_INT32(sipi_vector, APICCommonState),
  295. VMSTATE_INT32(wait_for_sipi, APICCommonState),
  296. VMSTATE_END_OF_LIST()
  297. }
  298. };
  299. static const VMStateDescription vmstate_apic_common = {
  300. .name = "apic",
  301. .version_id = 3,
  302. .minimum_version_id = 3,
  303. .pre_load = apic_pre_load,
  304. .pre_save = apic_dispatch_pre_save,
  305. .post_load = apic_dispatch_post_load,
  306. .fields = (VMStateField[]) {
  307. VMSTATE_UINT32(apicbase, APICCommonState),
  308. VMSTATE_UINT8(id, APICCommonState),
  309. VMSTATE_UINT8(arb_id, APICCommonState),
  310. VMSTATE_UINT8(tpr, APICCommonState),
  311. VMSTATE_UINT32(spurious_vec, APICCommonState),
  312. VMSTATE_UINT8(log_dest, APICCommonState),
  313. VMSTATE_UINT8(dest_mode, APICCommonState),
  314. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  315. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  316. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  317. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  318. VMSTATE_UINT32(esr, APICCommonState),
  319. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  320. VMSTATE_UINT32(divide_conf, APICCommonState),
  321. VMSTATE_INT32(count_shift, APICCommonState),
  322. VMSTATE_UINT32(initial_count, APICCommonState),
  323. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  324. VMSTATE_INT64(next_time, APICCommonState),
  325. VMSTATE_INT64(timer_expiry,
  326. APICCommonState), /* open-coded timer state */
  327. VMSTATE_END_OF_LIST()
  328. },
  329. .subsections = (const VMStateDescription*[]) {
  330. &vmstate_apic_common_sipi,
  331. NULL
  332. }
  333. };
  334. static Property apic_properties_common[] = {
  335. DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
  336. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  337. true),
  338. DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
  339. false),
  340. DEFINE_PROP_END_OF_LIST(),
  341. };
  342. static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
  343. void *opaque, Error **errp)
  344. {
  345. APICCommonState *s = APIC_COMMON(obj);
  346. uint32_t value;
  347. value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
  348. visit_type_uint32(v, name, &value, errp);
  349. }
  350. static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
  351. void *opaque, Error **errp)
  352. {
  353. APICCommonState *s = APIC_COMMON(obj);
  354. DeviceState *dev = DEVICE(obj);
  355. Error *local_err = NULL;
  356. uint32_t value;
  357. if (dev->realized) {
  358. qdev_prop_set_after_realize(dev, name, errp);
  359. return;
  360. }
  361. visit_type_uint32(v, name, &value, &local_err);
  362. if (local_err) {
  363. error_propagate(errp, local_err);
  364. return;
  365. }
  366. s->initial_apic_id = value;
  367. s->id = (uint8_t)value;
  368. }
  369. static void apic_common_initfn(Object *obj)
  370. {
  371. APICCommonState *s = APIC_COMMON(obj);
  372. s->id = s->initial_apic_id = -1;
  373. object_property_add(obj, "id", "uint32",
  374. apic_common_get_id,
  375. apic_common_set_id, NULL, NULL, NULL);
  376. }
  377. static void apic_common_class_init(ObjectClass *klass, void *data)
  378. {
  379. DeviceClass *dc = DEVICE_CLASS(klass);
  380. dc->reset = apic_reset_common;
  381. dc->props = apic_properties_common;
  382. dc->realize = apic_common_realize;
  383. dc->unrealize = apic_common_unrealize;
  384. /*
  385. * Reason: APIC and CPU need to be wired up by
  386. * x86_cpu_apic_create()
  387. */
  388. dc->user_creatable = false;
  389. }
  390. static const TypeInfo apic_common_type = {
  391. .name = TYPE_APIC_COMMON,
  392. .parent = TYPE_DEVICE,
  393. .instance_size = sizeof(APICCommonState),
  394. .instance_init = apic_common_initfn,
  395. .class_size = sizeof(APICCommonClass),
  396. .class_init = apic_common_class_init,
  397. .abstract = true,
  398. };
  399. static void apic_common_register_types(void)
  400. {
  401. type_register_static(&apic_common_type);
  402. }
  403. type_init(apic_common_register_types)