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 void * router_handle;
164 void * controller_handle;
168 static void DumpPICState(struct pic_internal *p)
171 PrintDebug("8259 PIC: master_state=0x%x\n",p->master_state);
172 PrintDebug("8259 PIC: master_irr=0x%x\n",p->master_irr);
173 PrintDebug("8259 PIC: master_isr=0x%x\n",p->master_isr);
174 PrintDebug("8259 PIC: master_imr=0x%x\n",p->master_imr);
176 PrintDebug("8259 PIC: master_ocw2=0x%x\n",p->master_ocw2);
177 PrintDebug("8259 PIC: master_ocw3=0x%x\n",p->master_ocw3);
179 PrintDebug("8259 PIC: master_icw1=0x%x\n",p->master_icw1);
180 PrintDebug("8259 PIC: master_icw2=0x%x\n",p->master_icw2);
181 PrintDebug("8259 PIC: master_icw3=0x%x\n",p->master_icw3);
182 PrintDebug("8259 PIC: master_icw4=0x%x\n",p->master_icw4);
184 PrintDebug("8259 PIC: slave_state=0x%x\n",p->slave_state);
185 PrintDebug("8259 PIC: slave_irr=0x%x\n",p->slave_irr);
186 PrintDebug("8259 PIC: slave_isr=0x%x\n",p->slave_isr);
187 PrintDebug("8259 PIC: slave_imr=0x%x\n",p->slave_imr);
189 PrintDebug("8259 PIC: slave_ocw2=0x%x\n",p->slave_ocw2);
190 PrintDebug("8259 PIC: slave_ocw3=0x%x\n",p->slave_ocw3);
192 PrintDebug("8259 PIC: slave_icw1=0x%x\n",p->slave_icw1);
193 PrintDebug("8259 PIC: slave_icw2=0x%x\n",p->slave_icw2);
194 PrintDebug("8259 PIC: slave_icw3=0x%x\n",p->slave_icw3);
195 PrintDebug("8259 PIC: slave_icw4=0x%x\n",p->slave_icw4);
200 static int pic_raise_intr(struct v3_vm_info * vm, void * private_data, int irq) {
201 struct pic_internal * state = (struct pic_internal*)private_data;
205 state->master_irr |= 0x04; // PAD
208 PrintDebug("8259 PIC: Raising irq %d in the PIC\n", irq);
211 state->master_irr |= 0x01 << irq;
212 } else if ((irq > 7) && (irq < 16)) {
213 state->slave_irr |= 0x01 << (irq - 8); // PAD if -7 then irq 15=no irq
215 PrintDebug("8259 PIC: Invalid IRQ raised (%d)\n", irq);
219 #ifdef V3_CONFIG_MULTITHREAD_OS
220 v3_interrupt_cpu(vm, 0, 0);
227 static int pic_lower_intr(struct v3_vm_info * vm, void * private_data, int irq) {
228 struct pic_internal * state = (struct pic_internal*)private_data;
230 PrintDebug("[pic_lower_intr] IRQ line %d now low\n", irq);
233 state->master_irr &= ~(1 << irq);
234 if ((state->master_irr & ~(state->master_imr)) == 0) {
235 PrintDebug("\t\tFIXME: Master maybe should do sth\n");
237 } else if ((irq > 7) && (irq < 16)) {
239 state->slave_irr &= ~(1 << (irq - 8));
240 if ((state->slave_irr & (~(state->slave_imr))) == 0) {
241 PrintDebug("\t\tFIXME: Slave maybe should do sth\n");
249 static int pic_intr_pending(struct guest_info * info, void * private_data) {
250 struct pic_internal * state = (struct pic_internal*)private_data;
252 if ((state->master_irr & ~(state->master_imr)) ||
253 (state->slave_irr & ~(state->slave_imr))) {
260 static int pic_get_intr_number(struct guest_info * info, void * private_data) {
261 struct pic_internal * state = (struct pic_internal *)private_data;
265 PrintDebug("8259 PIC: getnum: master_irr: 0x%x master_imr: 0x%x\n", state->master_irr, state->master_imr);
266 PrintDebug("8259 PIC: getnum: slave_irr: 0x%x slave_imr: 0x%x\n", state->slave_irr, state->slave_imr);
268 for (i = 0; i < 16; i++) {
270 if (((state->master_irr & ~(state->master_imr)) >> i) & 0x01) {
271 //state->master_isr |= (0x1 << i);
273 //state->master_irr &= ~(0x1 << i);
274 PrintDebug("8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
275 irq = i + state->master_icw2;
279 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) & 0x01) {
280 //state->slave_isr |= (0x1 << (i - 8));
281 //state->slave_irr &= ~(0x1 << (i - 8));
282 PrintDebug("8259 PIC: IRQ: %d, slave_icw2: %x\n", i, state->slave_icw2);
283 irq = (i - 8) + state->slave_icw2;
290 if ((i == 15) || (i == 6)) {
298 PrintDebug("8259 PIC: get num is returning %d\n",irq);
305 /* The IRQ number is the number returned by pic_get_intr_number(), not the pin number */
306 static int pic_begin_irq(struct guest_info * info, void * private_data, int irq) {
307 struct pic_internal * state = (struct pic_internal*)private_data;
309 if ((irq >= state->master_icw2) && (irq <= state->master_icw2 + 7)) {
311 } else if ((irq >= state->slave_icw2) && (irq <= state->slave_icw2 + 7)) {
315 // PrintError("8259 PIC: Could not find IRQ (0x%x) to Begin\n",irq);
320 // This should always be true: See pic_get_intr_number
321 if (((state->master_irr & ~(state->master_imr)) >> irq) & 0x01) {
322 state->master_isr |= (0x1 << irq);
324 if (!(state->master_elcr & (0x1 << irq))) {
325 state->master_irr &= ~(0x1 << irq);
328 PrintDebug("8259 PIC: (master) Ignoring begin_irq for %d since I don't own it\n",irq);
332 // This should always be true: See pic_get_intr_number
333 if (((state->slave_irr & ~(state->slave_imr)) >> (irq - 8)) & 0x01) {
334 state->slave_isr |= (0x1 << (irq - 8));
336 if (!(state->slave_elcr & (0x1 << (irq - 8)))) {
337 state->slave_irr &= ~(0x1 << (irq - 8));
340 PrintDebug("8259 PIC: (slave) Ignoring begin_irq for %d since I don't own it\n",irq);
350 static int pic_end_irq(void * private_data, int irq) {
357 static struct intr_ctrl_ops intr_ops = {
358 .intr_pending = pic_intr_pending,
359 .get_intr_number = pic_get_intr_number,
360 .begin_irq = pic_begin_irq
363 static struct intr_router_ops router_ops = {
364 .raise_intr = pic_raise_intr,
365 .lower_intr = pic_lower_intr
369 static int read_master_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
370 struct pic_internal * state = (struct pic_internal *)priv_data;
373 PrintError("8259 PIC: Invalid Read length (rd_Master1)\n");
377 if ((state->master_ocw3 & 0x03) == 0x02) {
378 *(uint8_t *)dst = state->master_irr;
379 } else if ((state->master_ocw3 & 0x03) == 0x03) {
380 *(uint8_t *)dst = state->master_isr;
388 static int read_master_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
389 struct pic_internal * state = (struct pic_internal *)priv_data;
392 PrintError("8259 PIC: Invalid Read length (rd_Master2)\n");
396 *(uint8_t *)dst = state->master_imr;
402 static int read_slave_port1(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
403 struct pic_internal * state = (struct pic_internal *)priv_data;
406 PrintError("8259 PIC: Invalid Read length (rd_Slave1)\n");
410 if ((state->slave_ocw3 & 0x03) == 0x02) {
411 *(uint8_t*)dst = state->slave_irr;
412 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
413 *(uint8_t *)dst = state->slave_isr;
421 static int read_slave_port2(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
422 struct pic_internal * state = (struct pic_internal *)priv_data;
425 PrintError("8259 PIC: Invalid Read length (rd_Slave2)\n");
429 *(uint8_t *)dst = state->slave_imr;
435 static int write_master_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
436 struct pic_internal * state = (struct pic_internal *)priv_data;
437 uint8_t cw = *(uint8_t *)src;
439 PrintDebug("8259 PIC: Write master port 1 with 0x%x\n",cw);
442 PrintError("8259 PIC: Invalid Write length (wr_Master1)\n");
446 v3_clear_pending_intr(core);
450 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Master1)\n", cw);
452 state->master_icw1 = cw;
453 state->master_state = ICW2;
455 } else if (state->master_state == READY) {
457 // handle the EOI here
458 struct ocw2 * cw2 = (struct ocw2*)&cw;
460 PrintDebug("8259 PIC: Handling OCW2 = %x (wr_Master1)\n", cw);
462 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
464 state->master_isr &= ~(0x01 << cw2->level);
465 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
468 PrintDebug("8259 PIC: Pre ISR = %x (wr_Master1)\n", state->master_isr);
469 for (i = 0; i < 8; i++) {
470 if (state->master_isr & (0x01 << i)) {
471 state->master_isr &= ~(0x01 << i);
475 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
476 } else if (!(cw2->EOI) && (cw2->R) && (cw2->SL)) {
477 PrintDebug("8259 PIC: Ignoring set-priority, priorities not implemented (level=%d, wr_Master1)\n", cw2->level);
478 } else if (!(cw2->EOI) && !(cw2->R) && (cw2->SL)) {
479 PrintDebug("8259 PIC: Ignoring no-op (level=%d, wr_Master1)\n", cw2->level);
481 PrintError("8259 PIC: Command not handled, or in error (wr_Master1)\n");
485 state->master_ocw2 = cw;
486 } else if (IS_OCW3(cw)) {
487 PrintDebug("8259 PIC: Handling OCW3 = %x (wr_Master1)\n", cw);
488 state->master_ocw3 = cw;
490 PrintError("8259 PIC: Invalid OCW to PIC (wr_Master1)\n");
491 PrintError("8259 PIC: CW=%x\n", cw);
495 PrintError("8259 PIC: Invalid PIC State (wr_Master1)\n");
496 PrintError("8259 PIC: CW=%x\n", cw);
503 static int write_master_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
504 struct pic_internal * state = (struct pic_internal *)priv_data;
505 uint8_t cw = *(uint8_t *)src;
507 PrintDebug("8259 PIC: Write master port 2 with 0x%x\n",cw);
510 PrintError("8259 PIC: Invalid Write length (wr_Master2)\n");
514 v3_clear_pending_intr(core);
516 if (state->master_state == ICW2) {
517 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
519 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Master2)\n", cw);
520 state->master_icw2 = cw;
524 if (cw1->sngl == 0) {
525 state->master_state = ICW3;
526 } else if (cw1->ic4 == 1) {
527 state->master_state = ICW4;
529 state->master_state = READY;
534 } else if (state->master_state == ICW3) {
535 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
537 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Master2)\n", cw);
539 state->master_icw3 = cw;
542 state->master_state = ICW4;
544 state->master_state = READY;
547 } else if (state->master_state == ICW4) {
548 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Master2)\n", cw);
549 state->master_icw4 = cw;
550 state->master_state = READY;
551 } else if ((state->master_state == ICW1) || (state->master_state == READY)) {
552 PrintDebug("8259 PIC: Setting IMR = %x (wr_Master2)\n", cw);
553 state->master_imr = cw;
556 PrintError("8259 PIC: Invalid master PIC State (wr_Master2) (state=%d)\n",
557 state->master_state);
564 static int write_slave_port1(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
565 struct pic_internal * state = (struct pic_internal *)priv_data;
566 uint8_t cw = *(uint8_t *)src;
568 PrintDebug("8259 PIC: Write slave port 1 with 0x%x\n",cw);
572 PrintError("8259 PIC: Invalid Write length (wr_Slave1)\n");
576 v3_clear_pending_intr(core);
579 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Slave1)\n", cw);
580 state->slave_icw1 = cw;
581 state->slave_state = ICW2;
582 } else if (state->slave_state == READY) {
584 // handle the EOI here
585 struct ocw2 * cw2 = (struct ocw2 *)&cw;
587 PrintDebug("8259 PIC: Setting OCW2 = %x (wr_Slave1)\n", cw);
589 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
591 state->slave_isr &= ~(0x01 << cw2->level);
592 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
595 PrintDebug("8259 PIC: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
596 for (i = 0; i < 8; i++) {
597 if (state->slave_isr & (0x01 << i)) {
598 state->slave_isr &= ~(0x01 << i);
602 PrintDebug("8259 PIC: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
604 PrintError("8259 PIC: Command not handled or invalid (wr_Slave1)\n");
608 state->slave_ocw2 = cw;
609 } else if (IS_OCW3(cw)) {
610 // Basically sets the IRR/ISR read flag
611 PrintDebug("8259 PIC: Setting OCW3 = %x (wr_Slave1)\n", cw);
612 state->slave_ocw3 = cw;
614 PrintError("8259 PIC: Invalid command work (wr_Slave1)\n");
618 PrintError("8259 PIC: Invalid State writing (wr_Slave1)\n");
625 static int write_slave_port2(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
626 struct pic_internal * state = (struct pic_internal *)priv_data;
627 uint8_t cw = *(uint8_t *)src;
629 PrintDebug("8259 PIC: Write slave port 2 with 0x%x\n",cw);
632 PrintError("8259 PIC: Invalid write length (wr_Slave2)\n");
636 v3_clear_pending_intr(core);
639 if (state->slave_state == ICW2) {
640 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
642 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Slave2)\n", cw);
644 state->slave_icw2 = cw;
646 if (cw1->sngl == 0) {
647 state->slave_state = ICW3;
648 } else if (cw1->ic4 == 1) {
649 state->slave_state = ICW4;
651 state->slave_state = READY;
654 } else if (state->slave_state == ICW3) {
655 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
657 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Slave2)\n", cw);
659 state->slave_icw3 = cw;
662 state->slave_state = ICW4;
664 state->slave_state = READY;
667 } else if (state->slave_state == ICW4) {
668 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Slave2)\n", cw);
669 state->slave_icw4 = cw;
670 state->slave_state = READY;
671 } else if ((state->slave_state == ICW1) || (state->slave_state == READY)) {
672 PrintDebug("8259 PIC: Setting IMR = %x (wr_Slave2)\n", cw);
673 state->slave_imr = cw;
675 PrintError("8259 PIC: Invalid State at write (wr_Slave2)\n");
685 static int read_elcr_port(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
686 struct pic_internal * state = (struct pic_internal *)priv_data;
689 PrintError("ELCR read of invalid length %d\n", length);
693 if (port == ELCR1_PORT) {
695 *(uint8_t *)dst = state->master_elcr;
696 } else if (port == ELCR2_PORT) {
697 *(uint8_t *)dst = state->slave_elcr;
699 PrintError("Invalid port %x\n", port);
707 static int write_elcr_port(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
708 struct pic_internal * state = (struct pic_internal *)priv_data;
711 PrintError("ELCR read of invalid length %d\n", length);
715 if (port == ELCR1_PORT) {
717 state->master_elcr = (*(uint8_t *)src) & state->master_elcr_mask;
718 } else if (port == ELCR2_PORT) {
719 state->slave_elcr = (*(uint8_t *)src) & state->slave_elcr_mask;
721 PrintError("Invalid port %x\n", port);
730 static int pic_free(struct pic_internal * state) {
731 struct guest_info * core = state->core;
733 v3_remove_intr_controller(core, state->controller_handle);
734 v3_remove_intr_router(core->vm_info, state->router_handle);
740 #ifdef V3_CONFIG_CHECKPOINT
741 static int pic_save(struct v3_chkpt_ctx * ctx, void * private_data) {
742 struct pic_internal * pic = (struct pic_internal *)private_data;
744 v3_chkpt_save_8(ctx, "MASTER_IRR", &(pic->master_irr));
745 v3_chkpt_save_8(ctx, "SLAVE_IRR", &(pic->slave_irr));
747 v3_chkpt_save_8(ctx, "MASTER_ISR", &(pic->master_isr));
748 v3_chkpt_save_8(ctx, "SLAVE_ISR", &(pic->slave_isr));
750 v3_chkpt_save_8(ctx, "MASTER_ELCR", &(pic->master_elcr));
751 v3_chkpt_save_8(ctx, "SLAVE_ELCR", &(pic->slave_elcr));
752 v3_chkpt_save_8(ctx, "MASTER_ELCR_MASK", &(pic->master_elcr_mask));
753 v3_chkpt_save_8(ctx, "SLAVE_ELCR_MASK", &(pic->slave_elcr_mask));
755 v3_chkpt_save_8(ctx, "MASTER_ICW1", &(pic->master_icw1));
756 v3_chkpt_save_8(ctx, "MASTER_ICW2", &(pic->master_icw2));
757 v3_chkpt_save_8(ctx, "MASTER_ICW3", &(pic->master_icw3));
758 v3_chkpt_save_8(ctx, "MASTER_ICW4", &(pic->master_icw4));
761 v3_chkpt_save_8(ctx, "SLAVE_ICW1", &(pic->slave_icw1));
762 v3_chkpt_save_8(ctx, "SLAVE_ICW2", &(pic->slave_icw2));
763 v3_chkpt_save_8(ctx, "SLAVE_ICW3", &(pic->slave_icw3));
764 v3_chkpt_save_8(ctx, "SLAVE_ICW4", &(pic->slave_icw4));
767 v3_chkpt_save_8(ctx, "MASTER_IMR", &(pic->master_imr));
768 v3_chkpt_save_8(ctx, "SLAVE_IMR", &(pic->slave_imr));
769 v3_chkpt_save_8(ctx, "MASTER_OCW2", &(pic->master_ocw2));
770 v3_chkpt_save_8(ctx, "MASTER_OCW3", &(pic->master_ocw3));
771 v3_chkpt_save_8(ctx, "SLAVE_OCW2", &(pic->slave_ocw2));
772 v3_chkpt_save_8(ctx, "SLAVE_OCW3", &(pic->slave_ocw3));
774 v3_chkpt_save_8(ctx, "MASTER_STATE", &(pic->master_state));
775 v3_chkpt_save_8(ctx, "SLAVE_STATE", &(pic->slave_state));
782 static int pic_load(struct v3_chkpt_ctx * ctx, void * private_data) {
783 struct pic_internal * pic = (struct pic_internal *)private_data;
786 v3_chkpt_load_8(ctx, "MASTER_IRR", &(pic->master_irr));
787 v3_chkpt_load_8(ctx, "SLAVE_IRR", &(pic->slave_irr));
789 v3_chkpt_load_8(ctx, "MASTER_ISR", &(pic->master_isr));
790 v3_chkpt_load_8(ctx, "SLAVE_ISR", &(pic->slave_isr));
792 v3_chkpt_load_8(ctx, "MASTER_ELCR", &(pic->master_elcr));
793 v3_chkpt_load_8(ctx, "SLAVE_ELCR", &(pic->slave_elcr));
794 v3_chkpt_load_8(ctx, "MASTER_ELCR_MASK", &(pic->master_elcr_mask));
795 v3_chkpt_load_8(ctx, "SLAVE_ELCR_MASK", &(pic->slave_elcr_mask));
797 v3_chkpt_load_8(ctx, "MASTER_ICW1", &(pic->master_icw1));
798 v3_chkpt_load_8(ctx, "MASTER_ICW2", &(pic->master_icw2));
799 v3_chkpt_load_8(ctx, "MASTER_ICW3", &(pic->master_icw3));
800 v3_chkpt_load_8(ctx, "MASTER_ICW4", &(pic->master_icw4));
803 v3_chkpt_load_8(ctx, "SLAVE_ICW1", &(pic->slave_icw1));
804 v3_chkpt_load_8(ctx, "SLAVE_ICW2", &(pic->slave_icw2));
805 v3_chkpt_load_8(ctx, "SLAVE_ICW3", &(pic->slave_icw3));
806 v3_chkpt_load_8(ctx, "SLAVE_ICW4", &(pic->slave_icw4));
809 v3_chkpt_load_8(ctx, "MASTER_IMR", &(pic->master_imr));
810 v3_chkpt_load_8(ctx, "SLAVE_IMR", &(pic->slave_imr));
811 v3_chkpt_load_8(ctx, "MASTER_OCW2", &(pic->master_ocw2));
812 v3_chkpt_load_8(ctx, "MASTER_OCW3", &(pic->master_ocw3));
813 v3_chkpt_load_8(ctx, "SLAVE_OCW2", &(pic->slave_ocw2));
814 v3_chkpt_load_8(ctx, "SLAVE_OCW3", &(pic->slave_ocw3));
816 v3_chkpt_load_8(ctx, "MASTER_STATE", &(pic->master_state));
817 v3_chkpt_load_8(ctx, "SLAVE_STATE", &(pic->slave_state));
825 static struct v3_device_ops dev_ops = {
826 .free = (int (*)(void *))pic_free,
827 #ifdef V3_CONFIG_CHECKPOINT
837 static int pic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
838 struct pic_internal * state = NULL;
839 char * dev_id = v3_cfg_val(cfg, "ID");
842 // PIC is only usable in non-multicore environments
843 // just hardcode the core context
844 struct guest_info * core = &(vm->cores[0]);
846 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
848 V3_ASSERT(state != NULL);
850 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
853 PrintError("Could not add device %s\n", dev_id);
860 state->controller_handle = v3_register_intr_controller(core, &intr_ops, state);
861 state->router_handle = v3_register_intr_router(vm, &router_ops, state);
863 state->master_irr = 0;
864 state->master_isr = 0;
865 state->master_elcr = 0;
866 state->master_elcr_mask = 0xf8;
867 state->master_icw1 = 0;
868 state->master_icw2 = 0;
869 state->master_icw3 = 0;
870 state->master_icw4 = 0;
871 state->master_imr = 0;
872 state->master_ocw2 = 0;
873 state->master_ocw3 = 0x02;
874 state->master_state = ICW1;
877 state->slave_irr = 0;
878 state->slave_isr = 0;
879 state->slave_elcr = 0;
880 state->slave_elcr_mask = 0xde;
881 state->slave_icw1 = 0;
882 state->slave_icw2 = 0;
883 state->slave_icw3 = 0;
884 state->slave_icw4 = 0;
885 state->slave_imr = 0;
886 state->slave_ocw2 = 0;
887 state->slave_ocw3 = 0x02;
888 state->slave_state = ICW1;
891 ret |= v3_dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
892 ret |= v3_dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
893 ret |= v3_dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
894 ret |= v3_dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
897 ret |= v3_dev_hook_io(dev, ELCR1_PORT, &read_elcr_port, &write_elcr_port);
898 ret |= v3_dev_hook_io(dev, ELCR2_PORT, &read_elcr_port, &write_elcr_port);
901 PrintError("Error hooking io ports\n");
902 v3_remove_device(dev);
911 device_register("8259A", pic_init);