2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <palacios/vmm_intr.h>
23 #include <palacios/vmm_types.h>
24 #include <palacios/vmm.h>
25 #include <palacios/vmm_dev_mgr.h>
26 #include <palacios/vm_guest.h>
28 #ifndef V3_CONFIG_DEBUG_PIC
30 #define PrintDebug(fmt, args...)
34 typedef enum {RESET, ICW1, ICW2, ICW3, ICW4, READY} pic_state_t;
36 static const uint_t MASTER_PORT1 = 0x20;
37 static const uint_t MASTER_PORT2 = 0x21;
38 static const uint_t SLAVE_PORT1 = 0xA0;
39 static const uint_t SLAVE_PORT2 = 0xA1;
41 static const uint_t ELCR1_PORT = 0x4d0;
42 static const uint_t ELCR2_PORT = 0x4d1;
45 #define IS_ICW1(x) (((x & 0x10) >> 4) == 0x1)
46 #define IS_OCW2(x) (((x & 0x18) >> 3) == 0x0)
47 #define IS_OCW3(x) (((x & 0x18) >> 3) == 0x1)
51 uint_t ic4 : 1; // ICW4 has to be read
52 uint_t sngl : 1; // single (only one PIC)
53 uint_t adi : 1; // call address interval
54 uint_t ltim : 1; // level interrupt mode
66 // Each bit that is set indicates that the IR input has a slave
78 // The ID is the Slave device ID
85 uint_t uPM : 1; // 1=x86
86 uint_t AEOI : 1; // Automatic End of Interrupt
87 uint_t M_S : 1; // only if buffered 1=master,0=slave
88 uint_t BUF : 1; // buffered mode
89 uint_t SFNM : 1; // special fully nexted mode
107 uint_t cw_code : 2; // should be 00
117 uint_t cw_code : 2; // should be 01
124 struct pic_internal {
135 uint8_t master_elcr_mask;
136 uint8_t slave_elcr_mask;
157 pic_state_t master_state;
158 pic_state_t slave_state;
160 struct guest_info * core;
163 int (*ack)(struct guest_info * core, uint32_t irq, void * private_data);
168 void * router_handle;
169 void * controller_handle;
173 static void DumpPICState(struct pic_internal *p)
176 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_state=0x%x\n",p->master_state);
177 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_irr=0x%x\n",p->master_irr);
178 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_isr=0x%x\n",p->master_isr);
179 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_imr=0x%x\n",p->master_imr);
181 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_ocw2=0x%x\n",p->master_ocw2);
182 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_ocw3=0x%x\n",p->master_ocw3);
184 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_icw1=0x%x\n",p->master_icw1);
185 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_icw2=0x%x\n",p->master_icw2);
186 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_icw3=0x%x\n",p->master_icw3);
187 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: master_icw4=0x%x\n",p->master_icw4);
189 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_state=0x%x\n",p->slave_state);
190 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_irr=0x%x\n",p->slave_irr);
191 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_isr=0x%x\n",p->slave_isr);
192 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_imr=0x%x\n",p->slave_imr);
194 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_ocw2=0x%x\n",p->slave_ocw2);
195 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_ocw3=0x%x\n",p->slave_ocw3);
197 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_icw1=0x%x\n",p->slave_icw1);
198 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_icw2=0x%x\n",p->slave_icw2);
199 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_icw3=0x%x\n",p->slave_icw3);
200 V3_Print(VM_NONE, VCORE_NONE, "8259 PIC: slave_icw4=0x%x\n",p->slave_icw4);
205 static int pic_vec_to_irq(struct guest_info *info, struct pic_internal *state, int vec)
207 if ((vec >= state->master_icw2) && (vec <= state->master_icw2 + 7)) {
209 } else if ((vec >= state->slave_icw2) && (vec <= state->slave_icw2 + 7)) {
210 return (vec & 0x7) + 8;
212 // Note that this is not an error since there may also be IOAPICs
213 PrintDebug(info->vm_info, info, "8259 PIC: Cannot translate vector %d back to an IRQ I support\n",vec);
218 static int pic_irq_to_vec(struct guest_info *info, struct pic_internal *state, int irq)
224 // This will treat IRQ2 as occuring on the master,
225 // not on slave IRQ9 as expected for legacy behavior
226 // We shouldn't see anything attempting to raise IRQ2...
228 PrintError(info->vm_info, info, "8259 PIC: Warning - IRQ 2 is being translated...\n");
232 return irq + state->master_icw2;
233 } else if (irq<=15) {
234 return (irq-8) + state->slave_icw2;
236 PrintDebug(info->vm_info, info, "8259 PIC: Warning: IRQ %d is being translated, but only IRQs 0..15 are supported\n",irq);
243 static int pic_raise_intr(struct v3_vm_info * vm, void * private_data, struct v3_irq * irq) {
244 struct pic_internal * state = (struct pic_internal*)private_data;
245 uint8_t irq_num = irq->irq;
248 PrintError(vm, VCORE_NONE, "8259 PIC: Warning - IRQ 2 is being raised...\n");
249 // This is the legacy reroute of IRQ2 to IRQ9
253 PrintDebug(vm, VCORE_NONE, "8259 PIC: Raising irq %d in the PIC\n", irq_num);
256 state->master_irr |= 0x01 << irq_num;
257 PrintDebug(vm, VCORE_NONE, "8259 PIC: Master: Raising IRQ %d\n",irq_num);
258 } else if ((irq_num > 7) && (irq_num < 16)) {
259 state->slave_irr |= 0x01 << (irq_num - 8);
260 state->master_irr |= 0x04; // immediately signal to the master pin we're attached to
261 PrintDebug(vm, VCORE_NONE, "8259 PIC: Master + Slave: Raising IRQ %d\n",irq_num);
263 // This is not an error as the system could have other interrupt controllers
264 PrintDebug(vm, VCORE_NONE, "8259 PIC: Ignoring raise of IRQ %d as it is not supported by the PIC\n", irq_num);
268 state->irq_ack_cbs[irq_num].ack = irq->ack;
269 state->irq_ack_cbs[irq_num].private_data = irq->private_data;
271 if (V3_Get_CPU() != vm->cores[0].pcpu_id) {
272 // guest is running on another core, interrupt it to deliver irq
273 v3_interrupt_cpu(vm, 0, 0);
280 static int pic_lower_intr(struct v3_vm_info * vm, void * private_data, struct v3_irq * irq) {
281 struct pic_internal * state = (struct pic_internal*)private_data;
282 uint8_t irq_num = irq->irq;
285 PrintError(vm, VCORE_NONE, "8259 PIC: Warning - IRQ 2 is being lowered...\n");
286 // Legacy reroute of IRQ2 to IRQ9
290 PrintDebug(vm, VCORE_NONE, "8259 PIC: [pic_lower_intr] IRQ line %d now low\n", irq_num);
294 PrintDebug(vm, VCORE_NONE, "8259 PIC: Master: IRQ line %d lowered\n", irq_num);
295 state->master_irr &= ~(1 << irq_num);
296 // Note that another interrupt may still be in the IRR, but that's OK
297 // We'll recognize it on the next entry
298 } else if ((irq_num > 7) && (irq_num < 16)) {
300 PrintDebug(vm, VCORE_NONE, "8259 PIC: Slave: IRQ line %d lowered\n", irq_num);
301 state->slave_irr &= ~(1 << (irq_num - 8));
302 if ((state->slave_irr & (~(state->slave_imr))) == 0) {
303 // If there is no other slave interrupt available, we can
304 // turn off IRQ2 on the master
305 PrintDebug(vm, VCORE_NONE, "8259 PIC: Master: IRQ line 2 also lowered due to no other interrupts pending in slave\n");
306 state->master_irr &= ~(0x04);
309 // This is not an error as the system could have other interrupt controllers
310 PrintDebug(vm, VCORE_NONE, "8259 PIC: Ignoring lower of IRQ %d as it is not supported by the PIC\n",irq_num);
318 static int pic_intr_pending_from_master(struct guest_info * info, void * private_data) {
319 struct pic_internal * state = (struct pic_internal*)private_data;
321 return state->master_irr & (~(state->master_imr));
324 static int pic_intr_pending_from_slave(struct guest_info * info, void * private_data) {
325 struct pic_internal * state = (struct pic_internal*)private_data;
327 return (!(state->master_imr & 0x4)) && // master has slave unmasked and
328 (state->slave_irr & (~(state->slave_imr))); // slave is pending
331 static int pic_intr_pending(struct guest_info * info, void * private_data) {
333 return pic_intr_pending_from_master(info,private_data) ||
334 pic_intr_pending_from_slave(info,private_data);
338 8259 prioritization is oddball since there are two chips. The
339 slave chip signals an interrupt through pin 2 of the master chip.
340 This means that all the slave chip's pins are actually at a higher priority
341 than pins 3..7 of the master. The scheme is as follows, from highest
342 to lowest priority, including legacy mappings:
344 Master Slave Typical Legacy Use
345 --------------------------------------------------------------
348 IRQ2 ****NOT USED - Slave chip inputs here
350 IRQ9 VGA / previous IRQ2 (or PCI via PIRQ LINK B)
351 IRQ10 unused (or PCI via PIRQ LINK C)
352 IRQ11 unused (or PCI via PIRQ LINK D)
353 IRQ12 PS/2 Mouse (8042)
354 IRQ13 Coprocessor error
355 IRQ14 First IDE controller
356 IRQ15 Second IDE controller
357 IRQ3 Second and Fourth Serial Port (COM2/4)
358 IRQ4 First and Third serial port (COM1/3)
359 IRQ5 Second Parallel Port (or PCI via PIRQ LINK A)
360 IRQ6 Floppy controller
361 IRQ7 First Parallel Port
365 static int pic_get_intr_number(struct guest_info * info, void * private_data) {
366 struct pic_internal * state = (struct pic_internal *)private_data;
370 PrintDebug(info->vm_info, info, "8259 PIC: getnum: master_irr: 0x%x master_imr: 0x%x\n", state->master_irr, state->master_imr);
371 PrintDebug(info->vm_info, info, "8259 PIC: getnum: slave_irr: 0x%x slave_imr: 0x%x\n", state->slave_irr, state->slave_imr);
373 // First, see if we have something upstream of the slave
375 // Interrupt requested and not masked
376 if (((state->master_irr & ~(state->master_imr)) >> i) & 0x01) {
377 PrintDebug(info->vm_info, info, "8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
378 vec = pic_irq_to_vec(info, state, i);
380 PrintError(info->vm_info, info, "8259 PIC: Master Interrupt Ready, but vector=%d\n",vec);
387 if (vec<0 && // Nothing upstream and
388 !(state->master_imr & 0x4)) { // Master is not masking the slave
389 for (i = 8; i < 16; i++) {
390 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) & 0x01) {
391 PrintDebug(info->vm_info, info, "8259 PIC: IRQ: %d, slave_icw2: %x\n", i, state->slave_icw2);
392 vec = pic_irq_to_vec(info, state, i);
394 PrintError(info->vm_info, info, "8259 PIC: Slave Interrupt Readby, but vector=%d\n",vec);
401 // And finally the master downstream of the slave
403 for (i = 3; i < 8; i++) {
404 if (((state->master_irr & ~(state->master_imr)) >> i) & 0x01) {
405 PrintDebug(info->vm_info, info, "8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
406 vec = pic_irq_to_vec(info, state, i);
408 PrintError(info->vm_info, info, "8259 PIC: Master Interrupt Ready in 2nd pass, but vector=%d\n",vec);
416 PrintDebug(info->vm_info, info, "8259 PIC: get num is returning vector %d\n",vec);
418 PrintDebug(info->vm_info, info, "8259 PIC: no vector available\n");
427 /* The vec number is the number returned by pic_get_irq_number(), not the pin number. */
428 /* In other words, it's the INT vector the PIC is feeding the processor */
429 static int pic_begin_irq(struct guest_info * info, void * private_data, int vec) {
430 struct pic_internal * state = (struct pic_internal*)private_data;
433 irq = pic_vec_to_irq(info,state,vec);
436 // Not an error - could be for other interrupt controller
437 PrintDebug(info->vm_info,info,"8259 PIC: Ignoring begin_irq on vector %d since it's not ours\n", vec);
443 PrintDebug(info->vm_info, info, "8259 PIC: Master: Beginning IRQ %d\n",irq);
444 // This should always be true: See pic_get_irq_number
445 if (((state->master_irr & (~(state->master_imr))) >> irq) & 0x01) {
446 // unmasked - let's start it
447 state->master_isr |= (0x1 << irq);
448 // auto reset the request if the elcr has this as edge-triggered
449 if (!(state->master_elcr & (0x1 << irq))) {
450 state->master_irr &= ~(0x1 << irq);
453 PrintDebug(info->vm_info, info, "8259 PIC: Master: Ignoring begin_irq vector %d since I either do not see it set or have it masked (mnaster_irr=0x%x, master_imr=0x%x\n", irq, state->master_irr, state->master_imr);
455 } else if (irq>=8 && irq<=15) {
457 PrintDebug(info->vm_info, info, "8259 PIC: Master + Slave: Beginning IRQ %d\n",irq);
458 // This should always be true: See pic_get_irq_number
459 if (((state->slave_irr & (~(state->slave_imr))) >> (irq - 8)) & 0x01) {
460 // unmasked - so let's start it in the slave
461 state->slave_isr |= (0x1 << (irq - 8));
462 // We must have previously pushed it to the master's irr,
463 // so all we need to do here is put it in service there too
464 state->master_isr |= 0x4; // pin 2 is where the slave attaches
466 // auto-reset the request in the slave if it's marked as edge-triggered
467 if (!(state->slave_elcr & (0x1 << (irq - 8)))) {
468 state->slave_irr &= ~(0x1 << (irq - 8));
471 // auto-reset the request in pin 2 of the master if it's marked as edge-trigged
472 if (!(state->master_elcr & 0x04)) {
473 state->master_irr &= ~0x04;
476 PrintDebug(info->vm_info, info, "8259 PIC: Maser + Slave: Ignoring begin_irq for %d since I either don't see it set or I don't own it (master_irr=0x%x, master_imr=0x%x, slave_irr=0x%x, slave_imr=0x%x\n", irq,state->master_irr, state->master_imr, state->slave_irr, state->slave_imr);
479 PrintDebug(info->vm_info, info, "8259 PIC: Ignoring begin_irq for %d since I don't own it\n", irq);
487 static int pic_end_irq(void * private_data, int irq) {
494 static struct intr_ctrl_ops intr_ops = {
495 .intr_pending = pic_intr_pending,
496 .get_intr_number = pic_get_intr_number,
497 .begin_irq = pic_begin_irq
500 static struct intr_router_ops router_ops = {
501 .raise_intr = pic_raise_intr,
502 .lower_intr = pic_lower_intr
506 static int read_master_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
507 struct pic_internal * state = (struct pic_internal *)priv_data;
510 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid Read length (rd_Master1)\n");
514 if ((state->master_ocw3 & 0x03) == 0x02) {
515 *(uint8_t *)dst = state->master_irr;
516 } else if ((state->master_ocw3 & 0x03) == 0x03) {
517 *(uint8_t *)dst = state->master_isr;
525 static int read_master_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
526 struct pic_internal * state = (struct pic_internal *)priv_data;
529 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid Read length (rd_Master2)\n");
533 *(uint8_t *)dst = state->master_imr;
539 static int read_slave_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
540 struct pic_internal * state = (struct pic_internal *)priv_data;
543 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid Read length (rd_Slave1)\n");
547 if ((state->slave_ocw3 & 0x03) == 0x02) {
548 *(uint8_t*)dst = state->slave_irr;
549 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
550 *(uint8_t *)dst = state->slave_isr;
558 static int read_slave_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
559 struct pic_internal * state = (struct pic_internal *)priv_data;
562 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid Read length (rd_Slave2)\n");
566 *(uint8_t *)dst = state->slave_imr;
572 static int write_master_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
573 struct pic_internal * state = (struct pic_internal *)priv_data;
574 uint8_t cw = *(uint8_t *)src;
576 PrintDebug(core->vm_info, core, "8259 PIC: Master: Write port 1 with 0x%x\n",cw);
579 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid Write length (wr_Master1)\n");
583 v3_clear_pending_intr(core);
587 PrintDebug(core->vm_info, core, "8259 PIC: Master: Setting ICW1 = %x (wr_Master1)\n", cw);
589 state->master_icw1 = cw;
590 state->master_state = ICW2;
592 } else if (state->master_state == READY) {
594 // handle the EOI here
595 struct ocw2 * cw2 = (struct ocw2*)&cw;
598 PrintDebug(core->vm_info, core, "8259 PIC: Master: Handling OCW2 = %x (wr_Master1)\n", cw);
600 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
602 state->master_isr &= ~(0x01 << cw2->level);
603 eoi_irq = cw2->level;
606 // ack the irq if requested
607 if (state->irq_ack_cbs[irq].ack) {
608 state->irq_ack_cbs[irq].ack(info, irq, state->irq_ack_cbs[irq].private_data);
612 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
615 PrintDebug(core->vm_info, core, "8259 PIC: Master: Pre ISR = %x (wr_Master1)\n", state->master_isr);
616 for (i = 0; i < 8; i++) {
617 if (state->master_isr & (0x01 << i)) {
618 state->master_isr &= ~(0x01 << i);
624 PrintDebug(core->vm_info, core, "8259 PIC: Master: Strange... non-specific EOI but no in-service interrupts\n");
627 PrintDebug(core->vm_info, core, "8259 PIC: Master: Post ISR = %x (wr_Master1)\n", state->master_isr);
628 } else if (!(cw2->EOI) && (cw2->R) && (cw2->SL)) {
629 PrintDebug(core->vm_info, core, "8259 PIC: Master: Ignoring set-priority, priorities not implemented (level=%d, wr_Master1)\n", cw2->level);
630 } else if (!(cw2->EOI) && !(cw2->R) && (cw2->SL)) {
631 PrintDebug(core->vm_info, core, "8259 PIC: Master: Ignoring no-op (level=%d, wr_Master1)\n", cw2->level);
633 PrintError(core->vm_info, core, "8259 PIC: Master: Command not handled, or in error (wr_Master1)\n");
638 if (pic_intr_pending_from_master(core,state)) {
639 // this is perfectly fine as there may be other latched interrupts
640 // but it would be strange if the one we just cleared is suddenly
641 // alive again - well, depending on concurrent behavior external to
642 int irq = pic_vec_to_irq(core,state,pic_get_intr_number(core,state));
644 if (irq == eoi_irq) {
645 // Not necessarily an error, since it could have been raised again in another thread...
646 PrintError(core->vm_info, core, "8259 PIC: Master: IRQ %d pending after EOI of IRQ %d\n", irq,eoi_irq);
652 state->master_ocw2 = cw;
653 } else if (IS_OCW3(cw)) {
654 PrintDebug(core->vm_info, core, "8259 PIC: Master: Handling OCW3 = %x (wr_Master1)\n", cw);
655 state->master_ocw3 = cw;
657 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid OCW to PIC (wr_Master1)\n");
658 PrintError(core->vm_info, core, "8259 PIC: Master: CW=%x\n", cw);
662 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid PIC State (wr_Master1)\n");
663 PrintError(core->vm_info, core, "8259 PIC: Master: CW=%x\n", cw);
670 static int write_master_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
671 struct pic_internal * state = (struct pic_internal *)priv_data;
672 uint8_t cw = *(uint8_t *)src;
674 PrintDebug(core->vm_info, core, "8259 PIC: Master: Write master port 2 with 0x%x\n",cw);
677 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid Write length (wr_Master2)\n");
681 v3_clear_pending_intr(core);
683 if (state->master_state == ICW2) {
684 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
686 PrintDebug(core->vm_info, core, "8259 PIC: Master: Setting ICW2 = %x (wr_Master2)\n", cw);
687 state->master_icw2 = cw;
691 if (cw1->sngl == 0) {
692 state->master_state = ICW3;
693 } else if (cw1->ic4 == 1) {
694 state->master_state = ICW4;
696 state->master_state = READY;
701 } else if (state->master_state == ICW3) {
702 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
704 PrintDebug(core->vm_info, core, "8259 PIC: Master: Setting ICW3 = %x (wr_Master2)\n", cw);
706 state->master_icw3 = cw;
709 state->master_state = ICW4;
711 state->master_state = READY;
714 } else if (state->master_state == ICW4) {
715 PrintDebug(core->vm_info, core, "8259 PIC: Master: Setting ICW4 = %x (wr_Master2)\n", cw);
716 state->master_icw4 = cw;
717 state->master_state = READY;
718 } else if ((state->master_state == ICW1) || (state->master_state == READY)) {
719 PrintDebug(core->vm_info, core, "8259 PIC: Master: Setting IMR = %x (wr_Master2)\n", cw);
720 state->master_imr = cw;
723 PrintError(core->vm_info, core, "8259 PIC: Master: Invalid master PIC State (wr_Master2) (state=%d)\n",
724 state->master_state);
731 static int write_slave_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
732 struct pic_internal * state = (struct pic_internal *)priv_data;
733 uint8_t cw = *(uint8_t *)src;
735 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Write slave port 1 with 0x%x\n",cw);
739 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid Write length (wr_Slave1)\n");
743 v3_clear_pending_intr(core);
746 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting ICW1 = %x (wr_Slave1)\n", cw);
747 state->slave_icw1 = cw;
748 state->slave_state = ICW2;
749 } else if (state->slave_state == READY) {
752 // handle the EOI here
753 struct ocw2 * cw2 = (struct ocw2 *)&cw;
755 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting OCW2 = %x (wr_Slave1)\n", cw);
757 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
759 state->slave_isr &= ~(0x01 << cw2->level);
760 eoi_irq = 8+cw2->level;
761 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
764 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
765 for (i = 0; i < 8; i++) {
766 if (state->slave_isr & (0x01 << i)) {
767 state->slave_isr &= ~(0x01 << i);
773 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Strange... non-specific EOI but no in-service interrupts\n");
775 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
777 PrintError(core->vm_info, core, "8259 PIC: Slave: Command not handled or invalid (wr_Slave1)\n");
781 // If we now have no further requested interrupts,
782 // we are not requesting from the master either
783 if (!(state->slave_irr)) {
784 state->master_irr &= ~0x04;
788 if (pic_intr_pending_from_slave(core,state)) {
789 // this is perfectly fine as there may be other latched interrupts
790 // but it would be strange if the one we just cleared is suddenly
791 // alive again - well, depending on concurrent behavior external to
792 int irq = pic_vec_to_irq(core,state,pic_get_intr_number(core,state));
794 if (irq == eoi_irq) {
795 // Not necessarily an error, since it could have been raised again in another thread.
796 PrintError(core->vm_info, core, "8259 PIC: Slave: IRQ %d pending after EOI of IRQ %d\n", irq,eoi_irq);
802 state->slave_ocw2 = cw;
803 } else if (IS_OCW3(cw)) {
804 // Basically sets the IRR/ISR read flag
805 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting OCW3 = %x (wr_Slave1)\n", cw);
806 state->slave_ocw3 = cw;
808 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid command work (wr_Slave1)\n");
812 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid State writing (wr_Slave1)\n");
819 static int write_slave_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
820 struct pic_internal * state = (struct pic_internal *)priv_data;
821 uint8_t cw = *(uint8_t *)src;
823 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Write slave port 2 with 0x%x\n",cw);
826 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid write length (wr_Slave2)\n");
830 v3_clear_pending_intr(core);
833 if (state->slave_state == ICW2) {
834 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
836 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting ICW2 = %x (wr_Slave2)\n", cw);
838 state->slave_icw2 = cw;
840 if (cw1->sngl == 0) {
841 state->slave_state = ICW3;
842 } else if (cw1->ic4 == 1) {
843 state->slave_state = ICW4;
845 state->slave_state = READY;
848 } else if (state->slave_state == ICW3) {
849 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
851 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting ICW3 = %x (wr_Slave2)\n", cw);
853 state->slave_icw3 = cw;
856 state->slave_state = ICW4;
858 state->slave_state = READY;
861 } else if (state->slave_state == ICW4) {
862 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting ICW4 = %x (wr_Slave2)\n", cw);
863 state->slave_icw4 = cw;
864 state->slave_state = READY;
865 } else if ((state->slave_state == ICW1) || (state->slave_state == READY)) {
866 PrintDebug(core->vm_info, core, "8259 PIC: Slave: Setting IMR = %x (wr_Slave2)\n", cw);
867 state->slave_imr = cw;
869 PrintError(core->vm_info, core, "8259 PIC: Slave: Invalid State at write (wr_Slave2)\n");
879 static int read_elcr_port(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
880 struct pic_internal * state = (struct pic_internal *)priv_data;
883 PrintError(core->vm_info, core, "8259 PIC: ELCR read of invalid length %d\n", length);
887 if (port == ELCR1_PORT) {
889 *(uint8_t *)dst = state->master_elcr;
890 } else if (port == ELCR2_PORT) {
891 *(uint8_t *)dst = state->slave_elcr;
893 PrintError(core->vm_info, core, "8259 PIC: Invalid port %x\n", port);
901 static int write_elcr_port(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
902 struct pic_internal * state = (struct pic_internal *)priv_data;
905 PrintError(core->vm_info, core, "8259 PIC: ELCR read of invalid length %d\n", length);
909 if (port == ELCR1_PORT) {
911 state->master_elcr = (*(uint8_t *)src) & state->master_elcr_mask;
912 } else if (port == ELCR2_PORT) {
913 state->slave_elcr = (*(uint8_t *)src) & state->slave_elcr_mask;
915 PrintError(core->vm_info, core, "8259 PIC: Invalid port %x\n", port);
924 static int pic_free(struct pic_internal * state) {
925 struct guest_info * core = state->core;
927 v3_remove_intr_controller(core, state->controller_handle);
928 v3_remove_intr_router(core->vm_info, state->router_handle);
934 #ifdef V3_CONFIG_CHECKPOINT
935 static int pic_save(struct v3_chkpt_ctx * ctx, void * private_data) {
936 struct pic_internal * pic = (struct pic_internal *)private_data;
938 V3_CHKPT_SAVE(ctx, "MASTER_IRR", pic->master_irr, savefailout);
939 V3_CHKPT_SAVE(ctx, "SLAVE_IRR", pic->slave_irr, savefailout);
941 V3_CHKPT_SAVE(ctx, "MASTER_ISR", pic->master_isr, savefailout);
942 V3_CHKPT_SAVE(ctx, "SLAVE_ISR", pic->slave_isr, savefailout);
944 V3_CHKPT_SAVE(ctx, "MASTER_ELCR", pic->master_elcr, savefailout);
945 V3_CHKPT_SAVE(ctx, "SLAVE_ELCR", pic->slave_elcr, savefailout);
946 V3_CHKPT_SAVE(ctx, "MASTER_ELCR_MASK", pic->master_elcr_mask, savefailout);
947 V3_CHKPT_SAVE(ctx, "SLAVE_ELCR_MASK", pic->slave_elcr_mask, savefailout);
949 V3_CHKPT_SAVE(ctx, "MASTER_ICW1", pic->master_icw1, savefailout);
950 V3_CHKPT_SAVE(ctx, "MASTER_ICW2", pic->master_icw2, savefailout);
951 V3_CHKPT_SAVE(ctx, "MASTER_ICW3", pic->master_icw3, savefailout);
952 V3_CHKPT_SAVE(ctx, "MASTER_ICW4", pic->master_icw4, savefailout);
955 V3_CHKPT_SAVE(ctx, "SLAVE_ICW1", pic->slave_icw1, savefailout);
956 V3_CHKPT_SAVE(ctx, "SLAVE_ICW2", pic->slave_icw2, savefailout);
957 V3_CHKPT_SAVE(ctx, "SLAVE_ICW3", pic->slave_icw3, savefailout);
958 V3_CHKPT_SAVE(ctx, "SLAVE_ICW4", pic->slave_icw4, savefailout);
961 V3_CHKPT_SAVE(ctx, "MASTER_IMR", pic->master_imr, savefailout);
962 V3_CHKPT_SAVE(ctx, "SLAVE_IMR", pic->slave_imr, savefailout);
963 V3_CHKPT_SAVE(ctx, "MASTER_OCW2", pic->master_ocw2, savefailout);
964 V3_CHKPT_SAVE(ctx, "MASTER_OCW3", pic->master_ocw3, savefailout);
965 V3_CHKPT_SAVE(ctx, "SLAVE_OCW2", pic->slave_ocw2, savefailout);
966 V3_CHKPT_SAVE(ctx, "SLAVE_OCW3", pic->slave_ocw3, savefailout);
968 V3_CHKPT_SAVE(ctx, "MASTER_STATE", pic->master_state, savefailout);
969 V3_CHKPT_SAVE(ctx, "SLAVE_STATE", pic->slave_state, savefailout);
975 PrintError(VM_NONE, VCORE_NONE, "Failed to save PIC\n");
980 static int pic_load(struct v3_chkpt_ctx * ctx, void * private_data) {
981 struct pic_internal * pic = (struct pic_internal *)private_data;
984 V3_CHKPT_LOAD(ctx, "MASTER_IRR", pic->master_irr, loadfailout);
985 V3_CHKPT_LOAD(ctx, "SLAVE_IRR", pic->slave_irr, loadfailout);
987 V3_CHKPT_LOAD(ctx, "MASTER_ISR", pic->master_isr, loadfailout);
988 V3_CHKPT_LOAD(ctx, "SLAVE_ISR", pic->slave_isr, loadfailout);
990 V3_CHKPT_LOAD(ctx, "MASTER_ELCR", pic->master_elcr, loadfailout);
991 V3_CHKPT_LOAD(ctx, "SLAVE_ELCR", pic->slave_elcr, loadfailout);
992 V3_CHKPT_LOAD(ctx, "MASTER_ELCR_MASK", pic->master_elcr_mask, loadfailout);
993 V3_CHKPT_LOAD(ctx, "SLAVE_ELCR_MASK", pic->slave_elcr_mask, loadfailout);
995 V3_CHKPT_LOAD(ctx, "MASTER_ICW1", pic->master_icw1, loadfailout);
996 V3_CHKPT_LOAD(ctx, "MASTER_ICW2", pic->master_icw2, loadfailout);
997 V3_CHKPT_LOAD(ctx, "MASTER_ICW3", pic->master_icw3, loadfailout);
998 V3_CHKPT_LOAD(ctx, "MASTER_ICW4", pic->master_icw4, loadfailout);
1001 V3_CHKPT_LOAD(ctx, "SLAVE_ICW1", pic->slave_icw1, loadfailout);
1002 V3_CHKPT_LOAD(ctx, "SLAVE_ICW2", pic->slave_icw2, loadfailout);
1003 V3_CHKPT_LOAD(ctx, "SLAVE_ICW3", pic->slave_icw3, loadfailout);
1004 V3_CHKPT_LOAD(ctx, "SLAVE_ICW4", pic->slave_icw4, loadfailout);
1007 V3_CHKPT_LOAD(ctx, "MASTER_IMR", pic->master_imr, loadfailout);
1008 V3_CHKPT_LOAD(ctx, "SLAVE_IMR", pic->slave_imr, loadfailout);
1009 V3_CHKPT_LOAD(ctx, "MASTER_OCW2", pic->master_ocw2, loadfailout);
1010 V3_CHKPT_LOAD(ctx, "MASTER_OCW3", pic->master_ocw3, loadfailout);
1011 V3_CHKPT_LOAD(ctx, "SLAVE_OCW2", pic->slave_ocw2, loadfailout);
1012 V3_CHKPT_LOAD(ctx, "SLAVE_OCW3", pic->slave_ocw3, loadfailout);
1014 V3_CHKPT_LOAD(ctx, "MASTER_STATE", pic->master_state, loadfailout);
1015 V3_CHKPT_LOAD(ctx, "SLAVE_STATE", pic->slave_state, loadfailout);
1020 PrintError(VM_NONE, VCORE_NONE, "Failed to load PIC\n");
1027 static struct v3_device_ops dev_ops = {
1028 .free = (int (*)(void *))pic_free,
1029 #ifdef V3_CONFIG_CHECKPOINT
1039 static int pic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
1040 struct pic_internal * state = NULL;
1041 char * dev_id = v3_cfg_val(cfg, "ID");
1044 // PIC is only usable in non-multicore environments
1045 // just hardcode the core context
1046 struct guest_info * core = &(vm->cores[0]);
1048 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
1051 PrintError(vm, VCORE_NONE, "8259 PIC: Cannot allocate in init\n");
1055 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
1058 PrintError(vm, VCORE_NONE, "8259 PIC: Could not add device %s\n", dev_id);
1065 state->controller_handle = v3_register_intr_controller(core, &intr_ops, state);
1066 state->router_handle = v3_register_intr_router(vm, &router_ops, state);
1068 state->master_irr = 0;
1069 state->master_isr = 0;
1070 state->master_elcr = 0;
1071 state->master_elcr_mask = 0xf8;
1072 state->master_icw1 = 0;
1073 state->master_icw2 = 0;
1074 state->master_icw3 = 0;
1075 state->master_icw4 = 0;
1076 state->master_imr = 0;
1077 state->master_ocw2 = 0;
1078 state->master_ocw3 = 0x02;
1079 state->master_state = ICW1;
1082 state->slave_irr = 0;
1083 state->slave_isr = 0;
1084 state->slave_elcr = 0;
1085 state->slave_elcr_mask = 0xde;
1086 state->slave_icw1 = 0;
1087 state->slave_icw2 = 0;
1088 state->slave_icw3 = 0;
1089 state->slave_icw4 = 0;
1090 state->slave_imr = 0;
1091 state->slave_ocw2 = 0;
1092 state->slave_ocw3 = 0x02;
1093 state->slave_state = ICW1;
1096 ret |= v3_dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
1097 ret |= v3_dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
1098 ret |= v3_dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
1099 ret |= v3_dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
1102 ret |= v3_dev_hook_io(dev, ELCR1_PORT, &read_elcr_port, &write_elcr_port);
1103 ret |= v3_dev_hook_io(dev, ELCR2_PORT, &read_elcr_port, &write_elcr_port);
1106 PrintError(vm, VCORE_NONE, "8259 PIC: Error hooking io ports\n");
1107 v3_remove_device(dev);
1116 device_register("8259A", pic_init);