apic_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 "qapi/error.h"
  23. #include "qemu-common.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.h"
  32. #include "hw/sysbus.h"
  33. static int apic_irq_delivered;
  34. bool apic_report_tpr_access;
  35. void cpu_set_apic_base(DeviceState *dev, uint64_t val)
  36. {
  37. trace_cpu_set_apic_base(val);
  38. if (dev) {
  39. APICCommonState *s = APIC_COMMON(dev);
  40. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  41. /* switching to x2APIC, reset possibly modified xAPIC ID */
  42. if (!(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  43. (val & MSR_IA32_APICBASE_EXTD)) {
  44. s->id = s->initial_apic_id;
  45. }
  46. info->set_base(s, val);
  47. }
  48. }
  49. uint64_t cpu_get_apic_base(DeviceState *dev)
  50. {
  51. if (dev) {
  52. APICCommonState *s = APIC_COMMON(dev);
  53. trace_cpu_get_apic_base((uint64_t)s->apicbase);
  54. return s->apicbase;
  55. } else {
  56. trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
  57. return MSR_IA32_APICBASE_BSP;
  58. }
  59. }
  60. void cpu_set_apic_tpr(DeviceState *dev, uint8_t val)
  61. {
  62. APICCommonState *s;
  63. APICCommonClass *info;
  64. if (!dev) {
  65. return;
  66. }
  67. s = APIC_COMMON(dev);
  68. info = APIC_COMMON_GET_CLASS(s);
  69. info->set_tpr(s, val);
  70. }
  71. uint8_t cpu_get_apic_tpr(DeviceState *dev)
  72. {
  73. APICCommonState *s;
  74. APICCommonClass *info;
  75. if (!dev) {
  76. return 0;
  77. }
  78. s = APIC_COMMON(dev);
  79. info = APIC_COMMON_GET_CLASS(s);
  80. return info->get_tpr(s);
  81. }
  82. void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable)
  83. {
  84. APICCommonState *s = APIC_COMMON(dev);
  85. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  86. apic_report_tpr_access = enable;
  87. if (info->enable_tpr_reporting) {
  88. info->enable_tpr_reporting(s, enable);
  89. }
  90. }
  91. void apic_enable_vapic(DeviceState *dev, hwaddr paddr)
  92. {
  93. APICCommonState *s = APIC_COMMON(dev);
  94. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  95. s->vapic_paddr = paddr;
  96. info->vapic_base_update(s);
  97. }
  98. void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip,
  99. TPRAccess access)
  100. {
  101. APICCommonState *s = APIC_COMMON(dev);
  102. vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
  103. }
  104. void apic_report_irq_delivered(int delivered)
  105. {
  106. apic_irq_delivered += delivered;
  107. trace_apic_report_irq_delivered(apic_irq_delivered);
  108. }
  109. void apic_reset_irq_delivered(void)
  110. {
  111. /* Copy this into a local variable to encourage gcc to emit a plain
  112. * register for a sys/sdt.h marker. For details on this workaround, see:
  113. * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
  114. */
  115. volatile int a_i_d = apic_irq_delivered;
  116. trace_apic_reset_irq_delivered(a_i_d);
  117. apic_irq_delivered = 0;
  118. }
  119. int apic_get_irq_delivered(void)
  120. {
  121. trace_apic_get_irq_delivered(apic_irq_delivered);
  122. return apic_irq_delivered;
  123. }
  124. void apic_deliver_nmi(DeviceState *dev)
  125. {
  126. APICCommonState *s = APIC_COMMON(dev);
  127. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  128. info->external_nmi(s);
  129. }
  130. bool apic_next_timer(APICCommonState *s, int64_t current_time)
  131. {
  132. int64_t d;
  133. /* We need to store the timer state separately to support APIC
  134. * implementations that maintain a non-QEMU timer, e.g. inside the
  135. * host kernel. This open-coded state allows us to migrate between
  136. * both models. */
  137. s->timer_expiry = -1;
  138. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
  139. return false;
  140. }
  141. d = (current_time - s->initial_count_load_time) >> s->count_shift;
  142. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  143. if (!s->initial_count) {
  144. return false;
  145. }
  146. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
  147. ((uint64_t)s->initial_count + 1);
  148. } else {
  149. if (d >= s->initial_count) {
  150. return false;
  151. }
  152. d = (uint64_t)s->initial_count + 1;
  153. }
  154. s->next_time = s->initial_count_load_time + (d << s->count_shift);
  155. s->timer_expiry = s->next_time;
  156. return true;
  157. }
  158. void apic_init_reset(DeviceState *dev)
  159. {
  160. APICCommonState *s;
  161. APICCommonClass *info;
  162. int i;
  163. if (!dev) {
  164. return;
  165. }
  166. s = APIC_COMMON(dev);
  167. s->tpr = 0;
  168. s->spurious_vec = 0xff;
  169. s->log_dest = 0;
  170. s->dest_mode = 0xf;
  171. memset(s->isr, 0, sizeof(s->isr));
  172. memset(s->tmr, 0, sizeof(s->tmr));
  173. memset(s->irr, 0, sizeof(s->irr));
  174. for (i = 0; i < APIC_LVT_NB; i++) {
  175. s->lvt[i] = APIC_LVT_MASKED;
  176. }
  177. s->esr = 0;
  178. memset(s->icr, 0, sizeof(s->icr));
  179. s->divide_conf = 0;
  180. s->count_shift = 0;
  181. s->initial_count = 0;
  182. s->initial_count_load_time = 0;
  183. s->next_time = 0;
  184. s->wait_for_sipi = !cpu_is_bsp(s->cpu);
  185. if (s->timer) {
  186. timer_del(s->timer);
  187. }
  188. s->timer_expiry = -1;
  189. info = APIC_COMMON_GET_CLASS(s);
  190. if (info->reset) {
  191. info->reset(s);
  192. }
  193. }
  194. void apic_designate_bsp(DeviceState *dev, bool bsp)
  195. {
  196. if (dev == NULL) {
  197. return;
  198. }
  199. APICCommonState *s = APIC_COMMON(dev);
  200. if (bsp) {
  201. s->apicbase |= MSR_IA32_APICBASE_BSP;
  202. } else {
  203. s->apicbase &= ~MSR_IA32_APICBASE_BSP;
  204. }
  205. }
  206. static void apic_reset_common(DeviceState *dev)
  207. {
  208. APICCommonState *s = APIC_COMMON(dev);
  209. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  210. uint32_t bsp;
  211. bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
  212. s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
  213. s->id = s->initial_apic_id;
  214. s->vapic_paddr = 0;
  215. info->vapic_base_update(s);
  216. apic_init_reset(dev);
  217. }
  218. /* This function is only used for old state version 1 and 2 */
  219. static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
  220. {
  221. APICCommonState *s = opaque;
  222. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  223. int i;
  224. if (version_id > 2) {
  225. return -EINVAL;
  226. }
  227. /* XXX: what if the base changes? (registered memory regions) */
  228. qemu_get_be32s(f, &s->apicbase);
  229. qemu_get_8s(f, &s->id);
  230. qemu_get_8s(f, &s->arb_id);
  231. qemu_get_8s(f, &s->tpr);
  232. qemu_get_be32s(f, &s->spurious_vec);
  233. qemu_get_8s(f, &s->log_dest);
  234. qemu_get_8s(f, &s->dest_mode);
  235. for (i = 0; i < 8; i++) {
  236. qemu_get_be32s(f, &s->isr[i]);
  237. qemu_get_be32s(f, &s->tmr[i]);
  238. qemu_get_be32s(f, &s->irr[i]);
  239. }
  240. for (i = 0; i < APIC_LVT_NB; i++) {
  241. qemu_get_be32s(f, &s->lvt[i]);
  242. }
  243. qemu_get_be32s(f, &s->esr);
  244. qemu_get_be32s(f, &s->icr[0]);
  245. qemu_get_be32s(f, &s->icr[1]);
  246. qemu_get_be32s(f, &s->divide_conf);
  247. s->count_shift = qemu_get_be32(f);
  248. qemu_get_be32s(f, &s->initial_count);
  249. s->initial_count_load_time = qemu_get_be64(f);
  250. s->next_time = qemu_get_be64(f);
  251. if (version_id >= 2) {
  252. s->timer_expiry = qemu_get_be64(f);
  253. }
  254. if (info->post_load) {
  255. info->post_load(s);
  256. }
  257. return 0;
  258. }
  259. static const VMStateDescription vmstate_apic_common;
  260. static void apic_common_realize(DeviceState *dev, Error **errp)
  261. {
  262. APICCommonState *s = APIC_COMMON(dev);
  263. APICCommonClass *info;
  264. static DeviceState *vapic;
  265. int instance_id = s->id;
  266. info = APIC_COMMON_GET_CLASS(s);
  267. info->realize(dev, errp);
  268. /* Note: We need at least 1M to map the VAPIC option ROM */
  269. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  270. !hax_enabled() && ram_size >= 1024 * 1024) {
  271. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  272. }
  273. s->vapic = vapic;
  274. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  275. info->enable_tpr_reporting(s, true);
  276. }
  277. if (s->legacy_instance_id) {
  278. instance_id = -1;
  279. }
  280. vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
  281. s, -1, 0);
  282. }
  283. static void apic_common_unrealize(DeviceState *dev, Error **errp)
  284. {
  285. APICCommonState *s = APIC_COMMON(dev);
  286. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  287. vmstate_unregister(NULL, &vmstate_apic_common, s);
  288. info->unrealize(dev, errp);
  289. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  290. info->enable_tpr_reporting(s, false);
  291. }
  292. }
  293. static int apic_pre_load(void *opaque)
  294. {
  295. APICCommonState *s = APIC_COMMON(opaque);
  296. /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
  297. * so that's what apic_common_sipi_needed checks for. Reset to
  298. * the value that is assumed when the apic_sipi subsection is
  299. * absent.
  300. */
  301. s->wait_for_sipi = 0;
  302. return 0;
  303. }
  304. static void apic_dispatch_pre_save(void *opaque)
  305. {
  306. APICCommonState *s = APIC_COMMON(opaque);
  307. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  308. if (info->pre_save) {
  309. info->pre_save(s);
  310. }
  311. }
  312. static int apic_dispatch_post_load(void *opaque, int version_id)
  313. {
  314. APICCommonState *s = APIC_COMMON(opaque);
  315. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  316. if (info->post_load) {
  317. info->post_load(s);
  318. }
  319. return 0;
  320. }
  321. static bool apic_common_sipi_needed(void *opaque)
  322. {
  323. APICCommonState *s = APIC_COMMON(opaque);
  324. return s->wait_for_sipi != 0;
  325. }
  326. static const VMStateDescription vmstate_apic_common_sipi = {
  327. .name = "apic_sipi",
  328. .version_id = 1,
  329. .minimum_version_id = 1,
  330. .needed = apic_common_sipi_needed,
  331. .fields = (VMStateField[]) {
  332. VMSTATE_INT32(sipi_vector, APICCommonState),
  333. VMSTATE_INT32(wait_for_sipi, APICCommonState),
  334. VMSTATE_END_OF_LIST()
  335. }
  336. };
  337. static const VMStateDescription vmstate_apic_common = {
  338. .name = "apic",
  339. .version_id = 3,
  340. .minimum_version_id = 3,
  341. .minimum_version_id_old = 1,
  342. .load_state_old = apic_load_old,
  343. .pre_load = apic_pre_load,
  344. .pre_save = apic_dispatch_pre_save,
  345. .post_load = apic_dispatch_post_load,
  346. .fields = (VMStateField[]) {
  347. VMSTATE_UINT32(apicbase, APICCommonState),
  348. VMSTATE_UINT8(id, APICCommonState),
  349. VMSTATE_UINT8(arb_id, APICCommonState),
  350. VMSTATE_UINT8(tpr, APICCommonState),
  351. VMSTATE_UINT32(spurious_vec, APICCommonState),
  352. VMSTATE_UINT8(log_dest, APICCommonState),
  353. VMSTATE_UINT8(dest_mode, APICCommonState),
  354. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  355. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  356. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  357. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  358. VMSTATE_UINT32(esr, APICCommonState),
  359. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  360. VMSTATE_UINT32(divide_conf, APICCommonState),
  361. VMSTATE_INT32(count_shift, APICCommonState),
  362. VMSTATE_UINT32(initial_count, APICCommonState),
  363. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  364. VMSTATE_INT64(next_time, APICCommonState),
  365. VMSTATE_INT64(timer_expiry,
  366. APICCommonState), /* open-coded timer state */
  367. VMSTATE_END_OF_LIST()
  368. },
  369. .subsections = (const VMStateDescription*[]) {
  370. &vmstate_apic_common_sipi,
  371. NULL
  372. }
  373. };
  374. static Property apic_properties_common[] = {
  375. DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
  376. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  377. true),
  378. DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
  379. false),
  380. DEFINE_PROP_END_OF_LIST(),
  381. };
  382. static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
  383. void *opaque, Error **errp)
  384. {
  385. APICCommonState *s = APIC_COMMON(obj);
  386. int64_t value;
  387. value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
  388. visit_type_int(v, name, &value, errp);
  389. }
  390. static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
  391. void *opaque, Error **errp)
  392. {
  393. APICCommonState *s = APIC_COMMON(obj);
  394. DeviceState *dev = DEVICE(obj);
  395. Error *local_err = NULL;
  396. int64_t value;
  397. if (dev->realized) {
  398. qdev_prop_set_after_realize(dev, name, errp);
  399. return;
  400. }
  401. visit_type_int(v, name, &value, &local_err);
  402. if (local_err) {
  403. error_propagate(errp, local_err);
  404. return;
  405. }
  406. s->initial_apic_id = value;
  407. s->id = (uint8_t)value;
  408. }
  409. static void apic_common_initfn(Object *obj)
  410. {
  411. APICCommonState *s = APIC_COMMON(obj);
  412. s->id = s->initial_apic_id = -1;
  413. object_property_add(obj, "id", "int",
  414. apic_common_get_id,
  415. apic_common_set_id, NULL, NULL, NULL);
  416. }
  417. static void apic_common_class_init(ObjectClass *klass, void *data)
  418. {
  419. DeviceClass *dc = DEVICE_CLASS(klass);
  420. dc->reset = apic_reset_common;
  421. dc->props = apic_properties_common;
  422. dc->realize = apic_common_realize;
  423. dc->unrealize = apic_common_unrealize;
  424. /*
  425. * Reason: APIC and CPU need to be wired up by
  426. * x86_cpu_apic_create()
  427. */
  428. dc->cannot_instantiate_with_device_add_yet = true;
  429. }
  430. static const TypeInfo apic_common_type = {
  431. .name = TYPE_APIC_COMMON,
  432. .parent = TYPE_DEVICE,
  433. .instance_size = sizeof(APICCommonState),
  434. .instance_init = apic_common_initfn,
  435. .class_size = sizeof(APICCommonClass),
  436. .class_init = apic_common_class_init,
  437. .abstract = true,
  438. };
  439. static void apic_common_register_types(void)
  440. {
  441. type_register_static(&apic_common_type);
  442. }
  443. type_init(apic_common_register_types)