2
0

apic.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * APIC support
  3. *
  4. * Copyright (c) 2004-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/thread.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/i386/apic_internal.h"
  23. #include "hw/i386/apic.h"
  24. #include "hw/intc/ioapic.h"
  25. #include "hw/intc/i8259.h"
  26. #include "hw/intc/kvm_irqcount.h"
  27. #include "hw/pci/msi.h"
  28. #include "qemu/host-utils.h"
  29. #include "system/kvm.h"
  30. #include "trace.h"
  31. #include "hw/i386/apic-msidef.h"
  32. #include "qapi/error.h"
  33. #include "qom/object.h"
  34. #define SYNC_FROM_VAPIC 0x1
  35. #define SYNC_TO_VAPIC 0x2
  36. #define SYNC_ISR_IRR_TO_VAPIC 0x4
  37. static APICCommonState **local_apics;
  38. static uint32_t max_apics;
  39. static uint32_t max_apic_words;
  40. #define TYPE_APIC "apic"
  41. /*This is reusing the APICCommonState typedef from APIC_COMMON */
  42. DECLARE_INSTANCE_CHECKER(APICCommonState, APIC,
  43. TYPE_APIC)
  44. static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
  45. static void apic_update_irq(APICCommonState *s);
  46. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  47. uint32_t dest, uint8_t dest_mode);
  48. void apic_set_max_apic_id(uint32_t max_apic_id)
  49. {
  50. int word_size = 32;
  51. /* round up the max apic id to next multiple of words */
  52. max_apics = (max_apic_id + word_size - 1) & ~(word_size - 1);
  53. local_apics = g_malloc0(sizeof(*local_apics) * max_apics);
  54. max_apic_words = max_apics >> 5;
  55. }
  56. /* Find first bit starting from msb */
  57. static int apic_fls_bit(uint32_t value)
  58. {
  59. return 31 - clz32(value);
  60. }
  61. /* Find first bit starting from lsb */
  62. static int apic_ffs_bit(uint32_t value)
  63. {
  64. return ctz32(value);
  65. }
  66. static inline void apic_reset_bit(uint32_t *tab, int index)
  67. {
  68. int i, mask;
  69. i = index >> 5;
  70. mask = 1 << (index & 0x1f);
  71. tab[i] &= ~mask;
  72. }
  73. /* return -1 if no bit is set */
  74. static int get_highest_priority_int(uint32_t *tab)
  75. {
  76. int i;
  77. for (i = 7; i >= 0; i--) {
  78. if (tab[i] != 0) {
  79. return i * 32 + apic_fls_bit(tab[i]);
  80. }
  81. }
  82. return -1;
  83. }
  84. static void apic_sync_vapic(APICCommonState *s, int sync_type)
  85. {
  86. VAPICState vapic_state;
  87. size_t length;
  88. off_t start;
  89. int vector;
  90. if (!s->vapic_paddr) {
  91. return;
  92. }
  93. if (sync_type & SYNC_FROM_VAPIC) {
  94. cpu_physical_memory_read(s->vapic_paddr, &vapic_state,
  95. sizeof(vapic_state));
  96. s->tpr = vapic_state.tpr;
  97. }
  98. if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
  99. start = offsetof(VAPICState, isr);
  100. length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
  101. if (sync_type & SYNC_TO_VAPIC) {
  102. assert(qemu_cpu_is_self(CPU(s->cpu)));
  103. vapic_state.tpr = s->tpr;
  104. vapic_state.enabled = 1;
  105. start = 0;
  106. length = sizeof(VAPICState);
  107. }
  108. vector = get_highest_priority_int(s->isr);
  109. if (vector < 0) {
  110. vector = 0;
  111. }
  112. vapic_state.isr = vector & 0xf0;
  113. vapic_state.zero = 0;
  114. vector = get_highest_priority_int(s->irr);
  115. if (vector < 0) {
  116. vector = 0;
  117. }
  118. vapic_state.irr = vector & 0xff;
  119. address_space_write_rom(&address_space_memory,
  120. s->vapic_paddr + start,
  121. MEMTXATTRS_UNSPECIFIED,
  122. ((void *)&vapic_state) + start, length);
  123. }
  124. }
  125. static void apic_vapic_base_update(APICCommonState *s)
  126. {
  127. apic_sync_vapic(s, SYNC_TO_VAPIC);
  128. }
  129. static void apic_local_deliver(APICCommonState *s, int vector)
  130. {
  131. uint32_t lvt = s->lvt[vector];
  132. int trigger_mode;
  133. trace_apic_local_deliver(vector, (lvt >> 8) & 7);
  134. if (lvt & APIC_LVT_MASKED)
  135. return;
  136. switch ((lvt >> 8) & 7) {
  137. case APIC_DM_SMI:
  138. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SMI);
  139. break;
  140. case APIC_DM_NMI:
  141. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_NMI);
  142. break;
  143. case APIC_DM_EXTINT:
  144. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HARD);
  145. break;
  146. case APIC_DM_FIXED:
  147. trigger_mode = APIC_TRIGGER_EDGE;
  148. if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
  149. (lvt & APIC_LVT_LEVEL_TRIGGER))
  150. trigger_mode = APIC_TRIGGER_LEVEL;
  151. apic_set_irq(s, lvt & 0xff, trigger_mode);
  152. }
  153. }
  154. void apic_deliver_pic_intr(DeviceState *dev, int level)
  155. {
  156. APICCommonState *s = APIC(dev);
  157. if (level) {
  158. apic_local_deliver(s, APIC_LVT_LINT0);
  159. } else {
  160. uint32_t lvt = s->lvt[APIC_LVT_LINT0];
  161. switch ((lvt >> 8) & 7) {
  162. case APIC_DM_FIXED:
  163. if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
  164. break;
  165. apic_reset_bit(s->irr, lvt & 0xff);
  166. /* fall through */
  167. case APIC_DM_EXTINT:
  168. apic_update_irq(s);
  169. break;
  170. }
  171. }
  172. }
  173. static void apic_external_nmi(APICCommonState *s)
  174. {
  175. apic_local_deliver(s, APIC_LVT_LINT1);
  176. }
  177. #define foreach_apic(apic, deliver_bitmask, code) \
  178. {\
  179. int __i, __j;\
  180. for (__i = 0; __i < max_apic_words; __i++) {\
  181. uint32_t __mask = deliver_bitmask[__i];\
  182. if (__mask) {\
  183. for (__j = 0; __j < 32; __j++) {\
  184. if (__mask & (1U << __j)) {\
  185. apic = local_apics[__i * 32 + __j];\
  186. if (apic) {\
  187. code;\
  188. }\
  189. }\
  190. }\
  191. }\
  192. }\
  193. }
  194. static void apic_bus_deliver(const uint32_t *deliver_bitmask,
  195. uint8_t delivery_mode, uint8_t vector_num,
  196. uint8_t trigger_mode)
  197. {
  198. APICCommonState *apic_iter;
  199. switch (delivery_mode) {
  200. case APIC_DM_LOWPRI:
  201. /* XXX: search for focus processor, arbitration */
  202. {
  203. int i, d;
  204. d = -1;
  205. for (i = 0; i < max_apic_words; i++) {
  206. if (deliver_bitmask[i]) {
  207. d = i * 32 + apic_ffs_bit(deliver_bitmask[i]);
  208. break;
  209. }
  210. }
  211. if (d >= 0) {
  212. apic_iter = local_apics[d];
  213. if (apic_iter) {
  214. apic_set_irq(apic_iter, vector_num, trigger_mode);
  215. }
  216. }
  217. }
  218. return;
  219. case APIC_DM_FIXED:
  220. break;
  221. case APIC_DM_SMI:
  222. foreach_apic(apic_iter, deliver_bitmask,
  223. cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_SMI)
  224. );
  225. return;
  226. case APIC_DM_NMI:
  227. foreach_apic(apic_iter, deliver_bitmask,
  228. cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_NMI)
  229. );
  230. return;
  231. case APIC_DM_INIT:
  232. /* normal INIT IPI sent to processors */
  233. foreach_apic(apic_iter, deliver_bitmask,
  234. cpu_interrupt(CPU(apic_iter->cpu),
  235. CPU_INTERRUPT_INIT)
  236. );
  237. return;
  238. case APIC_DM_EXTINT:
  239. /* handled in I/O APIC code */
  240. break;
  241. default:
  242. return;
  243. }
  244. foreach_apic(apic_iter, deliver_bitmask,
  245. apic_set_irq(apic_iter, vector_num, trigger_mode) );
  246. }
  247. static void apic_deliver_irq(uint32_t dest, uint8_t dest_mode,
  248. uint8_t delivery_mode, uint8_t vector_num,
  249. uint8_t trigger_mode)
  250. {
  251. g_autofree uint32_t *deliver_bitmask = g_new(uint32_t, max_apic_words);
  252. trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
  253. trigger_mode);
  254. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  255. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
  256. }
  257. bool is_x2apic_mode(DeviceState *dev)
  258. {
  259. APICCommonState *s = APIC(dev);
  260. return s->apicbase & MSR_IA32_APICBASE_EXTD;
  261. }
  262. static int apic_set_base_check(APICCommonState *s, uint64_t val)
  263. {
  264. /* Enable x2apic when x2apic is not supported by CPU */
  265. if (!cpu_has_x2apic_feature(&s->cpu->env) &&
  266. val & MSR_IA32_APICBASE_EXTD) {
  267. return -1;
  268. }
  269. /*
  270. * Transition into invalid state
  271. * (s->apicbase & MSR_IA32_APICBASE_ENABLE == 0) &&
  272. * (s->apicbase & MSR_IA32_APICBASE_EXTD) == 1
  273. */
  274. if (!(val & MSR_IA32_APICBASE_ENABLE) &&
  275. (val & MSR_IA32_APICBASE_EXTD)) {
  276. return -1;
  277. }
  278. /* Invalid transition from disabled mode to x2APIC */
  279. if (!(s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
  280. !(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  281. (val & MSR_IA32_APICBASE_ENABLE) &&
  282. (val & MSR_IA32_APICBASE_EXTD)) {
  283. return -1;
  284. }
  285. /* Invalid transition from x2APIC to xAPIC */
  286. if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
  287. (s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  288. (val & MSR_IA32_APICBASE_ENABLE) &&
  289. !(val & MSR_IA32_APICBASE_EXTD)) {
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. static int apic_set_base(APICCommonState *s, uint64_t val)
  295. {
  296. if (apic_set_base_check(s, val) < 0) {
  297. return -1;
  298. }
  299. s->apicbase = (val & MSR_IA32_APICBASE_BASE) |
  300. (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
  301. if (!(val & MSR_IA32_APICBASE_ENABLE)) {
  302. s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
  303. cpu_clear_apic_feature(&s->cpu->env);
  304. s->spurious_vec &= ~APIC_SV_ENABLE;
  305. }
  306. /* Transition from disabled mode to xAPIC */
  307. if (!(s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
  308. (val & MSR_IA32_APICBASE_ENABLE)) {
  309. s->apicbase |= MSR_IA32_APICBASE_ENABLE;
  310. cpu_set_apic_feature(&s->cpu->env);
  311. }
  312. /* Transition from xAPIC to x2APIC */
  313. if (cpu_has_x2apic_feature(&s->cpu->env) &&
  314. !(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  315. (val & MSR_IA32_APICBASE_EXTD)) {
  316. s->apicbase |= MSR_IA32_APICBASE_EXTD;
  317. s->log_dest = ((s->initial_apic_id & 0xffff0) << 16) |
  318. (1 << (s->initial_apic_id & 0xf));
  319. }
  320. return 0;
  321. }
  322. static void apic_set_tpr(APICCommonState *s, uint8_t val)
  323. {
  324. /* Updates from cr8 are ignored while the VAPIC is active */
  325. if (!s->vapic_paddr) {
  326. s->tpr = val << 4;
  327. apic_update_irq(s);
  328. }
  329. }
  330. int apic_get_highest_priority_irr(DeviceState *dev)
  331. {
  332. APICCommonState *s;
  333. if (!dev) {
  334. /* no interrupts */
  335. return -1;
  336. }
  337. s = APIC_COMMON(dev);
  338. return get_highest_priority_int(s->irr);
  339. }
  340. static uint8_t apic_get_tpr(APICCommonState *s)
  341. {
  342. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  343. return s->tpr >> 4;
  344. }
  345. int apic_get_ppr(APICCommonState *s)
  346. {
  347. int tpr, isrv, ppr;
  348. tpr = (s->tpr >> 4);
  349. isrv = get_highest_priority_int(s->isr);
  350. if (isrv < 0)
  351. isrv = 0;
  352. isrv >>= 4;
  353. if (tpr >= isrv)
  354. ppr = s->tpr;
  355. else
  356. ppr = isrv << 4;
  357. return ppr;
  358. }
  359. static int apic_get_arb_pri(APICCommonState *s)
  360. {
  361. /* XXX: arbitration */
  362. return 0;
  363. }
  364. /*
  365. * <0 - low prio interrupt,
  366. * 0 - no interrupt,
  367. * >0 - interrupt number
  368. */
  369. static int apic_irq_pending(APICCommonState *s)
  370. {
  371. int irrv, ppr;
  372. if (!(s->spurious_vec & APIC_SV_ENABLE)) {
  373. return 0;
  374. }
  375. irrv = get_highest_priority_int(s->irr);
  376. if (irrv < 0) {
  377. return 0;
  378. }
  379. ppr = apic_get_ppr(s);
  380. if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
  381. return -1;
  382. }
  383. return irrv;
  384. }
  385. /* signal the CPU if an irq is pending */
  386. static void apic_update_irq(APICCommonState *s)
  387. {
  388. CPUState *cpu;
  389. DeviceState *dev = (DeviceState *)s;
  390. cpu = CPU(s->cpu);
  391. if (!qemu_cpu_is_self(cpu)) {
  392. cpu_interrupt(cpu, CPU_INTERRUPT_POLL);
  393. } else if (apic_irq_pending(s) > 0) {
  394. cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
  395. } else if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  396. cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
  397. }
  398. }
  399. void apic_poll_irq(DeviceState *dev)
  400. {
  401. APICCommonState *s = APIC(dev);
  402. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  403. apic_update_irq(s);
  404. }
  405. static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
  406. {
  407. kvm_report_irq_delivered(!apic_get_bit(s->irr, vector_num));
  408. apic_set_bit(s->irr, vector_num);
  409. if (trigger_mode)
  410. apic_set_bit(s->tmr, vector_num);
  411. else
  412. apic_reset_bit(s->tmr, vector_num);
  413. if (s->vapic_paddr) {
  414. apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
  415. /*
  416. * The vcpu thread needs to see the new IRR before we pull its current
  417. * TPR value. That way, if we miss a lowering of the TRP, the guest
  418. * has the chance to notice the new IRR and poll for IRQs on its own.
  419. */
  420. smp_wmb();
  421. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  422. }
  423. apic_update_irq(s);
  424. }
  425. static void apic_eoi(APICCommonState *s)
  426. {
  427. int isrv;
  428. isrv = get_highest_priority_int(s->isr);
  429. if (isrv < 0)
  430. return;
  431. apic_reset_bit(s->isr, isrv);
  432. if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && apic_get_bit(s->tmr, isrv)) {
  433. ioapic_eoi_broadcast(isrv);
  434. }
  435. apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
  436. apic_update_irq(s);
  437. }
  438. static bool apic_match_dest(APICCommonState *apic, uint32_t dest)
  439. {
  440. if (is_x2apic_mode(&apic->parent_obj)) {
  441. return apic->initial_apic_id == dest;
  442. } else {
  443. return apic->id == (uint8_t)dest;
  444. }
  445. }
  446. static void apic_find_dest(uint32_t *deliver_bitmask, uint32_t dest)
  447. {
  448. APICCommonState *apic = NULL;
  449. int i;
  450. for (i = 0; i < max_apics; i++) {
  451. apic = local_apics[i];
  452. if (apic && apic_match_dest(apic, dest)) {
  453. apic_set_bit(deliver_bitmask, i);
  454. }
  455. }
  456. }
  457. /*
  458. * Deliver interrupt to x2APIC CPUs if it is x2APIC broadcast.
  459. * Otherwise, deliver interrupt to xAPIC CPUs if it is xAPIC
  460. * broadcast.
  461. */
  462. static void apic_get_broadcast_bitmask(uint32_t *deliver_bitmask,
  463. bool is_x2apic_broadcast)
  464. {
  465. int i;
  466. APICCommonState *apic_iter;
  467. for (i = 0; i < max_apics; i++) {
  468. apic_iter = local_apics[i];
  469. if (apic_iter) {
  470. bool apic_in_x2apic = is_x2apic_mode(&apic_iter->parent_obj);
  471. if (is_x2apic_broadcast && apic_in_x2apic) {
  472. apic_set_bit(deliver_bitmask, i);
  473. } else if (!is_x2apic_broadcast && !apic_in_x2apic) {
  474. apic_set_bit(deliver_bitmask, i);
  475. }
  476. }
  477. }
  478. }
  479. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  480. uint32_t dest, uint8_t dest_mode)
  481. {
  482. APICCommonState *apic;
  483. int i;
  484. memset(deliver_bitmask, 0x00, max_apic_words * sizeof(uint32_t));
  485. /*
  486. * x2APIC broadcast is delivered to all x2APIC CPUs regardless of
  487. * destination mode. In case the destination mode is physical, it is
  488. * broadcasted to all xAPIC CPUs too. Otherwise, if the destination
  489. * mode is logical, we need to continue checking if xAPIC CPUs accepts
  490. * the interrupt.
  491. */
  492. if (dest == 0xffffffff) {
  493. if (dest_mode == APIC_DESTMODE_PHYSICAL) {
  494. memset(deliver_bitmask, 0xff, max_apic_words * sizeof(uint32_t));
  495. return;
  496. } else {
  497. apic_get_broadcast_bitmask(deliver_bitmask, true);
  498. }
  499. }
  500. if (dest_mode == APIC_DESTMODE_PHYSICAL) {
  501. apic_find_dest(deliver_bitmask, dest);
  502. /* Any APIC in xAPIC mode will interpret 0xFF as broadcast */
  503. if (dest == 0xff) {
  504. apic_get_broadcast_bitmask(deliver_bitmask, false);
  505. }
  506. } else {
  507. /* XXX: logical mode */
  508. for (i = 0; i < max_apics; i++) {
  509. apic = local_apics[i];
  510. if (apic) {
  511. /* x2APIC logical mode */
  512. if (apic->apicbase & MSR_IA32_APICBASE_EXTD) {
  513. if ((dest >> 16) == (apic->extended_log_dest >> 16) &&
  514. (dest & apic->extended_log_dest & 0xffff)) {
  515. apic_set_bit(deliver_bitmask, i);
  516. }
  517. continue;
  518. }
  519. /* xAPIC logical mode */
  520. dest = (uint8_t)dest;
  521. if (apic->dest_mode == APIC_DESTMODE_LOGICAL_FLAT) {
  522. if (dest & apic->log_dest) {
  523. apic_set_bit(deliver_bitmask, i);
  524. }
  525. } else if (apic->dest_mode == APIC_DESTMODE_LOGICAL_CLUSTER) {
  526. /*
  527. * In cluster model of xAPIC logical mode IPI, 4 higher
  528. * bits are used as cluster address, 4 lower bits are
  529. * the bitmask for local APICs in the cluster. The IPI
  530. * is delivered to an APIC if the cluster address
  531. * matches and the APIC's address bit in the cluster is
  532. * set in bitmask of destination ID in IPI.
  533. *
  534. * The cluster address ranges from 0 - 14, the cluster
  535. * address 15 (0xf) is the broadcast address to all
  536. * clusters.
  537. */
  538. if ((dest & 0xf0) == 0xf0 ||
  539. (dest & 0xf0) == (apic->log_dest & 0xf0)) {
  540. if (dest & apic->log_dest & 0x0f) {
  541. apic_set_bit(deliver_bitmask, i);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. }
  549. static void apic_startup(APICCommonState *s, int vector_num)
  550. {
  551. s->sipi_vector = vector_num;
  552. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  553. }
  554. void apic_sipi(DeviceState *dev)
  555. {
  556. APICCommonState *s = APIC(dev);
  557. cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  558. if (!s->wait_for_sipi)
  559. return;
  560. cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
  561. s->wait_for_sipi = 0;
  562. }
  563. static void apic_deliver(DeviceState *dev, uint32_t dest, uint8_t dest_mode,
  564. uint8_t delivery_mode, uint8_t vector_num,
  565. uint8_t trigger_mode, uint8_t dest_shorthand)
  566. {
  567. APICCommonState *s = APIC(dev);
  568. APICCommonState *apic_iter;
  569. uint32_t deliver_bitmask_size = max_apic_words * sizeof(uint32_t);
  570. g_autofree uint32_t *deliver_bitmask = g_new(uint32_t, max_apic_words);
  571. uint32_t current_apic_id;
  572. if (is_x2apic_mode(dev)) {
  573. current_apic_id = s->initial_apic_id;
  574. } else {
  575. current_apic_id = s->id;
  576. }
  577. switch (dest_shorthand) {
  578. case 0:
  579. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  580. break;
  581. case 1:
  582. memset(deliver_bitmask, 0x00, deliver_bitmask_size);
  583. apic_set_bit(deliver_bitmask, current_apic_id);
  584. break;
  585. case 2:
  586. memset(deliver_bitmask, 0xff, deliver_bitmask_size);
  587. break;
  588. case 3:
  589. memset(deliver_bitmask, 0xff, deliver_bitmask_size);
  590. apic_reset_bit(deliver_bitmask, current_apic_id);
  591. break;
  592. }
  593. switch (delivery_mode) {
  594. case APIC_DM_INIT:
  595. {
  596. int trig_mode = (s->icr[0] >> 15) & 1;
  597. int level = (s->icr[0] >> 14) & 1;
  598. if (level == 0 && trig_mode == 1) {
  599. foreach_apic(apic_iter, deliver_bitmask,
  600. apic_iter->arb_id = apic_iter->id );
  601. return;
  602. }
  603. }
  604. break;
  605. case APIC_DM_SIPI:
  606. foreach_apic(apic_iter, deliver_bitmask,
  607. apic_startup(apic_iter, vector_num) );
  608. return;
  609. }
  610. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
  611. }
  612. static bool apic_check_pic(APICCommonState *s)
  613. {
  614. DeviceState *dev = (DeviceState *)s;
  615. if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  616. return false;
  617. }
  618. apic_deliver_pic_intr(dev, 1);
  619. return true;
  620. }
  621. int apic_get_interrupt(DeviceState *dev)
  622. {
  623. APICCommonState *s = APIC(dev);
  624. int intno;
  625. /* if the APIC is installed or enabled, we let the 8259 handle the
  626. IRQs */
  627. if (!s)
  628. return -1;
  629. if (!(s->spurious_vec & APIC_SV_ENABLE))
  630. return -1;
  631. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  632. intno = apic_irq_pending(s);
  633. /* if there is an interrupt from the 8259, let the caller handle
  634. * that first since ExtINT interrupts ignore the priority.
  635. */
  636. if (intno == 0 || apic_check_pic(s)) {
  637. apic_sync_vapic(s, SYNC_TO_VAPIC);
  638. return -1;
  639. } else if (intno < 0) {
  640. apic_sync_vapic(s, SYNC_TO_VAPIC);
  641. return s->spurious_vec & 0xff;
  642. }
  643. apic_reset_bit(s->irr, intno);
  644. apic_set_bit(s->isr, intno);
  645. apic_sync_vapic(s, SYNC_TO_VAPIC);
  646. apic_update_irq(s);
  647. return intno;
  648. }
  649. int apic_accept_pic_intr(DeviceState *dev)
  650. {
  651. APICCommonState *s = APIC(dev);
  652. uint32_t lvt0;
  653. if (!s)
  654. return -1;
  655. lvt0 = s->lvt[APIC_LVT_LINT0];
  656. if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
  657. (lvt0 & APIC_LVT_MASKED) == 0)
  658. return isa_pic != NULL;
  659. return 0;
  660. }
  661. static void apic_timer_update(APICCommonState *s, int64_t current_time)
  662. {
  663. if (apic_next_timer(s, current_time)) {
  664. timer_mod(s->timer, s->next_time);
  665. } else {
  666. timer_del(s->timer);
  667. }
  668. }
  669. static void apic_timer(void *opaque)
  670. {
  671. APICCommonState *s = opaque;
  672. apic_local_deliver(s, APIC_LVT_TIMER);
  673. apic_timer_update(s, s->next_time);
  674. }
  675. static int apic_register_read(int index, uint64_t *value)
  676. {
  677. DeviceState *dev;
  678. APICCommonState *s;
  679. uint32_t val;
  680. int ret = 0;
  681. dev = cpu_get_current_apic();
  682. if (!dev) {
  683. return -1;
  684. }
  685. s = APIC(dev);
  686. switch(index) {
  687. case 0x02: /* id */
  688. if (is_x2apic_mode(dev)) {
  689. val = s->initial_apic_id;
  690. } else {
  691. val = s->id << 24;
  692. }
  693. break;
  694. case 0x03: /* version */
  695. val = s->version | ((APIC_LVT_NB - 1) << 16);
  696. break;
  697. case 0x08:
  698. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  699. if (apic_report_tpr_access) {
  700. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_READ);
  701. }
  702. val = s->tpr;
  703. break;
  704. case 0x09:
  705. val = apic_get_arb_pri(s);
  706. break;
  707. case 0x0a:
  708. /* ppr */
  709. val = apic_get_ppr(s);
  710. break;
  711. case 0x0b:
  712. val = 0;
  713. break;
  714. case 0x0d:
  715. if (is_x2apic_mode(dev)) {
  716. val = s->extended_log_dest;
  717. } else {
  718. val = s->log_dest << 24;
  719. }
  720. break;
  721. case 0x0e:
  722. if (is_x2apic_mode(dev)) {
  723. val = 0;
  724. ret = -1;
  725. } else {
  726. val = (s->dest_mode << 28) | 0xfffffff;
  727. }
  728. break;
  729. case 0x0f:
  730. val = s->spurious_vec;
  731. break;
  732. case 0x10 ... 0x17:
  733. val = s->isr[index & 7];
  734. break;
  735. case 0x18 ... 0x1f:
  736. val = s->tmr[index & 7];
  737. break;
  738. case 0x20 ... 0x27:
  739. val = s->irr[index & 7];
  740. break;
  741. case 0x28:
  742. val = s->esr;
  743. break;
  744. case 0x30:
  745. case 0x31:
  746. val = s->icr[index & 1];
  747. break;
  748. case 0x32 ... 0x37:
  749. val = s->lvt[index - 0x32];
  750. break;
  751. case 0x38:
  752. val = s->initial_count;
  753. break;
  754. case 0x39:
  755. val = apic_get_current_count(s);
  756. break;
  757. case 0x3e:
  758. val = s->divide_conf;
  759. break;
  760. default:
  761. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  762. val = 0;
  763. ret = -1;
  764. break;
  765. }
  766. trace_apic_register_read(index, val);
  767. *value = val;
  768. return ret;
  769. }
  770. static uint64_t apic_mem_read(void *opaque, hwaddr addr, unsigned size)
  771. {
  772. uint64_t val;
  773. int index;
  774. if (size < 4) {
  775. return 0;
  776. }
  777. index = (addr >> 4) & 0xff;
  778. apic_register_read(index, &val);
  779. return val;
  780. }
  781. int apic_msr_read(int index, uint64_t *val)
  782. {
  783. DeviceState *dev;
  784. dev = cpu_get_current_apic();
  785. if (!dev) {
  786. return -1;
  787. }
  788. if (!is_x2apic_mode(dev)) {
  789. return -1;
  790. }
  791. return apic_register_read(index, val);
  792. }
  793. static void apic_send_msi(MSIMessage *msi)
  794. {
  795. uint64_t addr = msi->address;
  796. uint32_t data = msi->data;
  797. uint32_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
  798. /*
  799. * The higher 3 bytes of destination id is stored in higher word of
  800. * msi address. See x86_iommu_irq_to_msi_message()
  801. */
  802. dest = dest | (addr >> 32);
  803. uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
  804. uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
  805. uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
  806. uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
  807. /* XXX: Ignore redirection hint. */
  808. apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
  809. }
  810. static int apic_register_write(int index, uint64_t val)
  811. {
  812. DeviceState *dev;
  813. APICCommonState *s;
  814. dev = cpu_get_current_apic();
  815. if (!dev) {
  816. return -1;
  817. }
  818. s = APIC(dev);
  819. trace_apic_register_write(index, val);
  820. switch(index) {
  821. case 0x02:
  822. if (is_x2apic_mode(dev)) {
  823. return -1;
  824. }
  825. s->id = (val >> 24);
  826. break;
  827. case 0x03:
  828. break;
  829. case 0x08:
  830. if (apic_report_tpr_access) {
  831. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_WRITE);
  832. }
  833. s->tpr = val;
  834. apic_sync_vapic(s, SYNC_TO_VAPIC);
  835. apic_update_irq(s);
  836. break;
  837. case 0x09:
  838. case 0x0a:
  839. break;
  840. case 0x0b: /* EOI */
  841. apic_eoi(s);
  842. break;
  843. case 0x0d:
  844. if (is_x2apic_mode(dev)) {
  845. return -1;
  846. }
  847. s->log_dest = val >> 24;
  848. break;
  849. case 0x0e:
  850. if (is_x2apic_mode(dev)) {
  851. return -1;
  852. }
  853. s->dest_mode = val >> 28;
  854. break;
  855. case 0x0f:
  856. s->spurious_vec = val & 0x1ff;
  857. apic_update_irq(s);
  858. break;
  859. case 0x10 ... 0x17:
  860. case 0x18 ... 0x1f:
  861. case 0x20 ... 0x27:
  862. case 0x28:
  863. break;
  864. case 0x30: {
  865. uint32_t dest;
  866. s->icr[0] = val;
  867. if (is_x2apic_mode(dev)) {
  868. s->icr[1] = val >> 32;
  869. dest = s->icr[1];
  870. } else {
  871. dest = (s->icr[1] >> 24) & 0xff;
  872. }
  873. apic_deliver(dev, dest, (s->icr[0] >> 11) & 1,
  874. (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
  875. (s->icr[0] >> 15) & 1, (s->icr[0] >> 18) & 3);
  876. break;
  877. }
  878. case 0x31:
  879. if (is_x2apic_mode(dev)) {
  880. return -1;
  881. }
  882. s->icr[1] = val;
  883. break;
  884. case 0x32 ... 0x37:
  885. {
  886. int n = index - 0x32;
  887. s->lvt[n] = val;
  888. if (n == APIC_LVT_TIMER) {
  889. apic_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  890. } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
  891. apic_update_irq(s);
  892. }
  893. }
  894. break;
  895. case 0x38:
  896. s->initial_count = val;
  897. s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  898. apic_timer_update(s, s->initial_count_load_time);
  899. break;
  900. case 0x39:
  901. break;
  902. case 0x3e:
  903. {
  904. int v;
  905. s->divide_conf = val & 0xb;
  906. v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
  907. s->count_shift = (v + 1) & 7;
  908. }
  909. break;
  910. case 0x3f: {
  911. int vector = val & 0xff;
  912. if (!is_x2apic_mode(dev)) {
  913. return -1;
  914. }
  915. /*
  916. * Self IPI is identical to IPI with
  917. * - Destination shorthand: 1 (Self)
  918. * - Trigger mode: 0 (Edge)
  919. * - Delivery mode: 0 (Fixed)
  920. */
  921. apic_deliver(dev, 0, 0, APIC_DM_FIXED, vector, 0, 1);
  922. break;
  923. }
  924. default:
  925. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  926. return -1;
  927. }
  928. return 0;
  929. }
  930. static void apic_mem_write(void *opaque, hwaddr addr, uint64_t val,
  931. unsigned size)
  932. {
  933. int index = (addr >> 4) & 0xff;
  934. if (size < 4) {
  935. return;
  936. }
  937. if (addr > 0xfff || !index) {
  938. /*
  939. * MSI and MMIO APIC are at the same memory location,
  940. * but actually not on the global bus: MSI is on PCI bus
  941. * APIC is connected directly to the CPU.
  942. * Mapping them on the global bus happens to work because
  943. * MSI registers are reserved in APIC MMIO and vice versa.
  944. */
  945. MSIMessage msi = { .address = addr, .data = val };
  946. apic_send_msi(&msi);
  947. return;
  948. }
  949. apic_register_write(index, val);
  950. }
  951. int apic_msr_write(int index, uint64_t val)
  952. {
  953. DeviceState *dev;
  954. dev = cpu_get_current_apic();
  955. if (!dev) {
  956. return -1;
  957. }
  958. if (!is_x2apic_mode(dev)) {
  959. return -1;
  960. }
  961. return apic_register_write(index, val);
  962. }
  963. static void apic_pre_save(APICCommonState *s)
  964. {
  965. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  966. }
  967. static void apic_post_load(APICCommonState *s)
  968. {
  969. if (s->timer_expiry != -1) {
  970. timer_mod(s->timer, s->timer_expiry);
  971. } else {
  972. timer_del(s->timer);
  973. }
  974. }
  975. static const MemoryRegionOps apic_io_ops = {
  976. .read = apic_mem_read,
  977. .write = apic_mem_write,
  978. .impl.min_access_size = 1,
  979. .impl.max_access_size = 4,
  980. .valid.min_access_size = 1,
  981. .valid.max_access_size = 4,
  982. .endianness = DEVICE_NATIVE_ENDIAN,
  983. };
  984. static void apic_realize(DeviceState *dev, Error **errp)
  985. {
  986. APICCommonState *s = APIC(dev);
  987. if (kvm_enabled()) {
  988. warn_report("Userspace local APIC is deprecated for KVM.");
  989. warn_report("Do not use kernel-irqchip except for the -M isapc machine type.");
  990. }
  991. memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
  992. APIC_SPACE_SIZE);
  993. /*
  994. * apic-msi's apic_mem_write can call into ioapic_eoi_broadcast, which can
  995. * write back to apic-msi. As such mark the apic-msi region re-entrancy
  996. * safe.
  997. */
  998. s->io_memory.disable_reentrancy_guard = true;
  999. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
  1000. /*
  1001. * The --machine none does not call apic_set_max_apic_id before creating
  1002. * apic, so we need to call it here and set it to 1 which is the max cpus
  1003. * in machine none.
  1004. */
  1005. if (!local_apics) {
  1006. apic_set_max_apic_id(1);
  1007. }
  1008. local_apics[s->initial_apic_id] = s;
  1009. msi_nonbroken = true;
  1010. }
  1011. static void apic_unrealize(DeviceState *dev)
  1012. {
  1013. APICCommonState *s = APIC(dev);
  1014. timer_free(s->timer);
  1015. local_apics[s->initial_apic_id] = NULL;
  1016. }
  1017. static void apic_class_init(ObjectClass *klass, void *data)
  1018. {
  1019. APICCommonClass *k = APIC_COMMON_CLASS(klass);
  1020. k->realize = apic_realize;
  1021. k->unrealize = apic_unrealize;
  1022. k->set_base = apic_set_base;
  1023. k->set_tpr = apic_set_tpr;
  1024. k->get_tpr = apic_get_tpr;
  1025. k->vapic_base_update = apic_vapic_base_update;
  1026. k->external_nmi = apic_external_nmi;
  1027. k->pre_save = apic_pre_save;
  1028. k->post_load = apic_post_load;
  1029. k->send_msi = apic_send_msi;
  1030. }
  1031. static const TypeInfo apic_info = {
  1032. .name = TYPE_APIC,
  1033. .instance_size = sizeof(APICCommonState),
  1034. .parent = TYPE_APIC_COMMON,
  1035. .class_init = apic_class_init,
  1036. };
  1037. static void apic_register_types(void)
  1038. {
  1039. type_register_static(&apic_info);
  1040. }
  1041. type_init(apic_register_types)