SoftCPU.cpp 98 KB

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