SoftCPU.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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<bool update_dest, typename Op>
  503. ALWAYS_INLINE void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  504. {
  505. auto dest = al();
  506. auto src = insn.imm8();
  507. auto result = op(*this, dest, src);
  508. if (update_dest)
  509. set_al(result);
  510. }
  511. template<bool update_dest, typename Op>
  512. ALWAYS_INLINE void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  513. {
  514. auto dest = ax();
  515. auto src = insn.imm16();
  516. auto result = op(*this, dest, src);
  517. if (update_dest)
  518. set_ax(result);
  519. }
  520. template<bool update_dest, typename Op>
  521. ALWAYS_INLINE void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  522. {
  523. auto dest = eax();
  524. auto src = insn.imm32();
  525. auto result = op(*this, dest, src);
  526. if (update_dest)
  527. set_eax(result);
  528. }
  529. template<bool update_dest, typename Op>
  530. ALWAYS_INLINE void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  531. {
  532. auto dest = insn.modrm().read16(*this, insn);
  533. auto src = insn.imm16();
  534. auto result = op(*this, dest, src);
  535. if (update_dest)
  536. insn.modrm().write16(*this, insn, result);
  537. }
  538. template<bool update_dest, typename Op>
  539. ALWAYS_INLINE void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  540. {
  541. auto dest = insn.modrm().read16(*this, insn);
  542. auto src = sign_extended_to<u16>(insn.imm8());
  543. auto result = op(*this, dest, src);
  544. if (update_dest)
  545. insn.modrm().write16(*this, insn, result);
  546. }
  547. template<bool update_dest, typename Op>
  548. ALWAYS_INLINE void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  549. {
  550. auto dest = insn.modrm().read16(*this, insn);
  551. auto src = gpr16(insn.reg16());
  552. auto result = op(*this, dest, src);
  553. if (update_dest)
  554. insn.modrm().write16(*this, insn, result);
  555. }
  556. template<bool update_dest, typename Op>
  557. ALWAYS_INLINE void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  558. {
  559. auto dest = insn.modrm().read32(*this, insn);
  560. auto src = insn.imm32();
  561. auto result = op(*this, dest, src);
  562. if (update_dest)
  563. insn.modrm().write32(*this, insn, result);
  564. }
  565. template<bool update_dest, typename Op>
  566. ALWAYS_INLINE void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  567. {
  568. auto dest = insn.modrm().read32(*this, insn);
  569. auto src = sign_extended_to<u32>(insn.imm8());
  570. auto result = op(*this, dest, src);
  571. if (update_dest)
  572. insn.modrm().write32(*this, insn, result);
  573. }
  574. template<bool update_dest, typename Op>
  575. ALWAYS_INLINE void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  576. {
  577. auto dest = insn.modrm().read32(*this, insn);
  578. auto src = gpr32(insn.reg32());
  579. auto result = op(*this, dest, src);
  580. if (update_dest)
  581. insn.modrm().write32(*this, insn, result);
  582. }
  583. template<bool update_dest, typename Op>
  584. ALWAYS_INLINE void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  585. {
  586. auto dest = insn.modrm().read8(*this, insn);
  587. auto src = insn.imm8();
  588. auto result = op(*this, dest, src);
  589. if (update_dest)
  590. insn.modrm().write8(*this, insn, result);
  591. }
  592. template<bool update_dest, typename Op>
  593. ALWAYS_INLINE void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  594. {
  595. auto dest = insn.modrm().read8(*this, insn);
  596. auto src = gpr8(insn.reg8());
  597. auto result = op(*this, dest, src);
  598. if (update_dest)
  599. insn.modrm().write8(*this, insn, result);
  600. }
  601. template<bool update_dest, typename Op>
  602. ALWAYS_INLINE void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  603. {
  604. auto dest = gpr16(insn.reg16());
  605. auto src = insn.modrm().read16(*this, insn);
  606. auto result = op(*this, dest, src);
  607. if (update_dest)
  608. gpr16(insn.reg16()) = result;
  609. }
  610. template<bool update_dest, typename Op>
  611. ALWAYS_INLINE void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  612. {
  613. auto dest = gpr32(insn.reg32());
  614. auto src = insn.modrm().read32(*this, insn);
  615. auto result = op(*this, dest, src);
  616. if (update_dest)
  617. gpr32(insn.reg32()) = result;
  618. }
  619. template<bool update_dest, typename Op>
  620. ALWAYS_INLINE void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  621. {
  622. auto dest = gpr8(insn.reg8());
  623. auto src = insn.modrm().read8(*this, insn);
  624. auto result = op(*this, dest, src);
  625. if (update_dest)
  626. gpr8(insn.reg8()) = result;
  627. }
  628. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  629. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  630. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  631. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  632. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  633. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  634. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  635. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  636. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  638. void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
  639. {
  640. gpr32(insn.reg32()) = __builtin_bswap32(gpr32(insn.reg32()));
  641. }
  642. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  643. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  644. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  645. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  646. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  647. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  648. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  649. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  650. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  651. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  652. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  653. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  654. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  655. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  656. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  657. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  658. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  659. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  661. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  662. {
  663. push32(eip());
  664. set_eip(insn.modrm().read32(*this, insn));
  665. }
  666. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  667. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  668. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  669. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  670. {
  671. push32(eip());
  672. set_eip(eip() + (i32)insn.imm32());
  673. }
  674. void SoftCPU::CBW(const X86::Instruction&)
  675. {
  676. set_ah((al() & 0x80) ? 0xff : 0x00);
  677. }
  678. void SoftCPU::CDQ(const X86::Instruction&)
  679. {
  680. if (eax() & 0x80000000)
  681. set_edx(0xffffffff);
  682. else
  683. set_edx(0x00000000);
  684. }
  685. void SoftCPU::CLC(const X86::Instruction&)
  686. {
  687. set_cf(false);
  688. }
  689. void SoftCPU::CLD(const X86::Instruction&)
  690. {
  691. set_df(false);
  692. }
  693. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  694. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  695. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  696. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  697. {
  698. if (evaluate_condition(insn.cc()))
  699. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  700. }
  701. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  702. {
  703. if (evaluate_condition(insn.cc()))
  704. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  705. }
  706. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  707. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  708. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  709. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  710. {
  711. auto current = insn.modrm().read16(*this, insn);
  712. if (current == eax()) {
  713. set_zf(true);
  714. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  715. } else {
  716. set_zf(false);
  717. set_eax(current);
  718. }
  719. }
  720. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  721. {
  722. auto current = insn.modrm().read32(*this, insn);
  723. if (current == eax()) {
  724. set_zf(true);
  725. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  726. } else {
  727. set_zf(false);
  728. set_eax(current);
  729. }
  730. }
  731. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  732. {
  733. auto current = insn.modrm().read8(*this, insn);
  734. if (current == eax()) {
  735. set_zf(true);
  736. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  737. } else {
  738. set_zf(false);
  739. set_eax(current);
  740. }
  741. }
  742. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  743. void SoftCPU::CWD(const X86::Instruction&)
  744. {
  745. set_dx((ax() & 0x8000) ? 0xffff : 0x0000);
  746. }
  747. void SoftCPU::CWDE(const X86::Instruction&)
  748. {
  749. set_eax(sign_extended_to<u32>(ax()));
  750. }
  751. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  752. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  753. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  754. {
  755. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16(*this, insn)));
  756. }
  757. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  758. {
  759. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32(*this, insn)));
  760. }
  761. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  762. {
  763. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8(*this, insn)));
  764. }
  765. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  766. {
  767. gpr16(insn.reg16()) = op_dec(*this, gpr16(insn.reg16()));
  768. }
  769. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  770. {
  771. gpr32(insn.reg32()) = op_dec(*this, gpr32(insn.reg32()));
  772. }
  773. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  774. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  775. {
  776. auto divisor = insn.modrm().read32(*this, insn);
  777. if (divisor == 0) {
  778. warn() << "Divide by zero";
  779. TODO();
  780. }
  781. u64 dividend = ((u64)edx() << 32) | eax();
  782. auto result = dividend / divisor;
  783. if (result > NumericLimits<u32>::max()) {
  784. warn() << "Divide overflow";
  785. TODO();
  786. }
  787. set_eax(result);
  788. set_edx(dividend % divisor);
  789. }
  790. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  791. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  792. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  793. void SoftCPU::ESCAPE(const X86::Instruction&)
  794. {
  795. dbg() << "FIXME: x87 floating-point support";
  796. m_emulator.dump_backtrace();
  797. TODO();
  798. }
  799. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  800. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  801. void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
  802. {
  803. auto divisor = insn.modrm().read32(*this, insn);
  804. if (divisor == 0) {
  805. warn() << "Divide by zero";
  806. TODO();
  807. }
  808. i64 dividend = ((i64)edx() << 32) | eax();
  809. auto result = dividend / divisor;
  810. if (result > NumericLimits<i32>::max()) {
  811. warn() << "Divide overflow";
  812. TODO();
  813. }
  814. set_eax(result);
  815. set_edx(dividend % divisor);
  816. }
  817. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  818. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  819. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  820. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  821. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  822. {
  823. gpr16(insn.reg16()) = op_imul<i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  824. }
  825. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  826. {
  827. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  828. }
  829. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  830. {
  831. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), sign_extended_to<i16>(insn.imm8()));
  832. }
  833. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  834. {
  835. gpr32(insn.reg32()) = op_imul<i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  836. }
  837. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  838. {
  839. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  840. }
  841. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  842. {
  843. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), sign_extended_to<i32>(insn.imm8()));
  844. }
  845. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  846. {
  847. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  848. }
  849. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  850. {
  851. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  852. }
  853. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  854. {
  855. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  856. }
  857. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  858. {
  859. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  860. }
  861. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  862. {
  863. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  864. }
  865. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  867. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  868. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  869. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  870. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  871. {
  872. ASSERT(insn.imm8() == 0x82);
  873. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  874. }
  875. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  876. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  877. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  878. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  879. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  880. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  881. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  882. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  883. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  884. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  885. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  886. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  887. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  888. {
  889. set_eip(insn.modrm().read32(*this, insn));
  890. }
  891. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  892. {
  893. set_eip(eip() + (i16)insn.imm16());
  894. }
  895. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  896. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  897. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  898. {
  899. set_eip(eip() + (i32)insn.imm32());
  900. }
  901. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  902. {
  903. set_eip(eip() + (i8)insn.imm8());
  904. }
  905. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  906. {
  907. if (evaluate_condition(insn.cc()))
  908. set_eip(eip() + (i32)insn.imm32());
  909. }
  910. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  911. {
  912. if (evaluate_condition(insn.cc()))
  913. set_eip(eip() + (i8)insn.imm8());
  914. }
  915. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  916. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  917. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  918. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  919. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  920. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  921. void SoftCPU::LEAVE32(const X86::Instruction&)
  922. {
  923. u32 new_ebp = read_memory32({ ss(), ebp() });
  924. set_esp(ebp() + 4);
  925. set_ebp(new_ebp);
  926. }
  927. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  928. {
  929. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn).offset();
  930. }
  931. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  932. {
  933. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn).offset();
  934. }
  935. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  936. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  937. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  938. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  939. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  940. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  941. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  942. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  943. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  944. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  945. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  946. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  947. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  948. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  949. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  950. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  951. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  952. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  953. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  954. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  955. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  956. void SoftCPU::MOVSB(const X86::Instruction& insn)
  957. {
  958. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  959. if (insn.has_address_size_override_prefix()) {
  960. do_once_or_repeat<false>(insn, [&] {
  961. auto src = read_memory8({ src_segment, si() });
  962. write_memory8({ es(), di() }, src);
  963. set_di(di() + (df() ? -1 : 1));
  964. set_si(si() + (df() ? -1 : 1));
  965. });
  966. } else {
  967. do_once_or_repeat<false>(insn, [&] {
  968. auto src = read_memory8({ src_segment, esi() });
  969. write_memory8({ es(), edi() }, src);
  970. set_edi(edi() + (df() ? -1 : 1));
  971. set_esi(esi() + (df() ? -1 : 1));
  972. });
  973. }
  974. }
  975. void SoftCPU::MOVSD(const X86::Instruction& insn)
  976. {
  977. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  978. if (insn.has_address_size_override_prefix()) {
  979. do_once_or_repeat<false>(insn, [&] {
  980. auto src = read_memory32({ src_segment, si() });
  981. write_memory32({ es(), di() }, src);
  982. set_di(di() + (df() ? -4 : 4));
  983. set_si(si() + (df() ? -4 : 4));
  984. });
  985. } else {
  986. do_once_or_repeat<false>(insn, [&] {
  987. auto src = read_memory32({ src_segment, esi() });
  988. write_memory32({ es(), edi() }, src);
  989. set_edi(edi() + (df() ? -4 : 4));
  990. set_esi(esi() + (df() ? -4 : 4));
  991. });
  992. }
  993. }
  994. void SoftCPU::MOVSW(const X86::Instruction& insn)
  995. {
  996. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  997. if (insn.has_address_size_override_prefix()) {
  998. do_once_or_repeat<false>(insn, [&] {
  999. auto src = read_memory16({ src_segment, si() });
  1000. write_memory16({ es(), di() }, src);
  1001. set_di(di() + (df() ? -2 : 2));
  1002. set_si(si() + (df() ? -2 : 2));
  1003. });
  1004. } else {
  1005. do_once_or_repeat<false>(insn, [&] {
  1006. auto src = read_memory16({ src_segment, esi() });
  1007. write_memory16({ es(), edi() }, src);
  1008. set_edi(edi() + (df() ? -2 : 2));
  1009. set_esi(esi() + (df() ? -2 : 2));
  1010. });
  1011. }
  1012. }
  1013. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  1014. {
  1015. gpr16(insn.reg16()) = sign_extended_to<u16>(insn.modrm().read8(*this, insn));
  1016. }
  1017. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  1018. {
  1019. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read16(*this, insn));
  1020. }
  1021. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  1022. {
  1023. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read8(*this, insn));
  1024. }
  1025. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  1026. {
  1027. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  1028. }
  1029. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1030. {
  1031. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  1032. }
  1033. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1034. {
  1035. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  1036. }
  1037. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1038. {
  1039. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1040. }
  1041. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1042. {
  1043. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1044. }
  1045. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1046. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1047. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1048. {
  1049. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1050. }
  1051. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1052. {
  1053. insn.modrm().write16(*this, insn, insn.imm16());
  1054. }
  1055. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1056. {
  1057. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1058. }
  1059. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1060. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1061. {
  1062. insn.modrm().write32(*this, insn, insn.imm32());
  1063. }
  1064. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1065. {
  1066. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1067. }
  1068. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1069. {
  1070. insn.modrm().write8(*this, insn, insn.imm8());
  1071. }
  1072. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1073. {
  1074. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1075. }
  1076. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1077. {
  1078. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1079. }
  1080. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1081. {
  1082. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1083. }
  1084. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1085. {
  1086. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1087. }
  1088. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1089. {
  1090. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1091. }
  1092. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1093. {
  1094. gpr16(insn.reg16()) = insn.imm16();
  1095. }
  1096. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1097. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1098. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1099. {
  1100. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1101. }
  1102. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1103. {
  1104. gpr32(insn.reg32()) = insn.imm32();
  1105. }
  1106. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1107. {
  1108. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1109. }
  1110. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1111. {
  1112. gpr8(insn.reg8()) = insn.imm8();
  1113. }
  1114. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1115. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1116. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  1117. void SoftCPU::MUL_RM32(const X86::Instruction& insn)
  1118. {
  1119. u64 result = (u64)eax() * (u64)insn.modrm().read32(*this, insn);
  1120. set_eax(result & 0xffffffff);
  1121. set_edx(result >> 32);
  1122. set_cf(edx() != 0);
  1123. set_of(edx() != 0);
  1124. }
  1125. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  1126. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1127. {
  1128. insn.modrm().write16(*this, insn, op_sub<u16>(*this, 0, insn.modrm().read16(*this, insn)));
  1129. }
  1130. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1131. {
  1132. insn.modrm().write32(*this, insn, op_sub<u32>(*this, 0, insn.modrm().read32(*this, insn)));
  1133. }
  1134. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1135. {
  1136. insn.modrm().write8(*this, insn, op_sub<u8>(*this, 0, insn.modrm().read8(*this, insn)));
  1137. }
  1138. void SoftCPU::NOP(const X86::Instruction&)
  1139. {
  1140. }
  1141. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1142. {
  1143. insn.modrm().write16(*this, insn, ~insn.modrm().read16(*this, insn));
  1144. }
  1145. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1146. {
  1147. insn.modrm().write32(*this, insn, ~insn.modrm().read32(*this, insn));
  1148. }
  1149. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1150. {
  1151. insn.modrm().write8(*this, insn, ~insn.modrm().read8(*this, insn));
  1152. }
  1153. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1154. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1155. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1156. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1157. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1158. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1159. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1160. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1161. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1162. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1163. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1164. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1165. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1166. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1167. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1168. void SoftCPU::POPFD(const X86::Instruction&)
  1169. {
  1170. m_eflags &= ~0x00fcffff;
  1171. m_eflags |= pop32() & 0x00fcffff;
  1172. }
  1173. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1174. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1175. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1176. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1177. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  1178. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  1179. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1180. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  1181. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1182. {
  1183. gpr32(insn.reg32()) = pop32();
  1184. }
  1185. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1186. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1187. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1188. void SoftCPU::PUSHFD(const X86::Instruction&)
  1189. {
  1190. push32(m_eflags & 0x00fcffff);
  1191. }
  1192. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1193. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1194. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1195. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1196. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1197. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1198. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1199. {
  1200. push32(insn.modrm().read32(*this, insn));
  1201. }
  1202. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1203. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1204. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  1205. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1206. {
  1207. push32(insn.imm32());
  1208. }
  1209. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1210. {
  1211. ASSERT(!insn.has_operand_size_override_prefix());
  1212. push32(sign_extended_to<i32>(insn.imm8()));
  1213. }
  1214. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  1215. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1216. {
  1217. push32(gpr32(insn.reg32()));
  1218. }
  1219. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  1220. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  1221. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1222. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  1223. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  1224. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1225. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  1226. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  1227. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1228. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  1229. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  1230. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1231. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  1232. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  1233. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1234. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  1235. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  1236. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1237. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1238. void SoftCPU::RET(const X86::Instruction& insn)
  1239. {
  1240. ASSERT(!insn.has_operand_size_override_prefix());
  1241. set_eip(pop32());
  1242. }
  1243. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1244. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1245. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1246. {
  1247. ASSERT(!insn.has_operand_size_override_prefix());
  1248. set_eip(pop32());
  1249. set_esp(esp() + insn.imm16());
  1250. }
  1251. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  1252. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  1253. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1254. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  1255. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  1256. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1257. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  1258. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  1259. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1260. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  1261. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  1262. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1263. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  1264. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  1265. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1266. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  1267. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  1268. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1269. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1270. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  1271. template<typename T>
  1272. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1273. {
  1274. if (steps == 0)
  1275. return data;
  1276. u32 result = 0;
  1277. u32 new_flags = 0;
  1278. if constexpr (sizeof(T) == 4) {
  1279. asm volatile("sarl %%cl, %%eax\n"
  1280. : "=a"(result)
  1281. : "a"(data), "c"(steps));
  1282. } else if constexpr (sizeof(T) == 2) {
  1283. asm volatile("sarw %%cl, %%ax\n"
  1284. : "=a"(result)
  1285. : "a"(data), "c"(steps));
  1286. } else if constexpr (sizeof(T) == 1) {
  1287. asm volatile("sarb %%cl, %%al\n"
  1288. : "=a"(result)
  1289. : "a"(data), "c"(steps));
  1290. }
  1291. asm volatile(
  1292. "pushf\n"
  1293. "pop %%ebx"
  1294. : "=b"(new_flags));
  1295. cpu.set_flags_oszapc(new_flags);
  1296. return result;
  1297. }
  1298. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  1299. {
  1300. auto data = insn.modrm().read16(*this, insn);
  1301. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  1302. }
  1303. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  1304. {
  1305. auto data = insn.modrm().read16(*this, insn);
  1306. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  1307. }
  1308. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  1309. {
  1310. auto data = insn.modrm().read16(*this, insn);
  1311. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  1312. }
  1313. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  1314. {
  1315. auto data = insn.modrm().read32(*this, insn);
  1316. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  1317. }
  1318. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  1319. {
  1320. auto data = insn.modrm().read32(*this, insn);
  1321. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  1322. }
  1323. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  1324. {
  1325. auto data = insn.modrm().read32(*this, insn);
  1326. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  1327. }
  1328. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  1329. {
  1330. auto data = insn.modrm().read8(*this, insn);
  1331. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  1332. }
  1333. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  1334. {
  1335. auto data = insn.modrm().read8(*this, insn);
  1336. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  1337. }
  1338. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  1339. {
  1340. auto data = insn.modrm().read8(*this, insn);
  1341. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  1342. }
  1343. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1344. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1345. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1346. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1347. {
  1348. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1349. }
  1350. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1351. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1352. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1353. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1354. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1355. void SoftCPU::SHL_RM16_1(const X86::Instruction& insn)
  1356. {
  1357. auto data = insn.modrm().read16(*this, insn);
  1358. insn.modrm().write16(*this, insn, op_shl(*this, data, 1));
  1359. }
  1360. void SoftCPU::SHL_RM16_CL(const X86::Instruction& insn)
  1361. {
  1362. auto data = insn.modrm().read16(*this, insn);
  1363. insn.modrm().write16(*this, insn, op_shl(*this, data, cl()));
  1364. }
  1365. void SoftCPU::SHL_RM16_imm8(const X86::Instruction& insn)
  1366. {
  1367. auto data = insn.modrm().read16(*this, insn);
  1368. insn.modrm().write16(*this, insn, op_shl(*this, data, insn.imm8()));
  1369. }
  1370. void SoftCPU::SHL_RM32_1(const X86::Instruction& insn)
  1371. {
  1372. auto data = insn.modrm().read32(*this, insn);
  1373. insn.modrm().write32(*this, insn, op_shl(*this, data, 1));
  1374. }
  1375. void SoftCPU::SHL_RM32_CL(const X86::Instruction& insn)
  1376. {
  1377. auto data = insn.modrm().read32(*this, insn);
  1378. insn.modrm().write32(*this, insn, op_shl(*this, data, cl()));
  1379. }
  1380. void SoftCPU::SHL_RM32_imm8(const X86::Instruction& insn)
  1381. {
  1382. auto data = insn.modrm().read32(*this, insn);
  1383. insn.modrm().write32(*this, insn, op_shl(*this, data, insn.imm8()));
  1384. }
  1385. void SoftCPU::SHL_RM8_1(const X86::Instruction& insn)
  1386. {
  1387. auto data = insn.modrm().read8(*this, insn);
  1388. insn.modrm().write8(*this, insn, op_shl(*this, data, 1));
  1389. }
  1390. void SoftCPU::SHL_RM8_CL(const X86::Instruction& insn)
  1391. {
  1392. auto data = insn.modrm().read8(*this, insn);
  1393. insn.modrm().write8(*this, insn, op_shl(*this, data, cl()));
  1394. }
  1395. void SoftCPU::SHL_RM8_imm8(const X86::Instruction& insn)
  1396. {
  1397. auto data = insn.modrm().read8(*this, insn);
  1398. insn.modrm().write8(*this, insn, op_shl(*this, data, insn.imm8()));
  1399. }
  1400. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1401. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1402. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1403. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1404. void SoftCPU::SHR_RM16_1(const X86::Instruction& insn)
  1405. {
  1406. auto data = insn.modrm().read16(*this, insn);
  1407. insn.modrm().write16(*this, insn, op_shr(*this, data, 1));
  1408. }
  1409. void SoftCPU::SHR_RM16_CL(const X86::Instruction& insn)
  1410. {
  1411. auto data = insn.modrm().read16(*this, insn);
  1412. insn.modrm().write16(*this, insn, op_shr(*this, data, cl()));
  1413. }
  1414. void SoftCPU::SHR_RM16_imm8(const X86::Instruction& insn)
  1415. {
  1416. auto data = insn.modrm().read16(*this, insn);
  1417. insn.modrm().write16(*this, insn, op_shr(*this, data, insn.imm8()));
  1418. }
  1419. void SoftCPU::SHR_RM32_1(const X86::Instruction& insn)
  1420. {
  1421. auto data = insn.modrm().read32(*this, insn);
  1422. insn.modrm().write32(*this, insn, op_shr(*this, data, 1));
  1423. }
  1424. void SoftCPU::SHR_RM32_CL(const X86::Instruction& insn)
  1425. {
  1426. auto data = insn.modrm().read32(*this, insn);
  1427. insn.modrm().write32(*this, insn, op_shr(*this, data, cl()));
  1428. }
  1429. void SoftCPU::SHR_RM32_imm8(const X86::Instruction& insn)
  1430. {
  1431. auto data = insn.modrm().read32(*this, insn);
  1432. insn.modrm().write32(*this, insn, op_shr(*this, data, insn.imm8()));
  1433. }
  1434. void SoftCPU::SHR_RM8_1(const X86::Instruction& insn)
  1435. {
  1436. auto data = insn.modrm().read8(*this, insn);
  1437. insn.modrm().write8(*this, insn, op_shr(*this, data, 1));
  1438. }
  1439. void SoftCPU::SHR_RM8_CL(const X86::Instruction& insn)
  1440. {
  1441. auto data = insn.modrm().read8(*this, insn);
  1442. insn.modrm().write8(*this, insn, op_shr(*this, data, cl()));
  1443. }
  1444. void SoftCPU::SHR_RM8_imm8(const X86::Instruction& insn)
  1445. {
  1446. auto data = insn.modrm().read8(*this, insn);
  1447. insn.modrm().write8(*this, insn, op_shr(*this, data, insn.imm8()));
  1448. }
  1449. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1450. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1451. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1452. void SoftCPU::STC(const X86::Instruction&)
  1453. {
  1454. set_cf(true);
  1455. }
  1456. void SoftCPU::STD(const X86::Instruction&)
  1457. {
  1458. set_df(true);
  1459. }
  1460. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1461. void SoftCPU::STOSB(const X86::Instruction& insn)
  1462. {
  1463. if (insn.has_address_size_override_prefix()) {
  1464. do_once_or_repeat<false>(insn, [&] {
  1465. write_memory8({ es(), di() }, al());
  1466. set_di(di() + (df() ? -1 : 1));
  1467. });
  1468. } else {
  1469. do_once_or_repeat<false>(insn, [&] {
  1470. write_memory8({ es(), edi() }, al());
  1471. set_edi(edi() + (df() ? -1 : 1));
  1472. });
  1473. }
  1474. }
  1475. void SoftCPU::STOSD(const X86::Instruction& insn)
  1476. {
  1477. if (insn.has_address_size_override_prefix()) {
  1478. do_once_or_repeat<false>(insn, [&] {
  1479. write_memory32({ es(), di() }, eax());
  1480. set_di(di() + (df() ? -4 : 4));
  1481. });
  1482. } else {
  1483. do_once_or_repeat<false>(insn, [&] {
  1484. write_memory32({ es(), edi() }, eax());
  1485. set_edi(edi() + (df() ? -4 : 4));
  1486. });
  1487. }
  1488. }
  1489. void SoftCPU::STOSW(const X86::Instruction& insn)
  1490. {
  1491. if (insn.has_address_size_override_prefix()) {
  1492. do_once_or_repeat<false>(insn, [&] {
  1493. write_memory16({ es(), di() }, ax());
  1494. set_di(di() + (df() ? -2 : 2));
  1495. });
  1496. } else {
  1497. do_once_or_repeat<false>(insn, [&] {
  1498. write_memory16({ es(), edi() }, ax());
  1499. set_edi(edi() + (df() ? -2 : 2));
  1500. });
  1501. }
  1502. }
  1503. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1504. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1505. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1506. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1507. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1508. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1509. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1510. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1511. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1512. {
  1513. auto dest = insn.modrm().read16(*this, insn);
  1514. auto src = gpr16(insn.reg16());
  1515. auto result = op_add(*this, dest, src);
  1516. gpr16(insn.reg16()) = dest;
  1517. insn.modrm().write16(*this, insn, result);
  1518. }
  1519. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1520. {
  1521. auto dest = insn.modrm().read32(*this, insn);
  1522. auto src = gpr32(insn.reg32());
  1523. auto result = op_add(*this, dest, src);
  1524. gpr32(insn.reg32()) = dest;
  1525. insn.modrm().write32(*this, insn, result);
  1526. }
  1527. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1528. {
  1529. auto dest = insn.modrm().read8(*this, insn);
  1530. auto src = gpr8(insn.reg8());
  1531. auto result = op_add(*this, dest, src);
  1532. gpr8(insn.reg8()) = dest;
  1533. insn.modrm().write8(*this, insn, result);
  1534. }
  1535. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1536. {
  1537. auto temp = gpr16(insn.reg16());
  1538. gpr16(insn.reg16()) = eax();
  1539. set_eax(temp);
  1540. }
  1541. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1542. {
  1543. auto temp = gpr32(insn.reg32());
  1544. gpr32(insn.reg32()) = eax();
  1545. set_eax(temp);
  1546. }
  1547. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1548. {
  1549. auto temp = insn.modrm().read16(*this, insn);
  1550. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1551. gpr16(insn.reg16()) = temp;
  1552. }
  1553. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1554. {
  1555. auto temp = insn.modrm().read32(*this, insn);
  1556. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1557. gpr32(insn.reg32()) = temp;
  1558. }
  1559. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1560. {
  1561. auto temp = insn.modrm().read8(*this, insn);
  1562. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1563. gpr8(insn.reg8()) = temp;
  1564. }
  1565. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1566. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1567. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1568. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1569. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1570. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1571. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1572. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1573. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1574. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1575. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1576. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1577. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1578. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1579. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1580. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1581. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1582. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1583. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1584. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1585. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1586. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  1587. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1588. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1589. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1590. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1591. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1592. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1593. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1594. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1595. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1596. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1597. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1598. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1599. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1600. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1601. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1602. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1603. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1604. }