2
0

apic_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. uint32_t apic_get_current_count(APICCommonState *s)
  160. {
  161. int64_t d;
  162. uint32_t val;
  163. d = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->initial_count_load_time) >>
  164. s->count_shift;
  165. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  166. /* periodic */
  167. val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
  168. } else {
  169. if (d >= s->initial_count) {
  170. val = 0;
  171. } else {
  172. val = s->initial_count - d;
  173. }
  174. }
  175. return val;
  176. }
  177. void apic_init_reset(DeviceState *dev)
  178. {
  179. APICCommonState *s;
  180. APICCommonClass *info;
  181. int i;
  182. if (!dev) {
  183. return;
  184. }
  185. s = APIC_COMMON(dev);
  186. s->tpr = 0;
  187. s->spurious_vec = 0xff;
  188. s->log_dest = 0;
  189. s->dest_mode = 0xf;
  190. memset(s->isr, 0, sizeof(s->isr));
  191. memset(s->tmr, 0, sizeof(s->tmr));
  192. memset(s->irr, 0, sizeof(s->irr));
  193. for (i = 0; i < APIC_LVT_NB; i++) {
  194. s->lvt[i] = APIC_LVT_MASKED;
  195. }
  196. s->esr = 0;
  197. memset(s->icr, 0, sizeof(s->icr));
  198. s->divide_conf = 0;
  199. s->count_shift = 0;
  200. s->initial_count = 0;
  201. s->initial_count_load_time = 0;
  202. s->next_time = 0;
  203. s->wait_for_sipi = !cpu_is_bsp(s->cpu);
  204. if (s->timer) {
  205. timer_del(s->timer);
  206. }
  207. s->timer_expiry = -1;
  208. info = APIC_COMMON_GET_CLASS(s);
  209. if (info->reset) {
  210. info->reset(s);
  211. }
  212. }
  213. void apic_designate_bsp(DeviceState *dev, bool bsp)
  214. {
  215. if (dev == NULL) {
  216. return;
  217. }
  218. APICCommonState *s = APIC_COMMON(dev);
  219. if (bsp) {
  220. s->apicbase |= MSR_IA32_APICBASE_BSP;
  221. } else {
  222. s->apicbase &= ~MSR_IA32_APICBASE_BSP;
  223. }
  224. }
  225. static void apic_reset_common(DeviceState *dev)
  226. {
  227. APICCommonState *s = APIC_COMMON(dev);
  228. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  229. uint32_t bsp;
  230. bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
  231. s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
  232. s->id = s->initial_apic_id;
  233. apic_reset_irq_delivered();
  234. s->vapic_paddr = 0;
  235. info->vapic_base_update(s);
  236. apic_init_reset(dev);
  237. }
  238. static const VMStateDescription vmstate_apic_common;
  239. static void apic_common_realize(DeviceState *dev, Error **errp)
  240. {
  241. APICCommonState *s = APIC_COMMON(dev);
  242. APICCommonClass *info;
  243. static DeviceState *vapic;
  244. uint32_t instance_id = s->initial_apic_id;
  245. /* Normally initial APIC ID should be no more than hundreds */
  246. assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
  247. info = APIC_COMMON_GET_CLASS(s);
  248. info->realize(dev, errp);
  249. /* Note: We need at least 1M to map the VAPIC option ROM */
  250. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  251. !hax_enabled() && ram_size >= 1024 * 1024) {
  252. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  253. }
  254. s->vapic = vapic;
  255. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  256. info->enable_tpr_reporting(s, true);
  257. }
  258. if (s->legacy_instance_id) {
  259. instance_id = VMSTATE_INSTANCE_ID_ANY;
  260. }
  261. vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
  262. s, -1, 0, NULL);
  263. }
  264. static void apic_common_unrealize(DeviceState *dev)
  265. {
  266. APICCommonState *s = APIC_COMMON(dev);
  267. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  268. vmstate_unregister(NULL, &vmstate_apic_common, s);
  269. info->unrealize(dev);
  270. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  271. info->enable_tpr_reporting(s, false);
  272. }
  273. }
  274. static int apic_pre_load(void *opaque)
  275. {
  276. APICCommonState *s = APIC_COMMON(opaque);
  277. /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
  278. * so that's what apic_common_sipi_needed checks for. Reset to
  279. * the value that is assumed when the apic_sipi subsection is
  280. * absent.
  281. */
  282. s->wait_for_sipi = 0;
  283. return 0;
  284. }
  285. static int apic_dispatch_pre_save(void *opaque)
  286. {
  287. APICCommonState *s = APIC_COMMON(opaque);
  288. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  289. if (info->pre_save) {
  290. info->pre_save(s);
  291. }
  292. return 0;
  293. }
  294. static int apic_dispatch_post_load(void *opaque, int version_id)
  295. {
  296. APICCommonState *s = APIC_COMMON(opaque);
  297. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  298. if (info->post_load) {
  299. info->post_load(s);
  300. }
  301. return 0;
  302. }
  303. static bool apic_common_sipi_needed(void *opaque)
  304. {
  305. APICCommonState *s = APIC_COMMON(opaque);
  306. return s->wait_for_sipi != 0;
  307. }
  308. static const VMStateDescription vmstate_apic_common_sipi = {
  309. .name = "apic_sipi",
  310. .version_id = 1,
  311. .minimum_version_id = 1,
  312. .needed = apic_common_sipi_needed,
  313. .fields = (VMStateField[]) {
  314. VMSTATE_INT32(sipi_vector, APICCommonState),
  315. VMSTATE_INT32(wait_for_sipi, APICCommonState),
  316. VMSTATE_END_OF_LIST()
  317. }
  318. };
  319. static const VMStateDescription vmstate_apic_common = {
  320. .name = "apic",
  321. .version_id = 3,
  322. .minimum_version_id = 3,
  323. .pre_load = apic_pre_load,
  324. .pre_save = apic_dispatch_pre_save,
  325. .post_load = apic_dispatch_post_load,
  326. .fields = (VMStateField[]) {
  327. VMSTATE_UINT32(apicbase, APICCommonState),
  328. VMSTATE_UINT8(id, APICCommonState),
  329. VMSTATE_UINT8(arb_id, APICCommonState),
  330. VMSTATE_UINT8(tpr, APICCommonState),
  331. VMSTATE_UINT32(spurious_vec, APICCommonState),
  332. VMSTATE_UINT8(log_dest, APICCommonState),
  333. VMSTATE_UINT8(dest_mode, APICCommonState),
  334. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  335. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  336. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  337. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  338. VMSTATE_UINT32(esr, APICCommonState),
  339. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  340. VMSTATE_UINT32(divide_conf, APICCommonState),
  341. VMSTATE_INT32(count_shift, APICCommonState),
  342. VMSTATE_UINT32(initial_count, APICCommonState),
  343. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  344. VMSTATE_INT64(next_time, APICCommonState),
  345. VMSTATE_INT64(timer_expiry,
  346. APICCommonState), /* open-coded timer state */
  347. VMSTATE_END_OF_LIST()
  348. },
  349. .subsections = (const VMStateDescription*[]) {
  350. &vmstate_apic_common_sipi,
  351. NULL
  352. }
  353. };
  354. static Property apic_properties_common[] = {
  355. DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
  356. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  357. true),
  358. DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
  359. false),
  360. DEFINE_PROP_END_OF_LIST(),
  361. };
  362. static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
  363. void *opaque, Error **errp)
  364. {
  365. APICCommonState *s = APIC_COMMON(obj);
  366. uint32_t value;
  367. value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
  368. visit_type_uint32(v, name, &value, errp);
  369. }
  370. static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
  371. void *opaque, Error **errp)
  372. {
  373. APICCommonState *s = APIC_COMMON(obj);
  374. DeviceState *dev = DEVICE(obj);
  375. uint32_t value;
  376. if (dev->realized) {
  377. qdev_prop_set_after_realize(dev, name, errp);
  378. return;
  379. }
  380. if (!visit_type_uint32(v, name, &value, errp)) {
  381. return;
  382. }
  383. s->initial_apic_id = value;
  384. s->id = (uint8_t)value;
  385. }
  386. static void apic_common_initfn(Object *obj)
  387. {
  388. APICCommonState *s = APIC_COMMON(obj);
  389. s->id = s->initial_apic_id = -1;
  390. object_property_add(obj, "id", "uint32",
  391. apic_common_get_id,
  392. apic_common_set_id, NULL, NULL);
  393. }
  394. static void apic_common_class_init(ObjectClass *klass, void *data)
  395. {
  396. DeviceClass *dc = DEVICE_CLASS(klass);
  397. dc->reset = apic_reset_common;
  398. device_class_set_props(dc, apic_properties_common);
  399. dc->realize = apic_common_realize;
  400. dc->unrealize = apic_common_unrealize;
  401. /*
  402. * Reason: APIC and CPU need to be wired up by
  403. * x86_cpu_apic_create()
  404. */
  405. dc->user_creatable = false;
  406. }
  407. static const TypeInfo apic_common_type = {
  408. .name = TYPE_APIC_COMMON,
  409. .parent = TYPE_DEVICE,
  410. .instance_size = sizeof(APICCommonState),
  411. .instance_init = apic_common_initfn,
  412. .class_size = sizeof(APICCommonClass),
  413. .class_init = apic_common_class_init,
  414. .abstract = true,
  415. };
  416. static void apic_common_register_types(void)
  417. {
  418. type_register_static(&apic_common_type);
  419. }
  420. type_init(apic_common_register_types)