SoftCPU.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SoftCPU.h"
  27. #include "Emulator.h"
  28. #include <AK/Assertions.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #if defined(__GNUC__) && !defined(__clang__)
  32. # pragma GCC optimize("O3")
  33. #endif
  34. //#define MEMORY_DEBUG
  35. #define DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(mnemonic, op) \
  36. void SoftCPU::mnemonic##_RM8_1(const X86::Instruction& insn) { generic_RM8_1(op<u8>, insn); } \
  37. void SoftCPU::mnemonic##_RM8_CL(const X86::Instruction& insn) { generic_RM8_CL(op<u8>, insn); } \
  38. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<true>(op<u8>, insn); } \
  39. void SoftCPU::mnemonic##_RM16_1(const X86::Instruction& insn) { generic_RM16_1(op<u16>, insn); } \
  40. void SoftCPU::mnemonic##_RM16_CL(const X86::Instruction& insn) { generic_RM16_CL(op<u16>, insn); } \
  41. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<true>(op<u16>, insn); } \
  42. void SoftCPU::mnemonic##_RM32_1(const X86::Instruction& insn) { generic_RM32_1(op<u32>, insn); } \
  43. void SoftCPU::mnemonic##_RM32_CL(const X86::Instruction& insn) { generic_RM32_CL(op<u32>, insn); } \
  44. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<true>(op<u32>, insn); }
  45. namespace UserspaceEmulator {
  46. template<typename T, typename U>
  47. inline constexpr T sign_extended_to(U value)
  48. {
  49. if (!(value & X86::TypeTrivia<U>::sign_bit))
  50. return value;
  51. return (X86::TypeTrivia<T>::mask & ~X86::TypeTrivia<U>::mask) | value;
  52. }
  53. SoftCPU::SoftCPU(Emulator& emulator)
  54. : m_emulator(emulator)
  55. {
  56. memset(m_gpr, 0, sizeof(m_gpr));
  57. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  58. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  59. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  60. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  61. m_segment[(int)X86::SegmentRegister::GS] = 0x28;
  62. }
  63. void SoftCPU::dump() const
  64. {
  65. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  66. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  67. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  68. }
  69. void SoftCPU::did_receive_secret_data()
  70. {
  71. if (m_secret_data[0] == 1) {
  72. if (auto* tracer = m_emulator.malloc_tracer())
  73. tracer->target_did_malloc({}, m_secret_data[2], m_secret_data[1]);
  74. } else if (m_secret_data[0] == 2) {
  75. if (auto* tracer = m_emulator.malloc_tracer())
  76. tracer->target_did_free({}, m_secret_data[1]);
  77. } else {
  78. ASSERT_NOT_REACHED();
  79. }
  80. }
  81. void SoftCPU::update_code_cache()
  82. {
  83. auto* region = m_emulator.mmu().find_region({ cs(), eip() });
  84. ASSERT(region);
  85. m_cached_code_ptr = region->cacheable_ptr(eip() - region->base());
  86. m_cached_code_end = region->cacheable_ptr(region->size());
  87. }
  88. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  89. {
  90. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  91. auto value = m_emulator.mmu().read8(address);
  92. #ifdef MEMORY_DEBUG
  93. printf("\033[36;1mread_memory8: @%08x:%08x -> %02x\033[0m\n", address.selector(), address.offset(), value);
  94. #endif
  95. return value;
  96. }
  97. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  98. {
  99. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  100. auto value = m_emulator.mmu().read16(address);
  101. #ifdef MEMORY_DEBUG
  102. printf("\033[36;1mread_memory16: @%04x:%08x -> %04x\033[0m\n", address.selector(), address.offset(), value);
  103. #endif
  104. return value;
  105. }
  106. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  107. {
  108. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  109. auto value = m_emulator.mmu().read32(address);
  110. #ifdef MEMORY_DEBUG
  111. printf("\033[36;1mread_memory32: @%04x:%08x -> %08x\033[0m\n", address.selector(), address.offset(), value);
  112. #endif
  113. return value;
  114. }
  115. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  116. {
  117. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  118. #ifdef MEMORY_DEBUG
  119. printf("\033[35;1mwrite_memory8: @%04x:%08x <- %02x\033[0m\n", address.selector(), address.offset(), value);
  120. #endif
  121. m_emulator.mmu().write8(address, value);
  122. }
  123. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  124. {
  125. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  126. #ifdef MEMORY_DEBUG
  127. printf("\033[35;1mwrite_memory16: @%04x:%08x <- %04x\033[0m\n", address.selector(), address.offset(), value);
  128. #endif
  129. m_emulator.mmu().write16(address, value);
  130. }
  131. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  132. {
  133. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  134. #ifdef MEMORY_DEBUG
  135. printf("\033[35;1mwrite_memory32: @%04x:%08x <- %08x\033[0m\n", address.selector(), address.offset(), value);
  136. #endif
  137. m_emulator.mmu().write32(address, value);
  138. }
  139. void SoftCPU::push_string(const StringView& string)
  140. {
  141. size_t space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
  142. set_esp(esp() - space_to_allocate);
  143. m_emulator.mmu().copy_to_vm(esp(), string.characters_without_null_termination(), string.length());
  144. m_emulator.mmu().write8({ 0x20, esp() + string.length() }, '\0');
  145. }
  146. void SoftCPU::push32(u32 value)
  147. {
  148. set_esp(esp() - sizeof(value));
  149. write_memory32({ ss(), esp() }, value);
  150. }
  151. u32 SoftCPU::pop32()
  152. {
  153. auto value = read_memory32({ ss(), esp() });
  154. set_esp(esp() + sizeof(value));
  155. return value;
  156. }
  157. template<bool check_zf, typename Callback>
  158. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  159. {
  160. if (!insn.has_rep_prefix())
  161. return callback();
  162. if (insn.has_address_size_override_prefix()) {
  163. while (cx()) {
  164. callback();
  165. set_cx(cx() - 1);
  166. if constexpr (check_zf) {
  167. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  168. break;
  169. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  170. break;
  171. }
  172. }
  173. return;
  174. }
  175. while (ecx()) {
  176. callback();
  177. set_ecx(ecx() - 1);
  178. if constexpr (check_zf) {
  179. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  180. break;
  181. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  182. break;
  183. }
  184. }
  185. }
  186. template<typename T>
  187. ALWAYS_INLINE static T op_inc(SoftCPU& cpu, T data)
  188. {
  189. T result = 0;
  190. u32 new_flags = 0;
  191. if constexpr (sizeof(T) == 4) {
  192. asm volatile("incl %%eax\n"
  193. : "=a"(result)
  194. : "a"(data));
  195. } else if constexpr (sizeof(T) == 2) {
  196. asm volatile("incw %%ax\n"
  197. : "=a"(result)
  198. : "a"(data));
  199. } else if constexpr (sizeof(T) == 1) {
  200. asm volatile("incb %%al\n"
  201. : "=a"(result)
  202. : "a"(data));
  203. }
  204. asm volatile(
  205. "pushf\n"
  206. "pop %%ebx"
  207. : "=b"(new_flags));
  208. cpu.set_flags_oszap(new_flags);
  209. return result;
  210. }
  211. template<typename T>
  212. ALWAYS_INLINE static T op_dec(SoftCPU& cpu, T data)
  213. {
  214. T result = 0;
  215. u32 new_flags = 0;
  216. if constexpr (sizeof(T) == 4) {
  217. asm volatile("decl %%eax\n"
  218. : "=a"(result)
  219. : "a"(data));
  220. } else if constexpr (sizeof(T) == 2) {
  221. asm volatile("decw %%ax\n"
  222. : "=a"(result)
  223. : "a"(data));
  224. } else if constexpr (sizeof(T) == 1) {
  225. asm volatile("decb %%al\n"
  226. : "=a"(result)
  227. : "a"(data));
  228. }
  229. asm volatile(
  230. "pushf\n"
  231. "pop %%ebx"
  232. : "=b"(new_flags));
  233. cpu.set_flags_oszap(new_flags);
  234. return result;
  235. }
  236. template<typename T>
  237. ALWAYS_INLINE static T op_xor(SoftCPU& cpu, const T& dest, const T& src)
  238. {
  239. T result = 0;
  240. u32 new_flags = 0;
  241. if constexpr (sizeof(T) == 4) {
  242. asm volatile("xorl %%ecx, %%eax\n"
  243. : "=a"(result)
  244. : "a"(dest), "c"((u32)src));
  245. } else if constexpr (sizeof(T) == 2) {
  246. asm volatile("xor %%cx, %%ax\n"
  247. : "=a"(result)
  248. : "a"(dest), "c"((u16)src));
  249. } else if constexpr (sizeof(T) == 1) {
  250. asm volatile("xorb %%cl, %%al\n"
  251. : "=a"(result)
  252. : "a"(dest), "c"((u8)src));
  253. } else {
  254. ASSERT_NOT_REACHED();
  255. }
  256. asm volatile(
  257. "pushf\n"
  258. "pop %%ebx"
  259. : "=b"(new_flags));
  260. cpu.set_flags_oszpc(new_flags);
  261. return result;
  262. }
  263. template<typename T>
  264. ALWAYS_INLINE static T op_or(SoftCPU& cpu, const T& dest, const T& src)
  265. {
  266. T result = 0;
  267. u32 new_flags = 0;
  268. if constexpr (sizeof(T) == 4) {
  269. asm volatile("orl %%ecx, %%eax\n"
  270. : "=a"(result)
  271. : "a"(dest), "c"((u32)src));
  272. } else if constexpr (sizeof(T) == 2) {
  273. asm volatile("or %%cx, %%ax\n"
  274. : "=a"(result)
  275. : "a"(dest), "c"((u16)src));
  276. } else if constexpr (sizeof(T) == 1) {
  277. asm volatile("orb %%cl, %%al\n"
  278. : "=a"(result)
  279. : "a"(dest), "c"((u8)src));
  280. } else {
  281. ASSERT_NOT_REACHED();
  282. }
  283. asm volatile(
  284. "pushf\n"
  285. "pop %%ebx"
  286. : "=b"(new_flags));
  287. cpu.set_flags_oszpc(new_flags);
  288. return result;
  289. }
  290. template<typename T>
  291. ALWAYS_INLINE static T op_sub(SoftCPU& cpu, const T& dest, const T& src)
  292. {
  293. T result = 0;
  294. u32 new_flags = 0;
  295. if constexpr (sizeof(T) == 4) {
  296. asm volatile("subl %%ecx, %%eax\n"
  297. : "=a"(result)
  298. : "a"(dest), "c"((u32)src));
  299. } else if constexpr (sizeof(T) == 2) {
  300. asm volatile("subw %%cx, %%ax\n"
  301. : "=a"(result)
  302. : "a"(dest), "c"((u16)src));
  303. } else if constexpr (sizeof(T) == 1) {
  304. asm volatile("subb %%cl, %%al\n"
  305. : "=a"(result)
  306. : "a"(dest), "c"((u8)src));
  307. } else {
  308. ASSERT_NOT_REACHED();
  309. }
  310. asm volatile(
  311. "pushf\n"
  312. "pop %%ebx"
  313. : "=b"(new_flags));
  314. cpu.set_flags_oszapc(new_flags);
  315. return result;
  316. }
  317. template<typename T, bool cf>
  318. ALWAYS_INLINE static T op_sbb_impl(SoftCPU& cpu, const T& dest, const T& src)
  319. {
  320. T result = 0;
  321. u32 new_flags = 0;
  322. if constexpr (cf)
  323. asm volatile("stc");
  324. else
  325. asm volatile("clc");
  326. if constexpr (sizeof(T) == 4) {
  327. asm volatile("sbbl %%ecx, %%eax\n"
  328. : "=a"(result)
  329. : "a"(dest), "c"((u32)src));
  330. } else if constexpr (sizeof(T) == 2) {
  331. asm volatile("sbbw %%cx, %%ax\n"
  332. : "=a"(result)
  333. : "a"(dest), "c"((u16)src));
  334. } else if constexpr (sizeof(T) == 1) {
  335. asm volatile("sbbb %%cl, %%al\n"
  336. : "=a"(result)
  337. : "a"(dest), "c"((u8)src));
  338. } else {
  339. ASSERT_NOT_REACHED();
  340. }
  341. asm volatile(
  342. "pushf\n"
  343. "pop %%ebx"
  344. : "=b"(new_flags));
  345. cpu.set_flags_oszapc(new_flags);
  346. return result;
  347. }
  348. template<typename T>
  349. ALWAYS_INLINE static T op_sbb(SoftCPU& cpu, T& dest, const T& src)
  350. {
  351. if (cpu.cf())
  352. return op_sbb_impl<T, true>(cpu, dest, src);
  353. return op_sbb_impl<T, false>(cpu, dest, src);
  354. }
  355. template<typename T>
  356. ALWAYS_INLINE static T op_add(SoftCPU& cpu, T& dest, const T& src)
  357. {
  358. T result = 0;
  359. u32 new_flags = 0;
  360. if constexpr (sizeof(T) == 4) {
  361. asm volatile("addl %%ecx, %%eax\n"
  362. : "=a"(result)
  363. : "a"(dest), "c"((u32)src));
  364. } else if constexpr (sizeof(T) == 2) {
  365. asm volatile("addw %%cx, %%ax\n"
  366. : "=a"(result)
  367. : "a"(dest), "c"((u16)src));
  368. } else if constexpr (sizeof(T) == 1) {
  369. asm volatile("addb %%cl, %%al\n"
  370. : "=a"(result)
  371. : "a"(dest), "c"((u8)src));
  372. } else {
  373. ASSERT_NOT_REACHED();
  374. }
  375. asm volatile(
  376. "pushf\n"
  377. "pop %%ebx"
  378. : "=b"(new_flags));
  379. cpu.set_flags_oszapc(new_flags);
  380. return result;
  381. }
  382. template<typename T, bool cf>
  383. ALWAYS_INLINE static T op_adc_impl(SoftCPU& cpu, T& dest, const T& src)
  384. {
  385. T result = 0;
  386. u32 new_flags = 0;
  387. if constexpr (cf)
  388. asm volatile("stc");
  389. else
  390. asm volatile("clc");
  391. if constexpr (sizeof(T) == 4) {
  392. asm volatile("adcl %%ecx, %%eax\n"
  393. : "=a"(result)
  394. : "a"(dest), "c"((u32)src));
  395. } else if constexpr (sizeof(T) == 2) {
  396. asm volatile("adcw %%cx, %%ax\n"
  397. : "=a"(result)
  398. : "a"(dest), "c"((u16)src));
  399. } else if constexpr (sizeof(T) == 1) {
  400. asm volatile("adcb %%cl, %%al\n"
  401. : "=a"(result)
  402. : "a"(dest), "c"((u8)src));
  403. } else {
  404. ASSERT_NOT_REACHED();
  405. }
  406. asm volatile(
  407. "pushf\n"
  408. "pop %%ebx"
  409. : "=b"(new_flags));
  410. cpu.set_flags_oszapc(new_flags);
  411. return result;
  412. }
  413. template<typename T>
  414. ALWAYS_INLINE static T op_adc(SoftCPU& cpu, T& dest, const T& src)
  415. {
  416. if (cpu.cf())
  417. return op_adc_impl<T, true>(cpu, dest, src);
  418. return op_adc_impl<T, false>(cpu, dest, src);
  419. }
  420. template<typename T>
  421. ALWAYS_INLINE static T op_and(SoftCPU& cpu, const T& dest, const T& src)
  422. {
  423. T result = 0;
  424. u32 new_flags = 0;
  425. if constexpr (sizeof(T) == 4) {
  426. asm volatile("andl %%ecx, %%eax\n"
  427. : "=a"(result)
  428. : "a"(dest), "c"((u32)src));
  429. } else if constexpr (sizeof(T) == 2) {
  430. asm volatile("andw %%cx, %%ax\n"
  431. : "=a"(result)
  432. : "a"(dest), "c"((u16)src));
  433. } else if constexpr (sizeof(T) == 1) {
  434. asm volatile("andb %%cl, %%al\n"
  435. : "=a"(result)
  436. : "a"(dest), "c"((u8)src));
  437. } else {
  438. ASSERT_NOT_REACHED();
  439. }
  440. asm volatile(
  441. "pushf\n"
  442. "pop %%ebx"
  443. : "=b"(new_flags));
  444. cpu.set_flags_oszpc(new_flags);
  445. return result;
  446. }
  447. template<typename T>
  448. ALWAYS_INLINE static T op_imul(SoftCPU& cpu, const T& dest, const T& src)
  449. {
  450. T result = 0;
  451. u32 new_flags = 0;
  452. if constexpr (sizeof(T) == 4) {
  453. asm volatile("imull %%ecx, %%eax\n"
  454. : "=a"(result)
  455. : "a"(dest), "c"((i32)src));
  456. } else if constexpr (sizeof(T) == 2) {
  457. asm volatile("imulw %%cx, %%ax\n"
  458. : "=a"(result)
  459. : "a"(dest), "c"((i16)src));
  460. } else {
  461. ASSERT_NOT_REACHED();
  462. }
  463. asm volatile(
  464. "pushf\n"
  465. "pop %%ebx"
  466. : "=b"(new_flags));
  467. cpu.set_flags_oszapc(new_flags);
  468. return result;
  469. }
  470. template<typename T>
  471. ALWAYS_INLINE static T op_shr(SoftCPU& cpu, T data, u8 steps)
  472. {
  473. if (steps == 0)
  474. return data;
  475. u32 result = 0;
  476. u32 new_flags = 0;
  477. if constexpr (sizeof(T) == 4) {
  478. asm volatile("shrl %%cl, %%eax\n"
  479. : "=a"(result)
  480. : "a"(data), "c"(steps));
  481. } else if constexpr (sizeof(T) == 2) {
  482. asm volatile("shrw %%cl, %%ax\n"
  483. : "=a"(result)
  484. : "a"(data), "c"(steps));
  485. } else if constexpr (sizeof(T) == 1) {
  486. asm volatile("shrb %%cl, %%al\n"
  487. : "=a"(result)
  488. : "a"(data), "c"(steps));
  489. }
  490. asm volatile(
  491. "pushf\n"
  492. "pop %%ebx"
  493. : "=b"(new_flags));
  494. cpu.set_flags_oszapc(new_flags);
  495. return result;
  496. }
  497. template<typename T>
  498. ALWAYS_INLINE static T op_shl(SoftCPU& cpu, T data, u8 steps)
  499. {
  500. if (steps == 0)
  501. return data;
  502. u32 result = 0;
  503. u32 new_flags = 0;
  504. if constexpr (sizeof(T) == 4) {
  505. asm volatile("shll %%cl, %%eax\n"
  506. : "=a"(result)
  507. : "a"(data), "c"(steps));
  508. } else if constexpr (sizeof(T) == 2) {
  509. asm volatile("shlw %%cl, %%ax\n"
  510. : "=a"(result)
  511. : "a"(data), "c"(steps));
  512. } else if constexpr (sizeof(T) == 1) {
  513. asm volatile("shlb %%cl, %%al\n"
  514. : "=a"(result)
  515. : "a"(data), "c"(steps));
  516. }
  517. asm volatile(
  518. "pushf\n"
  519. "pop %%ebx"
  520. : "=b"(new_flags));
  521. cpu.set_flags_oszapc(new_flags);
  522. return result;
  523. }
  524. template<typename T>
  525. ALWAYS_INLINE static T op_shrd(SoftCPU& cpu, T data, T extra_bits, u8 steps)
  526. {
  527. if (steps == 0)
  528. return data;
  529. u32 result = 0;
  530. u32 new_flags = 0;
  531. if constexpr (sizeof(T) == 4) {
  532. asm volatile("shrd %%cl, %%edx, %%eax\n"
  533. : "=a"(result)
  534. : "a"(data), "d"(extra_bits), "c"(steps));
  535. } else if constexpr (sizeof(T) == 2) {
  536. asm volatile("shrd %%cl, %%dx, %%ax\n"
  537. : "=a"(result)
  538. : "a"(data), "d"(extra_bits), "c"(steps));
  539. }
  540. asm volatile(
  541. "pushf\n"
  542. "pop %%ebx"
  543. : "=b"(new_flags));
  544. cpu.set_flags_oszapc(new_flags);
  545. return result;
  546. }
  547. template<typename T>
  548. ALWAYS_INLINE static T op_shld(SoftCPU& cpu, T data, T extra_bits, u8 steps)
  549. {
  550. if (steps == 0)
  551. return data;
  552. u32 result = 0;
  553. u32 new_flags = 0;
  554. if constexpr (sizeof(T) == 4) {
  555. asm volatile("shld %%cl, %%edx, %%eax\n"
  556. : "=a"(result)
  557. : "a"(data), "d"(extra_bits), "c"(steps));
  558. } else if constexpr (sizeof(T) == 2) {
  559. asm volatile("shld %%cl, %%dx, %%ax\n"
  560. : "=a"(result)
  561. : "a"(data), "d"(extra_bits), "c"(steps));
  562. }
  563. asm volatile(
  564. "pushf\n"
  565. "pop %%ebx"
  566. : "=b"(new_flags));
  567. cpu.set_flags_oszapc(new_flags);
  568. return result;
  569. }
  570. template<bool update_dest, typename Op>
  571. ALWAYS_INLINE void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  572. {
  573. auto dest = al();
  574. auto src = insn.imm8();
  575. auto result = op(*this, dest, src);
  576. if (update_dest)
  577. set_al(result);
  578. }
  579. template<bool update_dest, typename Op>
  580. ALWAYS_INLINE void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  581. {
  582. auto dest = ax();
  583. auto src = insn.imm16();
  584. auto result = op(*this, dest, src);
  585. if (update_dest)
  586. set_ax(result);
  587. }
  588. template<bool update_dest, typename Op>
  589. ALWAYS_INLINE void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  590. {
  591. auto dest = eax();
  592. auto src = insn.imm32();
  593. auto result = op(*this, dest, src);
  594. if (update_dest)
  595. set_eax(result);
  596. }
  597. template<bool update_dest, typename Op>
  598. ALWAYS_INLINE void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  599. {
  600. auto dest = insn.modrm().read16(*this, insn);
  601. auto src = insn.imm16();
  602. auto result = op(*this, dest, src);
  603. if (update_dest)
  604. insn.modrm().write16(*this, insn, result);
  605. }
  606. template<bool update_dest, typename Op>
  607. ALWAYS_INLINE void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  608. {
  609. auto dest = insn.modrm().read16(*this, insn);
  610. auto src = sign_extended_to<u16>(insn.imm8());
  611. auto result = op(*this, dest, src);
  612. if (update_dest)
  613. insn.modrm().write16(*this, insn, result);
  614. }
  615. template<bool update_dest, typename Op>
  616. ALWAYS_INLINE void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  617. {
  618. auto dest = insn.modrm().read16(*this, insn);
  619. auto src = gpr16(insn.reg16());
  620. auto result = op(*this, dest, src);
  621. if (update_dest)
  622. insn.modrm().write16(*this, insn, result);
  623. }
  624. template<bool update_dest, typename Op>
  625. ALWAYS_INLINE void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  626. {
  627. auto dest = insn.modrm().read32(*this, insn);
  628. auto src = insn.imm32();
  629. auto result = op(*this, dest, src);
  630. if (update_dest)
  631. insn.modrm().write32(*this, insn, result);
  632. }
  633. template<bool update_dest, typename Op>
  634. ALWAYS_INLINE void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  635. {
  636. auto dest = insn.modrm().read32(*this, insn);
  637. auto src = sign_extended_to<u32>(insn.imm8());
  638. auto result = op(*this, dest, src);
  639. if (update_dest)
  640. insn.modrm().write32(*this, insn, result);
  641. }
  642. template<bool update_dest, typename Op>
  643. ALWAYS_INLINE void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  644. {
  645. auto dest = insn.modrm().read32(*this, insn);
  646. auto src = gpr32(insn.reg32());
  647. auto result = op(*this, dest, src);
  648. if (update_dest)
  649. insn.modrm().write32(*this, insn, result);
  650. }
  651. template<bool update_dest, typename Op>
  652. ALWAYS_INLINE void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  653. {
  654. auto dest = insn.modrm().read8(*this, insn);
  655. auto src = insn.imm8();
  656. auto result = op(*this, dest, src);
  657. if (update_dest)
  658. insn.modrm().write8(*this, insn, result);
  659. }
  660. template<bool update_dest, typename Op>
  661. ALWAYS_INLINE void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  662. {
  663. auto dest = insn.modrm().read8(*this, insn);
  664. auto src = gpr8(insn.reg8());
  665. auto result = op(*this, dest, src);
  666. if (update_dest)
  667. insn.modrm().write8(*this, insn, result);
  668. }
  669. template<bool update_dest, typename Op>
  670. ALWAYS_INLINE void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  671. {
  672. auto dest = gpr16(insn.reg16());
  673. auto src = insn.modrm().read16(*this, insn);
  674. auto result = op(*this, dest, src);
  675. if (update_dest)
  676. gpr16(insn.reg16()) = result;
  677. }
  678. template<bool update_dest, typename Op>
  679. ALWAYS_INLINE void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  680. {
  681. auto dest = gpr32(insn.reg32());
  682. auto src = insn.modrm().read32(*this, insn);
  683. auto result = op(*this, dest, src);
  684. if (update_dest)
  685. gpr32(insn.reg32()) = result;
  686. }
  687. template<bool update_dest, typename Op>
  688. ALWAYS_INLINE void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  689. {
  690. auto dest = gpr8(insn.reg8());
  691. auto src = insn.modrm().read8(*this, insn);
  692. auto result = op(*this, dest, src);
  693. if (update_dest)
  694. gpr8(insn.reg8()) = result;
  695. }
  696. template<typename Op>
  697. ALWAYS_INLINE void SoftCPU::generic_RM8_1(Op op, const X86::Instruction& insn)
  698. {
  699. auto data = insn.modrm().read8(*this, insn);
  700. insn.modrm().write8(*this, insn, op(*this, data, 1));
  701. }
  702. template<typename Op>
  703. ALWAYS_INLINE void SoftCPU::generic_RM8_CL(Op op, const X86::Instruction& insn)
  704. {
  705. auto data = insn.modrm().read8(*this, insn);
  706. insn.modrm().write8(*this, insn, op(*this, data, cl()));
  707. }
  708. template<typename Op>
  709. ALWAYS_INLINE void SoftCPU::generic_RM16_1(Op op, const X86::Instruction& insn)
  710. {
  711. auto data = insn.modrm().read16(*this, insn);
  712. insn.modrm().write16(*this, insn, op(*this, data, 1));
  713. }
  714. template<typename Op>
  715. ALWAYS_INLINE void SoftCPU::generic_RM16_CL(Op op, const X86::Instruction& insn)
  716. {
  717. auto data = insn.modrm().read16(*this, insn);
  718. insn.modrm().write16(*this, insn, op(*this, data, cl()));
  719. }
  720. template<typename Op>
  721. ALWAYS_INLINE void SoftCPU::generic_RM32_1(Op op, const X86::Instruction& insn)
  722. {
  723. auto data = insn.modrm().read32(*this, insn);
  724. insn.modrm().write32(*this, insn, op(*this, data, 1));
  725. }
  726. template<typename Op>
  727. ALWAYS_INLINE void SoftCPU::generic_RM32_CL(Op op, const X86::Instruction& insn)
  728. {
  729. auto data = insn.modrm().read32(*this, insn);
  730. insn.modrm().write32(*this, insn, op(*this, data, cl()));
  731. }
  732. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  733. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  734. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  735. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  736. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  737. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  738. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  739. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  740. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  741. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  742. void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
  743. {
  744. gpr32(insn.reg32()) = __builtin_bswap32(gpr32(insn.reg32()));
  745. }
  746. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  747. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  748. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  749. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  750. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  751. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  752. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  753. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  754. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  755. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  756. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  757. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  758. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  759. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  760. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  761. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  762. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  763. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  764. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  765. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  766. {
  767. push32(eip());
  768. set_eip(insn.modrm().read32(*this, insn));
  769. }
  770. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  771. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  772. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  773. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  774. {
  775. push32(eip());
  776. set_eip(eip() + (i32)insn.imm32());
  777. }
  778. void SoftCPU::CBW(const X86::Instruction&)
  779. {
  780. set_ah((al() & 0x80) ? 0xff : 0x00);
  781. }
  782. void SoftCPU::CDQ(const X86::Instruction&)
  783. {
  784. if (eax() & 0x80000000)
  785. set_edx(0xffffffff);
  786. else
  787. set_edx(0x00000000);
  788. }
  789. void SoftCPU::CLC(const X86::Instruction&)
  790. {
  791. set_cf(false);
  792. }
  793. void SoftCPU::CLD(const X86::Instruction&)
  794. {
  795. set_df(false);
  796. }
  797. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  800. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  801. {
  802. if (evaluate_condition(insn.cc()))
  803. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  804. }
  805. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  806. {
  807. if (evaluate_condition(insn.cc()))
  808. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  809. }
  810. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  811. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  812. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  813. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  814. {
  815. auto current = insn.modrm().read16(*this, insn);
  816. if (current == eax()) {
  817. set_zf(true);
  818. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  819. } else {
  820. set_zf(false);
  821. set_eax(current);
  822. }
  823. }
  824. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  825. {
  826. auto current = insn.modrm().read32(*this, insn);
  827. if (current == eax()) {
  828. set_zf(true);
  829. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  830. } else {
  831. set_zf(false);
  832. set_eax(current);
  833. }
  834. }
  835. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  836. {
  837. auto current = insn.modrm().read8(*this, insn);
  838. if (current == eax()) {
  839. set_zf(true);
  840. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  841. } else {
  842. set_zf(false);
  843. set_eax(current);
  844. }
  845. }
  846. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  847. void SoftCPU::CWD(const X86::Instruction&)
  848. {
  849. set_dx((ax() & 0x8000) ? 0xffff : 0x0000);
  850. }
  851. void SoftCPU::CWDE(const X86::Instruction&)
  852. {
  853. set_eax(sign_extended_to<u32>(ax()));
  854. }
  855. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  856. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  857. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  858. {
  859. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16(*this, insn)));
  860. }
  861. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  862. {
  863. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32(*this, insn)));
  864. }
  865. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  866. {
  867. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8(*this, insn)));
  868. }
  869. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  870. {
  871. gpr16(insn.reg16()) = op_dec(*this, gpr16(insn.reg16()));
  872. }
  873. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  874. {
  875. gpr32(insn.reg32()) = op_dec(*this, gpr32(insn.reg32()));
  876. }
  877. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  878. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  879. {
  880. auto divisor = insn.modrm().read32(*this, insn);
  881. if (divisor == 0) {
  882. warn() << "Divide by zero";
  883. TODO();
  884. }
  885. u64 dividend = ((u64)edx() << 32) | eax();
  886. auto result = dividend / divisor;
  887. if (result > NumericLimits<u32>::max()) {
  888. warn() << "Divide overflow";
  889. TODO();
  890. }
  891. set_eax(result);
  892. set_edx(dividend % divisor);
  893. }
  894. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  895. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  896. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  897. void SoftCPU::ESCAPE(const X86::Instruction&)
  898. {
  899. dbg() << "FIXME: x87 floating-point support";
  900. m_emulator.dump_backtrace();
  901. TODO();
  902. }
  903. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  904. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  905. void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
  906. {
  907. auto divisor = insn.modrm().read32(*this, insn);
  908. if (divisor == 0) {
  909. warn() << "Divide by zero";
  910. TODO();
  911. }
  912. i64 dividend = ((i64)edx() << 32) | eax();
  913. auto result = dividend / divisor;
  914. if (result > NumericLimits<i32>::max()) {
  915. warn() << "Divide overflow";
  916. TODO();
  917. }
  918. set_eax(result);
  919. set_edx(dividend % divisor);
  920. }
  921. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  922. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  923. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  924. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  925. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  926. {
  927. gpr16(insn.reg16()) = op_imul<i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  928. }
  929. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  930. {
  931. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  932. }
  933. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  934. {
  935. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), sign_extended_to<i16>(insn.imm8()));
  936. }
  937. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  938. {
  939. gpr32(insn.reg32()) = op_imul<i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  940. }
  941. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  942. {
  943. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  944. }
  945. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  946. {
  947. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), sign_extended_to<i32>(insn.imm8()));
  948. }
  949. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  950. {
  951. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  952. }
  953. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  954. {
  955. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  956. }
  957. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  958. {
  959. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  960. }
  961. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  962. {
  963. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  964. }
  965. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  966. {
  967. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  968. }
  969. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  970. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  971. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  972. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  973. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  974. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  975. {
  976. ASSERT(insn.imm8() == 0x82);
  977. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  978. }
  979. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  980. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  981. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  982. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  983. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  984. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  985. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  986. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  987. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  988. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  989. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  990. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  991. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  992. {
  993. set_eip(insn.modrm().read32(*this, insn));
  994. }
  995. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  996. {
  997. set_eip(eip() + (i16)insn.imm16());
  998. }
  999. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  1000. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  1001. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  1002. {
  1003. set_eip(eip() + (i32)insn.imm32());
  1004. }
  1005. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  1006. {
  1007. set_eip(eip() + (i8)insn.imm8());
  1008. }
  1009. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  1010. {
  1011. if (evaluate_condition(insn.cc()))
  1012. set_eip(eip() + (i32)insn.imm32());
  1013. }
  1014. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  1015. {
  1016. if (evaluate_condition(insn.cc()))
  1017. set_eip(eip() + (i8)insn.imm8());
  1018. }
  1019. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  1020. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  1021. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  1022. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1023. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1024. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  1025. void SoftCPU::LEAVE32(const X86::Instruction&)
  1026. {
  1027. u32 new_ebp = read_memory32({ ss(), ebp() });
  1028. set_esp(ebp() + 4);
  1029. set_ebp(new_ebp);
  1030. }
  1031. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  1032. {
  1033. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn).offset();
  1034. }
  1035. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  1036. {
  1037. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn).offset();
  1038. }
  1039. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  1040. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  1041. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1042. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1043. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  1044. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1045. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1046. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  1047. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  1048. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  1049. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  1050. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  1051. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  1052. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  1053. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  1054. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  1055. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  1056. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  1057. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  1058. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  1059. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  1060. void SoftCPU::MOVSB(const X86::Instruction& insn)
  1061. {
  1062. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1063. if (insn.has_address_size_override_prefix()) {
  1064. do_once_or_repeat<false>(insn, [&] {
  1065. auto src = read_memory8({ src_segment, si() });
  1066. write_memory8({ es(), di() }, src);
  1067. set_di(di() + (df() ? -1 : 1));
  1068. set_si(si() + (df() ? -1 : 1));
  1069. });
  1070. } else {
  1071. do_once_or_repeat<false>(insn, [&] {
  1072. auto src = read_memory8({ src_segment, esi() });
  1073. write_memory8({ es(), edi() }, src);
  1074. set_edi(edi() + (df() ? -1 : 1));
  1075. set_esi(esi() + (df() ? -1 : 1));
  1076. });
  1077. }
  1078. }
  1079. void SoftCPU::MOVSD(const X86::Instruction& insn)
  1080. {
  1081. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1082. if (insn.has_address_size_override_prefix()) {
  1083. do_once_or_repeat<false>(insn, [&] {
  1084. auto src = read_memory32({ src_segment, si() });
  1085. write_memory32({ es(), di() }, src);
  1086. set_di(di() + (df() ? -4 : 4));
  1087. set_si(si() + (df() ? -4 : 4));
  1088. });
  1089. } else {
  1090. do_once_or_repeat<false>(insn, [&] {
  1091. auto src = read_memory32({ src_segment, esi() });
  1092. write_memory32({ es(), edi() }, src);
  1093. set_edi(edi() + (df() ? -4 : 4));
  1094. set_esi(esi() + (df() ? -4 : 4));
  1095. });
  1096. }
  1097. }
  1098. void SoftCPU::MOVSW(const X86::Instruction& insn)
  1099. {
  1100. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  1101. if (insn.has_address_size_override_prefix()) {
  1102. do_once_or_repeat<false>(insn, [&] {
  1103. auto src = read_memory16({ src_segment, si() });
  1104. write_memory16({ es(), di() }, src);
  1105. set_di(di() + (df() ? -2 : 2));
  1106. set_si(si() + (df() ? -2 : 2));
  1107. });
  1108. } else {
  1109. do_once_or_repeat<false>(insn, [&] {
  1110. auto src = read_memory16({ src_segment, esi() });
  1111. write_memory16({ es(), edi() }, src);
  1112. set_edi(edi() + (df() ? -2 : 2));
  1113. set_esi(esi() + (df() ? -2 : 2));
  1114. });
  1115. }
  1116. }
  1117. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1118. {
  1119. gpr16(insn.reg16()) = sign_extended_to<u16>(insn.modrm().read8(*this, insn));
  1120. }
  1121. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1122. {
  1123. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read16(*this, insn));
  1124. }
  1125. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1126. {
  1127. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read8(*this, insn));
  1128. }
  1129. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1130. {
  1131. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  1132. }
  1133. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1134. {
  1135. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  1136. }
  1137. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1138. {
  1139. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  1140. }
  1141. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1142. {
  1143. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1144. }
  1145. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1146. {
  1147. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1148. }
  1149. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1150. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1151. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1152. {
  1153. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1154. }
  1155. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1156. {
  1157. insn.modrm().write16(*this, insn, insn.imm16());
  1158. }
  1159. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1160. {
  1161. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1162. }
  1163. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1164. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1165. {
  1166. insn.modrm().write32(*this, insn, insn.imm32());
  1167. }
  1168. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1169. {
  1170. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1171. }
  1172. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1173. {
  1174. insn.modrm().write8(*this, insn, insn.imm8());
  1175. }
  1176. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1177. {
  1178. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1179. }
  1180. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1181. {
  1182. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1183. }
  1184. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1185. {
  1186. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1187. }
  1188. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1189. {
  1190. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1191. }
  1192. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1193. {
  1194. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1195. }
  1196. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1197. {
  1198. gpr16(insn.reg16()) = insn.imm16();
  1199. }
  1200. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1201. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1202. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1203. {
  1204. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1205. }
  1206. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1207. {
  1208. gpr32(insn.reg32()) = insn.imm32();
  1209. }
  1210. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1211. {
  1212. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1213. }
  1214. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1215. {
  1216. gpr8(insn.reg8()) = insn.imm8();
  1217. }
  1218. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1219. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1220. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  1221. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1222. {
  1223. u64 result = (u64)eax() * (u64)insn.modrm().read32(*this, insn);
  1224. set_eax(result & 0xffffffff);
  1225. set_edx(result >> 32);
  1226. set_cf(edx() != 0);
  1227. set_of(edx() != 0);
  1228. }
  1229. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  1230. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1231. {
  1232. insn.modrm().write16(*this, insn, op_sub<u16>(*this, 0, insn.modrm().read16(*this, insn)));
  1233. }
  1234. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1235. {
  1236. insn.modrm().write32(*this, insn, op_sub<u32>(*this, 0, insn.modrm().read32(*this, insn)));
  1237. }
  1238. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1239. {
  1240. insn.modrm().write8(*this, insn, op_sub<u8>(*this, 0, insn.modrm().read8(*this, insn)));
  1241. }
  1242. void SoftCPU::NOP(const X86::Instruction&)
  1243. {
  1244. }
  1245. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1246. {
  1247. insn.modrm().write16(*this, insn, ~insn.modrm().read16(*this, insn));
  1248. }
  1249. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1250. {
  1251. insn.modrm().write32(*this, insn, ~insn.modrm().read32(*this, insn));
  1252. }
  1253. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1254. {
  1255. insn.modrm().write8(*this, insn, ~insn.modrm().read8(*this, insn));
  1256. }
  1257. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1258. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1259. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1260. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1261. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1262. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1263. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1264. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1265. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1266. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1267. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1268. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1269. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1270. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1271. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1272. void SoftCPU::POPFD(const X86::Instruction&)
  1273. {
  1274. m_eflags &= ~0x00fcffff;
  1275. m_eflags |= pop32() & 0x00fcffff;
  1276. }
  1277. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1278. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1279. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1280. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1281. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  1282. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  1283. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1284. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  1285. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1286. {
  1287. gpr32(insn.reg32()) = pop32();
  1288. }
  1289. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1290. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1291. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1292. void SoftCPU::PUSHFD(const X86::Instruction&)
  1293. {
  1294. push32(m_eflags & 0x00fcffff);
  1295. }
  1296. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1297. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1298. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1299. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1300. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1301. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1302. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1303. {
  1304. push32(insn.modrm().read32(*this, insn));
  1305. }
  1306. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1307. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1308. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  1309. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1310. {
  1311. push32(insn.imm32());
  1312. }
  1313. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1314. {
  1315. ASSERT(!insn.has_operand_size_override_prefix());
  1316. push32(sign_extended_to<i32>(insn.imm8()));
  1317. }
  1318. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  1319. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1320. {
  1321. push32(gpr32(insn.reg32()));
  1322. if (m_secret_handshake_state == 2) {
  1323. m_secret_data[0] = gpr32(insn.reg32());
  1324. ++m_secret_handshake_state;
  1325. } else if (m_secret_handshake_state == 3) {
  1326. m_secret_data[1] = gpr32(insn.reg32());
  1327. ++m_secret_handshake_state;
  1328. } else if (m_secret_handshake_state == 4) {
  1329. m_secret_data[2] = gpr32(insn.reg32());
  1330. m_secret_handshake_state = 0;
  1331. did_receive_secret_data();
  1332. }
  1333. }
  1334. template<typename T, bool cf>
  1335. ALWAYS_INLINE static T op_rcl_impl(SoftCPU& cpu, T data, u8 steps)
  1336. {
  1337. if (steps == 0)
  1338. return data;
  1339. u32 result = 0;
  1340. u32 new_flags = 0;
  1341. if constexpr (cf)
  1342. asm volatile("stc");
  1343. else
  1344. asm volatile("clc");
  1345. if constexpr (sizeof(T) == 4) {
  1346. asm volatile("rcll %%cl, %%eax\n"
  1347. : "=a"(result)
  1348. : "a"(data), "c"(steps));
  1349. } else if constexpr (sizeof(T) == 2) {
  1350. asm volatile("rclw %%cl, %%ax\n"
  1351. : "=a"(result)
  1352. : "a"(data), "c"(steps));
  1353. } else if constexpr (sizeof(T) == 1) {
  1354. asm volatile("rclb %%cl, %%al\n"
  1355. : "=a"(result)
  1356. : "a"(data), "c"(steps));
  1357. }
  1358. asm volatile(
  1359. "pushf\n"
  1360. "pop %%ebx"
  1361. : "=b"(new_flags));
  1362. cpu.set_flags_oc(new_flags);
  1363. return result;
  1364. }
  1365. template<typename T>
  1366. ALWAYS_INLINE static T op_rcl(SoftCPU& cpu, T data, u8 steps)
  1367. {
  1368. if (cpu.cf())
  1369. return op_rcl_impl<T, true>(cpu, data, steps);
  1370. return op_rcl_impl<T, false>(cpu, data, steps);
  1371. }
  1372. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCL, op_rcl)
  1373. template<typename T, bool cf>
  1374. ALWAYS_INLINE static T op_rcr_impl(SoftCPU& cpu, T data, u8 steps)
  1375. {
  1376. if (steps == 0)
  1377. return data;
  1378. u32 result = 0;
  1379. u32 new_flags = 0;
  1380. if constexpr (cf)
  1381. asm volatile("stc");
  1382. else
  1383. asm volatile("clc");
  1384. if constexpr (sizeof(T) == 4) {
  1385. asm volatile("rcrl %%cl, %%eax\n"
  1386. : "=a"(result)
  1387. : "a"(data), "c"(steps));
  1388. } else if constexpr (sizeof(T) == 2) {
  1389. asm volatile("rcrw %%cl, %%ax\n"
  1390. : "=a"(result)
  1391. : "a"(data), "c"(steps));
  1392. } else if constexpr (sizeof(T) == 1) {
  1393. asm volatile("rcrb %%cl, %%al\n"
  1394. : "=a"(result)
  1395. : "a"(data), "c"(steps));
  1396. }
  1397. asm volatile(
  1398. "pushf\n"
  1399. "pop %%ebx"
  1400. : "=b"(new_flags));
  1401. cpu.set_flags_oc(new_flags);
  1402. return result;
  1403. }
  1404. template<typename T>
  1405. ALWAYS_INLINE static T op_rcr(SoftCPU& cpu, T data, u8 steps)
  1406. {
  1407. if (cpu.cf())
  1408. return op_rcr_impl<T, true>(cpu, data, steps);
  1409. return op_rcr_impl<T, false>(cpu, data, steps);
  1410. }
  1411. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(RCR, op_rcr)
  1412. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1413. void SoftCPU::RET(const X86::Instruction& insn)
  1414. {
  1415. ASSERT(!insn.has_operand_size_override_prefix());
  1416. set_eip(pop32());
  1417. }
  1418. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1419. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1420. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1421. {
  1422. ASSERT(!insn.has_operand_size_override_prefix());
  1423. set_eip(pop32());
  1424. set_esp(esp() + insn.imm16());
  1425. }
  1426. template<typename T>
  1427. ALWAYS_INLINE static T op_rol(SoftCPU& cpu, T data, u8 steps)
  1428. {
  1429. if (steps == 0)
  1430. return data;
  1431. u32 result = 0;
  1432. u32 new_flags = 0;
  1433. if constexpr (sizeof(T) == 4) {
  1434. asm volatile("roll %%cl, %%eax\n"
  1435. : "=a"(result)
  1436. : "a"(data), "c"(steps));
  1437. } else if constexpr (sizeof(T) == 2) {
  1438. asm volatile("rolw %%cl, %%ax\n"
  1439. : "=a"(result)
  1440. : "a"(data), "c"(steps));
  1441. } else if constexpr (sizeof(T) == 1) {
  1442. asm volatile("rolb %%cl, %%al\n"
  1443. : "=a"(result)
  1444. : "a"(data), "c"(steps));
  1445. }
  1446. asm volatile(
  1447. "pushf\n"
  1448. "pop %%ebx"
  1449. : "=b"(new_flags));
  1450. cpu.set_flags_oc(new_flags);
  1451. return result;
  1452. }
  1453. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROL, op_rol)
  1454. template<typename T>
  1455. ALWAYS_INLINE static T op_ror(SoftCPU& cpu, T data, u8 steps)
  1456. {
  1457. if (steps == 0)
  1458. return data;
  1459. u32 result = 0;
  1460. u32 new_flags = 0;
  1461. if constexpr (sizeof(T) == 4) {
  1462. asm volatile("rorl %%cl, %%eax\n"
  1463. : "=a"(result)
  1464. : "a"(data), "c"(steps));
  1465. } else if constexpr (sizeof(T) == 2) {
  1466. asm volatile("rorw %%cl, %%ax\n"
  1467. : "=a"(result)
  1468. : "a"(data), "c"(steps));
  1469. } else if constexpr (sizeof(T) == 1) {
  1470. asm volatile("rorb %%cl, %%al\n"
  1471. : "=a"(result)
  1472. : "a"(data), "c"(steps));
  1473. }
  1474. asm volatile(
  1475. "pushf\n"
  1476. "pop %%ebx"
  1477. : "=b"(new_flags));
  1478. cpu.set_flags_oc(new_flags);
  1479. return result;
  1480. }
  1481. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(ROR, op_ror)
  1482. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1483. void SoftCPU::SALC(const X86::Instruction&)
  1484. {
  1485. set_al(cf() ? 0xff : 0x00);
  1486. if (m_secret_handshake_state < 2)
  1487. ++m_secret_handshake_state;
  1488. else
  1489. m_secret_handshake_state = 0;
  1490. }
  1491. template<typename T>
  1492. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1493. {
  1494. if (steps == 0)
  1495. return data;
  1496. u32 result = 0;
  1497. u32 new_flags = 0;
  1498. if constexpr (sizeof(T) == 4) {
  1499. asm volatile("sarl %%cl, %%eax\n"
  1500. : "=a"(result)
  1501. : "a"(data), "c"(steps));
  1502. } else if constexpr (sizeof(T) == 2) {
  1503. asm volatile("sarw %%cl, %%ax\n"
  1504. : "=a"(result)
  1505. : "a"(data), "c"(steps));
  1506. } else if constexpr (sizeof(T) == 1) {
  1507. asm volatile("sarb %%cl, %%al\n"
  1508. : "=a"(result)
  1509. : "a"(data), "c"(steps));
  1510. }
  1511. asm volatile(
  1512. "pushf\n"
  1513. "pop %%ebx"
  1514. : "=b"(new_flags));
  1515. cpu.set_flags_oszapc(new_flags);
  1516. return result;
  1517. }
  1518. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SAR, op_sar)
  1519. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1520. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1521. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1522. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1523. {
  1524. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1525. }
  1526. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1527. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction& insn)
  1528. {
  1529. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1530. }
  1531. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction& insn)
  1532. {
  1533. insn.modrm().write16(*this, insn, op_shld(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1534. }
  1535. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction& insn)
  1536. {
  1537. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1538. }
  1539. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction& insn)
  1540. {
  1541. insn.modrm().write32(*this, insn, op_shld(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1542. }
  1543. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHL, op_shl)
  1544. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction& insn)
  1545. {
  1546. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), cl()));
  1547. }
  1548. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction& insn)
  1549. {
  1550. insn.modrm().write16(*this, insn, op_shrd(*this, insn.modrm().read16(*this, insn), gpr16(insn.reg16()), insn.imm8()));
  1551. }
  1552. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction& insn)
  1553. {
  1554. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), cl()));
  1555. }
  1556. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction& insn)
  1557. {
  1558. insn.modrm().write32(*this, insn, op_shrd(*this, insn.modrm().read32(*this, insn), gpr32(insn.reg32()), insn.imm8()));
  1559. }
  1560. DEFINE_GENERIC_SHIFT_ROTATE_INSN_HANDLERS(SHR, op_shr)
  1561. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1562. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1563. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1564. void SoftCPU::STC(const X86::Instruction&)
  1565. {
  1566. set_cf(true);
  1567. }
  1568. void SoftCPU::STD(const X86::Instruction&)
  1569. {
  1570. set_df(true);
  1571. }
  1572. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1573. void SoftCPU::STOSB(const X86::Instruction& insn)
  1574. {
  1575. if (insn.has_address_size_override_prefix()) {
  1576. do_once_or_repeat<false>(insn, [&] {
  1577. write_memory8({ es(), di() }, al());
  1578. set_di(di() + (df() ? -1 : 1));
  1579. });
  1580. } else {
  1581. do_once_or_repeat<false>(insn, [&] {
  1582. write_memory8({ es(), edi() }, al());
  1583. set_edi(edi() + (df() ? -1 : 1));
  1584. });
  1585. }
  1586. }
  1587. void SoftCPU::STOSD(const X86::Instruction& insn)
  1588. {
  1589. if (insn.has_address_size_override_prefix()) {
  1590. do_once_or_repeat<false>(insn, [&] {
  1591. write_memory32({ es(), di() }, eax());
  1592. set_di(di() + (df() ? -4 : 4));
  1593. });
  1594. } else {
  1595. do_once_or_repeat<false>(insn, [&] {
  1596. write_memory32({ es(), edi() }, eax());
  1597. set_edi(edi() + (df() ? -4 : 4));
  1598. });
  1599. }
  1600. }
  1601. void SoftCPU::STOSW(const X86::Instruction& insn)
  1602. {
  1603. if (insn.has_address_size_override_prefix()) {
  1604. do_once_or_repeat<false>(insn, [&] {
  1605. write_memory16({ es(), di() }, ax());
  1606. set_di(di() + (df() ? -2 : 2));
  1607. });
  1608. } else {
  1609. do_once_or_repeat<false>(insn, [&] {
  1610. write_memory16({ es(), edi() }, ax());
  1611. set_edi(edi() + (df() ? -2 : 2));
  1612. });
  1613. }
  1614. }
  1615. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1616. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1617. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1618. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1619. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1620. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1621. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1622. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1623. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1624. {
  1625. auto dest = insn.modrm().read16(*this, insn);
  1626. auto src = gpr16(insn.reg16());
  1627. auto result = op_add(*this, dest, src);
  1628. gpr16(insn.reg16()) = dest;
  1629. insn.modrm().write16(*this, insn, result);
  1630. }
  1631. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1632. {
  1633. auto dest = insn.modrm().read32(*this, insn);
  1634. auto src = gpr32(insn.reg32());
  1635. auto result = op_add(*this, dest, src);
  1636. gpr32(insn.reg32()) = dest;
  1637. insn.modrm().write32(*this, insn, result);
  1638. }
  1639. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1640. {
  1641. auto dest = insn.modrm().read8(*this, insn);
  1642. auto src = gpr8(insn.reg8());
  1643. auto result = op_add(*this, dest, src);
  1644. gpr8(insn.reg8()) = dest;
  1645. insn.modrm().write8(*this, insn, result);
  1646. }
  1647. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1648. {
  1649. auto temp = gpr16(insn.reg16());
  1650. gpr16(insn.reg16()) = eax();
  1651. set_eax(temp);
  1652. }
  1653. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1654. {
  1655. auto temp = gpr32(insn.reg32());
  1656. gpr32(insn.reg32()) = eax();
  1657. set_eax(temp);
  1658. }
  1659. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1660. {
  1661. auto temp = insn.modrm().read16(*this, insn);
  1662. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1663. gpr16(insn.reg16()) = temp;
  1664. }
  1665. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1666. {
  1667. auto temp = insn.modrm().read32(*this, insn);
  1668. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1669. gpr32(insn.reg32()) = temp;
  1670. }
  1671. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1672. {
  1673. auto temp = insn.modrm().read8(*this, insn);
  1674. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1675. gpr8(insn.reg8()) = temp;
  1676. }
  1677. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1678. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1679. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1680. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1681. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1682. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1683. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1684. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1685. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1686. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1687. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1688. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1689. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1690. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1691. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1692. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1693. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1694. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1695. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1696. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1697. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1698. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  1699. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1700. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1701. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1702. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1703. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1704. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1705. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1706. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1707. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1708. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1709. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1710. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1711. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1712. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1713. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1714. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1715. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1716. }