2
0

apic_common.c 13 KB

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