Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Added ICC bus/apic stuff.
[palacios.git] / palacios / src / devices / apic.c
index 407d3af..f45e7b8 100644 (file)
 #include <devices/apic_regs.h>
 #include <palacios/vmm.h>
 #include <palacios/vmm_msr.h>
+#include <palacios/vmm_sprintf.h>
+#include <palacios/vm_guest.h>
 
-
-#ifndef DEBUG_APIC
+#ifndef CONFIG_DEBUG_APIC
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
@@ -180,9 +181,14 @@ struct apic_state {
   
     uint32_t eoi;
 
+    uint32_t my_apic_id;
+    struct vm_device *icc_bus;
 
+    v3_lock_t  lock;
 };
 
+static int apic_read(addr_t guest_addr, void * dst, uint_t length, void * priv_data);
+static int apic_write(addr_t guest_addr, void * src, uint_t length, void * priv_data);
 
 static void init_apic_state(struct apic_state * apic) {
     apic->base_addr = DEFAULT_BASE_ADDR;
@@ -228,6 +234,8 @@ static void init_apic_state(struct apic_state * apic) {
     apic->ext_apic_feature.val = 0x00040007;
     apic->ext_apic_ctrl.val = 0x00000000;
     apic->spec_eoi.val = 0x00000000;
+
+    v3_lock_init(&(apic->lock));
 }
 
 
@@ -236,19 +244,39 @@ static void init_apic_state(struct apic_state * apic) {
 static int read_apic_msr(uint_t msr, v3_msr_t * dst, void * priv_data) {
     struct vm_device * dev = (struct vm_device *)priv_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
-    PrintError("READING APIC BASE ADDR: HI=%x LO=%x\n", apic->base_addr_msr.hi, apic->base_addr_msr.lo);
-
-    return -1;
+    v3_lock(apic->lock);
+    dst->value = apic->base_addr;
+    v3_unlock(apic->lock);
+    return 0;
 }
 
 
 static int write_apic_msr(uint_t msr, v3_msr_t src, void * priv_data) {
-    //  struct vm_device * dev = (struct vm_device *)priv_data;
-    //  struct apic_state * apic = (struct apic_state *)dev->private_data;
+    struct vm_device * dev = (struct vm_device *)priv_data;
+    struct apic_state * apic = (struct apic_state *)dev->private_data;
+    struct v3_shadow_region * old_reg = v3_get_shadow_region(dev->vm, apic->base_addr);
 
-    PrintError("WRITING APIC BASE ADDR: HI=%x LO=%x\n", src.hi, src.lo);
 
-    return -1;
+    if (old_reg == NULL) {
+       // uh oh...
+       PrintError("APIC Base address region does not exit...\n");
+       return -1;
+    }
+    
+    v3_lock(apic->lock);
+
+    v3_delete_shadow_region(dev->vm, old_reg);
+
+    apic->base_addr = src.value;
+
+    if (v3_hook_full_mem(dev->vm, apic->base_addr, apic->base_addr + PAGE_SIZE_4KB, apic_read, apic_write, dev) == -1) {
+       PrintError("Could not hook new APIC Base address\n");
+       v3_unlock(apic->lock);
+       return -1;
+    }
+
+    v3_unlock(apic->lock);
+    return 0;
 }
 
 
@@ -267,6 +295,10 @@ static int activate_apic_irq(struct apic_state * apic, uint32_t irq_num) {
 
     PrintDebug("Raising APIC IRQ %d\n", irq_num);
 
+    if (*req_location & flag) {
+       //V3_Print("Interrupts coallescing\n");
+    }
+
     if (*en_location & flag) {
        *req_location |= flag;
     } else {
@@ -290,8 +322,6 @@ static int get_highest_isr(struct apic_state * apic) {
            for (j = 7; j >= 0; j--) {
                uchar_t flag = 0x1 << j;
                if ((*svc_major) & flag) {
-
-
                    return ((i * 8) + j);
                }
            }
@@ -314,8 +344,6 @@ static int get_highest_irr(struct apic_state * apic) {
            for (j = 7; j >= 0; j--) {
                uchar_t flag = 0x1 << j;
                if ((*req_major) & flag) {
-
-
                    return ((i * 8) + j);
                }
            }
@@ -337,11 +365,11 @@ static int apic_do_eoi(struct apic_state * apic) {
        uchar_t flag = 0x1 << minor_offset;
        uchar_t * svc_location = apic->int_svc_reg + major_offset;
        
-       PrintDebug("Received APIC EOI\n");
+       PrintDebug("Received APIC EOI for IRQ %d\n", isr_irq);
        
        *svc_location &= ~flag;
 
-#ifdef CRAY_XT
+#ifdef CONFIG_CRAY_XT
        
        if ((isr_irq == 238) || 
            (isr_irq == 239)) {
@@ -353,7 +381,7 @@ static int apic_do_eoi(struct apic_state * apic) {
        }
 #endif
     } else {
-       PrintError("Spurious EOI...\n");
+       //PrintError("Spurious EOI...\n");
     }
        
     return 0;
@@ -674,6 +702,9 @@ static int apic_read(addr_t guest_addr, void * dst, uint_t length, void * priv_d
 }
 
 
+/**
+ *
+ */
 static int apic_write(addr_t guest_addr, void * src, uint_t length, void * priv_data) {
     struct vm_device * dev = (struct vm_device *)priv_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
@@ -829,7 +860,12 @@ static int apic_write(addr_t guest_addr, void * src, uint_t length, void * priv_
            break;
 
        case INT_CMD_LO_OFFSET:
+           apic->int_cmd.lo = op_val;
+        v3_icc_send_ipi(apic->icc_bus, apic->int_cmd.dst, apic->int_cmd.val);
+           break;
        case INT_CMD_HI_OFFSET:
+           apic->int_cmd.hi = op_val;
+           break;
            // Unhandled Registers
 
        case EXT_APIC_CMD_OFFSET:
@@ -849,7 +885,7 @@ static int apic_write(addr_t guest_addr, void * src, uint_t length, void * priv_
 /* Interrupt Controller Functions */
 
 // returns 1 if an interrupt is pending, 0 otherwise
-static int apic_intr_pending(void * private_data) {
+static int apic_intr_pending(struct guest_info * info, void * private_data) {
     struct vm_device * dev = (struct vm_device *)private_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
     int req_irq = get_highest_irr(apic);
@@ -863,7 +899,7 @@ static int apic_intr_pending(void * private_data) {
     return 0;
 }
 
-static int apic_get_intr_number(void * private_data) {
+static int apic_get_intr_number(struct guest_info * info, void * private_data) {
     struct vm_device * dev = (struct vm_device *)private_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
     int req_irq = get_highest_irr(apic);
@@ -878,8 +914,9 @@ static int apic_get_intr_number(void * private_data) {
     return -1;
 }
 
-static int apic_raise_intr(void * private_data, int irq) {
-#ifdef CRAY_XT
+#if 0
+static int apic_raise_intr(struct guest_info * info, void * private_data, int irq) {
+#ifdef CONFIG_CRAY_XT
     // The Seastar is connected directly to the LAPIC via LINT0 on the ICC bus
 
     if (irq == 238) {
@@ -889,14 +926,16 @@ static int apic_raise_intr(void * private_data, int irq) {
        return activate_apic_irq(apic, irq);
     }
 #endif
+
     return 0;
 }
 
-static int apic_lower_intr(void * private_data, int irq) {
+static int apic_lower_intr(struct guest_info * info, void * private_data, int irq) {
     return 0;
 }
+#endif
 
-static int apic_begin_irq(void * private_data, int irq) {
+static int apic_begin_irq(struct guest_info * info, void * private_data, int irq) {
     struct vm_device * dev = (struct vm_device *)private_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
     int major_offset = (irq & ~0x00000007) >> 3;
@@ -908,7 +947,7 @@ static int apic_begin_irq(void * private_data, int irq) {
     *svc_location |= flag;
     *req_location &= ~flag;
 
-#ifdef CRAY_XT
+#ifdef CONFIG_CRAY_XT
     if ((irq == 238) || (irq == 239)) {
        PrintError("APIC: Begin IRQ %d (ISR=%x), (IRR=%x)\n", irq, *svc_location, *req_location);
     }
@@ -919,15 +958,28 @@ static int apic_begin_irq(void * private_data, int irq) {
 
 
 
-int v3_apic_raise_intr(struct vm_device * apic_dev, int intr_num) {
+int v3_apic_raise_intr(struct guest_info * info, struct vm_device * apic_dev, int intr_num) {
     struct apic_state * apic = (struct apic_state *)apic_dev->private_data;
-    return activate_apic_irq(apic, intr_num);
+
+    // Special cases go here
+    // startup, etc
+
+    if (activate_apic_irq(apic, intr_num) == -1) {
+       PrintError("Error: Could not activate apic_irq\n");
+       return -1;
+    } 
+
+    // Don't need this since we'll have the IPI force an exit if
+    // This is called on a different core
+    /* v3_interrupt_cpu(info, 0); */
+
+    return 0;
 }
 
 
 
 /* Timer Functions */
-static void apic_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * priv_data) {
+static void apic_update_time(struct guest_info * info, ullong_t cpu_cycles, ullong_t cpu_freq, void * priv_data) {
     struct vm_device * dev = (struct vm_device *)priv_data;
     struct apic_state * apic = (struct apic_state *)dev->private_data;
     // The 32 bit GCC runtime is a pile of shit
@@ -983,7 +1035,7 @@ static void apic_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * priv
     }
 
     tmr_ticks = cpu_cycles >> shift_num;
-    PrintDebug("Timer Ticks: %p\n", (void *)tmr_ticks);
+    //    PrintDebug("Timer Ticks: %p\n", (void *)tmr_ticks);
 
     if (tmr_ticks < apic->tmr_cur_cnt) {
        apic->tmr_cur_cnt -= tmr_ticks;
@@ -995,6 +1047,10 @@ static void apic_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * priv
        PrintDebug("Raising APIC Timer interrupt (periodic=%d) (icnt=%d) (div=%d)\n", 
                   apic->tmr_vec_tbl.tmr_mode, apic->tmr_init_cnt, shift_num);
 
+       if (apic_intr_pending(info, priv_data)) {
+           PrintDebug("Overriding pending IRQ %d\n", apic_get_intr_number(info, priv_data));
+       }
+
        if (activate_internal_irq(apic, APIC_TMR_INT) == -1) {
            PrintError("Could not raise Timer interrupt\n");
        }
@@ -1009,13 +1065,10 @@ static void apic_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * priv
 }
 
 
-
 static struct intr_ctrl_ops intr_ops = {
     .intr_pending = apic_intr_pending,
     .get_intr_number = apic_get_intr_number,
-    .raise_intr = apic_raise_intr,
     .begin_irq = apic_begin_irq,
-    .lower_intr = apic_lower_intr, 
 };
 
 
@@ -1024,46 +1077,64 @@ static struct vm_timer_ops timer_ops = {
 };
 
 
-static int apic_init(struct vm_device * dev) {
-    struct guest_info * info = dev->vm;
-    struct apic_state * apic = (struct apic_state *)(dev->private_data);
-
-    v3_register_intr_controller(dev->vm, &intr_ops, dev);
-    v3_add_timer(dev->vm, &timer_ops, dev);
 
-    init_apic_state(apic);
-
-    v3_hook_msr(info, BASE_ADDR_MSR, read_apic_msr, write_apic_msr, dev);
-
-    v3_hook_full_mem(info, apic->base_addr, apic->base_addr + PAGE_SIZE_4KB, apic_read, apic_write, dev);
-
-    return 0;
-}
 
-static int apic_deinit(struct vm_device * dev) {
-    struct guest_info * info = dev->vm;
+static int apic_free(struct vm_device * dev) {
+    //   struct apic_state * apic = (struct apic_state *)dev->private_data;
 
-    v3_unhook_msr(info, BASE_ADDR_MSR);
+    v3_unhook_msr(dev->vm, BASE_ADDR_MSR);
 
     return 0;
 }
 
 
-static struct vm_device_ops dev_ops = {
-    .init = apic_init,
-    .deinit = apic_deinit,
+static struct v3_device_ops dev_ops = {
+    .free = apic_free,
     .reset = NULL,
     .start = NULL,
     .stop = NULL,
 };
 
 
-struct vm_device * v3_create_apic() {
+
+static int apic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     PrintDebug("Creating APIC\n");
+    char * name = v3_cfg_val(cfg, "name");
+    char * icc_name = v3_cfg_val(cfg,"irq_bus");
+    int i;
+    struct vm_device * icc = v3_find_dev(vm, icc_name);
+
+    if (!icc) {
+        PrintError("Cannot find device %s\n", icc_name);
+        return -1;
+    }
 
     struct apic_state * apic = (struct apic_state *)V3_Malloc(sizeof(struct apic_state));
 
-    struct vm_device * device = v3_create_device("APIC", &dev_ops, apic);
-  
-    return device;
+    struct vm_device * dev = v3_allocate_device(name, &dev_ops, apic);
+
+    if (v3_attach_device(vm, dev) == -1) {
+       PrintError("Could not attach device %s\n", name);
+       return -1;
+    }
+
+    for (i = 0; i < vm->num_cores; i++)
+    {
+       v3_register_intr_controller(&vm->cores[i], &intr_ops, dev);
+       v3_add_timer(&vm->cores[i], &timer_ops, dev);
+    }
+
+    init_apic_state(apic);
+
+    v3_icc_register_apic(vm, icc, dev,apic->my_apic_id);
+
+    v3_hook_msr(vm, BASE_ADDR_MSR, read_apic_msr, write_apic_msr, dev);
+
+    v3_hook_full_mem(vm, apic->base_addr, apic->base_addr + PAGE_SIZE_4KB, apic_read, apic_write, dev);
+
+    return 0;
 }
+
+
+
+device_register("LAPIC", apic_init)