SoftCPU.cpp 112 KB

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