SoftCPU.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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. #if defined(__GNUC__) && !defined(__clang__)
  32. # pragma GCC optimize("O3")
  33. #endif
  34. //#define MEMORY_DEBUG
  35. #define DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(mnemonic, op) \
  36. void SoftCPU::mnemonic##_RM8_1(const X86::Instruction& insn) { generic_RM8_1(op<u8>, insn); } \
  37. void SoftCPU::mnemonic##_RM8_CL(const X86::Instruction& insn) { generic_RM8_CL(op<u8>, insn); } \
  38. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<true>(op<u8>, insn); } \
  39. void SoftCPU::mnemonic##_RM16_1(const X86::Instruction& insn) { generic_RM16_1(op<u16>, insn); } \
  40. void SoftCPU::mnemonic##_RM16_CL(const X86::Instruction& insn) { generic_RM16_CL(op<u16>, insn); } \
  41. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<true>(op<u16>, insn); } \
  42. void SoftCPU::mnemonic##_RM32_1(const X86::Instruction& insn) { generic_RM32_1(op<u32>, insn); } \
  43. void SoftCPU::mnemonic##_RM32_CL(const X86::Instruction& insn) { generic_RM32_CL(op<u32>, insn); } \
  44. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<true>(op<u32>, insn); }
  45. namespace UserspaceEmulator {
  46. template<typename T, typename U>
  47. inline constexpr T sign_extended_to(U value)
  48. {
  49. if (!(value & X86::TypeTrivia<U>::sign_bit))
  50. return value;
  51. return (X86::TypeTrivia<T>::mask & ~X86::TypeTrivia<U>::mask) | value;
  52. }
  53. SoftCPU::SoftCPU(Emulator& emulator)
  54. : m_emulator(emulator)
  55. {
  56. memset(m_gpr, 0, sizeof(m_gpr));
  57. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  58. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  59. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  60. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  61. m_segment[(int)X86::SegmentRegister::GS] = 0x28;
  62. }
  63. void SoftCPU::dump() const
  64. {
  65. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  66. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  67. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  68. }
  69. void SoftCPU::did_receive_secret_data()
  70. {
  71. if (m_secret_data[0] == 1) {
  72. if (auto* tracer = m_emulator.malloc_tracer())
  73. tracer->target_did_malloc({}, m_secret_data[2], m_secret_data[1]);
  74. } else if (m_secret_data[0] == 2) {
  75. if (auto* tracer = m_emulator.malloc_tracer())
  76. tracer->target_did_free({}, m_secret_data[1]);
  77. } else {
  78. ASSERT_NOT_REACHED();
  79. }
  80. }
  81. void SoftCPU::update_code_cache()
  82. {
  83. auto* region = m_emulator.mmu().find_region({ cs(), eip() });
  84. ASSERT(region);
  85. m_cached_code_ptr = region->cacheable_ptr(eip() - region->base());
  86. m_cached_code_end = region->cacheable_ptr(region->size());
  87. }
  88. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  89. {
  90. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  91. auto value = m_emulator.mmu().read8(address);
  92. #ifdef MEMORY_DEBUG
  93. printf("\033[36;1mread_memory8: @%08x:%08x -> %02x\033[0m\n", address.selector(), address.offset(), value);
  94. #endif
  95. return value;
  96. }
  97. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  98. {
  99. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  100. auto value = m_emulator.mmu().read16(address);
  101. #ifdef MEMORY_DEBUG
  102. printf("\033[36;1mread_memory16: @%04x:%08x -> %04x\033[0m\n", address.selector(), address.offset(), value);
  103. #endif
  104. return value;
  105. }
  106. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  107. {
  108. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  109. auto value = m_emulator.mmu().read32(address);
  110. #ifdef MEMORY_DEBUG
  111. printf("\033[36;1mread_memory32: @%04x:%08x -> %08x\033[0m\n", address.selector(), address.offset(), value);
  112. #endif
  113. return value;
  114. }
  115. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  116. {
  117. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  118. #ifdef MEMORY_DEBUG
  119. printf("\033[35;1mwrite_memory8: @%04x:%08x <- %02x\033[0m\n", address.selector(), address.offset(), value);
  120. #endif
  121. m_emulator.mmu().write8(address, value);
  122. }
  123. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  124. {
  125. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  126. #ifdef MEMORY_DEBUG
  127. printf("\033[35;1mwrite_memory16: @%04x:%08x <- %04x\033[0m\n", address.selector(), address.offset(), value);
  128. #endif
  129. m_emulator.mmu().write16(address, value);
  130. }
  131. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  132. {
  133. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  134. #ifdef MEMORY_DEBUG
  135. printf("\033[35;1mwrite_memory32: @%04x:%08x <- %08x\033[0m\n", address.selector(), address.offset(), value);
  136. #endif
  137. m_emulator.mmu().write32(address, value);
  138. }
  139. void SoftCPU::push_string(const StringView& string)
  140. {
  141. size_t space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
  142. set_esp(esp() - space_to_allocate);
  143. m_emulator.mmu().copy_to_vm(esp(), string.characters_without_null_termination(), string.length());
  144. m_emulator.mmu().write8({ 0x20, esp() + string.length() }, '\0');
  145. }
  146. void SoftCPU::push32(u32 value)
  147. {
  148. set_esp(esp() - sizeof(value));
  149. write_memory32({ ss(), esp() }, value);
  150. }
  151. u32 SoftCPU::pop32()
  152. {
  153. auto value = read_memory32({ ss(), esp() });
  154. set_esp(esp() + sizeof(value));
  155. return value;
  156. }
  157. void SoftCPU::push16(u16 value)
  158. {
  159. set_esp(esp() - sizeof(value));
  160. write_memory16({ ss(), esp() }, value);
  161. }
  162. u16 SoftCPU::pop16()
  163. {
  164. auto value = read_memory16({ ss(), esp() });
  165. set_esp(esp() + sizeof(value));
  166. return value;
  167. }
  168. template<bool check_zf, typename Callback>
  169. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  170. {
  171. if (!insn.has_rep_prefix())
  172. return callback();
  173. while (loop_index(insn.a32())) {
  174. callback();
  175. decrement_loop_index(insn.a32());
  176. if constexpr (check_zf) {
  177. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  178. break;
  179. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  180. break;
  181. }
  182. }
  183. }
  184. template<typename T>
  185. ALWAYS_INLINE static T op_inc(SoftCPU& cpu, T data)
  186. {
  187. T result = 0;
  188. u32 new_flags = 0;
  189. if constexpr (sizeof(T) == 4) {
  190. asm volatile("incl %%eax\n"
  191. : "=a"(result)
  192. : "a"(data));
  193. } else if constexpr (sizeof(T) == 2) {
  194. asm volatile("incw %%ax\n"
  195. : "=a"(result)
  196. : "a"(data));
  197. } else if constexpr (sizeof(T) == 1) {
  198. asm volatile("incb %%al\n"
  199. : "=a"(result)
  200. : "a"(data));
  201. }
  202. asm volatile(
  203. "pushf\n"
  204. "pop %%ebx"
  205. : "=b"(new_flags));
  206. cpu.set_flags_oszap(new_flags);
  207. return result;
  208. }
  209. template<typename T>
  210. ALWAYS_INLINE static T op_dec(SoftCPU& cpu, T data)
  211. {
  212. T result = 0;
  213. u32 new_flags = 0;
  214. if constexpr (sizeof(T) == 4) {
  215. asm volatile("decl %%eax\n"
  216. : "=a"(result)
  217. : "a"(data));
  218. } else if constexpr (sizeof(T) == 2) {
  219. asm volatile("decw %%ax\n"
  220. : "=a"(result)
  221. : "a"(data));
  222. } else if constexpr (sizeof(T) == 1) {
  223. asm volatile("decb %%al\n"
  224. : "=a"(result)
  225. : "a"(data));
  226. }
  227. asm volatile(
  228. "pushf\n"
  229. "pop %%ebx"
  230. : "=b"(new_flags));
  231. cpu.set_flags_oszap(new_flags);
  232. return result;
  233. }
  234. template<typename T>
  235. ALWAYS_INLINE static T op_xor(SoftCPU& cpu, const T& dest, const T& src)
  236. {
  237. T result = 0;
  238. u32 new_flags = 0;
  239. if constexpr (sizeof(T) == 4) {
  240. asm volatile("xorl %%ecx, %%eax\n"
  241. : "=a"(result)
  242. : "a"(dest), "c"((u32)src));
  243. } else if constexpr (sizeof(T) == 2) {
  244. asm volatile("xor %%cx, %%ax\n"
  245. : "=a"(result)
  246. : "a"(dest), "c"((u16)src));
  247. } else if constexpr (sizeof(T) == 1) {
  248. asm volatile("xorb %%cl, %%al\n"
  249. : "=a"(result)
  250. : "a"(dest), "c"((u8)src));
  251. } else {
  252. ASSERT_NOT_REACHED();
  253. }
  254. asm volatile(
  255. "pushf\n"
  256. "pop %%ebx"
  257. : "=b"(new_flags));
  258. cpu.set_flags_oszpc(new_flags);
  259. return result;
  260. }
  261. template<typename T>
  262. ALWAYS_INLINE static T op_or(SoftCPU& cpu, const T& dest, const T& src)
  263. {
  264. T result = 0;
  265. u32 new_flags = 0;
  266. if constexpr (sizeof(T) == 4) {
  267. asm volatile("orl %%ecx, %%eax\n"
  268. : "=a"(result)
  269. : "a"(dest), "c"((u32)src));
  270. } else if constexpr (sizeof(T) == 2) {
  271. asm volatile("or %%cx, %%ax\n"
  272. : "=a"(result)
  273. : "a"(dest), "c"((u16)src));
  274. } else if constexpr (sizeof(T) == 1) {
  275. asm volatile("orb %%cl, %%al\n"
  276. : "=a"(result)
  277. : "a"(dest), "c"((u8)src));
  278. } else {
  279. ASSERT_NOT_REACHED();
  280. }
  281. asm volatile(
  282. "pushf\n"
  283. "pop %%ebx"
  284. : "=b"(new_flags));
  285. cpu.set_flags_oszpc(new_flags);
  286. return result;
  287. }
  288. template<typename T>
  289. ALWAYS_INLINE static T op_sub(SoftCPU& cpu, const T& dest, const T& src)
  290. {
  291. T result = 0;
  292. u32 new_flags = 0;
  293. if constexpr (sizeof(T) == 4) {
  294. asm volatile("subl %%ecx, %%eax\n"
  295. : "=a"(result)
  296. : "a"(dest), "c"((u32)src));
  297. } else if constexpr (sizeof(T) == 2) {
  298. asm volatile("subw %%cx, %%ax\n"
  299. : "=a"(result)
  300. : "a"(dest), "c"((u16)src));
  301. } else if constexpr (sizeof(T) == 1) {
  302. asm volatile("subb %%cl, %%al\n"
  303. : "=a"(result)
  304. : "a"(dest), "c"((u8)src));
  305. } else {
  306. ASSERT_NOT_REACHED();
  307. }
  308. asm volatile(
  309. "pushf\n"
  310. "pop %%ebx"
  311. : "=b"(new_flags));
  312. cpu.set_flags_oszapc(new_flags);
  313. return result;
  314. }
  315. template<typename T, bool cf>
  316. ALWAYS_INLINE static T op_sbb_impl(SoftCPU& cpu, const T& dest, const T& src)
  317. {
  318. T result = 0;
  319. u32 new_flags = 0;
  320. if constexpr (cf)
  321. asm volatile("stc");
  322. else
  323. asm volatile("clc");
  324. if constexpr (sizeof(T) == 4) {
  325. asm volatile("sbbl %%ecx, %%eax\n"
  326. : "=a"(result)
  327. : "a"(dest), "c"((u32)src));
  328. } else if constexpr (sizeof(T) == 2) {
  329. asm volatile("sbbw %%cx, %%ax\n"
  330. : "=a"(result)
  331. : "a"(dest), "c"((u16)src));
  332. } else if constexpr (sizeof(T) == 1) {
  333. asm volatile("sbbb %%cl, %%al\n"
  334. : "=a"(result)
  335. : "a"(dest), "c"((u8)src));
  336. } else {
  337. ASSERT_NOT_REACHED();
  338. }
  339. asm volatile(
  340. "pushf\n"
  341. "pop %%ebx"
  342. : "=b"(new_flags));
  343. cpu.set_flags_oszapc(new_flags);
  344. return result;
  345. }
  346. template<typename T>
  347. ALWAYS_INLINE static T op_sbb(SoftCPU& cpu, T& dest, const T& src)
  348. {
  349. if (cpu.cf())
  350. return op_sbb_impl<T, true>(cpu, dest, src);
  351. return op_sbb_impl<T, false>(cpu, dest, src);
  352. }
  353. template<typename T>
  354. ALWAYS_INLINE static T op_add(SoftCPU& cpu, T& dest, const T& src)
  355. {
  356. T result = 0;
  357. u32 new_flags = 0;
  358. if constexpr (sizeof(T) == 4) {
  359. asm volatile("addl %%ecx, %%eax\n"
  360. : "=a"(result)
  361. : "a"(dest), "c"((u32)src));
  362. } else if constexpr (sizeof(T) == 2) {
  363. asm volatile("addw %%cx, %%ax\n"
  364. : "=a"(result)
  365. : "a"(dest), "c"((u16)src));
  366. } else if constexpr (sizeof(T) == 1) {
  367. asm volatile("addb %%cl, %%al\n"
  368. : "=a"(result)
  369. : "a"(dest), "c"((u8)src));
  370. } else {
  371. ASSERT_NOT_REACHED();
  372. }
  373. asm volatile(
  374. "pushf\n"
  375. "pop %%ebx"
  376. : "=b"(new_flags));
  377. cpu.set_flags_oszapc(new_flags);
  378. return result;
  379. }
  380. template<typename T, bool cf>
  381. ALWAYS_INLINE static T op_adc_impl(SoftCPU& cpu, T& dest, const T& src)
  382. {
  383. T result = 0;
  384. u32 new_flags = 0;
  385. if constexpr (cf)
  386. asm volatile("stc");
  387. else
  388. asm volatile("clc");
  389. if constexpr (sizeof(T) == 4) {
  390. asm volatile("adcl %%ecx, %%eax\n"
  391. : "=a"(result)
  392. : "a"(dest), "c"((u32)src));
  393. } else if constexpr (sizeof(T) == 2) {
  394. asm volatile("adcw %%cx, %%ax\n"
  395. : "=a"(result)
  396. : "a"(dest), "c"((u16)src));
  397. } else if constexpr (sizeof(T) == 1) {
  398. asm volatile("adcb %%cl, %%al\n"
  399. : "=a"(result)
  400. : "a"(dest), "c"((u8)src));
  401. } else {
  402. ASSERT_NOT_REACHED();
  403. }
  404. asm volatile(
  405. "pushf\n"
  406. "pop %%ebx"
  407. : "=b"(new_flags));
  408. cpu.set_flags_oszapc(new_flags);
  409. return result;
  410. }
  411. template<typename T>
  412. ALWAYS_INLINE static T op_adc(SoftCPU& cpu, T& dest, const T& src)
  413. {
  414. if (cpu.cf())
  415. return op_adc_impl<T, true>(cpu, dest, src);
  416. return op_adc_impl<T, false>(cpu, dest, src);
  417. }
  418. template<typename T>
  419. ALWAYS_INLINE static T op_and(SoftCPU& cpu, const T& dest, const T& src)
  420. {
  421. T result = 0;
  422. u32 new_flags = 0;
  423. if constexpr (sizeof(T) == 4) {
  424. asm volatile("andl %%ecx, %%eax\n"
  425. : "=a"(result)
  426. : "a"(dest), "c"((u32)src));
  427. } else if constexpr (sizeof(T) == 2) {
  428. asm volatile("andw %%cx, %%ax\n"
  429. : "=a"(result)
  430. : "a"(dest), "c"((u16)src));
  431. } else if constexpr (sizeof(T) == 1) {
  432. asm volatile("andb %%cl, %%al\n"
  433. : "=a"(result)
  434. : "a"(dest), "c"((u8)src));
  435. } else {
  436. ASSERT_NOT_REACHED();
  437. }
  438. asm volatile(
  439. "pushf\n"
  440. "pop %%ebx"
  441. : "=b"(new_flags));
  442. cpu.set_flags_oszpc(new_flags);
  443. return result;
  444. }
  445. template<typename T>
  446. ALWAYS_INLINE static T op_imul(SoftCPU& cpu, const T& dest, const T& src)
  447. {
  448. u32 result_high = 0;
  449. u32 result_low = 0;
  450. T result = 0;
  451. u32 new_flags = 0;
  452. if constexpr (sizeof(T) == 8) {
  453. asm volatile("imull %%ecx"
  454. : "=a"(result_low), "=d"(result_high)
  455. : "a"((i32)dest), "c"((i32)src));
  456. } else if constexpr (sizeof(T) == 4) {
  457. asm volatile("imull %%ecx, %%eax\n"
  458. : "=a"(result)
  459. : "a"(dest), "c"((i32)src));
  460. } else if constexpr (sizeof(T) == 2) {
  461. asm volatile("imulw %%cx, %%ax\n"
  462. : "=a"(result)
  463. : "a"(dest), "c"((i16)src));
  464. } else {
  465. ASSERT_NOT_REACHED();
  466. }
  467. asm volatile(
  468. "pushf\n"
  469. "pop %%ebx"
  470. : "=b"(new_flags));
  471. if constexpr (sizeof(T) == 8)
  472. result = ((u64)result_high << 32) | result_low;
  473. cpu.set_flags_oc(new_flags);
  474. return result;
  475. }
  476. template<typename T>
  477. ALWAYS_INLINE static T op_shr(SoftCPU& cpu, T data, u8 steps)
  478. {
  479. if (steps == 0)
  480. return data;
  481. u32 result = 0;
  482. u32 new_flags = 0;
  483. if constexpr (sizeof(T) == 4) {
  484. asm volatile("shrl %%cl, %%eax\n"
  485. : "=a"(result)
  486. : "a"(data), "c"(steps));
  487. } else if constexpr (sizeof(T) == 2) {
  488. asm volatile("shrw %%cl, %%ax\n"
  489. : "=a"(result)
  490. : "a"(data), "c"(steps));
  491. } else if constexpr (sizeof(T) == 1) {
  492. asm volatile("shrb %%cl, %%al\n"
  493. : "=a"(result)
  494. : "a"(data), "c"(steps));
  495. }
  496. asm volatile(
  497. "pushf\n"
  498. "pop %%ebx"
  499. : "=b"(new_flags));
  500. cpu.set_flags_oszapc(new_flags);
  501. return result;
  502. }
  503. template<typename T>
  504. ALWAYS_INLINE static T op_shl(SoftCPU& cpu, T data, u8 steps)
  505. {
  506. if (steps == 0)
  507. return data;
  508. u32 result = 0;
  509. u32 new_flags = 0;
  510. if constexpr (sizeof(T) == 4) {
  511. asm volatile("shll %%cl, %%eax\n"
  512. : "=a"(result)
  513. : "a"(data), "c"(steps));
  514. } else if constexpr (sizeof(T) == 2) {
  515. asm volatile("shlw %%cl, %%ax\n"
  516. : "=a"(result)
  517. : "a"(data), "c"(steps));
  518. } else if constexpr (sizeof(T) == 1) {
  519. asm volatile("shlb %%cl, %%al\n"
  520. : "=a"(result)
  521. : "a"(data), "c"(steps));
  522. }
  523. asm volatile(
  524. "pushf\n"
  525. "pop %%ebx"
  526. : "=b"(new_flags));
  527. cpu.set_flags_oszapc(new_flags);
  528. return result;
  529. }
  530. template<typename T>
  531. ALWAYS_INLINE static T op_shrd(SoftCPU& cpu, T data, T extra_bits, u8 steps)
  532. {
  533. if (steps == 0)
  534. return data;
  535. u32 result = 0;
  536. u32 new_flags = 0;
  537. if constexpr (sizeof(T) == 4) {
  538. asm volatile("shrd %%cl, %%edx, %%eax\n"
  539. : "=a"(result)
  540. : "a"(data), "d"(extra_bits), "c"(steps));
  541. } else if constexpr (sizeof(T) == 2) {
  542. asm volatile("shrd %%cl, %%dx, %%ax\n"
  543. : "=a"(result)
  544. : "a"(data), "d"(extra_bits), "c"(steps));
  545. }
  546. asm volatile(
  547. "pushf\n"
  548. "pop %%ebx"
  549. : "=b"(new_flags));
  550. cpu.set_flags_oszapc(new_flags);
  551. return result;
  552. }
  553. template<typename T>
  554. ALWAYS_INLINE static T op_shld(SoftCPU& cpu, T data, T extra_bits, u8 steps)
  555. {
  556. if (steps == 0)
  557. return data;
  558. u32 result = 0;
  559. u32 new_flags = 0;
  560. if constexpr (sizeof(T) == 4) {
  561. asm volatile("shld %%cl, %%edx, %%eax\n"
  562. : "=a"(result)
  563. : "a"(data), "d"(extra_bits), "c"(steps));
  564. } else if constexpr (sizeof(T) == 2) {
  565. asm volatile("shld %%cl, %%dx, %%ax\n"
  566. : "=a"(result)
  567. : "a"(data), "d"(extra_bits), "c"(steps));
  568. }
  569. asm volatile(
  570. "pushf\n"
  571. "pop %%ebx"
  572. : "=b"(new_flags));
  573. cpu.set_flags_oszapc(new_flags);
  574. return result;
  575. }
  576. template<bool update_dest, typename Op>
  577. ALWAYS_INLINE void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  578. {
  579. auto dest = al();
  580. auto src = insn.imm8();
  581. auto result = op(*this, dest, src);
  582. if (update_dest)
  583. set_al(result);
  584. }
  585. template<bool update_dest, typename Op>
  586. ALWAYS_INLINE void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  587. {
  588. auto dest = ax();
  589. auto src = insn.imm16();
  590. auto result = op(*this, dest, src);
  591. if (update_dest)
  592. set_ax(result);
  593. }
  594. template<bool update_dest, typename Op>
  595. ALWAYS_INLINE void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  596. {
  597. auto dest = eax();
  598. auto src = insn.imm32();
  599. auto result = op(*this, dest, src);
  600. if (update_dest)
  601. set_eax(result);
  602. }
  603. template<bool update_dest, typename Op>
  604. ALWAYS_INLINE void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  605. {
  606. auto dest = insn.modrm().read16(*this, insn);
  607. auto src = insn.imm16();
  608. auto result = op(*this, dest, src);
  609. if (update_dest)
  610. insn.modrm().write16(*this, insn, result);
  611. }
  612. template<bool update_dest, typename Op>
  613. ALWAYS_INLINE void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  614. {
  615. auto dest = insn.modrm().read16(*this, insn);
  616. auto src = sign_extended_to<u16>(insn.imm8());
  617. auto result = op(*this, dest, src);
  618. if (update_dest)
  619. insn.modrm().write16(*this, insn, result);
  620. }
  621. template<bool update_dest, typename Op>
  622. ALWAYS_INLINE void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  623. {
  624. auto dest = insn.modrm().read16(*this, insn);
  625. auto src = gpr16(insn.reg16());
  626. auto result = op(*this, dest, src);
  627. if (update_dest)
  628. insn.modrm().write16(*this, insn, result);
  629. }
  630. template<bool update_dest, typename Op>
  631. ALWAYS_INLINE void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  632. {
  633. auto dest = insn.modrm().read32(*this, insn);
  634. auto src = insn.imm32();
  635. auto result = op(*this, dest, src);
  636. if (update_dest)
  637. insn.modrm().write32(*this, insn, result);
  638. }
  639. template<bool update_dest, typename Op>
  640. ALWAYS_INLINE void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  641. {
  642. auto dest = insn.modrm().read32(*this, insn);
  643. auto src = sign_extended_to<u32>(insn.imm8());
  644. auto result = op(*this, dest, src);
  645. if (update_dest)
  646. insn.modrm().write32(*this, insn, result);
  647. }
  648. template<bool update_dest, typename Op>
  649. ALWAYS_INLINE void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  650. {
  651. auto dest = insn.modrm().read32(*this, insn);
  652. auto src = gpr32(insn.reg32());
  653. auto result = op(*this, dest, src);
  654. if (update_dest)
  655. insn.modrm().write32(*this, insn, result);
  656. }
  657. template<bool update_dest, typename Op>
  658. ALWAYS_INLINE void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  659. {
  660. auto dest = insn.modrm().read8(*this, insn);
  661. auto src = insn.imm8();
  662. auto result = op(*this, dest, src);
  663. if (update_dest)
  664. insn.modrm().write8(*this, insn, result);
  665. }
  666. template<bool update_dest, typename Op>
  667. ALWAYS_INLINE void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  668. {
  669. auto dest = insn.modrm().read8(*this, insn);
  670. auto src = gpr8(insn.reg8());
  671. auto result = op(*this, dest, src);
  672. if (update_dest)
  673. insn.modrm().write8(*this, insn, result);
  674. }
  675. template<bool update_dest, typename Op>
  676. ALWAYS_INLINE void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  677. {
  678. auto dest = gpr16(insn.reg16());
  679. auto src = insn.modrm().read16(*this, insn);
  680. auto result = op(*this, dest, src);
  681. if (update_dest)
  682. gpr16(insn.reg16()) = result;
  683. }
  684. template<bool update_dest, typename Op>
  685. ALWAYS_INLINE void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  686. {
  687. auto dest = gpr32(insn.reg32());
  688. auto src = insn.modrm().read32(*this, insn);
  689. auto result = op(*this, dest, src);
  690. if (update_dest)
  691. gpr32(insn.reg32()) = result;
  692. }
  693. template<bool update_dest, typename Op>
  694. ALWAYS_INLINE void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  695. {
  696. auto dest = gpr8(insn.reg8());
  697. auto src = insn.modrm().read8(*this, insn);
  698. auto result = op(*this, dest, src);
  699. if (update_dest)
  700. gpr8(insn.reg8()) = result;
  701. }
  702. template<typename Op>
  703. ALWAYS_INLINE void SoftCPU::generic_RM8_1(Op op, const X86::Instruction& insn)
  704. {
  705. auto data = insn.modrm().read8(*this, insn);
  706. insn.modrm().write8(*this, insn, op(*this, data, 1));
  707. }
  708. template<typename Op>
  709. ALWAYS_INLINE void SoftCPU::generic_RM8_CL(Op op, const X86::Instruction& insn)
  710. {
  711. auto data = insn.modrm().read8(*this, insn);
  712. insn.modrm().write8(*this, insn, op(*this, data, cl()));
  713. }
  714. template<typename Op>
  715. ALWAYS_INLINE void SoftCPU::generic_RM16_1(Op op, const X86::Instruction& insn)
  716. {
  717. auto data = insn.modrm().read16(*this, insn);
  718. insn.modrm().write16(*this, insn, op(*this, data, 1));
  719. }
  720. template<typename Op>
  721. ALWAYS_INLINE void SoftCPU::generic_RM16_CL(Op op, const X86::Instruction& insn)
  722. {
  723. auto data = insn.modrm().read16(*this, insn);
  724. insn.modrm().write16(*this, insn, op(*this, data, cl()));
  725. }
  726. template<typename Op>
  727. ALWAYS_INLINE void SoftCPU::generic_RM32_1(Op op, const X86::Instruction& insn)
  728. {
  729. auto data = insn.modrm().read32(*this, insn);
  730. insn.modrm().write32(*this, insn, op(*this, data, 1));
  731. }
  732. template<typename Op>
  733. ALWAYS_INLINE void SoftCPU::generic_RM32_CL(Op op, const X86::Instruction& insn)
  734. {
  735. auto data = insn.modrm().read32(*this, insn);
  736. insn.modrm().write32(*this, insn, op(*this, data, cl()));
  737. }
  738. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  739. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  740. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  741. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  742. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  743. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  744. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { }
  745. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { }
  746. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { }
  747. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { }
  748. void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
  749. {
  750. gpr32(insn.reg32()) = __builtin_bswap32(gpr32(insn.reg32()));
  751. }
  752. template<typename T>
  753. ALWAYS_INLINE static T op_bt(T value, T)
  754. {
  755. return value;
  756. }
  757. template<typename T>
  758. ALWAYS_INLINE static T op_bts(T value, T bit_mask)
  759. {
  760. return value | bit_mask;
  761. }
  762. template<typename T>
  763. ALWAYS_INLINE static T op_btr(T value, T bit_mask)
  764. {
  765. return value & ~bit_mask;
  766. }
  767. template<typename T>
  768. ALWAYS_INLINE static T op_btc(T value, T bit_mask)
  769. {
  770. return value ^ bit_mask;
  771. }
  772. template<bool should_update, typename Op>
  773. ALWAYS_INLINE void BTx_RM16_reg16(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  774. {
  775. if (insn.modrm().is_register()) {
  776. unsigned bit_index = cpu.gpr16(insn.reg16()) & (X86::TypeTrivia<u16>::bits - 1);
  777. u16 original = insn.modrm().read16(cpu, insn);
  778. u16 bit_mask = 1 << bit_index;
  779. u16 result = op(original, bit_mask);
  780. cpu.set_cf((original & bit_mask) != 0);
  781. if (should_update)
  782. insn.modrm().write16(cpu, insn, result);
  783. return;
  784. }
  785. // FIXME: Is this supposed to perform a full 16-bit read/modify/write?
  786. unsigned bit_offset_in_array = cpu.gpr16(insn.reg16()) / 8;
  787. unsigned bit_offset_in_byte = cpu.gpr16(insn.reg16()) & 7;
  788. auto address = insn.modrm().resolve(cpu, insn);
  789. address.set_offset(address.offset() + bit_offset_in_array);
  790. u8 dest = cpu.read_memory8(address);
  791. u8 bit_mask = 1 << bit_offset_in_byte;
  792. u8 result = op(dest, bit_mask);
  793. cpu.set_cf((dest & bit_mask) != 0);
  794. if (should_update)
  795. cpu.write_memory8(address, result);
  796. }
  797. template<bool should_update, typename Op>
  798. ALWAYS_INLINE void BTx_RM32_reg32(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  799. {
  800. if (insn.modrm().is_register()) {
  801. unsigned bit_index = cpu.gpr32(insn.reg32()) & (X86::TypeTrivia<u32>::bits - 1);
  802. u32 original = insn.modrm().read32(cpu, insn);
  803. u32 bit_mask = 1 << bit_index;
  804. u32 result = op(original, bit_mask);
  805. cpu.set_cf((original & bit_mask) != 0);
  806. if (should_update)
  807. insn.modrm().write32(cpu, insn, result);
  808. return;
  809. }
  810. // FIXME: Is this supposed to perform a full 32-bit read/modify/write?
  811. unsigned bit_offset_in_array = cpu.gpr32(insn.reg32()) / 8;
  812. unsigned bit_offset_in_byte = cpu.gpr32(insn.reg32()) & 7;
  813. auto address = insn.modrm().resolve(cpu, insn);
  814. address.set_offset(address.offset() + bit_offset_in_array);
  815. u8 dest = cpu.read_memory8(address);
  816. u8 bit_mask = 1 << bit_offset_in_byte;
  817. u8 result = op(dest, bit_mask);
  818. cpu.set_cf((dest & bit_mask) != 0);
  819. if (should_update)
  820. cpu.write_memory8(address, result);
  821. }
  822. template<bool should_update, typename Op>
  823. ALWAYS_INLINE void BTx_RM16_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  824. {
  825. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u16>::mask);
  826. // FIXME: Support higher bit indices
  827. ASSERT(bit_index < 16);
  828. u16 original = insn.modrm().read16(cpu, insn);
  829. u16 bit_mask = 1 << bit_index;
  830. u16 result = op(original, bit_mask);
  831. cpu.set_cf((original & bit_mask) != 0);
  832. if (should_update)
  833. insn.modrm().write16(cpu, insn, result);
  834. }
  835. template<bool should_update, typename Op>
  836. ALWAYS_INLINE void BTx_RM32_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  837. {
  838. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u32>::mask);
  839. // FIXME: Support higher bit indices
  840. ASSERT(bit_index < 32);
  841. u32 original = insn.modrm().read32(cpu, insn);
  842. u32 bit_mask = 1 << bit_index;
  843. u32 result = op(original, bit_mask);
  844. cpu.set_cf((original & bit_mask) != 0);
  845. if (should_update)
  846. insn.modrm().write32(cpu, insn, result);
  847. }
  848. #define DEFINE_GENERIC_BTx_INSN_HANDLERS(mnemonic, op, update_dest) \
  849. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { BTx_RM32_reg32<update_dest>(*this, insn, op<u32>); } \
  850. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { BTx_RM16_reg16<update_dest>(*this, insn, op<u16>); } \
  851. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { BTx_RM32_imm8<update_dest>(*this, insn, op<u32>); } \
  852. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { BTx_RM16_imm8<update_dest>(*this, insn, op<u16>); }
  853. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTS, op_bts, true);
  854. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTR, op_btr, true);
  855. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTC, op_btc, true);
  856. DEFINE_GENERIC_BTx_INSN_HANDLERS(BT, op_bt, false);
  857. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&)
  858. {
  859. TODO();
  860. }
  861. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  862. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  863. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  864. {
  865. push32(eip());
  866. set_eip(insn.modrm().read32(*this, insn));
  867. }
  868. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  869. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  870. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  871. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  872. {
  873. push32(eip());
  874. set_eip(eip() + (i32)insn.imm32());
  875. }
  876. void SoftCPU::CBW(const X86::Instruction&)
  877. {
  878. set_ah((al() & 0x80) ? 0xff : 0x00);
  879. }
  880. void SoftCPU::CDQ(const X86::Instruction&)
  881. {
  882. if (eax() & 0x80000000)
  883. set_edx(0xffffffff);
  884. else
  885. set_edx(0x00000000);
  886. }
  887. void SoftCPU::CLC(const X86::Instruction&)
  888. {
  889. set_cf(false);
  890. }
  891. void SoftCPU::CLD(const X86::Instruction&)
  892. {
  893. set_df(false);
  894. }
  895. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  896. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  897. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  898. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  899. {
  900. if (evaluate_condition(insn.cc()))
  901. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  902. }
  903. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  904. {
  905. if (evaluate_condition(insn.cc()))
  906. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  907. }
  908. template<typename T>
  909. ALWAYS_INLINE static void do_cmps(SoftCPU& cpu, const X86::Instruction& insn)
  910. {
  911. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  912. cpu.do_once_or_repeat<true>(insn, [&] {
  913. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.a32()) });
  914. auto dest = cpu.read_memory<T>({ cpu.es(), cpu.destination_index(insn.a32()) });
  915. op_sub(cpu, dest, src);
  916. cpu.step_source_index(insn.a32(), sizeof(T));
  917. cpu.step_destination_index(insn.a32(), sizeof(T));
  918. });
  919. }
  920. void SoftCPU::CMPSB(const X86::Instruction& insn)
  921. {
  922. do_cmps<u8>(*this, insn);
  923. }
  924. void SoftCPU::CMPSD(const X86::Instruction& insn)
  925. {
  926. do_cmps<u32>(*this, insn);
  927. }
  928. void SoftCPU::CMPSW(const X86::Instruction& insn)
  929. {
  930. do_cmps<u16>(*this, insn);
  931. }
  932. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  933. {
  934. auto current = insn.modrm().read16(*this, insn);
  935. if (current == ax()) {
  936. set_zf(true);
  937. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  938. } else {
  939. set_zf(false);
  940. set_eax(current);
  941. }
  942. }
  943. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  944. {
  945. auto current = insn.modrm().read32(*this, insn);
  946. if (current == eax()) {
  947. set_zf(true);
  948. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  949. } else {
  950. set_zf(false);
  951. set_eax(current);
  952. }
  953. }
  954. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  955. {
  956. auto current = insn.modrm().read8(*this, insn);
  957. if (current == al()) {
  958. set_zf(true);
  959. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  960. } else {
  961. set_zf(false);
  962. set_eax(current);
  963. }
  964. }
  965. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  966. void SoftCPU::CWD(const X86::Instruction&)
  967. {
  968. set_dx((ax() & 0x8000) ? 0xffff : 0x0000);
  969. }
  970. void SoftCPU::CWDE(const X86::Instruction&)
  971. {
  972. set_eax(sign_extended_to<u32>(ax()));
  973. }
  974. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  975. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  976. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  977. {
  978. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16(*this, insn)));
  979. }
  980. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  981. {
  982. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32(*this, insn)));
  983. }
  984. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  985. {
  986. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8(*this, insn)));
  987. }
  988. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  989. {
  990. gpr16(insn.reg16()) = op_dec(*this, gpr16(insn.reg16()));
  991. }
  992. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  993. {
  994. gpr32(insn.reg32()) = op_dec(*this, gpr32(insn.reg32()));
  995. }
  996. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  997. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  998. {
  999. auto divisor = insn.modrm().read32(*this, insn);
  1000. if (divisor == 0) {
  1001. warn() << "Divide by zero";
  1002. TODO();
  1003. }
  1004. u64 dividend = ((u64)edx() << 32) | eax();
  1005. auto result = dividend / divisor;
  1006. if (result > NumericLimits<u32>::max()) {
  1007. warn() << "Divide overflow";
  1008. TODO();
  1009. }
  1010. set_eax(result);
  1011. set_edx(dividend % divisor);
  1012. }
  1013. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  1014. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  1015. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  1016. void SoftCPU::ESCAPE(const X86::Instruction&)
  1017. {
  1018. dbg() << "FIXME: x87 floating-point support";
  1019. m_emulator.dump_backtrace();
  1020. TODO();
  1021. }
  1022. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  1023. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  1024. void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
  1025. {
  1026. auto divisor = insn.modrm().read32(*this, insn);
  1027. if (divisor == 0) {
  1028. warn() << "Divide by zero";
  1029. TODO();
  1030. }
  1031. i64 dividend = ((i64)edx() << 32) | eax();
  1032. auto result = dividend / divisor;
  1033. if (result > NumericLimits<i32>::max()) {
  1034. warn() << "Divide overflow";
  1035. TODO();
  1036. }
  1037. set_eax(result);
  1038. set_edx(dividend % divisor);
  1039. }
  1040. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  1041. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  1042. void SoftCPU::IMUL_RM32(const X86::Instruction& insn)
  1043. {
  1044. i64 value = op_imul<i64>(*this, insn.modrm().read32(*this, insn), eax());
  1045. set_edx(value >> 32);
  1046. set_eax(value & 0xffffffff);
  1047. }
  1048. void SoftCPU::IMUL_RM8(const X86::Instruction& insn)
  1049. {
  1050. set_ax(op_imul<i16>(*this, insn.modrm().read8(*this, insn), al()));
  1051. }
  1052. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  1053. {
  1054. gpr16(insn.reg16()) = op_imul<i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  1055. }
  1056. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  1057. {
  1058. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  1059. }
  1060. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  1061. {
  1062. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), sign_extended_to<i16>(insn.imm8()));
  1063. }
  1064. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  1065. {
  1066. gpr32(insn.reg32()) = op_imul<i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  1067. }
  1068. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  1069. {
  1070. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  1071. }
  1072. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  1073. {
  1074. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), sign_extended_to<i32>(insn.imm8()));
  1075. }
  1076. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  1077. {
  1078. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  1079. }
  1080. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  1081. {
  1082. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  1083. }
  1084. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  1085. {
  1086. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  1087. }
  1088. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  1089. {
  1090. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  1091. }
  1092. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  1093. {
  1094. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  1095. }
  1096. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  1097. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  1098. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  1099. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  1100. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  1101. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  1102. {
  1103. ASSERT(insn.imm8() == 0x82);
  1104. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  1105. }
  1106. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  1107. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  1108. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  1109. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  1110. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  1111. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  1112. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  1113. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  1114. void SoftCPU::JCXZ_imm8(const X86::Instruction& insn)
  1115. {
  1116. if ((insn.a32() && ecx() == 0) || (!insn.a32() && cx() == 0))
  1117. set_eip(eip() + (i8)insn.imm8());
  1118. }
  1119. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  1120. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  1121. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  1122. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  1123. {
  1124. set_eip(insn.modrm().read32(*this, insn));
  1125. }
  1126. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  1127. {
  1128. set_eip(eip() + (i16)insn.imm16());
  1129. }
  1130. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  1131. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  1132. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  1133. {
  1134. set_eip(eip() + (i32)insn.imm32());
  1135. }
  1136. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  1137. {
  1138. set_eip(eip() + (i8)insn.imm8());
  1139. }
  1140. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  1141. {
  1142. if (evaluate_condition(insn.cc()))
  1143. set_eip(eip() + (i32)insn.imm32());
  1144. }
  1145. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  1146. {
  1147. if (evaluate_condition(insn.cc()))
  1148. set_eip(eip() + (i8)insn.imm8());
  1149. }
  1150. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  1151. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  1152. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  1153. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1154. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1155. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  1156. void SoftCPU::LEAVE32(const X86::Instruction&)
  1157. {
  1158. u32 new_ebp = read_memory32({ ss(), ebp() });
  1159. set_esp(ebp() + 4);
  1160. set_ebp(new_ebp);
  1161. }
  1162. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  1163. {
  1164. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn).offset();
  1165. }
  1166. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  1167. {
  1168. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn).offset();
  1169. }
  1170. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  1171. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  1172. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1173. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1174. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  1175. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1176. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1177. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  1178. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  1179. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  1180. template<typename T>
  1181. ALWAYS_INLINE static void do_lods(SoftCPU& cpu, const X86::Instruction& insn)
  1182. {
  1183. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1184. cpu.do_once_or_repeat<true>(insn, [&] {
  1185. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.a32()) });
  1186. cpu.gpr<T>(X86::RegisterAL) = src;
  1187. cpu.step_source_index(insn.a32(), sizeof(T));
  1188. });
  1189. }
  1190. void SoftCPU::LODSB(const X86::Instruction& insn)
  1191. {
  1192. do_lods<u8>(*this, insn);
  1193. }
  1194. void SoftCPU::LODSD(const X86::Instruction& insn)
  1195. {
  1196. do_lods<u32>(*this, insn);
  1197. }
  1198. void SoftCPU::LODSW(const X86::Instruction& insn)
  1199. {
  1200. do_lods<u16>(*this, insn);
  1201. }
  1202. void SoftCPU::LOOPNZ_imm8(const X86::Instruction& insn)
  1203. {
  1204. if (insn.a32()) {
  1205. set_ecx(ecx() - 1);
  1206. if (ecx() != 0 && !zf())
  1207. set_eip(eip() + (i8)insn.imm8());
  1208. } else {
  1209. set_cx(cx() - 1);
  1210. if (cx() != 0 && !zf())
  1211. set_eip(eip() + (i8)insn.imm8());
  1212. }
  1213. }
  1214. void SoftCPU::LOOPZ_imm8(const X86::Instruction& insn)
  1215. {
  1216. if (insn.a32()) {
  1217. set_ecx(ecx() - 1);
  1218. if (ecx() != 0 && zf())
  1219. set_eip(eip() + (i8)insn.imm8());
  1220. } else {
  1221. set_cx(cx() - 1);
  1222. if (cx() != 0 && zf())
  1223. set_eip(eip() + (i8)insn.imm8());
  1224. }
  1225. }
  1226. void SoftCPU::LOOP_imm8(const X86::Instruction& insn)
  1227. {
  1228. if (insn.a32()) {
  1229. set_ecx(ecx() - 1);
  1230. if (ecx() != 0)
  1231. set_eip(eip() + (i8)insn.imm8());
  1232. } else {
  1233. set_cx(cx() - 1);
  1234. if (cx() != 0)
  1235. set_eip(eip() + (i8)insn.imm8());
  1236. }
  1237. }
  1238. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  1239. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  1240. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1241. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1242. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  1243. void SoftCPU::MOVSB(const X86::Instruction& insn)
  1244. {
  1245. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1246. if (insn.has_address_size_override_prefix()) {
  1247. do_once_or_repeat<false>(insn, [&] {
  1248. auto src = read_memory8({ src_segment, si() });
  1249. write_memory8({ es(), di() }, src);
  1250. set_di(di() + (df() ? -1 : 1));
  1251. set_si(si() + (df() ? -1 : 1));
  1252. });
  1253. } else {
  1254. do_once_or_repeat<false>(insn, [&] {
  1255. auto src = read_memory8({ src_segment, esi() });
  1256. write_memory8({ es(), edi() }, src);
  1257. set_edi(edi() + (df() ? -1 : 1));
  1258. set_esi(esi() + (df() ? -1 : 1));
  1259. });
  1260. }
  1261. }
  1262. void SoftCPU::MOVSD(const X86::Instruction& insn)
  1263. {
  1264. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1265. if (insn.has_address_size_override_prefix()) {
  1266. do_once_or_repeat<false>(insn, [&] {
  1267. auto src = read_memory32({ src_segment, si() });
  1268. write_memory32({ es(), di() }, src);
  1269. set_di(di() + (df() ? -4 : 4));
  1270. set_si(si() + (df() ? -4 : 4));
  1271. });
  1272. } else {
  1273. do_once_or_repeat<false>(insn, [&] {
  1274. auto src = read_memory32({ src_segment, esi() });
  1275. write_memory32({ es(), edi() }, src);
  1276. set_edi(edi() + (df() ? -4 : 4));
  1277. set_esi(esi() + (df() ? -4 : 4));
  1278. });
  1279. }
  1280. }
  1281. void SoftCPU::MOVSW(const X86::Instruction& insn)
  1282. {
  1283. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1284. if (insn.has_address_size_override_prefix()) {
  1285. do_once_or_repeat<false>(insn, [&] {
  1286. auto src = read_memory16({ src_segment, si() });
  1287. write_memory16({ es(), di() }, src);
  1288. set_di(di() + (df() ? -2 : 2));
  1289. set_si(si() + (df() ? -2 : 2));
  1290. });
  1291. } else {
  1292. do_once_or_repeat<false>(insn, [&] {
  1293. auto src = read_memory16({ src_segment, esi() });
  1294. write_memory16({ es(), edi() }, src);
  1295. set_edi(edi() + (df() ? -2 : 2));
  1296. set_esi(esi() + (df() ? -2 : 2));
  1297. });
  1298. }
  1299. }
  1300. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1301. {
  1302. gpr16(insn.reg16()) = sign_extended_to<u16>(insn.modrm().read8(*this, insn));
  1303. }
  1304. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1305. {
  1306. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read16(*this, insn));
  1307. }
  1308. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1309. {
  1310. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read8(*this, insn));
  1311. }
  1312. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1313. {
  1314. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  1315. }
  1316. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1317. {
  1318. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  1319. }
  1320. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1321. {
  1322. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  1323. }
  1324. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1325. {
  1326. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1327. }
  1328. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1329. {
  1330. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1331. }
  1332. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1333. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1334. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1335. {
  1336. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1337. }
  1338. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1339. {
  1340. insn.modrm().write16(*this, insn, insn.imm16());
  1341. }
  1342. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1343. {
  1344. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1345. }
  1346. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1347. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1348. {
  1349. insn.modrm().write32(*this, insn, insn.imm32());
  1350. }
  1351. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1352. {
  1353. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1354. }
  1355. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1356. {
  1357. insn.modrm().write8(*this, insn, insn.imm8());
  1358. }
  1359. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1360. {
  1361. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1362. }
  1363. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1364. {
  1365. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1366. }
  1367. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1368. {
  1369. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1370. }
  1371. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1372. {
  1373. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1374. }
  1375. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1376. {
  1377. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1378. }
  1379. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1380. {
  1381. gpr16(insn.reg16()) = insn.imm16();
  1382. }
  1383. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1384. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1385. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1386. {
  1387. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1388. }
  1389. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1390. {
  1391. gpr32(insn.reg32()) = insn.imm32();
  1392. }
  1393. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1394. {
  1395. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1396. }
  1397. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1398. {
  1399. gpr8(insn.reg8()) = insn.imm8();
  1400. }
  1401. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1402. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1403. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  1404. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1405. {
  1406. u64 result = (u64)eax() * (u64)insn.modrm().read32(*this, insn);
  1407. set_eax(result & 0xffffffff);
  1408. set_edx(result >> 32);
  1409. set_cf(edx() != 0);
  1410. set_of(edx() != 0);
  1411. }
  1412. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  1413. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1414. {
  1415. insn.modrm().write16(*this, insn, op_sub<u16>(*this, 0, insn.modrm().read16(*this, insn)));
  1416. }
  1417. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1418. {
  1419. insn.modrm().write32(*this, insn, op_sub<u32>(*this, 0, insn.modrm().read32(*this, insn)));
  1420. }
  1421. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1422. {
  1423. insn.modrm().write8(*this, insn, op_sub<u8>(*this, 0, insn.modrm().read8(*this, insn)));
  1424. }
  1425. void SoftCPU::NOP(const X86::Instruction&)
  1426. {
  1427. }
  1428. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1429. {
  1430. insn.modrm().write16(*this, insn, ~insn.modrm().read16(*this, insn));
  1431. }
  1432. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1433. {
  1434. insn.modrm().write32(*this, insn, ~insn.modrm().read32(*this, insn));
  1435. }
  1436. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1437. {
  1438. insn.modrm().write8(*this, insn, ~insn.modrm().read8(*this, insn));
  1439. }
  1440. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1441. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1442. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1443. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1444. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1445. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1446. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1447. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1448. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1449. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1450. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1451. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1452. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1453. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1454. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1455. void SoftCPU::POPFD(const X86::Instruction&)
  1456. {
  1457. m_eflags &= ~0x00fcffff;
  1458. m_eflags |= pop32() & 0x00fcffff;
  1459. }
  1460. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1461. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1462. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1463. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1464. void SoftCPU::POP_RM16(const X86::Instruction& insn)
  1465. {
  1466. insn.modrm().write16(*this, insn, pop16());
  1467. }
  1468. void SoftCPU::POP_RM32(const X86::Instruction& insn)
  1469. {
  1470. insn.modrm().write32(*this, insn, pop32());
  1471. }
  1472. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1473. void SoftCPU::POP_reg16(const X86::Instruction& insn)
  1474. {
  1475. gpr16(insn.reg16()) = pop16();
  1476. }
  1477. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1478. {
  1479. gpr32(insn.reg32()) = pop32();
  1480. }
  1481. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1482. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1483. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1484. void SoftCPU::PUSHFD(const X86::Instruction&)
  1485. {
  1486. push32(m_eflags & 0x00fcffff);
  1487. }
  1488. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1489. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1490. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1491. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1492. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1493. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1494. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1495. {
  1496. push32(insn.modrm().read32(*this, insn));
  1497. }
  1498. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1499. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1500. void SoftCPU::PUSH_imm16(const X86::Instruction& insn)
  1501. {
  1502. push16(insn.imm16());
  1503. }
  1504. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1505. {
  1506. push32(insn.imm32());
  1507. }
  1508. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1509. {
  1510. ASSERT(!insn.has_operand_size_override_prefix());
  1511. push32(sign_extended_to<i32>(insn.imm8()));
  1512. }
  1513. void SoftCPU::PUSH_reg16(const X86::Instruction& insn)
  1514. {
  1515. push16(gpr16(insn.reg16()));
  1516. }
  1517. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1518. {
  1519. push32(gpr32(insn.reg32()));
  1520. if (m_secret_handshake_state == 2) {
  1521. m_secret_data[0] = gpr32(insn.reg32());
  1522. ++m_secret_handshake_state;
  1523. } else if (m_secret_handshake_state == 3) {
  1524. m_secret_data[1] = gpr32(insn.reg32());
  1525. ++m_secret_handshake_state;
  1526. } else if (m_secret_handshake_state == 4) {
  1527. m_secret_data[2] = gpr32(insn.reg32());
  1528. m_secret_handshake_state = 0;
  1529. did_receive_secret_data();
  1530. }
  1531. }
  1532. template<typename T, bool cf>
  1533. ALWAYS_INLINE static T op_rcl_impl(SoftCPU& cpu, T data, u8 steps)
  1534. {
  1535. if (steps == 0)
  1536. return data;
  1537. u32 result = 0;
  1538. u32 new_flags = 0;
  1539. if constexpr (cf)
  1540. asm volatile("stc");
  1541. else
  1542. asm volatile("clc");
  1543. if constexpr (sizeof(T) == 4) {
  1544. asm volatile("rcll %%cl, %%eax\n"
  1545. : "=a"(result)
  1546. : "a"(data), "c"(steps));
  1547. } else if constexpr (sizeof(T) == 2) {
  1548. asm volatile("rclw %%cl, %%ax\n"
  1549. : "=a"(result)
  1550. : "a"(data), "c"(steps));
  1551. } else if constexpr (sizeof(T) == 1) {
  1552. asm volatile("rclb %%cl, %%al\n"
  1553. : "=a"(result)
  1554. : "a"(data), "c"(steps));
  1555. }
  1556. asm volatile(
  1557. "pushf\n"
  1558. "pop %%ebx"
  1559. : "=b"(new_flags));
  1560. cpu.set_flags_oc(new_flags);
  1561. return result;
  1562. }
  1563. template<typename T>
  1564. ALWAYS_INLINE static T op_rcl(SoftCPU& cpu, T data, u8 steps)
  1565. {
  1566. if (cpu.cf())
  1567. return op_rcl_impl<T, true>(cpu, data, steps);
  1568. return op_rcl_impl<T, false>(cpu, data, steps);
  1569. }
  1570. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCL, op_rcl)
  1571. template<typename T, bool cf>
  1572. ALWAYS_INLINE static T op_rcr_impl(SoftCPU& cpu, T data, u8 steps)
  1573. {
  1574. if (steps == 0)
  1575. return data;
  1576. u32 result = 0;
  1577. u32 new_flags = 0;
  1578. if constexpr (cf)
  1579. asm volatile("stc");
  1580. else
  1581. asm volatile("clc");
  1582. if constexpr (sizeof(T) == 4) {
  1583. asm volatile("rcrl %%cl, %%eax\n"
  1584. : "=a"(result)
  1585. : "a"(data), "c"(steps));
  1586. } else if constexpr (sizeof(T) == 2) {
  1587. asm volatile("rcrw %%cl, %%ax\n"
  1588. : "=a"(result)
  1589. : "a"(data), "c"(steps));
  1590. } else if constexpr (sizeof(T) == 1) {
  1591. asm volatile("rcrb %%cl, %%al\n"
  1592. : "=a"(result)
  1593. : "a"(data), "c"(steps));
  1594. }
  1595. asm volatile(
  1596. "pushf\n"
  1597. "pop %%ebx"
  1598. : "=b"(new_flags));
  1599. cpu.set_flags_oc(new_flags);
  1600. return result;
  1601. }
  1602. template<typename T>
  1603. ALWAYS_INLINE static T op_rcr(SoftCPU& cpu, T data, u8 steps)
  1604. {
  1605. if (cpu.cf())
  1606. return op_rcr_impl<T, true>(cpu, data, steps);
  1607. return op_rcr_impl<T, false>(cpu, data, steps);
  1608. }
  1609. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCR, op_rcr)
  1610. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1611. void SoftCPU::RET(const X86::Instruction& insn)
  1612. {
  1613. ASSERT(!insn.has_operand_size_override_prefix());
  1614. set_eip(pop32());
  1615. }
  1616. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1617. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1618. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1619. {
  1620. ASSERT(!insn.has_operand_size_override_prefix());
  1621. set_eip(pop32());
  1622. set_esp(esp() + insn.imm16());
  1623. }
  1624. template<typename T>
  1625. ALWAYS_INLINE static T op_rol(SoftCPU& cpu, T data, u8 steps)
  1626. {
  1627. if (steps == 0)
  1628. return data;
  1629. u32 result = 0;
  1630. u32 new_flags = 0;
  1631. if constexpr (sizeof(T) == 4) {
  1632. asm volatile("roll %%cl, %%eax\n"
  1633. : "=a"(result)
  1634. : "a"(data), "c"(steps));
  1635. } else if constexpr (sizeof(T) == 2) {
  1636. asm volatile("rolw %%cl, %%ax\n"
  1637. : "=a"(result)
  1638. : "a"(data), "c"(steps));
  1639. } else if constexpr (sizeof(T) == 1) {
  1640. asm volatile("rolb %%cl, %%al\n"
  1641. : "=a"(result)
  1642. : "a"(data), "c"(steps));
  1643. }
  1644. asm volatile(
  1645. "pushf\n"
  1646. "pop %%ebx"
  1647. : "=b"(new_flags));
  1648. cpu.set_flags_oc(new_flags);
  1649. return result;
  1650. }
  1651. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROL, op_rol)
  1652. template<typename T>
  1653. ALWAYS_INLINE static T op_ror(SoftCPU& cpu, T data, u8 steps)
  1654. {
  1655. if (steps == 0)
  1656. return data;
  1657. u32 result = 0;
  1658. u32 new_flags = 0;
  1659. if constexpr (sizeof(T) == 4) {
  1660. asm volatile("rorl %%cl, %%eax\n"
  1661. : "=a"(result)
  1662. : "a"(data), "c"(steps));
  1663. } else if constexpr (sizeof(T) == 2) {
  1664. asm volatile("rorw %%cl, %%ax\n"
  1665. : "=a"(result)
  1666. : "a"(data), "c"(steps));
  1667. } else if constexpr (sizeof(T) == 1) {
  1668. asm volatile("rorb %%cl, %%al\n"
  1669. : "=a"(result)
  1670. : "a"(data), "c"(steps));
  1671. }
  1672. asm volatile(
  1673. "pushf\n"
  1674. "pop %%ebx"
  1675. : "=b"(new_flags));
  1676. cpu.set_flags_oc(new_flags);
  1677. return result;
  1678. }
  1679. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROR, op_ror)
  1680. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1681. void SoftCPU::SALC(const X86::Instruction&)
  1682. {
  1683. set_al(cf() ? 0xff : 0x00);
  1684. if (m_secret_handshake_state < 2)
  1685. ++m_secret_handshake_state;
  1686. else
  1687. m_secret_handshake_state = 0;
  1688. }
  1689. template<typename T>
  1690. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1691. {
  1692. if (steps == 0)
  1693. return data;
  1694. u32 result = 0;
  1695. u32 new_flags = 0;
  1696. if constexpr (sizeof(T) == 4) {
  1697. asm volatile("sarl %%cl, %%eax\n"
  1698. : "=a"(result)
  1699. : "a"(data), "c"(steps));
  1700. } else if constexpr (sizeof(T) == 2) {
  1701. asm volatile("sarw %%cl, %%ax\n"
  1702. : "=a"(result)
  1703. : "a"(data), "c"(steps));
  1704. } else if constexpr (sizeof(T) == 1) {
  1705. asm volatile("sarb %%cl, %%al\n"
  1706. : "=a"(result)
  1707. : "a"(data), "c"(steps));
  1708. }
  1709. asm volatile(
  1710. "pushf\n"
  1711. "pop %%ebx"
  1712. : "=b"(new_flags));
  1713. cpu.set_flags_oszapc(new_flags);
  1714. return result;
  1715. }
  1716. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SAR, op_sar)
  1717. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1718. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1719. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1720. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1721. {
  1722. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1723. }
  1724. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1725. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction& insn)
  1726. {
  1727. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1728. }
  1729. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction& insn)
  1730. {
  1731. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1732. }
  1733. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction& insn)
  1734. {
  1735. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1736. }
  1737. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction& insn)
  1738. {
  1739. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1740. }
  1741. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHL, op_shl)
  1742. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction& insn)
  1743. {
  1744. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1745. }
  1746. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction& insn)
  1747. {
  1748. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1749. }
  1750. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction& insn)
  1751. {
  1752. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1753. }
  1754. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction& insn)
  1755. {
  1756. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1757. }
  1758. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHR, op_shr)
  1759. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1760. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1761. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1762. void SoftCPU::STC(const X86::Instruction&)
  1763. {
  1764. set_cf(true);
  1765. }
  1766. void SoftCPU::STD(const X86::Instruction&)
  1767. {
  1768. set_df(true);
  1769. }
  1770. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1771. void SoftCPU::STOSB(const X86::Instruction& insn)
  1772. {
  1773. if (insn.has_address_size_override_prefix()) {
  1774. do_once_or_repeat<false>(insn, [&] {
  1775. write_memory8({ es(), di() }, al());
  1776. set_di(di() + (df() ? -1 : 1));
  1777. });
  1778. } else {
  1779. do_once_or_repeat<false>(insn, [&] {
  1780. write_memory8({ es(), edi() }, al());
  1781. set_edi(edi() + (df() ? -1 : 1));
  1782. });
  1783. }
  1784. }
  1785. void SoftCPU::STOSD(const X86::Instruction& insn)
  1786. {
  1787. if (insn.has_address_size_override_prefix()) {
  1788. do_once_or_repeat<false>(insn, [&] {
  1789. write_memory32({ es(), di() }, eax());
  1790. set_di(di() + (df() ? -4 : 4));
  1791. });
  1792. } else {
  1793. do_once_or_repeat<false>(insn, [&] {
  1794. write_memory32({ es(), edi() }, eax());
  1795. set_edi(edi() + (df() ? -4 : 4));
  1796. });
  1797. }
  1798. }
  1799. void SoftCPU::STOSW(const X86::Instruction& insn)
  1800. {
  1801. if (insn.has_address_size_override_prefix()) {
  1802. do_once_or_repeat<false>(insn, [&] {
  1803. write_memory16({ es(), di() }, ax());
  1804. set_di(di() + (df() ? -2 : 2));
  1805. });
  1806. } else {
  1807. do_once_or_repeat<false>(insn, [&] {
  1808. write_memory16({ es(), edi() }, ax());
  1809. set_edi(edi() + (df() ? -2 : 2));
  1810. });
  1811. }
  1812. }
  1813. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1814. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1815. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1816. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1817. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1818. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1819. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1820. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1821. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1822. {
  1823. auto dest = insn.modrm().read16(*this, insn);
  1824. auto src = gpr16(insn.reg16());
  1825. auto result = op_add(*this, dest, src);
  1826. gpr16(insn.reg16()) = dest;
  1827. insn.modrm().write16(*this, insn, result);
  1828. }
  1829. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1830. {
  1831. auto dest = insn.modrm().read32(*this, insn);
  1832. auto src = gpr32(insn.reg32());
  1833. auto result = op_add(*this, dest, src);
  1834. gpr32(insn.reg32()) = dest;
  1835. insn.modrm().write32(*this, insn, result);
  1836. }
  1837. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1838. {
  1839. auto dest = insn.modrm().read8(*this, insn);
  1840. auto src = gpr8(insn.reg8());
  1841. auto result = op_add(*this, dest, src);
  1842. gpr8(insn.reg8()) = dest;
  1843. insn.modrm().write8(*this, insn, result);
  1844. }
  1845. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1846. {
  1847. auto temp = gpr16(insn.reg16());
  1848. gpr16(insn.reg16()) = eax();
  1849. set_eax(temp);
  1850. }
  1851. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1852. {
  1853. auto temp = gpr32(insn.reg32());
  1854. gpr32(insn.reg32()) = eax();
  1855. set_eax(temp);
  1856. }
  1857. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1858. {
  1859. auto temp = insn.modrm().read16(*this, insn);
  1860. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1861. gpr16(insn.reg16()) = temp;
  1862. }
  1863. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1864. {
  1865. auto temp = insn.modrm().read32(*this, insn);
  1866. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1867. gpr32(insn.reg32()) = temp;
  1868. }
  1869. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1870. {
  1871. auto temp = insn.modrm().read8(*this, insn);
  1872. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1873. gpr8(insn.reg8()) = temp;
  1874. }
  1875. void SoftCPU::XLAT(const X86::Instruction& insn)
  1876. {
  1877. u32 offset = (insn.a32() ? ebx() : bx()) + al();
  1878. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), offset }));
  1879. }
  1880. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1881. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1882. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1883. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1884. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1885. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1886. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1887. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1888. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1889. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1890. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1891. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1892. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1893. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1894. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1895. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1896. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1897. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1898. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1899. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1900. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  1901. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1902. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1903. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1904. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1905. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1906. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1907. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1908. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1909. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1910. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1911. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1912. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1913. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1914. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1915. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1916. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1917. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1918. }