SoftCPU.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  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>(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. dbgprintf("\n");
  58. dbgprintf("==%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, 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 (update_dest)
  621. set_al(result);
  622. }
  623. template<bool update_dest, typename Op>
  624. ALWAYS_INLINE void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  625. {
  626. auto dest = ax();
  627. auto src = shadow_wrap_as_initialized(insn.imm16());
  628. auto result = op(*this, dest, src);
  629. if (update_dest)
  630. set_ax(result);
  631. }
  632. template<bool update_dest, typename Op>
  633. ALWAYS_INLINE void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  634. {
  635. auto dest = eax();
  636. auto src = shadow_wrap_as_initialized(insn.imm32());
  637. auto result = op(*this, dest, src);
  638. if (update_dest)
  639. set_eax(result);
  640. }
  641. template<bool update_dest, typename Op>
  642. ALWAYS_INLINE void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  643. {
  644. auto dest = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  645. auto src = shadow_wrap_as_initialized(insn.imm16());
  646. auto result = op(*this, dest, src);
  647. if (update_dest)
  648. insn.modrm().write16(*this, insn, result);
  649. }
  650. template<bool update_dest, typename Op>
  651. ALWAYS_INLINE void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  652. {
  653. auto dest = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  654. auto src = shadow_wrap_as_initialized<u16>(sign_extended_to<u16>(insn.imm8()));
  655. auto result = op(*this, dest, src);
  656. if (update_dest)
  657. insn.modrm().write16(*this, insn, result);
  658. }
  659. template<bool update_dest, typename Op>
  660. ALWAYS_INLINE void SoftCPU::generic_RM16_unsigned_imm8(Op op, const X86::Instruction& insn)
  661. {
  662. auto dest = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  663. auto src = shadow_wrap_as_initialized(insn.imm8());
  664. auto result = op(*this, dest, src);
  665. if (update_dest)
  666. insn.modrm().write16(*this, insn, result);
  667. }
  668. template<bool update_dest, typename Op>
  669. ALWAYS_INLINE void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  670. {
  671. auto dest = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  672. auto src = const_gpr16(insn.reg16());
  673. auto result = op(*this, dest, src);
  674. if (update_dest)
  675. insn.modrm().write16(*this, insn, result);
  676. }
  677. template<bool update_dest, typename Op>
  678. ALWAYS_INLINE void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  679. {
  680. auto dest = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  681. auto src = insn.imm32();
  682. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  683. if (update_dest)
  684. insn.modrm().write32(*this, insn, result);
  685. }
  686. template<bool update_dest, typename Op>
  687. ALWAYS_INLINE void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  688. {
  689. auto dest = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  690. auto src = sign_extended_to<u32>(insn.imm8());
  691. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  692. if (update_dest)
  693. insn.modrm().write32(*this, insn, result);
  694. }
  695. template<bool update_dest, typename Op>
  696. ALWAYS_INLINE void SoftCPU::generic_RM32_unsigned_imm8(Op op, const X86::Instruction& insn)
  697. {
  698. auto dest = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  699. auto src = shadow_wrap_as_initialized(insn.imm8());
  700. auto result = op(*this, dest, src);
  701. if (update_dest)
  702. insn.modrm().write32(*this, insn, result);
  703. }
  704. template<bool update_dest, typename Op>
  705. ALWAYS_INLINE void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  706. {
  707. auto dest = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  708. auto src = const_gpr32(insn.reg32());
  709. auto result = op(*this, dest, src);
  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_RM8_imm8(Op op, const X86::Instruction& insn)
  715. {
  716. auto dest = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  717. auto src = insn.imm8();
  718. auto result = op(*this, dest, shadow_wrap_as_initialized(src));
  719. if (update_dest)
  720. insn.modrm().write8(*this, insn, result);
  721. }
  722. template<bool update_dest, typename Op>
  723. ALWAYS_INLINE void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  724. {
  725. auto dest = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  726. auto src = const_gpr8(insn.reg8());
  727. auto result = op(*this, dest, src);
  728. if (update_dest)
  729. insn.modrm().write8(*this, insn, result);
  730. }
  731. template<bool update_dest, typename Op>
  732. ALWAYS_INLINE void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  733. {
  734. auto dest = const_gpr16(insn.reg16());
  735. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  736. auto result = op(*this, dest, src);
  737. if (update_dest)
  738. gpr16(insn.reg16()) = result;
  739. }
  740. template<bool update_dest, typename Op>
  741. ALWAYS_INLINE void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  742. {
  743. auto dest = const_gpr32(insn.reg32());
  744. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  745. auto result = op(*this, dest, src);
  746. if (update_dest)
  747. gpr32(insn.reg32()) = result;
  748. }
  749. template<bool update_dest, typename Op>
  750. ALWAYS_INLINE void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  751. {
  752. auto dest = const_gpr8(insn.reg8());
  753. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  754. auto result = op(*this, dest, src);
  755. if (update_dest)
  756. gpr8(insn.reg8()) = result;
  757. }
  758. template<typename Op>
  759. ALWAYS_INLINE void SoftCPU::generic_RM8_1(Op op, const X86::Instruction& insn)
  760. {
  761. auto data = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  762. insn.modrm().write8(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  763. }
  764. template<typename Op>
  765. ALWAYS_INLINE void SoftCPU::generic_RM8_CL(Op op, const X86::Instruction& insn)
  766. {
  767. auto data = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  768. insn.modrm().write8(*this, insn, op(*this, data, cl()));
  769. }
  770. template<typename Op>
  771. ALWAYS_INLINE void SoftCPU::generic_RM16_1(Op op, const X86::Instruction& insn)
  772. {
  773. auto data = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  774. insn.modrm().write16(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  775. }
  776. template<typename Op>
  777. ALWAYS_INLINE void SoftCPU::generic_RM16_CL(Op op, const X86::Instruction& insn)
  778. {
  779. auto data = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  780. insn.modrm().write16(*this, insn, op(*this, data, cl()));
  781. }
  782. template<typename Op>
  783. ALWAYS_INLINE void SoftCPU::generic_RM32_1(Op op, const X86::Instruction& insn)
  784. {
  785. auto data = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  786. insn.modrm().write32(*this, insn, op(*this, data, shadow_wrap_as_initialized<u8>(1)));
  787. }
  788. template<typename Op>
  789. ALWAYS_INLINE void SoftCPU::generic_RM32_CL(Op op, const X86::Instruction& insn)
  790. {
  791. auto data = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  792. insn.modrm().write32(*this, insn, op(*this, data, cl()));
  793. }
  794. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  795. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  796. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  797. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  800. template<typename T>
  801. ALWAYS_INLINE static T op_bsf(SoftCPU&, T value)
  802. {
  803. return { (typename T::ValueType)__builtin_ctz(value.value()), value.shadow() };
  804. }
  805. template<typename T>
  806. ALWAYS_INLINE static T op_bsr(SoftCPU&, T value)
  807. {
  808. typename T::ValueType bit_index = 0;
  809. if constexpr (sizeof(typename T::ValueType) == 4) {
  810. asm volatile("bsrl %%eax, %%edx"
  811. : "=d"(bit_index)
  812. : "a"(value.value()));
  813. }
  814. if constexpr (sizeof(typename T::ValueType) == 2) {
  815. asm volatile("bsrw %%ax, %%dx"
  816. : "=d"(bit_index)
  817. : "a"(value.value()));
  818. }
  819. return shadow_wrap_with_taint_from(bit_index, value);
  820. }
  821. void SoftCPU::BSF_reg16_RM16(const X86::Instruction& insn)
  822. {
  823. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  824. set_zf(!src.value());
  825. if (src.value())
  826. gpr16(insn.reg16()) = op_bsf(*this, src);
  827. taint_flags_from(src);
  828. }
  829. void SoftCPU::BSF_reg32_RM32(const X86::Instruction& insn)
  830. {
  831. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  832. set_zf(!src.value());
  833. if (src.value()) {
  834. gpr32(insn.reg32()) = op_bsf(*this, src);
  835. taint_flags_from(src);
  836. }
  837. }
  838. void SoftCPU::BSR_reg16_RM16(const X86::Instruction& insn)
  839. {
  840. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  841. set_zf(!src.value());
  842. if (src.value()) {
  843. gpr16(insn.reg16()) = op_bsr(*this, src);
  844. taint_flags_from(src);
  845. }
  846. }
  847. void SoftCPU::BSR_reg32_RM32(const X86::Instruction& insn)
  848. {
  849. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  850. set_zf(!src.value());
  851. if (src.value()) {
  852. gpr32(insn.reg32()) = op_bsr(*this, src);
  853. taint_flags_from(src);
  854. }
  855. }
  856. void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
  857. {
  858. gpr32(insn.reg32()) = { __builtin_bswap32(gpr32(insn.reg32()).value()), __builtin_bswap32(gpr32(insn.reg32()).shadow()) };
  859. }
  860. template<typename T>
  861. ALWAYS_INLINE static T op_bt(T value, T)
  862. {
  863. return value;
  864. }
  865. template<typename T>
  866. ALWAYS_INLINE static T op_bts(T value, T bit_mask)
  867. {
  868. return value | bit_mask;
  869. }
  870. template<typename T>
  871. ALWAYS_INLINE static T op_btr(T value, T bit_mask)
  872. {
  873. return value & ~bit_mask;
  874. }
  875. template<typename T>
  876. ALWAYS_INLINE static T op_btc(T value, T bit_mask)
  877. {
  878. return value ^ bit_mask;
  879. }
  880. template<bool should_update, typename Op>
  881. ALWAYS_INLINE void BTx_RM16_reg16(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  882. {
  883. if (insn.modrm().is_register()) {
  884. unsigned bit_index = cpu.const_gpr16(insn.reg16()).value() & (X86::TypeTrivia<u16>::bits - 1);
  885. auto original = insn.modrm().read16<ValueWithShadow<u16>>(cpu, insn);
  886. u16 bit_mask = 1 << bit_index;
  887. u16 result = op(original.value(), bit_mask);
  888. cpu.set_cf((original.value() & bit_mask) != 0);
  889. cpu.taint_flags_from(cpu.gpr16(insn.reg16()), original);
  890. if (should_update)
  891. insn.modrm().write16(cpu, insn, shadow_wrap_with_taint_from(result, cpu.gpr16(insn.reg16()), original));
  892. return;
  893. }
  894. // FIXME: Is this supposed to perform a full 16-bit read/modify/write?
  895. unsigned bit_offset_in_array = cpu.const_gpr16(insn.reg16()).value() / 8;
  896. unsigned bit_offset_in_byte = cpu.const_gpr16(insn.reg16()).value() & 7;
  897. auto address = insn.modrm().resolve(cpu, insn);
  898. address.set_offset(address.offset() + bit_offset_in_array);
  899. auto dest = cpu.read_memory8(address);
  900. u8 bit_mask = 1 << bit_offset_in_byte;
  901. u8 result = op(dest.value(), bit_mask);
  902. cpu.set_cf((dest.value() & bit_mask) != 0);
  903. cpu.taint_flags_from(cpu.gpr16(insn.reg16()), dest);
  904. if (should_update)
  905. cpu.write_memory8(address, shadow_wrap_with_taint_from(result, cpu.gpr16(insn.reg16()), dest));
  906. }
  907. template<bool should_update, typename Op>
  908. ALWAYS_INLINE void BTx_RM32_reg32(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  909. {
  910. if (insn.modrm().is_register()) {
  911. unsigned bit_index = cpu.const_gpr32(insn.reg32()).value() & (X86::TypeTrivia<u32>::bits - 1);
  912. auto original = insn.modrm().read32<ValueWithShadow<u32>>(cpu, insn);
  913. u32 bit_mask = 1 << bit_index;
  914. u32 result = op(original.value(), bit_mask);
  915. cpu.set_cf((original.value() & bit_mask) != 0);
  916. cpu.taint_flags_from(cpu.gpr32(insn.reg32()), original);
  917. if (should_update)
  918. insn.modrm().write32(cpu, insn, shadow_wrap_with_taint_from(result, cpu.gpr32(insn.reg32()), original));
  919. return;
  920. }
  921. // FIXME: Is this supposed to perform a full 32-bit read/modify/write?
  922. unsigned bit_offset_in_array = cpu.const_gpr32(insn.reg32()).value() / 8;
  923. unsigned bit_offset_in_byte = cpu.const_gpr32(insn.reg32()).value() & 7;
  924. auto address = insn.modrm().resolve(cpu, insn);
  925. address.set_offset(address.offset() + bit_offset_in_array);
  926. auto dest = cpu.read_memory8(address);
  927. u8 bit_mask = 1 << bit_offset_in_byte;
  928. u8 result = op(dest.value(), bit_mask);
  929. cpu.set_cf((dest.value() & bit_mask) != 0);
  930. cpu.taint_flags_from(cpu.gpr32(insn.reg32()), dest);
  931. if (should_update)
  932. cpu.write_memory8(address, shadow_wrap_with_taint_from(result, cpu.gpr32(insn.reg32()), dest));
  933. }
  934. template<bool should_update, typename Op>
  935. ALWAYS_INLINE void BTx_RM16_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  936. {
  937. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u16>::mask);
  938. // FIXME: Support higher bit indices
  939. ASSERT(bit_index < 16);
  940. auto original = insn.modrm().read16<ValueWithShadow<u16>>(cpu, insn);
  941. u16 bit_mask = 1 << bit_index;
  942. auto result = op(original.value(), bit_mask);
  943. cpu.set_cf((original.value() & bit_mask) != 0);
  944. cpu.taint_flags_from(original);
  945. if (should_update)
  946. insn.modrm().write16(cpu, insn, shadow_wrap_with_taint_from(result, original));
  947. }
  948. template<bool should_update, typename Op>
  949. ALWAYS_INLINE void BTx_RM32_imm8(SoftCPU& cpu, const X86::Instruction& insn, Op op)
  950. {
  951. unsigned bit_index = insn.imm8() & (X86::TypeTrivia<u32>::mask);
  952. // FIXME: Support higher bit indices
  953. ASSERT(bit_index < 32);
  954. auto original = insn.modrm().read32<ValueWithShadow<u32>>(cpu, insn);
  955. u32 bit_mask = 1 << bit_index;
  956. auto result = op(original.value(), bit_mask);
  957. cpu.set_cf((original.value() & bit_mask) != 0);
  958. cpu.taint_flags_from(original);
  959. if (should_update)
  960. insn.modrm().write32(cpu, insn, shadow_wrap_with_taint_from(result, original));
  961. }
  962. #define DEFINE_GENERIC_BTx_INSN_HANDLERS(mnemonic, op, update_dest) \
  963. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { BTx_RM32_reg32<update_dest>(*this, insn, op<u32>); } \
  964. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { BTx_RM16_reg16<update_dest>(*this, insn, op<u16>); } \
  965. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { BTx_RM32_imm8<update_dest>(*this, insn, op<u32>); } \
  966. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { BTx_RM16_imm8<update_dest>(*this, insn, op<u16>); }
  967. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTS, op_bts, true);
  968. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTR, op_btr, true);
  969. DEFINE_GENERIC_BTx_INSN_HANDLERS(BTC, op_btc, true);
  970. DEFINE_GENERIC_BTx_INSN_HANDLERS(BT, op_bt, false);
  971. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&)
  972. {
  973. TODO();
  974. }
  975. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  976. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  977. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  978. {
  979. push32(shadow_wrap_as_initialized(eip()));
  980. auto address = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  981. warn_if_uninitialized(address, "call rm32");
  982. set_eip(address.value());
  983. }
  984. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  985. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  986. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  987. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  988. {
  989. push32(shadow_wrap_as_initialized(eip()));
  990. set_eip(eip() + (i32)insn.imm32());
  991. }
  992. void SoftCPU::CBW(const X86::Instruction&)
  993. {
  994. set_ah(shadow_wrap_with_taint_from<u8>((al().value() & 0x80) ? 0xff : 0x00, al()));
  995. }
  996. void SoftCPU::CDQ(const X86::Instruction&)
  997. {
  998. if (eax().value() & 0x80000000)
  999. set_edx(shadow_wrap_with_taint_from<u32>(0xffffffff, eax()));
  1000. else
  1001. set_edx(shadow_wrap_with_taint_from<u32>(0, eax()));
  1002. }
  1003. void SoftCPU::CLC(const X86::Instruction&)
  1004. {
  1005. set_cf(false);
  1006. }
  1007. void SoftCPU::CLD(const X86::Instruction&)
  1008. {
  1009. set_df(false);
  1010. }
  1011. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  1012. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  1013. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  1014. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  1015. {
  1016. warn_if_flags_tainted("cmovcc reg16, rm16");
  1017. if (evaluate_condition(insn.cc()))
  1018. gpr16(insn.reg16()) = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1019. }
  1020. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  1021. {
  1022. warn_if_flags_tainted("cmovcc reg32, rm32");
  1023. if (evaluate_condition(insn.cc()))
  1024. gpr32(insn.reg32()) = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1025. }
  1026. template<typename T>
  1027. ALWAYS_INLINE static void do_cmps(SoftCPU& cpu, const X86::Instruction& insn)
  1028. {
  1029. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1030. cpu.do_once_or_repeat<true>(insn, [&] {
  1031. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.a32()).value() });
  1032. auto dest = cpu.read_memory<T>({ cpu.es(), cpu.destination_index(insn.a32()).value() });
  1033. op_sub(cpu, dest, src);
  1034. cpu.step_source_index(insn.a32(), sizeof(T));
  1035. cpu.step_destination_index(insn.a32(), sizeof(T));
  1036. });
  1037. }
  1038. void SoftCPU::CMPSB(const X86::Instruction& insn)
  1039. {
  1040. do_cmps<u8>(*this, insn);
  1041. }
  1042. void SoftCPU::CMPSD(const X86::Instruction& insn)
  1043. {
  1044. do_cmps<u32>(*this, insn);
  1045. }
  1046. void SoftCPU::CMPSW(const X86::Instruction& insn)
  1047. {
  1048. do_cmps<u16>(*this, insn);
  1049. }
  1050. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  1051. {
  1052. auto current = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1053. taint_flags_from(current, ax());
  1054. if (current.value() == ax().value()) {
  1055. set_zf(true);
  1056. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  1057. } else {
  1058. set_zf(false);
  1059. set_ax(current);
  1060. }
  1061. }
  1062. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  1063. {
  1064. auto current = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1065. taint_flags_from(current, eax());
  1066. if (current.value() == eax().value()) {
  1067. set_zf(true);
  1068. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  1069. } else {
  1070. set_zf(false);
  1071. set_eax(current);
  1072. }
  1073. }
  1074. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  1075. {
  1076. auto current = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1077. taint_flags_from(current, al());
  1078. if (current.value() == al().value()) {
  1079. set_zf(true);
  1080. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  1081. } else {
  1082. set_zf(false);
  1083. set_al(current);
  1084. }
  1085. }
  1086. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  1087. void SoftCPU::CWD(const X86::Instruction&)
  1088. {
  1089. set_dx(shadow_wrap_with_taint_from<u16>((ax().value() & 0x8000) ? 0xffff : 0x0000, ax()));
  1090. }
  1091. void SoftCPU::CWDE(const X86::Instruction&)
  1092. {
  1093. set_eax(shadow_wrap_with_taint_from(sign_extended_to<u32>(ax().value()), ax()));
  1094. }
  1095. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  1096. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  1097. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  1098. {
  1099. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16<ValueWithShadow<u16>>(*this, insn)));
  1100. }
  1101. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  1102. {
  1103. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32<ValueWithShadow<u32>>(*this, insn)));
  1104. }
  1105. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  1106. {
  1107. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8<ValueWithShadow<u8>>(*this, insn)));
  1108. }
  1109. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  1110. {
  1111. gpr16(insn.reg16()) = op_dec(*this, const_gpr16(insn.reg16()));
  1112. }
  1113. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  1114. {
  1115. gpr32(insn.reg32()) = op_dec(*this, const_gpr32(insn.reg32()));
  1116. }
  1117. void SoftCPU::DIV_RM16(const X86::Instruction& insn)
  1118. {
  1119. auto divisor = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1120. if (divisor.value() == 0) {
  1121. warn() << "Divide by zero";
  1122. TODO();
  1123. }
  1124. u32 dividend = ((u32)dx().value() << 16) | ax().value();
  1125. auto quotient = dividend / divisor.value();
  1126. if (quotient > NumericLimits<u16>::max()) {
  1127. warn() << "Divide overflow";
  1128. TODO();
  1129. }
  1130. auto remainder = dividend % divisor.value();
  1131. auto original_ax = ax();
  1132. set_ax(shadow_wrap_with_taint_from<u16>(quotient, original_ax, dx()));
  1133. set_dx(shadow_wrap_with_taint_from<u16>(remainder, original_ax, dx()));
  1134. }
  1135. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  1136. {
  1137. auto divisor = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1138. if (divisor.value() == 0) {
  1139. warn() << "Divide by zero";
  1140. TODO();
  1141. }
  1142. u64 dividend = ((u64)edx().value() << 32) | eax().value();
  1143. auto quotient = dividend / divisor.value();
  1144. if (quotient > NumericLimits<u32>::max()) {
  1145. warn() << "Divide overflow";
  1146. TODO();
  1147. }
  1148. auto remainder = dividend % divisor.value();
  1149. auto original_eax = eax();
  1150. set_eax(shadow_wrap_with_taint_from<u32>(quotient, original_eax, edx(), divisor));
  1151. set_edx(shadow_wrap_with_taint_from<u32>(remainder, original_eax, edx(), divisor));
  1152. }
  1153. void SoftCPU::DIV_RM8(const X86::Instruction& insn)
  1154. {
  1155. auto divisor = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1156. if (divisor.value() == 0) {
  1157. warn() << "Divide by zero";
  1158. TODO();
  1159. }
  1160. u16 dividend = ax().value();
  1161. auto quotient = dividend / divisor.value();
  1162. if (quotient > NumericLimits<u8>::max()) {
  1163. warn() << "Divide overflow";
  1164. TODO();
  1165. }
  1166. auto remainder = dividend % divisor.value();
  1167. auto original_ax = ax();
  1168. set_al(shadow_wrap_with_taint_from<u8>(quotient, original_ax, divisor));
  1169. set_ah(shadow_wrap_with_taint_from<u8>(remainder, original_ax, divisor));
  1170. }
  1171. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  1172. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  1173. void SoftCPU::ESCAPE(const X86::Instruction&)
  1174. {
  1175. dbg() << "FIXME: x87 floating-point support";
  1176. m_emulator.dump_backtrace();
  1177. TODO();
  1178. }
  1179. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  1180. void SoftCPU::IDIV_RM16(const X86::Instruction& insn)
  1181. {
  1182. auto divisor_with_shadow = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1183. auto divisor = (i16)divisor_with_shadow.value();
  1184. if (divisor == 0) {
  1185. warn() << "Divide by zero";
  1186. TODO();
  1187. }
  1188. i32 dividend = (i32)(((u32)dx().value() << 16) | (u32)ax().value());
  1189. i32 result = dividend / divisor;
  1190. if (result > NumericLimits<i16>::max() || result < NumericLimits<i16>::min()) {
  1191. warn() << "Divide overflow";
  1192. TODO();
  1193. }
  1194. auto original_ax = ax();
  1195. set_ax(shadow_wrap_with_taint_from<u16>(result, original_ax, dx(), divisor_with_shadow));
  1196. set_dx(shadow_wrap_with_taint_from<u16>(dividend % divisor, original_ax, dx(), divisor_with_shadow));
  1197. }
  1198. void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
  1199. {
  1200. auto divisor_with_shadow = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1201. auto divisor = (i32)divisor_with_shadow.value();
  1202. if (divisor == 0) {
  1203. warn() << "Divide by zero";
  1204. TODO();
  1205. }
  1206. i64 dividend = (i64)(((u64)edx().value() << 32) | (u64)eax().value());
  1207. i64 result = dividend / divisor;
  1208. if (result > NumericLimits<i32>::max() || result < NumericLimits<i32>::min()) {
  1209. warn() << "Divide overflow";
  1210. TODO();
  1211. }
  1212. auto original_eax = eax();
  1213. set_eax(shadow_wrap_with_taint_from<u32>(result, original_eax, edx(), divisor_with_shadow));
  1214. set_edx(shadow_wrap_with_taint_from<u32>(dividend % divisor, original_eax, edx(), divisor_with_shadow));
  1215. }
  1216. void SoftCPU::IDIV_RM8(const X86::Instruction& insn)
  1217. {
  1218. auto divisor_with_shadow = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1219. auto divisor = (i8)divisor_with_shadow.value();
  1220. if (divisor == 0) {
  1221. warn() << "Divide by zero";
  1222. TODO();
  1223. }
  1224. i16 dividend = ax().value();
  1225. i16 result = dividend / divisor;
  1226. if (result > NumericLimits<i8>::max() || result < NumericLimits<i8>::min()) {
  1227. warn() << "Divide overflow";
  1228. TODO();
  1229. }
  1230. auto original_ax = ax();
  1231. set_al(shadow_wrap_with_taint_from<u8>(result, divisor_with_shadow, original_ax));
  1232. set_ah(shadow_wrap_with_taint_from<u8>(dividend % divisor, divisor_with_shadow, original_ax));
  1233. }
  1234. void SoftCPU::IMUL_RM16(const X86::Instruction& insn)
  1235. {
  1236. i16 result_high;
  1237. i16 result_low;
  1238. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1239. op_imul<i16>(*this, src.value(), ax().value(), result_high, result_low);
  1240. gpr16(X86::RegisterDX) = shadow_wrap_with_taint_from<u16>(result_high, src, ax());
  1241. gpr16(X86::RegisterAX) = shadow_wrap_with_taint_from<u16>(result_low, src, ax());
  1242. }
  1243. void SoftCPU::IMUL_RM32(const X86::Instruction& insn)
  1244. {
  1245. i32 result_high;
  1246. i32 result_low;
  1247. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1248. op_imul<i32>(*this, src.value(), eax().value(), result_high, result_low);
  1249. gpr32(X86::RegisterEDX) = shadow_wrap_with_taint_from<u32>(result_high, src, eax());
  1250. gpr32(X86::RegisterEAX) = shadow_wrap_with_taint_from<u32>(result_low, src, eax());
  1251. }
  1252. void SoftCPU::IMUL_RM8(const X86::Instruction& insn)
  1253. {
  1254. i8 result_high;
  1255. i8 result_low;
  1256. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1257. op_imul<i8>(*this, src.value(), al().value(), result_high, result_low);
  1258. gpr8(X86::RegisterAH) = shadow_wrap_with_taint_from<u8>(result_high, src, al());
  1259. gpr8(X86::RegisterAL) = shadow_wrap_with_taint_from<u8>(result_low, src, al());
  1260. }
  1261. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  1262. {
  1263. i16 result_high;
  1264. i16 result_low;
  1265. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1266. op_imul<i16>(*this, gpr16(insn.reg16()).value(), src.value(), result_high, result_low);
  1267. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src, gpr16(insn.reg16()));
  1268. }
  1269. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  1270. {
  1271. i16 result_high;
  1272. i16 result_low;
  1273. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1274. op_imul<i16>(*this, src.value(), insn.imm16(), result_high, result_low);
  1275. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src);
  1276. }
  1277. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  1278. {
  1279. i16 result_high;
  1280. i16 result_low;
  1281. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1282. op_imul<i16>(*this, src.value(), sign_extended_to<i16>(insn.imm8()), result_high, result_low);
  1283. gpr16(insn.reg16()) = shadow_wrap_with_taint_from<u16>(result_low, src);
  1284. }
  1285. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  1286. {
  1287. i32 result_high;
  1288. i32 result_low;
  1289. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1290. op_imul<i32>(*this, gpr32(insn.reg32()).value(), src.value(), result_high, result_low);
  1291. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src, gpr32(insn.reg32()));
  1292. }
  1293. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  1294. {
  1295. i32 result_high;
  1296. i32 result_low;
  1297. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1298. op_imul<i32>(*this, src.value(), insn.imm32(), result_high, result_low);
  1299. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src);
  1300. }
  1301. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  1302. {
  1303. i32 result_high;
  1304. i32 result_low;
  1305. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1306. op_imul<i32>(*this, src.value(), sign_extended_to<i32>(insn.imm8()), result_high, result_low);
  1307. gpr32(insn.reg32()) = shadow_wrap_with_taint_from<u32>(result_low, src);
  1308. }
  1309. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  1310. {
  1311. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16<ValueWithShadow<u16>>(*this, insn)));
  1312. }
  1313. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  1314. {
  1315. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32<ValueWithShadow<u32>>(*this, insn)));
  1316. }
  1317. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  1318. {
  1319. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8<ValueWithShadow<u8>>(*this, insn)));
  1320. }
  1321. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  1322. {
  1323. gpr16(insn.reg16()) = op_inc(*this, const_gpr16(insn.reg16()));
  1324. }
  1325. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  1326. {
  1327. gpr32(insn.reg32()) = op_inc(*this, const_gpr32(insn.reg32()));
  1328. }
  1329. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  1330. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  1331. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  1332. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  1333. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  1334. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  1335. {
  1336. ASSERT(insn.imm8() == 0x82);
  1337. // FIXME: virt_syscall should take ValueWithShadow and whine about uninitialized arguments
  1338. set_eax(shadow_wrap_as_initialized(m_emulator.virt_syscall(eax().value(), edx().value(), ecx().value(), ebx().value())));
  1339. }
  1340. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  1341. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  1342. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  1343. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  1344. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  1345. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  1346. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  1347. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  1348. void SoftCPU::JCXZ_imm8(const X86::Instruction& insn)
  1349. {
  1350. if (insn.a32()) {
  1351. warn_if_uninitialized(ecx(), "jecxz imm8");
  1352. if (ecx().value() == 0)
  1353. set_eip(eip() + (i8)insn.imm8());
  1354. } else {
  1355. warn_if_uninitialized(cx(), "jcxz imm8");
  1356. if (cx().value() == 0)
  1357. set_eip(eip() + (i8)insn.imm8());
  1358. }
  1359. }
  1360. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  1361. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  1362. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  1363. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  1364. {
  1365. set_eip(insn.modrm().read32<ValueWithShadow<u32>>(*this, insn).value());
  1366. }
  1367. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  1368. {
  1369. set_eip(eip() + (i16)insn.imm16());
  1370. }
  1371. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  1372. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  1373. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  1374. {
  1375. set_eip(eip() + (i32)insn.imm32());
  1376. }
  1377. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  1378. {
  1379. set_eip(eip() + (i8)insn.imm8());
  1380. }
  1381. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  1382. {
  1383. warn_if_flags_tainted("jcc near imm32");
  1384. if (evaluate_condition(insn.cc()))
  1385. set_eip(eip() + (i32)insn.imm32());
  1386. }
  1387. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  1388. {
  1389. warn_if_flags_tainted("jcc imm8");
  1390. if (evaluate_condition(insn.cc()))
  1391. set_eip(eip() + (i8)insn.imm8());
  1392. }
  1393. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  1394. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  1395. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  1396. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1397. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1398. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  1399. void SoftCPU::LEAVE32(const X86::Instruction&)
  1400. {
  1401. auto new_ebp = read_memory32({ ss(), ebp().value() });
  1402. set_esp({ ebp().value() + 4, ebp().shadow() });
  1403. set_ebp(new_ebp);
  1404. }
  1405. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  1406. {
  1407. // FIXME: Respect shadow values
  1408. gpr16(insn.reg16()) = shadow_wrap_as_initialized<u16>(insn.modrm().resolve(*this, insn).offset());
  1409. }
  1410. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  1411. {
  1412. // FIXME: Respect shadow values
  1413. gpr32(insn.reg32()) = shadow_wrap_as_initialized<u32>(insn.modrm().resolve(*this, insn).offset());
  1414. }
  1415. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  1416. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  1417. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1418. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1419. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  1420. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1421. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1422. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  1423. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  1424. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  1425. template<typename T>
  1426. ALWAYS_INLINE static void do_lods(SoftCPU& cpu, const X86::Instruction& insn)
  1427. {
  1428. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1429. cpu.do_once_or_repeat<true>(insn, [&] {
  1430. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.a32()).value() });
  1431. cpu.gpr<T>(X86::RegisterAL) = src;
  1432. cpu.step_source_index(insn.a32(), sizeof(T));
  1433. });
  1434. }
  1435. void SoftCPU::LODSB(const X86::Instruction& insn)
  1436. {
  1437. do_lods<u8>(*this, insn);
  1438. }
  1439. void SoftCPU::LODSD(const X86::Instruction& insn)
  1440. {
  1441. do_lods<u32>(*this, insn);
  1442. }
  1443. void SoftCPU::LODSW(const X86::Instruction& insn)
  1444. {
  1445. do_lods<u16>(*this, insn);
  1446. }
  1447. void SoftCPU::LOOPNZ_imm8(const X86::Instruction& insn)
  1448. {
  1449. warn_if_flags_tainted("loopnz");
  1450. if (insn.a32()) {
  1451. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1452. if (ecx().value() != 0 && !zf())
  1453. set_eip(eip() + (i8)insn.imm8());
  1454. } else {
  1455. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1456. if (cx().value() != 0 && !zf())
  1457. set_eip(eip() + (i8)insn.imm8());
  1458. }
  1459. }
  1460. void SoftCPU::LOOPZ_imm8(const X86::Instruction& insn)
  1461. {
  1462. warn_if_flags_tainted("loopz");
  1463. if (insn.a32()) {
  1464. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1465. if (ecx().value() != 0 && zf())
  1466. set_eip(eip() + (i8)insn.imm8());
  1467. } else {
  1468. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1469. if (cx().value() != 0 && zf())
  1470. set_eip(eip() + (i8)insn.imm8());
  1471. }
  1472. }
  1473. void SoftCPU::LOOP_imm8(const X86::Instruction& insn)
  1474. {
  1475. if (insn.a32()) {
  1476. set_ecx({ ecx().value() - 1, ecx().shadow() });
  1477. if (ecx().value() != 0)
  1478. set_eip(eip() + (i8)insn.imm8());
  1479. } else {
  1480. set_cx({ (u16)(cx().value() - 1), cx().shadow() });
  1481. if (cx().value() != 0)
  1482. set_eip(eip() + (i8)insn.imm8());
  1483. }
  1484. }
  1485. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  1486. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  1487. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1488. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1489. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  1490. template<typename T>
  1491. ALWAYS_INLINE static void do_movs(SoftCPU& cpu, const X86::Instruction& insn)
  1492. {
  1493. auto src_segment = cpu.segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1494. cpu.do_once_or_repeat<false>(insn, [&] {
  1495. auto src = cpu.read_memory<T>({ src_segment, cpu.source_index(insn.a32()).value() });
  1496. cpu.write_memory<T>({ cpu.es(), cpu.destination_index(insn.a32()).value() }, src);
  1497. cpu.step_source_index(insn.a32(), sizeof(T));
  1498. cpu.step_destination_index(insn.a32(), sizeof(T));
  1499. });
  1500. }
  1501. void SoftCPU::MOVSB(const X86::Instruction& insn)
  1502. {
  1503. do_movs<u8>(*this, insn);
  1504. }
  1505. void SoftCPU::MOVSD(const X86::Instruction& insn)
  1506. {
  1507. do_movs<u32>(*this, insn);
  1508. }
  1509. void SoftCPU::MOVSW(const X86::Instruction& insn)
  1510. {
  1511. do_movs<u16>(*this, insn);
  1512. }
  1513. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1514. {
  1515. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1516. gpr16(insn.reg16()) = ValueWithShadow<u16>(sign_extended_to<u16>(src.value()), 0x0100 | (src.shadow()));
  1517. }
  1518. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1519. {
  1520. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1521. gpr32(insn.reg32()) = ValueWithShadow<u32>(sign_extended_to<u32>(src.value()), 0x01010000 | (src.shadow()));
  1522. }
  1523. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1524. {
  1525. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1526. gpr32(insn.reg32()) = ValueWithShadow<u32>(sign_extended_to<u32>(src.value()), 0x01010100 | (src.shadow()));
  1527. }
  1528. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1529. {
  1530. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1531. gpr16(insn.reg16()) = ValueWithShadow<u16>(src.value(), 0x0100 | (src.shadow() & 0xff));
  1532. }
  1533. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1534. {
  1535. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1536. gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010000 | (src.shadow() & 0xffff));
  1537. }
  1538. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1539. {
  1540. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1541. gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010100 | (src.shadow() & 0xff));
  1542. }
  1543. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1544. {
  1545. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1546. }
  1547. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1548. {
  1549. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1550. }
  1551. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1552. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1553. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1554. {
  1555. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1556. }
  1557. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1558. {
  1559. insn.modrm().write16(*this, insn, shadow_wrap_as_initialized(insn.imm16()));
  1560. }
  1561. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1562. {
  1563. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  1564. }
  1565. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1566. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1567. {
  1568. insn.modrm().write32(*this, insn, shadow_wrap_as_initialized(insn.imm32()));
  1569. }
  1570. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1571. {
  1572. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  1573. }
  1574. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1575. {
  1576. insn.modrm().write8(*this, insn, shadow_wrap_as_initialized(insn.imm8()));
  1577. }
  1578. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1579. {
  1580. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  1581. }
  1582. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1583. {
  1584. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1585. }
  1586. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1587. {
  1588. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1589. }
  1590. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1591. {
  1592. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1593. }
  1594. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1595. {
  1596. gpr16(insn.reg16()) = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1597. }
  1598. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1599. {
  1600. gpr16(insn.reg16()) = shadow_wrap_as_initialized(insn.imm16());
  1601. }
  1602. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1603. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1604. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1605. {
  1606. gpr32(insn.reg32()) = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1607. }
  1608. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1609. {
  1610. gpr32(insn.reg32()) = shadow_wrap_as_initialized(insn.imm32());
  1611. }
  1612. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1613. {
  1614. gpr8(insn.reg8()) = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1615. }
  1616. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1617. {
  1618. gpr8(insn.reg8()) = shadow_wrap_as_initialized(insn.imm8());
  1619. }
  1620. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1621. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1622. void SoftCPU::MUL_RM16(const X86::Instruction& insn)
  1623. {
  1624. auto src = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1625. u32 result = (u32)ax().value() * (u32)src.value();
  1626. auto original_ax = ax();
  1627. set_ax(shadow_wrap_with_taint_from<u16>(result & 0xffff, src, original_ax));
  1628. set_dx(shadow_wrap_with_taint_from<u16>(result >> 16, src, original_ax));
  1629. taint_flags_from(src, original_ax);
  1630. set_cf(dx().value() != 0);
  1631. set_of(dx().value() != 0);
  1632. }
  1633. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1634. {
  1635. auto src = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1636. u64 result = (u64)eax().value() * (u64)src.value();
  1637. auto original_eax = eax();
  1638. set_eax(shadow_wrap_with_taint_from<u32>(result, src, original_eax));
  1639. set_edx(shadow_wrap_with_taint_from<u32>(result >> 32, src, original_eax));
  1640. taint_flags_from(src, original_eax);
  1641. set_cf(edx().value() != 0);
  1642. set_of(edx().value() != 0);
  1643. }
  1644. void SoftCPU::MUL_RM8(const X86::Instruction& insn)
  1645. {
  1646. auto src = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1647. u16 result = (u16)al().value() * src.value();
  1648. auto original_al = al();
  1649. set_ax(shadow_wrap_with_taint_from(result, src, original_al));
  1650. taint_flags_from(src, original_al);
  1651. set_cf((result & 0xff00) != 0);
  1652. set_of((result & 0xff00) != 0);
  1653. }
  1654. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1655. {
  1656. insn.modrm().write16(*this, insn, op_sub<ValueWithShadow<u16>>(*this, shadow_wrap_as_initialized<u16>(0), insn.modrm().read16<ValueWithShadow<u16>>(*this, insn)));
  1657. }
  1658. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1659. {
  1660. insn.modrm().write32(*this, insn, op_sub<ValueWithShadow<u32>>(*this, shadow_wrap_as_initialized<u32>(0), insn.modrm().read32<ValueWithShadow<u32>>(*this, insn)));
  1661. }
  1662. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1663. {
  1664. insn.modrm().write8(*this, insn, op_sub<ValueWithShadow<u8>>(*this, shadow_wrap_as_initialized<u8>(0), insn.modrm().read8<ValueWithShadow<u8>>(*this, insn)));
  1665. }
  1666. void SoftCPU::NOP(const X86::Instruction&)
  1667. {
  1668. }
  1669. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1670. {
  1671. auto data = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  1672. insn.modrm().write16(*this, insn, ValueWithShadow<u16>(~data.value(), data.shadow()));
  1673. }
  1674. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1675. {
  1676. auto data = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  1677. insn.modrm().write32(*this, insn, ValueWithShadow<u32>(~data.value(), data.shadow()));
  1678. }
  1679. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1680. {
  1681. auto data = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  1682. insn.modrm().write8(*this, insn, ValueWithShadow<u8>(~data.value(), data.shadow()));
  1683. }
  1684. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1685. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1686. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1687. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1688. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1689. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1690. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1691. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1692. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1693. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1694. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1695. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1696. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1697. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1698. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1699. void SoftCPU::POPFD(const X86::Instruction&)
  1700. {
  1701. auto popped_value = pop32();
  1702. m_eflags &= ~0x00fcffff;
  1703. m_eflags |= popped_value.value() & 0x00fcffff;
  1704. taint_flags_from(popped_value);
  1705. }
  1706. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1707. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1708. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1709. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1710. void SoftCPU::POP_RM16(const X86::Instruction& insn)
  1711. {
  1712. insn.modrm().write16(*this, insn, pop16());
  1713. }
  1714. void SoftCPU::POP_RM32(const X86::Instruction& insn)
  1715. {
  1716. insn.modrm().write32(*this, insn, pop32());
  1717. }
  1718. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1719. void SoftCPU::POP_reg16(const X86::Instruction& insn)
  1720. {
  1721. gpr16(insn.reg16()) = pop16();
  1722. }
  1723. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1724. {
  1725. gpr32(insn.reg32()) = pop32();
  1726. }
  1727. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1728. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1729. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1730. void SoftCPU::PUSHFD(const X86::Instruction&)
  1731. {
  1732. // FIXME: Respect shadow flags when they exist!
  1733. push32(shadow_wrap_as_initialized(m_eflags & 0x00fcffff));
  1734. }
  1735. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1736. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1737. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1738. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1739. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1740. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1741. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1742. {
  1743. push32(insn.modrm().read32<ValueWithShadow<u32>>(*this, insn));
  1744. }
  1745. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1746. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1747. void SoftCPU::PUSH_imm16(const X86::Instruction& insn)
  1748. {
  1749. push16(shadow_wrap_as_initialized(insn.imm16()));
  1750. }
  1751. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1752. {
  1753. push32(shadow_wrap_as_initialized(insn.imm32()));
  1754. }
  1755. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1756. {
  1757. ASSERT(!insn.has_operand_size_override_prefix());
  1758. push32(shadow_wrap_as_initialized<u32>(sign_extended_to<i32>(insn.imm8())));
  1759. }
  1760. void SoftCPU::PUSH_reg16(const X86::Instruction& insn)
  1761. {
  1762. push16(gpr16(insn.reg16()));
  1763. }
  1764. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1765. {
  1766. push32(gpr32(insn.reg32()));
  1767. if (m_secret_handshake_state == 2) {
  1768. m_secret_data[0] = gpr32(insn.reg32()).value();
  1769. ++m_secret_handshake_state;
  1770. } else if (m_secret_handshake_state == 3) {
  1771. m_secret_data[1] = gpr32(insn.reg32()).value();
  1772. ++m_secret_handshake_state;
  1773. } else if (m_secret_handshake_state == 4) {
  1774. m_secret_data[2] = gpr32(insn.reg32()).value();
  1775. m_secret_handshake_state = 0;
  1776. did_receive_secret_data();
  1777. }
  1778. }
  1779. template<typename T, bool cf>
  1780. ALWAYS_INLINE static T op_rcl_impl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1781. {
  1782. if (steps.value() == 0)
  1783. return shadow_wrap_with_taint_from(data.value(), data, steps);
  1784. u32 result = 0;
  1785. u32 new_flags = 0;
  1786. if constexpr (cf)
  1787. asm volatile("stc");
  1788. else
  1789. asm volatile("clc");
  1790. if constexpr (sizeof(typename T::ValueType) == 4) {
  1791. asm volatile("rcll %%cl, %%eax\n"
  1792. : "=a"(result)
  1793. : "a"(data.value()), "c"(steps.value()));
  1794. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  1795. asm volatile("rclw %%cl, %%ax\n"
  1796. : "=a"(result)
  1797. : "a"(data.value()), "c"(steps.value()));
  1798. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  1799. asm volatile("rclb %%cl, %%al\n"
  1800. : "=a"(result)
  1801. : "a"(data.value()), "c"(steps.value()));
  1802. }
  1803. asm volatile(
  1804. "pushf\n"
  1805. "pop %%ebx"
  1806. : "=b"(new_flags));
  1807. cpu.set_flags_oc(new_flags);
  1808. cpu.taint_flags_from(data, steps);
  1809. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  1810. }
  1811. template<typename T>
  1812. ALWAYS_INLINE static T op_rcl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1813. {
  1814. cpu.warn_if_flags_tainted("rcl");
  1815. if (cpu.cf())
  1816. return op_rcl_impl<T, true>(cpu, data, steps);
  1817. return op_rcl_impl<T, false>(cpu, data, steps);
  1818. }
  1819. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCL, op_rcl)
  1820. template<typename T, bool cf>
  1821. ALWAYS_INLINE static T op_rcr_impl(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1822. {
  1823. if (steps.value() == 0)
  1824. return shadow_wrap_with_taint_from(data.value(), data, steps);
  1825. u32 result = 0;
  1826. u32 new_flags = 0;
  1827. if constexpr (cf)
  1828. asm volatile("stc");
  1829. else
  1830. asm volatile("clc");
  1831. if constexpr (sizeof(typename T::ValueType) == 4) {
  1832. asm volatile("rcrl %%cl, %%eax\n"
  1833. : "=a"(result)
  1834. : "a"(data.value()), "c"(steps.value()));
  1835. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  1836. asm volatile("rcrw %%cl, %%ax\n"
  1837. : "=a"(result)
  1838. : "a"(data.value()), "c"(steps.value()));
  1839. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  1840. asm volatile("rcrb %%cl, %%al\n"
  1841. : "=a"(result)
  1842. : "a"(data.value()), "c"(steps.value()));
  1843. }
  1844. asm volatile(
  1845. "pushf\n"
  1846. "pop %%ebx"
  1847. : "=b"(new_flags));
  1848. cpu.set_flags_oc(new_flags);
  1849. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  1850. }
  1851. template<typename T>
  1852. ALWAYS_INLINE static T op_rcr(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1853. {
  1854. cpu.warn_if_flags_tainted("rcr");
  1855. if (cpu.cf())
  1856. return op_rcr_impl<T, true>(cpu, data, steps);
  1857. return op_rcr_impl<T, false>(cpu, data, steps);
  1858. }
  1859. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCR, op_rcr)
  1860. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1861. void SoftCPU::RET(const X86::Instruction& insn)
  1862. {
  1863. ASSERT(!insn.has_operand_size_override_prefix());
  1864. auto ret_address = pop32();
  1865. warn_if_uninitialized(ret_address, "ret");
  1866. set_eip(ret_address.value());
  1867. }
  1868. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1869. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1870. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1871. {
  1872. ASSERT(!insn.has_operand_size_override_prefix());
  1873. auto ret_address = pop32();
  1874. warn_if_uninitialized(ret_address, "ret imm16");
  1875. set_eip(ret_address.value());
  1876. set_esp({ esp().value() + insn.imm16(), esp().shadow() });
  1877. }
  1878. template<typename T>
  1879. ALWAYS_INLINE static T op_rol(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1880. {
  1881. if (steps.value() == 0)
  1882. return shadow_wrap_with_taint_from(data.value(), data, steps);
  1883. u32 result = 0;
  1884. u32 new_flags = 0;
  1885. if constexpr (sizeof(typename T::ValueType) == 4) {
  1886. asm volatile("roll %%cl, %%eax\n"
  1887. : "=a"(result)
  1888. : "a"(data.value()), "c"(steps.value()));
  1889. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  1890. asm volatile("rolw %%cl, %%ax\n"
  1891. : "=a"(result)
  1892. : "a"(data.value()), "c"(steps.value()));
  1893. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  1894. asm volatile("rolb %%cl, %%al\n"
  1895. : "=a"(result)
  1896. : "a"(data.value()), "c"(steps.value()));
  1897. }
  1898. asm volatile(
  1899. "pushf\n"
  1900. "pop %%ebx"
  1901. : "=b"(new_flags));
  1902. cpu.set_flags_oc(new_flags);
  1903. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  1904. }
  1905. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROL, op_rol)
  1906. template<typename T>
  1907. ALWAYS_INLINE static T op_ror(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1908. {
  1909. if (steps.value() == 0)
  1910. return shadow_wrap_with_taint_from(data.value(), data, steps);
  1911. u32 result = 0;
  1912. u32 new_flags = 0;
  1913. if constexpr (sizeof(typename T::ValueType) == 4) {
  1914. asm volatile("rorl %%cl, %%eax\n"
  1915. : "=a"(result)
  1916. : "a"(data.value()), "c"(steps.value()));
  1917. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  1918. asm volatile("rorw %%cl, %%ax\n"
  1919. : "=a"(result)
  1920. : "a"(data.value()), "c"(steps.value()));
  1921. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  1922. asm volatile("rorb %%cl, %%al\n"
  1923. : "=a"(result)
  1924. : "a"(data.value()), "c"(steps.value()));
  1925. }
  1926. asm volatile(
  1927. "pushf\n"
  1928. "pop %%ebx"
  1929. : "=b"(new_flags));
  1930. cpu.set_flags_oc(new_flags);
  1931. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  1932. }
  1933. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROR, op_ror)
  1934. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1935. void SoftCPU::SALC(const X86::Instruction&)
  1936. {
  1937. // FIXME: Respect shadow flags once they exists!
  1938. set_al(shadow_wrap_as_initialized<u8>(cf() ? 0xff : 0x00));
  1939. if (m_secret_handshake_state < 2)
  1940. ++m_secret_handshake_state;
  1941. else
  1942. m_secret_handshake_state = 0;
  1943. }
  1944. template<typename T>
  1945. static T op_sar(SoftCPU& cpu, T data, ValueWithShadow<u8> steps)
  1946. {
  1947. if (steps.value() == 0)
  1948. return shadow_wrap_with_taint_from(data.value(), data, steps);
  1949. u32 result = 0;
  1950. u32 new_flags = 0;
  1951. if constexpr (sizeof(typename T::ValueType) == 4) {
  1952. asm volatile("sarl %%cl, %%eax\n"
  1953. : "=a"(result)
  1954. : "a"(data.value()), "c"(steps.value()));
  1955. } else if constexpr (sizeof(typename T::ValueType) == 2) {
  1956. asm volatile("sarw %%cl, %%ax\n"
  1957. : "=a"(result)
  1958. : "a"(data.value()), "c"(steps.value()));
  1959. } else if constexpr (sizeof(typename T::ValueType) == 1) {
  1960. asm volatile("sarb %%cl, %%al\n"
  1961. : "=a"(result)
  1962. : "a"(data.value()), "c"(steps.value()));
  1963. }
  1964. asm volatile(
  1965. "pushf\n"
  1966. "pop %%ebx"
  1967. : "=b"(new_flags));
  1968. cpu.set_flags_oszapc(new_flags);
  1969. return shadow_wrap_with_taint_from<typename T::ValueType>(result, data, steps);
  1970. }
  1971. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SAR, op_sar)
  1972. template<typename T>
  1973. ALWAYS_INLINE static void do_scas(SoftCPU& cpu, const X86::Instruction& insn)
  1974. {
  1975. cpu.do_once_or_repeat<true>(insn, [&] {
  1976. auto src = cpu.const_gpr<T>(X86::RegisterAL);
  1977. auto dest = cpu.read_memory<T>({ cpu.es(), cpu.destination_index(insn.a32()).value() });
  1978. op_sub(cpu, dest, src);
  1979. cpu.step_destination_index(insn.a32(), sizeof(T));
  1980. });
  1981. }
  1982. void SoftCPU::SCASB(const X86::Instruction& insn)
  1983. {
  1984. do_scas<u8>(*this, insn);
  1985. }
  1986. void SoftCPU::SCASD(const X86::Instruction& insn)
  1987. {
  1988. do_scas<u32>(*this, insn);
  1989. }
  1990. void SoftCPU::SCASW(const X86::Instruction& insn)
  1991. {
  1992. do_scas<u16>(*this, insn);
  1993. }
  1994. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1995. {
  1996. warn_if_flags_tainted("setcc");
  1997. insn.modrm().write8(*this, insn, shadow_wrap_as_initialized<u8>(evaluate_condition(insn.cc())));
  1998. }
  1999. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  2000. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction& insn)
  2001. {
  2002. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16<ValueWithShadow<u16>>(*this, insn), const_gpr16(insn.reg16()), cl()));
  2003. }
  2004. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction& insn)
  2005. {
  2006. 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())));
  2007. }
  2008. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction& insn)
  2009. {
  2010. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32<ValueWithShadow<u32>>(*this, insn), const_gpr32(insn.reg32()), cl()));
  2011. }
  2012. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction& insn)
  2013. {
  2014. 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())));
  2015. }
  2016. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHL, op_shl)
  2017. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction& insn)
  2018. {
  2019. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16<ValueWithShadow<u16>>(*this, insn), const_gpr16(insn.reg16()), cl()));
  2020. }
  2021. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction& insn)
  2022. {
  2023. 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())));
  2024. }
  2025. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction& insn)
  2026. {
  2027. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32<ValueWithShadow<u32>>(*this, insn), const_gpr32(insn.reg32()), cl()));
  2028. }
  2029. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction& insn)
  2030. {
  2031. 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())));
  2032. }
  2033. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHR, op_shr)
  2034. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  2035. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  2036. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  2037. void SoftCPU::STC(const X86::Instruction&)
  2038. {
  2039. set_cf(true);
  2040. }
  2041. void SoftCPU::STD(const X86::Instruction&)
  2042. {
  2043. set_df(true);
  2044. }
  2045. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  2046. void SoftCPU::STOSB(const X86::Instruction& insn)
  2047. {
  2048. do_once_or_repeat<false>(insn, [&] {
  2049. write_memory8({ es(), destination_index(insn.a32()).value() }, al());
  2050. step_destination_index(insn.a32(), 1);
  2051. });
  2052. }
  2053. void SoftCPU::STOSD(const X86::Instruction& insn)
  2054. {
  2055. do_once_or_repeat<false>(insn, [&] {
  2056. write_memory32({ es(), destination_index(insn.a32()).value() }, eax());
  2057. step_destination_index(insn.a32(), 4);
  2058. });
  2059. }
  2060. void SoftCPU::STOSW(const X86::Instruction& insn)
  2061. {
  2062. do_once_or_repeat<false>(insn, [&] {
  2063. write_memory16({ es(), destination_index(insn.a32()).value() }, ax());
  2064. step_destination_index(insn.a32(), 2);
  2065. });
  2066. }
  2067. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  2068. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  2069. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  2070. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  2071. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  2072. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  2073. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  2074. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  2075. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  2076. {
  2077. auto dest = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  2078. auto src = const_gpr16(insn.reg16());
  2079. auto result = op_add(*this, dest, src);
  2080. gpr16(insn.reg16()) = dest;
  2081. insn.modrm().write16(*this, insn, result);
  2082. }
  2083. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  2084. {
  2085. auto dest = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  2086. auto src = const_gpr32(insn.reg32());
  2087. auto result = op_add(*this, dest, src);
  2088. gpr32(insn.reg32()) = dest;
  2089. insn.modrm().write32(*this, insn, result);
  2090. }
  2091. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  2092. {
  2093. auto dest = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  2094. auto src = const_gpr8(insn.reg8());
  2095. auto result = op_add(*this, dest, src);
  2096. gpr8(insn.reg8()) = dest;
  2097. insn.modrm().write8(*this, insn, result);
  2098. }
  2099. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  2100. {
  2101. auto temp = gpr16(insn.reg16());
  2102. gpr16(insn.reg16()) = ax();
  2103. set_ax(temp);
  2104. }
  2105. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  2106. {
  2107. auto temp = gpr32(insn.reg32());
  2108. gpr32(insn.reg32()) = eax();
  2109. set_eax(temp);
  2110. }
  2111. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  2112. {
  2113. auto temp = insn.modrm().read16<ValueWithShadow<u16>>(*this, insn);
  2114. insn.modrm().write16(*this, insn, const_gpr16(insn.reg16()));
  2115. gpr16(insn.reg16()) = temp;
  2116. }
  2117. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  2118. {
  2119. auto temp = insn.modrm().read32<ValueWithShadow<u32>>(*this, insn);
  2120. insn.modrm().write32(*this, insn, const_gpr32(insn.reg32()));
  2121. gpr32(insn.reg32()) = temp;
  2122. }
  2123. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  2124. {
  2125. auto temp = insn.modrm().read8<ValueWithShadow<u8>>(*this, insn);
  2126. insn.modrm().write8(*this, insn, const_gpr8(insn.reg8()));
  2127. gpr8(insn.reg8()) = temp;
  2128. }
  2129. void SoftCPU::XLAT(const X86::Instruction& insn)
  2130. {
  2131. if (insn.a32())
  2132. warn_if_uninitialized(ebx(), "xlat ebx");
  2133. else
  2134. warn_if_uninitialized(bx(), "xlat bx");
  2135. warn_if_uninitialized(al(), "xlat al");
  2136. u32 offset = (insn.a32() ? ebx().value() : bx().value()) + al().value();
  2137. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), offset }));
  2138. }
  2139. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  2140. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<ValueWithShadow<u8>>, insn); } \
  2141. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<ValueWithShadow<u16>>, insn); } \
  2142. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<ValueWithShadow<u32>>, insn); } \
  2143. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<ValueWithShadow<u16>>, insn); } \
  2144. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<ValueWithShadow<u16>>, insn); } \
  2145. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<ValueWithShadow<u32>>, insn); } \
  2146. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<ValueWithShadow<u32>>, insn); } \
  2147. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<ValueWithShadow<u8>>, insn); } \
  2148. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<ValueWithShadow<u8>>, insn); }
  2149. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  2150. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  2151. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<ValueWithShadow<u16>>, insn); } \
  2152. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<ValueWithShadow<u32>>, insn); } \
  2153. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<ValueWithShadow<u16>>, insn); } \
  2154. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<ValueWithShadow<u32>>, insn); } \
  2155. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<ValueWithShadow<u8>>, insn); }
  2156. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  2157. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  2158. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  2159. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  2160. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  2161. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  2162. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  2163. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  2164. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  2165. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  2166. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  2167. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  2168. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  2169. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  2170. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  2171. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  2172. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  2173. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  2174. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  2175. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  2176. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  2177. }