SoftCPU.cpp 111 KB

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