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>
27 #ifndef CONFIG_DEBUG_PIC
29 #define PrintDebug(fmt, args...)
33 typedef enum {RESET, ICW1, ICW2, ICW3, ICW4, READY} pic_state_t;
35 static const uint_t MASTER_PORT1 = 0x20;
36 static const uint_t MASTER_PORT2 = 0x21;
37 static const uint_t SLAVE_PORT1 = 0xA0;
38 static const uint_t SLAVE_PORT2 = 0xA1;
40 static const uint_t ELCR1_PORT = 0x4d0;
41 static const uint_t ELCR2_PORT = 0x4d1;
44 #define IS_ICW1(x) (((x & 0x10) >> 4) == 0x1)
45 #define IS_OCW2(x) (((x & 0x18) >> 3) == 0x0)
46 #define IS_OCW3(x) (((x & 0x18) >> 3) == 0x1)
50 uint_t ic4 : 1; // ICW4 has to be read
51 uint_t sngl : 1; // single (only one PIC)
52 uint_t adi : 1; // call address interval
53 uint_t ltim : 1; // level interrupt mode
65 // Each bit that is set indicates that the IR input has a slave
77 // The ID is the Slave device ID
84 uint_t uPM : 1; // 1=x86
85 uint_t AEOI : 1; // Automatic End of Interrupt
86 uint_t M_S : 1; // only if buffered 1=master,0=slave
87 uint_t BUF : 1; // buffered mode
88 uint_t SFNM : 1; // special fully nexted mode
106 uint_t cw_code : 2; // should be 00
116 uint_t cw_code : 2; // should be 01
123 struct pic_internal {
134 uchar_t master_elcr_mask;
135 uchar_t slave_elcr_mask;
156 pic_state_t master_state;
157 pic_state_t slave_state;
161 static void DumpPICState(struct pic_internal *p)
164 PrintDebug("8259 PIC: master_state=0x%x\n",p->master_state);
165 PrintDebug("8259 PIC: master_irr=0x%x\n",p->master_irr);
166 PrintDebug("8259 PIC: master_isr=0x%x\n",p->master_isr);
167 PrintDebug("8259 PIC: master_imr=0x%x\n",p->master_imr);
169 PrintDebug("8259 PIC: master_ocw2=0x%x\n",p->master_ocw2);
170 PrintDebug("8259 PIC: master_ocw3=0x%x\n",p->master_ocw3);
172 PrintDebug("8259 PIC: master_icw1=0x%x\n",p->master_icw1);
173 PrintDebug("8259 PIC: master_icw2=0x%x\n",p->master_icw2);
174 PrintDebug("8259 PIC: master_icw3=0x%x\n",p->master_icw3);
175 PrintDebug("8259 PIC: master_icw4=0x%x\n",p->master_icw4);
177 PrintDebug("8259 PIC: slave_state=0x%x\n",p->slave_state);
178 PrintDebug("8259 PIC: slave_irr=0x%x\n",p->slave_irr);
179 PrintDebug("8259 PIC: slave_isr=0x%x\n",p->slave_isr);
180 PrintDebug("8259 PIC: slave_imr=0x%x\n",p->slave_imr);
182 PrintDebug("8259 PIC: slave_ocw2=0x%x\n",p->slave_ocw2);
183 PrintDebug("8259 PIC: slave_ocw3=0x%x\n",p->slave_ocw3);
185 PrintDebug("8259 PIC: slave_icw1=0x%x\n",p->slave_icw1);
186 PrintDebug("8259 PIC: slave_icw2=0x%x\n",p->slave_icw2);
187 PrintDebug("8259 PIC: slave_icw3=0x%x\n",p->slave_icw3);
188 PrintDebug("8259 PIC: slave_icw4=0x%x\n",p->slave_icw4);
193 static int pic_raise_intr(struct v3_vm_info * vm, void * private_data, int irq) {
194 struct pic_internal * state = (struct pic_internal*)private_data;
198 state->master_irr |= 0x04; // PAD
201 PrintDebug("8259 PIC: Raising irq %d in the PIC\n", irq);
204 state->master_irr |= 0x01 << irq;
205 } else if ((irq > 7) && (irq < 16)) {
206 state->slave_irr |= 0x01 << (irq - 8); // PAD if -7 then irq 15=no irq
208 PrintDebug("8259 PIC: Invalid IRQ raised (%d)\n", irq);
212 v3_interrupt_cpu(vm, 0, 0);
218 static int pic_lower_intr(struct v3_vm_info * vm, void * private_data, int irq) {
219 struct pic_internal * state = (struct pic_internal*)private_data;
221 PrintDebug("[pic_lower_intr] IRQ line %d now low\n", irq);
224 state->master_irr &= ~(1 << irq);
225 if ((state->master_irr & ~(state->master_imr)) == 0) {
226 PrintDebug("\t\tFIXME: Master maybe should do sth\n");
228 } else if ((irq > 7) && (irq < 16)) {
230 state->slave_irr &= ~(1 << (irq - 8));
231 if ((state->slave_irr & (~(state->slave_imr))) == 0) {
232 PrintDebug("\t\tFIXME: Slave maybe should do sth\n");
240 static int pic_intr_pending(struct guest_info * info, void * private_data) {
241 struct pic_internal * state = (struct pic_internal*)private_data;
243 if ((state->master_irr & ~(state->master_imr)) ||
244 (state->slave_irr & ~(state->slave_imr))) {
251 static int pic_get_intr_number(struct guest_info * info, void * private_data) {
252 struct pic_internal * state = (struct pic_internal *)private_data;
256 PrintDebug("8259 PIC: getnum: master_irr: 0x%x master_imr: 0x%x\n", state->master_irr, state->master_imr);
257 PrintDebug("8259 PIC: getnum: slave_irr: 0x%x slave_imr: 0x%x\n", state->slave_irr, state->slave_imr);
259 for (i = 0; i < 16; i++) {
261 if (((state->master_irr & ~(state->master_imr)) >> i) & 0x01) {
262 //state->master_isr |= (0x1 << i);
264 //state->master_irr &= ~(0x1 << i);
265 PrintDebug("8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
266 irq = i + state->master_icw2;
270 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) & 0x01) {
271 //state->slave_isr |= (0x1 << (i - 8));
272 //state->slave_irr &= ~(0x1 << (i - 8));
273 PrintDebug("8259 PIC: IRQ: %d, slave_icw2: %x\n", i, state->slave_icw2);
274 irq = (i - 8) + state->slave_icw2;
281 if ((i == 15) || (i == 6)) {
289 PrintDebug("8259 PIC: get num is returning %d\n",irq);
296 /* The IRQ number is the number returned by pic_get_intr_number(), not the pin number */
297 static int pic_begin_irq(struct guest_info * info, void * private_data, int irq) {
298 struct pic_internal * state = (struct pic_internal*)private_data;
300 if ((irq >= state->master_icw2) && (irq <= state->master_icw2 + 7)) {
302 } else if ((irq >= state->slave_icw2) && (irq <= state->slave_icw2 + 7)) {
306 // PrintError("8259 PIC: Could not find IRQ (0x%x) to Begin\n",irq);
311 // This should always be true: See pic_get_intr_number
312 if (((state->master_irr & ~(state->master_imr)) >> irq) & 0x01) {
313 state->master_isr |= (0x1 << irq);
315 if (!(state->master_elcr & (0x1 << irq))) {
316 state->master_irr &= ~(0x1 << irq);
319 PrintDebug("8259 PIC: (master) Ignoring begin_irq for %d since I don't own it\n",irq);
323 // This should always be true: See pic_get_intr_number
324 if (((state->slave_irr & ~(state->slave_imr)) >> (irq - 8)) & 0x01) {
325 state->slave_isr |= (0x1 << (irq - 8));
327 if (!(state->slave_elcr & (0x1 << (irq - 8)))) {
328 state->slave_irr &= ~(0x1 << (irq - 8));
331 PrintDebug("8259 PIC: (slave) Ignoring begin_irq for %d since I don't own it\n",irq);
341 static int pic_end_irq(void * private_data, int irq) {
348 static struct intr_ctrl_ops intr_ops = {
349 .intr_pending = pic_intr_pending,
350 .get_intr_number = pic_get_intr_number,
351 .begin_irq = pic_begin_irq
354 static struct intr_router_ops router_ops = {
355 .raise_intr = pic_raise_intr,
356 .lower_intr = pic_lower_intr
360 static int read_master_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
361 struct pic_internal * state = (struct pic_internal *)priv_data;
364 PrintError("8259 PIC: Invalid Read length (rd_Master1)\n");
368 if ((state->master_ocw3 & 0x03) == 0x02) {
369 *(uchar_t *)dst = state->master_irr;
370 } else if ((state->master_ocw3 & 0x03) == 0x03) {
371 *(uchar_t *)dst = state->master_isr;
379 static int read_master_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
380 struct pic_internal * state = (struct pic_internal *)priv_data;
383 PrintError("8259 PIC: Invalid Read length (rd_Master2)\n");
387 *(uchar_t *)dst = state->master_imr;
393 static int read_slave_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
394 struct pic_internal * state = (struct pic_internal *)priv_data;
397 PrintError("8259 PIC: Invalid Read length (rd_Slave1)\n");
401 if ((state->slave_ocw3 & 0x03) == 0x02) {
402 *(uchar_t*)dst = state->slave_irr;
403 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
404 *(uchar_t *)dst = state->slave_isr;
412 static int read_slave_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
413 struct pic_internal * state = (struct pic_internal *)priv_data;
416 PrintError("8259 PIC: Invalid Read length (rd_Slave2)\n");
420 *(uchar_t *)dst = state->slave_imr;
426 static int write_master_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
427 struct pic_internal * state = (struct pic_internal *)priv_data;
428 uchar_t cw = *(uchar_t *)src;
430 PrintDebug("8259 PIC: Write master port 1 with 0x%x\n",cw);
433 PrintError("8259 PIC: Invalid Write length (wr_Master1)\n");
437 v3_clear_pending_intr(core);
441 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Master1)\n", cw);
443 state->master_icw1 = cw;
444 state->master_state = ICW2;
446 } else if (state->master_state == READY) {
448 // handle the EOI here
449 struct ocw2 * cw2 = (struct ocw2*)&cw;
451 PrintDebug("8259 PIC: Handling OCW2 = %x (wr_Master1)\n", cw);
453 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
455 state->master_isr &= ~(0x01 << cw2->level);
456 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
459 PrintDebug("8259 PIC: Pre ISR = %x (wr_Master1)\n", state->master_isr);
460 for (i = 0; i < 8; i++) {
461 if (state->master_isr & (0x01 << i)) {
462 state->master_isr &= ~(0x01 << i);
466 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
467 } else if (!(cw2->EOI) && (cw2->R) && (cw2->SL)) {
468 PrintDebug("8259 PIC: Ignoring set-priority, priorities not implemented (level=%d, wr_Master1)\n", cw2->level);
469 } else if (!(cw2->EOI) && !(cw2->R) && (cw2->SL)) {
470 PrintDebug("8259 PIC: Ignoring no-op (level=%d, wr_Master1)\n", cw2->level);
472 PrintError("8259 PIC: Command not handled, or in error (wr_Master1)\n");
476 state->master_ocw2 = cw;
477 } else if (IS_OCW3(cw)) {
478 PrintDebug("8259 PIC: Handling OCW3 = %x (wr_Master1)\n", cw);
479 state->master_ocw3 = cw;
481 PrintError("8259 PIC: Invalid OCW to PIC (wr_Master1)\n");
482 PrintError("8259 PIC: CW=%x\n", cw);
486 PrintError("8259 PIC: Invalid PIC State (wr_Master1)\n");
487 PrintError("8259 PIC: CW=%x\n", cw);
494 static int write_master_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
495 struct pic_internal * state = (struct pic_internal *)priv_data;
496 uchar_t cw = *(uchar_t *)src;
498 PrintDebug("8259 PIC: Write master port 2 with 0x%x\n",cw);
501 PrintError("8259 PIC: Invalid Write length (wr_Master2)\n");
505 v3_clear_pending_intr(core);
507 if (state->master_state == ICW2) {
508 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
510 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Master2)\n", cw);
511 state->master_icw2 = cw;
515 if (cw1->sngl == 0) {
516 state->master_state = ICW3;
517 } else if (cw1->ic4 == 1) {
518 state->master_state = ICW4;
520 state->master_state = READY;
525 } else if (state->master_state == ICW3) {
526 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
528 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Master2)\n", cw);
530 state->master_icw3 = cw;
533 state->master_state = ICW4;
535 state->master_state = READY;
538 } else if (state->master_state == ICW4) {
539 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Master2)\n", cw);
540 state->master_icw4 = cw;
541 state->master_state = READY;
542 } else if ((state->master_state == ICW1) || (state->master_state == READY)) {
543 PrintDebug("8259 PIC: Setting IMR = %x (wr_Master2)\n", cw);
544 state->master_imr = cw;
547 PrintError("8259 PIC: Invalid master PIC State (wr_Master2) (state=%d)\n",
548 state->master_state);
555 static int write_slave_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
556 struct pic_internal * state = (struct pic_internal *)priv_data;
557 uchar_t cw = *(uchar_t *)src;
559 PrintDebug("8259 PIC: Write slave port 1 with 0x%x\n",cw);
563 PrintError("8259 PIC: Invalid Write length (wr_Slave1)\n");
567 v3_clear_pending_intr(core);
570 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Slave1)\n", cw);
571 state->slave_icw1 = cw;
572 state->slave_state = ICW2;
573 } else if (state->slave_state == READY) {
575 // handle the EOI here
576 struct ocw2 * cw2 = (struct ocw2 *)&cw;
578 PrintDebug("8259 PIC: Setting OCW2 = %x (wr_Slave1)\n", cw);
580 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
582 state->slave_isr &= ~(0x01 << cw2->level);
583 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
586 PrintDebug("8259 PIC: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
587 for (i = 0; i < 8; i++) {
588 if (state->slave_isr & (0x01 << i)) {
589 state->slave_isr &= ~(0x01 << i);
593 PrintDebug("8259 PIC: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
595 PrintError("8259 PIC: Command not handled or invalid (wr_Slave1)\n");
599 state->slave_ocw2 = cw;
600 } else if (IS_OCW3(cw)) {
601 // Basically sets the IRR/ISR read flag
602 PrintDebug("8259 PIC: Setting OCW3 = %x (wr_Slave1)\n", cw);
603 state->slave_ocw3 = cw;
605 PrintError("8259 PIC: Invalid command work (wr_Slave1)\n");
609 PrintError("8259 PIC: Invalid State writing (wr_Slave1)\n");
616 static int write_slave_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
617 struct pic_internal * state = (struct pic_internal *)priv_data;
618 uchar_t cw = *(uchar_t *)src;
620 PrintDebug("8259 PIC: Write slave port 2 with 0x%x\n",cw);
623 PrintError("8259 PIC: Invalid write length (wr_Slave2)\n");
627 v3_clear_pending_intr(core);
630 if (state->slave_state == ICW2) {
631 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
633 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Slave2)\n", cw);
635 state->slave_icw2 = cw;
637 if (cw1->sngl == 0) {
638 state->slave_state = ICW3;
639 } else if (cw1->ic4 == 1) {
640 state->slave_state = ICW4;
642 state->slave_state = READY;
645 } else if (state->slave_state == ICW3) {
646 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
648 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Slave2)\n", cw);
650 state->slave_icw3 = cw;
653 state->slave_state = ICW4;
655 state->slave_state = READY;
658 } else if (state->slave_state == ICW4) {
659 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Slave2)\n", cw);
660 state->slave_icw4 = cw;
661 state->slave_state = READY;
662 } else if ((state->slave_state == ICW1) || (state->slave_state == READY)) {
663 PrintDebug("8259 PIC: Setting IMR = %x (wr_Slave2)\n", cw);
664 state->slave_imr = cw;
666 PrintError("8259 PIC: Invalid State at write (wr_Slave2)\n");
676 static int read_elcr_port(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
677 struct pic_internal * state = (struct pic_internal *)priv_data;
680 PrintError("ELCR read of invalid length %d\n", length);
684 if (port == ELCR1_PORT) {
686 *(uint8_t *)dst = state->master_elcr;
687 } else if (port == ELCR2_PORT) {
688 *(uint8_t *)dst = state->slave_elcr;
690 PrintError("Invalid port %x\n", port);
698 static int write_elcr_port(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
699 struct pic_internal * state = (struct pic_internal *)priv_data;
702 PrintError("ELCR read of invalid length %d\n", length);
706 if (port == ELCR1_PORT) {
708 state->master_elcr = (*(uint8_t *)src) & state->master_elcr_mask;
709 } else if (port == ELCR2_PORT) {
710 state->slave_elcr = (*(uint8_t *)src) & state->slave_elcr_mask;
712 PrintError("Invalid port %x\n", port);
724 static int pic_free(struct vm_device * dev) {
735 static struct v3_device_ops dev_ops = {
742 #include <palacios/vm_guest.h>
744 static int pic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
745 struct pic_internal * state = NULL;
746 char * dev_id = v3_cfg_val(cfg, "ID");
749 // PIC is only usable in non-multicore environments
750 // just hardcode the core context
751 struct guest_info * core = &(vm->cores[0]);
753 V3_ASSERT(state != NULL);
755 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
758 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
761 PrintError("Could not add device %s\n", dev_id);
767 v3_register_intr_controller(core, &intr_ops, state);
768 v3_register_intr_router(vm, &router_ops, state);
770 state->master_irr = 0;
771 state->master_isr = 0;
772 state->master_elcr = 0;
773 state->master_elcr_mask = 0xf8;
774 state->master_icw1 = 0;
775 state->master_icw2 = 0;
776 state->master_icw3 = 0;
777 state->master_icw4 = 0;
778 state->master_imr = 0;
779 state->master_ocw2 = 0;
780 state->master_ocw3 = 0x02;
781 state->master_state = ICW1;
784 state->slave_irr = 0;
785 state->slave_isr = 0;
786 state->slave_elcr = 0;
787 state->slave_elcr_mask = 0xde;
788 state->slave_icw1 = 0;
789 state->slave_icw2 = 0;
790 state->slave_icw3 = 0;
791 state->slave_icw4 = 0;
792 state->slave_imr = 0;
793 state->slave_ocw2 = 0;
794 state->slave_ocw3 = 0x02;
795 state->slave_state = ICW1;
798 ret |= v3_dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
799 ret |= v3_dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
800 ret |= v3_dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
801 ret |= v3_dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
804 ret |= v3_dev_hook_io(dev, ELCR1_PORT, &read_elcr_port, &write_elcr_port);
805 ret |= v3_dev_hook_io(dev, ELCR2_PORT, &read_elcr_port, &write_elcr_port);
808 PrintError("Error hooking io ports\n");
809 v3_remove_device(dev);
818 device_register("8259A", pic_init);