apic_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.1 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 "qapi/visitor.h"
  25. #include "hw/i386/apic.h"
  26. #include "hw/i386/apic_internal.h"
  27. #include "hw/intc/kvm_irqcount.h"
  28. #include "trace.h"
  29. #include "hw/boards.h"
  30. #include "sysemu/kvm.h"
  31. #include "hw/qdev-properties.h"
  32. #include "hw/sysbus.h"
  33. #include "migration/vmstate.h"
  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_deliver_nmi(DeviceState *dev)
  105. {
  106. APICCommonState *s = APIC_COMMON(dev);
  107. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  108. info->external_nmi(s);
  109. }
  110. bool apic_next_timer(APICCommonState *s, int64_t current_time)
  111. {
  112. int64_t d;
  113. /* We need to store the timer state separately to support APIC
  114. * implementations that maintain a non-QEMU timer, e.g. inside the
  115. * host kernel. This open-coded state allows us to migrate between
  116. * both models. */
  117. s->timer_expiry = -1;
  118. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
  119. return false;
  120. }
  121. d = (current_time - s->initial_count_load_time) >> s->count_shift;
  122. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  123. if (!s->initial_count) {
  124. return false;
  125. }
  126. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
  127. ((uint64_t)s->initial_count + 1);
  128. } else {
  129. if (d >= s->initial_count) {
  130. return false;
  131. }
  132. d = (uint64_t)s->initial_count + 1;
  133. }
  134. s->next_time = s->initial_count_load_time + (d << s->count_shift);
  135. s->timer_expiry = s->next_time;
  136. return true;
  137. }
  138. uint32_t apic_get_current_count(APICCommonState *s)
  139. {
  140. int64_t d;
  141. uint32_t val;
  142. d = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->initial_count_load_time) >>
  143. s->count_shift;
  144. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  145. /* periodic */
  146. val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
  147. } else {
  148. if (d >= s->initial_count) {
  149. val = 0;
  150. } else {
  151. val = s->initial_count - d;
  152. }
  153. }
  154. return val;
  155. }
  156. void apic_init_reset(DeviceState *dev)
  157. {
  158. APICCommonState *s;
  159. APICCommonClass *info;
  160. int i;
  161. if (!dev) {
  162. return;
  163. }
  164. s = APIC_COMMON(dev);
  165. s->tpr = 0;
  166. s->spurious_vec = 0xff;
  167. s->log_dest = 0;
  168. s->dest_mode = 0xf;
  169. memset(s->isr, 0, sizeof(s->isr));
  170. memset(s->tmr, 0, sizeof(s->tmr));
  171. memset(s->irr, 0, sizeof(s->irr));
  172. for (i = 0; i < APIC_LVT_NB; i++) {
  173. s->lvt[i] = APIC_LVT_MASKED;
  174. }
  175. s->esr = 0;
  176. memset(s->icr, 0, sizeof(s->icr));
  177. s->divide_conf = 0;
  178. s->count_shift = 0;
  179. s->initial_count = 0;
  180. s->initial_count_load_time = 0;
  181. s->next_time = 0;
  182. s->wait_for_sipi = !cpu_is_bsp(s->cpu);
  183. if (s->timer) {
  184. timer_del(s->timer);
  185. }
  186. s->timer_expiry = -1;
  187. info = APIC_COMMON_GET_CLASS(s);
  188. if (info->reset) {
  189. info->reset(s);
  190. }
  191. }
  192. void apic_designate_bsp(DeviceState *dev, bool bsp)
  193. {
  194. if (dev == NULL) {
  195. return;
  196. }
  197. APICCommonState *s = APIC_COMMON(dev);
  198. if (bsp) {
  199. s->apicbase |= MSR_IA32_APICBASE_BSP;
  200. } else {
  201. s->apicbase &= ~MSR_IA32_APICBASE_BSP;
  202. }
  203. }
  204. static void apic_reset_common(DeviceState *dev)
  205. {
  206. APICCommonState *s = APIC_COMMON(dev);
  207. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  208. uint32_t bsp;
  209. bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
  210. s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
  211. s->id = s->initial_apic_id;
  212. kvm_reset_irq_delivered();
  213. s->vapic_paddr = 0;
  214. info->vapic_base_update(s);
  215. apic_init_reset(dev);
  216. }
  217. static const VMStateDescription vmstate_apic_common;
  218. static void apic_common_realize(DeviceState *dev, Error **errp)
  219. {
  220. ERRP_GUARD();
  221. APICCommonState *s = APIC_COMMON(dev);
  222. APICCommonClass *info;
  223. static DeviceState *vapic;
  224. uint32_t instance_id = s->initial_apic_id;
  225. /* Normally initial APIC ID should be no more than hundreds */
  226. assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
  227. info = APIC_COMMON_GET_CLASS(s);
  228. info->realize(dev, errp);
  229. if (*errp) {
  230. return;
  231. }
  232. /* Note: We need at least 1M to map the VAPIC option ROM */
  233. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  234. current_machine->ram_size >= 1024 * 1024) {
  235. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  236. }
  237. s->vapic = vapic;
  238. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  239. info->enable_tpr_reporting(s, true);
  240. }
  241. if (s->legacy_instance_id) {
  242. instance_id = VMSTATE_INSTANCE_ID_ANY;
  243. }
  244. vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
  245. s, -1, 0, NULL);
  246. }
  247. static void apic_common_unrealize(DeviceState *dev)
  248. {
  249. APICCommonState *s = APIC_COMMON(dev);
  250. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  251. vmstate_unregister(NULL, &vmstate_apic_common, s);
  252. info->unrealize(dev);
  253. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  254. info->enable_tpr_reporting(s, false);
  255. }
  256. }
  257. static int apic_pre_load(void *opaque)
  258. {
  259. APICCommonState *s = APIC_COMMON(opaque);
  260. /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
  261. * so that's what apic_common_sipi_needed checks for. Reset to
  262. * the value that is assumed when the apic_sipi subsection is
  263. * absent.
  264. */
  265. s->wait_for_sipi = 0;
  266. return 0;
  267. }
  268. static int apic_dispatch_pre_save(void *opaque)
  269. {
  270. APICCommonState *s = APIC_COMMON(opaque);
  271. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  272. if (info->pre_save) {
  273. info->pre_save(s);
  274. }
  275. return 0;
  276. }
  277. static int apic_dispatch_post_load(void *opaque, int version_id)
  278. {
  279. APICCommonState *s = APIC_COMMON(opaque);
  280. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  281. if (info->post_load) {
  282. info->post_load(s);
  283. }
  284. return 0;
  285. }
  286. static bool apic_common_sipi_needed(void *opaque)
  287. {
  288. APICCommonState *s = APIC_COMMON(opaque);
  289. return s->wait_for_sipi != 0;
  290. }
  291. static const VMStateDescription vmstate_apic_common_sipi = {
  292. .name = "apic_sipi",
  293. .version_id = 1,
  294. .minimum_version_id = 1,
  295. .needed = apic_common_sipi_needed,
  296. .fields = (const VMStateField[]) {
  297. VMSTATE_INT32(sipi_vector, APICCommonState),
  298. VMSTATE_INT32(wait_for_sipi, APICCommonState),
  299. VMSTATE_END_OF_LIST()
  300. }
  301. };
  302. static const VMStateDescription vmstate_apic_common = {
  303. .name = "apic",
  304. .version_id = 3,
  305. .minimum_version_id = 3,
  306. .pre_load = apic_pre_load,
  307. .pre_save = apic_dispatch_pre_save,
  308. .post_load = apic_dispatch_post_load,
  309. .fields = (const VMStateField[]) {
  310. VMSTATE_UINT32(apicbase, APICCommonState),
  311. VMSTATE_UINT8(id, APICCommonState),
  312. VMSTATE_UINT8(arb_id, APICCommonState),
  313. VMSTATE_UINT8(tpr, APICCommonState),
  314. VMSTATE_UINT32(spurious_vec, APICCommonState),
  315. VMSTATE_UINT8(log_dest, APICCommonState),
  316. VMSTATE_UINT8(dest_mode, APICCommonState),
  317. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  318. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  319. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  320. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  321. VMSTATE_UINT32(esr, APICCommonState),
  322. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  323. VMSTATE_UINT32(divide_conf, APICCommonState),
  324. VMSTATE_INT32(count_shift, APICCommonState),
  325. VMSTATE_UINT32(initial_count, APICCommonState),
  326. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  327. VMSTATE_INT64(next_time, APICCommonState),
  328. VMSTATE_INT64(timer_expiry,
  329. APICCommonState), /* open-coded timer state */
  330. VMSTATE_END_OF_LIST()
  331. },
  332. .subsections = (const VMStateDescription * const []) {
  333. &vmstate_apic_common_sipi,
  334. NULL
  335. }
  336. };
  337. static Property apic_properties_common[] = {
  338. DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
  339. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  340. true),
  341. DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
  342. false),
  343. DEFINE_PROP_END_OF_LIST(),
  344. };
  345. static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
  346. void *opaque, Error **errp)
  347. {
  348. APICCommonState *s = APIC_COMMON(obj);
  349. uint32_t value;
  350. value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
  351. visit_type_uint32(v, name, &value, errp);
  352. }
  353. static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
  354. void *opaque, Error **errp)
  355. {
  356. APICCommonState *s = APIC_COMMON(obj);
  357. DeviceState *dev = DEVICE(obj);
  358. uint32_t value;
  359. if (dev->realized) {
  360. qdev_prop_set_after_realize(dev, name, errp);
  361. return;
  362. }
  363. if (!visit_type_uint32(v, name, &value, errp)) {
  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);
  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. device_class_set_props(dc, 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)