cpu.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. "use strict";
  2. /** @const */
  3. var CPU_LOG_VERBOSE = false;
  4. // Resources:
  5. // https://pdos.csail.mit.edu/6.828/2006/readings/i386/toc.htm
  6. // https://www-ssl.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
  7. // http://ref.x86asm.net/geek32.html
  8. /** @constructor */
  9. function CPU(bus, wm, next_tick_immediately)
  10. {
  11. this.next_tick_immediately = next_tick_immediately;
  12. this.wm = wm;
  13. this.wasm_patch();
  14. this.create_jit_imports();
  15. const memory = this.wm.exports.memory;
  16. this.wasm_memory = memory;
  17. this.memory_size = v86util.view(Uint32Array, memory, 812, 1);
  18. this.mem8 = new Uint8Array(0);
  19. this.mem32s = new Int32Array(this.mem8.buffer);
  20. this.segment_is_null = v86util.view(Uint8Array, memory, 724, 8);
  21. this.segment_offsets = v86util.view(Int32Array, memory, 736, 8);
  22. this.segment_limits = v86util.view(Uint32Array, memory, 768, 8);
  23. /**
  24. * Wheter or not in protected mode
  25. */
  26. this.protected_mode = v86util.view(Int32Array, memory, 800, 1);
  27. this.idtr_size = v86util.view(Int32Array, memory, 564, 1);
  28. this.idtr_offset = v86util.view(Int32Array, memory, 568, 1);
  29. /**
  30. * global descriptor table register
  31. */
  32. this.gdtr_size = v86util.view(Int32Array, memory, 572, 1);
  33. this.gdtr_offset = v86util.view(Int32Array, memory, 576, 1);
  34. this.tss_size_32 = v86util.view(Int32Array, memory, 1128, 1);
  35. /*
  36. * whether or not a page fault occured
  37. */
  38. this.page_fault = v86util.view(Uint32Array, memory, 540, 8);
  39. this.cr = v86util.view(Int32Array, memory, 580, 8);
  40. // current privilege level
  41. this.cpl = v86util.view(Uint8Array, memory, 612, 1);
  42. // current operand/address size
  43. this.is_32 = v86util.view(Int32Array, memory, 804, 1);
  44. this.stack_size_32 = v86util.view(Int32Array, memory, 808, 1);
  45. /**
  46. * Was the last instruction a hlt?
  47. */
  48. this.in_hlt = v86util.view(Uint8Array, memory, 616, 1);
  49. this.last_virt_eip = v86util.view(Int32Array, memory, 620, 1);
  50. this.eip_phys = v86util.view(Int32Array, memory, 624, 1);
  51. this.sysenter_cs = v86util.view(Int32Array, memory, 636, 1);
  52. this.sysenter_esp = v86util.view(Int32Array, memory, 640, 1);
  53. this.sysenter_eip = v86util.view(Int32Array, memory, 644, 1);
  54. this.prefixes = v86util.view(Int32Array, memory, 648, 1);
  55. this.flags = v86util.view(Int32Array, memory, 120, 1);
  56. /**
  57. * bitmap of flags which are not updated in the flags variable
  58. * changed by arithmetic instructions, so only relevant to arithmetic flags
  59. */
  60. this.flags_changed = v86util.view(Int32Array, memory, 116, 1);
  61. /**
  62. * enough infos about the last arithmetic operation to compute eflags
  63. */
  64. this.last_op1 = v86util.view(Int32Array, memory, 96, 1);
  65. this.last_op_size = v86util.view(Int32Array, memory, 104, 1);
  66. this.last_result = v86util.view(Int32Array, memory, 112, 1);
  67. this.current_tsc = v86util.view(Uint32Array, memory, 960, 2); // 64 bit
  68. /** @type {!Object} */
  69. this.devices = {};
  70. this.instruction_pointer = v86util.view(Int32Array, memory, 556, 1);
  71. this.previous_ip = v86util.view(Int32Array, memory, 560, 1);
  72. // configured by guest
  73. this.apic_enabled = v86util.view(Uint8Array, memory, 548, 1);
  74. // configured when the emulator starts (changes bios initialisation)
  75. this.acpi_enabled = v86util.view(Uint8Array, memory, 552, 1);
  76. // managed in io.js
  77. /** @const */ this.memory_map_read8 = [];
  78. /** @const */ this.memory_map_write8 = [];
  79. /** @const */ this.memory_map_read32 = [];
  80. /** @const */ this.memory_map_write32 = [];
  81. /**
  82. * @const
  83. * @type {{main: ArrayBuffer, vga: ArrayBuffer}}
  84. */
  85. this.bios = {
  86. main: null,
  87. vga: null,
  88. };
  89. this.instruction_counter = v86util.view(Uint32Array, memory, 664, 1);
  90. // registers
  91. this.reg32 = v86util.view(Int32Array, memory, 64, 8);
  92. this.fpu_st = v86util.view(Int32Array, memory, 1152, 4 * 8);
  93. this.fpu_stack_empty = v86util.view(Uint8Array, memory, 816, 1);
  94. this.fpu_stack_empty[0] = 0xFF;
  95. this.fpu_stack_ptr = v86util.view(Uint8Array, memory, 1032, 1);
  96. this.fpu_stack_ptr[0] = 0;
  97. this.fpu_control_word = v86util.view(Uint16Array, memory, 1036, 1);
  98. this.fpu_control_word[0] = 0x37F;
  99. this.fpu_status_word = v86util.view(Uint16Array, memory, 1040, 1);
  100. this.fpu_status_word[0] = 0;
  101. this.fpu_ip = v86util.view(Int32Array, memory, 1048, 1);
  102. this.fpu_ip[0] = 0;
  103. this.fpu_ip_selector = v86util.view(Int32Array, memory, 1052, 1);
  104. this.fpu_ip_selector[0] = 0;
  105. this.fpu_opcode = v86util.view(Int32Array, memory, 1044, 1);
  106. this.fpu_opcode[0] = 0;
  107. this.fpu_dp = v86util.view(Int32Array, memory, 1056, 1);
  108. this.fpu_dp[0] = 0;
  109. this.fpu_dp_selector = v86util.view(Int32Array, memory, 1060, 1);
  110. this.fpu_dp_selector[0] = 0;
  111. this.reg_xmm32s = v86util.view(Int32Array, memory, 832, 8 * 4);
  112. this.mxcsr = v86util.view(Int32Array, memory, 824, 1);
  113. // segment registers, tr and ldtr
  114. this.sreg = v86util.view(Uint16Array, memory, 668, 8);
  115. // debug registers
  116. this.dreg = v86util.view(Int32Array, memory, 684, 8);
  117. this.reg_pdpte = v86util.view(Int32Array, memory, 968, 8);
  118. this.fw_value = [];
  119. this.fw_pointer = 0;
  120. this.option_roms = [];
  121. this.io = undefined;
  122. this.bus = bus;
  123. this.set_tsc(0, 0);
  124. this.debug_init();
  125. if(DEBUG)
  126. {
  127. this.do_many_cycles_count = 0;
  128. this.do_many_cycles_total = 0;
  129. this.seen_code = {};
  130. this.seen_code_uncompiled = {};
  131. }
  132. //Object.seal(this);
  133. }
  134. CPU.prototype.clear_opstats = function()
  135. {
  136. new Uint8Array(this.wasm_memory.buffer, 0x8000, 0x20000).fill(0);
  137. this.wm.exports["profiler_init"]();
  138. };
  139. CPU.prototype.create_jit_imports = function()
  140. {
  141. // Set this.jit_imports as generated WASM modules will expect
  142. const jit_imports = Object.create(null);
  143. jit_imports["m"] = this.wm.exports["memory"];
  144. for(let name of Object.keys(this.wm.exports))
  145. {
  146. if(name.startsWith("_") || name.startsWith("zstd") || name.endsWith("_js"))
  147. {
  148. continue;
  149. }
  150. jit_imports[name] = this.wm.exports[name];
  151. }
  152. this.jit_imports = jit_imports;
  153. };
  154. CPU.prototype.wasm_patch = function()
  155. {
  156. const get_optional_import = (name) => {
  157. return this.wm.exports[name];
  158. };
  159. const get_import = (name) =>
  160. {
  161. const f = get_optional_import(name);
  162. console.assert(f, "Missing import: " + name);
  163. return f;
  164. };
  165. this.reset_cpu = get_import("reset_cpu");
  166. this.getiopl = get_import("getiopl");
  167. this.get_eflags = get_import("get_eflags");
  168. this.get_eflags_no_arith = get_import("get_eflags_no_arith");
  169. this.pic_call_irq = get_import("pic_call_irq");
  170. this.do_many_cycles_native = get_import("do_many_cycles_native");
  171. this.cycle_internal = get_import("cycle_internal");
  172. this.read8 = get_import("read8");
  173. this.read16 = get_import("read16");
  174. this.read32s = get_import("read32s");
  175. this.write16 = get_import("write16");
  176. this.write32 = get_import("write32");
  177. this.in_mapped_range = get_import("in_mapped_range");
  178. // used by nasmtests
  179. this.fpu_load_tag_word = get_import("fpu_load_tag_word");
  180. this.fpu_load_status_word = get_import("fpu_load_status_word");
  181. this.fpu_get_sti_f64 = get_import("fpu_get_sti_f64");
  182. this.translate_address_system_read = get_import("translate_address_system_read_js");
  183. this.get_seg_cs = get_import("get_seg_cs");
  184. this.get_real_eip = get_import("get_real_eip");
  185. this.clear_tlb = get_import("clear_tlb");
  186. this.full_clear_tlb = get_import("full_clear_tlb");
  187. this.set_tsc = get_import("set_tsc");
  188. this.store_current_tsc = get_import("store_current_tsc");
  189. if(DEBUG)
  190. {
  191. this.jit_force_generate_unsafe = get_optional_import("jit_force_generate_unsafe");
  192. }
  193. this.jit_clear_cache = get_import("jit_clear_cache_js");
  194. this.jit_dirty_cache = get_import("jit_dirty_cache");
  195. this.codegen_finalize_finished = get_import("codegen_finalize_finished");
  196. this.allocate_memory = get_import("allocate_memory");
  197. this.zero_memory = get_import("zero_memory");
  198. this.zstd_create_ctx = get_import("zstd_create_ctx");
  199. this.zstd_get_src_ptr = get_import("zstd_get_src_ptr");
  200. this.zstd_free_ctx = get_import("zstd_free_ctx");
  201. this.zstd_read = get_import("zstd_read");
  202. this.zstd_read_free = get_import("zstd_read_free");
  203. };
  204. CPU.prototype.jit_force_generate = function(addr)
  205. {
  206. if(!this.jit_force_generate_unsafe)
  207. {
  208. dbg_assert(false, "Not supported in this wasm build: jit_force_generate_unsafe");
  209. return;
  210. }
  211. this.jit_force_generate_unsafe(addr);
  212. };
  213. CPU.prototype.jit_clear_func = function(index)
  214. {
  215. dbg_assert(index >= 0 && index < WASM_TABLE_SIZE);
  216. this.wm.wasm_table.set(index + WASM_TABLE_OFFSET, null);
  217. };
  218. CPU.prototype.jit_clear_all_funcs = function()
  219. {
  220. const table = this.wm.wasm_table;
  221. for(let i = 0; i < WASM_TABLE_SIZE; i++)
  222. {
  223. table.set(WASM_TABLE_OFFSET + i, null);
  224. }
  225. };
  226. CPU.prototype.get_state = function()
  227. {
  228. var state = [];
  229. state[0] = this.memory_size[0];
  230. state[1] = this.segment_is_null;
  231. state[2] = this.segment_offsets;
  232. state[3] = this.segment_limits;
  233. state[4] = this.protected_mode[0];
  234. state[5] = this.idtr_offset[0];
  235. state[6] = this.idtr_size[0];
  236. state[7] = this.gdtr_offset[0];
  237. state[8] = this.gdtr_size[0];
  238. state[9] = this.page_fault[0];
  239. state[10] = this.cr;
  240. state[11] = this.cpl[0];
  241. state[13] = this.is_32[0];
  242. state[16] = this.stack_size_32[0];
  243. state[17] = this.in_hlt[0];
  244. state[18] = this.last_virt_eip[0];
  245. state[19] = this.eip_phys[0];
  246. state[22] = this.sysenter_cs[0];
  247. state[23] = this.sysenter_eip[0];
  248. state[24] = this.sysenter_esp[0];
  249. state[25] = this.prefixes[0];
  250. state[26] = this.flags[0];
  251. state[27] = this.flags_changed[0];
  252. state[28] = this.last_op1[0];
  253. state[30] = this.last_op_size[0];
  254. state[37] = this.instruction_pointer[0];
  255. state[38] = this.previous_ip[0];
  256. state[39] = this.reg32;
  257. state[40] = this.sreg;
  258. state[41] = this.dreg;
  259. state[42] = this.reg_pdpte;
  260. this.store_current_tsc();
  261. state[43] = this.current_tsc;
  262. state[45] = this.devices.virtio_9p;
  263. state[46] = this.devices.apic;
  264. state[47] = this.devices.rtc;
  265. state[48] = this.devices.pci;
  266. state[49] = this.devices.dma;
  267. state[50] = this.devices.acpi;
  268. state[51] = this.devices.hpet;
  269. state[52] = this.devices.vga;
  270. state[53] = this.devices.ps2;
  271. state[54] = this.devices.uart0;
  272. state[55] = this.devices.fdc;
  273. state[56] = this.devices.cdrom;
  274. state[57] = this.devices.hda;
  275. state[58] = this.devices.pit;
  276. state[59] = this.devices.net;
  277. state[60] = this.devices.pic;
  278. state[61] = this.devices.sb16;
  279. state[62] = this.fw_value;
  280. state[63] = this.devices.ioapic;
  281. state[64] = this.tss_size_32[0];
  282. state[66] = this.reg_xmm32s;
  283. state[67] = this.fpu_st;
  284. state[68] = this.fpu_stack_empty[0];
  285. state[69] = this.fpu_stack_ptr[0];
  286. state[70] = this.fpu_control_word[0];
  287. state[71] = this.fpu_ip[0];
  288. state[72] = this.fpu_ip_selector[0];
  289. state[73] = this.fpu_dp[0];
  290. state[74] = this.fpu_dp_selector[0];
  291. state[75] = this.fpu_opcode[0];
  292. const { packed_memory, bitmap } = this.pack_memory();
  293. state[77] = packed_memory;
  294. state[78] = new Uint8Array(bitmap.get_buffer());
  295. state[79] = this.devices.uart1;
  296. state[80] = this.devices.uart2;
  297. state[81] = this.devices.uart3;
  298. return state;
  299. };
  300. CPU.prototype.set_state = function(state)
  301. {
  302. this.memory_size[0] = state[0];
  303. if(this.mem8.length !== this.memory_size[0])
  304. {
  305. console.warn("Note: Memory size mismatch. we=" + this.mem8.length + " state=" + this.memory_size[0]);
  306. }
  307. this.segment_is_null.set(state[1]);
  308. this.segment_offsets.set(state[2]);
  309. this.segment_limits.set(state[3]);
  310. this.protected_mode[0] = state[4];
  311. this.idtr_offset[0] = state[5];
  312. this.idtr_size[0] = state[6];
  313. this.gdtr_offset[0] = state[7];
  314. this.gdtr_size[0] = state[8];
  315. this.page_fault[0] = state[9];
  316. this.cr.set(state[10]);
  317. this.cpl[0] = state[11];
  318. this.is_32[0] = state[13];
  319. this.stack_size_32[0] = state[16];
  320. this.in_hlt[0] = state[17];
  321. this.last_virt_eip[0] = state[18];
  322. this.eip_phys[0] = state[19];
  323. this.sysenter_cs[0] = state[22];
  324. this.sysenter_eip[0] = state[23];
  325. this.sysenter_esp[0] = state[24];
  326. this.prefixes[0] = state[25];
  327. this.flags[0] = state[26];
  328. this.flags_changed[0] = state[27];
  329. this.last_op1[0] = state[28];
  330. this.last_op_size[0] = state[30];
  331. this.instruction_pointer[0] = state[37];
  332. this.previous_ip[0] = state[38];
  333. this.reg32.set(state[39]);
  334. this.sreg.set(state[40]);
  335. this.dreg.set(state[41]);
  336. state[42] && this.reg_pdpte.set(state[42]);
  337. this.set_tsc(state[43][0], state[43][1]);
  338. this.devices.virtio_9p && this.devices.virtio_9p.set_state(state[45]);
  339. this.devices.apic && this.devices.apic.set_state(state[46]);
  340. this.devices.rtc && this.devices.rtc.set_state(state[47]);
  341. this.devices.pci && this.devices.pci.set_state(state[48]);
  342. this.devices.dma && this.devices.dma.set_state(state[49]);
  343. this.devices.acpi && this.devices.acpi.set_state(state[50]);
  344. this.devices.hpet && this.devices.hpet.set_state(state[51]);
  345. this.devices.vga && this.devices.vga.set_state(state[52]);
  346. this.devices.ps2 && this.devices.ps2.set_state(state[53]);
  347. this.devices.uart0 && this.devices.uart0.set_state(state[54]);
  348. this.devices.fdc && this.devices.fdc.set_state(state[55]);
  349. this.devices.cdrom && this.devices.cdrom.set_state(state[56]);
  350. this.devices.hda && this.devices.hda.set_state(state[57]);
  351. this.devices.pit && this.devices.pit.set_state(state[58]);
  352. this.devices.net && this.devices.net.set_state(state[59]);
  353. this.devices.pic && this.devices.pic.set_state(state[60]);
  354. this.devices.sb16 && this.devices.sb16.set_state(state[61]);
  355. this.devices.uart1 && this.devices.uart1.set_state(state[79]);
  356. this.devices.uart2 && this.devices.uart2.set_state(state[80]);
  357. this.devices.uart3 && this.devices.uart3.set_state(state[81]);
  358. this.fw_value = state[62];
  359. this.devices.ioapic && this.devices.ioapic.set_state(state[63]);
  360. this.tss_size_32[0] = state[64];
  361. this.reg_xmm32s.set(state[66]);
  362. this.fpu_st.set(state[67]);
  363. this.fpu_stack_empty[0] = state[68];
  364. this.fpu_stack_ptr[0] = state[69];
  365. this.fpu_control_word[0] = state[70];
  366. this.fpu_ip[0] = state[71];
  367. this.fpu_ip_selector[0] = state[72];
  368. this.fpu_dp[0] = state[73];
  369. this.fpu_dp_selector[0] = state[74];
  370. this.fpu_opcode[0] = state[75];
  371. const bitmap = new v86util.Bitmap(state[78].buffer);
  372. const packed_memory = state[77];
  373. this.unpack_memory(bitmap, packed_memory);
  374. this.full_clear_tlb();
  375. this.jit_clear_cache();
  376. };
  377. CPU.prototype.pack_memory = function()
  378. {
  379. dbg_assert((this.mem8.length & 0xFFF) === 0);
  380. const page_count = this.mem8.length >> 12;
  381. const nonzero_pages = [];
  382. for(let page = 0; page < page_count; page++)
  383. {
  384. const offset = page << 12;
  385. const view = this.mem32s.subarray(offset >> 2, offset + 0x1000 >> 2);
  386. let is_zero = true;
  387. for(let i = 0; i < view.length; i++)
  388. {
  389. if(view[i] !== 0)
  390. {
  391. is_zero = false;
  392. break;
  393. }
  394. }
  395. if(!is_zero)
  396. {
  397. nonzero_pages.push(page);
  398. }
  399. }
  400. const bitmap = new v86util.Bitmap(page_count);
  401. const packed_memory = new Uint8Array(nonzero_pages.length << 12);
  402. for(let [i, page] of nonzero_pages.entries())
  403. {
  404. bitmap.set(page, 1);
  405. const offset = page << 12;
  406. const page_contents = this.mem8.subarray(offset, offset + 0x1000);
  407. packed_memory.set(page_contents, i << 12);
  408. }
  409. return { bitmap, packed_memory };
  410. };
  411. CPU.prototype.unpack_memory = function(bitmap, packed_memory)
  412. {
  413. this.zero_memory(this.memory_size[0]);
  414. const page_count = this.memory_size[0] >> 12;
  415. let packed_page = 0;
  416. for(let page = 0; page < page_count; page++)
  417. {
  418. if(bitmap.get(page))
  419. {
  420. let offset = packed_page << 12;
  421. let view = packed_memory.subarray(offset, offset + 0x1000);
  422. this.mem8.set(view, page << 12);
  423. packed_page++;
  424. }
  425. }
  426. };
  427. /**
  428. * @return {number} time in ms until this method should becalled again
  429. */
  430. CPU.prototype.main_run = function()
  431. {
  432. if(this.in_hlt[0])
  433. {
  434. const t = this.hlt_loop();
  435. if(this.in_hlt[0])
  436. {
  437. return t;
  438. }
  439. }
  440. const start = v86.microtick();
  441. let now = start;
  442. for(; now - start < TIME_PER_FRAME;)
  443. {
  444. this.do_many_cycles();
  445. now = v86.microtick();
  446. const t = this.run_hardware_timers(now);
  447. this.handle_irqs();
  448. if(this.in_hlt[0])
  449. {
  450. return t;
  451. }
  452. }
  453. return 0;
  454. };
  455. CPU.prototype.reboot_internal = function()
  456. {
  457. this.reset_cpu();
  458. this.fw_value = [];
  459. if(this.devices.virtio)
  460. {
  461. this.devices.virtio.reset();
  462. }
  463. this.load_bios();
  464. };
  465. CPU.prototype.reset_memory = function()
  466. {
  467. this.mem8.fill(0);
  468. };
  469. /** @export */
  470. CPU.prototype.create_memory = function(size)
  471. {
  472. if(size < 1024 * 1024)
  473. {
  474. size = 1024 * 1024;
  475. }
  476. else if((size | 0) < 0)
  477. {
  478. size = Math.pow(2, 31) - MMAP_BLOCK_SIZE;
  479. }
  480. size = ((size - 1) | (MMAP_BLOCK_SIZE - 1)) + 1 | 0;
  481. dbg_assert((size | 0) > 0);
  482. dbg_assert((size & MMAP_BLOCK_SIZE - 1) === 0);
  483. console.assert(this.memory_size[0] === 0, "Expected uninitialised memory");
  484. this.memory_size[0] = size;
  485. const memory_offset = this.allocate_memory(size);
  486. this.mem8 = v86util.view(Uint8Array, this.wasm_memory, memory_offset, size);
  487. this.mem32s = v86util.view(Uint32Array, this.wasm_memory, memory_offset, size >> 2);
  488. };
  489. CPU.prototype.init = function(settings, device_bus)
  490. {
  491. if(typeof settings.log_level === "number")
  492. {
  493. // XXX: Shared between all emulator instances
  494. LOG_LEVEL = settings.log_level;
  495. }
  496. this.create_memory(typeof settings.memory_size === "number" ?
  497. settings.memory_size : 1024 * 1024 * 64);
  498. this.acpi_enabled[0] = +settings.acpi;
  499. this.reset_cpu();
  500. var io = new IO(this);
  501. this.io = io;
  502. this.bios.main = settings.bios;
  503. this.bios.vga = settings.vga_bios;
  504. this.load_bios();
  505. if(settings.bzimage)
  506. {
  507. const { option_rom } = load_kernel(this.mem8, settings.bzimage, settings.initrd, settings.cmdline || "");
  508. if(option_rom)
  509. {
  510. this.option_roms.push(option_rom);
  511. }
  512. }
  513. io.register_read(0xB3, this, function()
  514. {
  515. // seabios smm_relocate_and_restore
  516. dbg_log("port 0xB3 read");
  517. return 0;
  518. });
  519. var a20_byte = 0;
  520. io.register_read(0x92, this, function()
  521. {
  522. return a20_byte;
  523. });
  524. io.register_write(0x92, this, function(out_byte)
  525. {
  526. a20_byte = out_byte;
  527. });
  528. io.register_read(0x511, this, function()
  529. {
  530. // bios config port (used by seabios and kvm-unit-test)
  531. if(this.fw_pointer < this.fw_value.length)
  532. {
  533. return this.fw_value[this.fw_pointer++];
  534. }
  535. else
  536. {
  537. dbg_assert(false, "config port: Read past value");
  538. return 0;
  539. }
  540. });
  541. io.register_write(0x510, this, undefined, function(value)
  542. {
  543. // https://wiki.osdev.org/QEMU_fw_cfg
  544. // https://github.com/qemu/qemu/blob/master/docs/specs/fw_cfg.txt
  545. dbg_log("bios config port, index=" + h(value));
  546. function i32(x)
  547. {
  548. return new Uint8Array(new Int32Array([x]).buffer);
  549. }
  550. function to_be16(x)
  551. {
  552. return x >> 8 | x << 8 & 0xFF00;
  553. }
  554. function to_be32(x)
  555. {
  556. return x << 24 | x << 8 & 0xFF0000 | x >> 8 & 0xFF00 | x >>> 24;
  557. }
  558. this.fw_pointer = 0;
  559. if(value === FW_CFG_SIGNATURE)
  560. {
  561. // Pretend to be qemu (for seabios)
  562. this.fw_value = i32(FW_CFG_SIGNATURE_QEMU);
  563. }
  564. else if(value === FW_CFG_ID)
  565. {
  566. this.fw_value = i32(0);
  567. }
  568. else if(value === FW_CFG_RAM_SIZE)
  569. {
  570. this.fw_value = i32(this.memory_size[0]);
  571. }
  572. else if(value === FW_CFG_NB_CPUS)
  573. {
  574. this.fw_value = i32(1);
  575. }
  576. else if(value === FW_CFG_MAX_CPUS)
  577. {
  578. this.fw_value = i32(1);
  579. }
  580. else if(value === FW_CFG_NUMA)
  581. {
  582. this.fw_value = new Uint8Array(16);
  583. }
  584. else if(value === FW_CFG_FILE_DIR)
  585. {
  586. const buffer_size = 4 + 64 * this.option_roms.length;
  587. const buffer32 = new Int32Array(buffer_size);
  588. const buffer8 = new Uint8Array(buffer32.buffer);
  589. buffer32[0] = to_be32(this.option_roms.length);
  590. for(let i = 0; i < this.option_roms.length; i++)
  591. {
  592. const { name, data } = this.option_roms[i];
  593. const file_struct_ptr = 4 + 64 * i;
  594. dbg_assert(FW_CFG_FILE_START + i < 0x10000);
  595. buffer32[file_struct_ptr + 0 >> 2] = to_be32(data.length);
  596. buffer32[file_struct_ptr + 4 >> 2] = to_be16(FW_CFG_FILE_START + i);
  597. dbg_assert(name.length < 64 - 8);
  598. for(let j = 0; j < name.length; j++)
  599. {
  600. buffer8[file_struct_ptr + 8 + j] = name.charCodeAt(j);
  601. }
  602. }
  603. this.fw_value = buffer8;
  604. }
  605. else if(value >= FW_CFG_CUSTOM_START && value < FW_CFG_FILE_START)
  606. {
  607. this.fw_value = i32(0);
  608. }
  609. else if(value >= FW_CFG_FILE_START && value - FW_CFG_FILE_START < this.option_roms.length)
  610. {
  611. const i = value - FW_CFG_FILE_START;
  612. this.fw_value = this.option_roms[i].data;
  613. }
  614. else
  615. {
  616. dbg_log("Warning: Unimplemented fw index: " + h(value));
  617. this.fw_value = i32(0);
  618. }
  619. });
  620. if(DEBUG)
  621. {
  622. // Use by linux for port-IO delay
  623. // Avoid generating tons of debug messages
  624. io.register_write(0x80, this, function(out_byte)
  625. {
  626. });
  627. }
  628. this.devices = {};
  629. // TODO: Make this more configurable
  630. if(settings.load_devices)
  631. {
  632. this.devices.pic = new PIC(this);
  633. this.devices.pci = new PCI(this);
  634. if(this.acpi_enabled[0])
  635. {
  636. this.devices.ioapic = new IOAPIC(this);
  637. this.devices.apic = new APIC(this);
  638. this.devices.acpi = new ACPI(this);
  639. }
  640. this.devices.rtc = new RTC(this);
  641. this.fill_cmos(this.devices.rtc, settings);
  642. this.devices.dma = new DMA(this);
  643. if(ENABLE_HPET)
  644. {
  645. this.devices.hpet = new HPET(this);
  646. }
  647. this.devices.vga = new VGAScreen(this, device_bus,
  648. settings.vga_memory_size || 8 * 1024 * 1024);
  649. this.devices.ps2 = new PS2(this, device_bus);
  650. this.devices.uart0 = new UART(this, 0x3F8, device_bus);
  651. if(settings.uart1)
  652. {
  653. this.devices.uart1 = new UART(this, 0x2F8, device_bus);
  654. }
  655. if(settings.uart2)
  656. {
  657. this.devices.uart2 = new UART(this, 0x3E8, device_bus);
  658. }
  659. if(settings.uart3)
  660. {
  661. this.devices.uart3 = new UART(this, 0x2E8, device_bus);
  662. }
  663. this.devices.fdc = new FloppyController(this, settings.fda, settings.fdb);
  664. var ide_device_count = 0;
  665. if(settings.hda)
  666. {
  667. this.devices.hda = new IDEDevice(this, settings.hda, settings.hdb, false, ide_device_count++, device_bus);
  668. }
  669. if(settings.cdrom)
  670. {
  671. this.devices.cdrom = new IDEDevice(this, settings.cdrom, undefined, true, ide_device_count++, device_bus);
  672. }
  673. this.devices.pit = new PIT(this, device_bus);
  674. if(settings.enable_ne2k)
  675. {
  676. this.devices.net = new Ne2k(this, device_bus, settings.preserve_mac_from_state_image);
  677. }
  678. if(settings.fs9p)
  679. {
  680. this.devices.virtio_9p = new Virtio9p(settings.fs9p, this, device_bus);
  681. }
  682. if(true)
  683. {
  684. this.devices.sb16 = new SB16(this, device_bus);
  685. }
  686. }
  687. if(settings.multiboot)
  688. {
  689. this.load_multiboot(settings.multiboot);
  690. }
  691. if(DEBUG)
  692. {
  693. this.debug.init();
  694. }
  695. };
  696. CPU.prototype.load_multiboot = function(buffer)
  697. {
  698. // https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
  699. dbg_log("Trying multiboot from buffer of size " + buffer.byteLength, LOG_CPU);
  700. const MAGIC = 0x1BADB002;
  701. const ELF_MAGIC = 0x464C457F;
  702. const MULTIBOOT_HEADER_ADDRESS = 0x10000;
  703. const MULTIBOOT_SEARCH_BYTES = 8192;
  704. if(buffer.byteLength < MULTIBOOT_SEARCH_BYTES)
  705. {
  706. var buf32 = new Int32Array(MULTIBOOT_SEARCH_BYTES / 4);
  707. new Uint8Array(buf32.buffer).set(new Uint8Array(buffer));
  708. }
  709. else
  710. {
  711. var buf32 = new Int32Array(buffer, 0, MULTIBOOT_SEARCH_BYTES / 4);
  712. }
  713. for(var offset = 0; offset < MULTIBOOT_SEARCH_BYTES; offset += 4)
  714. {
  715. if(buf32[offset >> 2] === MAGIC)
  716. {
  717. var flags = buf32[offset + 4 >> 2];
  718. var checksum = buf32[offset + 8 >> 2];
  719. var total = MAGIC + flags + checksum | 0;
  720. if(total)
  721. {
  722. dbg_log("Multiboot checksum check failed", LOG_CPU);
  723. continue;
  724. }
  725. }
  726. else
  727. {
  728. continue;
  729. }
  730. dbg_log("Multiboot magic found, flags: " + h(flags >>> 0, 8), LOG_CPU);
  731. dbg_assert((flags & ~MULTIBOOT_HEADER_ADDRESS) === 0, "TODO");
  732. this.reg32[REG_EAX] = 0x2BADB002;
  733. let multiboot_info_addr = 0x7C00;
  734. this.reg32[REG_EBX] = multiboot_info_addr;
  735. this.write32(multiboot_info_addr, 0);
  736. this.cr[0] = 1;
  737. this.protected_mode[0] = +true;
  738. this.flags[0] = FLAGS_DEFAULT;
  739. this.is_32[0] = +true;
  740. this.stack_size_32[0] = +true;
  741. for(var i = 0; i < 6; i++)
  742. {
  743. this.segment_is_null[i] = 0;
  744. this.segment_offsets[i] = 0;
  745. this.segment_limits[i] = 0xFFFFFFFF;
  746. // Value doesn't matter, OS isn't allowed to reload without setting
  747. // up a proper GDT
  748. this.sreg[i] = 0xB002;
  749. }
  750. if(flags & MULTIBOOT_HEADER_ADDRESS)
  751. {
  752. dbg_log("Multiboot specifies its own address table", LOG_CPU);
  753. var header_addr = buf32[offset + 12 >> 2];
  754. var load_addr = buf32[offset + 16 >> 2];
  755. var load_end_addr = buf32[offset + 20 >> 2];
  756. var bss_end_addr = buf32[offset + 24 >> 2];
  757. var entry_addr = buf32[offset + 28 >> 2];
  758. dbg_log("header=" + h(header_addr, 8) +
  759. " load=" + h(load_addr, 8) +
  760. " load_end=" + h(load_end_addr, 8) +
  761. " bss_end=" + h(bss_end_addr, 8) +
  762. " entry=" + h(entry_addr, 8));
  763. dbg_assert(load_addr <= header_addr);
  764. var file_start = offset - (header_addr - load_addr);
  765. if(load_end_addr === 0)
  766. {
  767. var length = undefined;
  768. }
  769. else
  770. {
  771. dbg_assert(load_end_addr >= load_addr);
  772. var length = load_end_addr - load_addr;
  773. }
  774. let blob = new Uint8Array(buffer, file_start, length);
  775. this.write_blob(blob, load_addr);
  776. this.instruction_pointer[0] = this.get_seg_cs() + entry_addr | 0;
  777. }
  778. else if(buf32[0] === ELF_MAGIC)
  779. {
  780. dbg_log("Multiboot image is in elf format", LOG_CPU);
  781. let elf = read_elf(buffer);
  782. this.instruction_pointer[0] = this.get_seg_cs() + elf.header.entry | 0;
  783. for(let program of elf.program_headers)
  784. {
  785. if(program.type === 0)
  786. {
  787. // null
  788. }
  789. else if(program.type === 1)
  790. {
  791. // load
  792. // Since multiboot specifies that paging is disabled,
  793. // virtual and physical address must be equal
  794. dbg_assert(program.paddr === program.vaddr);
  795. dbg_assert(program.filesz <= program.memsz);
  796. if(program.paddr + program.memsz < this.memory_size[0])
  797. {
  798. if(program.filesz) // offset might be outside of buffer if filesz is 0
  799. {
  800. let blob = new Uint8Array(buffer, program.offset, program.filesz);
  801. this.write_blob(blob, program.paddr);
  802. }
  803. }
  804. else
  805. {
  806. dbg_log("Warning: Skipped loading section, paddr=" + h(program.paddr) + " memsz=" + program.memsz, LOG_CPU);
  807. }
  808. }
  809. else if(
  810. program.type === 2 ||
  811. program.type === 3 ||
  812. program.type === 4 ||
  813. program.type === 6 ||
  814. program.type === 0x6474e550 ||
  815. program.type === 0x6474e551 ||
  816. program.type === 0x6474e553)
  817. {
  818. // ignore for now
  819. }
  820. else
  821. {
  822. dbg_assert(false, "unimplemented elf section type: " + h(program.type));
  823. }
  824. }
  825. }
  826. else
  827. {
  828. dbg_assert(false, "Not a bootable multiboot format");
  829. }
  830. // only for kvm-unit-test
  831. this.io.register_write_consecutive(0xF4, this,
  832. function(value)
  833. {
  834. console.log("Test exited with code " + h(value, 2));
  835. throw "HALT";
  836. },
  837. function() {},
  838. function() {},
  839. function() {});
  840. // only for kvm-unit-test
  841. for(let i = 0xE; i <= 0xF; i++)
  842. {
  843. this.io.register_write(0x2000 + i, this,
  844. function(value)
  845. {
  846. dbg_log("kvm-unit-test: Set irq " + h(i) + " to " + h(value, 2));
  847. if(value)
  848. {
  849. this.device_raise_irq(i);
  850. }
  851. else
  852. {
  853. this.device_lower_irq(i);
  854. }
  855. });
  856. }
  857. dbg_log("Starting multiboot kernel at:", LOG_CPU);
  858. this.debug.dump_state();
  859. this.debug.dump_regs();
  860. break;
  861. }
  862. };
  863. CPU.prototype.fill_cmos = function(rtc, settings)
  864. {
  865. var boot_order = settings.boot_order || 0x213;
  866. // Used by seabios to determine the boot order
  867. // Nibble
  868. // 1: FloppyPrio
  869. // 2: HDPrio
  870. // 3: CDPrio
  871. // 4: BEVPrio
  872. // bootflag 1, high nibble, lowest priority
  873. // Low nibble: Disable floppy signature check (1)
  874. rtc.cmos_write(CMOS_BIOS_BOOTFLAG1 , 1 | boot_order >> 4 & 0xF0);
  875. // bootflag 2, both nibbles, high and middle priority
  876. rtc.cmos_write(CMOS_BIOS_BOOTFLAG2, boot_order & 0xFF);
  877. // 640k or less if less memory is used
  878. rtc.cmos_write(CMOS_MEM_BASE_LOW, 640 & 0xFF);
  879. rtc.cmos_write(CMOS_MEM_BASE_HIGH, 640 >> 8);
  880. var memory_above_1m = 0; // in k
  881. if(this.memory_size[0] >= 1024 * 1024)
  882. {
  883. memory_above_1m = (this.memory_size[0] - 1024 * 1024) >> 10;
  884. memory_above_1m = Math.min(memory_above_1m, 0xFFFF);
  885. }
  886. rtc.cmos_write(CMOS_MEM_OLD_EXT_LOW, memory_above_1m & 0xFF);
  887. rtc.cmos_write(CMOS_MEM_OLD_EXT_HIGH, memory_above_1m >> 8 & 0xFF);
  888. rtc.cmos_write(CMOS_MEM_EXTMEM_LOW, memory_above_1m & 0xFF);
  889. rtc.cmos_write(CMOS_MEM_EXTMEM_HIGH, memory_above_1m >> 8 & 0xFF);
  890. var memory_above_16m = 0; // in 64k blocks
  891. if(this.memory_size[0] >= 16 * 1024 * 1024)
  892. {
  893. memory_above_16m = (this.memory_size[0] - 16 * 1024 * 1024) >> 16;
  894. memory_above_16m = Math.min(memory_above_16m, 0xFFFF);
  895. }
  896. rtc.cmos_write(CMOS_MEM_EXTMEM2_LOW, memory_above_16m & 0xFF);
  897. rtc.cmos_write(CMOS_MEM_EXTMEM2_HIGH, memory_above_16m >> 8 & 0xFF);
  898. // memory above 4G (not supported by this emulator)
  899. rtc.cmos_write(CMOS_MEM_HIGHMEM_LOW, 0);
  900. rtc.cmos_write(CMOS_MEM_HIGHMEM_MID, 0);
  901. rtc.cmos_write(CMOS_MEM_HIGHMEM_HIGH, 0);
  902. rtc.cmos_write(CMOS_EQUIPMENT_INFO, 0x2F);
  903. rtc.cmos_write(CMOS_BIOS_SMP_COUNT, 0);
  904. // Used by bochs BIOS to skip the boot menu delay.
  905. if (settings.fastboot) rtc.cmos_write(0x3f, 0x01);
  906. };
  907. CPU.prototype.load_bios = function()
  908. {
  909. var bios = this.bios.main;
  910. var vga_bios = this.bios.vga;
  911. if(!bios)
  912. {
  913. dbg_log("Warning: No BIOS");
  914. return;
  915. }
  916. // load bios
  917. var data = new Uint8Array(bios),
  918. start = 0x100000 - bios.byteLength;
  919. this.write_blob(data, start);
  920. if(vga_bios)
  921. {
  922. // load vga bios
  923. var vga_bios8 = new Uint8Array(vga_bios);
  924. // older versions of seabios
  925. this.write_blob(vga_bios8, 0xC0000);
  926. // newer versions of seabios (needs to match pci rom address, see vga.js)
  927. this.io.mmap_register(0xFEB00000, 0x100000,
  928. function(addr)
  929. {
  930. addr = (addr - 0xFEB00000) | 0;
  931. if(addr < vga_bios8.length)
  932. {
  933. return vga_bios8[addr];
  934. }
  935. else
  936. {
  937. return 0;
  938. }
  939. },
  940. function(addr, value)
  941. {
  942. dbg_assert(false, "Unexpected write to VGA rom");
  943. });
  944. }
  945. else
  946. {
  947. dbg_log("Warning: No VGA BIOS");
  948. }
  949. // seabios expects the bios to be mapped to 0xFFF00000 also
  950. this.io.mmap_register(0xFFF00000, 0x100000,
  951. function(addr)
  952. {
  953. addr &= 0xFFFFF;
  954. return this.mem8[addr];
  955. }.bind(this),
  956. function(addr, value)
  957. {
  958. addr &= 0xFFFFF;
  959. this.mem8[addr] = value;
  960. }.bind(this));
  961. };
  962. CPU.prototype.do_many_cycles = function()
  963. {
  964. if(DEBUG)
  965. {
  966. var start_time = v86.microtick();
  967. }
  968. this.do_many_cycles_native();
  969. if(DEBUG)
  970. {
  971. this.do_many_cycles_total += v86.microtick() - start_time;
  972. this.do_many_cycles_count++;
  973. }
  974. };
  975. /** @export */
  976. CPU.prototype.cycle = function()
  977. {
  978. // XXX: May do several cycles
  979. this.cycle_internal();
  980. };
  981. CPU.prototype.codegen_finalize = function(wasm_table_index, start, state_flags, ptr, len)
  982. {
  983. ptr >>>= 0;
  984. len >>>= 0;
  985. dbg_assert(wasm_table_index >= 0 && wasm_table_index < WASM_TABLE_SIZE);
  986. const code = new Uint8Array(this.wasm_memory.buffer, ptr, len);
  987. if(DEBUG)
  988. {
  989. if(DUMP_GENERATED_WASM && !this.seen_code[start])
  990. {
  991. this.debug.dump_wasm(code);
  992. const DUMP_ASSEMBLY = false;
  993. if(DUMP_ASSEMBLY)
  994. {
  995. let end = 0;
  996. if((start ^ end) & ~0xFFF)
  997. {
  998. dbg_log("truncated disassembly start=" + h(start >>> 0) + " end=" + h(end >>> 0));
  999. end = (start | 0xFFF) + 1; // until the end of the page
  1000. }
  1001. dbg_assert(end >= start);
  1002. const buffer = new Uint8Array(end - start);
  1003. for(let i = start; i < end; i++)
  1004. {
  1005. buffer[i - start] = this.read8(i);
  1006. }
  1007. this.debug.dump_code(this.is_32[0] ? 1 : 0, buffer, start);
  1008. }
  1009. }
  1010. this.seen_code[start] = (this.seen_code[start] || 0) + 1;
  1011. if(this.test_hook_did_generate_wasm)
  1012. {
  1013. this.test_hook_did_generate_wasm(code);
  1014. }
  1015. }
  1016. const SYNC_COMPILATION = false;
  1017. if(SYNC_COMPILATION)
  1018. {
  1019. const module = new WebAssembly.Module(code);
  1020. const result = new WebAssembly.Instance(module, { "e": this.jit_imports });
  1021. const f = result.exports["f"];
  1022. this.codegen_finalize_finished(wasm_table_index, start, state_flags);
  1023. this.wm.wasm_table.set(wasm_table_index + WASM_TABLE_OFFSET, f);
  1024. if(this.test_hook_did_finalize_wasm)
  1025. {
  1026. this.test_hook_did_finalize_wasm(code);
  1027. }
  1028. return;
  1029. }
  1030. const result = WebAssembly.instantiate(code, { "e": this.jit_imports }).then(result => {
  1031. const f = result.instance.exports["f"];
  1032. this.codegen_finalize_finished(wasm_table_index, start, state_flags);
  1033. this.wm.wasm_table.set(wasm_table_index + WASM_TABLE_OFFSET, f);
  1034. if(this.test_hook_did_finalize_wasm)
  1035. {
  1036. this.test_hook_did_finalize_wasm(code);
  1037. }
  1038. });
  1039. if(DEBUG)
  1040. {
  1041. result.catch(e => {
  1042. console.log(e);
  1043. debugger;
  1044. throw e;
  1045. });
  1046. }
  1047. };
  1048. CPU.prototype.log_uncompiled_code = function(start, end)
  1049. {
  1050. if(!DEBUG || !DUMP_UNCOMPILED_ASSEMBLY)
  1051. {
  1052. return;
  1053. }
  1054. if((this.seen_code_uncompiled[start] || 0) < 100)
  1055. {
  1056. this.seen_code_uncompiled[start] = (this.seen_code_uncompiled[start] || 0) + 1;
  1057. end += 8; // final jump is not included
  1058. if((start ^ end) & ~0xFFF)
  1059. {
  1060. dbg_log("truncated disassembly start=" + h(start >>> 0) + " end=" + h(end >>> 0));
  1061. end = (start | 0xFFF) + 1; // until the end of the page
  1062. }
  1063. if(end < start) end = start;
  1064. dbg_assert(end >= start);
  1065. const buffer = new Uint8Array(end - start);
  1066. for(let i = start; i < end; i++)
  1067. {
  1068. buffer[i - start] = this.read8(i);
  1069. }
  1070. dbg_log("Uncompiled code:");
  1071. this.debug.dump_code(this.is_32[0] ? 1 : 0, buffer, start);
  1072. }
  1073. };
  1074. CPU.prototype.dump_function_code = function(block_ptr, count)
  1075. {
  1076. if(!DEBUG || !DUMP_GENERATED_WASM)
  1077. {
  1078. return;
  1079. }
  1080. const SIZEOF_BASIC_BLOCK_IN_DWORDS = 7;
  1081. const mem32 = new Int32Array(this.wasm_memory.buffer);
  1082. dbg_assert((block_ptr & 3) === 0);
  1083. const is_32 = this.is_32[0];
  1084. for(let i = 0; i < count; i++)
  1085. {
  1086. const struct_start = (block_ptr >> 2) + i * SIZEOF_BASIC_BLOCK_IN_DWORDS;
  1087. const start = mem32[struct_start + 0];
  1088. const end = mem32[struct_start + 1];
  1089. const is_entry_block = mem32[struct_start + 6] & 0xFF00;
  1090. const buffer = new Uint8Array(end - start);
  1091. for(let i = start; i < end; i++)
  1092. {
  1093. buffer[i - start] = this.read8(this.translate_address_system_read(i));
  1094. }
  1095. dbg_log("---" + (is_entry_block ? " entry" : ""));
  1096. this.debug.dump_code(is_32 ? 1 : 0, buffer, start);
  1097. }
  1098. };
  1099. CPU.prototype.hlt_loop = function()
  1100. {
  1101. if(this.get_eflags_no_arith() & FLAG_INTERRUPT)
  1102. {
  1103. const t = this.run_hardware_timers(v86.microtick());
  1104. this.handle_irqs();
  1105. return t;
  1106. }
  1107. else
  1108. {
  1109. return 100;
  1110. }
  1111. };
  1112. CPU.prototype.run_hardware_timers = function(now)
  1113. {
  1114. if(ENABLE_HPET)
  1115. {
  1116. var pit_time = this.devices.pit.timer(now, this.devices.hpet.legacy_mode);
  1117. var rtc_time = this.devices.rtc.timer(now, this.devices.hpet.legacy_mode);
  1118. var hpet_time = this.devices.hpet.timer(now);
  1119. }
  1120. else
  1121. {
  1122. var pit_time = this.devices.pit.timer(now, false);
  1123. var rtc_time = this.devices.rtc.timer(now, false);
  1124. var hpet_time = 100;
  1125. }
  1126. let acpi_time = 100;
  1127. let apic_time = 100;
  1128. if(this.acpi_enabled[0])
  1129. {
  1130. acpi_time = this.devices.acpi.timer(now);
  1131. apic_time = this.devices.apic.timer(now);
  1132. }
  1133. return Math.min(pit_time, rtc_time, hpet_time, acpi_time, apic_time);
  1134. };
  1135. CPU.prototype.hlt_op = function()
  1136. {
  1137. if((this.get_eflags_no_arith() & FLAG_INTERRUPT) === 0)
  1138. {
  1139. // execution can never resume (until NMIs are supported)
  1140. this.bus.send("cpu-event-halt");
  1141. }
  1142. // get out of here and into hlt_loop
  1143. this.in_hlt[0] = +true;
  1144. // Try an hlt loop right now: This will run timer interrupts, and if one is
  1145. // due it will immediately call call_interrupt_vector and continue
  1146. // execution without an unnecessary cycle through do_run
  1147. this.hlt_loop();
  1148. };
  1149. CPU.prototype.handle_irqs = function()
  1150. {
  1151. //dbg_assert(this.prefixes[0] === 0);
  1152. if(this.get_eflags_no_arith() & FLAG_INTERRUPT)
  1153. {
  1154. this.pic_acknowledge();
  1155. this.next_tick_immediately();
  1156. }
  1157. };
  1158. CPU.prototype.pic_acknowledge = function()
  1159. {
  1160. dbg_assert(this.get_eflags_no_arith() & FLAG_INTERRUPT);
  1161. if(this.devices.pic)
  1162. {
  1163. this.devices.pic.acknowledge_irq();
  1164. }
  1165. if(this.devices.apic)
  1166. {
  1167. this.devices.apic.acknowledge_irq();
  1168. }
  1169. };
  1170. CPU.prototype.device_raise_irq = function(i)
  1171. {
  1172. dbg_assert(arguments.length === 1);
  1173. if(this.devices.pic)
  1174. {
  1175. this.devices.pic.set_irq(i);
  1176. }
  1177. if(this.devices.ioapic)
  1178. {
  1179. this.devices.ioapic.set_irq(i);
  1180. }
  1181. };
  1182. CPU.prototype.device_lower_irq = function(i)
  1183. {
  1184. if(this.devices.pic)
  1185. {
  1186. this.devices.pic.clear_irq(i);
  1187. }
  1188. if(this.devices.ioapic)
  1189. {
  1190. this.devices.ioapic.clear_irq(i);
  1191. }
  1192. };
  1193. // Closure Compiler's way of exporting
  1194. if(typeof window !== "undefined")
  1195. {
  1196. window["CPU"] = CPU;
  1197. }
  1198. else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
  1199. {
  1200. module.exports["CPU"] = CPU;
  1201. }
  1202. else if(typeof importScripts === "function")
  1203. {
  1204. self["CPU"] = CPU;
  1205. }