apic.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 & 0xfffff000) |
  300. (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
  301. /* if disabled, cannot be enabled again */
  302. if (!(val & MSR_IA32_APICBASE_ENABLE)) {
  303. s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
  304. cpu_clear_apic_feature(&s->cpu->env);
  305. s->spurious_vec &= ~APIC_SV_ENABLE;
  306. }
  307. /* Transition from disabled mode to xAPIC */
  308. if (!(s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
  309. (val & MSR_IA32_APICBASE_ENABLE)) {
  310. s->apicbase |= MSR_IA32_APICBASE_ENABLE;
  311. cpu_set_apic_feature(&s->cpu->env);
  312. }
  313. /* Transition from xAPIC to x2APIC */
  314. if (cpu_has_x2apic_feature(&s->cpu->env) &&
  315. !(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
  316. (val & MSR_IA32_APICBASE_EXTD)) {
  317. s->apicbase |= MSR_IA32_APICBASE_EXTD;
  318. s->log_dest = ((s->initial_apic_id & 0xffff0) << 16) |
  319. (1 << (s->initial_apic_id & 0xf));
  320. }
  321. return 0;
  322. }
  323. static void apic_set_tpr(APICCommonState *s, uint8_t val)
  324. {
  325. /* Updates from cr8 are ignored while the VAPIC is active */
  326. if (!s->vapic_paddr) {
  327. s->tpr = val << 4;
  328. apic_update_irq(s);
  329. }
  330. }
  331. int apic_get_highest_priority_irr(DeviceState *dev)
  332. {
  333. APICCommonState *s;
  334. if (!dev) {
  335. /* no interrupts */
  336. return -1;
  337. }
  338. s = APIC_COMMON(dev);
  339. return get_highest_priority_int(s->irr);
  340. }
  341. static uint8_t apic_get_tpr(APICCommonState *s)
  342. {
  343. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  344. return s->tpr >> 4;
  345. }
  346. int apic_get_ppr(APICCommonState *s)
  347. {
  348. int tpr, isrv, ppr;
  349. tpr = (s->tpr >> 4);
  350. isrv = get_highest_priority_int(s->isr);
  351. if (isrv < 0)
  352. isrv = 0;
  353. isrv >>= 4;
  354. if (tpr >= isrv)
  355. ppr = s->tpr;
  356. else
  357. ppr = isrv << 4;
  358. return ppr;
  359. }
  360. static int apic_get_arb_pri(APICCommonState *s)
  361. {
  362. /* XXX: arbitration */
  363. return 0;
  364. }
  365. /*
  366. * <0 - low prio interrupt,
  367. * 0 - no interrupt,
  368. * >0 - interrupt number
  369. */
  370. static int apic_irq_pending(APICCommonState *s)
  371. {
  372. int irrv, ppr;
  373. if (!(s->spurious_vec & APIC_SV_ENABLE)) {
  374. return 0;
  375. }
  376. irrv = get_highest_priority_int(s->irr);
  377. if (irrv < 0) {
  378. return 0;
  379. }
  380. ppr = apic_get_ppr(s);
  381. if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
  382. return -1;
  383. }
  384. return irrv;
  385. }
  386. /* signal the CPU if an irq is pending */
  387. static void apic_update_irq(APICCommonState *s)
  388. {
  389. CPUState *cpu;
  390. DeviceState *dev = (DeviceState *)s;
  391. cpu = CPU(s->cpu);
  392. if (!qemu_cpu_is_self(cpu)) {
  393. cpu_interrupt(cpu, CPU_INTERRUPT_POLL);
  394. } else if (apic_irq_pending(s) > 0) {
  395. cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
  396. } else if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  397. cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
  398. }
  399. }
  400. void apic_poll_irq(DeviceState *dev)
  401. {
  402. APICCommonState *s = APIC(dev);
  403. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  404. apic_update_irq(s);
  405. }
  406. static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
  407. {
  408. kvm_report_irq_delivered(!apic_get_bit(s->irr, vector_num));
  409. apic_set_bit(s->irr, vector_num);
  410. if (trigger_mode)
  411. apic_set_bit(s->tmr, vector_num);
  412. else
  413. apic_reset_bit(s->tmr, vector_num);
  414. if (s->vapic_paddr) {
  415. apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
  416. /*
  417. * The vcpu thread needs to see the new IRR before we pull its current
  418. * TPR value. That way, if we miss a lowering of the TRP, the guest
  419. * has the chance to notice the new IRR and poll for IRQs on its own.
  420. */
  421. smp_wmb();
  422. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  423. }
  424. apic_update_irq(s);
  425. }
  426. static void apic_eoi(APICCommonState *s)
  427. {
  428. int isrv;
  429. isrv = get_highest_priority_int(s->isr);
  430. if (isrv < 0)
  431. return;
  432. apic_reset_bit(s->isr, isrv);
  433. if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && apic_get_bit(s->tmr, isrv)) {
  434. ioapic_eoi_broadcast(isrv);
  435. }
  436. apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
  437. apic_update_irq(s);
  438. }
  439. static bool apic_match_dest(APICCommonState *apic, uint32_t dest)
  440. {
  441. if (is_x2apic_mode(&apic->parent_obj)) {
  442. return apic->initial_apic_id == dest;
  443. } else {
  444. return apic->id == (uint8_t)dest;
  445. }
  446. }
  447. static void apic_find_dest(uint32_t *deliver_bitmask, uint32_t dest)
  448. {
  449. APICCommonState *apic = NULL;
  450. int i;
  451. for (i = 0; i < max_apics; i++) {
  452. apic = local_apics[i];
  453. if (apic && apic_match_dest(apic, dest)) {
  454. apic_set_bit(deliver_bitmask, i);
  455. }
  456. }
  457. }
  458. /*
  459. * Deliver interrupt to x2APIC CPUs if it is x2APIC broadcast.
  460. * Otherwise, deliver interrupt to xAPIC CPUs if it is xAPIC
  461. * broadcast.
  462. */
  463. static void apic_get_broadcast_bitmask(uint32_t *deliver_bitmask,
  464. bool is_x2apic_broadcast)
  465. {
  466. int i;
  467. APICCommonState *apic_iter;
  468. for (i = 0; i < max_apics; i++) {
  469. apic_iter = local_apics[i];
  470. if (apic_iter) {
  471. bool apic_in_x2apic = is_x2apic_mode(&apic_iter->parent_obj);
  472. if (is_x2apic_broadcast && apic_in_x2apic) {
  473. apic_set_bit(deliver_bitmask, i);
  474. } else if (!is_x2apic_broadcast && !apic_in_x2apic) {
  475. apic_set_bit(deliver_bitmask, i);
  476. }
  477. }
  478. }
  479. }
  480. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  481. uint32_t dest, uint8_t dest_mode)
  482. {
  483. APICCommonState *apic;
  484. int i;
  485. memset(deliver_bitmask, 0x00, max_apic_words * sizeof(uint32_t));
  486. /*
  487. * x2APIC broadcast is delivered to all x2APIC CPUs regardless of
  488. * destination mode. In case the destination mode is physical, it is
  489. * broadcasted to all xAPIC CPUs too. Otherwise, if the destination
  490. * mode is logical, we need to continue checking if xAPIC CPUs accepts
  491. * the interrupt.
  492. */
  493. if (dest == 0xffffffff) {
  494. if (dest_mode == APIC_DESTMODE_PHYSICAL) {
  495. memset(deliver_bitmask, 0xff, max_apic_words * sizeof(uint32_t));
  496. return;
  497. } else {
  498. apic_get_broadcast_bitmask(deliver_bitmask, true);
  499. }
  500. }
  501. if (dest_mode == APIC_DESTMODE_PHYSICAL) {
  502. apic_find_dest(deliver_bitmask, dest);
  503. /* Any APIC in xAPIC mode will interpret 0xFF as broadcast */
  504. if (dest == 0xff) {
  505. apic_get_broadcast_bitmask(deliver_bitmask, false);
  506. }
  507. } else {
  508. /* XXX: logical mode */
  509. for (i = 0; i < max_apics; i++) {
  510. apic = local_apics[i];
  511. if (apic) {
  512. /* x2APIC logical mode */
  513. if (apic->apicbase & MSR_IA32_APICBASE_EXTD) {
  514. if ((dest >> 16) == (apic->extended_log_dest >> 16) &&
  515. (dest & apic->extended_log_dest & 0xffff)) {
  516. apic_set_bit(deliver_bitmask, i);
  517. }
  518. continue;
  519. }
  520. /* xAPIC logical mode */
  521. dest = (uint8_t)dest;
  522. if (apic->dest_mode == APIC_DESTMODE_LOGICAL_FLAT) {
  523. if (dest & apic->log_dest) {
  524. apic_set_bit(deliver_bitmask, i);
  525. }
  526. } else if (apic->dest_mode == APIC_DESTMODE_LOGICAL_CLUSTER) {
  527. /*
  528. * In cluster model of xAPIC logical mode IPI, 4 higher
  529. * bits are used as cluster address, 4 lower bits are
  530. * the bitmask for local APICs in the cluster. The IPI
  531. * is delivered to an APIC if the cluster address
  532. * matches and the APIC's address bit in the cluster is
  533. * set in bitmask of destination ID in IPI.
  534. *
  535. * The cluster address ranges from 0 - 14, the cluster
  536. * address 15 (0xf) is the broadcast address to all
  537. * clusters.
  538. */
  539. if ((dest & 0xf0) == 0xf0 ||
  540. (dest & 0xf0) == (apic->log_dest & 0xf0)) {
  541. if (dest & apic->log_dest & 0x0f) {
  542. apic_set_bit(deliver_bitmask, i);
  543. }
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. static void apic_startup(APICCommonState *s, int vector_num)
  551. {
  552. s->sipi_vector = vector_num;
  553. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  554. }
  555. void apic_sipi(DeviceState *dev)
  556. {
  557. APICCommonState *s = APIC(dev);
  558. cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  559. if (!s->wait_for_sipi)
  560. return;
  561. cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
  562. s->wait_for_sipi = 0;
  563. }
  564. static void apic_deliver(DeviceState *dev, uint32_t dest, uint8_t dest_mode,
  565. uint8_t delivery_mode, uint8_t vector_num,
  566. uint8_t trigger_mode, uint8_t dest_shorthand)
  567. {
  568. APICCommonState *s = APIC(dev);
  569. APICCommonState *apic_iter;
  570. uint32_t deliver_bitmask_size = max_apic_words * sizeof(uint32_t);
  571. g_autofree uint32_t *deliver_bitmask = g_new(uint32_t, max_apic_words);
  572. uint32_t current_apic_id;
  573. if (is_x2apic_mode(dev)) {
  574. current_apic_id = s->initial_apic_id;
  575. } else {
  576. current_apic_id = s->id;
  577. }
  578. switch (dest_shorthand) {
  579. case 0:
  580. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  581. break;
  582. case 1:
  583. memset(deliver_bitmask, 0x00, deliver_bitmask_size);
  584. apic_set_bit(deliver_bitmask, current_apic_id);
  585. break;
  586. case 2:
  587. memset(deliver_bitmask, 0xff, deliver_bitmask_size);
  588. break;
  589. case 3:
  590. memset(deliver_bitmask, 0xff, deliver_bitmask_size);
  591. apic_reset_bit(deliver_bitmask, current_apic_id);
  592. break;
  593. }
  594. switch (delivery_mode) {
  595. case APIC_DM_INIT:
  596. {
  597. int trig_mode = (s->icr[0] >> 15) & 1;
  598. int level = (s->icr[0] >> 14) & 1;
  599. if (level == 0 && trig_mode == 1) {
  600. foreach_apic(apic_iter, deliver_bitmask,
  601. apic_iter->arb_id = apic_iter->id );
  602. return;
  603. }
  604. }
  605. break;
  606. case APIC_DM_SIPI:
  607. foreach_apic(apic_iter, deliver_bitmask,
  608. apic_startup(apic_iter, vector_num) );
  609. return;
  610. }
  611. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
  612. }
  613. static bool apic_check_pic(APICCommonState *s)
  614. {
  615. DeviceState *dev = (DeviceState *)s;
  616. if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  617. return false;
  618. }
  619. apic_deliver_pic_intr(dev, 1);
  620. return true;
  621. }
  622. int apic_get_interrupt(DeviceState *dev)
  623. {
  624. APICCommonState *s = APIC(dev);
  625. int intno;
  626. /* if the APIC is installed or enabled, we let the 8259 handle the
  627. IRQs */
  628. if (!s)
  629. return -1;
  630. if (!(s->spurious_vec & APIC_SV_ENABLE))
  631. return -1;
  632. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  633. intno = apic_irq_pending(s);
  634. /* if there is an interrupt from the 8259, let the caller handle
  635. * that first since ExtINT interrupts ignore the priority.
  636. */
  637. if (intno == 0 || apic_check_pic(s)) {
  638. apic_sync_vapic(s, SYNC_TO_VAPIC);
  639. return -1;
  640. } else if (intno < 0) {
  641. apic_sync_vapic(s, SYNC_TO_VAPIC);
  642. return s->spurious_vec & 0xff;
  643. }
  644. apic_reset_bit(s->irr, intno);
  645. apic_set_bit(s->isr, intno);
  646. apic_sync_vapic(s, SYNC_TO_VAPIC);
  647. apic_update_irq(s);
  648. return intno;
  649. }
  650. int apic_accept_pic_intr(DeviceState *dev)
  651. {
  652. APICCommonState *s = APIC(dev);
  653. uint32_t lvt0;
  654. if (!s)
  655. return -1;
  656. lvt0 = s->lvt[APIC_LVT_LINT0];
  657. if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
  658. (lvt0 & APIC_LVT_MASKED) == 0)
  659. return isa_pic != NULL;
  660. return 0;
  661. }
  662. static void apic_timer_update(APICCommonState *s, int64_t current_time)
  663. {
  664. if (apic_next_timer(s, current_time)) {
  665. timer_mod(s->timer, s->next_time);
  666. } else {
  667. timer_del(s->timer);
  668. }
  669. }
  670. static void apic_timer(void *opaque)
  671. {
  672. APICCommonState *s = opaque;
  673. apic_local_deliver(s, APIC_LVT_TIMER);
  674. apic_timer_update(s, s->next_time);
  675. }
  676. static int apic_register_read(int index, uint64_t *value)
  677. {
  678. DeviceState *dev;
  679. APICCommonState *s;
  680. uint32_t val;
  681. int ret = 0;
  682. dev = cpu_get_current_apic();
  683. if (!dev) {
  684. return -1;
  685. }
  686. s = APIC(dev);
  687. switch(index) {
  688. case 0x02: /* id */
  689. if (is_x2apic_mode(dev)) {
  690. val = s->initial_apic_id;
  691. } else {
  692. val = s->id << 24;
  693. }
  694. break;
  695. case 0x03: /* version */
  696. val = s->version | ((APIC_LVT_NB - 1) << 16);
  697. break;
  698. case 0x08:
  699. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  700. if (apic_report_tpr_access) {
  701. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_READ);
  702. }
  703. val = s->tpr;
  704. break;
  705. case 0x09:
  706. val = apic_get_arb_pri(s);
  707. break;
  708. case 0x0a:
  709. /* ppr */
  710. val = apic_get_ppr(s);
  711. break;
  712. case 0x0b:
  713. val = 0;
  714. break;
  715. case 0x0d:
  716. if (is_x2apic_mode(dev)) {
  717. val = s->extended_log_dest;
  718. } else {
  719. val = s->log_dest << 24;
  720. }
  721. break;
  722. case 0x0e:
  723. if (is_x2apic_mode(dev)) {
  724. val = 0;
  725. ret = -1;
  726. } else {
  727. val = (s->dest_mode << 28) | 0xfffffff;
  728. }
  729. break;
  730. case 0x0f:
  731. val = s->spurious_vec;
  732. break;
  733. case 0x10 ... 0x17:
  734. val = s->isr[index & 7];
  735. break;
  736. case 0x18 ... 0x1f:
  737. val = s->tmr[index & 7];
  738. break;
  739. case 0x20 ... 0x27:
  740. val = s->irr[index & 7];
  741. break;
  742. case 0x28:
  743. val = s->esr;
  744. break;
  745. case 0x30:
  746. case 0x31:
  747. val = s->icr[index & 1];
  748. break;
  749. case 0x32 ... 0x37:
  750. val = s->lvt[index - 0x32];
  751. break;
  752. case 0x38:
  753. val = s->initial_count;
  754. break;
  755. case 0x39:
  756. val = apic_get_current_count(s);
  757. break;
  758. case 0x3e:
  759. val = s->divide_conf;
  760. break;
  761. default:
  762. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  763. val = 0;
  764. ret = -1;
  765. break;
  766. }
  767. trace_apic_register_read(index, val);
  768. *value = val;
  769. return ret;
  770. }
  771. static uint64_t apic_mem_read(void *opaque, hwaddr addr, unsigned size)
  772. {
  773. uint64_t val;
  774. int index;
  775. if (size < 4) {
  776. return 0;
  777. }
  778. index = (addr >> 4) & 0xff;
  779. apic_register_read(index, &val);
  780. return val;
  781. }
  782. int apic_msr_read(int index, uint64_t *val)
  783. {
  784. DeviceState *dev;
  785. dev = cpu_get_current_apic();
  786. if (!dev) {
  787. return -1;
  788. }
  789. if (!is_x2apic_mode(dev)) {
  790. return -1;
  791. }
  792. return apic_register_read(index, val);
  793. }
  794. static void apic_send_msi(MSIMessage *msi)
  795. {
  796. uint64_t addr = msi->address;
  797. uint32_t data = msi->data;
  798. uint32_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
  799. /*
  800. * The higher 3 bytes of destination id is stored in higher word of
  801. * msi address. See x86_iommu_irq_to_msi_message()
  802. */
  803. dest = dest | (addr >> 32);
  804. uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
  805. uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
  806. uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
  807. uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
  808. /* XXX: Ignore redirection hint. */
  809. apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
  810. }
  811. static int apic_register_write(int index, uint64_t val)
  812. {
  813. DeviceState *dev;
  814. APICCommonState *s;
  815. dev = cpu_get_current_apic();
  816. if (!dev) {
  817. return -1;
  818. }
  819. s = APIC(dev);
  820. trace_apic_register_write(index, val);
  821. switch(index) {
  822. case 0x02:
  823. if (is_x2apic_mode(dev)) {
  824. return -1;
  825. }
  826. s->id = (val >> 24);
  827. break;
  828. case 0x03:
  829. break;
  830. case 0x08:
  831. if (apic_report_tpr_access) {
  832. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_WRITE);
  833. }
  834. s->tpr = val;
  835. apic_sync_vapic(s, SYNC_TO_VAPIC);
  836. apic_update_irq(s);
  837. break;
  838. case 0x09:
  839. case 0x0a:
  840. break;
  841. case 0x0b: /* EOI */
  842. apic_eoi(s);
  843. break;
  844. case 0x0d:
  845. if (is_x2apic_mode(dev)) {
  846. return -1;
  847. }
  848. s->log_dest = val >> 24;
  849. break;
  850. case 0x0e:
  851. if (is_x2apic_mode(dev)) {
  852. return -1;
  853. }
  854. s->dest_mode = val >> 28;
  855. break;
  856. case 0x0f:
  857. s->spurious_vec = val & 0x1ff;
  858. apic_update_irq(s);
  859. break;
  860. case 0x10 ... 0x17:
  861. case 0x18 ... 0x1f:
  862. case 0x20 ... 0x27:
  863. case 0x28:
  864. break;
  865. case 0x30: {
  866. uint32_t dest;
  867. s->icr[0] = val;
  868. if (is_x2apic_mode(dev)) {
  869. s->icr[1] = val >> 32;
  870. dest = s->icr[1];
  871. } else {
  872. dest = (s->icr[1] >> 24) & 0xff;
  873. }
  874. apic_deliver(dev, dest, (s->icr[0] >> 11) & 1,
  875. (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
  876. (s->icr[0] >> 15) & 1, (s->icr[0] >> 18) & 3);
  877. break;
  878. }
  879. case 0x31:
  880. if (is_x2apic_mode(dev)) {
  881. return -1;
  882. }
  883. s->icr[1] = val;
  884. break;
  885. case 0x32 ... 0x37:
  886. {
  887. int n = index - 0x32;
  888. s->lvt[n] = val;
  889. if (n == APIC_LVT_TIMER) {
  890. apic_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  891. } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
  892. apic_update_irq(s);
  893. }
  894. }
  895. break;
  896. case 0x38:
  897. s->initial_count = val;
  898. s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  899. apic_timer_update(s, s->initial_count_load_time);
  900. break;
  901. case 0x39:
  902. break;
  903. case 0x3e:
  904. {
  905. int v;
  906. s->divide_conf = val & 0xb;
  907. v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
  908. s->count_shift = (v + 1) & 7;
  909. }
  910. break;
  911. case 0x3f: {
  912. int vector = val & 0xff;
  913. if (!is_x2apic_mode(dev)) {
  914. return -1;
  915. }
  916. /*
  917. * Self IPI is identical to IPI with
  918. * - Destination shorthand: 1 (Self)
  919. * - Trigger mode: 0 (Edge)
  920. * - Delivery mode: 0 (Fixed)
  921. */
  922. apic_deliver(dev, 0, 0, APIC_DM_FIXED, vector, 0, 1);
  923. break;
  924. }
  925. default:
  926. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  927. return -1;
  928. }
  929. return 0;
  930. }
  931. static void apic_mem_write(void *opaque, hwaddr addr, uint64_t val,
  932. unsigned size)
  933. {
  934. int index = (addr >> 4) & 0xff;
  935. if (size < 4) {
  936. return;
  937. }
  938. if (addr > 0xfff || !index) {
  939. /*
  940. * MSI and MMIO APIC are at the same memory location,
  941. * but actually not on the global bus: MSI is on PCI bus
  942. * APIC is connected directly to the CPU.
  943. * Mapping them on the global bus happens to work because
  944. * MSI registers are reserved in APIC MMIO and vice versa.
  945. */
  946. MSIMessage msi = { .address = addr, .data = val };
  947. apic_send_msi(&msi);
  948. return;
  949. }
  950. apic_register_write(index, val);
  951. }
  952. int apic_msr_write(int index, uint64_t val)
  953. {
  954. DeviceState *dev;
  955. dev = cpu_get_current_apic();
  956. if (!dev) {
  957. return -1;
  958. }
  959. if (!is_x2apic_mode(dev)) {
  960. return -1;
  961. }
  962. return apic_register_write(index, val);
  963. }
  964. static void apic_pre_save(APICCommonState *s)
  965. {
  966. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  967. }
  968. static void apic_post_load(APICCommonState *s)
  969. {
  970. if (s->timer_expiry != -1) {
  971. timer_mod(s->timer, s->timer_expiry);
  972. } else {
  973. timer_del(s->timer);
  974. }
  975. }
  976. static const MemoryRegionOps apic_io_ops = {
  977. .read = apic_mem_read,
  978. .write = apic_mem_write,
  979. .impl.min_access_size = 1,
  980. .impl.max_access_size = 4,
  981. .valid.min_access_size = 1,
  982. .valid.max_access_size = 4,
  983. .endianness = DEVICE_NATIVE_ENDIAN,
  984. };
  985. static void apic_realize(DeviceState *dev, Error **errp)
  986. {
  987. APICCommonState *s = APIC(dev);
  988. if (kvm_enabled()) {
  989. warn_report("Userspace local APIC is deprecated for KVM.");
  990. warn_report("Do not use kernel-irqchip except for the -M isapc machine type.");
  991. }
  992. memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
  993. APIC_SPACE_SIZE);
  994. /*
  995. * apic-msi's apic_mem_write can call into ioapic_eoi_broadcast, which can
  996. * write back to apic-msi. As such mark the apic-msi region re-entrancy
  997. * safe.
  998. */
  999. s->io_memory.disable_reentrancy_guard = true;
  1000. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
  1001. /*
  1002. * The --machine none does not call apic_set_max_apic_id before creating
  1003. * apic, so we need to call it here and set it to 1 which is the max cpus
  1004. * in machine none.
  1005. */
  1006. if (!local_apics) {
  1007. apic_set_max_apic_id(1);
  1008. }
  1009. local_apics[s->initial_apic_id] = s;
  1010. msi_nonbroken = true;
  1011. }
  1012. static void apic_unrealize(DeviceState *dev)
  1013. {
  1014. APICCommonState *s = APIC(dev);
  1015. timer_free(s->timer);
  1016. local_apics[s->initial_apic_id] = NULL;
  1017. }
  1018. static void apic_class_init(ObjectClass *klass, void *data)
  1019. {
  1020. APICCommonClass *k = APIC_COMMON_CLASS(klass);
  1021. k->realize = apic_realize;
  1022. k->unrealize = apic_unrealize;
  1023. k->set_base = apic_set_base;
  1024. k->set_tpr = apic_set_tpr;
  1025. k->get_tpr = apic_get_tpr;
  1026. k->vapic_base_update = apic_vapic_base_update;
  1027. k->external_nmi = apic_external_nmi;
  1028. k->pre_save = apic_pre_save;
  1029. k->post_load = apic_post_load;
  1030. k->send_msi = apic_send_msi;
  1031. }
  1032. static const TypeInfo apic_info = {
  1033. .name = TYPE_APIC,
  1034. .instance_size = sizeof(APICCommonState),
  1035. .parent = TYPE_APIC_COMMON,
  1036. .class_init = apic_class_init,
  1037. };
  1038. static void apic_register_types(void)
  1039. {
  1040. type_register_static(&apic_info);
  1041. }
  1042. type_init(apic_register_types)