SoftCPU.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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_or(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("orl %%ecx, %%eax\n"
  205. : "=a"(result)
  206. : "a"(dest), "c"((u32)src));
  207. } else if constexpr (sizeof(Destination) == 2) {
  208. asm volatile("or %%cx, %%ax\n"
  209. : "=a"(result)
  210. : "a"(dest), "c"((u16)src));
  211. } else if constexpr (sizeof(Destination) == 1) {
  212. asm volatile("orb %%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_oszpc(new_flags);
  223. return result;
  224. }
  225. template<typename Destination, typename Source>
  226. static typename TypeDoubler<Destination>::type op_sub(SoftCPU& cpu, const Destination& dest, const Source& src)
  227. {
  228. Destination result = 0;
  229. u32 new_flags = 0;
  230. if constexpr (sizeof(Destination) == 4) {
  231. asm volatile("subl %%ecx, %%eax\n"
  232. : "=a"(result)
  233. : "a"(dest), "c"((u32)src));
  234. } else if constexpr (sizeof(Destination) == 2) {
  235. asm volatile("subw %%cx, %%ax\n"
  236. : "=a"(result)
  237. : "a"(dest), "c"((u16)src));
  238. } else if constexpr (sizeof(Destination) == 1) {
  239. asm volatile("subb %%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_add(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("addl %%ecx, %%eax\n"
  259. : "=a"(result)
  260. : "a"(dest), "c"((u32)src));
  261. } else if constexpr (sizeof(Destination) == 2) {
  262. asm volatile("addw %%cx, %%ax\n"
  263. : "=a"(result)
  264. : "a"(dest), "c"((u16)src));
  265. } else if constexpr (sizeof(Destination) == 1) {
  266. asm volatile("addb %%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_oszapc(new_flags);
  277. return result;
  278. }
  279. template<typename Destination, typename Source>
  280. static Destination op_and(SoftCPU& cpu, Destination& dest, const Source& src)
  281. {
  282. Destination result = 0;
  283. u32 new_flags = 0;
  284. if constexpr (sizeof(Destination) == 4) {
  285. asm volatile("andl %%ecx, %%eax\n"
  286. : "=a"(result)
  287. : "a"(dest), "c"((u32)src));
  288. } else if constexpr (sizeof(Destination) == 2) {
  289. asm volatile("andw %%cx, %%ax\n"
  290. : "=a"(result)
  291. : "a"(dest), "c"((u16)src));
  292. } else if constexpr (sizeof(Destination) == 1) {
  293. asm volatile("andb %%cl, %%al\n"
  294. : "=a"(result)
  295. : "a"(dest), "c"((u8)src));
  296. } else {
  297. ASSERT_NOT_REACHED();
  298. }
  299. asm volatile(
  300. "pushf\n"
  301. "pop %%ebx"
  302. : "=b"(new_flags));
  303. cpu.set_flags_oszpc(new_flags);
  304. return result;
  305. }
  306. template<typename Destination, typename Source>
  307. static typename TypeDoubler<Destination>::type op_imul(SoftCPU& cpu, const Destination& dest, const Source& src)
  308. {
  309. Destination result = 0;
  310. u32 new_flags = 0;
  311. if constexpr (sizeof(Destination) == 4) {
  312. asm volatile("imull %%ecx, %%eax\n"
  313. : "=a"(result)
  314. : "a"(dest), "c"((i32)src));
  315. } else if constexpr (sizeof(Destination) == 2) {
  316. asm volatile("imulw %%cx, %%ax\n"
  317. : "=a"(result)
  318. : "a"(dest), "c"((i16)src));
  319. } else {
  320. ASSERT_NOT_REACHED();
  321. }
  322. asm volatile(
  323. "pushf\n"
  324. "pop %%ebx"
  325. : "=b"(new_flags));
  326. cpu.set_flags_oszapc(new_flags);
  327. return result;
  328. }
  329. template<bool update_dest, typename Op>
  330. void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  331. {
  332. auto dest = al();
  333. auto src = insn.imm8();
  334. auto result = op(*this, dest, src);
  335. if (update_dest)
  336. set_al(result);
  337. }
  338. template<bool update_dest, typename Op>
  339. void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  340. {
  341. auto dest = ax();
  342. auto src = insn.imm16();
  343. auto result = op(*this, dest, src);
  344. if (update_dest)
  345. set_ax(result);
  346. }
  347. template<bool update_dest, typename Op>
  348. void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  349. {
  350. auto dest = eax();
  351. auto src = insn.imm32();
  352. auto result = op(*this, dest, src);
  353. if (update_dest)
  354. set_eax(result);
  355. }
  356. template<bool update_dest, typename Op>
  357. void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  358. {
  359. auto dest = insn.modrm().read16(*this, insn);
  360. auto src = insn.imm16();
  361. auto result = op(*this, dest, src);
  362. if (update_dest)
  363. insn.modrm().write16(*this, insn, result);
  364. }
  365. template<bool update_dest, typename Op>
  366. void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  367. {
  368. auto dest = insn.modrm().read16(*this, insn);
  369. auto src = insn.imm8();
  370. auto result = op(*this, dest, src);
  371. if (update_dest)
  372. insn.modrm().write16(*this, insn, result);
  373. }
  374. template<bool update_dest, typename Op>
  375. void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  376. {
  377. auto dest = insn.modrm().read16(*this, insn);
  378. auto src = gpr16(insn.reg16());
  379. auto result = op(*this, dest, src);
  380. if (update_dest)
  381. insn.modrm().write16(*this, insn, result);
  382. }
  383. template<bool update_dest, typename Op>
  384. void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  385. {
  386. auto dest = insn.modrm().read32(*this, insn);
  387. auto src = insn.imm32();
  388. auto result = op(*this, dest, src);
  389. if (update_dest)
  390. insn.modrm().write32(*this, insn, result);
  391. }
  392. template<bool update_dest, typename Op>
  393. void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  394. {
  395. auto dest = insn.modrm().read32(*this, insn);
  396. auto src = insn.imm8();
  397. auto result = op(*this, dest, src);
  398. if (update_dest)
  399. insn.modrm().write32(*this, insn, result);
  400. }
  401. template<bool update_dest, typename Op>
  402. void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  403. {
  404. auto dest = insn.modrm().read32(*this, insn);
  405. auto src = gpr32(insn.reg32());
  406. auto result = op(*this, dest, src);
  407. if (update_dest)
  408. insn.modrm().write32(*this, insn, result);
  409. }
  410. template<bool update_dest, typename Op>
  411. void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  412. {
  413. auto dest = insn.modrm().read8(*this, insn);
  414. auto src = insn.imm8();
  415. auto result = op(*this, dest, src);
  416. if (update_dest)
  417. insn.modrm().write8(*this, insn, result);
  418. }
  419. template<bool update_dest, typename Op>
  420. void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  421. {
  422. auto dest = insn.modrm().read8(*this, insn);
  423. auto src = gpr8(insn.reg8());
  424. auto result = op(*this, dest, src);
  425. if (update_dest)
  426. insn.modrm().write8(*this, insn, result);
  427. }
  428. template<bool update_dest, typename Op>
  429. void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  430. {
  431. auto dest = gpr16(insn.reg16());
  432. auto src = insn.modrm().read16(*this, insn);
  433. auto result = op(*this, dest, src);
  434. if (update_dest)
  435. gpr16(insn.reg16()) = result;
  436. }
  437. template<bool update_dest, typename Op>
  438. void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  439. {
  440. auto dest = gpr32(insn.reg32());
  441. auto src = insn.modrm().read32(*this, insn);
  442. auto result = op(*this, dest, src);
  443. if (update_dest)
  444. gpr32(insn.reg32()) = result;
  445. }
  446. template<bool update_dest, typename Op>
  447. void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  448. {
  449. auto dest = gpr8(insn.reg8());
  450. auto src = insn.modrm().read8(*this, insn);
  451. auto result = op(*this, dest, src);
  452. if (update_dest)
  453. gpr8(insn.reg8()) = result;
  454. }
  455. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  456. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  457. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  458. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  459. void SoftCPU::ADC_AL_imm8(const X86::Instruction&) { TODO(); }
  460. void SoftCPU::ADC_AX_imm16(const X86::Instruction&) { TODO(); }
  461. void SoftCPU::ADC_EAX_imm32(const X86::Instruction&) { TODO(); }
  462. void SoftCPU::ADC_RM16_imm16(const X86::Instruction&) { TODO(); }
  463. void SoftCPU::ADC_RM16_imm8(const X86::Instruction&) { TODO(); }
  464. void SoftCPU::ADC_RM16_reg16(const X86::Instruction&) { TODO(); }
  465. void SoftCPU::ADC_RM32_imm32(const X86::Instruction&) { TODO(); }
  466. void SoftCPU::ADC_RM32_imm8(const X86::Instruction&) { TODO(); }
  467. void SoftCPU::ADC_RM32_reg32(const X86::Instruction&) { TODO(); }
  468. void SoftCPU::ADC_RM8_imm8(const X86::Instruction&) { TODO(); }
  469. void SoftCPU::ADC_RM8_reg8(const X86::Instruction&) { TODO(); }
  470. void SoftCPU::ADC_reg16_RM16(const X86::Instruction&) { TODO(); }
  471. void SoftCPU::ADC_reg32_RM32(const X86::Instruction&) { TODO(); }
  472. void SoftCPU::ADC_reg8_RM8(const X86::Instruction&) { TODO(); }
  473. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  474. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  475. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  476. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  477. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  478. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  479. void SoftCPU::BSWAP_reg32(const X86::Instruction&) { TODO(); }
  480. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  481. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  482. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  483. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  484. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  485. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  486. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  487. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  488. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  489. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  490. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  491. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  492. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  493. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  494. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  495. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  496. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  497. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  498. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  499. void SoftCPU::CALL_RM32(const X86::Instruction&) { TODO(); }
  500. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  501. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  502. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  503. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  504. {
  505. push32(eip());
  506. set_eip(eip() + (i32)insn.imm32());
  507. }
  508. void SoftCPU::CBW(const X86::Instruction&) { TODO(); }
  509. void SoftCPU::CDQ(const X86::Instruction&) { TODO(); }
  510. void SoftCPU::CLC(const X86::Instruction&) { TODO(); }
  511. void SoftCPU::CLD(const X86::Instruction&) { TODO(); }
  512. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  513. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  514. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  515. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction&) { TODO(); }
  516. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction&) { TODO(); }
  517. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  518. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  519. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  520. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction&) { TODO(); }
  521. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction&) { TODO(); }
  522. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction&) { TODO(); }
  523. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  524. void SoftCPU::CWD(const X86::Instruction&) { TODO(); }
  525. void SoftCPU::CWDE(const X86::Instruction&) { TODO(); }
  526. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  527. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  528. void SoftCPU::DEC_RM16(const X86::Instruction&) { TODO(); }
  529. void SoftCPU::DEC_RM32(const X86::Instruction&) { TODO(); }
  530. void SoftCPU::DEC_RM8(const X86::Instruction&) { TODO(); }
  531. void SoftCPU::DEC_reg16(const X86::Instruction&) { TODO(); }
  532. void SoftCPU::DEC_reg32(const X86::Instruction&) { TODO(); }
  533. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  534. void SoftCPU::DIV_RM32(const X86::Instruction&) { TODO(); }
  535. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  536. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  537. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  538. void SoftCPU::ESCAPE(const X86::Instruction&) { TODO(); }
  539. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  540. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  541. void SoftCPU::IDIV_RM32(const X86::Instruction&) { TODO(); }
  542. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  543. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  544. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  545. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  546. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  547. {
  548. gpr16(insn.reg16()) = op_imul<i16, i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  549. }
  550. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  551. {
  552. gpr16(insn.reg16()) = op_imul<i16, i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  553. }
  554. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  555. {
  556. gpr16(insn.reg16()) = op_imul<i16, i8>(*this, insn.modrm().read16(*this, insn), insn.imm8());
  557. }
  558. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  559. {
  560. gpr32(insn.reg32()) = op_imul<i32, i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  561. }
  562. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  563. {
  564. gpr32(insn.reg32()) = op_imul<i32, i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  565. }
  566. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  567. {
  568. gpr32(insn.reg32()) = op_imul<i32, i8>(*this, insn.modrm().read32(*this, insn), insn.imm8());
  569. }
  570. template<typename T>
  571. static T op_inc(SoftCPU& cpu, T data)
  572. {
  573. T result = 0;
  574. u32 new_flags = 0;
  575. if constexpr (sizeof(T) == 4) {
  576. asm volatile("incl %%eax\n"
  577. : "=a"(result)
  578. : "a"(data));
  579. } else if constexpr (sizeof(T) == 2) {
  580. asm volatile("incw %%ax\n"
  581. : "=a"(result)
  582. : "a"(data));
  583. } else if constexpr (sizeof(T) == 1) {
  584. asm volatile("incb %%al\n"
  585. : "=a"(result)
  586. : "a"(data));
  587. }
  588. asm volatile(
  589. "pushf\n"
  590. "pop %%ebx"
  591. : "=b"(new_flags));
  592. cpu.set_flags_oszap(new_flags);
  593. return result;
  594. }
  595. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  596. {
  597. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  598. }
  599. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  600. {
  601. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  602. }
  603. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  604. {
  605. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  606. }
  607. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  608. {
  609. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  610. }
  611. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  612. {
  613. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  614. }
  615. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  616. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  617. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  618. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  619. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  620. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  621. {
  622. ASSERT(insn.imm8() == 0x82);
  623. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  624. }
  625. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  626. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  627. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  628. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  629. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  630. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  631. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  632. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  633. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  634. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  635. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  636. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::JMP_RM32(const X86::Instruction&) { TODO(); }
  638. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  639. {
  640. set_eip(eip() + (i16)insn.imm16());
  641. }
  642. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  643. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  644. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  645. {
  646. set_eip(eip() + (i32)insn.imm32());
  647. }
  648. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  649. {
  650. set_eip(eip() + (i8)insn.imm8());
  651. }
  652. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction&) { TODO(); }
  653. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  654. {
  655. if (evaluate_condition(insn.cc()))
  656. set_eip(eip() + (i8)insn.imm8());
  657. }
  658. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  659. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  661. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  662. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  663. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  664. void SoftCPU::LEAVE32(const X86::Instruction&)
  665. {
  666. u32 new_ebp = read_memory32({ ss(), ebp() });
  667. set_esp(ebp() + 4);
  668. set_ebp(new_ebp);
  669. }
  670. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  671. {
  672. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  673. }
  674. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  675. {
  676. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  677. }
  678. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  679. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  680. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  681. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  682. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  683. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  684. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  685. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  686. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  687. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  688. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  689. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  690. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  691. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  692. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  693. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  694. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  695. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  696. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  697. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  698. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  699. void SoftCPU::MOVSB(const X86::Instruction&) { TODO(); }
  700. void SoftCPU::MOVSD(const X86::Instruction&) { TODO(); }
  701. void SoftCPU::MOVSW(const X86::Instruction&) { TODO(); }
  702. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction&) { TODO(); }
  703. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction&) { TODO(); }
  704. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction&) { TODO(); }
  705. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  706. {
  707. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  708. }
  709. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  710. {
  711. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  712. }
  713. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  714. {
  715. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  716. }
  717. void SoftCPU::MOV_AL_moff8(const X86::Instruction&) { TODO(); }
  718. void SoftCPU::MOV_AX_moff16(const X86::Instruction&) { TODO(); }
  719. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  720. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  721. void SoftCPU::MOV_EAX_moff32(const X86::Instruction&) { TODO(); }
  722. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  723. {
  724. insn.modrm().write16(*this, insn, insn.imm16());
  725. }
  726. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  727. {
  728. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  729. }
  730. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  731. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  732. {
  733. insn.modrm().write32(*this, insn, insn.imm32());
  734. }
  735. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  736. {
  737. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  738. }
  739. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  740. {
  741. insn.modrm().write8(*this, insn, insn.imm8());
  742. }
  743. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  744. {
  745. insn.modrm().write8(*this, insn, insn.modrm().read8(*this, insn));
  746. }
  747. void SoftCPU::MOV_moff16_AX(const X86::Instruction&) { TODO(); }
  748. void SoftCPU::MOV_moff32_EAX(const X86::Instruction&) { TODO(); }
  749. void SoftCPU::MOV_moff8_AL(const X86::Instruction&) { TODO(); }
  750. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  751. {
  752. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  753. }
  754. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  755. {
  756. gpr16(insn.reg16()) = insn.imm16();
  757. }
  758. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  759. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  760. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  761. {
  762. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  763. }
  764. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  765. {
  766. gpr32(insn.reg32()) = insn.imm32();
  767. }
  768. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  769. {
  770. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  771. }
  772. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  773. {
  774. gpr8(insn.reg8()) = insn.imm8();
  775. }
  776. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  777. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  778. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  779. void SoftCPU::MUL_RM32(const X86::Instruction&) { TODO(); }
  780. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  781. void SoftCPU::NEG_RM16(const X86::Instruction&) { TODO(); }
  782. void SoftCPU::NEG_RM32(const X86::Instruction&) { TODO(); }
  783. void SoftCPU::NEG_RM8(const X86::Instruction&) { TODO(); }
  784. void SoftCPU::NOP(const X86::Instruction&) { TODO(); }
  785. void SoftCPU::NOT_RM16(const X86::Instruction&) { TODO(); }
  786. void SoftCPU::NOT_RM32(const X86::Instruction&) { TODO(); }
  787. void SoftCPU::NOT_RM8(const X86::Instruction&) { TODO(); }
  788. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  789. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  790. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  791. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  792. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  793. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  794. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  795. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  796. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  797. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  800. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  801. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  802. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  803. void SoftCPU::POPFD(const X86::Instruction&) { TODO(); }
  804. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  805. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  806. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  807. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  808. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  809. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  810. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  811. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  812. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  813. {
  814. gpr32(insn.reg32()) = pop32();
  815. }
  816. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  817. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  818. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  819. void SoftCPU::PUSHFD(const X86::Instruction&) { TODO(); }
  820. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  821. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  822. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  823. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  824. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  825. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  826. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  827. {
  828. push32(insn.modrm().read32(*this, insn));
  829. }
  830. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  831. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  832. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  833. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  834. {
  835. push32(insn.imm32());
  836. }
  837. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  838. {
  839. ASSERT(!insn.has_operand_size_override_prefix());
  840. push32((i32)insn.imm8());
  841. }
  842. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  843. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  844. {
  845. push32(gpr32(insn.reg32()));
  846. }
  847. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  848. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  849. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  850. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  851. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  852. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  853. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  854. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  855. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  856. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  857. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  858. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  859. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  860. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  861. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  862. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  863. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  864. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  865. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::RET(const X86::Instruction& insn)
  867. {
  868. ASSERT(!insn.has_operand_size_override_prefix());
  869. set_eip(pop32());
  870. }
  871. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  872. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  873. void SoftCPU::RET_imm16(const X86::Instruction&) { TODO(); }
  874. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  875. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  876. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  877. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  878. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  879. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  880. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  881. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  882. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  883. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  884. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  885. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  886. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  887. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  888. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  889. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  890. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  891. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  892. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  893. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  894. template<typename T>
  895. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  896. {
  897. if (steps == 0)
  898. return data;
  899. u32 result = 0;
  900. u32 new_flags = 0;
  901. if constexpr (sizeof(T) == 4)
  902. asm volatile("sarl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  903. else if constexpr (sizeof(T) == 2)
  904. asm volatile("sarw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  905. else if constexpr (sizeof(T) == 1)
  906. asm volatile("sarb %%cl, %%al\n" ::"a"(data), "c"(steps));
  907. asm volatile(
  908. "mov %%eax, %%ebx\n"
  909. : "=b"(result));
  910. asm volatile(
  911. "pushf\n"
  912. "pop %%eax"
  913. : "=a"(new_flags));
  914. cpu.set_flags_oszapc(new_flags);
  915. return result;
  916. }
  917. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  918. {
  919. auto data = insn.modrm().read16(*this, insn);
  920. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  921. }
  922. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  923. {
  924. auto data = insn.modrm().read16(*this, insn);
  925. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  926. }
  927. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  928. {
  929. auto data = insn.modrm().read16(*this, insn);
  930. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  931. }
  932. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  933. {
  934. auto data = insn.modrm().read32(*this, insn);
  935. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  936. }
  937. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  938. {
  939. auto data = insn.modrm().read32(*this, insn);
  940. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  941. }
  942. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  943. {
  944. auto data = insn.modrm().read32(*this, insn);
  945. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  946. }
  947. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  948. {
  949. auto data = insn.modrm().read8(*this, insn);
  950. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  951. }
  952. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  953. {
  954. auto data = insn.modrm().read8(*this, insn);
  955. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  956. }
  957. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  958. {
  959. auto data = insn.modrm().read8(*this, insn);
  960. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  961. }
  962. void SoftCPU::SBB_AL_imm8(const X86::Instruction&) { TODO(); }
  963. void SoftCPU::SBB_AX_imm16(const X86::Instruction&) { TODO(); }
  964. void SoftCPU::SBB_EAX_imm32(const X86::Instruction&) { TODO(); }
  965. void SoftCPU::SBB_RM16_imm16(const X86::Instruction&) { TODO(); }
  966. void SoftCPU::SBB_RM16_imm8(const X86::Instruction&) { TODO(); }
  967. void SoftCPU::SBB_RM16_reg16(const X86::Instruction&) { TODO(); }
  968. void SoftCPU::SBB_RM32_imm32(const X86::Instruction&) { TODO(); }
  969. void SoftCPU::SBB_RM32_imm8(const X86::Instruction&) { TODO(); }
  970. void SoftCPU::SBB_RM32_reg32(const X86::Instruction&) { TODO(); }
  971. void SoftCPU::SBB_RM8_imm8(const X86::Instruction&) { TODO(); }
  972. void SoftCPU::SBB_RM8_reg8(const X86::Instruction&) { TODO(); }
  973. void SoftCPU::SBB_reg16_RM16(const X86::Instruction&) { TODO(); }
  974. void SoftCPU::SBB_reg32_RM32(const X86::Instruction&) { TODO(); }
  975. void SoftCPU::SBB_reg8_RM8(const X86::Instruction&) { TODO(); }
  976. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  977. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  978. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  979. void SoftCPU::SETcc_RM8(const X86::Instruction&) { TODO(); }
  980. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  981. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  982. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  983. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  984. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  985. void SoftCPU::SHL_RM16_1(const X86::Instruction&) { TODO(); }
  986. void SoftCPU::SHL_RM16_CL(const X86::Instruction&) { TODO(); }
  987. void SoftCPU::SHL_RM16_imm8(const X86::Instruction&) { TODO(); }
  988. void SoftCPU::SHL_RM32_1(const X86::Instruction&) { TODO(); }
  989. void SoftCPU::SHL_RM32_CL(const X86::Instruction&) { TODO(); }
  990. void SoftCPU::SHL_RM32_imm8(const X86::Instruction&) { TODO(); }
  991. void SoftCPU::SHL_RM8_1(const X86::Instruction&) { TODO(); }
  992. void SoftCPU::SHL_RM8_CL(const X86::Instruction&) { TODO(); }
  993. void SoftCPU::SHL_RM8_imm8(const X86::Instruction&) { TODO(); }
  994. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  995. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  996. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  997. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  998. void SoftCPU::SHR_RM16_1(const X86::Instruction&) { TODO(); }
  999. void SoftCPU::SHR_RM16_CL(const X86::Instruction&) { TODO(); }
  1000. void SoftCPU::SHR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1001. void SoftCPU::SHR_RM32_1(const X86::Instruction&) { TODO(); }
  1002. void SoftCPU::SHR_RM32_CL(const X86::Instruction&) { TODO(); }
  1003. void SoftCPU::SHR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1004. void SoftCPU::SHR_RM8_1(const X86::Instruction&) { TODO(); }
  1005. void SoftCPU::SHR_RM8_CL(const X86::Instruction&) { TODO(); }
  1006. void SoftCPU::SHR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1007. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1008. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1009. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1010. void SoftCPU::STC(const X86::Instruction&) { TODO(); }
  1011. void SoftCPU::STD(const X86::Instruction&) { TODO(); }
  1012. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1013. void SoftCPU::STOSB(const X86::Instruction& insn)
  1014. {
  1015. if (insn.has_address_size_override_prefix()) {
  1016. do_once_or_repeat<false>(insn, [&] {
  1017. write_memory8({ es(), di() }, al());
  1018. set_di(di() + (df() ? -1 : 1));
  1019. });
  1020. } else {
  1021. do_once_or_repeat<false>(insn, [&] {
  1022. write_memory8({ es(), edi() }, al());
  1023. set_edi(edi() + (df() ? -1 : 1));
  1024. });
  1025. }
  1026. }
  1027. void SoftCPU::STOSD(const X86::Instruction& insn)
  1028. {
  1029. if (insn.has_address_size_override_prefix()) {
  1030. do_once_or_repeat<false>(insn, [&] {
  1031. write_memory32({ es(), di() }, eax());
  1032. set_di(di() + (df() ? -4 : 4));
  1033. });
  1034. } else {
  1035. do_once_or_repeat<false>(insn, [&] {
  1036. write_memory32({ es(), edi() }, eax());
  1037. set_edi(edi() + (df() ? -4 : 4));
  1038. });
  1039. }
  1040. }
  1041. void SoftCPU::STOSW(const X86::Instruction& insn)
  1042. {
  1043. if (insn.has_address_size_override_prefix()) {
  1044. do_once_or_repeat<false>(insn, [&] {
  1045. write_memory16({ es(), di() }, ax());
  1046. set_di(di() + (df() ? -2 : 2));
  1047. });
  1048. } else {
  1049. do_once_or_repeat<false>(insn, [&] {
  1050. write_memory16({ es(), edi() }, ax());
  1051. set_edi(edi() + (df() ? -2 : 2));
  1052. });
  1053. }
  1054. }
  1055. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1056. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1057. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1058. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1059. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1060. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1061. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1062. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1063. void SoftCPU::XADD_RM16_reg16(const X86::Instruction&) { TODO(); }
  1064. void SoftCPU::XADD_RM32_reg32(const X86::Instruction&) { TODO(); }
  1065. void SoftCPU::XADD_RM8_reg8(const X86::Instruction&) { TODO(); }
  1066. void SoftCPU::XCHG_AX_reg16(const X86::Instruction&) { TODO(); }
  1067. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction&) { TODO(); }
  1068. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction&) { TODO(); }
  1069. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction&) { TODO(); }
  1070. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction&) { TODO(); }
  1071. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1072. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1073. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8, u8>, insn); } \
  1074. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16, u16>, insn); } \
  1075. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32, u32>, insn); } \
  1076. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16, u16>, insn); } \
  1077. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16, u16>, insn); } \
  1078. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32, u32>, insn); } \
  1079. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32, u32>, insn); } \
  1080. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8, u8>, insn); } \
  1081. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8, u8>, insn); }
  1082. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1083. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1084. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16, u8>, insn); } \
  1085. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32, u8>, insn); } \
  1086. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16, u16>, insn); } \
  1087. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32, u32>, insn); } \
  1088. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8, u8>, insn); }
  1089. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1090. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1091. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1092. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1093. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1094. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1095. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1096. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1097. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1098. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1099. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1100. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1101. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1102. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1103. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1104. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1105. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1106. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1107. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1108. }