SoftCPU.cpp 61 KB

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