SoftCPU.cpp 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SoftCPU.h"
  8. #include "Emulator.h"
  9. #include <AK/Assertions.h>
  10. #include <AK/BuiltinWrappers.h>
  11. #include <AK/Debug.h>
  12. #include <AK/Format.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #if defined(AK_COMPILER_GCC)
  17. # pragma GCC optimize("O3")
  18. #endif
  19. #define TODO_INSN() \
  20. do { \
  21. reportln("\n=={}== Unimplemented instruction: {}\n"sv, getpid(), __FUNCTION__); \
  22. m_emulator.dump_backtrace(); \
  23. _exit(0); \
  24. } while (0)
  25. #define FPU_INSTRUCTION(name) \
  26. void SoftCPU::name(const X86::Instruction& insn) \
  27. { \
  28. m_fpu.name(insn); \
  29. }
  30. #define VPU_INSTRUCTION(name) \
  31. void SoftCPU::name(const X86::Instruction& insn) \
  32. { \
  33. m_vpu.name(insn); \
  34. }
  35. #define DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(mnemonic, op) \
  36. void SoftCPU::mnemonic##_RM8_1(const X86::Instruction& insn) \
  37. { \
  38. generic_RM8_1(op<ValueWithShadow<u8>>, insn); \
  39. } \
  40. void SoftCPU::mnemonic##_RM8_CL(const X86::Instruction& insn) \
  41. { \
  42. generic_RM8_CL(op<ValueWithShadow<u8>>, insn); \
  43. } \
  44. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) \
  45. { \
  46. generic_RM8_imm8<true, false>(op<ValueWithShadow<u8>>, insn); \
  47. } \
  48. void SoftCPU::mnemonic##_RM16_1(const X86::Instruction& insn) \
  49. { \
  50. generic_RM16_1(op<ValueWithShadow<u16>>, insn); \
  51. } \
  52. void SoftCPU::mnemonic##_RM16_CL(const X86::Instruction& insn) \
  53. { \
  54. generic_RM16_CL(op<ValueWithShadow<u16>>, insn); \
  55. } \
  56. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) \
  57. { \
  58. generic_RM16_unsigned_imm8<true>(op<ValueWithShadow<u16>>, insn); \
  59. } \
  60. void SoftCPU::mnemonic##_RM32_1(const X86::Instruction& insn) \
  61. { \
  62. generic_RM32_1(op<ValueWithShadow<u32>>, insn); \
  63. } \
  64. void SoftCPU::mnemonic##_RM32_CL(const X86::Instruction& insn) \
  65. { \
  66. generic_RM32_CL(op<ValueWithShadow<u32>>, insn); \
  67. } \
  68. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) \
  69. { \
  70. generic_RM32_unsigned_imm8<true>(op<ValueWithShadow<u32>>, insn); \
  71. }
  72. namespace UserspaceEmulator {
  73. template<typename T>
  74. ALWAYS_INLINE void warn_if_uninitialized(T value_with_shadow, char const* message)
  75. {
  76. if (value_with_shadow.is_uninitialized()) [[unlikely]] {
  77. reportln("\033[31;1mWarning! Use of uninitialized value: {}\033[0m\n"sv, message);
  78. Emulator::the().dump_backtrace();
  79. }
  80. }
  81. ALWAYS_INLINE void SoftCPU::warn_if_flags_tainted(char const* message) const
  82. {
  83. if (m_flags_tainted) [[unlikely]] {
  84. reportln("\n=={}== \033[31;1mConditional depends on uninitialized data\033[0m ({})\n"sv, getpid(), message);
  85. Emulator::the().dump_backtrace();
  86. }
  87. }
  88. template<typename T, typename U>
  89. constexpr T sign_extended_to(U value)
  90. {
  91. if (!(value & X86::TypeTrivia<U>::sign_bit))
  92. return value;
  93. return (X86::TypeTrivia<T>::mask & ~X86::TypeTrivia<U>::mask) | value;
  94. }
  95. SoftCPU::SoftCPU(Emulator& emulator)
  96. : m_emulator(emulator)
  97. , m_fpu(emulator, *this)
  98. , m_vpu(emulator, *this)
  99. {
  100. PartAddressableRegister empty_reg;
  101. explicit_bzero(&empty_reg, sizeof(empty_reg));
  102. for (auto& gpr : m_gpr)
  103. gpr = ValueWithShadow<PartAddressableRegister>::create_initialized(empty_reg);
  104. m_segment[(int)X86::SegmentRegister::CS] = 0x1b;
  105. m_segment[(int)X86::SegmentRegister::DS] = 0x23;
  106. m_segment[(int)X86::SegmentRegister::ES] = 0x23;
  107. m_segment[(int)X86::SegmentRegister::SS] = 0x23;
  108. m_segment[(int)X86::SegmentRegister::FS] = 0x23;
  109. m_segment[(int)X86::SegmentRegister::GS] = 0x2b;
  110. }
  111. void SoftCPU::dump() const
  112. {
  113. outln(" eax={:p} ebx={:p} ecx={:p} edx={:p} ebp={:p} esp={:p} esi={:p} edi={:p} o={:d} s={:d} z={:d} a={:d} p={:d} c={:d}",
  114. eax(), ebx(), ecx(), edx(), ebp(), esp(), esi(), edi(), of(), sf(), zf(), af(), pf(), cf());
  115. outln("#eax={:hex-dump} #ebx={:hex-dump} #ecx={:hex-dump} #edx={:hex-dump} #ebhex-dump={:hex-dump} #eshex-dump={:hex-dump} #esi={:hex-dump} #edi={:hex-dump} #f={}",
  116. eax().shadow().span(), ebx().shadow().span(), ecx().shadow().span(), edx().shadow().span(), ebp().shadow().span(), esp().shadow().span(), esi().shadow().span(), edi().shadow().span(), m_flags_tainted);
  117. fflush(stdout);
  118. }
  119. void SoftCPU::update_code_cache()
  120. {
  121. auto* region = m_emulator.mmu().find_region({ cs(), eip() });
  122. VERIFY(region);
  123. if (!region->is_executable()) {
  124. reportln("SoftCPU::update_code_cache: Non-executable region @ {:p}"sv, eip());
  125. Emulator::the().dump_backtrace();
  126. TODO();
  127. }
  128. // FIXME: This cache needs to be invalidated if the code region is ever unmapped.
  129. m_cached_code_region = region;
  130. m_cached_code_base_ptr = region->data();
  131. }
  132. ValueWithShadow<u8> SoftCPU::read_memory8(X86::LogicalAddress address)
  133. {
  134. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  135. auto value = m_emulator.mmu().read8(address);
  136. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory8: @{:#04x}:{:p} -> {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  137. return value;
  138. }
  139. ValueWithShadow<u16> SoftCPU::read_memory16(X86::LogicalAddress address)
  140. {
  141. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  142. auto value = m_emulator.mmu().read16(address);
  143. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory16: @{:#04x}:{:p} -> {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  144. return value;
  145. }
  146. ValueWithShadow<u32> SoftCPU::read_memory32(X86::LogicalAddress address)
  147. {
  148. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  149. auto value = m_emulator.mmu().read32(address);
  150. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory32: @{:#04x}:{:p} -> {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  151. return value;
  152. }
  153. ValueWithShadow<u64> SoftCPU::read_memory64(X86::LogicalAddress address)
  154. {
  155. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  156. auto value = m_emulator.mmu().read64(address);
  157. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory64: @{:#04x}:{:p} -> {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  158. return value;
  159. }
  160. ValueWithShadow<u128> SoftCPU::read_memory128(X86::LogicalAddress address)
  161. {
  162. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  163. auto value = m_emulator.mmu().read128(address);
  164. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory128: @{:#04x}:{:p} -> {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  165. return value;
  166. }
  167. ValueWithShadow<u256> SoftCPU::read_memory256(X86::LogicalAddress address)
  168. {
  169. VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
  170. auto value = m_emulator.mmu().read256(address);
  171. outln_if(MEMORY_DEBUG, "\033[36;1mread_memory256: @{:#04x}:{:p} -> {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  172. return value;
  173. }
  174. void SoftCPU::write_memory8(X86::LogicalAddress address, ValueWithShadow<u8> value)
  175. {
  176. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  177. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory8: @{:#04x}:{:p} <- {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  178. m_emulator.mmu().write8(address, value);
  179. }
  180. void SoftCPU::write_memory16(X86::LogicalAddress address, ValueWithShadow<u16> value)
  181. {
  182. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  183. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory16: @{:#04x}:{:p} <- {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  184. m_emulator.mmu().write16(address, value);
  185. }
  186. void SoftCPU::write_memory32(X86::LogicalAddress address, ValueWithShadow<u32> value)
  187. {
  188. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  189. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory32: @{:#04x}:{:p} <- {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  190. m_emulator.mmu().write32(address, value);
  191. }
  192. void SoftCPU::write_memory64(X86::LogicalAddress address, ValueWithShadow<u64> value)
  193. {
  194. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  195. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory64: @{:#04x}:{:p} <- {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  196. m_emulator.mmu().write64(address, value);
  197. }
  198. void SoftCPU::write_memory128(X86::LogicalAddress address, ValueWithShadow<u128> value)
  199. {
  200. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  201. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory128: @{:#04x}:{:p} <- {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  202. m_emulator.mmu().write128(address, value);
  203. }
  204. void SoftCPU::write_memory256(X86::LogicalAddress address, ValueWithShadow<u256> value)
  205. {
  206. VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
  207. outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory256: @{:#04x}:{:p} <- {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
  208. m_emulator.mmu().write256(address, value);
  209. }
  210. void SoftCPU::push_string(StringView string)
  211. {
  212. u32 space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
  213. set_esp({ esp().value() - space_to_allocate, esp().shadow() });
  214. m_emulator.mmu().copy_to_vm(esp().value(), string.characters_without_null_termination(), string.length());
  215. m_emulator.mmu().write8({ 0x23, esp().value() + string.length() }, shadow_wrap_as_initialized((u8)'\0'));
  216. }
  217. void SoftCPU::push_buffer(u8 const* data, size_t size)
  218. {
  219. set_esp({ esp().value() - size, esp().shadow() });
  220. warn_if_uninitialized(esp(), "push_buffer");
  221. m_emulator.mmu().copy_to_vm(esp().value(), data, size);
  222. }
  223. void SoftCPU::push32(ValueWithShadow<u32> value)
  224. {
  225. set_esp({ esp().value() - sizeof(u32), esp().shadow() });
  226. warn_if_uninitialized(esp(), "push32");
  227. write_memory32({ ss(), esp().value() }, value);
  228. }
  229. ValueWithShadow<u32> SoftCPU::pop32()
  230. {
  231. warn_if_uninitialized(esp(), "pop32");
  232. auto value = read_memory32({ ss(), esp().value() });
  233. set_esp({ esp().value() + sizeof(u32), esp().shadow() });
  234. return value;
  235. }
  236. void SoftCPU::push16(ValueWithShadow<u16> value)
  237. {
  238. warn_if_uninitialized(esp(), "push16");
  239. set_esp({ esp().value() - sizeof(u16), esp().shadow() });
  240. write_memory16({ ss(), esp().value() }, value);
  241. }
  242. ValueWithShadow<u16> SoftCPU::pop16()
  243. {
  244. warn_if_uninitialized(esp(), "pop16");
  245. auto value = read_memory16({ ss(), esp().value() });
  246. set_esp({ esp().value() + sizeof(u16), esp().shadow() });
  247. return value;
  248. }
  249. template<bool check_zf, typename Callback>
  250. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  251. {
  252. if (!insn.has_rep_prefix())
  253. return callback();
  254. while (loop_index(insn.address_size()).value()) {
  255. callback();
  256. decrement_loop_index(insn.address_size());
  257. if constexpr (check_zf) {
  258. warn_if_flags_tainted("repz/repnz");
  259. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  260. break;
  261. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  262. break;
  263. }
  264. }
  265. }
  266. template<typename T>
  267. ALWAYS_INLINE static T op_inc(SoftCPU& cpu, T data)
  268. {
  269. typename T::ValueType result;
  270. u32 new_flags = 0;
  271. if constexpr (sizeof(typename T::ValueType) == 4) {
  272. asm volatile("incl %%eax\n"
  273. : "=a"(result)
  274. : "a"(data.value()));
  275. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  276. asm volatile("incw %%ax\n"
  277. : "=a"(result)
  278. : "a"(data.value()));
  279. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  280. asm volatile("incb %%al\n"
  281. : "=a"(result)
  282. : "a"(data.value()));
  283. }
  284. asm volatile(
  285. "pushf\n"
  286. "pop %%ebx"
  287. : "=b"(new_flags));
  288. cpu.set_flags_oszap(new_flags);
  289. cpu.taint_flags_from(data);
  290. return shadow_wrap_with_taint_from(result, data);
  291. }
  292. template<typename T>
  293. ALWAYS_INLINE static T op_dec(SoftCPU& cpu, T data)
  294. {
  295. typename T::ValueType result;
  296. u32 new_flags = 0;
  297. if constexpr (sizeof(typename T::ValueType) == 4) {
  298. asm volatile("decl %%eax\n"
  299. : "=a"(result)
  300. : "a"(data.value()));
  301. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  302. asm volatile("decw %%ax\n"
  303. : "=a"(result)
  304. : "a"(data.value()));
  305. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  306. asm volatile("decb %%al\n"
  307. : "=a"(result)
  308. : "a"(data.value()));
  309. }
  310. asm volatile(
  311. "pushf\n"
  312. "pop %%ebx"
  313. : "=b"(new_flags));
  314. cpu.set_flags_oszap(new_flags);
  315. cpu.taint_flags_from(data);
  316. return shadow_wrap_with_taint_from(result, data);
  317. }
  318. template<typename T>
  319. ALWAYS_INLINE static T op_xor(SoftCPU& cpu, T const& dest, T const& src)
  320. {
  321. typename T::ValueType result;
  322. u32 new_flags = 0;
  323. if constexpr (sizeof(typename T::ValueType) == 4) {
  324. asm volatile("xorl %%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("xor %%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("xorb %%cl, %%al\n"
  333. : "=a"(result)
  334. : "a"(dest.value()), "c"(src.value()));
  335. } else {
  336. VERIFY_NOT_REACHED();
  337. }
  338. asm volatile(
  339. "pushf\n"
  340. "pop %%ebx"
  341. : "=b"(new_flags));
  342. cpu.set_flags_oszpc(new_flags);
  343. cpu.taint_flags_from(dest, src);
  344. return shadow_wrap_with_taint_from(result, dest, src);
  345. }
  346. template<typename T>
  347. ALWAYS_INLINE static T op_or(SoftCPU& cpu, T const& dest, T const& src)
  348. {
  349. typename T::ValueType result = 0;
  350. u32 new_flags = 0;
  351. if constexpr (sizeof(typename T::ValueType) == 4) {
  352. asm volatile("orl %%ecx, %%eax\n"
  353. : "=a"(result)
  354. : "a"(dest.value()), "c"(src.value()));
  355. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  356. asm volatile("or %%cx, %%ax\n"
  357. : "=a"(result)
  358. : "a"(dest.value()), "c"(src.value()));
  359. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  360. asm volatile("orb %%cl, %%al\n"
  361. : "=a"(result)
  362. : "a"(dest.value()), "c"(src.value()));
  363. } else {
  364. VERIFY_NOT_REACHED();
  365. }
  366. asm volatile(
  367. "pushf\n"
  368. "pop %%ebx"
  369. : "=b"(new_flags));
  370. cpu.set_flags_oszpc(new_flags);
  371. cpu.taint_flags_from(dest, src);
  372. return shadow_wrap_with_taint_from(result, dest, src);
  373. }
  374. template<typename T>
  375. ALWAYS_INLINE static T op_sub(SoftCPU& cpu, T const& dest, T const& src)
  376. {
  377. typename T::ValueType result = 0;
  378. u32 new_flags = 0;
  379. if constexpr (sizeof(typename T::ValueType) == 4) {
  380. asm volatile("subl %%ecx, %%eax\n"
  381. : "=a"(result)
  382. : "a"(dest.value()), "c"(src.value()));
  383. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  384. asm volatile("subw %%cx, %%ax\n"
  385. : "=a"(result)
  386. : "a"(dest.value()), "c"(src.value()));
  387. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  388. asm volatile("subb %%cl, %%al\n"
  389. : "=a"(result)
  390. : "a"(dest.value()), "c"(src.value()));
  391. } else {
  392. VERIFY_NOT_REACHED();
  393. }
  394. asm volatile(
  395. "pushf\n"
  396. "pop %%ebx"
  397. : "=b"(new_flags));
  398. cpu.set_flags_oszapc(new_flags);
  399. cpu.taint_flags_from(dest, src);
  400. return shadow_wrap_with_taint_from(result, dest, src);
  401. }
  402. template<typename T, bool cf>
  403. ALWAYS_INLINE static T op_sbb_impl(SoftCPU& cpu, T const& dest, T const& src)
  404. {
  405. typename T::ValueType result = 0;
  406. u32 new_flags = 0;
  407. if constexpr (cf)
  408. asm volatile("stc");
  409. else
  410. asm volatile("clc");
  411. if constexpr (sizeof(typename T::ValueType) == 4) {
  412. asm volatile("sbbl %%ecx, %%eax\n"
  413. : "=a"(result)
  414. : "a"(dest.value()), "c"(src.value()));
  415. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  416. asm volatile("sbbw %%cx, %%ax\n"
  417. : "=a"(result)
  418. : "a"(dest.value()), "c"(src.value()));
  419. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  420. asm volatile("sbbb %%cl, %%al\n"
  421. : "=a"(result)
  422. : "a"(dest.value()), "c"(src.value()));
  423. } else {
  424. VERIFY_NOT_REACHED();
  425. }
  426. asm volatile(
  427. "pushf\n"
  428. "pop %%ebx"
  429. : "=b"(new_flags));
  430. cpu.set_flags_oszapc(new_flags);
  431. cpu.taint_flags_from(dest, src);
  432. return shadow_wrap_with_taint_from<typename T::ValueType>(result, dest, src);
  433. }
  434. template<typename T>
  435. ALWAYS_INLINE static T op_sbb(SoftCPU& cpu, T& dest, T const& src)
  436. {
  437. cpu.warn_if_flags_tainted("sbb");
  438. if (cpu.cf())
  439. return op_sbb_impl<T, true>(cpu, dest, src);
  440. return op_sbb_impl<T, false>(cpu, dest, src);
  441. }
  442. template<typename T>
  443. ALWAYS_INLINE static T op_add(SoftCPU& cpu, T& dest, T const& src)
  444. {
  445. typename T::ValueType result = 0;
  446. u32 new_flags = 0;
  447. if constexpr (sizeof(typename T::ValueType) == 4) {
  448. asm volatile("addl %%ecx, %%eax\n"
  449. : "=a"(result)
  450. : "a"(dest.value()), "c"(src.value()));
  451. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  452. asm volatile("addw %%cx, %%ax\n"
  453. : "=a"(result)
  454. : "a"(dest.value()), "c"(src.value()));
  455. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  456. asm volatile("addb %%cl, %%al\n"
  457. : "=a"(result)
  458. : "a"(dest.value()), "c"(src.value()));
  459. } else {
  460. VERIFY_NOT_REACHED();
  461. }
  462. asm volatile(
  463. "pushf\n"
  464. "pop %%ebx"
  465. : "=b"(new_flags));
  466. cpu.set_flags_oszapc(new_flags);
  467. cpu.taint_flags_from(dest, src);
  468. return shadow_wrap_with_taint_from<typename T::ValueType>(result, dest, src);
  469. }
  470. template<typename T, bool cf>
  471. ALWAYS_INLINE static T op_adc_impl(SoftCPU& cpu, T& dest, T const& src)
  472. {
  473. typename T::ValueType result = 0;
  474. u32 new_flags = 0;
  475. if constexpr (cf)
  476. asm volatile("stc");
  477. else
  478. asm volatile("clc");
  479. if constexpr (sizeof(typename T::ValueType) == 4) {
  480. asm volatile("adcl %%ecx, %%eax\n"
  481. : "=a"(result)
  482. : "a"(dest.value()), "c"(src.value()));
  483. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  484. asm volatile("adcw %%cx, %%ax\n"
  485. : "=a"(result)
  486. : "a"(dest.value()), "c"(src.value()));
  487. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  488. asm volatile("adcb %%cl, %%al\n"
  489. : "=a"(result)
  490. : "a"(dest.value()), "c"(src.value()));
  491. } else {
  492. VERIFY_NOT_REACHED();
  493. }
  494. asm volatile(
  495. "pushf\n"
  496. "pop %%ebx"
  497. : "=b"(new_flags));
  498. cpu.set_flags_oszapc(new_flags);
  499. cpu.taint_flags_from(dest, src);
  500. return shadow_wrap_with_taint_from<typename T::ValueType>(result, dest, src);
  501. }
  502. template<typename T>
  503. ALWAYS_INLINE static T op_adc(SoftCPU& cpu, T& dest, T const& src)
  504. {
  505. cpu.warn_if_flags_tainted("adc");
  506. if (cpu.cf())
  507. return op_adc_impl<T, true>(cpu, dest, src);
  508. return op_adc_impl<T, false>(cpu, dest, src);
  509. }
  510. template<typename T>
  511. ALWAYS_INLINE static T op_and(SoftCPU& cpu, T const& dest, T const& src)
  512. {
  513. typename T::ValueType result = 0;
  514. u32 new_flags = 0;
  515. if constexpr (sizeof(typename T::ValueType) == 4) {
  516. asm volatile("andl %%ecx, %%eax\n"
  517. : "=a"(result)
  518. : "a"(dest.value()), "c"(src.value()));
  519. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  520. asm volatile("andw %%cx, %%ax\n"
  521. : "=a"(result)
  522. : "a"(dest.value()), "c"(src.value()));
  523. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  524. asm volatile("andb %%cl, %%al\n"
  525. : "=a"(result)
  526. : "a"(dest.value()), "c"(src.value()));
  527. } else {
  528. VERIFY_NOT_REACHED();
  529. }
  530. asm volatile(
  531. "pushf\n"
  532. "pop %%ebx"
  533. : "=b"(new_flags));
  534. cpu.set_flags_oszpc(new_flags);
  535. cpu.taint_flags_from(dest, src);
  536. return shadow_wrap_with_taint_from<typename T::ValueType>(result, dest, src);
  537. }
  538. template<typename T>
  539. ALWAYS_INLINE static void op_imul(SoftCPU& cpu, T const& dest, T const& src, T& result_high, T& result_low)
  540. {
  541. bool did_overflow = false;
  542. if constexpr (sizeof(T) == 4) {
  543. i64 result = (i64)src * (i64)dest;
  544. result_low = result & 0xffffffff;
  545. result_high = result >> 32;
  546. did_overflow = (result > NumericLimits<T>::max() || result < NumericLimits<T>::min());
  547. } else if constexpr (sizeof(T) == 2) {
  548. i32 result = (i32)src * (i32)dest;
  549. result_low = result & 0xffff;
  550. result_high = result >> 16;
  551. did_overflow = (result > NumericLimits<T>::max() || result < NumericLimits<T>::min());
  552. } else if constexpr (sizeof(T) == 1) {
  553. i16 result = (i16)src * (i16)dest;
  554. result_low = result & 0xff;
  555. result_high = result >> 8;
  556. did_overflow = (result > NumericLimits<T>::max() || result < NumericLimits<T>::min());
  557. }
  558. if (did_overflow) {
  559. cpu.set_cf(true);
  560. cpu.set_of(true);
  561. } else {
  562. cpu.set_cf(false);
  563. cpu.set_of(false);
  564. }
  565. }
  566. template<typename T>
  567. ALWAYS_INLINE static T op_shr(SoftCPU& cpu, T data, 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("shrl %%cl, %%eax\n"
  575. : "=a"(result)
  576. : "a"(data.value()), "c"(steps.value()));
  577. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  578. asm volatile("shrw %%cl, %%ax\n"
  579. : "=a"(result)
  580. : "a"(data.value()), "c"(steps.value()));
  581. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  582. asm volatile("shrb %%cl, %%al\n"
  583. : "=a"(result)
  584. : "a"(data.value()), "c"(steps.value()));
  585. }
  586. asm volatile(
  587. "pushf\n"
  588. "pop %%ebx"
  589. : "=b"(new_flags));
  590. cpu.set_flags_oszapc(new_flags);
  591. cpu.taint_flags_from(data, steps);
  592. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  593. }
  594. template<typename T>
  595. ALWAYS_INLINE static T op_shl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  596. {
  597. if (steps.value() == 0)
  598. return shadow_wrap_with_taint_from(data.value(), data, steps);
  599. u32 result = 0;
  600. u32 new_flags = 0;
  601. if constexpr (sizeof(typename T::ValueType) == 4) {
  602. asm volatile("shll %%cl, %%eax\n"
  603. : "=a"(result)
  604. : "a"(data.value()), "c"(steps.value()));
  605. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  606. asm volatile("shlw %%cl, %%ax\n"
  607. : "=a"(result)
  608. : "a"(data.value()), "c"(steps.value()));
  609. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  610. asm volatile("shlb %%cl, %%al\n"
  611. : "=a"(result)
  612. : "a"(data.value()), "c"(steps.value()));
  613. }
  614. asm volatile(
  615. "pushf\n"
  616. "pop %%ebx"
  617. : "=b"(new_flags));
  618. cpu.set_flags_oszapc(new_flags);
  619. cpu.taint_flags_from(data, steps);
  620. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  621. }
  622. template<typename T>
  623. ALWAYS_INLINE static T op_shrd(SoftCPU& cpu, T data, T extra_bits, ValueWithShadow<u8> steps)
  624. {
  625. if (steps.value() == 0)
  626. return shadow_wrap_with_taint_from(data.value(), data, steps);
  627. u32 result = 0;
  628. u32 new_flags = 0;
  629. if constexpr (sizeof(typename T::ValueType) == 4) {
  630. asm volatile("shrd %%cl, %%edx, %%eax\n"
  631. : "=a"(result)
  632. : "a"(data.value()), "d"(extra_bits.value()), "c"(steps.value()));
  633. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  634. asm volatile("shrd %%cl, %%dx, %%ax\n"
  635. : "=a"(result)
  636. : "a"(data.value()), "d"(extra_bits.value()), "c"(steps.value()));
  637. }
  638. asm volatile(
  639. "pushf\n"
  640. "pop %%ebx"
  641. : "=b"(new_flags));
  642. cpu.set_flags_oszapc(new_flags);
  643. cpu.taint_flags_from(data, steps);
  644. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  645. }
  646. template<typename T>
  647. ALWAYS_INLINE static T op_shld(SoftCPU& cpu, T data, T extra_bits, ValueWithShadow<u8> steps)
  648. {
  649. if (steps.value() == 0)
  650. return shadow_wrap_with_taint_from(data.value(), data, steps);
  651. u32 result = 0;
  652. u32 new_flags = 0;
  653. if constexpr (sizeof(typename T::ValueType) == 4) {
  654. asm volatile("shld %%cl, %%edx, %%eax\n"
  655. : "=a"(result)
  656. : "a"(data.value()), "d"(extra_bits.value()), "c"(steps.value()));
  657. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  658. asm volatile("shld %%cl, %%dx, %%ax\n"
  659. : "=a"(result)
  660. : "a"(data.value()), "d"(extra_bits.value()), "c"(steps.value()));
  661. }
  662. asm volatile(
  663. "pushf\n"
  664. "pop %%ebx"
  665. : "=b"(new_flags));
  666. cpu.set_flags_oszapc(new_flags);
  667. cpu.taint_flags_from(data, steps);
  668. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  669. }
  670. template<bool update_dest, bool is_or, typename Op>
  671. ALWAYS_INLINE void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  672. {
  673. auto dest = al();
  674. auto src = shadow_wrap_as_initialized(insn.imm8());
  675. auto result = op(*this, dest, src);
  676. if (is_or && insn.imm8() == 0xff)
  677. result.set_initialized();
  678. if (update_dest)
  679. set_al(result);
  680. }
  681. template<bool update_dest, bool is_or, typename Op>
  682. ALWAYS_INLINE void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  683. {
  684. auto dest = ax();
  685. auto src = shadow_wrap_as_initialized(insn.imm16());
  686. auto result = op(*this, dest, src);
  687. if (is_or && insn.imm16() == 0xffff)
  688. result.set_initialized();
  689. if (update_dest)
  690. set_ax(result);
  691. }
  692. template<bool update_dest, bool is_or, typename Op>
  693. ALWAYS_INLINE void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  694. {
  695. auto dest = eax();
  696. auto src = shadow_wrap_as_initialized(insn.imm32());
  697. auto result = op(*this, dest, src);
  698. if (is_or && insn.imm32() == 0xffffffff)
  699. result.set_initialized();
  700. if (update_dest)
  701. set_eax(result);
  702. }
  703. template<bool update_dest, bool is_or, typename Op>
  704. ALWAYS_INLINE void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  705. {
  706. auto dest = insn.modrm().read16(*this, insn);
  707. auto src = shadow_wrap_as_initialized(insn.imm16());
  708. auto result = op(*this, dest, src);
  709. if (is_or && insn.imm16() == 0xffff)
  710. result.set_initialized();
  711. if (update_dest)
  712. insn.modrm().write16(*this, insn, result);
  713. }
  714. template<bool update_dest, bool is_or, typename Op>
  715. ALWAYS_INLINE void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  716. {
  717. auto dest = insn.modrm().read16(*this, insn);
  718. auto src = shadow_wrap_as_initialized<u16>(sign_extended_to<u16>(insn.imm8()));
  719. auto result = op(*this, dest, src);
  720. if (is_or && src.value() == 0xffff)
  721. result.set_initialized();
  722. if (update_dest)
  723. insn.modrm().write16(*this, insn, result);
  724. }
  725. template<bool update_dest, typename Op>
  726. ALWAYS_INLINE void SoftCPU::generic_RM16_unsigned_imm8(Op op, const X86::Instruction& insn)
  727. {
  728. auto dest = insn.modrm().read16(*this, insn);
  729. auto src = shadow_wrap_as_initialized(insn.imm8());
  730. auto result = op(*this, dest, src);
  731. if (update_dest)
  732. insn.modrm().write16(*this, insn, result);
  733. }
  734. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  735. ALWAYS_INLINE void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  736. {
  737. auto dest = insn.modrm().read16(*this, insn);
  738. auto src = const_gpr16(insn.reg16());
  739. auto result = op(*this, dest, src);
  740. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  741. result.set_initialized();
  742. m_flags_tainted = false;
  743. }
  744. if (update_dest)
  745. insn.modrm().write16(*this, insn, result);
  746. }
  747. template<bool update_dest, bool is_or, typename Op>
  748. ALWAYS_INLINE void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  749. {
  750. auto dest = insn.modrm().read32(*this, insn);
  751. auto src = insn.imm32();
  752. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  753. if (is_or && src == 0xffffffff)
  754. result.set_initialized();
  755. if (update_dest)
  756. insn.modrm().write32(*this, insn, result);
  757. }
  758. template<bool update_dest, bool is_or, typename Op>
  759. ALWAYS_INLINE void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  760. {
  761. auto dest = insn.modrm().read32(*this, insn);
  762. auto src = sign_extended_to<u32>(insn.imm8());
  763. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  764. if (is_or && src == 0xffffffff)
  765. result.set_initialized();
  766. if (update_dest)
  767. insn.modrm().write32(*this, insn, result);
  768. }
  769. template<bool update_dest, typename Op>
  770. ALWAYS_INLINE void SoftCPU::generic_RM32_unsigned_imm8(Op op, const X86::Instruction& insn)
  771. {
  772. auto dest = insn.modrm().read32(*this, insn);
  773. auto src = shadow_wrap_as_initialized(insn.imm8());
  774. auto result = op(*this, dest, src);
  775. if (update_dest)
  776. insn.modrm().write32(*this, insn, result);
  777. }
  778. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  779. ALWAYS_INLINE void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  780. {
  781. auto dest = insn.modrm().read32(*this, insn);
  782. auto src = const_gpr32(insn.reg32());
  783. auto result = op(*this, dest, src);
  784. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  785. result.set_initialized();
  786. m_flags_tainted = false;
  787. }
  788. if (update_dest)
  789. insn.modrm().write32(*this, insn, result);
  790. }
  791. template<bool update_dest, bool is_or, typename Op>
  792. ALWAYS_INLINE void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  793. {
  794. auto dest = insn.modrm().read8(*this, insn);
  795. auto src = insn.imm8();
  796. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  797. if (is_or && src == 0xff)
  798. result.set_initialized();
  799. if (update_dest)
  800. insn.modrm().write8(*this, insn, result);
  801. }
  802. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  803. ALWAYS_INLINE void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  804. {
  805. auto dest = insn.modrm().read8(*this, insn);
  806. auto src = const_gpr8(insn.reg8());
  807. auto result = op(*this, dest, src);
  808. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  809. result.set_initialized();
  810. m_flags_tainted = false;
  811. }
  812. if (update_dest)
  813. insn.modrm().write8(*this, insn, result);
  814. }
  815. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  816. ALWAYS_INLINE void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  817. {
  818. auto dest = const_gpr16(insn.reg16());
  819. auto src = insn.modrm().read16(*this, insn);
  820. auto result = op(*this, dest, src);
  821. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  822. result.set_initialized();
  823. m_flags_tainted = false;
  824. }
  825. if (update_dest)
  826. gpr16(insn.reg16()) = result;
  827. }
  828. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  829. ALWAYS_INLINE void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  830. {
  831. auto dest = const_gpr32(insn.reg32());
  832. auto src = insn.modrm().read32(*this, insn);
  833. auto result = op(*this, dest, src);
  834. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  835. result.set_initialized();
  836. m_flags_tainted = false;
  837. }
  838. if (update_dest)
  839. gpr32(insn.reg32()) = result;
  840. }
  841. template<bool update_dest, bool dont_taint_for_same_operand, typename Op>
  842. ALWAYS_INLINE void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  843. {
  844. auto dest = const_gpr8(insn.reg8());
  845. auto src = insn.modrm().read8(*this, insn);
  846. auto result = op(*this, dest, src);
  847. if (dont_taint_for_same_operand && insn.modrm().is_register() && insn.modrm().register_index() == insn.register_index()) {
  848. result.set_initialized();
  849. m_flags_tainted = false;
  850. }
  851. if (update_dest)
  852. gpr8(insn.reg8()) = result;
  853. }
  854. template<typename Op>
  855. ALWAYS_INLINE void SoftCPU::generic_RM8_1(Op op, const X86::Instruction& insn)
  856. {
  857. auto data = insn.modrm().read8(*this, insn);
  858. insn.modrm().write8(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  859. }
  860. template<typename Op>
  861. ALWAYS_INLINE void SoftCPU::generic_RM8_CL(Op op, const X86::Instruction& insn)
  862. {
  863. auto data = insn.modrm().read8(*this, insn);
  864. insn.modrm().write8(*this, insn, op(*this, data, cl()));
  865. }
  866. template<typename Op>
  867. ALWAYS_INLINE void SoftCPU::generic_RM16_1(Op op, const X86::Instruction& insn)
  868. {
  869. auto data = insn.modrm().read16(*this, insn);
  870. insn.modrm().write16(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  871. }
  872. template<typename Op>
  873. ALWAYS_INLINE void SoftCPU::generic_RM16_CL(Op op, const X86::Instruction& insn)
  874. {
  875. auto data = insn.modrm().read16(*this, insn);
  876. insn.modrm().write16(*this, insn, op(*this, data, cl()));
  877. }
  878. template<typename Op>
  879. ALWAYS_INLINE void SoftCPU::generic_RM32_1(Op op, const X86::Instruction& insn)
  880. {
  881. auto data = insn.modrm().read32(*this, insn);
  882. insn.modrm().write32(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  883. }
  884. template<typename Op>
  885. ALWAYS_INLINE void SoftCPU::generic_RM32_CL(Op op, const X86::Instruction& insn)
  886. {
  887. auto data = insn.modrm().read32(*this, insn);
  888. insn.modrm().write32(*this, insn, op(*this, data, cl()));
  889. }
  890. void SoftCPU::AAA(const X86::Instruction&) { TODO_INSN(); }
  891. void SoftCPU::AAD(const X86::Instruction&) { TODO_INSN(); }
  892. void SoftCPU::AAM(const X86::Instruction&) { TODO_INSN(); }
  893. void SoftCPU::AAS(const X86::Instruction&) { TODO_INSN(); }
  894. void SoftCPU::ARPL(const X86::Instruction&) { TODO_INSN(); }
  895. void SoftCPU::BOUND(const X86::Instruction&) { TODO_INSN(); }
  896. template<typename T>
  897. ALWAYS_INLINE static T op_bsf(SoftCPU&, T value)
  898. {
  899. return { (typename T::ValueType)bit_scan_forward(value.value()), value.shadow() };
  900. }
  901. template<typename T>
  902. ALWAYS_INLINE static T op_bsr(SoftCPU&, T value)
  903. {
  904. typename T::ValueType bit_index = 0;
  905. if constexpr (sizeof(typename T::ValueType) == 4) {
  906. asm volatile("bsrl %%eax, %%edx"
  907. : "=d"(bit_index)
  908. : "a"(value.value()));
  909. }
  910. if constexpr (sizeof(typename T::ValueType) == 2) {
  911. asm volatile("bsrw %%ax, %%dx"
  912. : "=d"(bit_index)
  913. : "a"(value.value()));
  914. }
  915. return shadow_wrap_with_taint_from(bit_index, value);
  916. }
  917. void SoftCPU::BSF_reg16_RM16(const X86::Instruction& insn)
  918. {
  919. auto src = insn.modrm().read16(*this, insn);
  920. set_zf(!src.value());
  921. if (src.value())
  922. gpr16(insn.reg16()) = op_bsf(*this, src);
  923. taint_flags_from(src);
  924. }
  925. void SoftCPU::BSF_reg32_RM32(const X86::Instruction& insn)
  926. {
  927. auto src = insn.modrm().read32(*this, insn);
  928. set_zf(!src.value());
  929. if (src.value()) {
  930. gpr32(insn.reg32()) = op_bsf(*this, src);
  931. taint_flags_from(src);
  932. }
  933. }
  934. void SoftCPU::BSR_reg16_RM16(const X86::Instruction& insn)
  935. {
  936. auto src = insn.modrm().read16(*this, insn);
  937. set_zf(!src.value());
  938. if (src.value()) {
  939. gpr16(insn.reg16()) = op_bsr(*this, src);
  940. taint_flags_from(src);
  941. }
  942. }
  943. void SoftCPU::BSR_reg32_RM32(const X86::Instruction& insn)
  944. {
  945. auto src = insn.modrm().read32(*this, insn);
  946. set_zf(!src.value());
  947. if (src.value()) {
  948. gpr32(insn.reg32()) = op_bsr(*this, src);
  949. taint_flags_from(src);
  950. }
  951. }
  952. void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
  953. {
  954. gpr32(insn.reg32()) = { __builtin_bswap32(gpr32(insn.reg32()).value()), __builtin_bswap32(gpr32(insn.reg32()).shadow_as_value()) };
  955. }
  956. template<typename T>
  957. ALWAYS_INLINE static T op_bt(T value, T)
  958. {
  959. return value;
  960. }
  961. template<typename T>
  962. ALWAYS_INLINE static T op_bts(T value, T bit_mask)
  963. {
  964. return value | bit_mask;
  965. }
  966. template<typename T>
  967. ALWAYS_INLINE static T op_btr(T value, T bit_mask)
  968. {
  969. return value & ~bit_mask;
  970. }
  971. template<typename T>
  972. ALWAYS_INLINE static T op_btc(T value, T bit_mask)
  973. {
  974. return value ^ bit_mask;
  975. }
  976. template<bool should_update, typename Op>
  977. ALWAYS_INLINE void BTx_RM16_reg16(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  978. {
  979. if (insn.modrm().is_register()) {
  980. unsigned bit_index = cpu.const_gpr16(insn.reg16()).value() & (X86::TypeTrivia<u16>::bits - 1);
  981. auto original = insn.modrm().read16(cpu, insn);
  982. u16 bit_mask = 1 << bit_index;
  983. u16 result = op(original.value(), bit_mask);
  984. cpu.set_cf((original.value() & bit_mask) != 0);
  985. cpu.taint_flags_from(cpu.gpr16(insn.reg16()), original);
  986. if (should_update)
  987. insn.modrm().write16(cpu, insn, shadow_wrap_with_taint_from(result, cpu.gpr16(insn.reg16()), original));
  988. return;
  989. }
  990. // FIXME: Is this supposed to perform a full 16-bit read/modify/write?
  991. unsigned bit_offset_in_array = cpu.const_gpr16(insn.reg16()).value() / 8;
  992. unsigned bit_offset_in_byte = cpu.const_gpr16(insn.reg16()).value() & 7;
  993. auto address = insn.modrm().resolve(cpu, insn);
  994. address.set_offset(address.offset() + bit_offset_in_array);
  995. auto dest = cpu.read_memory8(address);
  996. u8 bit_mask = 1 << bit_offset_in_byte;
  997. u8 result = op(dest.value(), bit_mask);
  998. cpu.set_cf((dest.value() & bit_mask) != 0);
  999. cpu.taint_flags_from(cpu.gpr16(insn.reg16()), dest);
  1000. if (should_update)
  1001. cpu.write_memory8(address, shadow_wrap_with_taint_from(result, cpu.gpr16(insn.reg16()), dest));
  1002. }
  1003. template<bool should_update, typename Op>
  1004. ALWAYS_INLINE void BTx_RM32_reg32(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  1005. {
  1006. if (insn.modrm().is_register()) {
  1007. unsigned bit_index = cpu.const_gpr32(insn.reg32()).value() & (X86::TypeTrivia<u32>::bits - 1);
  1008. auto original = insn.modrm().read32(cpu, insn);
  1009. u32 bit_mask = 1 << bit_index;
  1010. u32 result = op(original.value(), bit_mask);
  1011. cpu.set_cf((original.value() & bit_mask) != 0);
  1012. cpu.taint_flags_from(cpu.gpr32(insn.reg32()), original);
  1013. if (should_update)
  1014. insn.modrm().write32(cpu, insn, shadow_wrap_with_taint_from(result, cpu.gpr32(insn.reg32()), original));
  1015. return;
  1016. }
  1017. // FIXME: Is this supposed to perform a full 32-bit read/modify/write?
  1018. unsigned bit_offset_in_array = cpu.const_gpr32(insn.reg32()).value() / 8;
  1019. unsigned bit_offset_in_byte = cpu.const_gpr32(insn.reg32()).value() & 7;
  1020. auto address = insn.modrm().resolve(cpu, insn);
  1021. address.set_offset(address.offset() + bit_offset_in_array);
  1022. auto dest = cpu.read_memory8(address);
  1023. u8 bit_mask = 1 << bit_offset_in_byte;
  1024. u8 result = op(dest.value(), bit_mask);
  1025. cpu.set_cf((dest.value() & bit_mask) != 0);
  1026. cpu.taint_flags_from(cpu.gpr32(insn.reg32()), dest);
  1027. if (should_update)
  1028. cpu.write_memory8(address, shadow_wrap_with_taint_from(result, cpu.gpr32(insn.reg32()), dest));
  1029. }
  1030. template<bool should_update, typename Op>
  1031. ALWAYS_INLINE void BTx_RM16_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  1032. {
  1033. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u16>::mask);
  1034. // FIXME: Support higher bit indices
  1035. VERIFY(bit_index < 16);
  1036. auto original = insn.modrm().read16(cpu, insn);
  1037. u16 bit_mask = 1 << bit_index;
  1038. auto result = op(original.value(), bit_mask);
  1039. cpu.set_cf((original.value() & bit_mask) != 0);
  1040. cpu.taint_flags_from(original);
  1041. if (should_update)
  1042. insn.modrm().write16(cpu, insn, shadow_wrap_with_taint_from(result, original));
  1043. }
  1044. template<bool should_update, typename Op>
  1045. ALWAYS_INLINE void BTx_RM32_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  1046. {
  1047. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u32>::mask);
  1048. // FIXME: Support higher bit indices
  1049. VERIFY(bit_index < 32);
  1050. auto original = insn.modrm().read32(cpu, insn);
  1051. u32 bit_mask = 1 << bit_index;
  1052. auto result = op(original.value(), bit_mask);
  1053. cpu.set_cf((original.value() & bit_mask) != 0);
  1054. cpu.taint_flags_from(original);
  1055. if (should_update)
  1056. insn.modrm().write32(cpu, insn, shadow_wrap_with_taint_from(result, original));
  1057. }
  1058. #define DEFINE_GENERIC_BTx_INSN_HANDLERS(mnemonic, op, update_dest) \
  1059. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) \
  1060. { \
  1061. BTx_RM32_reg32<update_dest>(*this, insn, op<u32>); \
  1062. } \
  1063. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) \
  1064. { \
  1065. BTx_RM16_reg16<update_dest>(*this, insn, op<u16>); \
  1066. } \
  1067. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) \
  1068. { \
  1069. BTx_RM32_imm8<update_dest>(*this, insn, op<u32>); \
  1070. } \
  1071. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) \
  1072. { \
  1073. BTx_RM16_imm8<update_dest>(*this, insn, op<u16>); \
  1074. }
  1075. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTS, op_bts, true);
  1076. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTR, op_btr, true);
  1077. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTC, op_btc, true);
  1078. DEFINE_GENERIC_BTx_INSN_HANDLERS(BT, op_bt, false);
  1079. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&)
  1080. {
  1081. TODO();
  1082. }
  1083. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO_INSN(); }
  1084. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO_INSN(); }
  1085. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  1086. {
  1087. auto address = insn.modrm().read32(*this, insn);
  1088. push32(shadow_wrap_as_initialized(eip()));
  1089. warn_if_uninitialized(address, "call rm32");
  1090. set_eip(address.value());
  1091. // FIXME: this won't catch at the moment due to us not having a way to set
  1092. // the watch point
  1093. m_emulator.call_callback(address.value());
  1094. }
  1095. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO_INSN(); }
  1096. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO_INSN(); }
  1097. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO_INSN(); }
  1098. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  1099. {
  1100. push32(shadow_wrap_as_initialized(eip()));
  1101. set_eip(eip() + (i32)insn.imm32());
  1102. // FIXME: this won't catch at the moment due to us not having a way to set
  1103. // the watch point
  1104. m_emulator.call_callback(eip() + (i32)insn.imm32());
  1105. }
  1106. void SoftCPU::CBW(const X86::Instruction&)
  1107. {
  1108. set_ah(shadow_wrap_with_taint_from<u8>((al().value() & 0x80) ? 0xff : 0x00, al()));
  1109. }
  1110. void SoftCPU::CDQ(const X86::Instruction&)
  1111. {
  1112. if (eax().value() & 0x80000000)
  1113. set_edx(shadow_wrap_with_taint_from<u32>(0xffffffff, eax()));
  1114. else
  1115. set_edx(shadow_wrap_with_taint_from<u32>(0, eax()));
  1116. }
  1117. void SoftCPU::CLC(const X86::Instruction&)
  1118. {
  1119. set_cf(false);
  1120. }
  1121. void SoftCPU::CLD(const X86::Instruction&)
  1122. {
  1123. set_df(false);
  1124. }
  1125. void SoftCPU::CLI(const X86::Instruction&) { TODO_INSN(); }
  1126. void SoftCPU::CLTS(const X86::Instruction&) { TODO_INSN(); }
  1127. void SoftCPU::CMC(const X86::Instruction&)
  1128. {
  1129. set_cf(!cf());
  1130. }
  1131. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  1132. {
  1133. warn_if_flags_tainted("cmovcc reg16, rm16");
  1134. if (evaluate_condition(insn.cc()))
  1135. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1136. }
  1137. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  1138. {
  1139. warn_if_flags_tainted("cmovcc reg32, rm32");
  1140. if (evaluate_condition(insn.cc()))
  1141. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1142. }
  1143. template<typename T>
  1144. ALWAYS_INLINE static void do_cmps(SoftCPU& cpu, const X86::Instruction& insn)
  1145. {
  1146. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1147. cpu.do_once_or_repeat<true>(insn, [&] {
  1148. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.address_size()).value() });
  1149. auto dest = cpu.read_memory<T>({ cpu.es(), cpu.destination_index(insn.address_size()).value() });
  1150. op_sub(cpu, dest, src);
  1151. cpu.step_source_index(insn.address_size(), sizeof(T));
  1152. cpu.step_destination_index(insn.address_size(), sizeof(T));
  1153. });
  1154. }
  1155. void SoftCPU::CMPSB(const X86::Instruction& insn)
  1156. {
  1157. do_cmps<u8>(*this, insn);
  1158. }
  1159. void SoftCPU::CMPSD(const X86::Instruction& insn)
  1160. {
  1161. do_cmps<u32>(*this, insn);
  1162. }
  1163. void SoftCPU::CMPSW(const X86::Instruction& insn)
  1164. {
  1165. do_cmps<u16>(*this, insn);
  1166. }
  1167. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  1168. {
  1169. auto current = insn.modrm().read16(*this, insn);
  1170. taint_flags_from(current, ax());
  1171. if (current.value() == ax().value()) {
  1172. set_zf(true);
  1173. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  1174. } else {
  1175. set_zf(false);
  1176. set_ax(current);
  1177. }
  1178. }
  1179. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  1180. {
  1181. auto current = insn.modrm().read32(*this, insn);
  1182. taint_flags_from(current, eax());
  1183. if (current.value() == eax().value()) {
  1184. set_zf(true);
  1185. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  1186. } else {
  1187. set_zf(false);
  1188. set_eax(current);
  1189. }
  1190. }
  1191. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  1192. {
  1193. auto current = insn.modrm().read8(*this, insn);
  1194. taint_flags_from(current, al());
  1195. if (current.value() == al().value()) {
  1196. set_zf(true);
  1197. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  1198. } else {
  1199. set_zf(false);
  1200. set_al(current);
  1201. }
  1202. }
  1203. void SoftCPU::CPUID(const X86::Instruction&)
  1204. {
  1205. if (eax().value() == 0) {
  1206. set_eax(shadow_wrap_as_initialized<u32>(1));
  1207. set_ebx(shadow_wrap_as_initialized<u32>(0x6c6c6548));
  1208. set_edx(shadow_wrap_as_initialized<u32>(0x6972466f));
  1209. set_ecx(shadow_wrap_as_initialized<u32>(0x73646e65));
  1210. return;
  1211. }
  1212. if (eax().value() == 1) {
  1213. u32 stepping = 0;
  1214. u32 model = 1;
  1215. u32 family = 3;
  1216. u32 type = 0;
  1217. set_eax(shadow_wrap_as_initialized<u32>(stepping | (model << 4) | (family << 8) | (type << 12)));
  1218. set_ebx(shadow_wrap_as_initialized<u32>(0));
  1219. set_edx(shadow_wrap_as_initialized<u32>((1 << 15))); // Features (CMOV)
  1220. set_ecx(shadow_wrap_as_initialized<u32>(0));
  1221. return;
  1222. }
  1223. dbgln("Unhandled CPUID with eax={:p}", eax().value());
  1224. }
  1225. void SoftCPU::CWD(const X86::Instruction&)
  1226. {
  1227. set_dx(shadow_wrap_with_taint_from<u16>((ax().value() & 0x8000) ? 0xffff : 0x0000, ax()));
  1228. }
  1229. void SoftCPU::CWDE(const X86::Instruction&)
  1230. {
  1231. set_eax(shadow_wrap_with_taint_from(sign_extended_to<u32>(ax().value()), ax()));
  1232. }
  1233. void SoftCPU::DAA(const X86::Instruction&) { TODO_INSN(); }
  1234. void SoftCPU::DAS(const X86::Instruction&) { TODO_INSN(); }
  1235. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  1236. {
  1237. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16(*this, insn)));
  1238. }
  1239. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  1240. {
  1241. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32(*this, insn)));
  1242. }
  1243. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  1244. {
  1245. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8(*this, insn)));
  1246. }
  1247. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  1248. {
  1249. gpr16(insn.reg16()) = op_dec(*this, const_gpr16(insn.reg16()));
  1250. }
  1251. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  1252. {
  1253. gpr32(insn.reg32()) = op_dec(*this, const_gpr32(insn.reg32()));
  1254. }
  1255. void SoftCPU::DIV_RM16(const X86::Instruction& insn)
  1256. {
  1257. auto divisor = insn.modrm().read16(*this, insn);
  1258. if (divisor.value() == 0) {
  1259. reportln("Divide by zero"sv);
  1260. TODO();
  1261. }
  1262. u32 dividend = ((u32)dx().value() << 16) | ax().value();
  1263. auto quotient = dividend / divisor.value();
  1264. if (quotient > NumericLimits<u16>::max()) {
  1265. reportln("Divide overflow"sv);
  1266. TODO();
  1267. }
  1268. auto remainder = dividend % divisor.value();
  1269. auto original_ax = ax();
  1270. set_ax(shadow_wrap_with_taint_from<u16>(quotient, original_ax, dx()));
  1271. set_dx(shadow_wrap_with_taint_from<u16>(remainder, original_ax, dx()));
  1272. }
  1273. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  1274. {
  1275. auto divisor = insn.modrm().read32(*this, insn);
  1276. if (divisor.value() == 0) {
  1277. reportln("Divide by zero"sv);
  1278. TODO();
  1279. }
  1280. u64 dividend = ((u64)edx().value() << 32) | eax().value();
  1281. auto quotient = dividend / divisor.value();
  1282. if (quotient > NumericLimits<u32>::max()) {
  1283. reportln("Divide overflow"sv);
  1284. TODO();
  1285. }
  1286. auto remainder = dividend % divisor.value();
  1287. auto original_eax = eax();
  1288. set_eax(shadow_wrap_with_taint_from<u32>(quotient, original_eax, edx(), divisor));
  1289. set_edx(shadow_wrap_with_taint_from<u32>(remainder, original_eax, edx(), divisor));
  1290. }
  1291. void SoftCPU::DIV_RM8(const X86::Instruction& insn)
  1292. {
  1293. auto divisor = insn.modrm().read8(*this, insn);
  1294. if (divisor.value() == 0) {
  1295. reportln("Divide by zero"sv);
  1296. TODO();
  1297. }
  1298. u16 dividend = ax().value();
  1299. auto quotient = dividend / divisor.value();
  1300. if (quotient > NumericLimits<u8>::max()) {
  1301. reportln("Divide overflow"sv);
  1302. TODO();
  1303. }
  1304. auto remainder = dividend % divisor.value();
  1305. auto original_ax = ax();
  1306. set_al(shadow_wrap_with_taint_from<u8>(quotient, original_ax, divisor));
  1307. set_ah(shadow_wrap_with_taint_from<u8>(remainder, original_ax, divisor));
  1308. }
  1309. void SoftCPU::ENTER16(const X86::Instruction&) { TODO_INSN(); }
  1310. void SoftCPU::ENTER32(const X86::Instruction&) { TODO_INSN(); }
  1311. void SoftCPU::ESCAPE(const X86::Instruction&)
  1312. {
  1313. reportln("FIXME: x87 floating-point support"sv);
  1314. m_emulator.dump_backtrace();
  1315. TODO();
  1316. }
  1317. FPU_INSTRUCTION(FADD_RM32);
  1318. FPU_INSTRUCTION(FMUL_RM32);
  1319. FPU_INSTRUCTION(FCOM_RM32);
  1320. FPU_INSTRUCTION(FCOMP_RM32);
  1321. FPU_INSTRUCTION(FSUB_RM32);
  1322. FPU_INSTRUCTION(FSUBR_RM32);
  1323. FPU_INSTRUCTION(FDIV_RM32);
  1324. FPU_INSTRUCTION(FDIVR_RM32);
  1325. FPU_INSTRUCTION(FLD_RM32);
  1326. FPU_INSTRUCTION(FXCH);
  1327. FPU_INSTRUCTION(FST_RM32);
  1328. FPU_INSTRUCTION(FNOP);
  1329. FPU_INSTRUCTION(FSTP_RM32);
  1330. FPU_INSTRUCTION(FLDENV);
  1331. FPU_INSTRUCTION(FCHS);
  1332. FPU_INSTRUCTION(FABS);
  1333. FPU_INSTRUCTION(FTST);
  1334. FPU_INSTRUCTION(FXAM);
  1335. FPU_INSTRUCTION(FLDCW);
  1336. FPU_INSTRUCTION(FLD1);
  1337. FPU_INSTRUCTION(FLDL2T);
  1338. FPU_INSTRUCTION(FLDL2E);
  1339. FPU_INSTRUCTION(FLDPI);
  1340. FPU_INSTRUCTION(FLDLG2);
  1341. FPU_INSTRUCTION(FLDLN2);
  1342. FPU_INSTRUCTION(FLDZ);
  1343. FPU_INSTRUCTION(FNSTENV);
  1344. FPU_INSTRUCTION(F2XM1);
  1345. FPU_INSTRUCTION(FYL2X);
  1346. FPU_INSTRUCTION(FPTAN);
  1347. FPU_INSTRUCTION(FPATAN);
  1348. FPU_INSTRUCTION(FXTRACT);
  1349. FPU_INSTRUCTION(FPREM1);
  1350. FPU_INSTRUCTION(FDECSTP);
  1351. FPU_INSTRUCTION(FINCSTP);
  1352. FPU_INSTRUCTION(FNSTCW);
  1353. FPU_INSTRUCTION(FPREM);
  1354. FPU_INSTRUCTION(FYL2XP1);
  1355. FPU_INSTRUCTION(FSQRT);
  1356. FPU_INSTRUCTION(FSINCOS);
  1357. FPU_INSTRUCTION(FRNDINT);
  1358. FPU_INSTRUCTION(FSCALE);
  1359. FPU_INSTRUCTION(FSIN);
  1360. FPU_INSTRUCTION(FCOS);
  1361. FPU_INSTRUCTION(FIADD_RM32);
  1362. FPU_INSTRUCTION(FCMOVB);
  1363. FPU_INSTRUCTION(FIMUL_RM32);
  1364. FPU_INSTRUCTION(FCMOVE);
  1365. FPU_INSTRUCTION(FICOM_RM32);
  1366. FPU_INSTRUCTION(FCMOVBE);
  1367. FPU_INSTRUCTION(FICOMP_RM32);
  1368. FPU_INSTRUCTION(FCMOVU);
  1369. FPU_INSTRUCTION(FISUB_RM32);
  1370. FPU_INSTRUCTION(FISUBR_RM32);
  1371. FPU_INSTRUCTION(FUCOMPP);
  1372. FPU_INSTRUCTION(FIDIV_RM32);
  1373. FPU_INSTRUCTION(FIDIVR_RM32);
  1374. FPU_INSTRUCTION(FILD_RM32);
  1375. FPU_INSTRUCTION(FCMOVNB);
  1376. FPU_INSTRUCTION(FISTTP_RM32);
  1377. FPU_INSTRUCTION(FCMOVNE);
  1378. FPU_INSTRUCTION(FIST_RM32);
  1379. FPU_INSTRUCTION(FCMOVNBE);
  1380. FPU_INSTRUCTION(FISTP_RM32);
  1381. FPU_INSTRUCTION(FCMOVNU);
  1382. FPU_INSTRUCTION(FNENI);
  1383. FPU_INSTRUCTION(FNDISI);
  1384. FPU_INSTRUCTION(FNCLEX);
  1385. FPU_INSTRUCTION(FNINIT);
  1386. FPU_INSTRUCTION(FNSETPM);
  1387. FPU_INSTRUCTION(FLD_RM80);
  1388. FPU_INSTRUCTION(FUCOMI);
  1389. FPU_INSTRUCTION(FCOMI);
  1390. FPU_INSTRUCTION(FSTP_RM80);
  1391. FPU_INSTRUCTION(FADD_RM64);
  1392. FPU_INSTRUCTION(FMUL_RM64);
  1393. FPU_INSTRUCTION(FCOM_RM64);
  1394. FPU_INSTRUCTION(FCOMP_RM64);
  1395. FPU_INSTRUCTION(FSUB_RM64);
  1396. FPU_INSTRUCTION(FSUBR_RM64);
  1397. FPU_INSTRUCTION(FDIV_RM64);
  1398. FPU_INSTRUCTION(FDIVR_RM64);
  1399. FPU_INSTRUCTION(FLD_RM64);
  1400. FPU_INSTRUCTION(FFREE);
  1401. FPU_INSTRUCTION(FISTTP_RM64);
  1402. FPU_INSTRUCTION(FST_RM64);
  1403. FPU_INSTRUCTION(FSTP_RM64);
  1404. FPU_INSTRUCTION(FRSTOR);
  1405. FPU_INSTRUCTION(FUCOM);
  1406. FPU_INSTRUCTION(FUCOMP);
  1407. FPU_INSTRUCTION(FNSAVE);
  1408. FPU_INSTRUCTION(FNSTSW);
  1409. FPU_INSTRUCTION(FIADD_RM16);
  1410. FPU_INSTRUCTION(FADDP);
  1411. FPU_INSTRUCTION(FIMUL_RM16);
  1412. FPU_INSTRUCTION(FMULP);
  1413. FPU_INSTRUCTION(FICOM_RM16);
  1414. FPU_INSTRUCTION(FICOMP_RM16);
  1415. FPU_INSTRUCTION(FCOMPP);
  1416. FPU_INSTRUCTION(FISUB_RM16);
  1417. FPU_INSTRUCTION(FSUBRP);
  1418. FPU_INSTRUCTION(FISUBR_RM16);
  1419. FPU_INSTRUCTION(FSUBP);
  1420. FPU_INSTRUCTION(FIDIV_RM16);
  1421. FPU_INSTRUCTION(FDIVRP);
  1422. FPU_INSTRUCTION(FIDIVR_RM16);
  1423. FPU_INSTRUCTION(FDIVP);
  1424. FPU_INSTRUCTION(FILD_RM16);
  1425. FPU_INSTRUCTION(FFREEP);
  1426. FPU_INSTRUCTION(FISTTP_RM16);
  1427. FPU_INSTRUCTION(FIST_RM16);
  1428. FPU_INSTRUCTION(FISTP_RM16);
  1429. FPU_INSTRUCTION(FBLD_M80);
  1430. FPU_INSTRUCTION(FNSTSW_AX);
  1431. FPU_INSTRUCTION(FILD_RM64);
  1432. FPU_INSTRUCTION(FUCOMIP);
  1433. FPU_INSTRUCTION(FBSTP_M80);
  1434. FPU_INSTRUCTION(FCOMIP);
  1435. FPU_INSTRUCTION(FISTP_RM64);
  1436. void SoftCPU::HLT(const X86::Instruction&) { TODO_INSN(); }
  1437. void SoftCPU::IDIV_RM16(const X86::Instruction& insn)
  1438. {
  1439. auto divisor_with_shadow = insn.modrm().read16(*this, insn);
  1440. auto divisor = (i16)divisor_with_shadow.value();
  1441. if (divisor == 0) {
  1442. reportln("Divide by zero"sv);
  1443. TODO();
  1444. }
  1445. i32 dividend = (i32)(((u32)dx().value() << 16) | (u32)ax().value());
  1446. i32 result = dividend / divisor;
  1447. if (result > NumericLimits<i16>::max() || result < NumericLimits<i16>::min()) {
  1448. reportln("Divide overflow"sv);
  1449. TODO();
  1450. }
  1451. auto original_ax = ax();
  1452. set_ax(shadow_wrap_with_taint_from<u16>(result, original_ax, dx(), divisor_with_shadow));
  1453. set_dx(shadow_wrap_with_taint_from<u16>(dividend % divisor, original_ax, dx(), divisor_with_shadow));
  1454. }
  1455. void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
  1456. {
  1457. auto divisor_with_shadow = insn.modrm().read32(*this, insn);
  1458. auto divisor = (i32)divisor_with_shadow.value();
  1459. if (divisor == 0) {
  1460. reportln("Divide by zero"sv);
  1461. TODO();
  1462. }
  1463. i64 dividend = (i64)(((u64)edx().value() << 32) | (u64)eax().value());
  1464. i64 result = dividend / divisor;
  1465. if (result > NumericLimits<i32>::max() || result < NumericLimits<i32>::min()) {
  1466. reportln("Divide overflow"sv);
  1467. TODO();
  1468. }
  1469. auto original_eax = eax();
  1470. set_eax(shadow_wrap_with_taint_from<u32>(result, original_eax, edx(), divisor_with_shadow));
  1471. set_edx(shadow_wrap_with_taint_from<u32>(dividend % divisor, original_eax, edx(), divisor_with_shadow));
  1472. }
  1473. void SoftCPU::IDIV_RM8(const X86::Instruction& insn)
  1474. {
  1475. auto divisor_with_shadow = insn.modrm().read8(*this, insn);
  1476. auto divisor = (i8)divisor_with_shadow.value();
  1477. if (divisor == 0) {
  1478. reportln("Divide by zero"sv);
  1479. TODO();
  1480. }
  1481. i16 dividend = ax().value();
  1482. i16 result = dividend / divisor;
  1483. if (result > NumericLimits<i8>::max() || result < NumericLimits<i8>::min()) {
  1484. reportln("Divide overflow"sv);
  1485. TODO();
  1486. }
  1487. auto original_ax = ax();
  1488. set_al(shadow_wrap_with_taint_from<u8>(result, divisor_with_shadow, original_ax));
  1489. set_ah(shadow_wrap_with_taint_from<u8>(dividend % divisor, divisor_with_shadow, original_ax));
  1490. }
  1491. void SoftCPU::IMUL_RM16(const X86::Instruction& insn)
  1492. {
  1493. i16 result_high;
  1494. i16 result_low;
  1495. auto src = insn.modrm().read16(*this, insn);
  1496. op_imul<i16>(*this, src.value(), ax().value(), result_high, result_low);
  1497. gpr16(X86::RegisterDX) = shadow_wrap_with_taint_from<u16>(result_high, src, ax());
  1498. gpr16(X86::RegisterAX) = shadow_wrap_with_taint_from<u16>(result_low, src, ax());
  1499. }
  1500. void SoftCPU::IMUL_RM32(const X86::Instruction& insn)
  1501. {
  1502. i32 result_high;
  1503. i32 result_low;
  1504. auto src = insn.modrm().read32(*this, insn);
  1505. op_imul<i32>(*this, src.value(), eax().value(), result_high, result_low);
  1506. gpr32(X86::RegisterEDX) = shadow_wrap_with_taint_from<u32>(result_high, src, eax());
  1507. gpr32(X86::RegisterEAX) = shadow_wrap_with_taint_from<u32>(result_low, src, eax());
  1508. }
  1509. void SoftCPU::IMUL_RM8(const X86::Instruction& insn)
  1510. {
  1511. i8 result_high;
  1512. i8 result_low;
  1513. auto src = insn.modrm().read8(*this, insn);
  1514. op_imul<i8>(*this, src.value(), al().value(), result_high, result_low);
  1515. gpr8(X86::RegisterAH) = shadow_wrap_with_taint_from<u8>(result_high, src, al());
  1516. gpr8(X86::RegisterAL) = shadow_wrap_with_taint_from<u8>(result_low, src, al());
  1517. }
  1518. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  1519. {
  1520. i16 result_high;
  1521. i16 result_low;
  1522. auto src = insn.modrm().read16(*this, insn);
  1523. op_imul<i16>(*this, gpr16(insn.reg16()).value(), src.value(), result_high, result_low);
  1524. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src, gpr16(insn.reg16()));
  1525. }
  1526. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  1527. {
  1528. i16 result_high;
  1529. i16 result_low;
  1530. auto src = insn.modrm().read16(*this, insn);
  1531. op_imul<i16>(*this, src.value(), insn.imm16(), result_high, result_low);
  1532. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src);
  1533. }
  1534. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  1535. {
  1536. i16 result_high;
  1537. i16 result_low;
  1538. auto src = insn.modrm().read16(*this, insn);
  1539. op_imul<i16>(*this, src.value(), sign_extended_to<i16>(insn.imm8()), result_high, result_low);
  1540. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src);
  1541. }
  1542. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  1543. {
  1544. i32 result_high;
  1545. i32 result_low;
  1546. auto src = insn.modrm().read32(*this, insn);
  1547. op_imul<i32>(*this, gpr32(insn.reg32()).value(), src.value(), result_high, result_low);
  1548. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src, gpr32(insn.reg32()));
  1549. }
  1550. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  1551. {
  1552. i32 result_high;
  1553. i32 result_low;
  1554. auto src = insn.modrm().read32(*this, insn);
  1555. op_imul<i32>(*this, src.value(), insn.imm32(), result_high, result_low);
  1556. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src);
  1557. }
  1558. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  1559. {
  1560. i32 result_high;
  1561. i32 result_low;
  1562. auto src = insn.modrm().read32(*this, insn);
  1563. op_imul<i32>(*this, src.value(), sign_extended_to<i32>(insn.imm8()), result_high, result_low);
  1564. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src);
  1565. }
  1566. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  1567. {
  1568. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  1569. }
  1570. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  1571. {
  1572. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  1573. }
  1574. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  1575. {
  1576. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  1577. }
  1578. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  1579. {
  1580. gpr16(insn.reg16()) = op_inc(*this, const_gpr16(insn.reg16()));
  1581. }
  1582. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  1583. {
  1584. gpr32(insn.reg32()) = op_inc(*this, const_gpr32(insn.reg32()));
  1585. }
  1586. void SoftCPU::INSB(const X86::Instruction&) { TODO_INSN(); }
  1587. void SoftCPU::INSD(const X86::Instruction&) { TODO_INSN(); }
  1588. void SoftCPU::INSW(const X86::Instruction&) { TODO_INSN(); }
  1589. void SoftCPU::INT1(const X86::Instruction&) { TODO_INSN(); }
  1590. void SoftCPU::INT3(const X86::Instruction&) { TODO_INSN(); }
  1591. void SoftCPU::INTO(const X86::Instruction&) { TODO_INSN(); }
  1592. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  1593. {
  1594. VERIFY(insn.imm8() == 0x82);
  1595. // FIXME: virt_syscall should take ValueWithShadow and whine about uninitialized arguments
  1596. set_eax(shadow_wrap_as_initialized(m_emulator.virt_syscall(eax().value(), edx().value(), ecx().value(), ebx().value())));
  1597. }
  1598. void SoftCPU::INVLPG(const X86::Instruction&) { TODO_INSN(); }
  1599. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO_INSN(); }
  1600. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO_INSN(); }
  1601. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO_INSN(); }
  1602. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO_INSN(); }
  1603. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO_INSN(); }
  1604. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO_INSN(); }
  1605. void SoftCPU::IRET(const X86::Instruction&) { TODO_INSN(); }
  1606. void SoftCPU::JCXZ_imm8(const X86::Instruction& insn)
  1607. {
  1608. switch (insn.address_size()) {
  1609. case X86::AddressSize::Size32:
  1610. warn_if_uninitialized(ecx(), "jecxz imm8");
  1611. if (ecx().value() == 0)
  1612. set_eip(eip() + (i8)insn.imm8());
  1613. break;
  1614. case X86::AddressSize::Size16:
  1615. warn_if_uninitialized(cx(), "jcxz imm8");
  1616. if (cx().value() == 0)
  1617. set_eip(eip() + (i8)insn.imm8());
  1618. break;
  1619. default:
  1620. VERIFY_NOT_REACHED();
  1621. }
  1622. }
  1623. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO_INSN(); }
  1624. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO_INSN(); }
  1625. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO_INSN(); }
  1626. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  1627. {
  1628. set_eip(insn.modrm().read32(*this, insn).value());
  1629. }
  1630. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  1631. {
  1632. set_eip(eip() + (i16)insn.imm16());
  1633. }
  1634. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO_INSN(); }
  1635. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO_INSN(); }
  1636. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  1637. {
  1638. set_eip(eip() + (i32)insn.imm32());
  1639. }
  1640. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  1641. {
  1642. set_eip(eip() + (i8)insn.imm8());
  1643. }
  1644. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  1645. {
  1646. warn_if_flags_tainted("jcc near imm32");
  1647. if (evaluate_condition(insn.cc()))
  1648. set_eip(eip() + (i32)insn.imm32());
  1649. }
  1650. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  1651. {
  1652. warn_if_flags_tainted("jcc imm8");
  1653. if (evaluate_condition(insn.cc()))
  1654. set_eip(eip() + (i8)insn.imm8());
  1655. }
  1656. void SoftCPU::LAHF(const X86::Instruction&) { TODO_INSN(); }
  1657. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO_INSN(); }
  1658. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO_INSN(); }
  1659. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO_INSN(); }
  1660. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO_INSN(); }
  1661. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO_INSN(); }
  1662. void SoftCPU::LEAVE32(const X86::Instruction&)
  1663. {
  1664. auto new_ebp = read_memory32({ ss(), ebp().value() });
  1665. set_esp({ ebp().value() + 4, ebp().shadow() });
  1666. set_ebp(new_ebp);
  1667. }
  1668. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  1669. {
  1670. // FIXME: Respect shadow values
  1671. gpr16(insn.reg16()) = shadow_wrap_as_initialized<u16>(insn.modrm().resolve(*this, insn).offset());
  1672. }
  1673. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  1674. {
  1675. // FIXME: Respect shadow values
  1676. gpr32(insn.reg32()) = shadow_wrap_as_initialized<u32>(insn.modrm().resolve(*this, insn).offset());
  1677. }
  1678. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO_INSN(); }
  1679. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO_INSN(); }
  1680. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO_INSN(); }
  1681. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO_INSN(); }
  1682. void SoftCPU::LGDT(const X86::Instruction&) { TODO_INSN(); }
  1683. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO_INSN(); }
  1684. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO_INSN(); }
  1685. void SoftCPU::LIDT(const X86::Instruction&) { TODO_INSN(); }
  1686. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO_INSN(); }
  1687. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO_INSN(); }
  1688. template<typename T>
  1689. ALWAYS_INLINE static void do_lods(SoftCPU& cpu, const X86::Instruction& insn)
  1690. {
  1691. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1692. cpu.do_once_or_repeat<true>(insn, [&] {
  1693. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.address_size()).value() });
  1694. cpu.gpr<T>(X86::RegisterAL) = src;
  1695. cpu.step_source_index(insn.address_size(), sizeof(T));
  1696. });
  1697. }
  1698. void SoftCPU::LODSB(const X86::Instruction& insn)
  1699. {
  1700. do_lods<u8>(*this, insn);
  1701. }
  1702. void SoftCPU::LODSD(const X86::Instruction& insn)
  1703. {
  1704. do_lods<u32>(*this, insn);
  1705. }
  1706. void SoftCPU::LODSW(const X86::Instruction& insn)
  1707. {
  1708. do_lods<u16>(*this, insn);
  1709. }
  1710. void SoftCPU::LOOPNZ_imm8(const X86::Instruction& insn)
  1711. {
  1712. warn_if_flags_tainted("loopnz");
  1713. switch (insn.address_size()) {
  1714. case X86::AddressSize::Size32:
  1715. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1716. if (ecx().value() != 0 && !zf())
  1717. set_eip(eip() + (i8)insn.imm8());
  1718. break;
  1719. case X86::AddressSize::Size16:
  1720. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1721. if (cx().value() != 0 && !zf())
  1722. set_eip(eip() + (i8)insn.imm8());
  1723. break;
  1724. default:
  1725. VERIFY_NOT_REACHED();
  1726. }
  1727. }
  1728. void SoftCPU::LOOPZ_imm8(const X86::Instruction& insn)
  1729. {
  1730. warn_if_flags_tainted("loopz");
  1731. switch (insn.address_size()) {
  1732. case X86::AddressSize::Size32:
  1733. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1734. if (ecx().value() != 0 && zf())
  1735. set_eip(eip() + (i8)insn.imm8());
  1736. break;
  1737. case X86::AddressSize::Size16:
  1738. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1739. if (cx().value() != 0 && zf())
  1740. set_eip(eip() + (i8)insn.imm8());
  1741. break;
  1742. default:
  1743. VERIFY_NOT_REACHED();
  1744. }
  1745. }
  1746. void SoftCPU::LOOP_imm8(const X86::Instruction& insn)
  1747. {
  1748. switch (insn.address_size()) {
  1749. case X86::AddressSize::Size32:
  1750. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1751. if (ecx().value() != 0)
  1752. set_eip(eip() + (i8)insn.imm8());
  1753. break;
  1754. case X86::AddressSize::Size16:
  1755. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1756. if (cx().value() != 0)
  1757. set_eip(eip() + (i8)insn.imm8());
  1758. break;
  1759. default:
  1760. VERIFY_NOT_REACHED();
  1761. }
  1762. }
  1763. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO_INSN(); }
  1764. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO_INSN(); }
  1765. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO_INSN(); }
  1766. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO_INSN(); }
  1767. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO_INSN(); }
  1768. template<typename T>
  1769. ALWAYS_INLINE static void do_movs(SoftCPU& cpu, const X86::Instruction& insn)
  1770. {
  1771. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1772. cpu.do_once_or_repeat<false>(insn, [&] {
  1773. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.address_size()).value() });
  1774. cpu.write_memory<T>({ cpu.es(), cpu.destination_index(insn.address_size()).value() }, src);
  1775. cpu.step_source_index(insn.address_size(), sizeof(T));
  1776. cpu.step_destination_index(insn.address_size(), sizeof(T));
  1777. });
  1778. }
  1779. void SoftCPU::MOVSB(const X86::Instruction& insn)
  1780. {
  1781. do_movs<u8>(*this, insn);
  1782. }
  1783. void SoftCPU::MOVSD(const X86::Instruction& insn)
  1784. {
  1785. do_movs<u32>(*this, insn);
  1786. }
  1787. void SoftCPU::MOVSW(const X86::Instruction& insn)
  1788. {
  1789. do_movs<u16>(*this, insn);
  1790. }
  1791. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1792. {
  1793. auto src = insn.modrm().read8(*this, insn);
  1794. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(sign_extended_to<u16>(src.value()), src);
  1795. }
  1796. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1797. {
  1798. auto src = insn.modrm().read16(*this, insn);
  1799. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(sign_extended_to<u32>(src.value()), src);
  1800. }
  1801. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1802. {
  1803. auto src = insn.modrm().read8(*this, insn);
  1804. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(sign_extended_to<u32>(src.value()), src);
  1805. }
  1806. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1807. {
  1808. auto src = insn.modrm().read8(*this, insn);
  1809. gpr16(insn.reg16()) = ValueWithShadow<u16>(src.value(), 0x0100 | (src.shadow_as_value() & 0xff));
  1810. }
  1811. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1812. {
  1813. auto src = insn.modrm().read16(*this, insn);
  1814. gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010000 | (src.shadow_as_value() & 0xffff));
  1815. }
  1816. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1817. {
  1818. auto src = insn.modrm().read8(*this, insn);
  1819. gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010100 | (src.shadow_as_value() & 0xff));
  1820. }
  1821. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1822. {
  1823. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1824. }
  1825. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1826. {
  1827. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1828. }
  1829. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO_INSN(); }
  1830. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO_INSN(); }
  1831. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1832. {
  1833. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1834. }
  1835. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1836. {
  1837. insn.modrm().write16(*this, insn, shadow_wrap_as_initialized(insn.imm16()));
  1838. }
  1839. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1840. {
  1841. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  1842. }
  1843. void SoftCPU::MOV_RM16_seg(X86::Instruction const& insn)
  1844. {
  1845. insn.modrm().write16(*this, insn, shadow_wrap_as_initialized(m_segment[to_underlying(insn.segment_register())]));
  1846. }
  1847. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1848. {
  1849. insn.modrm().write32(*this, insn, shadow_wrap_as_initialized(insn.imm32()));
  1850. }
  1851. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1852. {
  1853. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  1854. }
  1855. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1856. {
  1857. insn.modrm().write8(*this, insn, shadow_wrap_as_initialized(insn.imm8()));
  1858. }
  1859. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1860. {
  1861. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  1862. }
  1863. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1864. {
  1865. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1866. }
  1867. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1868. {
  1869. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1870. }
  1871. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1872. {
  1873. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1874. }
  1875. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1876. {
  1877. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1878. }
  1879. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1880. {
  1881. gpr16(insn.reg16()) = shadow_wrap_as_initialized(insn.imm16());
  1882. }
  1883. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO_INSN(); }
  1884. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO_INSN(); }
  1885. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1886. {
  1887. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1888. }
  1889. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1890. {
  1891. gpr32(insn.reg32()) = shadow_wrap_as_initialized(insn.imm32());
  1892. }
  1893. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1894. {
  1895. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1896. }
  1897. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1898. {
  1899. gpr8(insn.reg8()) = shadow_wrap_as_initialized(insn.imm8());
  1900. }
  1901. void SoftCPU::write_segment_register(X86::SegmentRegister segment_register, ValueWithShadow<u16> value)
  1902. {
  1903. // FIXME: Validate the segment selector and raise exception if necessary.
  1904. // FIXME: Complain if uninitialized data is moved into a segment register.
  1905. m_segment[to_underlying(segment_register)] = value.value();
  1906. }
  1907. void SoftCPU::MOV_seg_RM16(X86::Instruction const& insn)
  1908. {
  1909. write_segment_register(insn.segment_register(), insn.modrm().read16(*this, insn));
  1910. }
  1911. void SoftCPU::MOV_seg_RM32(X86::Instruction const& insn)
  1912. {
  1913. // NOTE: This instruction performs a 32-bit read but only the bottom 16 bits are used since segment registers are 16-bit.
  1914. auto value = insn.modrm().read32(*this, insn);
  1915. write_segment_register(insn.segment_register(), ValueWithShadow<u16>(value.value(), value.shadow_as_value()));
  1916. }
  1917. void SoftCPU::MUL_RM16(const X86::Instruction& insn)
  1918. {
  1919. auto src = insn.modrm().read16(*this, insn);
  1920. u32 result = (u32)ax().value() * (u32)src.value();
  1921. auto original_ax = ax();
  1922. set_ax(shadow_wrap_with_taint_from<u16>(result & 0xffff, src, original_ax));
  1923. set_dx(shadow_wrap_with_taint_from<u16>(result >> 16, src, original_ax));
  1924. taint_flags_from(src, original_ax);
  1925. set_cf(dx().value() != 0);
  1926. set_of(dx().value() != 0);
  1927. }
  1928. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1929. {
  1930. auto src = insn.modrm().read32(*this, insn);
  1931. u64 result = (u64)eax().value() * (u64)src.value();
  1932. auto original_eax = eax();
  1933. set_eax(shadow_wrap_with_taint_from<u32>(result, src, original_eax));
  1934. set_edx(shadow_wrap_with_taint_from<u32>(result >> 32, src, original_eax));
  1935. taint_flags_from(src, original_eax);
  1936. set_cf(edx().value() != 0);
  1937. set_of(edx().value() != 0);
  1938. }
  1939. void SoftCPU::MUL_RM8(const X86::Instruction& insn)
  1940. {
  1941. auto src = insn.modrm().read8(*this, insn);
  1942. u16 result = (u16)al().value() * src.value();
  1943. auto original_al = al();
  1944. set_ax(shadow_wrap_with_taint_from(result, src, original_al));
  1945. taint_flags_from(src, original_al);
  1946. set_cf((result & 0xff00) != 0);
  1947. set_of((result & 0xff00) != 0);
  1948. }
  1949. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1950. {
  1951. insn.modrm().write16(*this, insn, op_sub<ValueWithShadow<u16>>(*this, shadow_wrap_as_initialized<u16>(0), insn.modrm().read16(*this, insn)));
  1952. }
  1953. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1954. {
  1955. insn.modrm().write32(*this, insn, op_sub<ValueWithShadow<u32>>(*this, shadow_wrap_as_initialized<u32>(0), insn.modrm().read32(*this, insn)));
  1956. }
  1957. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1958. {
  1959. insn.modrm().write8(*this, insn, op_sub<ValueWithShadow<u8>>(*this, shadow_wrap_as_initialized<u8>(0), insn.modrm().read8(*this, insn)));
  1960. }
  1961. void SoftCPU::NOP(const X86::Instruction&)
  1962. {
  1963. }
  1964. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1965. {
  1966. auto data = insn.modrm().read16(*this, insn);
  1967. insn.modrm().write16(*this, insn, ValueWithShadow<u16>(~data.value(), data.shadow()));
  1968. }
  1969. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1970. {
  1971. auto data = insn.modrm().read32(*this, insn);
  1972. insn.modrm().write32(*this, insn, ValueWithShadow<u32>(~data.value(), data.shadow()));
  1973. }
  1974. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1975. {
  1976. auto data = insn.modrm().read8(*this, insn);
  1977. insn.modrm().write8(*this, insn, ValueWithShadow<u8>(~data.value(), data.shadow()));
  1978. }
  1979. void SoftCPU::OUTSB(const X86::Instruction&) { TODO_INSN(); }
  1980. void SoftCPU::OUTSD(const X86::Instruction&) { TODO_INSN(); }
  1981. void SoftCPU::OUTSW(const X86::Instruction&) { TODO_INSN(); }
  1982. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO_INSN(); }
  1983. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO_INSN(); }
  1984. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO_INSN(); }
  1985. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO_INSN(); }
  1986. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO_INSN(); }
  1987. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO_INSN(); }
  1988. FPU_INSTRUCTION(PACKSSDW_mm1_mm2m64);
  1989. FPU_INSTRUCTION(PACKSSWB_mm1_mm2m64);
  1990. FPU_INSTRUCTION(PACKUSWB_mm1_mm2m64);
  1991. FPU_INSTRUCTION(PADDB_mm1_mm2m64);
  1992. FPU_INSTRUCTION(PADDW_mm1_mm2m64);
  1993. FPU_INSTRUCTION(PADDD_mm1_mm2m64);
  1994. FPU_INSTRUCTION(PADDSB_mm1_mm2m64);
  1995. FPU_INSTRUCTION(PADDSW_mm1_mm2m64);
  1996. FPU_INSTRUCTION(PADDUSB_mm1_mm2m64);
  1997. FPU_INSTRUCTION(PADDUSW_mm1_mm2m64);
  1998. FPU_INSTRUCTION(PAND_mm1_mm2m64);
  1999. FPU_INSTRUCTION(PANDN_mm1_mm2m64);
  2000. FPU_INSTRUCTION(PCMPEQB_mm1_mm2m64);
  2001. FPU_INSTRUCTION(PCMPEQW_mm1_mm2m64);
  2002. FPU_INSTRUCTION(PCMPEQD_mm1_mm2m64);
  2003. FPU_INSTRUCTION(PCMPGTB_mm1_mm2m64);
  2004. FPU_INSTRUCTION(PCMPGTW_mm1_mm2m64);
  2005. FPU_INSTRUCTION(PCMPGTD_mm1_mm2m64);
  2006. FPU_INSTRUCTION(PMADDWD_mm1_mm2m64);
  2007. FPU_INSTRUCTION(PMULHW_mm1_mm2m64);
  2008. FPU_INSTRUCTION(PMULLW_mm1_mm2m64);
  2009. void SoftCPU::POPA(const X86::Instruction&)
  2010. {
  2011. set_di(pop16());
  2012. set_si(pop16());
  2013. set_bp(pop16());
  2014. pop16();
  2015. set_bx(pop16());
  2016. set_dx(pop16());
  2017. set_cx(pop16());
  2018. set_ax(pop16());
  2019. }
  2020. void SoftCPU::POPAD(const X86::Instruction&)
  2021. {
  2022. set_edi(pop32());
  2023. set_esi(pop32());
  2024. set_ebp(pop32());
  2025. pop32();
  2026. set_ebx(pop32());
  2027. set_edx(pop32());
  2028. set_ecx(pop32());
  2029. set_eax(pop32());
  2030. }
  2031. void SoftCPU::POPF(const X86::Instruction&)
  2032. {
  2033. auto popped_value = pop16();
  2034. m_eflags &= ~0xffff;
  2035. m_eflags |= popped_value.value();
  2036. taint_flags_from(popped_value);
  2037. }
  2038. void SoftCPU::POPFD(const X86::Instruction&)
  2039. {
  2040. auto popped_value = pop32();
  2041. m_eflags &= ~0x00fcffff;
  2042. m_eflags |= popped_value.value() & 0x00fcffff;
  2043. taint_flags_from(popped_value);
  2044. }
  2045. void SoftCPU::POP_DS(const X86::Instruction&) { TODO_INSN(); }
  2046. void SoftCPU::POP_ES(const X86::Instruction&) { TODO_INSN(); }
  2047. void SoftCPU::POP_FS(const X86::Instruction&) { TODO_INSN(); }
  2048. void SoftCPU::POP_GS(const X86::Instruction&) { TODO_INSN(); }
  2049. void SoftCPU::POP_RM16(const X86::Instruction& insn)
  2050. {
  2051. insn.modrm().write16(*this, insn, pop16());
  2052. }
  2053. void SoftCPU::POP_RM32(const X86::Instruction& insn)
  2054. {
  2055. insn.modrm().write32(*this, insn, pop32());
  2056. }
  2057. void SoftCPU::POP_SS(const X86::Instruction&) { TODO_INSN(); }
  2058. void SoftCPU::POP_reg16(const X86::Instruction& insn)
  2059. {
  2060. gpr16(insn.reg16()) = pop16();
  2061. }
  2062. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  2063. {
  2064. gpr32(insn.reg32()) = pop32();
  2065. }
  2066. FPU_INSTRUCTION(POR_mm1_mm2m64);
  2067. FPU_INSTRUCTION(PSLLW_mm1_mm2m64);
  2068. FPU_INSTRUCTION(PSLLW_mm1_imm8);
  2069. FPU_INSTRUCTION(PSLLD_mm1_mm2m64);
  2070. FPU_INSTRUCTION(PSLLD_mm1_imm8);
  2071. FPU_INSTRUCTION(PSLLQ_mm1_mm2m64);
  2072. FPU_INSTRUCTION(PSLLQ_mm1_imm8);
  2073. FPU_INSTRUCTION(PSRAW_mm1_mm2m64);
  2074. FPU_INSTRUCTION(PSRAW_mm1_imm8);
  2075. FPU_INSTRUCTION(PSRAD_mm1_mm2m64);
  2076. FPU_INSTRUCTION(PSRAD_mm1_imm8);
  2077. FPU_INSTRUCTION(PSRLW_mm1_mm2m64);
  2078. FPU_INSTRUCTION(PSRLW_mm1_imm8);
  2079. FPU_INSTRUCTION(PSRLD_mm1_mm2m64);
  2080. FPU_INSTRUCTION(PSRLD_mm1_imm8);
  2081. FPU_INSTRUCTION(PSRLQ_mm1_mm2m64);
  2082. FPU_INSTRUCTION(PSRLQ_mm1_imm8);
  2083. FPU_INSTRUCTION(PSUBB_mm1_mm2m64);
  2084. FPU_INSTRUCTION(PSUBW_mm1_mm2m64);
  2085. FPU_INSTRUCTION(PSUBD_mm1_mm2m64);
  2086. FPU_INSTRUCTION(PSUBSB_mm1_mm2m64);
  2087. FPU_INSTRUCTION(PSUBSW_mm1_mm2m64);
  2088. FPU_INSTRUCTION(PSUBUSB_mm1_mm2m64);
  2089. FPU_INSTRUCTION(PSUBUSW_mm1_mm2m64);
  2090. FPU_INSTRUCTION(PUNPCKHBW_mm1_mm2m64);
  2091. FPU_INSTRUCTION(PUNPCKHWD_mm1_mm2m64);
  2092. FPU_INSTRUCTION(PUNPCKHDQ_mm1_mm2m64);
  2093. FPU_INSTRUCTION(PUNPCKLBW_mm1_mm2m32);
  2094. FPU_INSTRUCTION(PUNPCKLWD_mm1_mm2m32);
  2095. FPU_INSTRUCTION(PUNPCKLDQ_mm1_mm2m32);
  2096. void SoftCPU::PUSHA(const X86::Instruction&)
  2097. {
  2098. auto temp = sp();
  2099. push16(ax());
  2100. push16(cx());
  2101. push16(dx());
  2102. push16(bx());
  2103. push16(temp);
  2104. push16(bp());
  2105. push16(si());
  2106. push16(di());
  2107. }
  2108. void SoftCPU::PUSHAD(const X86::Instruction&)
  2109. {
  2110. auto temp = esp();
  2111. push32(eax());
  2112. push32(ecx());
  2113. push32(edx());
  2114. push32(ebx());
  2115. push32(temp);
  2116. push32(ebp());
  2117. push32(esi());
  2118. push32(edi());
  2119. }
  2120. void SoftCPU::PUSHF(const X86::Instruction&)
  2121. {
  2122. // FIXME: Respect shadow flags when they exist!
  2123. push16(shadow_wrap_as_initialized<u16>(m_eflags & 0xffff));
  2124. }
  2125. void SoftCPU::PUSHFD(const X86::Instruction&)
  2126. {
  2127. // FIXME: Respect shadow flags when they exist!
  2128. push32(shadow_wrap_as_initialized(m_eflags & 0x00fcffff));
  2129. }
  2130. void SoftCPU::PUSH_CS(X86::Instruction const&)
  2131. {
  2132. push16(shadow_wrap_as_initialized(cs()));
  2133. }
  2134. void SoftCPU::PUSH_DS(X86::Instruction const&)
  2135. {
  2136. push16(shadow_wrap_as_initialized(ds()));
  2137. }
  2138. void SoftCPU::PUSH_ES(X86::Instruction const&)
  2139. {
  2140. push16(shadow_wrap_as_initialized(es()));
  2141. }
  2142. void SoftCPU::PUSH_FS(X86::Instruction const&)
  2143. {
  2144. push16(shadow_wrap_as_initialized(fs()));
  2145. }
  2146. void SoftCPU::PUSH_GS(X86::Instruction const&)
  2147. {
  2148. push16(shadow_wrap_as_initialized(gs()));
  2149. }
  2150. void SoftCPU::PUSH_RM16(const X86::Instruction& insn)
  2151. {
  2152. push16(insn.modrm().read16(*this, insn));
  2153. }
  2154. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  2155. {
  2156. push32(insn.modrm().read32(*this, insn));
  2157. }
  2158. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO_INSN(); }
  2159. void SoftCPU::PUSH_SS(X86::Instruction const&)
  2160. {
  2161. push16(shadow_wrap_as_initialized(ss()));
  2162. }
  2163. void SoftCPU::PUSH_imm16(const X86::Instruction& insn)
  2164. {
  2165. push16(shadow_wrap_as_initialized(insn.imm16()));
  2166. }
  2167. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  2168. {
  2169. push32(shadow_wrap_as_initialized(insn.imm32()));
  2170. }
  2171. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  2172. {
  2173. VERIFY(!insn.has_operand_size_override_prefix());
  2174. push32(shadow_wrap_as_initialized<u32>(sign_extended_to<i32>(insn.imm8())));
  2175. }
  2176. void SoftCPU::PUSH_reg16(const X86::Instruction& insn)
  2177. {
  2178. push16(gpr16(insn.reg16()));
  2179. }
  2180. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  2181. {
  2182. push32(gpr32(insn.reg32()));
  2183. }
  2184. FPU_INSTRUCTION(PXOR_mm1_mm2m64);
  2185. template<typename T, bool cf>
  2186. ALWAYS_INLINE static T op_rcl_impl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2187. {
  2188. if (steps.value() == 0)
  2189. return shadow_wrap_with_taint_from(data.value(), data, steps);
  2190. u32 result = 0;
  2191. u32 new_flags = 0;
  2192. if constexpr (cf)
  2193. asm volatile("stc");
  2194. else
  2195. asm volatile("clc");
  2196. if constexpr (sizeof(typename T::ValueType) == 4) {
  2197. asm volatile("rcll %%cl, %%eax\n"
  2198. : "=a"(result)
  2199. : "a"(data.value()), "c"(steps.value()));
  2200. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  2201. asm volatile("rclw %%cl, %%ax\n"
  2202. : "=a"(result)
  2203. : "a"(data.value()), "c"(steps.value()));
  2204. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  2205. asm volatile("rclb %%cl, %%al\n"
  2206. : "=a"(result)
  2207. : "a"(data.value()), "c"(steps.value()));
  2208. }
  2209. asm volatile(
  2210. "pushf\n"
  2211. "pop %%ebx"
  2212. : "=b"(new_flags));
  2213. cpu.set_flags_oc(new_flags);
  2214. cpu.taint_flags_from(data, steps);
  2215. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  2216. }
  2217. template<typename T>
  2218. ALWAYS_INLINE static T op_rcl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2219. {
  2220. cpu.warn_if_flags_tainted("rcl");
  2221. if (cpu.cf())
  2222. return op_rcl_impl<T, true>(cpu, data, steps);
  2223. return op_rcl_impl<T, false>(cpu, data, steps);
  2224. }
  2225. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCL, op_rcl)
  2226. template<typename T, bool cf>
  2227. ALWAYS_INLINE static T op_rcr_impl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2228. {
  2229. if (steps.value() == 0)
  2230. return shadow_wrap_with_taint_from(data.value(), data, steps);
  2231. u32 result = 0;
  2232. u32 new_flags = 0;
  2233. if constexpr (cf)
  2234. asm volatile("stc");
  2235. else
  2236. asm volatile("clc");
  2237. if constexpr (sizeof(typename T::ValueType) == 4) {
  2238. asm volatile("rcrl %%cl, %%eax\n"
  2239. : "=a"(result)
  2240. : "a"(data.value()), "c"(steps.value()));
  2241. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  2242. asm volatile("rcrw %%cl, %%ax\n"
  2243. : "=a"(result)
  2244. : "a"(data.value()), "c"(steps.value()));
  2245. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  2246. asm volatile("rcrb %%cl, %%al\n"
  2247. : "=a"(result)
  2248. : "a"(data.value()), "c"(steps.value()));
  2249. }
  2250. asm volatile(
  2251. "pushf\n"
  2252. "pop %%ebx"
  2253. : "=b"(new_flags));
  2254. cpu.set_flags_oc(new_flags);
  2255. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  2256. }
  2257. template<typename T>
  2258. ALWAYS_INLINE static T op_rcr(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2259. {
  2260. cpu.warn_if_flags_tainted("rcr");
  2261. if (cpu.cf())
  2262. return op_rcr_impl<T, true>(cpu, data, steps);
  2263. return op_rcr_impl<T, false>(cpu, data, steps);
  2264. }
  2265. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCR, op_rcr)
  2266. void SoftCPU::RDTSC(const X86::Instruction&) { TODO_INSN(); }
  2267. void SoftCPU::RET(const X86::Instruction& insn)
  2268. {
  2269. VERIFY(!insn.has_operand_size_override_prefix());
  2270. auto ret_address = pop32();
  2271. warn_if_uninitialized(ret_address, "ret");
  2272. set_eip(ret_address.value());
  2273. m_emulator.return_callback(ret_address.value());
  2274. }
  2275. void SoftCPU::RETF(const X86::Instruction&) { TODO_INSN(); }
  2276. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO_INSN(); }
  2277. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  2278. {
  2279. VERIFY(!insn.has_operand_size_override_prefix());
  2280. auto ret_address = pop32();
  2281. warn_if_uninitialized(ret_address, "ret imm16");
  2282. set_eip(ret_address.value());
  2283. set_esp({ esp().value() + insn.imm16(), esp().shadow() });
  2284. m_emulator.return_callback(ret_address.value());
  2285. }
  2286. template<typename T>
  2287. ALWAYS_INLINE static T op_rol(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2288. {
  2289. if (steps.value() == 0)
  2290. return shadow_wrap_with_taint_from(data.value(), data, steps);
  2291. u32 result = 0;
  2292. u32 new_flags = 0;
  2293. if constexpr (sizeof(typename T::ValueType) == 4) {
  2294. asm volatile("roll %%cl, %%eax\n"
  2295. : "=a"(result)
  2296. : "a"(data.value()), "c"(steps.value()));
  2297. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  2298. asm volatile("rolw %%cl, %%ax\n"
  2299. : "=a"(result)
  2300. : "a"(data.value()), "c"(steps.value()));
  2301. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  2302. asm volatile("rolb %%cl, %%al\n"
  2303. : "=a"(result)
  2304. : "a"(data.value()), "c"(steps.value()));
  2305. }
  2306. asm volatile(
  2307. "pushf\n"
  2308. "pop %%ebx"
  2309. : "=b"(new_flags));
  2310. cpu.set_flags_oc(new_flags);
  2311. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  2312. }
  2313. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROL, op_rol)
  2314. template<typename T>
  2315. ALWAYS_INLINE static T op_ror(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2316. {
  2317. if (steps.value() == 0)
  2318. return shadow_wrap_with_taint_from(data.value(), data, steps);
  2319. u32 result = 0;
  2320. u32 new_flags = 0;
  2321. if constexpr (sizeof(typename T::ValueType) == 4) {
  2322. asm volatile("rorl %%cl, %%eax\n"
  2323. : "=a"(result)
  2324. : "a"(data.value()), "c"(steps.value()));
  2325. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  2326. asm volatile("rorw %%cl, %%ax\n"
  2327. : "=a"(result)
  2328. : "a"(data.value()), "c"(steps.value()));
  2329. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  2330. asm volatile("rorb %%cl, %%al\n"
  2331. : "=a"(result)
  2332. : "a"(data.value()), "c"(steps.value()));
  2333. }
  2334. asm volatile(
  2335. "pushf\n"
  2336. "pop %%ebx"
  2337. : "=b"(new_flags));
  2338. cpu.set_flags_oc(new_flags);
  2339. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  2340. }
  2341. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROR, op_ror)
  2342. void SoftCPU::SAHF(const X86::Instruction&)
  2343. {
  2344. // FIXME: Respect shadow flags once they exists!
  2345. set_al(shadow_wrap_as_initialized<u8>(eflags() & 0xff));
  2346. }
  2347. void SoftCPU::SALC(const X86::Instruction&)
  2348. {
  2349. // FIXME: Respect shadow flags once they exists!
  2350. set_al(shadow_wrap_as_initialized<u8>(cf() ? 0xff : 0x00));
  2351. }
  2352. template<typename T>
  2353. static T op_sar(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  2354. {
  2355. if (steps.value() == 0)
  2356. return shadow_wrap_with_taint_from(data.value(), data, steps);
  2357. u32 result = 0;
  2358. u32 new_flags = 0;
  2359. if constexpr (sizeof(typename T::ValueType) == 4) {
  2360. asm volatile("sarl %%cl, %%eax\n"
  2361. : "=a"(result)
  2362. : "a"(data.value()), "c"(steps.value()));
  2363. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  2364. asm volatile("sarw %%cl, %%ax\n"
  2365. : "=a"(result)
  2366. : "a"(data.value()), "c"(steps.value()));
  2367. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  2368. asm volatile("sarb %%cl, %%al\n"
  2369. : "=a"(result)
  2370. : "a"(data.value()), "c"(steps.value()));
  2371. }
  2372. asm volatile(
  2373. "pushf\n"
  2374. "pop %%ebx"
  2375. : "=b"(new_flags));
  2376. cpu.set_flags_oszapc(new_flags);
  2377. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  2378. }
  2379. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SAR, op_sar)
  2380. template<typename T>
  2381. ALWAYS_INLINE static void do_scas(SoftCPU& cpu, const X86::Instruction& insn)
  2382. {
  2383. cpu.do_once_or_repeat<true>(insn, [&] {
  2384. auto src = cpu.const_gpr<T>(X86::RegisterAL);
  2385. auto dest = cpu.read_memory<T>({ cpu.es(), cpu.destination_index(insn.address_size()).value() });
  2386. op_sub(cpu, dest, src);
  2387. cpu.step_destination_index(insn.address_size(), sizeof(T));
  2388. });
  2389. }
  2390. void SoftCPU::SCASB(const X86::Instruction& insn)
  2391. {
  2392. do_scas<u8>(*this, insn);
  2393. }
  2394. void SoftCPU::SCASD(const X86::Instruction& insn)
  2395. {
  2396. do_scas<u32>(*this, insn);
  2397. }
  2398. void SoftCPU::SCASW(const X86::Instruction& insn)
  2399. {
  2400. do_scas<u16>(*this, insn);
  2401. }
  2402. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  2403. {
  2404. warn_if_flags_tainted("setcc");
  2405. insn.modrm().write8(*this, insn, shadow_wrap_as_initialized<u8>(evaluate_condition(insn.cc())));
  2406. }
  2407. void SoftCPU::SGDT(const X86::Instruction&) { TODO_INSN(); }
  2408. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction& insn)
  2409. {
  2410. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), const_gpr16(insn.reg16()), cl()));
  2411. }
  2412. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction& insn)
  2413. {
  2414. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), const_gpr16(insn.reg16()), shadow_wrap_as_initialized(insn.imm8())));
  2415. }
  2416. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction& insn)
  2417. {
  2418. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), const_gpr32(insn.reg32()), cl()));
  2419. }
  2420. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction& insn)
  2421. {
  2422. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), const_gpr32(insn.reg32()), shadow_wrap_as_initialized(insn.imm8())));
  2423. }
  2424. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHL, op_shl)
  2425. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction& insn)
  2426. {
  2427. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), const_gpr16(insn.reg16()), cl()));
  2428. }
  2429. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction& insn)
  2430. {
  2431. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), const_gpr16(insn.reg16()), shadow_wrap_as_initialized(insn.imm8())));
  2432. }
  2433. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction& insn)
  2434. {
  2435. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), const_gpr32(insn.reg32()), cl()));
  2436. }
  2437. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction& insn)
  2438. {
  2439. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), const_gpr32(insn.reg32()), shadow_wrap_as_initialized(insn.imm8())));
  2440. }
  2441. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHR, op_shr)
  2442. void SoftCPU::SIDT(const X86::Instruction&) { TODO_INSN(); }
  2443. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO_INSN(); }
  2444. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO_INSN(); }
  2445. void SoftCPU::STC(const X86::Instruction&)
  2446. {
  2447. set_cf(true);
  2448. }
  2449. void SoftCPU::STD(const X86::Instruction&)
  2450. {
  2451. set_df(true);
  2452. }
  2453. void SoftCPU::STI(const X86::Instruction&) { TODO_INSN(); }
  2454. void SoftCPU::STOSB(const X86::Instruction& insn)
  2455. {
  2456. if (insn.has_rep_prefix() && !df()) {
  2457. // Fast path for 8-bit forward memory fill.
  2458. if (m_emulator.mmu().fast_fill_memory8({ es(), destination_index(insn.address_size()).value() }, ecx().value(), al())) {
  2459. switch (insn.address_size()) {
  2460. case X86::AddressSize::Size32:
  2461. // FIXME: Should an uninitialized ECX taint EDI here?
  2462. set_edi({ (u32)(edi().value() + ecx().value()), edi().shadow() });
  2463. set_ecx(shadow_wrap_as_initialized<u32>(0));
  2464. break;
  2465. case X86::AddressSize::Size16:
  2466. // FIXME: Should an uninitialized CX taint DI here?
  2467. set_di({ (u16)(di().value() + cx().value()), di().shadow() });
  2468. set_cx(shadow_wrap_as_initialized<u16>(0));
  2469. break;
  2470. default:
  2471. VERIFY_NOT_REACHED();
  2472. }
  2473. return;
  2474. }
  2475. }
  2476. do_once_or_repeat<false>(insn, [&] {
  2477. write_memory8({ es(), destination_index(insn.address_size()).value() }, al());
  2478. step_destination_index(insn.address_size(), 1);
  2479. });
  2480. }
  2481. void SoftCPU::STOSD(const X86::Instruction& insn)
  2482. {
  2483. if (insn.has_rep_prefix() && !df()) {
  2484. // Fast path for 32-bit forward memory fill.
  2485. if (m_emulator.mmu().fast_fill_memory32({ es(), destination_index(insn.address_size()).value() }, ecx().value(), eax())) {
  2486. switch (insn.address_size()) {
  2487. case X86::AddressSize::Size32:
  2488. // FIXME: Should an uninitialized ECX taint EDI here?
  2489. set_edi({ (u32)(edi().value() + (ecx().value() * sizeof(u32))), edi().shadow() });
  2490. set_ecx(shadow_wrap_as_initialized<u32>(0));
  2491. break;
  2492. case X86::AddressSize::Size16:
  2493. // FIXME: Should an uninitialized CX taint DI here?
  2494. set_di({ (u16)(di().value() + (cx().value() * sizeof(u32))), di().shadow() });
  2495. set_cx(shadow_wrap_as_initialized<u16>(0));
  2496. break;
  2497. default:
  2498. VERIFY_NOT_REACHED();
  2499. }
  2500. return;
  2501. }
  2502. }
  2503. do_once_or_repeat<false>(insn, [&] {
  2504. write_memory32({ es(), destination_index(insn.address_size()).value() }, eax());
  2505. step_destination_index(insn.address_size(), 4);
  2506. });
  2507. }
  2508. void SoftCPU::STOSW(const X86::Instruction& insn)
  2509. {
  2510. do_once_or_repeat<false>(insn, [&] {
  2511. write_memory16({ es(), destination_index(insn.address_size()).value() }, ax());
  2512. step_destination_index(insn.address_size(), 2);
  2513. });
  2514. }
  2515. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO_INSN(); }
  2516. void SoftCPU::UD0(const X86::Instruction&) { TODO_INSN(); }
  2517. void SoftCPU::UD1(const X86::Instruction&) { TODO_INSN(); }
  2518. void SoftCPU::UD2(const X86::Instruction&) { TODO_INSN(); }
  2519. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO_INSN(); }
  2520. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO_INSN(); }
  2521. void SoftCPU::WAIT(const X86::Instruction&) { TODO_INSN(); }
  2522. void SoftCPU::WBINVD(const X86::Instruction&) { TODO_INSN(); }
  2523. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  2524. {
  2525. auto dest = insn.modrm().read16(*this, insn);
  2526. auto src = const_gpr16(insn.reg16());
  2527. auto result = op_add(*this, dest, src);
  2528. gpr16(insn.reg16()) = dest;
  2529. insn.modrm().write16(*this, insn, result);
  2530. }
  2531. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  2532. {
  2533. auto dest = insn.modrm().read32(*this, insn);
  2534. auto src = const_gpr32(insn.reg32());
  2535. auto result = op_add(*this, dest, src);
  2536. gpr32(insn.reg32()) = dest;
  2537. insn.modrm().write32(*this, insn, result);
  2538. }
  2539. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  2540. {
  2541. auto dest = insn.modrm().read8(*this, insn);
  2542. auto src = const_gpr8(insn.reg8());
  2543. auto result = op_add(*this, dest, src);
  2544. gpr8(insn.reg8()) = dest;
  2545. insn.modrm().write8(*this, insn, result);
  2546. }
  2547. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  2548. {
  2549. auto temp = gpr16(insn.reg16());
  2550. gpr16(insn.reg16()) = ax();
  2551. set_ax(temp);
  2552. }
  2553. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  2554. {
  2555. auto temp = gpr32(insn.reg32());
  2556. gpr32(insn.reg32()) = eax();
  2557. set_eax(temp);
  2558. }
  2559. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  2560. {
  2561. auto temp = insn.modrm().read16(*this, insn);
  2562. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  2563. gpr16(insn.reg16()) = temp;
  2564. }
  2565. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  2566. {
  2567. auto temp = insn.modrm().read32(*this, insn);
  2568. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  2569. gpr32(insn.reg32()) = temp;
  2570. }
  2571. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  2572. {
  2573. auto temp = insn.modrm().read8(*this, insn);
  2574. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  2575. gpr8(insn.reg8()) = temp;
  2576. }
  2577. void SoftCPU::XLAT(const X86::Instruction& insn)
  2578. {
  2579. u32 offset;
  2580. switch (insn.address_size()) {
  2581. case X86::AddressSize::Size32:
  2582. warn_if_uninitialized(ebx(), "xlat ebx");
  2583. offset = ebx().value() + al().value();
  2584. break;
  2585. case X86::AddressSize::Size16:
  2586. warn_if_uninitialized(bx(), "xlat bx");
  2587. offset = bx().value() + al().value();
  2588. break;
  2589. default:
  2590. VERIFY_NOT_REACHED();
  2591. }
  2592. warn_if_uninitialized(al(), "xlat al");
  2593. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), offset }));
  2594. }
  2595. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest, is_zero_idiom_if_both_operands_same, is_or) \
  2596. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) \
  2597. { \
  2598. generic_AL_imm8<update_dest, is_or>(op<ValueWithShadow<u8>>, insn); \
  2599. } \
  2600. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) \
  2601. { \
  2602. generic_AX_imm16<update_dest, is_or>(op<ValueWithShadow<u16>>, insn); \
  2603. } \
  2604. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) \
  2605. { \
  2606. generic_EAX_imm32<update_dest, is_or>(op<ValueWithShadow<u32>>, insn); \
  2607. } \
  2608. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) \
  2609. { \
  2610. generic_RM16_imm16<update_dest, is_or>(op<ValueWithShadow<u16>>, insn); \
  2611. } \
  2612. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) \
  2613. { \
  2614. generic_RM16_reg16<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u16>>, insn); \
  2615. } \
  2616. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) \
  2617. { \
  2618. generic_RM32_imm32<update_dest, is_or>(op<ValueWithShadow<u32>>, insn); \
  2619. } \
  2620. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) \
  2621. { \
  2622. generic_RM32_reg32<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u32>>, insn); \
  2623. } \
  2624. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) \
  2625. { \
  2626. generic_RM8_imm8<update_dest, is_or>(op<ValueWithShadow<u8>>, insn); \
  2627. } \
  2628. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) \
  2629. { \
  2630. generic_RM8_reg8<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u8>>, insn); \
  2631. }
  2632. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest, is_zero_idiom_if_both_operands_same, is_or) \
  2633. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest, is_zero_idiom_if_both_operands_same, is_or) \
  2634. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) \
  2635. { \
  2636. generic_RM16_imm8<update_dest, is_or>(op<ValueWithShadow<u16>>, insn); \
  2637. } \
  2638. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) \
  2639. { \
  2640. generic_RM32_imm8<update_dest, is_or>(op<ValueWithShadow<u32>>, insn); \
  2641. } \
  2642. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) \
  2643. { \
  2644. generic_reg16_RM16<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u16>>, insn); \
  2645. } \
  2646. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) \
  2647. { \
  2648. generic_reg32_RM32<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u32>>, insn); \
  2649. } \
  2650. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) \
  2651. { \
  2652. generic_reg8_RM8<update_dest, is_zero_idiom_if_both_operands_same>(op<ValueWithShadow<u8>>, insn); \
  2653. }
  2654. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true, true, false)
  2655. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true, false, true)
  2656. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true, false, false)
  2657. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true, false, false)
  2658. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true, true, false)
  2659. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true, false, false)
  2660. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true, false, false)
  2661. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false, false, false)
  2662. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false, false, false)
  2663. FPU_INSTRUCTION(MOVQ_mm1_mm2m64);
  2664. FPU_INSTRUCTION(MOVQ_mm1m64_mm2);
  2665. FPU_INSTRUCTION(MOVD_mm1_rm32);
  2666. FPU_INSTRUCTION(MOVQ_mm1_rm64); // long mode
  2667. FPU_INSTRUCTION(MOVD_rm32_mm2);
  2668. FPU_INSTRUCTION(MOVQ_rm64_mm2); // long mode
  2669. FPU_INSTRUCTION(EMMS);
  2670. void SoftCPU::CMPXCHG8B_m64(X86::Instruction const&) { TODO_INSN(); }
  2671. void SoftCPU::RDRAND_reg(X86::Instruction const&) { TODO_INSN(); }
  2672. void SoftCPU::RDSEED_reg(X86::Instruction const&) { TODO_INSN(); }
  2673. VPU_INSTRUCTION(PREFETCHTNTA);
  2674. VPU_INSTRUCTION(PREFETCHT0);
  2675. VPU_INSTRUCTION(PREFETCHT1);
  2676. VPU_INSTRUCTION(PREFETCHT2);
  2677. VPU_INSTRUCTION(LDMXCSR);
  2678. VPU_INSTRUCTION(STMXCSR);
  2679. VPU_INSTRUCTION(MOVUPS_xmm1_xmm2m128);
  2680. VPU_INSTRUCTION(MOVSS_xmm1_xmm2m32);
  2681. VPU_INSTRUCTION(MOVUPS_xmm1m128_xmm2);
  2682. VPU_INSTRUCTION(MOVSS_xmm1m32_xmm2);
  2683. VPU_INSTRUCTION(MOVLPS_xmm1_xmm2m64);
  2684. VPU_INSTRUCTION(MOVLPS_m64_xmm2);
  2685. VPU_INSTRUCTION(UNPCKLPS_xmm1_xmm2m128);
  2686. VPU_INSTRUCTION(UNPCKHPS_xmm1_xmm2m128);
  2687. VPU_INSTRUCTION(MOVHPS_xmm1_xmm2m64);
  2688. VPU_INSTRUCTION(MOVHPS_m64_xmm2);
  2689. VPU_INSTRUCTION(MOVAPS_xmm1_xmm2m128);
  2690. VPU_INSTRUCTION(MOVAPS_xmm1m128_xmm2);
  2691. VPU_INSTRUCTION(CVTTPS2PI_mm1_xmm2m64);
  2692. VPU_INSTRUCTION(CVTTSS2SI_r32_xmm2m32);
  2693. VPU_INSTRUCTION(CVTPI2PS_xmm1_mm2m64);
  2694. VPU_INSTRUCTION(CVTSI2SS_xmm1_rm32);
  2695. VPU_INSTRUCTION(MOVNTPS_xmm1m128_xmm2);
  2696. VPU_INSTRUCTION(CVTPS2PI_xmm1_mm2m64);
  2697. VPU_INSTRUCTION(CVTSS2SI_r32_xmm2m32);
  2698. VPU_INSTRUCTION(UCOMISS_xmm1_xmm2m32);
  2699. VPU_INSTRUCTION(COMISS_xmm1_xmm2m32);
  2700. VPU_INSTRUCTION(MOVMSKPS_reg_xmm);
  2701. VPU_INSTRUCTION(SQRTPS_xmm1_xmm2m128);
  2702. VPU_INSTRUCTION(SQRTSS_xmm1_xmm2m32);
  2703. VPU_INSTRUCTION(RSQRTPS_xmm1_xmm2m128);
  2704. VPU_INSTRUCTION(RSQRTSS_xmm1_xmm2m32);
  2705. VPU_INSTRUCTION(RCPPS_xmm1_xmm2m128);
  2706. VPU_INSTRUCTION(RCPSS_xmm1_xmm2m32);
  2707. VPU_INSTRUCTION(ANDPS_xmm1_xmm2m128);
  2708. VPU_INSTRUCTION(ANDNPS_xmm1_xmm2m128);
  2709. VPU_INSTRUCTION(ORPS_xmm1_xmm2m128);
  2710. VPU_INSTRUCTION(XORPS_xmm1_xmm2m128);
  2711. VPU_INSTRUCTION(ADDPS_xmm1_xmm2m128);
  2712. VPU_INSTRUCTION(ADDSS_xmm1_xmm2m32);
  2713. VPU_INSTRUCTION(MULPS_xmm1_xmm2m128);
  2714. VPU_INSTRUCTION(MULSS_xmm1_xmm2m32);
  2715. VPU_INSTRUCTION(SUBPS_xmm1_xmm2m128);
  2716. VPU_INSTRUCTION(SUBSS_xmm1_xmm2m32);
  2717. VPU_INSTRUCTION(MINPS_xmm1_xmm2m128);
  2718. VPU_INSTRUCTION(MINSS_xmm1_xmm2m32);
  2719. VPU_INSTRUCTION(DIVPS_xmm1_xmm2m128);
  2720. VPU_INSTRUCTION(DIVSS_xmm1_xmm2m32);
  2721. VPU_INSTRUCTION(MAXPS_xmm1_xmm2m128);
  2722. VPU_INSTRUCTION(MAXSS_xmm1_xmm2m32);
  2723. VPU_INSTRUCTION(PSHUFW_mm1_mm2m64_imm8);
  2724. VPU_INSTRUCTION(CMPPS_xmm1_xmm2m128_imm8);
  2725. VPU_INSTRUCTION(CMPSS_xmm1_xmm2m32_imm8);
  2726. VPU_INSTRUCTION(PINSRW_mm1_r32m16_imm8);
  2727. VPU_INSTRUCTION(PINSRW_xmm1_r32m16_imm8);
  2728. VPU_INSTRUCTION(PEXTRW_reg_mm1_imm8);
  2729. VPU_INSTRUCTION(PEXTRW_reg_xmm1_imm8);
  2730. VPU_INSTRUCTION(SHUFPS_xmm1_xmm2m128_imm8);
  2731. VPU_INSTRUCTION(PMOVMSKB_reg_mm1);
  2732. VPU_INSTRUCTION(PMOVMSKB_reg_xmm1);
  2733. VPU_INSTRUCTION(PMINUB_mm1_mm2m64);
  2734. VPU_INSTRUCTION(PMINUB_xmm1_xmm2m128);
  2735. VPU_INSTRUCTION(PMAXUB_mm1_mm2m64);
  2736. VPU_INSTRUCTION(PMAXUB_xmm1_xmm2m128);
  2737. VPU_INSTRUCTION(PAVGB_mm1_mm2m64);
  2738. VPU_INSTRUCTION(PAVGB_xmm1_xmm2m128);
  2739. VPU_INSTRUCTION(PAVGW_mm1_mm2m64);
  2740. VPU_INSTRUCTION(PAVGW_xmm1_xmm2m128);
  2741. VPU_INSTRUCTION(PMULHUW_mm1_mm2m64);
  2742. VPU_INSTRUCTION(PMULHUW_xmm1_xmm2m64);
  2743. VPU_INSTRUCTION(MOVNTQ_m64_mm1);
  2744. VPU_INSTRUCTION(PMINSB_mm1_mm2m64);
  2745. VPU_INSTRUCTION(PMINSB_xmm1_xmm2m128);
  2746. VPU_INSTRUCTION(PMAXSB_mm1_mm2m64);
  2747. VPU_INSTRUCTION(PMAXSB_xmm1_xmm2m128);
  2748. VPU_INSTRUCTION(PSADBB_mm1_mm2m64);
  2749. VPU_INSTRUCTION(PSADBB_xmm1_xmm2m128);
  2750. VPU_INSTRUCTION(MASKMOVQ_mm1_mm2m64);
  2751. void SoftCPU::MOVUPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2752. void SoftCPU::MOVSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2753. void SoftCPU::MOVUPD_xmm1m128_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2754. void SoftCPU::MOVSD_xmm1m32_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2755. void SoftCPU::MOVLPD_xmm1_m64(X86::Instruction const&) { TODO_INSN(); }
  2756. void SoftCPU::MOVLPD_m64_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2757. void SoftCPU::UNPCKLPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2758. void SoftCPU::UNPCKHPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2759. void SoftCPU::MOVHPD_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2760. void SoftCPU::MOVAPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2761. void SoftCPU::MOVAPD_xmm1m128_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2762. void SoftCPU::CVTPI2PD_xmm1_mm2m64(X86::Instruction const&) { TODO_INSN(); }
  2763. void SoftCPU::CVTSI2SD_xmm1_rm32(X86::Instruction const&) { TODO_INSN(); }
  2764. void SoftCPU::CVTTPD2PI_mm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2765. void SoftCPU::CVTTSS2SI_r32_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2766. void SoftCPU::CVTPD2PI_xmm1_mm2m128(X86::Instruction const&) { TODO_INSN(); }
  2767. void SoftCPU::CVTSD2SI_xmm1_rm64(X86::Instruction const&) { TODO_INSN(); }
  2768. void SoftCPU::UCOMISD_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2769. void SoftCPU::COMISD_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2770. void SoftCPU::MOVMSKPD_reg_xmm(X86::Instruction const&) { TODO_INSN(); }
  2771. void SoftCPU::SQRTPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2772. void SoftCPU::SQRTSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2773. void SoftCPU::ANDPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2774. void SoftCPU::ANDNPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2775. void SoftCPU::ORPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2776. void SoftCPU::XORPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2777. void SoftCPU::ADDPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2778. void SoftCPU::ADDSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2779. void SoftCPU::MULPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2780. void SoftCPU::MULSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2781. void SoftCPU::CVTPS2PD_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2782. void SoftCPU::CVTPD2PS_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2783. void SoftCPU::CVTSS2SD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2784. void SoftCPU::CVTSD2SS_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2785. void SoftCPU::CVTDQ2PS_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2786. void SoftCPU::CVTPS2DQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2787. void SoftCPU::CVTTPS2DQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2788. void SoftCPU::SUBPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2789. void SoftCPU::SUBSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2790. void SoftCPU::MINPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2791. void SoftCPU::MINSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2792. void SoftCPU::DIVPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2793. void SoftCPU::DIVSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2794. void SoftCPU::MAXPD_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2795. void SoftCPU::MAXSD_xmm1_xmm2m32(X86::Instruction const&) { TODO_INSN(); }
  2796. void SoftCPU::PUNPCKLQDQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2797. void SoftCPU::PUNPCKHQDQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2798. void SoftCPU::MOVDQA_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2799. void SoftCPU::MOVDQU_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2800. void SoftCPU::PSHUFD_xmm1_xmm2m128_imm8(X86::Instruction const&) { TODO_INSN(); }
  2801. void SoftCPU::PSHUFHW_xmm1_xmm2m128_imm8(X86::Instruction const&) { TODO_INSN(); }
  2802. void SoftCPU::PSHUFLW_xmm1_xmm2m128_imm8(X86::Instruction const&) { TODO_INSN(); }
  2803. void SoftCPU::PSRLQ_xmm1_imm8(X86::Instruction const&) { TODO_INSN(); }
  2804. void SoftCPU::PSRLDQ_xmm1_imm8(X86::Instruction const&) { TODO_INSN(); }
  2805. void SoftCPU::PSLLQ_xmm1_imm8(X86::Instruction const&) { TODO_INSN(); }
  2806. void SoftCPU::PSLLDQ_xmm1_imm8(X86::Instruction const&) { TODO_INSN(); }
  2807. void SoftCPU::MOVD_rm32_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2808. void SoftCPU::MOVQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2809. void SoftCPU::MOVDQA_xmm1m128_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2810. void SoftCPU::MOVDQU_xmm1m128_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2811. void SoftCPU::CMPPD_xmm1_xmm2m128_imm8(X86::Instruction const&) { TODO_INSN(); }
  2812. void SoftCPU::CMPSD_xmm1_xmm2m32_imm8(X86::Instruction const&) { TODO_INSN(); }
  2813. void SoftCPU::SHUFPD_xmm1_xmm2m128_imm8(X86::Instruction const&) { TODO_INSN(); }
  2814. void SoftCPU::PADDQ_mm1_mm2m64(X86::Instruction const&) { TODO_INSN(); }
  2815. void SoftCPU::MOVQ_xmm1m128_xmm2(X86::Instruction const&) { TODO_INSN(); }
  2816. void SoftCPU::MOVQ2DQ_xmm_mm(X86::Instruction const&) { TODO_INSN(); }
  2817. void SoftCPU::MOVDQ2Q_mm_xmm(X86::Instruction const&) { TODO_INSN(); }
  2818. void SoftCPU::CVTTPD2DQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2819. void SoftCPU::CVTPD2DQ_xmm1_xmm2m128(X86::Instruction const&) { TODO_INSN(); }
  2820. void SoftCPU::CVTDQ2PD_xmm1_xmm2m64(X86::Instruction const&) { TODO_INSN(); }
  2821. void SoftCPU::PMULUDQ_mm1_mm2m64(X86::Instruction const&) { TODO_INSN(); }
  2822. void SoftCPU::PMULUDQ_mm1_mm2m128(X86::Instruction const&) { TODO_INSN(); }
  2823. void SoftCPU::PSUBQ_mm1_mm2m64(X86::Instruction const&) { TODO_INSN(); }
  2824. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO_INSN(); }
  2825. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO_INSN(); }
  2826. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO_INSN(); }
  2827. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO_INSN(); }
  2828. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO_INSN(); }
  2829. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO_INSN(); }
  2830. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO_INSN(); }
  2831. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO_INSN(); }
  2832. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO_INSN(); }
  2833. }