SoftCPU.cpp 104 KB

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