SoftCPU.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  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. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  1181. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  1182. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  1183. void SoftCPU::LOOPNZ_imm8(const X86::Instruction& insn)
  1184. {
  1185. if (insn.a32()) {
  1186. set_ecx(ecx() - 1);
  1187. if (ecx() != 0 && !zf())
  1188. set_eip(eip() + (i8)insn.imm8());
  1189. } else {
  1190. set_cx(cx() - 1);
  1191. if (cx() != 0 && !zf())
  1192. set_eip(eip() + (i8)insn.imm8());
  1193. }
  1194. }
  1195. void SoftCPU::LOOPZ_imm8(const X86::Instruction& insn)
  1196. {
  1197. if (insn.a32()) {
  1198. set_ecx(ecx() - 1);
  1199. if (ecx() != 0 && zf())
  1200. set_eip(eip() + (i8)insn.imm8());
  1201. } else {
  1202. set_cx(cx() - 1);
  1203. if (cx() != 0 && zf())
  1204. set_eip(eip() + (i8)insn.imm8());
  1205. }
  1206. }
  1207. void SoftCPU::LOOP_imm8(const X86::Instruction& insn)
  1208. {
  1209. if (insn.a32()) {
  1210. set_ecx(ecx() - 1);
  1211. if (ecx() != 0)
  1212. set_eip(eip() + (i8)insn.imm8());
  1213. } else {
  1214. set_cx(cx() - 1);
  1215. if (cx() != 0)
  1216. set_eip(eip() + (i8)insn.imm8());
  1217. }
  1218. }
  1219. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  1220. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  1221. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1222. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1223. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  1224. void SoftCPU::MOVSB(const X86::Instruction& insn)
  1225. {
  1226. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1227. if (insn.has_address_size_override_prefix()) {
  1228. do_once_or_repeat<false>(insn, [&] {
  1229. auto src = read_memory8({ src_segment, si() });
  1230. write_memory8({ es(), di() }, src);
  1231. set_di(di() + (df() ? -1 : 1));
  1232. set_si(si() + (df() ? -1 : 1));
  1233. });
  1234. } else {
  1235. do_once_or_repeat<false>(insn, [&] {
  1236. auto src = read_memory8({ src_segment, esi() });
  1237. write_memory8({ es(), edi() }, src);
  1238. set_edi(edi() + (df() ? -1 : 1));
  1239. set_esi(esi() + (df() ? -1 : 1));
  1240. });
  1241. }
  1242. }
  1243. void SoftCPU::MOVSD(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_memory32({ src_segment, si() });
  1249. write_memory32({ es(), di() }, src);
  1250. set_di(di() + (df() ? -4 : 4));
  1251. set_si(si() + (df() ? -4 : 4));
  1252. });
  1253. } else {
  1254. do_once_or_repeat<false>(insn, [&] {
  1255. auto src = read_memory32({ src_segment, esi() });
  1256. write_memory32({ es(), edi() }, src);
  1257. set_edi(edi() + (df() ? -4 : 4));
  1258. set_esi(esi() + (df() ? -4 : 4));
  1259. });
  1260. }
  1261. }
  1262. void SoftCPU::MOVSW(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_memory16({ src_segment, si() });
  1268. write_memory16({ es(), di() }, src);
  1269. set_di(di() + (df() ? -2 : 2));
  1270. set_si(si() + (df() ? -2 : 2));
  1271. });
  1272. } else {
  1273. do_once_or_repeat<false>(insn, [&] {
  1274. auto src = read_memory16({ src_segment, esi() });
  1275. write_memory16({ es(), edi() }, src);
  1276. set_edi(edi() + (df() ? -2 : 2));
  1277. set_esi(esi() + (df() ? -2 : 2));
  1278. });
  1279. }
  1280. }
  1281. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1282. {
  1283. gpr16(insn.reg16()) = sign_extended_to<u16>(insn.modrm().read8(*this, insn));
  1284. }
  1285. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1286. {
  1287. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read16(*this, insn));
  1288. }
  1289. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1290. {
  1291. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read8(*this, insn));
  1292. }
  1293. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1294. {
  1295. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  1296. }
  1297. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1298. {
  1299. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  1300. }
  1301. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1302. {
  1303. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  1304. }
  1305. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1306. {
  1307. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1308. }
  1309. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1310. {
  1311. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1312. }
  1313. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1314. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1315. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1316. {
  1317. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1318. }
  1319. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1320. {
  1321. insn.modrm().write16(*this, insn, insn.imm16());
  1322. }
  1323. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1324. {
  1325. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1326. }
  1327. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1328. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1329. {
  1330. insn.modrm().write32(*this, insn, insn.imm32());
  1331. }
  1332. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1333. {
  1334. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1335. }
  1336. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1337. {
  1338. insn.modrm().write8(*this, insn, insn.imm8());
  1339. }
  1340. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1341. {
  1342. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1343. }
  1344. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1345. {
  1346. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1347. }
  1348. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1349. {
  1350. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1351. }
  1352. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1353. {
  1354. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1355. }
  1356. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1357. {
  1358. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1359. }
  1360. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1361. {
  1362. gpr16(insn.reg16()) = insn.imm16();
  1363. }
  1364. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1365. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1366. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1367. {
  1368. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1369. }
  1370. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1371. {
  1372. gpr32(insn.reg32()) = insn.imm32();
  1373. }
  1374. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1375. {
  1376. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1377. }
  1378. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1379. {
  1380. gpr8(insn.reg8()) = insn.imm8();
  1381. }
  1382. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1383. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1384. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  1385. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1386. {
  1387. u64 result = (u64)eax() * (u64)insn.modrm().read32(*this, insn);
  1388. set_eax(result & 0xffffffff);
  1389. set_edx(result >> 32);
  1390. set_cf(edx() != 0);
  1391. set_of(edx() != 0);
  1392. }
  1393. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  1394. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1395. {
  1396. insn.modrm().write16(*this, insn, op_sub<u16>(*this, 0, insn.modrm().read16(*this, insn)));
  1397. }
  1398. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1399. {
  1400. insn.modrm().write32(*this, insn, op_sub<u32>(*this, 0, insn.modrm().read32(*this, insn)));
  1401. }
  1402. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1403. {
  1404. insn.modrm().write8(*this, insn, op_sub<u8>(*this, 0, insn.modrm().read8(*this, insn)));
  1405. }
  1406. void SoftCPU::NOP(const X86::Instruction&)
  1407. {
  1408. }
  1409. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1410. {
  1411. insn.modrm().write16(*this, insn, ~insn.modrm().read16(*this, insn));
  1412. }
  1413. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1414. {
  1415. insn.modrm().write32(*this, insn, ~insn.modrm().read32(*this, insn));
  1416. }
  1417. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1418. {
  1419. insn.modrm().write8(*this, insn, ~insn.modrm().read8(*this, insn));
  1420. }
  1421. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1422. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1423. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1424. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1425. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1426. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1427. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1428. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1429. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1430. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1431. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1432. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1433. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1434. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1435. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1436. void SoftCPU::POPFD(const X86::Instruction&)
  1437. {
  1438. m_eflags &= ~0x00fcffff;
  1439. m_eflags |= pop32() & 0x00fcffff;
  1440. }
  1441. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1442. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1443. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1444. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1445. void SoftCPU::POP_RM16(const X86::Instruction& insn)
  1446. {
  1447. insn.modrm().write16(*this, insn, pop16());
  1448. }
  1449. void SoftCPU::POP_RM32(const X86::Instruction& insn)
  1450. {
  1451. insn.modrm().write32(*this, insn, pop32());
  1452. }
  1453. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1454. void SoftCPU::POP_reg16(const X86::Instruction& insn)
  1455. {
  1456. gpr16(insn.reg16()) = pop16();
  1457. }
  1458. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1459. {
  1460. gpr32(insn.reg32()) = pop32();
  1461. }
  1462. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1463. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1464. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1465. void SoftCPU::PUSHFD(const X86::Instruction&)
  1466. {
  1467. push32(m_eflags & 0x00fcffff);
  1468. }
  1469. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1470. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1471. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1472. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1473. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1474. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1475. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1476. {
  1477. push32(insn.modrm().read32(*this, insn));
  1478. }
  1479. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1480. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1481. void SoftCPU::PUSH_imm16(const X86::Instruction& insn)
  1482. {
  1483. push16(insn.imm16());
  1484. }
  1485. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1486. {
  1487. push32(insn.imm32());
  1488. }
  1489. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1490. {
  1491. ASSERT(!insn.has_operand_size_override_prefix());
  1492. push32(sign_extended_to<i32>(insn.imm8()));
  1493. }
  1494. void SoftCPU::PUSH_reg16(const X86::Instruction& insn)
  1495. {
  1496. push16(gpr16(insn.reg16()));
  1497. }
  1498. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1499. {
  1500. push32(gpr32(insn.reg32()));
  1501. if (m_secret_handshake_state == 2) {
  1502. m_secret_data[0] = gpr32(insn.reg32());
  1503. ++m_secret_handshake_state;
  1504. } else if (m_secret_handshake_state == 3) {
  1505. m_secret_data[1] = gpr32(insn.reg32());
  1506. ++m_secret_handshake_state;
  1507. } else if (m_secret_handshake_state == 4) {
  1508. m_secret_data[2] = gpr32(insn.reg32());
  1509. m_secret_handshake_state = 0;
  1510. did_receive_secret_data();
  1511. }
  1512. }
  1513. template<typename T, bool cf>
  1514. ALWAYS_INLINE static T op_rcl_impl(SoftCPU& cpu, T data, u8 steps)
  1515. {
  1516. if (steps == 0)
  1517. return data;
  1518. u32 result = 0;
  1519. u32 new_flags = 0;
  1520. if constexpr (cf)
  1521. asm volatile("stc");
  1522. else
  1523. asm volatile("clc");
  1524. if constexpr (sizeof(T) == 4) {
  1525. asm volatile("rcll %%cl, %%eax\n"
  1526. : "=a"(result)
  1527. : "a"(data), "c"(steps));
  1528. } else if constexpr (sizeof(T) == 2) {
  1529. asm volatile("rclw %%cl, %%ax\n"
  1530. : "=a"(result)
  1531. : "a"(data), "c"(steps));
  1532. } else if constexpr (sizeof(T) == 1) {
  1533. asm volatile("rclb %%cl, %%al\n"
  1534. : "=a"(result)
  1535. : "a"(data), "c"(steps));
  1536. }
  1537. asm volatile(
  1538. "pushf\n"
  1539. "pop %%ebx"
  1540. : "=b"(new_flags));
  1541. cpu.set_flags_oc(new_flags);
  1542. return result;
  1543. }
  1544. template<typename T>
  1545. ALWAYS_INLINE static T op_rcl(SoftCPU& cpu, T data, u8 steps)
  1546. {
  1547. if (cpu.cf())
  1548. return op_rcl_impl<T, true>(cpu, data, steps);
  1549. return op_rcl_impl<T, false>(cpu, data, steps);
  1550. }
  1551. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCL, op_rcl)
  1552. template<typename T, bool cf>
  1553. ALWAYS_INLINE static T op_rcr_impl(SoftCPU& cpu, T data, u8 steps)
  1554. {
  1555. if (steps == 0)
  1556. return data;
  1557. u32 result = 0;
  1558. u32 new_flags = 0;
  1559. if constexpr (cf)
  1560. asm volatile("stc");
  1561. else
  1562. asm volatile("clc");
  1563. if constexpr (sizeof(T) == 4) {
  1564. asm volatile("rcrl %%cl, %%eax\n"
  1565. : "=a"(result)
  1566. : "a"(data), "c"(steps));
  1567. } else if constexpr (sizeof(T) == 2) {
  1568. asm volatile("rcrw %%cl, %%ax\n"
  1569. : "=a"(result)
  1570. : "a"(data), "c"(steps));
  1571. } else if constexpr (sizeof(T) == 1) {
  1572. asm volatile("rcrb %%cl, %%al\n"
  1573. : "=a"(result)
  1574. : "a"(data), "c"(steps));
  1575. }
  1576. asm volatile(
  1577. "pushf\n"
  1578. "pop %%ebx"
  1579. : "=b"(new_flags));
  1580. cpu.set_flags_oc(new_flags);
  1581. return result;
  1582. }
  1583. template<typename T>
  1584. ALWAYS_INLINE static T op_rcr(SoftCPU& cpu, T data, u8 steps)
  1585. {
  1586. if (cpu.cf())
  1587. return op_rcr_impl<T, true>(cpu, data, steps);
  1588. return op_rcr_impl<T, false>(cpu, data, steps);
  1589. }
  1590. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCR, op_rcr)
  1591. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1592. void SoftCPU::RET(const X86::Instruction& insn)
  1593. {
  1594. ASSERT(!insn.has_operand_size_override_prefix());
  1595. set_eip(pop32());
  1596. }
  1597. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1598. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1599. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1600. {
  1601. ASSERT(!insn.has_operand_size_override_prefix());
  1602. set_eip(pop32());
  1603. set_esp(esp() + insn.imm16());
  1604. }
  1605. template<typename T>
  1606. ALWAYS_INLINE static T op_rol(SoftCPU& cpu, T data, u8 steps)
  1607. {
  1608. if (steps == 0)
  1609. return data;
  1610. u32 result = 0;
  1611. u32 new_flags = 0;
  1612. if constexpr (sizeof(T) == 4) {
  1613. asm volatile("roll %%cl, %%eax\n"
  1614. : "=a"(result)
  1615. : "a"(data), "c"(steps));
  1616. } else if constexpr (sizeof(T) == 2) {
  1617. asm volatile("rolw %%cl, %%ax\n"
  1618. : "=a"(result)
  1619. : "a"(data), "c"(steps));
  1620. } else if constexpr (sizeof(T) == 1) {
  1621. asm volatile("rolb %%cl, %%al\n"
  1622. : "=a"(result)
  1623. : "a"(data), "c"(steps));
  1624. }
  1625. asm volatile(
  1626. "pushf\n"
  1627. "pop %%ebx"
  1628. : "=b"(new_flags));
  1629. cpu.set_flags_oc(new_flags);
  1630. return result;
  1631. }
  1632. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROL, op_rol)
  1633. template<typename T>
  1634. ALWAYS_INLINE static T op_ror(SoftCPU& cpu, T data, u8 steps)
  1635. {
  1636. if (steps == 0)
  1637. return data;
  1638. u32 result = 0;
  1639. u32 new_flags = 0;
  1640. if constexpr (sizeof(T) == 4) {
  1641. asm volatile("rorl %%cl, %%eax\n"
  1642. : "=a"(result)
  1643. : "a"(data), "c"(steps));
  1644. } else if constexpr (sizeof(T) == 2) {
  1645. asm volatile("rorw %%cl, %%ax\n"
  1646. : "=a"(result)
  1647. : "a"(data), "c"(steps));
  1648. } else if constexpr (sizeof(T) == 1) {
  1649. asm volatile("rorb %%cl, %%al\n"
  1650. : "=a"(result)
  1651. : "a"(data), "c"(steps));
  1652. }
  1653. asm volatile(
  1654. "pushf\n"
  1655. "pop %%ebx"
  1656. : "=b"(new_flags));
  1657. cpu.set_flags_oc(new_flags);
  1658. return result;
  1659. }
  1660. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROR, op_ror)
  1661. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1662. void SoftCPU::SALC(const X86::Instruction&)
  1663. {
  1664. set_al(cf() ? 0xff : 0x00);
  1665. if (m_secret_handshake_state < 2)
  1666. ++m_secret_handshake_state;
  1667. else
  1668. m_secret_handshake_state = 0;
  1669. }
  1670. template<typename T>
  1671. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1672. {
  1673. if (steps == 0)
  1674. return data;
  1675. u32 result = 0;
  1676. u32 new_flags = 0;
  1677. if constexpr (sizeof(T) == 4) {
  1678. asm volatile("sarl %%cl, %%eax\n"
  1679. : "=a"(result)
  1680. : "a"(data), "c"(steps));
  1681. } else if constexpr (sizeof(T) == 2) {
  1682. asm volatile("sarw %%cl, %%ax\n"
  1683. : "=a"(result)
  1684. : "a"(data), "c"(steps));
  1685. } else if constexpr (sizeof(T) == 1) {
  1686. asm volatile("sarb %%cl, %%al\n"
  1687. : "=a"(result)
  1688. : "a"(data), "c"(steps));
  1689. }
  1690. asm volatile(
  1691. "pushf\n"
  1692. "pop %%ebx"
  1693. : "=b"(new_flags));
  1694. cpu.set_flags_oszapc(new_flags);
  1695. return result;
  1696. }
  1697. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SAR, op_sar)
  1698. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1699. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1700. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1701. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1702. {
  1703. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1704. }
  1705. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1706. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction& insn)
  1707. {
  1708. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1709. }
  1710. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction& insn)
  1711. {
  1712. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1713. }
  1714. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction& insn)
  1715. {
  1716. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1717. }
  1718. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction& insn)
  1719. {
  1720. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1721. }
  1722. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHL, op_shl)
  1723. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction& insn)
  1724. {
  1725. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1726. }
  1727. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction& insn)
  1728. {
  1729. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1730. }
  1731. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction& insn)
  1732. {
  1733. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1734. }
  1735. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction& insn)
  1736. {
  1737. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1738. }
  1739. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHR, op_shr)
  1740. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1741. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1742. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1743. void SoftCPU::STC(const X86::Instruction&)
  1744. {
  1745. set_cf(true);
  1746. }
  1747. void SoftCPU::STD(const X86::Instruction&)
  1748. {
  1749. set_df(true);
  1750. }
  1751. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1752. void SoftCPU::STOSB(const X86::Instruction& insn)
  1753. {
  1754. if (insn.has_address_size_override_prefix()) {
  1755. do_once_or_repeat<false>(insn, [&] {
  1756. write_memory8({ es(), di() }, al());
  1757. set_di(di() + (df() ? -1 : 1));
  1758. });
  1759. } else {
  1760. do_once_or_repeat<false>(insn, [&] {
  1761. write_memory8({ es(), edi() }, al());
  1762. set_edi(edi() + (df() ? -1 : 1));
  1763. });
  1764. }
  1765. }
  1766. void SoftCPU::STOSD(const X86::Instruction& insn)
  1767. {
  1768. if (insn.has_address_size_override_prefix()) {
  1769. do_once_or_repeat<false>(insn, [&] {
  1770. write_memory32({ es(), di() }, eax());
  1771. set_di(di() + (df() ? -4 : 4));
  1772. });
  1773. } else {
  1774. do_once_or_repeat<false>(insn, [&] {
  1775. write_memory32({ es(), edi() }, eax());
  1776. set_edi(edi() + (df() ? -4 : 4));
  1777. });
  1778. }
  1779. }
  1780. void SoftCPU::STOSW(const X86::Instruction& insn)
  1781. {
  1782. if (insn.has_address_size_override_prefix()) {
  1783. do_once_or_repeat<false>(insn, [&] {
  1784. write_memory16({ es(), di() }, ax());
  1785. set_di(di() + (df() ? -2 : 2));
  1786. });
  1787. } else {
  1788. do_once_or_repeat<false>(insn, [&] {
  1789. write_memory16({ es(), edi() }, ax());
  1790. set_edi(edi() + (df() ? -2 : 2));
  1791. });
  1792. }
  1793. }
  1794. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1795. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1796. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1797. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1798. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1799. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1800. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1801. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1802. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1803. {
  1804. auto dest = insn.modrm().read16(*this, insn);
  1805. auto src = gpr16(insn.reg16());
  1806. auto result = op_add(*this, dest, src);
  1807. gpr16(insn.reg16()) = dest;
  1808. insn.modrm().write16(*this, insn, result);
  1809. }
  1810. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1811. {
  1812. auto dest = insn.modrm().read32(*this, insn);
  1813. auto src = gpr32(insn.reg32());
  1814. auto result = op_add(*this, dest, src);
  1815. gpr32(insn.reg32()) = dest;
  1816. insn.modrm().write32(*this, insn, result);
  1817. }
  1818. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1819. {
  1820. auto dest = insn.modrm().read8(*this, insn);
  1821. auto src = gpr8(insn.reg8());
  1822. auto result = op_add(*this, dest, src);
  1823. gpr8(insn.reg8()) = dest;
  1824. insn.modrm().write8(*this, insn, result);
  1825. }
  1826. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1827. {
  1828. auto temp = gpr16(insn.reg16());
  1829. gpr16(insn.reg16()) = eax();
  1830. set_eax(temp);
  1831. }
  1832. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1833. {
  1834. auto temp = gpr32(insn.reg32());
  1835. gpr32(insn.reg32()) = eax();
  1836. set_eax(temp);
  1837. }
  1838. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1839. {
  1840. auto temp = insn.modrm().read16(*this, insn);
  1841. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1842. gpr16(insn.reg16()) = temp;
  1843. }
  1844. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1845. {
  1846. auto temp = insn.modrm().read32(*this, insn);
  1847. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1848. gpr32(insn.reg32()) = temp;
  1849. }
  1850. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1851. {
  1852. auto temp = insn.modrm().read8(*this, insn);
  1853. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1854. gpr8(insn.reg8()) = temp;
  1855. }
  1856. void SoftCPU::XLAT(const X86::Instruction& insn)
  1857. {
  1858. u32 offset = (insn.a32() ? ebx() : bx()) + al();
  1859. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), offset }));
  1860. }
  1861. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1862. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1863. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1864. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1865. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1866. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1867. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1868. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1869. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1870. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1871. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1872. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1873. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1874. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1875. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1876. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1877. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1878. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1879. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1880. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1881. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  1882. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1883. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1884. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1885. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1886. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1887. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1888. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1889. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1890. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1891. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1892. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1893. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1894. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1895. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1896. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1897. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1898. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1899. }