SoftCPU.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SoftCPU.h"
  27. #include "Emulator.h"
  28. #include <AK/Assertions.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. namespace UserspaceEmulator {
  32. template<typename T>
  33. struct TypeDoubler {
  34. };
  35. template<>
  36. struct TypeDoubler<u8> {
  37. typedef u16 type;
  38. };
  39. template<>
  40. struct TypeDoubler<u16> {
  41. typedef u32 type;
  42. };
  43. template<>
  44. struct TypeDoubler<u32> {
  45. typedef u64 type;
  46. };
  47. template<>
  48. struct TypeDoubler<i8> {
  49. typedef i16 type;
  50. };
  51. template<>
  52. struct TypeDoubler<i16> {
  53. typedef i32 type;
  54. };
  55. template<>
  56. struct TypeDoubler<i32> {
  57. typedef i64 type;
  58. };
  59. SoftCPU::SoftCPU(Emulator& emulator)
  60. : m_emulator(emulator)
  61. {
  62. memset(m_gpr, 0, sizeof(m_gpr));
  63. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  64. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  65. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  66. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  67. }
  68. void SoftCPU::dump() const
  69. {
  70. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  71. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  72. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  73. }
  74. u8 SoftCPU::read8()
  75. {
  76. auto value = read_memory8({ cs(), eip() });
  77. m_eip += 1;
  78. return value;
  79. }
  80. u16 SoftCPU::read16()
  81. {
  82. auto value = read_memory16({ cs(), eip() });
  83. m_eip += 2;
  84. return value;
  85. }
  86. u32 SoftCPU::read32()
  87. {
  88. auto value = read_memory32({ cs(), eip() });
  89. m_eip += 4;
  90. return value;
  91. }
  92. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  93. {
  94. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  95. auto value = m_emulator.mmu().read8(address.offset());
  96. printf("\033[36;1mread_memory8: @%08x -> %02x\033[0m\n", address.offset(), value);
  97. return value;
  98. }
  99. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  100. {
  101. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  102. auto value = m_emulator.mmu().read16(address.offset());
  103. printf("\033[36;1mread_memory16: @%08x -> %04x\033[0m\n", address.offset(), value);
  104. return value;
  105. }
  106. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  107. {
  108. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  109. auto value = m_emulator.mmu().read32(address.offset());
  110. printf("\033[36;1mread_memory32: @%08x -> %08x\033[0m\n", address.offset(), value);
  111. return value;
  112. }
  113. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  114. {
  115. ASSERT(address.selector() == 0x20);
  116. printf("\033[35;1mwrite_memory8: @%08x <- %02x\033[0m\n", address.offset(), value);
  117. m_emulator.mmu().write8(address.offset(), value);
  118. }
  119. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  120. {
  121. ASSERT(address.selector() == 0x20);
  122. printf("\033[35;1mwrite_memory16: @%08x <- %04x\033[0m\n", address.offset(), value);
  123. m_emulator.mmu().write16(address.offset(), value);
  124. }
  125. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  126. {
  127. ASSERT(address.selector() == 0x20);
  128. printf("\033[35;1mwrite_memory32: @%08x <- %08x\033[0m\n", address.offset(), value);
  129. m_emulator.mmu().write32(address.offset(), value);
  130. }
  131. void SoftCPU::push32(u32 value)
  132. {
  133. set_esp(esp() - sizeof(value));
  134. write_memory32({ ss(), esp() }, value);
  135. }
  136. u32 SoftCPU::pop32()
  137. {
  138. auto value = read_memory32({ ss(), esp() });
  139. set_esp(esp() + sizeof(value));
  140. return value;
  141. }
  142. template<bool check_zf, typename Callback>
  143. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  144. {
  145. if (!insn.has_rep_prefix())
  146. return callback();
  147. if (insn.has_address_size_override_prefix()) {
  148. while (cx()) {
  149. callback();
  150. set_cx(cx() - 1);
  151. if constexpr (check_zf) {
  152. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  153. break;
  154. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  155. break;
  156. }
  157. }
  158. return;
  159. }
  160. while (ecx()) {
  161. callback();
  162. set_ecx(ecx() - 1);
  163. if constexpr (check_zf) {
  164. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  165. break;
  166. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  167. break;
  168. }
  169. }
  170. }
  171. template<typename Destination, typename Source>
  172. static typename TypeDoubler<Destination>::type op_xor(SoftCPU& cpu, const Destination& dest, const Source& src)
  173. {
  174. Destination result = 0;
  175. u32 new_flags = 0;
  176. if constexpr (sizeof(Destination) == 4) {
  177. asm volatile("xorl %%ecx, %%eax\n"
  178. : "=a"(result)
  179. : "a"(dest), "c"((u32)src));
  180. } else if constexpr (sizeof(Destination) == 2) {
  181. asm volatile("xor %%cx, %%ax\n"
  182. : "=a"(result)
  183. : "a"(dest), "c"((u16)src));
  184. } else if constexpr (sizeof(Destination) == 1) {
  185. asm volatile("xorb %%cl, %%al\n"
  186. : "=a"(result)
  187. : "a"(dest), "c"((u8)src));
  188. } else {
  189. ASSERT_NOT_REACHED();
  190. }
  191. asm volatile(
  192. "pushf\n"
  193. "pop %%ebx"
  194. : "=b"(new_flags));
  195. cpu.set_flags_oszpc(new_flags);
  196. return result;
  197. }
  198. template<typename Destination, typename Source>
  199. static typename TypeDoubler<Destination>::type op_sub(SoftCPU& cpu, const Destination& dest, const Source& src)
  200. {
  201. Destination result = 0;
  202. u32 new_flags = 0;
  203. if constexpr (sizeof(Destination) == 4) {
  204. asm volatile("subl %%ecx, %%eax\n"
  205. : "=a"(result)
  206. : "a"(dest), "c"((u32)src));
  207. } else if constexpr (sizeof(Destination) == 2) {
  208. asm volatile("subw %%cx, %%ax\n"
  209. : "=a"(result)
  210. : "a"(dest), "c"((u16)src));
  211. } else if constexpr (sizeof(Destination) == 1) {
  212. asm volatile("subb %%cl, %%al\n"
  213. : "=a"(result)
  214. : "a"(dest), "c"((u8)src));
  215. } else {
  216. ASSERT_NOT_REACHED();
  217. }
  218. asm volatile(
  219. "pushf\n"
  220. "pop %%ebx"
  221. : "=b"(new_flags));
  222. cpu.set_flags_oszapc(new_flags);
  223. return result;
  224. }
  225. template<typename Destination, typename Source>
  226. static Destination op_add(SoftCPU& cpu, Destination& dest, const Source& src)
  227. {
  228. Destination result = 0;
  229. u32 new_flags = 0;
  230. if constexpr (sizeof(Destination) == 4) {
  231. asm volatile("addl %%ecx, %%eax\n"
  232. : "=a"(result)
  233. : "a"(dest), "c"((u32)src));
  234. } else if constexpr (sizeof(Destination) == 2) {
  235. asm volatile("addw %%cx, %%ax\n"
  236. : "=a"(result)
  237. : "a"(dest), "c"((u16)src));
  238. } else if constexpr (sizeof(Destination) == 1) {
  239. asm volatile("addb %%cl, %%al\n"
  240. : "=a"(result)
  241. : "a"(dest), "c"((u8)src));
  242. } else {
  243. ASSERT_NOT_REACHED();
  244. }
  245. asm volatile(
  246. "pushf\n"
  247. "pop %%ebx"
  248. : "=b"(new_flags));
  249. cpu.set_flags_oszapc(new_flags);
  250. return result;
  251. }
  252. template<typename Destination, typename Source>
  253. static Destination op_and(SoftCPU& cpu, Destination& dest, const Source& src)
  254. {
  255. Destination result = 0;
  256. u32 new_flags = 0;
  257. if constexpr (sizeof(Destination) == 4) {
  258. asm volatile("andl %%ecx, %%eax\n"
  259. : "=a"(result)
  260. : "a"(dest), "c"((u32)src));
  261. } else if constexpr (sizeof(Destination) == 2) {
  262. asm volatile("andw %%cx, %%ax\n"
  263. : "=a"(result)
  264. : "a"(dest), "c"((u16)src));
  265. } else if constexpr (sizeof(Destination) == 1) {
  266. asm volatile("andb %%cl, %%al\n"
  267. : "=a"(result)
  268. : "a"(dest), "c"((u8)src));
  269. } else {
  270. ASSERT_NOT_REACHED();
  271. }
  272. asm volatile(
  273. "pushf\n"
  274. "pop %%ebx"
  275. : "=b"(new_flags));
  276. cpu.set_flags_oszpc(new_flags);
  277. return result;
  278. }
  279. template<typename Destination, typename Source>
  280. static typename TypeDoubler<Destination>::type op_imul(SoftCPU& cpu, const Destination& dest, const Source& src)
  281. {
  282. Destination result = 0;
  283. u32 new_flags = 0;
  284. if constexpr (sizeof(Destination) == 4) {
  285. asm volatile("imull %%ecx, %%eax\n"
  286. : "=a"(result)
  287. : "a"(dest), "c"((i32)src));
  288. } else if constexpr (sizeof(Destination) == 2) {
  289. asm volatile("imulw %%cx, %%ax\n"
  290. : "=a"(result)
  291. : "a"(dest), "c"((i16)src));
  292. } else {
  293. ASSERT_NOT_REACHED();
  294. }
  295. asm volatile(
  296. "pushf\n"
  297. "pop %%ebx"
  298. : "=b"(new_flags));
  299. cpu.set_flags_oszapc(new_flags);
  300. return result;
  301. }
  302. template<bool update_dest, typename Op>
  303. void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  304. {
  305. auto dest = al();
  306. auto src = insn.imm8();
  307. auto result = op(*this, dest, src);
  308. if (update_dest)
  309. set_al(result);
  310. }
  311. template<bool update_dest, typename Op>
  312. void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  313. {
  314. auto dest = ax();
  315. auto src = insn.imm16();
  316. auto result = op(*this, dest, src);
  317. if (update_dest)
  318. set_ax(result);
  319. }
  320. template<bool update_dest, typename Op>
  321. void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  322. {
  323. auto dest = eax();
  324. auto src = insn.imm32();
  325. auto result = op(*this, dest, src);
  326. if (update_dest)
  327. set_eax(result);
  328. }
  329. template<bool update_dest, typename Op>
  330. void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  331. {
  332. auto dest = insn.modrm().read16(*this, insn);
  333. auto src = insn.imm16();
  334. auto result = op(*this, dest, src);
  335. if (update_dest)
  336. insn.modrm().write16(*this, insn, result);
  337. }
  338. template<bool update_dest, typename Op>
  339. void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  340. {
  341. auto dest = insn.modrm().read16(*this, insn);
  342. auto src = insn.imm8();
  343. auto result = op(*this, dest, src);
  344. if (update_dest)
  345. insn.modrm().write16(*this, insn, result);
  346. }
  347. template<bool update_dest, typename Op>
  348. void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  349. {
  350. auto dest = insn.modrm().read16(*this, insn);
  351. auto src = gpr16(insn.reg16());
  352. auto result = op(*this, dest, src);
  353. if (update_dest)
  354. insn.modrm().write16(*this, insn, result);
  355. }
  356. template<bool update_dest, typename Op>
  357. void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  358. {
  359. auto dest = insn.modrm().read32(*this, insn);
  360. auto src = insn.imm32();
  361. auto result = op(*this, dest, src);
  362. if (update_dest)
  363. insn.modrm().write32(*this, insn, result);
  364. }
  365. template<bool update_dest, typename Op>
  366. void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  367. {
  368. auto dest = insn.modrm().read32(*this, insn);
  369. auto src = insn.imm8();
  370. auto result = op(*this, dest, src);
  371. if (update_dest)
  372. insn.modrm().write32(*this, insn, result);
  373. }
  374. template<bool update_dest, typename Op>
  375. void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  376. {
  377. auto dest = insn.modrm().read32(*this, insn);
  378. auto src = gpr32(insn.reg32());
  379. auto result = op(*this, dest, src);
  380. if (update_dest)
  381. insn.modrm().write32(*this, insn, result);
  382. }
  383. template<bool update_dest, typename Op>
  384. void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  385. {
  386. auto dest = insn.modrm().read8(*this, insn);
  387. auto src = insn.imm8();
  388. auto result = op(*this, dest, src);
  389. if (update_dest)
  390. insn.modrm().write8(*this, insn, result);
  391. }
  392. template<bool update_dest, typename Op>
  393. void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  394. {
  395. auto dest = insn.modrm().read8(*this, insn);
  396. auto src = gpr8(insn.reg8());
  397. auto result = op(*this, dest, src);
  398. if (update_dest)
  399. insn.modrm().write8(*this, insn, result);
  400. }
  401. template<bool update_dest, typename Op>
  402. void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  403. {
  404. auto dest = gpr16(insn.reg16());
  405. auto src = insn.modrm().read16(*this, insn);
  406. auto result = op(*this, dest, src);
  407. if (update_dest)
  408. gpr16(insn.reg16()) = result;
  409. }
  410. template<bool update_dest, typename Op>
  411. void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  412. {
  413. auto dest = gpr32(insn.reg32());
  414. auto src = insn.modrm().read32(*this, insn);
  415. auto result = op(*this, dest, src);
  416. if (update_dest)
  417. gpr32(insn.reg32()) = result;
  418. }
  419. template<bool update_dest, typename Op>
  420. void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  421. {
  422. auto dest = gpr8(insn.reg8());
  423. auto src = insn.modrm().read8(*this, insn);
  424. auto result = op(*this, dest, src);
  425. if (update_dest)
  426. gpr8(insn.reg8()) = result;
  427. }
  428. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  429. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  430. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  431. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  432. void SoftCPU::ADC_AL_imm8(const X86::Instruction&) { TODO(); }
  433. void SoftCPU::ADC_AX_imm16(const X86::Instruction&) { TODO(); }
  434. void SoftCPU::ADC_EAX_imm32(const X86::Instruction&) { TODO(); }
  435. void SoftCPU::ADC_RM16_imm16(const X86::Instruction&) { TODO(); }
  436. void SoftCPU::ADC_RM16_imm8(const X86::Instruction&) { TODO(); }
  437. void SoftCPU::ADC_RM16_reg16(const X86::Instruction&) { TODO(); }
  438. void SoftCPU::ADC_RM32_imm32(const X86::Instruction&) { TODO(); }
  439. void SoftCPU::ADC_RM32_imm8(const X86::Instruction&) { TODO(); }
  440. void SoftCPU::ADC_RM32_reg32(const X86::Instruction&) { TODO(); }
  441. void SoftCPU::ADC_RM8_imm8(const X86::Instruction&) { TODO(); }
  442. void SoftCPU::ADC_RM8_reg8(const X86::Instruction&) { TODO(); }
  443. void SoftCPU::ADC_reg16_RM16(const X86::Instruction&) { TODO(); }
  444. void SoftCPU::ADC_reg32_RM32(const X86::Instruction&) { TODO(); }
  445. void SoftCPU::ADC_reg8_RM8(const X86::Instruction&) { TODO(); }
  446. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  447. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  448. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  449. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  450. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  451. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  452. void SoftCPU::BSWAP_reg32(const X86::Instruction&) { TODO(); }
  453. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  454. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  455. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  456. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  457. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  458. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  459. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  460. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  461. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  462. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  463. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  464. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  465. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  466. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  467. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  468. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  469. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  470. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  471. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  472. void SoftCPU::CALL_RM32(const X86::Instruction&) { TODO(); }
  473. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  474. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  475. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  476. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  477. {
  478. push32(eip());
  479. set_eip(eip() + (i32)insn.imm32());
  480. }
  481. void SoftCPU::CBW(const X86::Instruction&) { TODO(); }
  482. void SoftCPU::CDQ(const X86::Instruction&) { TODO(); }
  483. void SoftCPU::CLC(const X86::Instruction&) { TODO(); }
  484. void SoftCPU::CLD(const X86::Instruction&) { TODO(); }
  485. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  486. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  487. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  488. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction&) { TODO(); }
  489. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction&) { TODO(); }
  490. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  491. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  492. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  493. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction&) { TODO(); }
  494. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction&) { TODO(); }
  495. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction&) { TODO(); }
  496. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  497. void SoftCPU::CWD(const X86::Instruction&) { TODO(); }
  498. void SoftCPU::CWDE(const X86::Instruction&) { TODO(); }
  499. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  500. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  501. void SoftCPU::DEC_RM16(const X86::Instruction&) { TODO(); }
  502. void SoftCPU::DEC_RM32(const X86::Instruction&) { TODO(); }
  503. void SoftCPU::DEC_RM8(const X86::Instruction&) { TODO(); }
  504. void SoftCPU::DEC_reg16(const X86::Instruction&) { TODO(); }
  505. void SoftCPU::DEC_reg32(const X86::Instruction&) { TODO(); }
  506. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  507. void SoftCPU::DIV_RM32(const X86::Instruction&) { TODO(); }
  508. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  509. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  510. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  511. void SoftCPU::ESCAPE(const X86::Instruction&) { TODO(); }
  512. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  513. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  514. void SoftCPU::IDIV_RM32(const X86::Instruction&) { TODO(); }
  515. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  516. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  517. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  518. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  519. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  520. {
  521. gpr16(insn.reg16()) = op_imul<i16, i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  522. }
  523. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  524. {
  525. gpr16(insn.reg16()) = op_imul<i16, i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  526. }
  527. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  528. {
  529. gpr16(insn.reg16()) = op_imul<i16, i8>(*this, insn.modrm().read16(*this, insn), insn.imm8());
  530. }
  531. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  532. {
  533. gpr32(insn.reg32()) = op_imul<i32, i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  534. }
  535. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  536. {
  537. gpr32(insn.reg32()) = op_imul<i32, i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  538. }
  539. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  540. {
  541. gpr32(insn.reg32()) = op_imul<i32, i8>(*this, insn.modrm().read32(*this, insn), insn.imm8());
  542. }
  543. template<typename T>
  544. static T op_inc(SoftCPU& cpu, T data)
  545. {
  546. T result = 0;
  547. u32 new_flags = 0;
  548. if constexpr (sizeof(T) == 4) {
  549. asm volatile("incl %%eax\n"
  550. : "=a"(result)
  551. : "a"(data));
  552. } else if constexpr (sizeof(T) == 2) {
  553. asm volatile("incw %%ax\n"
  554. : "=a"(result)
  555. : "a"(data));
  556. } else if constexpr (sizeof(T) == 1) {
  557. asm volatile("incb %%al\n"
  558. : "=a"(result)
  559. : "a"(data));
  560. }
  561. asm volatile(
  562. "pushf\n"
  563. "pop %%ebx"
  564. : "=b"(new_flags));
  565. cpu.set_flags_oszap(new_flags);
  566. return result;
  567. }
  568. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  569. {
  570. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  571. }
  572. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  573. {
  574. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  575. }
  576. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  577. {
  578. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  579. }
  580. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  581. {
  582. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  583. }
  584. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  585. {
  586. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  587. }
  588. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  589. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  590. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  591. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  592. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  593. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  594. {
  595. ASSERT(insn.imm8() == 0x82);
  596. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  597. }
  598. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  599. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  600. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  601. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  602. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  603. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  604. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  605. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  606. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  607. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  608. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  609. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  610. void SoftCPU::JMP_RM32(const X86::Instruction&) { TODO(); }
  611. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  612. {
  613. set_eip(eip() + (i16)insn.imm16());
  614. }
  615. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  616. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  617. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  618. {
  619. set_eip(eip() + (i32)insn.imm32());
  620. }
  621. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  622. {
  623. set_eip(eip() + (i8)insn.imm8());
  624. }
  625. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction&) { TODO(); }
  626. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  627. {
  628. if (evaluate_condition(insn.cc()))
  629. set_eip(eip() + (i8)insn.imm8());
  630. }
  631. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  632. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  633. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  634. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  635. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  636. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::LEAVE32(const X86::Instruction&)
  638. {
  639. u32 new_ebp = read_memory32({ ss(), ebp() });
  640. set_esp(ebp() + 4);
  641. set_ebp(new_ebp);
  642. }
  643. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  644. {
  645. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  646. }
  647. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  648. {
  649. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  650. }
  651. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  652. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  653. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  654. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  655. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  656. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  657. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  658. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  659. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  661. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  662. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  663. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  664. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  665. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  666. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  667. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  668. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  669. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  670. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  671. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  672. void SoftCPU::MOVSB(const X86::Instruction&) { TODO(); }
  673. void SoftCPU::MOVSD(const X86::Instruction&) { TODO(); }
  674. void SoftCPU::MOVSW(const X86::Instruction&) { TODO(); }
  675. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction&) { TODO(); }
  676. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction&) { TODO(); }
  677. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction&) { TODO(); }
  678. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  679. {
  680. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  681. }
  682. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  683. {
  684. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  685. }
  686. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  687. {
  688. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  689. }
  690. void SoftCPU::MOV_AL_moff8(const X86::Instruction&) { TODO(); }
  691. void SoftCPU::MOV_AX_moff16(const X86::Instruction&) { TODO(); }
  692. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  693. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  694. void SoftCPU::MOV_EAX_moff32(const X86::Instruction&) { TODO(); }
  695. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  696. {
  697. insn.modrm().write16(*this, insn, insn.imm16());
  698. }
  699. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  700. {
  701. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  702. }
  703. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  704. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  705. {
  706. insn.modrm().write32(*this, insn, insn.imm32());
  707. }
  708. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  709. {
  710. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  711. }
  712. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  713. {
  714. insn.modrm().write8(*this, insn, insn.imm8());
  715. }
  716. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  717. {
  718. insn.modrm().write8(*this, insn, insn.modrm().read8(*this, insn));
  719. }
  720. void SoftCPU::MOV_moff16_AX(const X86::Instruction&) { TODO(); }
  721. void SoftCPU::MOV_moff32_EAX(const X86::Instruction&) { TODO(); }
  722. void SoftCPU::MOV_moff8_AL(const X86::Instruction&) { TODO(); }
  723. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  724. {
  725. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  726. }
  727. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  728. {
  729. gpr16(insn.reg16()) = insn.imm16();
  730. }
  731. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  732. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  733. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  734. {
  735. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  736. }
  737. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  738. {
  739. gpr32(insn.reg32()) = insn.imm32();
  740. }
  741. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  742. {
  743. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  744. }
  745. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  746. {
  747. gpr8(insn.reg8()) = insn.imm8();
  748. }
  749. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  750. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  751. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  752. void SoftCPU::MUL_RM32(const X86::Instruction&) { TODO(); }
  753. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  754. void SoftCPU::NEG_RM16(const X86::Instruction&) { TODO(); }
  755. void SoftCPU::NEG_RM32(const X86::Instruction&) { TODO(); }
  756. void SoftCPU::NEG_RM8(const X86::Instruction&) { TODO(); }
  757. void SoftCPU::NOP(const X86::Instruction&) { TODO(); }
  758. void SoftCPU::NOT_RM16(const X86::Instruction&) { TODO(); }
  759. void SoftCPU::NOT_RM32(const X86::Instruction&) { TODO(); }
  760. void SoftCPU::NOT_RM8(const X86::Instruction&) { TODO(); }
  761. void SoftCPU::OR_AL_imm8(const X86::Instruction&) { TODO(); }
  762. void SoftCPU::OR_AX_imm16(const X86::Instruction&) { TODO(); }
  763. void SoftCPU::OR_EAX_imm32(const X86::Instruction&) { TODO(); }
  764. void SoftCPU::OR_RM16_imm16(const X86::Instruction&) { TODO(); }
  765. void SoftCPU::OR_RM16_imm8(const X86::Instruction&) { TODO(); }
  766. void SoftCPU::OR_RM16_reg16(const X86::Instruction&) { TODO(); }
  767. void SoftCPU::OR_RM32_imm32(const X86::Instruction&) { TODO(); }
  768. void SoftCPU::OR_RM32_imm8(const X86::Instruction&) { TODO(); }
  769. void SoftCPU::OR_RM32_reg32(const X86::Instruction&) { TODO(); }
  770. void SoftCPU::OR_RM8_imm8(const X86::Instruction&) { TODO(); }
  771. void SoftCPU::OR_RM8_reg8(const X86::Instruction&) { TODO(); }
  772. void SoftCPU::OR_reg16_RM16(const X86::Instruction&) { TODO(); }
  773. void SoftCPU::OR_reg32_RM32(const X86::Instruction&) { TODO(); }
  774. void SoftCPU::OR_reg8_RM8(const X86::Instruction&) { TODO(); }
  775. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  776. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  777. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  778. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  779. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  780. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  781. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  782. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  783. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  784. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  785. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  786. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  787. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  788. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  789. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  790. void SoftCPU::POPFD(const X86::Instruction&) { TODO(); }
  791. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  792. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  793. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  794. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  795. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  796. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  797. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  800. {
  801. gpr32(insn.reg32()) = pop32();
  802. }
  803. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  804. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  805. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  806. void SoftCPU::PUSHFD(const X86::Instruction&) { TODO(); }
  807. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  808. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  809. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  810. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  811. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  812. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  813. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  814. {
  815. push32(insn.modrm().read32(*this, insn));
  816. }
  817. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  818. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  819. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  820. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  821. {
  822. push32(insn.imm32());
  823. }
  824. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  825. {
  826. ASSERT(!insn.has_operand_size_override_prefix());
  827. push32((i32)insn.imm8());
  828. }
  829. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  830. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  831. {
  832. push32(gpr32(insn.reg32()));
  833. }
  834. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  835. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  836. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  837. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  838. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  839. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  840. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  841. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  842. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  843. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  844. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  845. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  846. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  847. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  848. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  849. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  850. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  851. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  852. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  853. void SoftCPU::RET(const X86::Instruction& insn)
  854. {
  855. ASSERT(!insn.has_operand_size_override_prefix());
  856. set_eip(pop32());
  857. }
  858. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  859. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  860. void SoftCPU::RET_imm16(const X86::Instruction&) { TODO(); }
  861. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  862. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  863. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  864. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  865. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  867. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  868. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  869. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  870. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  871. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  872. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  873. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  874. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  875. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  876. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  877. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  878. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  879. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  880. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  881. template<typename T>
  882. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  883. {
  884. if (steps == 0)
  885. return data;
  886. u32 result = 0;
  887. u32 new_flags = 0;
  888. if constexpr (sizeof(T) == 4)
  889. asm volatile("sarl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  890. else if constexpr (sizeof(T) == 2)
  891. asm volatile("sarw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  892. else if constexpr (sizeof(T) == 1)
  893. asm volatile("sarb %%cl, %%al\n" ::"a"(data), "c"(steps));
  894. asm volatile(
  895. "mov %%eax, %%ebx\n"
  896. : "=b"(result));
  897. asm volatile(
  898. "pushf\n"
  899. "pop %%eax"
  900. : "=a"(new_flags));
  901. cpu.set_flags_oszapc(new_flags);
  902. return result;
  903. }
  904. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  905. {
  906. auto data = insn.modrm().read16(*this, insn);
  907. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  908. }
  909. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  910. {
  911. auto data = insn.modrm().read16(*this, insn);
  912. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  913. }
  914. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  915. {
  916. auto data = insn.modrm().read16(*this, insn);
  917. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  918. }
  919. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  920. {
  921. auto data = insn.modrm().read32(*this, insn);
  922. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  923. }
  924. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  925. {
  926. auto data = insn.modrm().read32(*this, insn);
  927. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  928. }
  929. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  930. {
  931. auto data = insn.modrm().read32(*this, insn);
  932. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  933. }
  934. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  935. {
  936. auto data = insn.modrm().read8(*this, insn);
  937. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  938. }
  939. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  940. {
  941. auto data = insn.modrm().read8(*this, insn);
  942. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  943. }
  944. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  945. {
  946. auto data = insn.modrm().read8(*this, insn);
  947. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  948. }
  949. void SoftCPU::SBB_AL_imm8(const X86::Instruction&) { TODO(); }
  950. void SoftCPU::SBB_AX_imm16(const X86::Instruction&) { TODO(); }
  951. void SoftCPU::SBB_EAX_imm32(const X86::Instruction&) { TODO(); }
  952. void SoftCPU::SBB_RM16_imm16(const X86::Instruction&) { TODO(); }
  953. void SoftCPU::SBB_RM16_imm8(const X86::Instruction&) { TODO(); }
  954. void SoftCPU::SBB_RM16_reg16(const X86::Instruction&) { TODO(); }
  955. void SoftCPU::SBB_RM32_imm32(const X86::Instruction&) { TODO(); }
  956. void SoftCPU::SBB_RM32_imm8(const X86::Instruction&) { TODO(); }
  957. void SoftCPU::SBB_RM32_reg32(const X86::Instruction&) { TODO(); }
  958. void SoftCPU::SBB_RM8_imm8(const X86::Instruction&) { TODO(); }
  959. void SoftCPU::SBB_RM8_reg8(const X86::Instruction&) { TODO(); }
  960. void SoftCPU::SBB_reg16_RM16(const X86::Instruction&) { TODO(); }
  961. void SoftCPU::SBB_reg32_RM32(const X86::Instruction&) { TODO(); }
  962. void SoftCPU::SBB_reg8_RM8(const X86::Instruction&) { TODO(); }
  963. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  964. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  965. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  966. void SoftCPU::SETcc_RM8(const X86::Instruction&) { TODO(); }
  967. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  968. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  969. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  970. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  971. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  972. void SoftCPU::SHL_RM16_1(const X86::Instruction&) { TODO(); }
  973. void SoftCPU::SHL_RM16_CL(const X86::Instruction&) { TODO(); }
  974. void SoftCPU::SHL_RM16_imm8(const X86::Instruction&) { TODO(); }
  975. void SoftCPU::SHL_RM32_1(const X86::Instruction&) { TODO(); }
  976. void SoftCPU::SHL_RM32_CL(const X86::Instruction&) { TODO(); }
  977. void SoftCPU::SHL_RM32_imm8(const X86::Instruction&) { TODO(); }
  978. void SoftCPU::SHL_RM8_1(const X86::Instruction&) { TODO(); }
  979. void SoftCPU::SHL_RM8_CL(const X86::Instruction&) { TODO(); }
  980. void SoftCPU::SHL_RM8_imm8(const X86::Instruction&) { TODO(); }
  981. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  982. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  983. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  984. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  985. void SoftCPU::SHR_RM16_1(const X86::Instruction&) { TODO(); }
  986. void SoftCPU::SHR_RM16_CL(const X86::Instruction&) { TODO(); }
  987. void SoftCPU::SHR_RM16_imm8(const X86::Instruction&) { TODO(); }
  988. void SoftCPU::SHR_RM32_1(const X86::Instruction&) { TODO(); }
  989. void SoftCPU::SHR_RM32_CL(const X86::Instruction&) { TODO(); }
  990. void SoftCPU::SHR_RM32_imm8(const X86::Instruction&) { TODO(); }
  991. void SoftCPU::SHR_RM8_1(const X86::Instruction&) { TODO(); }
  992. void SoftCPU::SHR_RM8_CL(const X86::Instruction&) { TODO(); }
  993. void SoftCPU::SHR_RM8_imm8(const X86::Instruction&) { TODO(); }
  994. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  995. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  996. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  997. void SoftCPU::STC(const X86::Instruction&) { TODO(); }
  998. void SoftCPU::STD(const X86::Instruction&) { TODO(); }
  999. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1000. void SoftCPU::STOSB(const X86::Instruction& insn)
  1001. {
  1002. if (insn.has_address_size_override_prefix()) {
  1003. do_once_or_repeat<false>(insn, [&] {
  1004. write_memory8({ es(), di() }, al());
  1005. set_di(di() + (df() ? -1 : 1));
  1006. });
  1007. } else {
  1008. do_once_or_repeat<false>(insn, [&] {
  1009. write_memory8({ es(), edi() }, al());
  1010. set_edi(edi() + (df() ? -1 : 1));
  1011. });
  1012. }
  1013. }
  1014. void SoftCPU::STOSD(const X86::Instruction& insn)
  1015. {
  1016. if (insn.has_address_size_override_prefix()) {
  1017. do_once_or_repeat<false>(insn, [&] {
  1018. write_memory32({ es(), di() }, eax());
  1019. set_di(di() + (df() ? -4 : 4));
  1020. });
  1021. } else {
  1022. do_once_or_repeat<false>(insn, [&] {
  1023. write_memory32({ es(), edi() }, eax());
  1024. set_edi(edi() + (df() ? -4 : 4));
  1025. });
  1026. }
  1027. }
  1028. void SoftCPU::STOSW(const X86::Instruction& insn)
  1029. {
  1030. if (insn.has_address_size_override_prefix()) {
  1031. do_once_or_repeat<false>(insn, [&] {
  1032. write_memory16({ es(), di() }, ax());
  1033. set_di(di() + (df() ? -2 : 2));
  1034. });
  1035. } else {
  1036. do_once_or_repeat<false>(insn, [&] {
  1037. write_memory16({ es(), edi() }, ax());
  1038. set_edi(edi() + (df() ? -2 : 2));
  1039. });
  1040. }
  1041. }
  1042. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1043. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1044. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1045. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1046. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1047. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1048. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1049. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1050. void SoftCPU::XADD_RM16_reg16(const X86::Instruction&) { TODO(); }
  1051. void SoftCPU::XADD_RM32_reg32(const X86::Instruction&) { TODO(); }
  1052. void SoftCPU::XADD_RM8_reg8(const X86::Instruction&) { TODO(); }
  1053. void SoftCPU::XCHG_AX_reg16(const X86::Instruction&) { TODO(); }
  1054. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction&) { TODO(); }
  1055. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction&) { TODO(); }
  1056. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction&) { TODO(); }
  1057. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction&) { TODO(); }
  1058. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1059. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1060. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8, u8>, insn); } \
  1061. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16, u16>, insn); } \
  1062. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32, u32>, insn); } \
  1063. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16, u16>, insn); } \
  1064. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16, u16>, insn); } \
  1065. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32, u32>, insn); } \
  1066. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32, u32>, insn); } \
  1067. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8, u8>, insn); } \
  1068. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8, u8>, insn); }
  1069. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1070. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1071. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16, u8>, insn); } \
  1072. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32, u8>, insn); } \
  1073. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16, u16>, insn); } \
  1074. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32, u32>, insn); } \
  1075. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8, u8>, insn); }
  1076. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1077. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1078. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1079. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1080. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1081. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1082. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1083. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1084. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1085. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1086. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1087. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1088. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1089. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1090. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1091. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1092. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1093. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1094. }