2
0

apic_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "hw/i386/apic.h"
  21. #include "hw/i386/apic_internal.h"
  22. #include "trace.h"
  23. #include "sysemu/kvm.h"
  24. #include "hw/qdev.h"
  25. #include "hw/sysbus.h"
  26. static int apic_irq_delivered;
  27. bool apic_report_tpr_access;
  28. void cpu_set_apic_base(DeviceState *dev, uint64_t val)
  29. {
  30. trace_cpu_set_apic_base(val);
  31. if (dev) {
  32. APICCommonState *s = APIC_COMMON(dev);
  33. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  34. info->set_base(s, val);
  35. }
  36. }
  37. uint64_t cpu_get_apic_base(DeviceState *dev)
  38. {
  39. if (dev) {
  40. APICCommonState *s = APIC_COMMON(dev);
  41. trace_cpu_get_apic_base((uint64_t)s->apicbase);
  42. return s->apicbase;
  43. } else {
  44. trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
  45. return MSR_IA32_APICBASE_BSP;
  46. }
  47. }
  48. void cpu_set_apic_tpr(DeviceState *dev, uint8_t val)
  49. {
  50. APICCommonState *s;
  51. APICCommonClass *info;
  52. if (!dev) {
  53. return;
  54. }
  55. s = APIC_COMMON(dev);
  56. info = APIC_COMMON_GET_CLASS(s);
  57. info->set_tpr(s, val);
  58. }
  59. uint8_t cpu_get_apic_tpr(DeviceState *dev)
  60. {
  61. APICCommonState *s;
  62. APICCommonClass *info;
  63. if (!dev) {
  64. return 0;
  65. }
  66. s = APIC_COMMON(dev);
  67. info = APIC_COMMON_GET_CLASS(s);
  68. return info->get_tpr(s);
  69. }
  70. void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable)
  71. {
  72. APICCommonState *s = APIC_COMMON(dev);
  73. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  74. apic_report_tpr_access = enable;
  75. if (info->enable_tpr_reporting) {
  76. info->enable_tpr_reporting(s, enable);
  77. }
  78. }
  79. void apic_enable_vapic(DeviceState *dev, hwaddr paddr)
  80. {
  81. APICCommonState *s = APIC_COMMON(dev);
  82. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  83. s->vapic_paddr = paddr;
  84. info->vapic_base_update(s);
  85. }
  86. void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip,
  87. TPRAccess access)
  88. {
  89. APICCommonState *s = APIC_COMMON(dev);
  90. vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
  91. }
  92. void apic_report_irq_delivered(int delivered)
  93. {
  94. apic_irq_delivered += delivered;
  95. trace_apic_report_irq_delivered(apic_irq_delivered);
  96. }
  97. void apic_reset_irq_delivered(void)
  98. {
  99. /* Copy this into a local variable to encourage gcc to emit a plain
  100. * register for a sys/sdt.h marker. For details on this workaround, see:
  101. * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
  102. */
  103. volatile int a_i_d = apic_irq_delivered;
  104. trace_apic_reset_irq_delivered(a_i_d);
  105. apic_irq_delivered = 0;
  106. }
  107. int apic_get_irq_delivered(void)
  108. {
  109. trace_apic_get_irq_delivered(apic_irq_delivered);
  110. return apic_irq_delivered;
  111. }
  112. void apic_deliver_nmi(DeviceState *dev)
  113. {
  114. APICCommonState *s = APIC_COMMON(dev);
  115. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  116. info->external_nmi(s);
  117. }
  118. bool apic_next_timer(APICCommonState *s, int64_t current_time)
  119. {
  120. int64_t d;
  121. /* We need to store the timer state separately to support APIC
  122. * implementations that maintain a non-QEMU timer, e.g. inside the
  123. * host kernel. This open-coded state allows us to migrate between
  124. * both models. */
  125. s->timer_expiry = -1;
  126. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
  127. return false;
  128. }
  129. d = (current_time - s->initial_count_load_time) >> s->count_shift;
  130. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  131. if (!s->initial_count) {
  132. return false;
  133. }
  134. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
  135. ((uint64_t)s->initial_count + 1);
  136. } else {
  137. if (d >= s->initial_count) {
  138. return false;
  139. }
  140. d = (uint64_t)s->initial_count + 1;
  141. }
  142. s->next_time = s->initial_count_load_time + (d << s->count_shift);
  143. s->timer_expiry = s->next_time;
  144. return true;
  145. }
  146. void apic_init_reset(DeviceState *dev)
  147. {
  148. APICCommonState *s = APIC_COMMON(dev);
  149. int i;
  150. if (!s) {
  151. return;
  152. }
  153. s->tpr = 0;
  154. s->spurious_vec = 0xff;
  155. s->log_dest = 0;
  156. s->dest_mode = 0xf;
  157. memset(s->isr, 0, sizeof(s->isr));
  158. memset(s->tmr, 0, sizeof(s->tmr));
  159. memset(s->irr, 0, sizeof(s->irr));
  160. for (i = 0; i < APIC_LVT_NB; i++) {
  161. s->lvt[i] = APIC_LVT_MASKED;
  162. }
  163. s->esr = 0;
  164. memset(s->icr, 0, sizeof(s->icr));
  165. s->divide_conf = 0;
  166. s->count_shift = 0;
  167. s->initial_count = 0;
  168. s->initial_count_load_time = 0;
  169. s->next_time = 0;
  170. s->wait_for_sipi = !cpu_is_bsp(s->cpu);
  171. if (s->timer) {
  172. timer_del(s->timer);
  173. }
  174. s->timer_expiry = -1;
  175. }
  176. void apic_designate_bsp(DeviceState *dev)
  177. {
  178. if (dev == NULL) {
  179. return;
  180. }
  181. APICCommonState *s = APIC_COMMON(dev);
  182. s->apicbase |= MSR_IA32_APICBASE_BSP;
  183. }
  184. static void apic_reset_common(DeviceState *dev)
  185. {
  186. APICCommonState *s = APIC_COMMON(dev);
  187. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  188. bool bsp;
  189. bsp = cpu_is_bsp(s->cpu);
  190. s->apicbase = APIC_DEFAULT_ADDRESS |
  191. (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
  192. s->vapic_paddr = 0;
  193. info->vapic_base_update(s);
  194. apic_init_reset(dev);
  195. if (bsp) {
  196. /*
  197. * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
  198. * time typically by BIOS, so PIC interrupt can be delivered to the
  199. * processor when local APIC is enabled.
  200. */
  201. s->lvt[APIC_LVT_LINT0] = 0x700;
  202. }
  203. }
  204. /* This function is only used for old state version 1 and 2 */
  205. static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
  206. {
  207. APICCommonState *s = opaque;
  208. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  209. int i;
  210. if (version_id > 2) {
  211. return -EINVAL;
  212. }
  213. /* XXX: what if the base changes? (registered memory regions) */
  214. qemu_get_be32s(f, &s->apicbase);
  215. qemu_get_8s(f, &s->id);
  216. qemu_get_8s(f, &s->arb_id);
  217. qemu_get_8s(f, &s->tpr);
  218. qemu_get_be32s(f, &s->spurious_vec);
  219. qemu_get_8s(f, &s->log_dest);
  220. qemu_get_8s(f, &s->dest_mode);
  221. for (i = 0; i < 8; i++) {
  222. qemu_get_be32s(f, &s->isr[i]);
  223. qemu_get_be32s(f, &s->tmr[i]);
  224. qemu_get_be32s(f, &s->irr[i]);
  225. }
  226. for (i = 0; i < APIC_LVT_NB; i++) {
  227. qemu_get_be32s(f, &s->lvt[i]);
  228. }
  229. qemu_get_be32s(f, &s->esr);
  230. qemu_get_be32s(f, &s->icr[0]);
  231. qemu_get_be32s(f, &s->icr[1]);
  232. qemu_get_be32s(f, &s->divide_conf);
  233. s->count_shift = qemu_get_be32(f);
  234. qemu_get_be32s(f, &s->initial_count);
  235. s->initial_count_load_time = qemu_get_be64(f);
  236. s->next_time = qemu_get_be64(f);
  237. if (version_id >= 2) {
  238. s->timer_expiry = qemu_get_be64(f);
  239. }
  240. if (info->post_load) {
  241. info->post_load(s);
  242. }
  243. return 0;
  244. }
  245. static void apic_common_realize(DeviceState *dev, Error **errp)
  246. {
  247. APICCommonState *s = APIC_COMMON(dev);
  248. APICCommonClass *info;
  249. static DeviceState *vapic;
  250. static int apic_no;
  251. static bool mmio_registered;
  252. if (apic_no >= MAX_APICS) {
  253. error_setg(errp, "%s initialization failed.",
  254. object_get_typename(OBJECT(dev)));
  255. return;
  256. }
  257. s->idx = apic_no++;
  258. info = APIC_COMMON_GET_CLASS(s);
  259. info->realize(dev, errp);
  260. if (!mmio_registered) {
  261. ICCBus *b = ICC_BUS(qdev_get_parent_bus(dev));
  262. memory_region_add_subregion(b->apic_address_space, 0, &s->io_memory);
  263. mmio_registered = true;
  264. }
  265. /* Note: We need at least 1M to map the VAPIC option ROM */
  266. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  267. ram_size >= 1024 * 1024) {
  268. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  269. }
  270. s->vapic = vapic;
  271. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  272. info->enable_tpr_reporting(s, true);
  273. }
  274. }
  275. static void apic_dispatch_pre_save(void *opaque)
  276. {
  277. APICCommonState *s = APIC_COMMON(opaque);
  278. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  279. if (info->pre_save) {
  280. info->pre_save(s);
  281. }
  282. }
  283. static int apic_dispatch_post_load(void *opaque, int version_id)
  284. {
  285. APICCommonState *s = APIC_COMMON(opaque);
  286. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  287. if (info->post_load) {
  288. info->post_load(s);
  289. }
  290. return 0;
  291. }
  292. static const VMStateDescription vmstate_apic_common = {
  293. .name = "apic",
  294. .version_id = 3,
  295. .minimum_version_id = 3,
  296. .minimum_version_id_old = 1,
  297. .load_state_old = apic_load_old,
  298. .pre_save = apic_dispatch_pre_save,
  299. .post_load = apic_dispatch_post_load,
  300. .fields = (VMStateField[]) {
  301. VMSTATE_UINT32(apicbase, APICCommonState),
  302. VMSTATE_UINT8(id, APICCommonState),
  303. VMSTATE_UINT8(arb_id, APICCommonState),
  304. VMSTATE_UINT8(tpr, APICCommonState),
  305. VMSTATE_UINT32(spurious_vec, APICCommonState),
  306. VMSTATE_UINT8(log_dest, APICCommonState),
  307. VMSTATE_UINT8(dest_mode, APICCommonState),
  308. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  309. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  310. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  311. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  312. VMSTATE_UINT32(esr, APICCommonState),
  313. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  314. VMSTATE_UINT32(divide_conf, APICCommonState),
  315. VMSTATE_INT32(count_shift, APICCommonState),
  316. VMSTATE_UINT32(initial_count, APICCommonState),
  317. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  318. VMSTATE_INT64(next_time, APICCommonState),
  319. VMSTATE_INT64(timer_expiry,
  320. APICCommonState), /* open-coded timer state */
  321. VMSTATE_END_OF_LIST()
  322. }
  323. };
  324. static Property apic_properties_common[] = {
  325. DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
  326. DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
  327. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  328. true),
  329. DEFINE_PROP_END_OF_LIST(),
  330. };
  331. static void apic_common_class_init(ObjectClass *klass, void *data)
  332. {
  333. ICCDeviceClass *idc = ICC_DEVICE_CLASS(klass);
  334. DeviceClass *dc = DEVICE_CLASS(klass);
  335. dc->vmsd = &vmstate_apic_common;
  336. dc->reset = apic_reset_common;
  337. dc->props = apic_properties_common;
  338. idc->realize = apic_common_realize;
  339. /*
  340. * Reason: APIC and CPU need to be wired up by
  341. * x86_cpu_apic_create()
  342. */
  343. dc->cannot_instantiate_with_device_add_yet = true;
  344. }
  345. static const TypeInfo apic_common_type = {
  346. .name = TYPE_APIC_COMMON,
  347. .parent = TYPE_ICC_DEVICE,
  348. .instance_size = sizeof(APICCommonState),
  349. .class_size = sizeof(APICCommonClass),
  350. .class_init = apic_common_class_init,
  351. .abstract = true,
  352. };
  353. static void apic_common_register_types(void)
  354. {
  355. type_register_static(&apic_common_type);
  356. }
  357. type_init(apic_common_register_types)