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".
20 #include <palacios/vmm_dev_mgr.h>
21 #include <palacios/vmm_sprintf.h>
22 #include <palacios/vm_guest.h>
23 #include <devices/icc_bus.h>
24 #include <devices/apic_regs.h>
28 #ifndef CONFIG_DEBUG_ICC_BUS
30 #define PrintDebug(fmt, args...)
34 void v3_force_exit(void *p) {
35 #ifdef CONFIG_DEBUG_ICC_BUS
36 struct guest_info *core=(struct guest_info *)p;
38 PrintDebug("core %u: Forced to exit!\n",core->cpu_id);
41 struct ipi_thunk_data {
42 struct vm_device * target;
49 struct guest_info * core;
50 struct v3_icc_ops * ops;
57 struct icc_bus_state {
58 struct apic_data apics[MAX_APICS];
63 static struct v3_device_ops dev_ops = {
70 #ifdef CONFIG_DEBUG_ICC_BUS
71 static char *shorthand_str[] = {
78 static char *deliverymode_str[] = {
91 static int deliver(uint32_t src_apic, struct apic_data *dest_apic, struct int_cmd_reg *icr, struct icc_bus_state * state, uint32_t extirq) {
93 switch (icr->del_mode) {
96 case 1: // lowest priority
98 PrintDebug("icc_bus: delivering IRQ to core %u\n",dest_apic->core->cpu_id);
100 dest_apic->ops->raise_intr(dest_apic->core,
101 (icr->del_mode != 7) ? icr->vec : extirq,
102 dest_apic->priv_data);
104 if ((src_apic != state->ioapic_id) && (dest_apic->core->cpu_id != src_apic)) {
105 // Assume core # is same as logical processor for now
106 // TODO FIX THIS FIX THIS
107 // THERE SHOULD BE: guestapicid->virtualapicid map,
108 // cpu_id->logical processor map
109 // host maitains logical proc->phsysical proc
110 PrintDebug("icc_bus: non-local core, forcing it to exit\n");
112 V3_Call_On_CPU(dest_apic->core->cpu_id, v3_force_exit, (void *)(dest_apic->core));
113 // TODO: do what the print says
118 PrintError("icc_bus: SMI delivery is unsupported\n");
123 PrintError("icc_bus: Reserved delivery mode 3 is unsupported\n");
128 PrintError("icc_bus: NMI delivery is unsupported\n");
133 struct guest_info *core = dest_apic->core;
135 PrintDebug("icc_bus: INIT delivery to core %u\n", core->cpu_id);
137 // TODO: any APIC reset on dest core (shouldn't be needed, but not sure...)
140 if (dest_apic->ipi_state != INIT) {
141 PrintError("icc_bus: Warning: core %u is not in INIT state (mode = %d), ignored\n",
142 core->cpu_id, core->cpu_mode);
143 // Only a warning, since INIT INIT SIPI is common
147 // We transition the target core to SIPI state
148 dest_apic->ipi_state = SIPI; // note: locking should not be needed here
150 // That should be it since the target core should be
151 // waiting in host on this transition
152 // either it's on another core or on a different preemptive thread
153 // in both cases, it will quickly notice this transition
154 // in particular, we should not need to force an exit here
156 PrintDebug("icc_bus: INIT delivery done\n");
162 struct guest_info *core = dest_apic->core;
165 if (dest_apic->ipi_state != SIPI) {
166 PrintError("icc_bus: core %u is not in SIPI state (mode = %d), ignored!\n",core->cpu_id, core->cpu_mode);
170 // Write the RIP, CS, and descriptor
171 // assume the rest is already good to go
173 // vector VV -> rip at 0
175 // This means we start executing at linear address VV000
177 // So the selector needs to be VV00
178 // and the base needs to be VV000
181 core->segments.cs.selector = icr->vec << 8;
182 core->segments.cs.limit = 0xffff;
183 core->segments.cs.base = icr->vec << 12;
185 PrintDebug("icc_bus: SIPI delivery (0x%x -> 0x%x:0x0) to core %u\n",
186 icr->vec, core->segments.cs.selector, core->cpu_id);
187 // Maybe need to adjust the APIC?
189 // We transition the target core to SIPI state
190 core->core_run_state = CORE_RUNNING; // note: locking should not be needed here
192 // As with INIT, we should not need to do anything else
194 PrintDebug("icc_bus: SIPI delivery done\n");
205 // icr_data contains interrupt vector *except* for ext_int
206 // in which case it is given via irq
209 int v3_icc_send_ipi(struct vm_device * icc_bus, uint32_t src_apic, uint64_t icr_data, uint32_t extirq) {
211 struct int_cmd_reg *icr = (struct int_cmd_reg *)&icr_data;
213 struct icc_bus_state * state = (struct icc_bus_state *)icc_bus->private_data;
214 struct apic_data * dest_apic = NULL;
215 PrintDebug("icc_bus: icc_bus=%p, src_apic=%u, icr_data=%llx, extirq=%u\n",
216 icc_bus, src_apic, icr_data, extirq);
218 // initial sanity checks
219 if ((src_apic >= MAX_APICS) ||
220 ((state->apics[src_apic].present == 0) &&
221 (src_apic != state->ioapic_id))) {
222 PrintError("icc_bus: Apparently sending from unregistered apic id=%u\n",src_apic);
227 if ((icr->dst_mode == 0) && (state->apics[icr->dst].present == 0)) {
228 PrintError("icc_bus: Attempted send to unregistered apic id=%u\n", icr->dst);
232 dest_apic = &(state->apics[icr->dst]);
235 PrintDebug("icc_bus: IPI %s %u from %s %u to %s %s %u (icr=0x%llx, extirq=%u)\n",
236 deliverymode_str[icr->del_mode], icr->vec,
237 src_apic==state->ioapic_id ? "ioapic" : "apic",
239 icr->dst_mode==0 ? "(physical)" : "(logical)",
240 shorthand_str[icr->dst_shorthand], icr->dst,icr->val,
245 if (icr->dst==state->ioapic_id) {
246 PrintError("icc_bus: Attempted send to ioapic ignored\n");
253 switch (icr->dst_shorthand) {
255 case 0: // no shorthand
257 if (icr->dst_mode==0) {
259 struct apic_data * dest_apic = &(state->apics[icr->dst]);
260 if (deliver(src_apic,dest_apic,icr,state,extirq)) {
266 uint8_t mda = icr->dst;
267 for (i=0;i<MAX_APICS;i++) {
268 struct apic_data *dest_apic=&(state->apics[i]);
269 if (dest_apic->present &&
270 dest_apic->ops->should_deliver(dest_apic->core,
272 dest_apic->priv_data)) {
273 if (deliver(src_apic,dest_apic,icr,state,extirq)) {
284 if (icr->dst_mode==0) {
286 if (icr->dst==state->ioapic_id) {
287 PrintError("icc_bus: ioapic attempting to send to itself\n");
290 struct apic_data *dest_apic=&(state->apics[src_apic]);
291 if (deliver(src_apic,dest_apic,icr,state,extirq)) {
296 PrintError("icc_bus: use of logical delivery in self is not yet supported.\n");
304 case 3: { // all and all-but-me
305 // assuming that logical verus physical doesn't matter
306 // although it is odd that both are used
308 for (i=0;i<MAX_APICS;i++) {
309 struct apic_data *dest_apic=&(state->apics[i]);
310 if (dest_apic->present && (i!=src_apic || icr->dst_shorthand==2)) {
311 if (deliver(src_apic,dest_apic,icr,state,extirq)) {
329 /* THIS IS A BIG ASSUMPTION: APIC PHYSID == LOGID == CORENUM */
331 int v3_icc_register_apic(struct guest_info * core, struct vm_device * icc_bus,
332 uint8_t apic_num, struct v3_icc_ops * ops, void * priv_data) {
333 struct icc_bus_state * icc = (struct icc_bus_state *)icc_bus->private_data;
334 struct apic_data * apic = &(icc->apics[apic_num]);
336 if (apic->present == 1) {
337 PrintError("icc_bus: Attempt to re-register apic %u\n", apic_num);
342 apic->priv_data = priv_data;
346 PrintDebug("icc_bus: Registered apic %u\n", apic_num);
352 int v3_icc_register_ioapic(struct v3_vm_info *vm, struct vm_device * icc_bus, uint8_t apic_num)
354 struct icc_bus_state * icc = (struct icc_bus_state *)icc_bus->private_data;
356 if (icc->ioapic_id) {
357 PrintError("icc_bus: Attempt to register a second ioapic!\n");
361 icc->ioapic_id=apic_num;
363 PrintDebug("icc_bus: Registered ioapic %u\n", apic_num);
370 static int icc_bus_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
371 PrintDebug("icc_bus: Creating ICC_BUS\n");
373 char * dev_id = v3_cfg_val(cfg, "ID");
375 struct icc_bus_state * icc_bus = (struct icc_bus_state *)V3_Malloc(sizeof(struct icc_bus_state));
376 memset(icc_bus, 0, sizeof(struct icc_bus_state));
378 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, icc_bus);
380 if (v3_attach_device(vm, dev) == -1) {
381 PrintError("icc_bus: Could not attach device %s\n", dev_id);
390 device_register("ICC_BUS", icc_bus_init)