SoftCPU.cpp 112 KB

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