SoftCPU.cpp 112 KB

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