SoftCPU.cpp 104 KB

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