2
0

apic_common.c 13 KB

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