SoftCPU.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. namespace UserspaceEmulator {
  32. template<typename T, typename U>
  33. inline constexpr T sign_extended_to(U value)
  34. {
  35. if (!(value & X86::TypeTrivia<U>::sign_bit))
  36. return value;
  37. return (X86::TypeTrivia<T>::mask & ~X86::TypeTrivia<U>::mask) | value;
  38. }
  39. SoftCPU::SoftCPU(Emulator& emulator)
  40. : m_emulator(emulator)
  41. {
  42. memset(m_gpr, 0, sizeof(m_gpr));
  43. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  44. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  45. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  46. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  47. }
  48. void SoftCPU::dump() const
  49. {
  50. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  51. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  52. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  53. }
  54. u8 SoftCPU::read8()
  55. {
  56. auto value = read_memory8({ cs(), eip() });
  57. m_eip += 1;
  58. return value;
  59. }
  60. u16 SoftCPU::read16()
  61. {
  62. auto value = read_memory16({ cs(), eip() });
  63. m_eip += 2;
  64. return value;
  65. }
  66. u32 SoftCPU::read32()
  67. {
  68. auto value = read_memory32({ cs(), eip() });
  69. m_eip += 4;
  70. return value;
  71. }
  72. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  73. {
  74. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  75. auto value = m_emulator.mmu().read8(address.offset());
  76. printf("\033[36;1mread_memory8: @%08x -> %02x\033[0m\n", address.offset(), value);
  77. return value;
  78. }
  79. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  80. {
  81. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  82. auto value = m_emulator.mmu().read16(address.offset());
  83. printf("\033[36;1mread_memory16: @%08x -> %04x\033[0m\n", address.offset(), value);
  84. return value;
  85. }
  86. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  87. {
  88. ASSERT(address.selector() == 0x18 || address.selector() == 0x20);
  89. auto value = m_emulator.mmu().read32(address.offset());
  90. printf("\033[36;1mread_memory32: @%08x -> %08x\033[0m\n", address.offset(), value);
  91. return value;
  92. }
  93. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  94. {
  95. ASSERT(address.selector() == 0x20);
  96. printf("\033[35;1mwrite_memory8: @%08x <- %02x\033[0m\n", address.offset(), value);
  97. m_emulator.mmu().write8(address.offset(), value);
  98. }
  99. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  100. {
  101. ASSERT(address.selector() == 0x20);
  102. printf("\033[35;1mwrite_memory16: @%08x <- %04x\033[0m\n", address.offset(), value);
  103. m_emulator.mmu().write16(address.offset(), value);
  104. }
  105. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  106. {
  107. ASSERT(address.selector() == 0x20);
  108. printf("\033[35;1mwrite_memory32: @%08x <- %08x\033[0m\n", address.offset(), value);
  109. m_emulator.mmu().write32(address.offset(), value);
  110. }
  111. void SoftCPU::push32(u32 value)
  112. {
  113. set_esp(esp() - sizeof(value));
  114. write_memory32({ ss(), esp() }, value);
  115. }
  116. u32 SoftCPU::pop32()
  117. {
  118. auto value = read_memory32({ ss(), esp() });
  119. set_esp(esp() + sizeof(value));
  120. return value;
  121. }
  122. template<bool check_zf, typename Callback>
  123. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  124. {
  125. if (!insn.has_rep_prefix())
  126. return callback();
  127. if (insn.has_address_size_override_prefix()) {
  128. while (cx()) {
  129. callback();
  130. set_cx(cx() - 1);
  131. if constexpr (check_zf) {
  132. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  133. break;
  134. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  135. break;
  136. }
  137. }
  138. return;
  139. }
  140. while (ecx()) {
  141. callback();
  142. set_ecx(ecx() - 1);
  143. if constexpr (check_zf) {
  144. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  145. break;
  146. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  147. break;
  148. }
  149. }
  150. }
  151. template<typename T>
  152. static T op_xor(SoftCPU& cpu, const T& dest, const T& src)
  153. {
  154. T result = 0;
  155. u32 new_flags = 0;
  156. if constexpr (sizeof(T) == 4) {
  157. asm volatile("xorl %%ecx, %%eax\n"
  158. : "=a"(result)
  159. : "a"(dest), "c"((u32)src));
  160. } else if constexpr (sizeof(T) == 2) {
  161. asm volatile("xor %%cx, %%ax\n"
  162. : "=a"(result)
  163. : "a"(dest), "c"((u16)src));
  164. } else if constexpr (sizeof(T) == 1) {
  165. asm volatile("xorb %%cl, %%al\n"
  166. : "=a"(result)
  167. : "a"(dest), "c"((u8)src));
  168. } else {
  169. ASSERT_NOT_REACHED();
  170. }
  171. asm volatile(
  172. "pushf\n"
  173. "pop %%ebx"
  174. : "=b"(new_flags));
  175. cpu.set_flags_oszpc(new_flags);
  176. return result;
  177. }
  178. template<typename T>
  179. static T op_or(SoftCPU& cpu, const T& dest, const T& src)
  180. {
  181. T result = 0;
  182. u32 new_flags = 0;
  183. if constexpr (sizeof(T) == 4) {
  184. asm volatile("orl %%ecx, %%eax\n"
  185. : "=a"(result)
  186. : "a"(dest), "c"((u32)src));
  187. } else if constexpr (sizeof(T) == 2) {
  188. asm volatile("or %%cx, %%ax\n"
  189. : "=a"(result)
  190. : "a"(dest), "c"((u16)src));
  191. } else if constexpr (sizeof(T) == 1) {
  192. asm volatile("orb %%cl, %%al\n"
  193. : "=a"(result)
  194. : "a"(dest), "c"((u8)src));
  195. } else {
  196. ASSERT_NOT_REACHED();
  197. }
  198. asm volatile(
  199. "pushf\n"
  200. "pop %%ebx"
  201. : "=b"(new_flags));
  202. cpu.set_flags_oszpc(new_flags);
  203. return result;
  204. }
  205. template<typename T>
  206. static T op_sub(SoftCPU& cpu, const T& dest, const T& src)
  207. {
  208. T result = 0;
  209. u32 new_flags = 0;
  210. if constexpr (sizeof(T) == 4) {
  211. asm volatile("subl %%ecx, %%eax\n"
  212. : "=a"(result)
  213. : "a"(dest), "c"((u32)src));
  214. } else if constexpr (sizeof(T) == 2) {
  215. asm volatile("subw %%cx, %%ax\n"
  216. : "=a"(result)
  217. : "a"(dest), "c"((u16)src));
  218. } else if constexpr (sizeof(T) == 1) {
  219. asm volatile("subb %%cl, %%al\n"
  220. : "=a"(result)
  221. : "a"(dest), "c"((u8)src));
  222. } else {
  223. ASSERT_NOT_REACHED();
  224. }
  225. asm volatile(
  226. "pushf\n"
  227. "pop %%ebx"
  228. : "=b"(new_flags));
  229. cpu.set_flags_oszapc(new_flags);
  230. return result;
  231. }
  232. template<typename T>
  233. static T op_add(SoftCPU& cpu, T& dest, const T& src)
  234. {
  235. T result = 0;
  236. u32 new_flags = 0;
  237. if constexpr (sizeof(T) == 4) {
  238. asm volatile("addl %%ecx, %%eax\n"
  239. : "=a"(result)
  240. : "a"(dest), "c"((u32)src));
  241. } else if constexpr (sizeof(T) == 2) {
  242. asm volatile("addw %%cx, %%ax\n"
  243. : "=a"(result)
  244. : "a"(dest), "c"((u16)src));
  245. } else if constexpr (sizeof(T) == 1) {
  246. asm volatile("addb %%cl, %%al\n"
  247. : "=a"(result)
  248. : "a"(dest), "c"((u8)src));
  249. } else {
  250. ASSERT_NOT_REACHED();
  251. }
  252. asm volatile(
  253. "pushf\n"
  254. "pop %%ebx"
  255. : "=b"(new_flags));
  256. cpu.set_flags_oszapc(new_flags);
  257. return result;
  258. }
  259. template<typename T>
  260. static T op_and(SoftCPU& cpu, const T& dest, const T& src)
  261. {
  262. T result = 0;
  263. u32 new_flags = 0;
  264. if constexpr (sizeof(T) == 4) {
  265. asm volatile("andl %%ecx, %%eax\n"
  266. : "=a"(result)
  267. : "a"(dest), "c"((u32)src));
  268. } else if constexpr (sizeof(T) == 2) {
  269. asm volatile("andw %%cx, %%ax\n"
  270. : "=a"(result)
  271. : "a"(dest), "c"((u16)src));
  272. } else if constexpr (sizeof(T) == 1) {
  273. asm volatile("andb %%cl, %%al\n"
  274. : "=a"(result)
  275. : "a"(dest), "c"((u8)src));
  276. } else {
  277. ASSERT_NOT_REACHED();
  278. }
  279. asm volatile(
  280. "pushf\n"
  281. "pop %%ebx"
  282. : "=b"(new_flags));
  283. cpu.set_flags_oszpc(new_flags);
  284. return result;
  285. }
  286. template<typename T>
  287. static T op_imul(SoftCPU& cpu, const T& dest, const T& src)
  288. {
  289. T result = 0;
  290. u32 new_flags = 0;
  291. if constexpr (sizeof(T) == 4) {
  292. asm volatile("imull %%ecx, %%eax\n"
  293. : "=a"(result)
  294. : "a"(dest), "c"((i32)src));
  295. } else if constexpr (sizeof(T) == 2) {
  296. asm volatile("imulw %%cx, %%ax\n"
  297. : "=a"(result)
  298. : "a"(dest), "c"((i16)src));
  299. } else {
  300. ASSERT_NOT_REACHED();
  301. }
  302. asm volatile(
  303. "pushf\n"
  304. "pop %%ebx"
  305. : "=b"(new_flags));
  306. cpu.set_flags_oszapc(new_flags);
  307. return result;
  308. }
  309. template<typename T>
  310. static T op_shr(SoftCPU& cpu, T data, u8 steps)
  311. {
  312. if (steps == 0)
  313. return data;
  314. u32 result = 0;
  315. u32 new_flags = 0;
  316. if constexpr (sizeof(T) == 4)
  317. asm volatile("shrl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  318. else if constexpr (sizeof(T) == 2)
  319. asm volatile("shrw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  320. else if constexpr (sizeof(T) == 1)
  321. asm volatile("shrb %%cl, %%al\n" ::"a"(data), "c"(steps));
  322. asm volatile(
  323. "mov %%eax, %%ebx\n"
  324. : "=b"(result));
  325. asm volatile(
  326. "pushf\n"
  327. "pop %%eax"
  328. : "=a"(new_flags));
  329. cpu.set_flags_oszapc(new_flags);
  330. return result;
  331. }
  332. template<bool update_dest, typename Op>
  333. void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  334. {
  335. auto dest = al();
  336. auto src = insn.imm8();
  337. auto result = op(*this, dest, src);
  338. if (update_dest)
  339. set_al(result);
  340. }
  341. template<bool update_dest, typename Op>
  342. void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  343. {
  344. auto dest = ax();
  345. auto src = insn.imm16();
  346. auto result = op(*this, dest, src);
  347. if (update_dest)
  348. set_ax(result);
  349. }
  350. template<bool update_dest, typename Op>
  351. void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  352. {
  353. auto dest = eax();
  354. auto src = insn.imm32();
  355. auto result = op(*this, dest, src);
  356. if (update_dest)
  357. set_eax(result);
  358. }
  359. template<bool update_dest, typename Op>
  360. void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  361. {
  362. auto dest = insn.modrm().read16(*this, insn);
  363. auto src = insn.imm16();
  364. auto result = op(*this, dest, src);
  365. if (update_dest)
  366. insn.modrm().write16(*this, insn, result);
  367. }
  368. template<bool update_dest, typename Op>
  369. void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  370. {
  371. auto dest = insn.modrm().read16(*this, insn);
  372. auto src = sign_extended_to<u16>(insn.imm8());
  373. auto result = op(*this, dest, src);
  374. if (update_dest)
  375. insn.modrm().write16(*this, insn, result);
  376. }
  377. template<bool update_dest, typename Op>
  378. void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  379. {
  380. auto dest = insn.modrm().read16(*this, insn);
  381. auto src = gpr16(insn.reg16());
  382. auto result = op(*this, dest, src);
  383. if (update_dest)
  384. insn.modrm().write16(*this, insn, result);
  385. }
  386. template<bool update_dest, typename Op>
  387. void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  388. {
  389. auto dest = insn.modrm().read32(*this, insn);
  390. auto src = insn.imm32();
  391. auto result = op(*this, dest, src);
  392. if (update_dest)
  393. insn.modrm().write32(*this, insn, result);
  394. }
  395. template<bool update_dest, typename Op>
  396. void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  397. {
  398. auto dest = insn.modrm().read32(*this, insn);
  399. auto src = sign_extended_to<u32>(insn.imm8());
  400. auto result = op(*this, dest, src);
  401. if (update_dest)
  402. insn.modrm().write32(*this, insn, result);
  403. }
  404. template<bool update_dest, typename Op>
  405. void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  406. {
  407. auto dest = insn.modrm().read32(*this, insn);
  408. auto src = gpr32(insn.reg32());
  409. auto result = op(*this, dest, src);
  410. if (update_dest)
  411. insn.modrm().write32(*this, insn, result);
  412. }
  413. template<bool update_dest, typename Op>
  414. void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  415. {
  416. auto dest = insn.modrm().read8(*this, insn);
  417. auto src = insn.imm8();
  418. auto result = op(*this, dest, src);
  419. if (update_dest)
  420. insn.modrm().write8(*this, insn, result);
  421. }
  422. template<bool update_dest, typename Op>
  423. void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  424. {
  425. auto dest = insn.modrm().read8(*this, insn);
  426. auto src = gpr8(insn.reg8());
  427. auto result = op(*this, dest, src);
  428. if (update_dest)
  429. insn.modrm().write8(*this, insn, result);
  430. }
  431. template<bool update_dest, typename Op>
  432. void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  433. {
  434. auto dest = gpr16(insn.reg16());
  435. auto src = insn.modrm().read16(*this, insn);
  436. auto result = op(*this, dest, src);
  437. if (update_dest)
  438. gpr16(insn.reg16()) = result;
  439. }
  440. template<bool update_dest, typename Op>
  441. void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  442. {
  443. auto dest = gpr32(insn.reg32());
  444. auto src = insn.modrm().read32(*this, insn);
  445. auto result = op(*this, dest, src);
  446. if (update_dest)
  447. gpr32(insn.reg32()) = result;
  448. }
  449. template<bool update_dest, typename Op>
  450. void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  451. {
  452. auto dest = gpr8(insn.reg8());
  453. auto src = insn.modrm().read8(*this, insn);
  454. auto result = op(*this, dest, src);
  455. if (update_dest)
  456. gpr8(insn.reg8()) = result;
  457. }
  458. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  459. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  460. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  461. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  462. void SoftCPU::ADC_AL_imm8(const X86::Instruction&) { TODO(); }
  463. void SoftCPU::ADC_AX_imm16(const X86::Instruction&) { TODO(); }
  464. void SoftCPU::ADC_EAX_imm32(const X86::Instruction&) { TODO(); }
  465. void SoftCPU::ADC_RM16_imm16(const X86::Instruction&) { TODO(); }
  466. void SoftCPU::ADC_RM16_imm8(const X86::Instruction&) { TODO(); }
  467. void SoftCPU::ADC_RM16_reg16(const X86::Instruction&) { TODO(); }
  468. void SoftCPU::ADC_RM32_imm32(const X86::Instruction&) { TODO(); }
  469. void SoftCPU::ADC_RM32_imm8(const X86::Instruction&) { TODO(); }
  470. void SoftCPU::ADC_RM32_reg32(const X86::Instruction&) { TODO(); }
  471. void SoftCPU::ADC_RM8_imm8(const X86::Instruction&) { TODO(); }
  472. void SoftCPU::ADC_RM8_reg8(const X86::Instruction&) { TODO(); }
  473. void SoftCPU::ADC_reg16_RM16(const X86::Instruction&) { TODO(); }
  474. void SoftCPU::ADC_reg32_RM32(const X86::Instruction&) { TODO(); }
  475. void SoftCPU::ADC_reg8_RM8(const X86::Instruction&) { TODO(); }
  476. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  477. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  478. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  479. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  480. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  481. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  482. void SoftCPU::BSWAP_reg32(const X86::Instruction&) { TODO(); }
  483. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  484. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  485. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  486. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  487. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  488. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  489. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  490. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  491. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  492. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  493. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  494. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  495. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  496. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  497. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  498. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  499. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  500. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  501. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  502. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  503. {
  504. push32(eip());
  505. set_eip(insn.modrm().read32(*this, insn));
  506. }
  507. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  508. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  509. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  510. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  511. {
  512. push32(eip());
  513. set_eip(eip() + (i32)insn.imm32());
  514. }
  515. void SoftCPU::CBW(const X86::Instruction&) { TODO(); }
  516. void SoftCPU::CDQ(const X86::Instruction&) { TODO(); }
  517. void SoftCPU::CLC(const X86::Instruction&) { TODO(); }
  518. void SoftCPU::CLD(const X86::Instruction&) { TODO(); }
  519. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  520. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  521. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  522. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction&) { TODO(); }
  523. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction&) { TODO(); }
  524. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  525. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  526. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  527. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction&) { TODO(); }
  528. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction&) { TODO(); }
  529. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction&) { TODO(); }
  530. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  531. void SoftCPU::CWD(const X86::Instruction&) { TODO(); }
  532. void SoftCPU::CWDE(const X86::Instruction&) { TODO(); }
  533. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  534. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  535. void SoftCPU::DEC_RM16(const X86::Instruction&) { TODO(); }
  536. void SoftCPU::DEC_RM32(const X86::Instruction&) { TODO(); }
  537. void SoftCPU::DEC_RM8(const X86::Instruction&) { TODO(); }
  538. void SoftCPU::DEC_reg16(const X86::Instruction&) { TODO(); }
  539. void SoftCPU::DEC_reg32(const X86::Instruction&) { TODO(); }
  540. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  541. void SoftCPU::DIV_RM32(const X86::Instruction&) { TODO(); }
  542. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  543. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  544. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  545. void SoftCPU::ESCAPE(const X86::Instruction&) { TODO(); }
  546. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  547. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  548. void SoftCPU::IDIV_RM32(const X86::Instruction&) { TODO(); }
  549. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  550. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  551. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  552. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  553. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  554. {
  555. gpr16(insn.reg16()) = op_imul<i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  556. }
  557. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  558. {
  559. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  560. }
  561. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  562. {
  563. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), sign_extended_to<i16>(insn.imm8()));
  564. }
  565. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  566. {
  567. gpr32(insn.reg32()) = op_imul<i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  568. }
  569. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  570. {
  571. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  572. }
  573. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  574. {
  575. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), sign_extended_to<i32>(insn.imm8()));
  576. }
  577. template<typename T>
  578. static T op_inc(SoftCPU& cpu, T data)
  579. {
  580. T result = 0;
  581. u32 new_flags = 0;
  582. if constexpr (sizeof(T) == 4) {
  583. asm volatile("incl %%eax\n"
  584. : "=a"(result)
  585. : "a"(data));
  586. } else if constexpr (sizeof(T) == 2) {
  587. asm volatile("incw %%ax\n"
  588. : "=a"(result)
  589. : "a"(data));
  590. } else if constexpr (sizeof(T) == 1) {
  591. asm volatile("incb %%al\n"
  592. : "=a"(result)
  593. : "a"(data));
  594. }
  595. asm volatile(
  596. "pushf\n"
  597. "pop %%ebx"
  598. : "=b"(new_flags));
  599. cpu.set_flags_oszap(new_flags);
  600. return result;
  601. }
  602. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  603. {
  604. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  605. }
  606. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  607. {
  608. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  609. }
  610. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  611. {
  612. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  613. }
  614. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  615. {
  616. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  617. }
  618. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  619. {
  620. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  621. }
  622. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  623. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  624. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  625. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  626. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  627. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  628. {
  629. ASSERT(insn.imm8() == 0x82);
  630. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  631. }
  632. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  633. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  634. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  635. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  636. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  638. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  639. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  640. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  641. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  642. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  643. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  644. void SoftCPU::JMP_RM32(const X86::Instruction&) { TODO(); }
  645. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  646. {
  647. set_eip(eip() + (i16)insn.imm16());
  648. }
  649. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  650. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  651. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  652. {
  653. set_eip(eip() + (i32)insn.imm32());
  654. }
  655. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  656. {
  657. set_eip(eip() + (i8)insn.imm8());
  658. }
  659. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  661. {
  662. if (evaluate_condition(insn.cc()))
  663. set_eip(eip() + (i8)insn.imm8());
  664. }
  665. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  666. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  667. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  668. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  669. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  670. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  671. void SoftCPU::LEAVE32(const X86::Instruction&)
  672. {
  673. u32 new_ebp = read_memory32({ ss(), ebp() });
  674. set_esp(ebp() + 4);
  675. set_ebp(new_ebp);
  676. }
  677. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  678. {
  679. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  680. }
  681. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  682. {
  683. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  684. }
  685. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  686. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  687. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  688. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  689. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  690. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  691. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  692. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  693. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  694. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  695. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  696. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  697. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  698. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  699. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  700. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  701. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  702. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  703. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  704. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  705. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  706. void SoftCPU::MOVSB(const X86::Instruction&) { TODO(); }
  707. void SoftCPU::MOVSD(const X86::Instruction&) { TODO(); }
  708. void SoftCPU::MOVSW(const X86::Instruction&) { TODO(); }
  709. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction&) { TODO(); }
  710. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction&) { TODO(); }
  711. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction&) { TODO(); }
  712. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  713. {
  714. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  715. }
  716. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  717. {
  718. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  719. }
  720. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  721. {
  722. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  723. }
  724. void SoftCPU::MOV_AL_moff8(const X86::Instruction&) { TODO(); }
  725. void SoftCPU::MOV_AX_moff16(const X86::Instruction&) { TODO(); }
  726. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  727. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  728. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  729. {
  730. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  731. }
  732. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  733. {
  734. insn.modrm().write16(*this, insn, insn.imm16());
  735. }
  736. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  737. {
  738. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  739. }
  740. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  741. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  742. {
  743. insn.modrm().write32(*this, insn, insn.imm32());
  744. }
  745. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  746. {
  747. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  748. }
  749. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  750. {
  751. insn.modrm().write8(*this, insn, insn.imm8());
  752. }
  753. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  754. {
  755. insn.modrm().write8(*this, insn, insn.modrm().read8(*this, insn));
  756. }
  757. void SoftCPU::MOV_moff16_AX(const X86::Instruction&) { TODO(); }
  758. void SoftCPU::MOV_moff32_EAX(const X86::Instruction&) { TODO(); }
  759. void SoftCPU::MOV_moff8_AL(const X86::Instruction&) { TODO(); }
  760. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  761. {
  762. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  763. }
  764. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  765. {
  766. gpr16(insn.reg16()) = insn.imm16();
  767. }
  768. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  769. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  770. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  771. {
  772. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  773. }
  774. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  775. {
  776. gpr32(insn.reg32()) = insn.imm32();
  777. }
  778. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  779. {
  780. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  781. }
  782. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  783. {
  784. gpr8(insn.reg8()) = insn.imm8();
  785. }
  786. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  787. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  788. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  789. void SoftCPU::MUL_RM32(const X86::Instruction&) { TODO(); }
  790. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  791. void SoftCPU::NEG_RM16(const X86::Instruction&) { TODO(); }
  792. void SoftCPU::NEG_RM32(const X86::Instruction&) { TODO(); }
  793. void SoftCPU::NEG_RM8(const X86::Instruction&) { TODO(); }
  794. void SoftCPU::NOP(const X86::Instruction&) { TODO(); }
  795. void SoftCPU::NOT_RM16(const X86::Instruction&) { TODO(); }
  796. void SoftCPU::NOT_RM32(const X86::Instruction&) { TODO(); }
  797. void SoftCPU::NOT_RM8(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  800. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  801. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  802. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  803. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  804. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  805. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  806. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  807. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  808. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  809. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  810. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  811. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  812. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  813. void SoftCPU::POPFD(const X86::Instruction&) { TODO(); }
  814. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  815. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  816. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  817. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  818. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  819. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  820. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  821. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  822. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  823. {
  824. gpr32(insn.reg32()) = pop32();
  825. }
  826. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  827. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  828. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  829. void SoftCPU::PUSHFD(const X86::Instruction&) { TODO(); }
  830. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  831. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  832. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  833. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  834. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  835. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  836. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  837. {
  838. push32(insn.modrm().read32(*this, insn));
  839. }
  840. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  841. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  842. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  843. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  844. {
  845. push32(insn.imm32());
  846. }
  847. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  848. {
  849. ASSERT(!insn.has_operand_size_override_prefix());
  850. push32((i32)insn.imm8());
  851. }
  852. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  853. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  854. {
  855. push32(gpr32(insn.reg32()));
  856. }
  857. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  858. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  859. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  860. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  861. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  862. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  863. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  864. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  865. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  867. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  868. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  869. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  870. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  871. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  872. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  873. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  874. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  875. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  876. void SoftCPU::RET(const X86::Instruction& insn)
  877. {
  878. ASSERT(!insn.has_operand_size_override_prefix());
  879. set_eip(pop32());
  880. }
  881. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  882. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  883. void SoftCPU::RET_imm16(const X86::Instruction&) { TODO(); }
  884. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  885. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  886. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  887. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  888. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  889. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  890. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  891. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  892. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  893. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  894. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  895. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  896. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  897. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  898. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  899. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  900. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  901. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  902. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  903. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  904. template<typename T>
  905. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  906. {
  907. if (steps == 0)
  908. return data;
  909. u32 result = 0;
  910. u32 new_flags = 0;
  911. if constexpr (sizeof(T) == 4)
  912. asm volatile("sarl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  913. else if constexpr (sizeof(T) == 2)
  914. asm volatile("sarw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  915. else if constexpr (sizeof(T) == 1)
  916. asm volatile("sarb %%cl, %%al\n" ::"a"(data), "c"(steps));
  917. asm volatile(
  918. "mov %%eax, %%ebx\n"
  919. : "=b"(result));
  920. asm volatile(
  921. "pushf\n"
  922. "pop %%eax"
  923. : "=a"(new_flags));
  924. cpu.set_flags_oszapc(new_flags);
  925. return result;
  926. }
  927. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  928. {
  929. auto data = insn.modrm().read16(*this, insn);
  930. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  931. }
  932. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  933. {
  934. auto data = insn.modrm().read16(*this, insn);
  935. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  936. }
  937. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  938. {
  939. auto data = insn.modrm().read16(*this, insn);
  940. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  941. }
  942. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  943. {
  944. auto data = insn.modrm().read32(*this, insn);
  945. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  946. }
  947. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  948. {
  949. auto data = insn.modrm().read32(*this, insn);
  950. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  951. }
  952. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  953. {
  954. auto data = insn.modrm().read32(*this, insn);
  955. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  956. }
  957. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  958. {
  959. auto data = insn.modrm().read8(*this, insn);
  960. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  961. }
  962. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  963. {
  964. auto data = insn.modrm().read8(*this, insn);
  965. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  966. }
  967. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  968. {
  969. auto data = insn.modrm().read8(*this, insn);
  970. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  971. }
  972. void SoftCPU::SBB_AL_imm8(const X86::Instruction&) { TODO(); }
  973. void SoftCPU::SBB_AX_imm16(const X86::Instruction&) { TODO(); }
  974. void SoftCPU::SBB_EAX_imm32(const X86::Instruction&) { TODO(); }
  975. void SoftCPU::SBB_RM16_imm16(const X86::Instruction&) { TODO(); }
  976. void SoftCPU::SBB_RM16_imm8(const X86::Instruction&) { TODO(); }
  977. void SoftCPU::SBB_RM16_reg16(const X86::Instruction&) { TODO(); }
  978. void SoftCPU::SBB_RM32_imm32(const X86::Instruction&) { TODO(); }
  979. void SoftCPU::SBB_RM32_imm8(const X86::Instruction&) { TODO(); }
  980. void SoftCPU::SBB_RM32_reg32(const X86::Instruction&) { TODO(); }
  981. void SoftCPU::SBB_RM8_imm8(const X86::Instruction&) { TODO(); }
  982. void SoftCPU::SBB_RM8_reg8(const X86::Instruction&) { TODO(); }
  983. void SoftCPU::SBB_reg16_RM16(const X86::Instruction&) { TODO(); }
  984. void SoftCPU::SBB_reg32_RM32(const X86::Instruction&) { TODO(); }
  985. void SoftCPU::SBB_reg8_RM8(const X86::Instruction&) { TODO(); }
  986. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  987. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  988. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  989. void SoftCPU::SETcc_RM8(const X86::Instruction&) { TODO(); }
  990. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  991. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  992. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  993. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  994. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  995. void SoftCPU::SHL_RM16_1(const X86::Instruction&) { TODO(); }
  996. void SoftCPU::SHL_RM16_CL(const X86::Instruction&) { TODO(); }
  997. void SoftCPU::SHL_RM16_imm8(const X86::Instruction&) { TODO(); }
  998. void SoftCPU::SHL_RM32_1(const X86::Instruction&) { TODO(); }
  999. void SoftCPU::SHL_RM32_CL(const X86::Instruction&) { TODO(); }
  1000. void SoftCPU::SHL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1001. void SoftCPU::SHL_RM8_1(const X86::Instruction&) { TODO(); }
  1002. void SoftCPU::SHL_RM8_CL(const X86::Instruction&) { TODO(); }
  1003. void SoftCPU::SHL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1004. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1005. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1006. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1007. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1008. void SoftCPU::SHR_RM16_1(const X86::Instruction& insn)
  1009. {
  1010. auto data = insn.modrm().read16(*this, insn);
  1011. insn.modrm().write16(*this, insn, op_shr(*this, data, 1));
  1012. }
  1013. void SoftCPU::SHR_RM16_CL(const X86::Instruction& insn)
  1014. {
  1015. auto data = insn.modrm().read16(*this, insn);
  1016. insn.modrm().write16(*this, insn, op_shr(*this, data, cl()));
  1017. }
  1018. void SoftCPU::SHR_RM16_imm8(const X86::Instruction& insn)
  1019. {
  1020. auto data = insn.modrm().read16(*this, insn);
  1021. insn.modrm().write16(*this, insn, op_shr(*this, data, insn.imm8()));
  1022. }
  1023. void SoftCPU::SHR_RM32_1(const X86::Instruction& insn)
  1024. {
  1025. auto data = insn.modrm().read32(*this, insn);
  1026. insn.modrm().write32(*this, insn, op_shr(*this, data, 1));
  1027. }
  1028. void SoftCPU::SHR_RM32_CL(const X86::Instruction& insn)
  1029. {
  1030. auto data = insn.modrm().read32(*this, insn);
  1031. insn.modrm().write32(*this, insn, op_shr(*this, data, cl()));
  1032. }
  1033. void SoftCPU::SHR_RM32_imm8(const X86::Instruction& insn)
  1034. {
  1035. auto data = insn.modrm().read32(*this, insn);
  1036. insn.modrm().write32(*this, insn, op_shr(*this, data, insn.imm8()));
  1037. }
  1038. void SoftCPU::SHR_RM8_1(const X86::Instruction& insn)
  1039. {
  1040. auto data = insn.modrm().read8(*this, insn);
  1041. insn.modrm().write8(*this, insn, op_shr(*this, data, 1));
  1042. }
  1043. void SoftCPU::SHR_RM8_CL(const X86::Instruction& insn)
  1044. {
  1045. auto data = insn.modrm().read8(*this, insn);
  1046. insn.modrm().write8(*this, insn, op_shr(*this, data, cl()));
  1047. }
  1048. void SoftCPU::SHR_RM8_imm8(const X86::Instruction& insn)
  1049. {
  1050. auto data = insn.modrm().read8(*this, insn);
  1051. insn.modrm().write8(*this, insn, op_shr(*this, data, insn.imm8()));
  1052. }
  1053. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1054. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1055. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1056. void SoftCPU::STC(const X86::Instruction&) { TODO(); }
  1057. void SoftCPU::STD(const X86::Instruction&) { TODO(); }
  1058. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1059. void SoftCPU::STOSB(const X86::Instruction& insn)
  1060. {
  1061. if (insn.has_address_size_override_prefix()) {
  1062. do_once_or_repeat<false>(insn, [&] {
  1063. write_memory8({ es(), di() }, al());
  1064. set_di(di() + (df() ? -1 : 1));
  1065. });
  1066. } else {
  1067. do_once_or_repeat<false>(insn, [&] {
  1068. write_memory8({ es(), edi() }, al());
  1069. set_edi(edi() + (df() ? -1 : 1));
  1070. });
  1071. }
  1072. }
  1073. void SoftCPU::STOSD(const X86::Instruction& insn)
  1074. {
  1075. if (insn.has_address_size_override_prefix()) {
  1076. do_once_or_repeat<false>(insn, [&] {
  1077. write_memory32({ es(), di() }, eax());
  1078. set_di(di() + (df() ? -4 : 4));
  1079. });
  1080. } else {
  1081. do_once_or_repeat<false>(insn, [&] {
  1082. write_memory32({ es(), edi() }, eax());
  1083. set_edi(edi() + (df() ? -4 : 4));
  1084. });
  1085. }
  1086. }
  1087. void SoftCPU::STOSW(const X86::Instruction& insn)
  1088. {
  1089. if (insn.has_address_size_override_prefix()) {
  1090. do_once_or_repeat<false>(insn, [&] {
  1091. write_memory16({ es(), di() }, ax());
  1092. set_di(di() + (df() ? -2 : 2));
  1093. });
  1094. } else {
  1095. do_once_or_repeat<false>(insn, [&] {
  1096. write_memory16({ es(), edi() }, ax());
  1097. set_edi(edi() + (df() ? -2 : 2));
  1098. });
  1099. }
  1100. }
  1101. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1102. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1103. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1104. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1105. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1106. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1107. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1108. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1109. void SoftCPU::XADD_RM16_reg16(const X86::Instruction&) { TODO(); }
  1110. void SoftCPU::XADD_RM32_reg32(const X86::Instruction&) { TODO(); }
  1111. void SoftCPU::XADD_RM8_reg8(const X86::Instruction&) { TODO(); }
  1112. void SoftCPU::XCHG_AX_reg16(const X86::Instruction&) { TODO(); }
  1113. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction&) { TODO(); }
  1114. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction&) { TODO(); }
  1115. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction&) { TODO(); }
  1116. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction&) { TODO(); }
  1117. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1118. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1119. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1120. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1121. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1122. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1123. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1124. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1125. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1126. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1127. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1128. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1129. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1130. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1131. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1132. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1133. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1134. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1135. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1136. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1137. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1138. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1139. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1140. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1141. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1142. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1143. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1144. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1145. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1146. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1147. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1148. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1149. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1150. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1151. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1152. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1153. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1154. }