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.


removed derogatory comments about Linux
[palacios.git] / palacios / src / devices / apic.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #include <devices/apic.h>
22 #include <devices/apic_regs.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_msr.h>
25
26
27 #ifndef DEBUG_APIC
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32
33 typedef enum { APIC_TMR_INT, APIC_THERM_INT, APIC_PERF_INT, 
34                APIC_LINT0_INT, APIC_LINT1_INT, APIC_ERR_INT } apic_irq_type_t;
35
36 #define APIC_FIXED_DELIVERY  0x0
37 #define APIC_SMI_DELIVERY    0x2
38 #define APIC_NMI_DELIVERY    0x4
39 #define APIC_INIT_DELIVERY   0x5
40 #define APIC_EXTINT_DELIVERY 0x7
41
42
43 #define BASE_ADDR_MSR 0x0000001B
44 #define DEFAULT_BASE_ADDR 0xfee00000
45
46 #define APIC_ID_OFFSET                    0x020
47 #define APIC_VERSION_OFFSET               0x030
48 #define TPR_OFFSET                        0x080
49 #define APR_OFFSET                        0x090
50 #define PPR_OFFSET                        0x0a0
51 #define EOI_OFFSET                        0x0b0
52 #define REMOTE_READ_OFFSET                0x0c0
53 #define LDR_OFFSET                        0x0d0
54 #define DFR_OFFSET                        0x0e0
55 #define SPURIOUS_INT_VEC_OFFSET           0x0f0
56
57 #define ISR_OFFSET0                       0x100   // 0x100 - 0x170
58 #define ISR_OFFSET1                       0x110   // 0x100 - 0x170
59 #define ISR_OFFSET2                       0x120   // 0x100 - 0x170
60 #define ISR_OFFSET3                       0x130   // 0x100 - 0x170
61 #define ISR_OFFSET4                       0x140   // 0x100 - 0x170
62 #define ISR_OFFSET5                       0x150   // 0x100 - 0x170
63 #define ISR_OFFSET6                       0x160   // 0x100 - 0x170
64 #define ISR_OFFSET7                       0x170   // 0x100 - 0x170
65
66 #define TRIG_OFFSET0                      0x180   // 0x180 - 0x1f0
67 #define TRIG_OFFSET1                      0x190   // 0x180 - 0x1f0
68 #define TRIG_OFFSET2                      0x1a0   // 0x180 - 0x1f0
69 #define TRIG_OFFSET3                      0x1b0   // 0x180 - 0x1f0
70 #define TRIG_OFFSET4                      0x1c0   // 0x180 - 0x1f0
71 #define TRIG_OFFSET5                      0x1d0   // 0x180 - 0x1f0
72 #define TRIG_OFFSET6                      0x1e0   // 0x180 - 0x1f0
73 #define TRIG_OFFSET7                      0x1f0   // 0x180 - 0x1f0
74
75
76 #define IRR_OFFSET0                       0x200   // 0x200 - 0x270
77 #define IRR_OFFSET1                       0x210   // 0x200 - 0x270
78 #define IRR_OFFSET2                       0x220   // 0x200 - 0x270
79 #define IRR_OFFSET3                       0x230   // 0x200 - 0x270
80 #define IRR_OFFSET4                       0x240   // 0x200 - 0x270
81 #define IRR_OFFSET5                       0x250   // 0x200 - 0x270
82 #define IRR_OFFSET6                       0x260   // 0x200 - 0x270
83 #define IRR_OFFSET7                       0x270   // 0x200 - 0x270
84
85
86 #define ESR_OFFSET                        0x280
87 #define INT_CMD_LO_OFFSET                 0x300
88 #define INT_CMD_HI_OFFSET                 0x310
89 #define TMR_LOC_VEC_TBL_OFFSET            0x320
90 #define THERM_LOC_VEC_TBL_OFFSET          0x330
91 #define PERF_CTR_LOC_VEC_TBL_OFFSET       0x340
92 #define LINT0_VEC_TBL_OFFSET              0x350
93 #define LINT1_VEC_TBL_OFFSET              0x360
94 #define ERR_VEC_TBL_OFFSET                0x370
95 #define TMR_INIT_CNT_OFFSET               0x380
96 #define TMR_CUR_CNT_OFFSET                0x390
97 #define TMR_DIV_CFG_OFFSET                0x3e0
98 #define EXT_APIC_FEATURE_OFFSET           0x400
99 #define EXT_APIC_CMD_OFFSET               0x410
100 #define SEOI_OFFSET                       0x420
101
102 #define IER_OFFSET0                       0x480   // 0x480 - 0x4f0
103 #define IER_OFFSET1                       0x490   // 0x480 - 0x4f0
104 #define IER_OFFSET2                       0x4a0   // 0x480 - 0x4f0
105 #define IER_OFFSET3                       0x4b0   // 0x480 - 0x4f0
106 #define IER_OFFSET4                       0x4c0   // 0x480 - 0x4f0
107 #define IER_OFFSET5                       0x4d0   // 0x480 - 0x4f0
108 #define IER_OFFSET6                       0x4e0   // 0x480 - 0x4f0
109 #define IER_OFFSET7                       0x4f0   // 0x480 - 0x4f0
110
111 #define EXT_INT_LOC_VEC_TBL_OFFSET0       0x500   // 0x500 - 0x530
112 #define EXT_INT_LOC_VEC_TBL_OFFSET1       0x510   // 0x500 - 0x530
113 #define EXT_INT_LOC_VEC_TBL_OFFSET2       0x520   // 0x500 - 0x530
114 #define EXT_INT_LOC_VEC_TBL_OFFSET3       0x530   // 0x500 - 0x530
115
116
117
118 struct apic_msr {
119     union {
120         uint64_t val;
121         struct {
122             uchar_t rsvd;
123             uint_t bootstrap_cpu : 1;
124             uint_t rsvd2         : 2;
125             uint_t apic_enable   : 1;
126             ullong_t base_addr   : 40;
127             uint_t rsvd3         : 12;
128         } __attribute__((packed));
129     } __attribute__((packed));
130 } __attribute__((packed));
131
132
133
134
135 struct apic_state {
136     addr_t base_addr;
137
138     /* MSRs */
139     v3_msr_t base_addr_msr;
140
141
142     /* memory map registers */
143
144     struct lapic_id_reg lapic_id;
145     struct apic_ver_reg apic_ver;
146     struct ext_apic_ctrl_reg ext_apic_ctrl;
147     struct local_vec_tbl_reg local_vec_tbl;
148     struct tmr_vec_tbl_reg tmr_vec_tbl;
149     struct tmr_div_cfg_reg tmr_div_cfg;
150     struct lint_vec_tbl_reg lint0_vec_tbl;
151     struct lint_vec_tbl_reg lint1_vec_tbl;
152     struct perf_ctr_loc_vec_tbl_reg perf_ctr_loc_vec_tbl;
153     struct therm_loc_vec_tbl_reg therm_loc_vec_tbl;
154     struct err_vec_tbl_reg err_vec_tbl;
155     struct err_status_reg err_status;
156     struct spurious_int_reg spurious_int;
157     struct int_cmd_reg int_cmd;
158     struct log_dst_reg log_dst;
159     struct dst_fmt_reg dst_fmt;
160     struct arb_prio_reg arb_prio;
161     struct task_prio_reg task_prio;
162     struct proc_prio_reg proc_prio;
163     struct ext_apic_feature_reg ext_apic_feature;
164     struct spec_eoi_reg spec_eoi;
165   
166
167     uint32_t tmr_cur_cnt;
168     uint32_t tmr_init_cnt;
169
170
171     struct local_vec_tbl_reg ext_intr_vec_tbl[4];
172
173     uint32_t rem_rd_data;
174
175
176     uchar_t int_req_reg[32];
177     uchar_t int_svc_reg[32];
178     uchar_t int_en_reg[32];
179     uchar_t trig_mode_reg[32];
180   
181     uint32_t eoi;
182
183
184 };
185
186
187 static void init_apic_state(struct apic_state * apic) {
188     apic->base_addr = DEFAULT_BASE_ADDR;
189     apic->base_addr_msr.value = 0x0000000000000900LL;
190     apic->base_addr_msr.value |= ((uint64_t)DEFAULT_BASE_ADDR); 
191
192     PrintDebug("Sizeof Interrupt Request Register %d, should be 32\n", 
193                (uint_t)sizeof(apic->int_req_reg));
194
195     memset(apic->int_req_reg, 0, sizeof(apic->int_req_reg));
196     memset(apic->int_svc_reg, 0, sizeof(apic->int_svc_reg));
197     memset(apic->int_en_reg, 0xff, sizeof(apic->int_en_reg));
198     memset(apic->trig_mode_reg, 0, sizeof(apic->trig_mode_reg));
199
200     apic->eoi = 0x00000000;
201     apic->rem_rd_data = 0x00000000;
202     apic->tmr_init_cnt = 0x00000000;
203     apic->tmr_cur_cnt = 0x00000000;
204
205     // TODO:
206     // We need to figure out what the APIC ID is....
207     apic->lapic_id.val = 0x00000000;
208
209     // The P6 has 6 LVT entries, so we set the value to (6-1)...
210     apic->apic_ver.val = 0x80050010;
211
212     apic->task_prio.val = 0x00000000;
213     apic->arb_prio.val = 0x00000000;
214     apic->proc_prio.val = 0x00000000;
215     apic->log_dst.val = 0x00000000;
216     apic->dst_fmt.val = 0xffffffff;
217     apic->spurious_int.val = 0x000000ff;
218     apic->err_status.val = 0x00000000;
219     apic->int_cmd.val = 0x0000000000000000LL;
220     apic->tmr_vec_tbl.val = 0x00010000;
221     apic->therm_loc_vec_tbl.val = 0x00010000;
222     apic->perf_ctr_loc_vec_tbl.val = 0x00010000;
223     apic->lint0_vec_tbl.val = 0x00010000;
224     apic->lint1_vec_tbl.val = 0x00010000;
225     apic->err_vec_tbl.val = 0x00010000;
226     apic->tmr_div_cfg.val = 0x00000000;
227     //apic->ext_apic_feature.val = 0x00000007;
228     apic->ext_apic_feature.val = 0x00040007;
229     apic->ext_apic_ctrl.val = 0x00000000;
230     apic->spec_eoi.val = 0x00000000;
231 }
232
233
234
235
236 static int read_apic_msr(uint_t msr, v3_msr_t * dst, void * priv_data) {
237     struct vm_device * dev = (struct vm_device *)priv_data;
238     struct apic_state * apic = (struct apic_state *)dev->private_data;
239     PrintError("READING APIC BASE ADDR: HI=%x LO=%x\n", apic->base_addr_msr.hi, apic->base_addr_msr.lo);
240
241     return -1;
242 }
243
244
245 static int write_apic_msr(uint_t msr, v3_msr_t src, void * priv_data) {
246     //  struct vm_device * dev = (struct vm_device *)priv_data;
247     //  struct apic_state * apic = (struct apic_state *)dev->private_data;
248
249     PrintError("WRITING APIC BASE ADDR: HI=%x LO=%x\n", src.hi, src.lo);
250
251     return -1;
252 }
253
254
255 // irq_num is the bit offset into a 256 bit buffer...
256 static int activate_apic_irq(struct apic_state * apic, uint32_t irq_num) {
257     int major_offset = (irq_num & ~0x00000007) >> 3;
258     int minor_offset = irq_num & 0x00000007;
259     uchar_t * req_location = apic->int_req_reg + major_offset;
260     uchar_t * en_location = apic->int_en_reg + major_offset;
261     uchar_t flag = 0x1 << minor_offset;
262
263     if (irq_num <= 15) {
264         PrintError("Attempting to raise an invalid interrupt: %d\n", irq_num);
265         return -1;
266     }
267
268     PrintDebug("Raising APIC IRQ %d\n", irq_num);
269
270     if (*en_location & flag) {
271         *req_location |= flag;
272     } else {
273         PrintDebug("Interrupt  not enabled... %.2x\n", *en_location);
274         return 0;
275     }
276
277     return 0;
278 }
279
280
281
282 static int get_highest_isr(struct apic_state * apic) {
283     int i = 0, j = 0;
284
285     // We iterate backwards to find the highest priority
286     for (i = 31; i >= 0; i--) {
287         uchar_t  * svc_major = apic->int_svc_reg + i;
288     
289         if ((*svc_major) & 0xff) {
290             for (j = 7; j >= 0; j--) {
291                 uchar_t flag = 0x1 << j;
292                 if ((*svc_major) & flag) {
293                     return ((i * 8) + j);
294                 }
295             }
296         }
297     }
298
299     return -1;
300 }
301  
302
303
304 static int get_highest_irr(struct apic_state * apic) {
305     int i = 0, j = 0;
306
307     // We iterate backwards to find the highest priority
308     for (i = 31; i >= 0; i--) {
309         uchar_t  * req_major = apic->int_req_reg + i;
310     
311         if ((*req_major) & 0xff) {
312             for (j = 7; j >= 0; j--) {
313                 uchar_t flag = 0x1 << j;
314                 if ((*req_major) & flag) {
315                     return ((i * 8) + j);
316                 }
317             }
318         }
319     }
320
321     return -1;
322 }
323  
324
325
326
327 static int apic_do_eoi(struct apic_state * apic) {
328     int isr_irq = get_highest_isr(apic);
329
330     if (isr_irq != -1) {
331         int major_offset = (isr_irq & ~0x00000007) >> 3;
332         int minor_offset = isr_irq & 0x00000007;
333         uchar_t flag = 0x1 << minor_offset;
334         uchar_t * svc_location = apic->int_svc_reg + major_offset;
335         
336         PrintDebug("Received APIC EOI\n");
337         
338         *svc_location &= ~flag;
339
340 #ifdef CRAY_XT
341         
342         if ((isr_irq == 238) || 
343             (isr_irq == 239)) {
344             PrintError("Acking IRQ %d\n", isr_irq);
345         }
346         
347         if (isr_irq == 238) {
348             V3_ACK_IRQ(238);
349         }
350 #endif
351     } else {
352         PrintError("Spurious EOI...\n");
353     }
354         
355     return 0;
356 }
357  
358
359 static int activate_internal_irq(struct apic_state * apic, apic_irq_type_t int_type) {
360     uint32_t vec_num = 0;
361     uint32_t del_mode = 0;
362     int masked = 0;
363
364
365     switch (int_type) {
366         case APIC_TMR_INT:
367             vec_num = apic->tmr_vec_tbl.vec;
368             del_mode = APIC_FIXED_DELIVERY;
369             masked = apic->tmr_vec_tbl.mask;
370             break;
371         case APIC_THERM_INT:
372             vec_num = apic->therm_loc_vec_tbl.vec;
373             del_mode = apic->therm_loc_vec_tbl.msg_type;
374             masked = apic->therm_loc_vec_tbl.mask;
375             break;
376         case APIC_PERF_INT:
377             vec_num = apic->perf_ctr_loc_vec_tbl.vec;
378             del_mode = apic->perf_ctr_loc_vec_tbl.msg_type;
379             masked = apic->perf_ctr_loc_vec_tbl.mask;
380             break;
381         case APIC_LINT0_INT:
382             vec_num = apic->lint0_vec_tbl.vec;
383             del_mode = apic->lint0_vec_tbl.msg_type;
384             masked = apic->lint0_vec_tbl.mask;
385             break;
386         case APIC_LINT1_INT:
387             vec_num = apic->lint1_vec_tbl.vec;
388             del_mode = apic->lint1_vec_tbl.msg_type;
389             masked = apic->lint1_vec_tbl.mask;
390             break;
391         case APIC_ERR_INT:
392             vec_num = apic->err_vec_tbl.vec;
393             del_mode = APIC_FIXED_DELIVERY;
394             masked = apic->err_vec_tbl.mask;
395             break;
396         default:
397             PrintError("Invalid APIC interrupt type\n");
398             return -1;
399     }
400
401     // interrupt is masked, don't send
402     if (masked == 1) {
403         PrintDebug("Inerrupt is masked\n");
404         return 0;
405     }
406
407     if (del_mode == APIC_FIXED_DELIVERY) {
408         //PrintDebug("Activating internal APIC IRQ %d\n", vec_num);
409         return activate_apic_irq(apic, vec_num);
410     } else {
411         PrintError("Unhandled Delivery Mode\n");
412         return -1;
413     }
414 }
415
416
417 static int apic_read(addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
418     struct vm_device * dev = (struct vm_device *)priv_data;
419     struct apic_state * apic = (struct apic_state *)dev->private_data;
420     addr_t reg_addr  = guest_addr - apic->base_addr;
421     struct apic_msr * msr = (struct apic_msr *)&(apic->base_addr_msr.value);
422     uint32_t val = 0;
423
424
425     PrintDebug("Read apic address space (%p)\n", 
426                (void *)guest_addr);
427
428     if (msr->apic_enable == 0) {
429         PrintError("Write to APIC address space with disabled APIC\n");
430         return -1;
431     }
432
433
434     /*   if (length != 4) { */
435     /*     PrintError("Invalid apic read length (%d)\n", length); */
436     /*     return -1; */
437     /*   } */
438
439     switch (reg_addr & ~0x3) {
440         case EOI_OFFSET:
441             //    PrintError("Attempting to read from write only register\n");
442             //    return -1;
443             break;
444
445             // data registers
446         case APIC_ID_OFFSET:
447             val = apic->lapic_id.val;
448             break;
449         case APIC_VERSION_OFFSET:
450             val = apic->apic_ver.val;
451             break;
452         case TPR_OFFSET:
453             val = apic->task_prio.val;
454             break;
455         case APR_OFFSET:
456             val = apic->arb_prio.val;
457             break;
458         case PPR_OFFSET:
459             val = apic->proc_prio.val;
460             break;
461         case REMOTE_READ_OFFSET:
462             val = apic->rem_rd_data;
463             break;
464         case LDR_OFFSET:
465             val = apic->log_dst.val;
466             break;
467         case DFR_OFFSET:
468             val = apic->dst_fmt.val;
469             break;
470         case SPURIOUS_INT_VEC_OFFSET:
471             val = apic->spurious_int.val;
472             break;
473         case ESR_OFFSET:
474             val = apic->err_status.val;
475             break;
476         case TMR_LOC_VEC_TBL_OFFSET:
477             val = apic->tmr_vec_tbl.val;
478             break;
479         case LINT0_VEC_TBL_OFFSET:
480             val = apic->lint0_vec_tbl.val;
481             break;
482         case LINT1_VEC_TBL_OFFSET:
483             val = apic->lint1_vec_tbl.val;
484             break;
485         case ERR_VEC_TBL_OFFSET:
486             val = apic->err_vec_tbl.val;
487             break;
488         case TMR_INIT_CNT_OFFSET:
489             val = apic->tmr_init_cnt;
490             break;
491         case TMR_DIV_CFG_OFFSET:
492             val = apic->tmr_div_cfg.val;
493             break;
494
495         case IER_OFFSET0:
496             val = *(uint32_t *)(apic->int_en_reg);
497             break;
498         case IER_OFFSET1:
499             val = *(uint32_t *)(apic->int_en_reg + 4);
500             break;
501         case IER_OFFSET2:
502             val = *(uint32_t *)(apic->int_en_reg + 8);
503             break;
504         case IER_OFFSET3:
505             val = *(uint32_t *)(apic->int_en_reg + 12);
506             break;
507         case IER_OFFSET4:
508             val = *(uint32_t *)(apic->int_en_reg + 16);
509             break;
510         case IER_OFFSET5:
511             val = *(uint32_t *)(apic->int_en_reg + 20);
512             break;
513         case IER_OFFSET6:
514             val = *(uint32_t *)(apic->int_en_reg + 24);
515             break;
516         case IER_OFFSET7:
517             val = *(uint32_t *)(apic->int_en_reg + 28);
518             break;
519
520         case ISR_OFFSET0:
521             val = *(uint32_t *)(apic->int_svc_reg);
522             break;
523         case ISR_OFFSET1:
524             val = *(uint32_t *)(apic->int_svc_reg + 4);
525             break;
526         case ISR_OFFSET2:
527             val = *(uint32_t *)(apic->int_svc_reg + 8);
528             break;
529         case ISR_OFFSET3:
530             val = *(uint32_t *)(apic->int_svc_reg + 12);
531             break;
532         case ISR_OFFSET4:
533             val = *(uint32_t *)(apic->int_svc_reg + 16);
534             break;
535         case ISR_OFFSET5:
536             val = *(uint32_t *)(apic->int_svc_reg + 20);
537             break;
538         case ISR_OFFSET6:
539             val = *(uint32_t *)(apic->int_svc_reg + 24);
540             break;
541         case ISR_OFFSET7:
542             val = *(uint32_t *)(apic->int_svc_reg + 28);
543             break;
544    
545         case TRIG_OFFSET0:
546             val = *(uint32_t *)(apic->trig_mode_reg);
547             break;
548         case TRIG_OFFSET1:
549             val = *(uint32_t *)(apic->trig_mode_reg + 4);
550             break;
551         case TRIG_OFFSET2:
552             val = *(uint32_t *)(apic->trig_mode_reg + 8);
553             break;
554         case TRIG_OFFSET3:
555             val = *(uint32_t *)(apic->trig_mode_reg + 12);
556             break;
557         case TRIG_OFFSET4:
558             val = *(uint32_t *)(apic->trig_mode_reg + 16);
559             break;
560         case TRIG_OFFSET5:
561             val = *(uint32_t *)(apic->trig_mode_reg + 20);
562             break;
563         case TRIG_OFFSET6:
564             val = *(uint32_t *)(apic->trig_mode_reg + 24);
565             break;
566         case TRIG_OFFSET7:
567             val = *(uint32_t *)(apic->trig_mode_reg + 28);
568             break;
569
570         case IRR_OFFSET0:
571             val = *(uint32_t *)(apic->int_req_reg);
572             break;
573         case IRR_OFFSET1:
574             val = *(uint32_t *)(apic->int_req_reg + 4);
575             break;
576         case IRR_OFFSET2:
577             val = *(uint32_t *)(apic->int_req_reg + 8);
578             break;
579         case IRR_OFFSET3:
580             val = *(uint32_t *)(apic->int_req_reg + 12);
581             break;
582         case IRR_OFFSET4:
583             val = *(uint32_t *)(apic->int_req_reg + 16);
584             break;
585         case IRR_OFFSET5:
586             val = *(uint32_t *)(apic->int_req_reg + 20);
587             break;
588         case IRR_OFFSET6:
589             val = *(uint32_t *)(apic->int_req_reg + 24);
590             break;
591         case IRR_OFFSET7:
592             val = *(uint32_t *)(apic->int_req_reg + 28);
593             break;
594         case TMR_CUR_CNT_OFFSET:
595             val = apic->tmr_cur_cnt;
596             break;
597
598             // We are not going to implement these....
599         case THERM_LOC_VEC_TBL_OFFSET:
600             val = apic->therm_loc_vec_tbl.val;
601             break;
602         case PERF_CTR_LOC_VEC_TBL_OFFSET:
603             val = apic->perf_ctr_loc_vec_tbl.val;
604             break;
605
606  
607
608             // handled registers
609         case INT_CMD_LO_OFFSET:    
610             val = apic->int_cmd.lo;
611             break;
612         case INT_CMD_HI_OFFSET:
613             val = apic->int_cmd.hi;
614             break;
615
616             // handle current timer count
617
618             // Unhandled Registers
619         case EXT_INT_LOC_VEC_TBL_OFFSET0:
620             val = apic->ext_intr_vec_tbl[0].val;
621             break;
622         case EXT_INT_LOC_VEC_TBL_OFFSET1:
623             val = apic->ext_intr_vec_tbl[1].val;
624             break;
625         case EXT_INT_LOC_VEC_TBL_OFFSET2:
626             val = apic->ext_intr_vec_tbl[2].val;
627             break;
628         case EXT_INT_LOC_VEC_TBL_OFFSET3:
629             val = apic->ext_intr_vec_tbl[3].val;
630             break;
631     
632
633         case EXT_APIC_FEATURE_OFFSET:
634         case EXT_APIC_CMD_OFFSET:
635         case SEOI_OFFSET:
636
637         default:
638             PrintError("Read from Unhandled APIC Register: %x\n", (uint32_t)reg_addr);
639             return -1;
640     }
641
642
643     if (length == 1) {
644         uint_t byte_addr = reg_addr & 0x3;
645         uint8_t * val_ptr = (uint8_t *)dst;
646     
647         *val_ptr = *(((uint8_t *)&val) + byte_addr);
648
649     } else if ((length == 2) && 
650                ((reg_addr & 0x3) == 0x3)) {
651         uint_t byte_addr = reg_addr & 0x3;
652         uint16_t * val_ptr = (uint16_t *)dst;
653         *val_ptr = *(((uint16_t *)&val) + byte_addr);
654
655     } else if (length == 4) {
656         uint32_t * val_ptr = (uint32_t *)dst;
657         *val_ptr = val;
658
659     } else {
660         PrintError("Invalid apic read length (%d)\n", length);
661         return -1;
662     }
663
664     PrintDebug("Read finished (val=%x)\n", *(uint32_t *)dst);
665
666     return length;
667 }
668
669
670 static int apic_write(addr_t guest_addr, void * src, uint_t length, void * priv_data) {
671     struct vm_device * dev = (struct vm_device *)priv_data;
672     struct apic_state * apic = (struct apic_state *)dev->private_data;
673     addr_t reg_addr  = guest_addr - apic->base_addr;
674     struct apic_msr * msr = (struct apic_msr *)&(apic->base_addr_msr.value);
675     uint32_t op_val = *(uint32_t *)src;
676
677     PrintDebug("Write to apic address space (%p) (val=%x)\n", 
678                (void *)guest_addr, *(uint32_t *)src);
679
680     if (msr->apic_enable == 0) {
681         PrintError("Write to APIC address space with disabled APIC\n");
682         return -1;
683     }
684
685
686     if (length != 4) {
687         PrintError("Invalid apic write length (%d)\n", length);
688         return -1;
689     }
690
691     switch (reg_addr) {
692         case REMOTE_READ_OFFSET:
693         case APIC_VERSION_OFFSET:
694         case APR_OFFSET:
695         case IRR_OFFSET0:
696         case IRR_OFFSET1:
697         case IRR_OFFSET2:
698         case IRR_OFFSET3:
699         case IRR_OFFSET4:
700         case IRR_OFFSET5:
701         case IRR_OFFSET6:
702         case IRR_OFFSET7:
703         case ISR_OFFSET0:
704         case ISR_OFFSET1:
705         case ISR_OFFSET2:
706         case ISR_OFFSET3:
707         case ISR_OFFSET4:
708         case ISR_OFFSET5:
709         case ISR_OFFSET6:
710         case ISR_OFFSET7:
711         case TRIG_OFFSET0:
712         case TRIG_OFFSET1:
713         case TRIG_OFFSET2:
714         case TRIG_OFFSET3:
715         case TRIG_OFFSET4:
716         case TRIG_OFFSET5:
717         case TRIG_OFFSET6:
718         case TRIG_OFFSET7:
719         case PPR_OFFSET:
720         case EXT_APIC_FEATURE_OFFSET:
721 #if 1
722             PrintError("Attempting to write to read only register %p (ignored)\n", (void *)reg_addr);
723 #else   
724             PrintError("Attempting to write to read only register %p (error)\n", (void *)reg_addr);
725             return -1;
726 #endif
727             break;
728
729             // Data registers
730         case APIC_ID_OFFSET:
731             apic->lapic_id.val = op_val;
732             break;
733         case TPR_OFFSET:
734             apic->task_prio.val = op_val;
735             break;
736         case LDR_OFFSET:
737             apic->log_dst.val = op_val;
738             break;
739         case DFR_OFFSET:
740             apic->dst_fmt.val = op_val;
741             break;
742         case SPURIOUS_INT_VEC_OFFSET:
743             apic->spurious_int.val = op_val;
744             break;
745         case ESR_OFFSET:
746             apic->err_status.val = op_val;
747             break;
748         case TMR_LOC_VEC_TBL_OFFSET:
749             apic->tmr_vec_tbl.val = op_val;
750             break;
751         case THERM_LOC_VEC_TBL_OFFSET:
752             apic->therm_loc_vec_tbl.val = op_val;
753             break;
754         case PERF_CTR_LOC_VEC_TBL_OFFSET:
755             apic->perf_ctr_loc_vec_tbl.val = op_val;
756             break;
757         case LINT0_VEC_TBL_OFFSET:
758             apic->lint0_vec_tbl.val = op_val;
759             break;
760         case LINT1_VEC_TBL_OFFSET:
761             apic->lint1_vec_tbl.val = op_val;
762             break;
763         case ERR_VEC_TBL_OFFSET:
764             apic->err_vec_tbl.val = op_val;
765             break;
766         case TMR_INIT_CNT_OFFSET:
767             apic->tmr_init_cnt = op_val;
768             apic->tmr_cur_cnt = op_val;
769             break;
770         case TMR_CUR_CNT_OFFSET:
771             apic->tmr_cur_cnt = op_val;
772             break;
773         case TMR_DIV_CFG_OFFSET:
774             apic->tmr_div_cfg.val = op_val;
775             break;
776
777
778             // Enable mask (256 bits)
779         case IER_OFFSET0:
780             *(uint32_t *)(apic->int_en_reg) = op_val;
781             break;
782         case IER_OFFSET1:
783             *(uint32_t *)(apic->int_en_reg + 4) = op_val;
784             break;
785         case IER_OFFSET2:
786             *(uint32_t *)(apic->int_en_reg + 8) = op_val;
787             break;
788         case IER_OFFSET3:
789             *(uint32_t *)(apic->int_en_reg + 12) = op_val;
790             break;
791         case IER_OFFSET4:
792             *(uint32_t *)(apic->int_en_reg + 16) = op_val;
793             break;
794         case IER_OFFSET5:
795             *(uint32_t *)(apic->int_en_reg + 20) = op_val;
796             break;
797         case IER_OFFSET6:
798             *(uint32_t *)(apic->int_en_reg + 24) = op_val;
799             break;
800         case IER_OFFSET7:
801             *(uint32_t *)(apic->int_en_reg + 28) = op_val;
802             break;
803
804         case EXT_INT_LOC_VEC_TBL_OFFSET0:
805             apic->ext_intr_vec_tbl[0].val = op_val;
806             break;
807         case EXT_INT_LOC_VEC_TBL_OFFSET1:
808             apic->ext_intr_vec_tbl[1].val = op_val;
809             break;
810         case EXT_INT_LOC_VEC_TBL_OFFSET2:
811             apic->ext_intr_vec_tbl[2].val = op_val;
812             break;
813         case EXT_INT_LOC_VEC_TBL_OFFSET3:
814             apic->ext_intr_vec_tbl[3].val = op_val;
815             break;
816
817
818             // Action Registers
819         case EOI_OFFSET:
820             // do eoi
821             apic_do_eoi(apic);
822             break;
823
824         case INT_CMD_LO_OFFSET:
825         case INT_CMD_HI_OFFSET:
826             // Unhandled Registers
827
828         case EXT_APIC_CMD_OFFSET:
829         case SEOI_OFFSET:
830         default:
831             PrintError("Write to Unhandled APIC Register: %x\n", (uint32_t)reg_addr);
832             return -1;
833     }
834
835     PrintDebug("Write finished\n");
836
837     return length;
838 }
839
840
841
842 /* Interrupt Controller Functions */
843
844 // returns 1 if an interrupt is pending, 0 otherwise
845 static int apic_intr_pending(void * private_data) {
846     struct vm_device * dev = (struct vm_device *)private_data;
847     struct apic_state * apic = (struct apic_state *)dev->private_data;
848     int req_irq = get_highest_irr(apic);
849     int svc_irq = get_highest_isr(apic);
850
851     if ((req_irq >= 0) && 
852         (req_irq > svc_irq)) {
853         return 1;
854     }
855
856     return 0;
857 }
858
859 static int apic_get_intr_number(void * private_data) {
860     struct vm_device * dev = (struct vm_device *)private_data;
861     struct apic_state * apic = (struct apic_state *)dev->private_data;
862     int req_irq = get_highest_irr(apic);
863     int svc_irq = get_highest_isr(apic);
864
865     if (svc_irq == -1) {
866         return req_irq;
867     } else if (svc_irq < req_irq) {
868         return req_irq;
869     }
870
871     return -1;
872 }
873
874 static int apic_raise_intr(void * private_data, int irq) {
875 #ifdef CRAY_XT
876     // The Seastar is connected directly to the LAPIC via LINT0 on the ICC bus
877
878     if (irq == 238) {
879         struct vm_device * dev = (struct vm_device *)private_data;
880         struct apic_state * apic = (struct apic_state *)dev->private_data;
881
882         return activate_apic_irq(apic, irq);
883     }
884 #endif
885     return 0;
886 }
887
888 static int apic_lower_intr(void * private_data, int irq) {
889     return 0;
890 }
891
892 static int apic_begin_irq(void * private_data, int irq) {
893     struct vm_device * dev = (struct vm_device *)private_data;
894     struct apic_state * apic = (struct apic_state *)dev->private_data;
895     int major_offset = (irq & ~0x00000007) >> 3;
896     int minor_offset = irq & 0x00000007;
897     uchar_t * req_location = apic->int_req_reg + major_offset;
898     uchar_t * svc_location = apic->int_svc_reg + major_offset;
899     uchar_t flag = 0x01 << minor_offset;
900
901     *svc_location |= flag;
902     *req_location &= ~flag;
903
904 #ifdef CRAY_XT
905     if ((irq == 238) || (irq == 239)) {
906         PrintError("APIC: Begin IRQ %d (ISR=%x), (IRR=%x)\n", irq, *svc_location, *req_location);
907     }
908 #endif
909
910     return 0;
911 }
912
913
914
915 int v3_apic_raise_intr(struct vm_device * apic_dev, int intr_num) {
916     struct apic_state * apic = (struct apic_state *)apic_dev->private_data;
917     return activate_apic_irq(apic, intr_num);
918 }
919
920
921
922 /* Timer Functions */
923 static void apic_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * priv_data) {
924     struct vm_device * dev = (struct vm_device *)priv_data;
925     struct apic_state * apic = (struct apic_state *)dev->private_data;
926     // The 32 bit GCC runtime is a pile of shit
927 #ifdef __V3_64BIT__
928     uint64_t tmr_ticks = 0;
929 #else 
930     uint32_t tmr_ticks = 0;
931 #endif
932
933     uchar_t tmr_div = *(uchar_t *)&(apic->tmr_div_cfg.val);
934     uint_t shift_num = 0;
935
936
937     // Check whether this is true:
938     //   -> If the Init count is zero then the timer is disabled
939     //      and doesn't just blitz interrupts to the CPU
940     if ((apic->tmr_init_cnt == 0) || 
941         ( (apic->tmr_vec_tbl.tmr_mode == APIC_TMR_ONESHOT) &&
942           (apic->tmr_cur_cnt == 0))) {
943         //PrintDebug("APIC timer not yet initialized\n");
944         return;
945     }
946
947
948     switch (tmr_div) {
949         case APIC_TMR_DIV1:
950             shift_num = 0;
951             break;
952         case APIC_TMR_DIV2:
953             shift_num = 1;
954             break;
955         case APIC_TMR_DIV4:
956             shift_num = 2;
957             break;
958         case APIC_TMR_DIV8:
959             shift_num = 3;
960             break;
961         case APIC_TMR_DIV16:
962             shift_num = 4;
963             break;
964         case APIC_TMR_DIV32:
965             shift_num = 5;
966             break;
967         case APIC_TMR_DIV64:
968             shift_num = 6;
969             break;
970         case APIC_TMR_DIV128:
971             shift_num = 7;
972             break;
973         default:
974             PrintError("Invalid Timer Divider configuration\n");
975             return;
976     }
977
978     tmr_ticks = cpu_cycles >> shift_num;
979     PrintDebug("Timer Ticks: %p\n", (void *)tmr_ticks);
980
981     if (tmr_ticks < apic->tmr_cur_cnt) {
982         apic->tmr_cur_cnt -= tmr_ticks;
983     } else {
984         tmr_ticks -= apic->tmr_cur_cnt;
985         apic->tmr_cur_cnt = 0;
986
987         // raise irq
988         PrintDebug("Raising APIC Timer interrupt (periodic=%d) (icnt=%d) (div=%d)\n", 
989                    apic->tmr_vec_tbl.tmr_mode, apic->tmr_init_cnt, shift_num);
990
991         if (activate_internal_irq(apic, APIC_TMR_INT) == -1) {
992             PrintError("Could not raise Timer interrupt\n");
993         }
994     
995         if (apic->tmr_vec_tbl.tmr_mode == APIC_TMR_PERIODIC) {
996             tmr_ticks = tmr_ticks % apic->tmr_init_cnt;
997             apic->tmr_cur_cnt = apic->tmr_init_cnt - tmr_ticks;
998         }
999     }
1000
1001
1002 }
1003
1004
1005
1006 static struct intr_ctrl_ops intr_ops = {
1007     .intr_pending = apic_intr_pending,
1008     .get_intr_number = apic_get_intr_number,
1009     .raise_intr = apic_raise_intr,
1010     .begin_irq = apic_begin_irq,
1011     .lower_intr = apic_lower_intr, 
1012 };
1013
1014
1015 static struct vm_timer_ops timer_ops = {
1016     .update_time = apic_update_time,
1017 };
1018
1019
1020 static int apic_init(struct vm_device * dev) {
1021     struct guest_info * info = dev->vm;
1022     struct apic_state * apic = (struct apic_state *)(dev->private_data);
1023
1024     v3_register_intr_controller(dev->vm, &intr_ops, dev);
1025     v3_add_timer(dev->vm, &timer_ops, dev);
1026
1027     init_apic_state(apic);
1028
1029     v3_hook_msr(info, BASE_ADDR_MSR, read_apic_msr, write_apic_msr, dev);
1030
1031     v3_hook_full_mem(info, apic->base_addr, apic->base_addr + PAGE_SIZE_4KB, apic_read, apic_write, dev);
1032
1033     return 0;
1034 }
1035
1036 static int apic_deinit(struct vm_device * dev) {
1037     struct guest_info * info = dev->vm;
1038
1039     v3_unhook_msr(info, BASE_ADDR_MSR);
1040
1041     return 0;
1042 }
1043
1044
1045 static struct vm_device_ops dev_ops = {
1046     .init = apic_init,
1047     .deinit = apic_deinit,
1048     .reset = NULL,
1049     .start = NULL,
1050     .stop = NULL,
1051 };
1052
1053
1054 struct vm_device * v3_create_apic() {
1055     PrintDebug("Creating APIC\n");
1056
1057     struct apic_state * apic = (struct apic_state *)V3_Malloc(sizeof(struct apic_state));
1058
1059     struct vm_device * device = v3_create_device("APIC", &dev_ops, apic);
1060   
1061     return device;
1062 }