@aptre/v86 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/LICENSE.MIT +22 -0
- package/Readme.md +237 -0
- package/dist/v86.browser.js +26666 -0
- package/dist/v86.browser.js.map +7 -0
- package/dist/v86.js +26632 -0
- package/dist/v86.js.map +7 -0
- package/gen/generate_analyzer.ts +512 -0
- package/gen/generate_interpreter.ts +522 -0
- package/gen/generate_jit.ts +624 -0
- package/gen/rust_ast.ts +107 -0
- package/gen/util.ts +35 -0
- package/gen/x86_table.ts +1836 -0
- package/lib/9p.ts +1547 -0
- package/lib/filesystem.ts +1879 -0
- package/lib/marshall.ts +168 -0
- package/lib/softfloat/softfloat.c +32501 -0
- package/lib/zstd/zstddeclib.c +13520 -0
- package/package.json +75 -0
- package/src/acpi.ts +267 -0
- package/src/browser/dummy_screen.ts +106 -0
- package/src/browser/fake_network.ts +1771 -0
- package/src/browser/fetch_network.ts +361 -0
- package/src/browser/filestorage.ts +124 -0
- package/src/browser/inbrowser_network.ts +57 -0
- package/src/browser/keyboard.ts +564 -0
- package/src/browser/main.ts +3415 -0
- package/src/browser/mouse.ts +255 -0
- package/src/browser/network.ts +142 -0
- package/src/browser/print_stats.ts +336 -0
- package/src/browser/screen.ts +978 -0
- package/src/browser/serial.ts +316 -0
- package/src/browser/speaker.ts +1223 -0
- package/src/browser/starter.ts +1688 -0
- package/src/browser/wisp_network.ts +332 -0
- package/src/browser/worker_bus.ts +64 -0
- package/src/buffer.ts +652 -0
- package/src/bus.ts +78 -0
- package/src/const.ts +128 -0
- package/src/cpu.ts +2891 -0
- package/src/dma.ts +474 -0
- package/src/elf.ts +251 -0
- package/src/floppy.ts +1778 -0
- package/src/ide.ts +3455 -0
- package/src/io.ts +504 -0
- package/src/iso9660.ts +317 -0
- package/src/kernel.ts +250 -0
- package/src/lib.ts +645 -0
- package/src/log.ts +149 -0
- package/src/main.ts +199 -0
- package/src/ne2k.ts +1589 -0
- package/src/pci.ts +815 -0
- package/src/pit.ts +406 -0
- package/src/ps2.ts +820 -0
- package/src/rtc.ts +537 -0
- package/src/rust/analysis.rs +101 -0
- package/src/rust/codegen.rs +2660 -0
- package/src/rust/config.rs +3 -0
- package/src/rust/control_flow.rs +425 -0
- package/src/rust/cpu/apic.rs +658 -0
- package/src/rust/cpu/arith.rs +1207 -0
- package/src/rust/cpu/call_indirect.rs +2 -0
- package/src/rust/cpu/cpu.rs +4501 -0
- package/src/rust/cpu/fpu.rs +923 -0
- package/src/rust/cpu/global_pointers.rs +112 -0
- package/src/rust/cpu/instructions.rs +2486 -0
- package/src/rust/cpu/instructions_0f.rs +5261 -0
- package/src/rust/cpu/ioapic.rs +316 -0
- package/src/rust/cpu/memory.rs +351 -0
- package/src/rust/cpu/misc_instr.rs +613 -0
- package/src/rust/cpu/mod.rs +16 -0
- package/src/rust/cpu/modrm.rs +133 -0
- package/src/rust/cpu/pic.rs +402 -0
- package/src/rust/cpu/sse_instr.rs +361 -0
- package/src/rust/cpu/string.rs +701 -0
- package/src/rust/cpu/vga.rs +175 -0
- package/src/rust/cpu_context.rs +69 -0
- package/src/rust/dbg.rs +98 -0
- package/src/rust/gen/analyzer.rs +3807 -0
- package/src/rust/gen/analyzer0f.rs +3992 -0
- package/src/rust/gen/interpreter.rs +4447 -0
- package/src/rust/gen/interpreter0f.rs +5404 -0
- package/src/rust/gen/jit.rs +5080 -0
- package/src/rust/gen/jit0f.rs +5547 -0
- package/src/rust/gen/mod.rs +14 -0
- package/src/rust/jit.rs +2443 -0
- package/src/rust/jit_instructions.rs +7881 -0
- package/src/rust/js_api.rs +6 -0
- package/src/rust/leb.rs +46 -0
- package/src/rust/lib.rs +29 -0
- package/src/rust/modrm.rs +330 -0
- package/src/rust/opstats.rs +249 -0
- package/src/rust/page.rs +15 -0
- package/src/rust/paging.rs +25 -0
- package/src/rust/prefix.rs +15 -0
- package/src/rust/profiler.rs +155 -0
- package/src/rust/regs.rs +38 -0
- package/src/rust/softfloat.rs +286 -0
- package/src/rust/state_flags.rs +27 -0
- package/src/rust/wasmgen/mod.rs +2 -0
- package/src/rust/wasmgen/wasm_builder.rs +1047 -0
- package/src/rust/wasmgen/wasm_opcodes.rs +221 -0
- package/src/rust/zstd.rs +105 -0
- package/src/sb16.ts +1928 -0
- package/src/state.ts +359 -0
- package/src/uart.ts +472 -0
- package/src/vga.ts +2791 -0
- package/src/virtio.ts +1756 -0
- package/src/virtio_balloon.ts +273 -0
- package/src/virtio_console.ts +372 -0
- package/src/virtio_net.ts +326 -0
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
// See Intel's System Programming Guide
|
|
2
|
+
|
|
3
|
+
use crate::cpu::{cpu::js, global_pointers::acpi_enabled, ioapic};
|
|
4
|
+
use std::sync::{Mutex, MutexGuard};
|
|
5
|
+
|
|
6
|
+
const APIC_LOG_VERBOSE: bool = false;
|
|
7
|
+
|
|
8
|
+
// should probably be kept in sync with TSC_RATE in cpu.rs
|
|
9
|
+
const APIC_TIMER_FREQ: f64 = 1.0 * 1000.0 * 1000.0;
|
|
10
|
+
|
|
11
|
+
const APIC_TIMER_MODE_MASK: u32 = 3 << 17;
|
|
12
|
+
|
|
13
|
+
const APIC_TIMER_MODE_ONE_SHOT: u32 = 0;
|
|
14
|
+
const APIC_TIMER_MODE_PERIODIC: u32 = 1 << 17;
|
|
15
|
+
|
|
16
|
+
const _APIC_TIMER_MODE_TSC: u32 = 2 << 17;
|
|
17
|
+
|
|
18
|
+
const DELIVERY_MODES: [&str; 8] = [
|
|
19
|
+
"Fixed (0)",
|
|
20
|
+
"Lowest Prio (1)",
|
|
21
|
+
"SMI (2)",
|
|
22
|
+
"Reserved (3)",
|
|
23
|
+
"NMI (4)",
|
|
24
|
+
"INIT (5)",
|
|
25
|
+
"Reserved (6)",
|
|
26
|
+
"ExtINT (7)",
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const DESTINATION_MODES: [&str; 2] = ["physical", "logical"];
|
|
30
|
+
|
|
31
|
+
const IOAPIC_CONFIG_MASKED: u32 = 0x10000;
|
|
32
|
+
|
|
33
|
+
const IOAPIC_DELIVERY_INIT: u8 = 5;
|
|
34
|
+
const IOAPIC_DELIVERY_NMI: u8 = 4;
|
|
35
|
+
const IOAPIC_DELIVERY_FIXED: u8 = 0;
|
|
36
|
+
|
|
37
|
+
// keep in sync with cpu.js
|
|
38
|
+
#[allow(dead_code)]
|
|
39
|
+
const APIC_STRUCT_SIZE: usize = 4 * 46;
|
|
40
|
+
|
|
41
|
+
// Note: JavaScript (cpu.get_state_apic) depens on this layout
|
|
42
|
+
const _: () = assert!(std::mem::offset_of!(Apic, timer_last_tick) == 6 * 4);
|
|
43
|
+
const _: () = assert!(std::mem::offset_of!(Apic, lvt_timer) == 8 * 4);
|
|
44
|
+
const _: () = assert!(std::mem::offset_of!(Apic, lvt_perf_counter) == 9 * 4);
|
|
45
|
+
const _: () = assert!(std::mem::offset_of!(Apic, icr0) == 14 * 4);
|
|
46
|
+
const _: () = assert!(std::mem::offset_of!(Apic, icr1) == 15 * 4);
|
|
47
|
+
const _: () = assert!(std::mem::offset_of!(Apic, irr) == 16 * 4);
|
|
48
|
+
const _: () = assert!(std::mem::offset_of!(Apic, isr) == 24 * 4);
|
|
49
|
+
const _: () = assert!(std::mem::offset_of!(Apic, tmr) == 32 * 4);
|
|
50
|
+
const _: () = assert!(std::mem::offset_of!(Apic, spurious_vector) == 40 * 4);
|
|
51
|
+
const _: () = assert!(std::mem::offset_of!(Apic, lvt_thermal_sensor) == 45 * 4);
|
|
52
|
+
const _: () = assert!(std::mem::size_of::<Apic>() == APIC_STRUCT_SIZE);
|
|
53
|
+
#[repr(C)]
|
|
54
|
+
pub struct Apic {
|
|
55
|
+
apic_id: u32,
|
|
56
|
+
timer_divider: u32,
|
|
57
|
+
timer_divider_shift: u32,
|
|
58
|
+
timer_initial_count: u32,
|
|
59
|
+
timer_current_count: u32,
|
|
60
|
+
timer_last_tick: f64,
|
|
61
|
+
lvt_timer: u32,
|
|
62
|
+
lvt_perf_counter: u32,
|
|
63
|
+
lvt_int0: u32,
|
|
64
|
+
lvt_int1: u32,
|
|
65
|
+
lvt_error: u32,
|
|
66
|
+
tpr: u32,
|
|
67
|
+
icr0: u32,
|
|
68
|
+
icr1: u32,
|
|
69
|
+
irr: [u32; 8],
|
|
70
|
+
isr: [u32; 8],
|
|
71
|
+
tmr: [u32; 8],
|
|
72
|
+
spurious_vector: u32,
|
|
73
|
+
destination_format: u32,
|
|
74
|
+
local_destination: u32,
|
|
75
|
+
error: u32,
|
|
76
|
+
read_error: u32,
|
|
77
|
+
lvt_thermal_sensor: u32,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static APIC: Mutex<Apic> = Mutex::new(Apic {
|
|
81
|
+
apic_id: 0,
|
|
82
|
+
timer_divider: 0,
|
|
83
|
+
timer_divider_shift: 1,
|
|
84
|
+
timer_initial_count: 0,
|
|
85
|
+
timer_current_count: 0,
|
|
86
|
+
timer_last_tick: 0.0,
|
|
87
|
+
lvt_timer: IOAPIC_CONFIG_MASKED,
|
|
88
|
+
lvt_thermal_sensor: IOAPIC_CONFIG_MASKED,
|
|
89
|
+
lvt_perf_counter: IOAPIC_CONFIG_MASKED,
|
|
90
|
+
lvt_int0: IOAPIC_CONFIG_MASKED,
|
|
91
|
+
lvt_int1: IOAPIC_CONFIG_MASKED,
|
|
92
|
+
lvt_error: IOAPIC_CONFIG_MASKED,
|
|
93
|
+
tpr: 0,
|
|
94
|
+
icr0: 0,
|
|
95
|
+
icr1: 0,
|
|
96
|
+
irr: [0; 8],
|
|
97
|
+
isr: [0; 8],
|
|
98
|
+
tmr: [0; 8],
|
|
99
|
+
spurious_vector: 0xFE,
|
|
100
|
+
destination_format: !0,
|
|
101
|
+
local_destination: 0,
|
|
102
|
+
error: 0,
|
|
103
|
+
read_error: 0,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
pub fn get_apic() -> MutexGuard<'static, Apic> { APIC.try_lock().unwrap() }
|
|
107
|
+
|
|
108
|
+
#[no_mangle]
|
|
109
|
+
pub fn get_apic_addr() -> u32 { &raw mut *get_apic() as u32 }
|
|
110
|
+
|
|
111
|
+
pub fn read32(addr: u32) -> u32 {
|
|
112
|
+
if unsafe { !*acpi_enabled } {
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
read32_internal(&mut get_apic(), addr)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
fn read32_internal(apic: &mut Apic, addr: u32) -> u32 {
|
|
119
|
+
match addr {
|
|
120
|
+
0x20 => {
|
|
121
|
+
dbg_log!("APIC read id");
|
|
122
|
+
apic.apic_id
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
0x30 => {
|
|
126
|
+
// version
|
|
127
|
+
dbg_log!("APIC read version");
|
|
128
|
+
0x50014
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
0x80 => {
|
|
132
|
+
if APIC_LOG_VERBOSE {
|
|
133
|
+
dbg_log!("APIC read tpr");
|
|
134
|
+
}
|
|
135
|
+
apic.tpr
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
0xB0 => {
|
|
139
|
+
// write-only (written by DSL)
|
|
140
|
+
if APIC_LOG_VERBOSE {
|
|
141
|
+
dbg_log!("APIC read eoi register");
|
|
142
|
+
}
|
|
143
|
+
0
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
0xD0 => {
|
|
147
|
+
dbg_log!("Read local destination");
|
|
148
|
+
apic.local_destination
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
0xE0 => {
|
|
152
|
+
dbg_log!("Read destination format");
|
|
153
|
+
apic.destination_format
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
0xF0 => apic.spurious_vector,
|
|
157
|
+
|
|
158
|
+
0x100 | 0x110 | 0x120 | 0x130 | 0x140 | 0x150 | 0x160 | 0x170 => {
|
|
159
|
+
let index = ((addr - 0x100) >> 4) as usize;
|
|
160
|
+
dbg_log!("Read isr {}: {:08x}", index, apic.isr[index] as u32);
|
|
161
|
+
apic.isr[index]
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
0x180 | 0x190 | 0x1A0 | 0x1B0 | 0x1C0 | 0x1D0 | 0x1E0 | 0x1F0 => {
|
|
165
|
+
let index = ((addr - 0x180) >> 4) as usize;
|
|
166
|
+
dbg_log!("Read tmr {}: {:08x}", index, apic.tmr[index] as u32);
|
|
167
|
+
apic.tmr[index]
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
0x200 | 0x210 | 0x220 | 0x230 | 0x240 | 0x250 | 0x260 | 0x270 => {
|
|
171
|
+
let index = ((addr - 0x200) >> 4) as usize;
|
|
172
|
+
dbg_log!("Read irr {}: {:08x}", index, apic.irr[index] as u32);
|
|
173
|
+
apic.irr[index]
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
0x280 => {
|
|
177
|
+
dbg_log!("Read error: {:08x}", apic.read_error);
|
|
178
|
+
apic.read_error
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
0x300 => {
|
|
182
|
+
if APIC_LOG_VERBOSE {
|
|
183
|
+
dbg_log!("APIC read icr0");
|
|
184
|
+
}
|
|
185
|
+
apic.icr0
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
0x310 => {
|
|
189
|
+
dbg_log!("APIC read icr1");
|
|
190
|
+
apic.icr1
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
0x320 => {
|
|
194
|
+
if APIC_LOG_VERBOSE {
|
|
195
|
+
dbg_log!("read timer lvt");
|
|
196
|
+
}
|
|
197
|
+
apic.lvt_timer
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
0x330 => {
|
|
201
|
+
dbg_log!("read lvt thermal sensor");
|
|
202
|
+
apic.lvt_thermal_sensor
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
0x340 => {
|
|
206
|
+
dbg_log!("read lvt perf counter");
|
|
207
|
+
apic.lvt_perf_counter
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
0x350 => {
|
|
211
|
+
dbg_log!("read lvt int0");
|
|
212
|
+
apic.lvt_int0
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
0x360 => {
|
|
216
|
+
dbg_log!("read lvt int1");
|
|
217
|
+
apic.lvt_int1
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
0x370 => {
|
|
221
|
+
dbg_log!("read lvt error");
|
|
222
|
+
apic.lvt_error
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
0x3E0 => {
|
|
226
|
+
// divider
|
|
227
|
+
dbg_log!("read timer divider");
|
|
228
|
+
apic.timer_divider
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
0x380 => {
|
|
232
|
+
dbg_log!("read timer initial count");
|
|
233
|
+
apic.timer_initial_count
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
0x390 => {
|
|
237
|
+
let now = unsafe { js::microtick() };
|
|
238
|
+
if apic.timer_last_tick > now {
|
|
239
|
+
// should only happen after restore_state
|
|
240
|
+
dbg_log!("warning: APIC last_tick is in the future, resetting");
|
|
241
|
+
apic.timer_last_tick = now;
|
|
242
|
+
}
|
|
243
|
+
let diff = now - apic.timer_last_tick;
|
|
244
|
+
let diff_in_ticks = diff * APIC_TIMER_FREQ / (1 << apic.timer_divider_shift) as f64;
|
|
245
|
+
dbg_assert!(diff_in_ticks >= 0.0);
|
|
246
|
+
let diff_in_ticks = diff_in_ticks as u64;
|
|
247
|
+
let result = if diff_in_ticks < apic.timer_initial_count as u64 {
|
|
248
|
+
apic.timer_initial_count - diff_in_ticks as u32
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
let mode = apic.lvt_timer & APIC_TIMER_MODE_MASK;
|
|
252
|
+
if mode == APIC_TIMER_MODE_PERIODIC {
|
|
253
|
+
apic.timer_initial_count
|
|
254
|
+
- (diff_in_ticks % (apic.timer_initial_count as u64 + 1)) as u32
|
|
255
|
+
}
|
|
256
|
+
else if mode == APIC_TIMER_MODE_ONE_SHOT {
|
|
257
|
+
0
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
dbg_assert!(false, "apic unimplemented timer mode: {:x}", mode);
|
|
261
|
+
0
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
if APIC_LOG_VERBOSE {
|
|
265
|
+
dbg_log!("read timer current count: {}", result);
|
|
266
|
+
}
|
|
267
|
+
result
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
_ => {
|
|
271
|
+
dbg_log!("APIC read {:x}", addr);
|
|
272
|
+
dbg_assert!(false);
|
|
273
|
+
0
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
pub fn write32(addr: u32, value: u32) {
|
|
279
|
+
if unsafe { !*acpi_enabled } {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
write32_internal(&mut get_apic(), addr, value)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
fn write32_internal(apic: &mut Apic, addr: u32, value: u32) {
|
|
286
|
+
match addr {
|
|
287
|
+
0x20 => {
|
|
288
|
+
dbg_log!("APIC write id: {:08x}", value >> 8);
|
|
289
|
+
apic.apic_id = value;
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
0x30 => {
|
|
293
|
+
// version
|
|
294
|
+
dbg_log!("APIC write version: {:08x}, ignored", value);
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
0x80 => {
|
|
298
|
+
if APIC_LOG_VERBOSE {
|
|
299
|
+
dbg_log!("Set tpr: {:02x}", value & 0xFF);
|
|
300
|
+
}
|
|
301
|
+
apic.tpr = value & 0xFF;
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
0xB0 => {
|
|
305
|
+
if let Some(highest_isr) = highest_isr(apic) {
|
|
306
|
+
if APIC_LOG_VERBOSE {
|
|
307
|
+
dbg_log!("eoi: {:08x} for vector {:x}", value, highest_isr);
|
|
308
|
+
}
|
|
309
|
+
register_clear_bit(&mut apic.isr, highest_isr);
|
|
310
|
+
if register_get_bit(&apic.tmr, highest_isr) {
|
|
311
|
+
// Send eoi to all IO APICs
|
|
312
|
+
ioapic::remote_eoi(apic, highest_isr);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
dbg_log!("Bad eoi: No isr set");
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
0xD0 => {
|
|
321
|
+
dbg_log!("Set local destination: {:08x}", value);
|
|
322
|
+
apic.local_destination = value & 0xFF000000;
|
|
323
|
+
},
|
|
324
|
+
|
|
325
|
+
0xE0 => {
|
|
326
|
+
dbg_log!("Set destination format: {:08x}", value);
|
|
327
|
+
apic.destination_format = value | 0xFFFFFF;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
0xF0 => {
|
|
331
|
+
dbg_log!("Set spurious vector: {:08x}", value);
|
|
332
|
+
apic.spurious_vector = value;
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
0x280 => {
|
|
336
|
+
// updated readable error register with real error
|
|
337
|
+
dbg_log!("Write error: {:08x}", value);
|
|
338
|
+
apic.read_error = apic.error;
|
|
339
|
+
apic.error = 0;
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
0x300 => {
|
|
343
|
+
let vector = (value & 0xFF) as u8;
|
|
344
|
+
let delivery_mode = ((value >> 8) & 7) as u8;
|
|
345
|
+
let destination_mode = ((value >> 11) & 1) as u8;
|
|
346
|
+
let is_level = value & ioapic::IOAPIC_CONFIG_TRIGGER_MODE_LEVEL
|
|
347
|
+
== ioapic::IOAPIC_CONFIG_TRIGGER_MODE_LEVEL;
|
|
348
|
+
let destination_shorthand = (value >> 18) & 3;
|
|
349
|
+
let destination = (apic.icr1 >> 24) as u8;
|
|
350
|
+
dbg_log!(
|
|
351
|
+
"APIC write icr0: {:08x} vector={:02x} destination_mode={} delivery_mode={} destination_shorthand={}",
|
|
352
|
+
value,
|
|
353
|
+
vector,
|
|
354
|
+
DESTINATION_MODES[destination_mode as usize],
|
|
355
|
+
DELIVERY_MODES[delivery_mode as usize],
|
|
356
|
+
["no", "self", "all with self", "all without self"][destination_shorthand as usize]
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
let mut value = value;
|
|
360
|
+
value &= !(1 << 12);
|
|
361
|
+
apic.icr0 = value;
|
|
362
|
+
|
|
363
|
+
if destination_shorthand == 0 {
|
|
364
|
+
// no shorthand
|
|
365
|
+
route(
|
|
366
|
+
apic,
|
|
367
|
+
vector,
|
|
368
|
+
delivery_mode,
|
|
369
|
+
is_level,
|
|
370
|
+
destination,
|
|
371
|
+
destination_mode,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
else if destination_shorthand == 1 {
|
|
375
|
+
// self
|
|
376
|
+
deliver(apic, vector, IOAPIC_DELIVERY_FIXED, is_level);
|
|
377
|
+
}
|
|
378
|
+
else if destination_shorthand == 2 {
|
|
379
|
+
// all including self
|
|
380
|
+
deliver(apic, vector, delivery_mode, is_level);
|
|
381
|
+
}
|
|
382
|
+
else if destination_shorthand == 3 {
|
|
383
|
+
// all but self
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
dbg_assert!(false);
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
0x310 => {
|
|
391
|
+
dbg_log!("APIC write icr1: {:08x}", value);
|
|
392
|
+
apic.icr1 = value;
|
|
393
|
+
},
|
|
394
|
+
|
|
395
|
+
0x320 => {
|
|
396
|
+
if APIC_LOG_VERBOSE {
|
|
397
|
+
dbg_log!("timer lvt: {:08x}", value);
|
|
398
|
+
}
|
|
399
|
+
// TODO: check if unmasking and if this should trigger an interrupt immediately
|
|
400
|
+
apic.lvt_timer = value;
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
0x330 => {
|
|
404
|
+
dbg_log!("lvt thermal sensor: {:08x}", value);
|
|
405
|
+
apic.lvt_thermal_sensor = value;
|
|
406
|
+
},
|
|
407
|
+
|
|
408
|
+
0x340 => {
|
|
409
|
+
dbg_log!("lvt perf counter: {:08x}", value);
|
|
410
|
+
apic.lvt_perf_counter = value;
|
|
411
|
+
},
|
|
412
|
+
|
|
413
|
+
0x350 => {
|
|
414
|
+
dbg_log!("lvt int0: {:08x}", value);
|
|
415
|
+
apic.lvt_int0 = value;
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
0x360 => {
|
|
419
|
+
dbg_log!("lvt int1: {:08x}", value);
|
|
420
|
+
apic.lvt_int1 = value;
|
|
421
|
+
},
|
|
422
|
+
|
|
423
|
+
0x370 => {
|
|
424
|
+
dbg_log!("lvt error: {:08x}", value);
|
|
425
|
+
apic.lvt_error = value;
|
|
426
|
+
},
|
|
427
|
+
|
|
428
|
+
0x3E0 => {
|
|
429
|
+
apic.timer_divider = value;
|
|
430
|
+
|
|
431
|
+
let divide_shift = (value & 0b11) | ((value & 0b1000) >> 1);
|
|
432
|
+
apic.timer_divider_shift = if divide_shift == 0b111 { 0 } else { divide_shift + 1 };
|
|
433
|
+
dbg_log!(
|
|
434
|
+
"APIC timer divider: {:08x} shift={} tick={:.6}ms",
|
|
435
|
+
apic.timer_divider,
|
|
436
|
+
apic.timer_divider_shift,
|
|
437
|
+
(1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ
|
|
438
|
+
);
|
|
439
|
+
},
|
|
440
|
+
|
|
441
|
+
0x380 => {
|
|
442
|
+
if APIC_LOG_VERBOSE {
|
|
443
|
+
dbg_log!(
|
|
444
|
+
"APIC timer initial: {} next_interrupt={:.2}ms",
|
|
445
|
+
value,
|
|
446
|
+
value as f64 * (1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ,
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
apic.timer_initial_count = value;
|
|
450
|
+
apic.timer_current_count = value;
|
|
451
|
+
apic.timer_last_tick = unsafe { js::microtick() };
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
0x390 => {
|
|
455
|
+
dbg_log!("write timer current: {:08x}", value);
|
|
456
|
+
dbg_assert!(false, "read-only register");
|
|
457
|
+
},
|
|
458
|
+
|
|
459
|
+
_ => {
|
|
460
|
+
dbg_log!("APIC write32 {:x} <- {:08x}", addr, value);
|
|
461
|
+
dbg_assert!(false);
|
|
462
|
+
},
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
#[no_mangle]
|
|
467
|
+
pub fn apic_timer(now: f64) -> f64 { timer(&mut get_apic(), now) }
|
|
468
|
+
|
|
469
|
+
fn timer(apic: &mut Apic, now: f64) -> f64 {
|
|
470
|
+
if apic.timer_initial_count == 0 || apic.timer_current_count == 0 {
|
|
471
|
+
return 100.0;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if apic.timer_last_tick > now {
|
|
475
|
+
// should only happen after restore_state
|
|
476
|
+
dbg_log!("warning: APIC last_tick is in the future, resetting");
|
|
477
|
+
apic.timer_last_tick = now;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
let diff = now - apic.timer_last_tick;
|
|
481
|
+
let diff_in_ticks = diff * APIC_TIMER_FREQ / (1 << apic.timer_divider_shift) as f64;
|
|
482
|
+
dbg_assert!(diff_in_ticks >= 0.0);
|
|
483
|
+
let diff_in_ticks = diff_in_ticks as u64;
|
|
484
|
+
|
|
485
|
+
let time_per_interrupt =
|
|
486
|
+
apic.timer_initial_count as f64 * (1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ;
|
|
487
|
+
|
|
488
|
+
if diff_in_ticks >= apic.timer_initial_count as u64 {
|
|
489
|
+
let mode = apic.lvt_timer & APIC_TIMER_MODE_MASK;
|
|
490
|
+
if mode == APIC_TIMER_MODE_PERIODIC {
|
|
491
|
+
if APIC_LOG_VERBOSE {
|
|
492
|
+
dbg_log!("APIC timer periodic interrupt");
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if diff_in_ticks >= 2 * apic.timer_initial_count as u64 {
|
|
496
|
+
dbg_log!(
|
|
497
|
+
"warning: APIC skipping {} interrupts initial={} ticks={} last_tick={:.1}ms now={:.1}ms d={:.1}ms",
|
|
498
|
+
diff_in_ticks / apic.timer_initial_count as u64 - 1,
|
|
499
|
+
apic.timer_initial_count,
|
|
500
|
+
diff_in_ticks,
|
|
501
|
+
apic.timer_last_tick,
|
|
502
|
+
now,
|
|
503
|
+
diff,
|
|
504
|
+
);
|
|
505
|
+
apic.timer_last_tick = now;
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
apic.timer_last_tick += time_per_interrupt;
|
|
509
|
+
dbg_assert!(apic.timer_last_tick <= now);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else if mode == APIC_TIMER_MODE_ONE_SHOT {
|
|
513
|
+
if APIC_LOG_VERBOSE {
|
|
514
|
+
dbg_log!("APIC timer one shot end");
|
|
515
|
+
}
|
|
516
|
+
apic.timer_current_count = 0;
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
dbg_assert!(false, "apic unimplemented timer mode: {:x}", mode);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if apic.lvt_timer & IOAPIC_CONFIG_MASKED == 0 {
|
|
523
|
+
deliver(
|
|
524
|
+
apic,
|
|
525
|
+
(apic.lvt_timer & 0xFF) as u8,
|
|
526
|
+
IOAPIC_DELIVERY_FIXED,
|
|
527
|
+
false,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
apic.timer_last_tick + time_per_interrupt - now
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
pub fn route(
|
|
536
|
+
apic: &mut Apic,
|
|
537
|
+
vector: u8,
|
|
538
|
+
mode: u8,
|
|
539
|
+
is_level: bool,
|
|
540
|
+
_destination: u8,
|
|
541
|
+
_destination_mode: u8,
|
|
542
|
+
) {
|
|
543
|
+
// TODO
|
|
544
|
+
deliver(apic, vector, mode, is_level);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
fn deliver(apic: &mut Apic, vector: u8, mode: u8, is_level: bool) {
|
|
548
|
+
if APIC_LOG_VERBOSE {
|
|
549
|
+
dbg_log!("Deliver {:02x} mode={} level={}", vector, mode, is_level);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if mode == IOAPIC_DELIVERY_INIT {
|
|
553
|
+
// TODO
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if mode == IOAPIC_DELIVERY_NMI {
|
|
558
|
+
// TODO
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if vector < 0x10 || vector == 0xFF {
|
|
563
|
+
dbg_assert!(false, "TODO: Invalid vector: {:x}", vector);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if register_get_bit(&apic.irr, vector) {
|
|
567
|
+
dbg_log!("Not delivered: irr already set, vector={:02x}", vector);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
register_set_bit(&mut apic.irr, vector);
|
|
572
|
+
|
|
573
|
+
if is_level {
|
|
574
|
+
register_set_bit(&mut apic.tmr, vector);
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
register_clear_bit(&mut apic.tmr, vector);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
fn highest_irr(apic: &mut Apic) -> Option<u8> {
|
|
582
|
+
let highest = register_get_highest_bit(&apic.irr);
|
|
583
|
+
if let Some(x) = highest {
|
|
584
|
+
dbg_assert!(x >= 0x10);
|
|
585
|
+
dbg_assert!(x != 0xFF);
|
|
586
|
+
}
|
|
587
|
+
highest
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
fn highest_isr(apic: &mut Apic) -> Option<u8> {
|
|
591
|
+
let highest = register_get_highest_bit(&apic.isr);
|
|
592
|
+
if let Some(x) = highest {
|
|
593
|
+
dbg_assert!(x >= 0x10);
|
|
594
|
+
dbg_assert!(x != 0xFF);
|
|
595
|
+
}
|
|
596
|
+
highest
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
pub fn acknowledge_irq() -> Option<u8> { acknowledge_irq_internal(&mut get_apic()) }
|
|
600
|
+
|
|
601
|
+
fn acknowledge_irq_internal(apic: &mut Apic) -> Option<u8> {
|
|
602
|
+
let highest_irr = match highest_irr(apic) {
|
|
603
|
+
None => return None,
|
|
604
|
+
Some(x) => x,
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
if let Some(highest_isr) = highest_isr(apic) {
|
|
608
|
+
if highest_isr >= highest_irr {
|
|
609
|
+
if APIC_LOG_VERBOSE {
|
|
610
|
+
dbg_log!("Higher isr, isr={:x} irr={:x}", highest_isr, highest_irr);
|
|
611
|
+
}
|
|
612
|
+
return None;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if highest_irr & 0xF0 <= apic.tpr as u8 & 0xF0 {
|
|
617
|
+
if APIC_LOG_VERBOSE {
|
|
618
|
+
dbg_log!(
|
|
619
|
+
"Higher tpr, tpr={:x} irr={:x}",
|
|
620
|
+
apic.tpr & 0xF0,
|
|
621
|
+
highest_irr
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
return None;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
register_clear_bit(&mut apic.irr, highest_irr);
|
|
628
|
+
register_set_bit(&mut apic.isr, highest_irr);
|
|
629
|
+
|
|
630
|
+
if APIC_LOG_VERBOSE {
|
|
631
|
+
dbg_log!("Calling vector {:x}", highest_irr);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
dbg_assert!(acknowledge_irq_internal(apic).is_none());
|
|
635
|
+
|
|
636
|
+
Some(highest_irr)
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// functions operating on 256-bit registers (for irr, isr, tmr)
|
|
640
|
+
fn register_get_bit(v: &[u32; 8], bit: u8) -> bool { v[(bit >> 5) as usize] & 1 << (bit & 31) != 0 }
|
|
641
|
+
|
|
642
|
+
fn register_set_bit(v: &mut [u32; 8], bit: u8) { v[(bit >> 5) as usize] |= 1 << (bit & 31); }
|
|
643
|
+
|
|
644
|
+
fn register_clear_bit(v: &mut [u32; 8], bit: u8) { v[(bit >> 5) as usize] &= !(1 << (bit & 31)); }
|
|
645
|
+
|
|
646
|
+
fn register_get_highest_bit(v: &[u32; 8]) -> Option<u8> {
|
|
647
|
+
dbg_assert!(v.as_ptr().addr() & std::mem::align_of::<u64>() - 1 == 0);
|
|
648
|
+
let v: &[u64; 4] = unsafe { std::mem::transmute(v) };
|
|
649
|
+
for i in (0..4).rev() {
|
|
650
|
+
let word = v[i];
|
|
651
|
+
|
|
652
|
+
if word != 0 {
|
|
653
|
+
return Some(word.ilog2() as u8 | (i as u8) << 6);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
None
|
|
658
|
+
}
|