SoftCPU.cpp 112 KB

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