SoftCPU.cpp 48 KB

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