7
var APIC_LOG_VERBOSE = false;
10
var APIC_ADDRESS = 0xFEE00000;
13
var APIC_TIMER_MODE_MASK = 3 << 17;
16
var APIC_TIMER_MODE_ONE_SHOT = 0;
19
var APIC_TIMER_MODE_PERIODIC = 1 << 17;
22
var APIC_TIMER_MODE_TSC = 2 << 17;
38
var DESTINATION_MODES = ["physical", "logical"];
52
this.timer_divider = 0;
53
this.timer_divider_shift = 1;
54
this.timer_initial_count = 0;
55
this.timer_current_count = 0;
57
this.next_tick = v86.microtick();
59
this.lvt_timer = IOAPIC_CONFIG_MASKED;
60
this.lvt_perf_counter = IOAPIC_CONFIG_MASKED;
61
this.lvt_int0 = IOAPIC_CONFIG_MASKED;
62
this.lvt_int1 = IOAPIC_CONFIG_MASKED;
63
this.lvt_error = IOAPIC_CONFIG_MASKED;
69
this.irr = new Int32Array(8);
70
this.isr = new Int32Array(8);
71
this.tmr = new Int32Array(8);
73
this.spurious_vector = 0xFE;
74
this.destination_format = -1;
75
this.local_destination = 0;
80
cpu.io.mmap_register(APIC_ADDRESS, 0x100000,
83
dbg_log("Unsupported read8 from apic: " + h(addr >>> 0), LOG_APIC);
86
return this.read32(addr) >> (off * 8) & 0xFF;
90
dbg_log("Unsupported write8 from apic: " + h(addr) + " <- " + h(value), LOG_APIC);
94
(addr) => this.read32(addr),
95
(addr, value) => this.write32(addr, value)
99
APIC.prototype.read32 = function(addr)
101
addr = addr - APIC_ADDRESS | 0;
106
dbg_log("APIC read id", LOG_APIC);
111
dbg_log("APIC read version", LOG_APIC);
115
APIC_LOG_VERBOSE && dbg_log("APIC read tpr", LOG_APIC);
119
dbg_log("Read local destination", LOG_APIC);
120
return this.local_destination;
123
dbg_log("Read destination format", LOG_APIC);
124
return this.destination_format;
127
return this.spurious_vector;
137
var index = addr - 0x100 >> 4;
138
dbg_log("Read isr " + index + ": " + h(this.isr[index] >>> 0, 8), LOG_APIC);
139
return this.isr[index];
149
var index = addr - 0x180 >> 4;
150
dbg_log("Read tmr " + index + ": " + h(this.tmr[index] >>> 0, 8), LOG_APIC);
151
return this.tmr[index];
161
var index = addr - 0x200 >> 4;
162
dbg_log("Read irr " + index + ": " + h(this.irr[index] >>> 0, 8), LOG_APIC);
163
return this.irr[index];
166
dbg_log("Read error: " + h(this.read_error >>> 0, 8), LOG_APIC);
167
return this.read_error;
170
APIC_LOG_VERBOSE && dbg_log("APIC read icr0", LOG_APIC);
174
dbg_log("APIC read icr1", LOG_APIC);
178
dbg_log("read timer lvt", LOG_APIC);
179
return this.lvt_timer;
182
dbg_log("read lvt perf counter", LOG_APIC);
183
return this.lvt_perf_counter;
186
dbg_log("read lvt int0", LOG_APIC);
187
return this.lvt_int0;
190
dbg_log("read lvt int1", LOG_APIC);
191
return this.lvt_int1;
194
dbg_log("read lvt error", LOG_APIC);
195
return this.lvt_error;
199
dbg_log("read timer divider", LOG_APIC);
200
return this.timer_divider;
203
dbg_log("read timer initial count", LOG_APIC);
204
return this.timer_initial_count;
207
dbg_log("read timer current count: " + h(this.timer_current_count >>> 0, 8), LOG_APIC);
208
return this.timer_current_count;
211
dbg_log("APIC read " + h(addr), LOG_APIC);
217
APIC.prototype.write32 = function(addr, value)
219
addr = addr - APIC_ADDRESS | 0;
225
dbg_log("APIC write version: " + h(value >>> 0, 8) + ", ignored", LOG_APIC);
229
APIC_LOG_VERBOSE && dbg_log("Set tpr: " + h(value & 0xFF, 2), LOG_APIC);
230
this.tpr = value & 0xFF;
235
var highest_isr = this.highest_isr();
236
if(highest_isr !== -1)
238
APIC_LOG_VERBOSE && dbg_log("eoi: " + h(value >>> 0, 8) + " for vector " + h(highest_isr), LOG_APIC);
239
this.register_clear_bit(this.isr, highest_isr);
240
if(this.register_get_bit(this.tmr, highest_isr))
243
this.cpu.devices.ioapic.remote_eoi(highest_isr);
249
dbg_log("Bad eoi: No isr set", LOG_APIC);
254
dbg_log("Set local destination: " + h(value >>> 0, 8), LOG_APIC);
255
this.local_destination = value & 0xFF000000;
259
dbg_log("Set destination format: " + h(value >>> 0, 8), LOG_APIC);
260
this.destination_format = value | 0xFFFFFF;
264
dbg_log("Set spurious vector: " + h(value >>> 0, 8), LOG_APIC);
265
this.spurious_vector = value;
270
dbg_log("Write error: " + h(value >>> 0, 8), LOG_APIC);
271
this.read_error = this.error;
276
var vector = value & 0xFF;
277
var delivery_mode = value >> 8 & 7;
278
var destination_mode = value >> 11 & 1;
279
var is_level = value >> 15 & 1;
280
var destination_shorthand = value >> 18 & 3;
281
var destination = this.icr1 >>> 24;
282
dbg_log("APIC write icr0: " + h(value, 8) + " vector=" + h(vector, 2) + " " +
283
"destination_mode=" + DESTINATION_MODES[destination_mode] + " delivery_mode=" + DELIVERY_MODES[delivery_mode] + " " +
284
"destination_shorthand=" + ["no", "self", "all with self", "all without self"][destination_shorthand], LOG_APIC);
289
if(destination_shorthand === 0)
292
this.route(vector, delivery_mode, is_level, destination, destination_mode);
294
else if(destination_shorthand === 1)
297
this.deliver(vector, IOAPIC_DELIVERY_FIXED, is_level);
299
else if(destination_shorthand === 2)
302
this.deliver(vector, delivery_mode, is_level);
304
else if(destination_shorthand === 3)
315
dbg_log("APIC write icr1: " + h(value >>> 0, 8), LOG_APIC);
320
dbg_log("timer lvt: " + h(value >>> 0, 8), LOG_APIC);
321
this.lvt_timer = value;
325
dbg_log("lvt perf counter: " + h(value >>> 0, 8), LOG_APIC);
326
this.lvt_perf_counter = value;
330
dbg_log("lvt int0: " + h(value >>> 0, 8), LOG_APIC);
331
this.lvt_int0 = value;
335
dbg_log("lvt int1: " + h(value >>> 0, 8), LOG_APIC);
336
this.lvt_int1 = value;
340
dbg_log("lvt error: " + h(value >>> 0, 8), LOG_APIC);
341
this.lvt_error = value;
345
dbg_log("timer divider: " + h(value >>> 0, 8), LOG_APIC);
346
this.timer_divider = value;
348
var divide_shift = value & 0b11 | (value & 0b1000) >> 1;
349
this.timer_divider_shift = divide_shift === 0b111 ? 0 : divide_shift + 1;
353
dbg_log("timer initial: " + h(value >>> 0, 8), LOG_APIC);
354
this.timer_initial_count = value >>> 0;
355
this.timer_current_count = value >>> 0;
357
this.next_tick = v86.microtick();
358
this.timer_active = true;
362
dbg_log("timer current: " + h(value >>> 0, 8), LOG_APIC);
363
dbg_assert(false, "read-only register");
367
dbg_log("APIC write32 " + h(addr) + " <- " + h(value >>> 0, 8), LOG_APIC);
372
APIC.prototype.timer = function(now)
374
if(this.timer_current_count === 0)
379
const freq = APIC_TIMER_FREQ / (1 << this.timer_divider_shift);
381
const steps = (now - this.next_tick) * freq >>> 0;
383
this.next_tick += steps / freq;
384
this.timer_current_count -= steps;
386
if(this.timer_current_count <= 0)
388
var mode = this.lvt_timer & APIC_TIMER_MODE_MASK;
390
if(mode === APIC_TIMER_MODE_PERIODIC)
392
this.timer_current_count = this.timer_current_count % this.timer_initial_count;
394
if(this.timer_current_count <= 0)
396
this.timer_current_count += this.timer_initial_count;
398
dbg_assert(this.timer_current_count !== 0);
400
if((this.lvt_timer & IOAPIC_CONFIG_MASKED) === 0)
402
this.deliver(this.lvt_timer & 0xFF, IOAPIC_DELIVERY_FIXED, false);
405
else if(mode === APIC_TIMER_MODE_ONE_SHOT)
407
this.timer_current_count = 0;
408
dbg_log("APIC timer one shot end", LOG_APIC);
410
if((this.lvt_timer & IOAPIC_CONFIG_MASKED) === 0)
412
this.deliver(this.lvt_timer & 0xFF, IOAPIC_DELIVERY_FIXED, false);
417
return Math.max(0, this.timer_current_count / freq);
420
APIC.prototype.route = function(vector, mode, is_level, destination, destination_mode)
423
this.deliver(vector, mode, is_level);
426
APIC.prototype.deliver = function(vector, mode, is_level)
428
APIC_LOG_VERBOSE && dbg_log("Deliver " + h(vector, 2) + " mode=" + mode + " level=" + is_level, LOG_APIC);
430
if(mode === IOAPIC_DELIVERY_INIT)
436
if(mode === IOAPIC_DELIVERY_NMI)
442
if(vector < 0x10 || vector === 0xFF)
444
dbg_assert(false, "TODO: Invalid vector");
447
if(this.register_get_bit(this.irr, vector))
449
dbg_log("Not delivered: irr already set, vector=" + h(vector, 2), LOG_APIC);
453
this.register_set_bit(this.irr, vector);
457
this.register_set_bit(this.tmr, vector);
461
this.register_clear_bit(this.tmr, vector);
467
APIC.prototype.highest_irr = function()
469
var highest = this.register_get_highest_bit(this.irr);
470
dbg_assert(highest !== 0xFF);
471
dbg_assert(highest >= 0x10 || highest === -1);
475
APIC.prototype.highest_isr = function()
477
var highest = this.register_get_highest_bit(this.isr);
478
dbg_assert(highest !== 0xFF);
479
dbg_assert(highest >= 0x10 || highest === -1);
483
APIC.prototype.check_vector = function()
485
var highest_irr = this.highest_irr();
487
if(highest_irr === -1)
492
var highest_isr = this.highest_isr();
494
if(highest_isr >= highest_irr)
496
APIC_LOG_VERBOSE && dbg_log("Higher isr, isr=" + h(highest_isr) + " irr=" + h(highest_irr), LOG_APIC);
500
if((highest_irr & 0xF0) <= (this.tpr & 0xF0))
502
APIC_LOG_VERBOSE && dbg_log("Higher tpr, tpr=" + h(this.tpr & 0xF0) + " irr=" + h(highest_irr), LOG_APIC);
506
this.cpu.handle_irqs();
509
APIC.prototype.acknowledge_irq = function()
511
var highest_irr = this.highest_irr();
513
if(highest_irr === -1)
519
var highest_isr = this.highest_isr();
521
if(highest_isr >= highest_irr)
523
APIC_LOG_VERBOSE && dbg_log("Higher isr, isr=" + h(highest_isr) + " irr=" + h(highest_irr), LOG_APIC);
527
if((highest_irr & 0xF0) <= (this.tpr & 0xF0))
529
APIC_LOG_VERBOSE && dbg_log("Higher tpr, tpr=" + h(this.tpr & 0xF0) + " irr=" + h(highest_irr), LOG_APIC);
533
this.register_clear_bit(this.irr, highest_irr);
534
this.register_set_bit(this.isr, highest_irr);
536
APIC_LOG_VERBOSE && dbg_log("Calling vector " + h(highest_irr), LOG_APIC);
537
this.cpu.pic_call_irq(highest_irr);
542
APIC.prototype.get_state = function()
546
state[0] = this.apic_id;
547
state[1] = this.timer_divider;
548
state[2] = this.timer_divider_shift;
549
state[3] = this.timer_initial_count;
550
state[4] = this.timer_current_count;
551
state[5] = this.next_tick;
552
state[6] = this.lvt_timer;
553
state[7] = this.lvt_perf_counter;
554
state[8] = this.lvt_int0;
555
state[9] = this.lvt_int1;
556
state[10] = this.lvt_error;
557
state[11] = this.tpr;
558
state[12] = this.icr0;
559
state[13] = this.icr1;
560
state[14] = this.irr;
561
state[15] = this.isr;
562
state[16] = this.tmr;
563
state[17] = this.spurious_vector;
564
state[18] = this.destination_format;
565
state[19] = this.local_destination;
566
state[20] = this.error;
567
state[21] = this.read_error;
572
APIC.prototype.set_state = function(state)
574
this.apic_id = state[0];
575
this.timer_divider = state[1];
576
this.timer_divider_shift = state[2];
577
this.timer_initial_count = state[3];
578
this.timer_current_count = state[4];
579
this.next_tick = state[5];
580
this.lvt_timer = state[6];
581
this.lvt_perf_counter = state[7];
582
this.lvt_int0 = state[8];
583
this.lvt_int1 = state[9];
584
this.lvt_error = state[10];
585
this.tpr = state[11];
586
this.icr0 = state[12];
587
this.icr1 = state[13];
588
this.irr = state[14];
589
this.isr = state[15];
590
this.tmr = state[16];
591
this.spurious_vector = state[17];
592
this.destination_format = state[18];
593
this.local_destination = state[19];
594
this.error = state[20];
595
this.read_error = state[21];
599
APIC.prototype.register_get_bit = function(v, bit)
601
dbg_assert(bit >= 0 && bit < 256);
602
return v[bit >> 5] >> (bit & 31) & 1;
605
APIC.prototype.register_set_bit = function(v, bit)
607
dbg_assert(bit >= 0 && bit < 256);
608
v[bit >> 5] |= 1 << (bit & 31);
611
APIC.prototype.register_clear_bit = function(v, bit)
613
dbg_assert(bit >= 0 && bit < 256);
614
v[bit >> 5] &= ~(1 << (bit & 31));
617
APIC.prototype.register_get_highest_bit = function(v)
619
for(var i = 7; i >= 0; i--)
625
return v86util.int_log2(word >>> 0) | i << 5;