SoftCPU.cpp 97 KB

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