SoftCPU.cpp 88 KB

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