vm86.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. *
  3. * Copyright © 2000 Keith Packard
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. /*
  24. Copyright (c) 2000 by Juliusz Chroboczek
  25. Permission is hereby granted, free of charge, to any person obtaining a copy
  26. of this software and associated documentation files (the "Software"), to deal
  27. in the Software without restriction, including without limitation the rights
  28. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  29. copies of the Software, and to permit persons to whom the Software is
  30. furnished to do so, subject to the following conditions:
  31. The above copyright notice and this permission notice shall be included in
  32. all copies or substantial portions of the Software.
  33. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  34. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  35. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  36. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  37. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  38. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  39. THE SOFTWARE.
  40. */
  41. #ifdef HAVE_CONFIG_H
  42. #include <kdrive-config.h>
  43. #endif
  44. #include "vm86.h"
  45. #define PUSHW(vi, i) \
  46. { vi->vms.regs.esp -= 2;\
  47. LMW(vi,MAKE_POINTER(vi->vms.regs.ss, vi->vms.regs.esp)) = i;}
  48. static int vm86old(struct vm86_struct *vms);
  49. static int vm86_loop(Vm86InfoPtr vi);
  50. static const U8 rev_ints[32] = { 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0x80,
  54. };
  55. static const U8 retcode_data[2] = { 0xCD, 0xFF };
  56. Vm86InfoPtr Vm86Setup(int mapHoles)
  57. {
  58. int devmem = -1, devzero = -1;
  59. void *magicMem, *loMem, *hiMem;
  60. void *hole1, *hole2;
  61. U32 stack_base, ret_code;
  62. Vm86InfoPtr vi = NULL;
  63. devmem = open("/dev/mem", O_RDWR);
  64. if (devmem < 0) {
  65. perror("open /dev/mem");
  66. goto fail;
  67. }
  68. devzero = open("/dev/zero", O_RDWR);
  69. if (devzero < 0) {
  70. perror("open /dev/zero");
  71. goto fail;
  72. }
  73. loMem = MAP_FAILED;
  74. hiMem = MAP_FAILED;
  75. hole1 = MAP_FAILED;
  76. hole2 = MAP_FAILED;
  77. magicMem = mmap((void *)MAGICMEM_BASE, MAGICMEM_SIZE,
  78. PROT_READ | PROT_WRITE | PROT_EXEC,
  79. MAP_PRIVATE | MAP_FIXED, devmem, MAGICMEM_BASE);
  80. if (magicMem == MAP_FAILED) {
  81. ErrorF("Couldn't map magic memory\n");
  82. goto unmapfail;
  83. }
  84. if (mapHoles) {
  85. hole1 = mmap((void *)HOLE1_BASE, HOLE1_SIZE,
  86. PROT_READ | PROT_WRITE | PROT_EXEC,
  87. MAP_PRIVATE | MAP_FIXED, devzero, HOLE1_BASE);
  88. if (hole1 == MAP_FAILED) {
  89. ErrorF("Couldn't map first hole\n");
  90. goto unmapfail;
  91. }
  92. }
  93. loMem = mmap((void *)LOMEM_BASE, LOMEM_SIZE,
  94. PROT_READ | PROT_WRITE | PROT_EXEC,
  95. MAP_PRIVATE | MAP_FIXED, devzero, LOMEM_BASE);
  96. if (loMem == MAP_FAILED) {
  97. ErrorF("Couldn't map low memory\n");
  98. munmap(magicMem, MAGICMEM_SIZE);
  99. goto unmapfail;
  100. }
  101. if (mapHoles) {
  102. hole2 = mmap((void *)HOLE2_BASE, HOLE2_SIZE,
  103. PROT_READ | PROT_WRITE | PROT_EXEC,
  104. MAP_PRIVATE | MAP_FIXED, devzero, HOLE2_BASE);
  105. if (hole2 == MAP_FAILED) {
  106. ErrorF("Couldn't map first hole\n");
  107. goto unmapfail;
  108. }
  109. }
  110. hiMem = mmap((void *)HIMEM_BASE, HIMEM_SIZE,
  111. PROT_READ | PROT_WRITE | PROT_EXEC,
  112. MAP_SHARED | MAP_FIXED, devmem, HIMEM_BASE);
  113. if (hiMem == MAP_FAILED) {
  114. ErrorF("Couldn't map high memory\n");
  115. goto unmapfail;
  116. }
  117. vi = malloc(sizeof(Vm86InfoRec));
  118. if (!vi)
  119. goto unmapfail;
  120. vi->magicMem = magicMem;
  121. vi->hole1 = hole1;
  122. vi->loMem = loMem;
  123. vi->hole2 = hole2;
  124. vi->hiMem = hiMem;
  125. vi->brk = LOMEM_BASE;
  126. stack_base = Vm86AllocateMemory(vi, STACK_SIZE);
  127. if (stack_base == ALLOC_FAIL)
  128. goto unmapfail;
  129. ret_code = Vm86AllocateMemory(vi, sizeof(retcode_data));
  130. if (ret_code == ALLOC_FAIL)
  131. goto unmapfail;
  132. vi->stack_base = stack_base;
  133. vi->ret_code = ret_code;
  134. memset(&vi->vms, 0, sizeof(struct vm86_struct));
  135. vi->vms.flags = 0;
  136. vi->vms.screen_bitmap = 0;
  137. vi->vms.cpu_type = CPU_586;
  138. memcpy(&vi->vms.int_revectored, rev_ints, sizeof(rev_ints));
  139. iopl(3);
  140. if (devmem >= 0)
  141. close(devmem);
  142. if (devzero >= 0)
  143. close(devzero);
  144. return vi;
  145. unmapfail:
  146. if (magicMem != MAP_FAILED)
  147. munmap(magicMem, MAGICMEM_SIZE);
  148. if (hole1 != MAP_FAILED)
  149. munmap(hole1, HOLE1_SIZE);
  150. if (loMem != MAP_FAILED)
  151. munmap(loMem, LOMEM_SIZE);
  152. if (hole2 != MAP_FAILED)
  153. munmap(hole2, HOLE2_SIZE);
  154. if (hiMem != MAP_FAILED)
  155. munmap(hiMem, HIMEM_SIZE);
  156. fail:
  157. if (devmem >= 0)
  158. close(devmem);
  159. if (devzero >= 0)
  160. close(devzero);
  161. if (vi)
  162. free(vi);
  163. return NULL;
  164. }
  165. void Vm86Cleanup(Vm86InfoPtr vi)
  166. {
  167. if (vi->magicMem != MAP_FAILED)
  168. munmap(vi->magicMem, MAGICMEM_SIZE);
  169. if (vi->hole1 != MAP_FAILED)
  170. munmap(vi->hole1, HOLE1_SIZE);
  171. if (vi->loMem != MAP_FAILED)
  172. munmap(vi->loMem, LOMEM_SIZE);
  173. if (vi->hole2 != MAP_FAILED)
  174. munmap(vi->hole2, HOLE2_SIZE);
  175. if (vi->hiMem != MAP_FAILED)
  176. munmap(vi->hiMem, HIMEM_SIZE);
  177. free(vi);
  178. }
  179. int Vm86DoInterrupt(Vm86InfoPtr vi, int num)
  180. {
  181. U16 seg, off;
  182. int code;
  183. if (num < 0 || num > 256) {
  184. ErrorF("Interrupt %d doesn't exist\n", num);
  185. return -1;
  186. }
  187. seg = MMW(vi, num * 4 + 2);
  188. off = MMW(vi, num * 4);
  189. if (MAKE_POINTER(seg, off) < ROM_BASE ||
  190. MAKE_POINTER(seg, off) >= ROM_BASE + ROM_SIZE) {
  191. ErrorF
  192. ("Interrupt pointer (seg %x off %x) doesn't point at ROM\n",
  193. seg, off);
  194. return -1;
  195. }
  196. memcpy(&(LM(vi, vi->ret_code)), retcode_data, sizeof(retcode_data));
  197. vi->vms.regs.eflags = IF_MASK | IOPL_MASK;
  198. vi->vms.regs.ss = POINTER_SEGMENT(vi->stack_base);
  199. vi->vms.regs.esp = STACK_SIZE;
  200. PUSHW(vi, IF_MASK | IOPL_MASK);
  201. PUSHW(vi, POINTER_SEGMENT(vi->ret_code));
  202. PUSHW(vi, POINTER_OFFSET(vi->ret_code));
  203. vi->vms.regs.cs = seg;
  204. vi->vms.regs.eip = off;
  205. OsBlockSignals();
  206. code = vm86_loop(vi);
  207. OsReleaseSignals();
  208. if (code < 0) {
  209. ErrorF("vm86 failed (errno %d)\n", errno);
  210. return -1;
  211. } else if (code != 0) {
  212. ErrorF("vm86 returned 0x%04X\n", code);
  213. return -1;
  214. } else
  215. return 0;
  216. }
  217. int Vm86DoPOST(Vm86InfoPtr vi)
  218. {
  219. U16 seg, off;
  220. int code;
  221. seg = 0xC000;
  222. off = 3;
  223. if (MAKE_POINTER(seg, off) < ROM_BASE ||
  224. MAKE_POINTER(seg, off) >= ROM_BASE + ROM_SIZE) {
  225. ErrorF("BIOS pointer (seg %x off %x) doesn't point at ROM\n",
  226. seg, off);
  227. return -1;
  228. }
  229. memcpy(&(LM(vi, vi->ret_code)), retcode_data, sizeof(retcode_data));
  230. vi->vms.regs.ss = POINTER_SEGMENT(vi->stack_base);
  231. vi->vms.regs.esp = STACK_SIZE;
  232. PUSHW(vi, POINTER_SEGMENT(vi->ret_code));
  233. PUSHW(vi, POINTER_OFFSET(vi->ret_code));
  234. vi->vms.regs.cs = seg;
  235. vi->vms.regs.eip = off;
  236. OsBlockSignals();
  237. code = vm86_loop(vi);
  238. OsReleaseSignals();
  239. if (code < 0) {
  240. ErrorF("vm86 failed (errno %d)\n", errno);
  241. return -1;
  242. } else if (code != 0) {
  243. ErrorF("vm86 returned 0x%04X\n", code);
  244. return -1;
  245. } else
  246. return 0;
  247. }
  248. #define DEBUG_VBE 0
  249. #if DEBUG_VBE
  250. #define DBG(x) ErrorF x; usleep(10*1000)
  251. #else
  252. #define DBG(x)
  253. #endif
  254. static inline U8 vm86_inb(U16 port)
  255. {
  256. U8 value;
  257. if (port != 0x3da) {
  258. DBG(("inb 0x%04x", port));
  259. }
  260. asm volatile ("inb %w1,%b0":"=a" (value):"d"(port));
  261. if (port != 0x3da) {
  262. DBG((" = 0x%02x\n", value));
  263. }
  264. return value;
  265. }
  266. static inline U16 vm86_inw(U16 port)
  267. {
  268. U16 value;
  269. DBG(("inw 0x%04x", port));
  270. asm volatile ("inw %w1,%w0":"=a" (value):"d"(port));
  271. DBG((" = 0x%04x\n", value));
  272. return value;
  273. }
  274. static inline U32 vm86_inl(U16 port)
  275. {
  276. U32 value;
  277. DBG(("inl 0x%04x", port));
  278. asm volatile ("inl %w1,%0":"=a" (value):"d"(port));
  279. DBG((" = 0x%08x\n", value));
  280. return value;
  281. }
  282. static inline void vm86_outb(U16 port, U8 value)
  283. {
  284. #if 0
  285. static U8 CR;
  286. if (port == 0x3d4)
  287. CR = value;
  288. if (port == 0x3d5 && CR == 0xa4) {
  289. DBG(("outb 0x%04x = 0x%02x (skipped)\n", port, value));
  290. return;
  291. }
  292. #endif
  293. DBG(("outb 0x%04x = 0x%02x\n", port, value));
  294. asm volatile ("outb %b0,%w1"::"a" (value), "d"(port));
  295. }
  296. static inline void vm86_outw(U16 port, U16 value)
  297. {
  298. DBG(("outw 0x%04x = 0x%04x\n", port, value));
  299. asm volatile ("outw %w0,%w1"::"a" (value), "d"(port));
  300. }
  301. static inline void vm86_outl(U16 port, U32 value)
  302. {
  303. DBG(("outl 0x%04x = 0x%08x\n", port, value));
  304. asm volatile ("outl %0,%w1"::"a" (value), "d"(port));
  305. }
  306. #define SEG_CS 1
  307. #define SEG_DS 2
  308. #define SEG_ES 3
  309. #define SEG_SS 4
  310. #define SEG_GS 5
  311. #define SEG_FS 6
  312. #define REP 1
  313. #define REPNZ 2
  314. #define SET_8(_x, _y) (_x) = ((_x) & ~0xFF) | ((_y) & 0xFF);
  315. #define SET_16(_x, _y) (_x) = ((_x) & ~0xFFFF) | ((_y) & 0xFFFF);
  316. #define INC_IP(_i) SET_16(regs->eip, (regs->eip + _i))
  317. #define AGAIN INC_IP(1); goto again;
  318. static int Vm86IsMemory(Vm86InfoPtr vi, U32 i)
  319. {
  320. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  321. return 1;
  322. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  323. return 1;
  324. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  325. return 1;
  326. else
  327. return 0;
  328. }
  329. static void Vm86WriteMemory(Vm86InfoPtr vi, U32 i, U8 val)
  330. {
  331. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  332. MM(vi, i) = val;
  333. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  334. LM(vi, i) = val;
  335. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  336. HM(vi, i) = val;
  337. else {
  338. ErrorF("Writing unmapped memory at 0x%08X\n", i);
  339. }
  340. }
  341. static void Vm86WriteMemoryW(Vm86InfoPtr vi, U32 i, U16 val)
  342. {
  343. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  344. MMW(vi, i) = val;
  345. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  346. LMW(vi, i) = val;
  347. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  348. HMW(vi, i) = val;
  349. else {
  350. ErrorF("Writing unmapped memory at 0x%08X\n", i);
  351. }
  352. }
  353. static void Vm86WriteMemoryL(Vm86InfoPtr vi, U32 i, U32 val)
  354. {
  355. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  356. MML(vi, i) = val;
  357. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  358. LML(vi, i) = val;
  359. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  360. HML(vi, i) = val;
  361. else {
  362. ErrorF("Writing unmapped memory at 0x%08X\n", i);
  363. }
  364. }
  365. static int vm86_emulate(Vm86InfoPtr vi)
  366. {
  367. struct vm86_regs *regs = &vi->vms.regs;
  368. U8 opcode;
  369. int size;
  370. int pref_seg _X_UNUSED = 0, pref_rep = 0, pref_66 = 0;
  371. again:
  372. if (!Vm86IsMemory(vi, MAKE_POINTER(regs->cs, regs->eip))) {
  373. ErrorF("Trying to execute unmapped memory\n");
  374. return -1;
  375. }
  376. opcode = Vm86Memory(vi, MAKE_POINTER(regs->cs, regs->eip));
  377. switch (opcode) {
  378. case 0x2E:
  379. pref_seg = SEG_CS;
  380. AGAIN;
  381. case 0x3E:
  382. pref_seg = SEG_DS;
  383. AGAIN;
  384. case 0x26:
  385. pref_seg = SEG_ES;
  386. AGAIN;
  387. case 0x36:
  388. pref_seg = SEG_SS;
  389. AGAIN;
  390. case 0x65:
  391. pref_seg = SEG_GS;
  392. AGAIN;
  393. case 0x64:
  394. pref_seg = SEG_FS;
  395. AGAIN;
  396. case 0x66:
  397. pref_66 = 1;
  398. AGAIN;
  399. case 0x67:
  400. // pref_67 = 1;
  401. AGAIN;
  402. case 0xF2:
  403. pref_rep = REPNZ;
  404. AGAIN;
  405. case 0xF3:
  406. pref_rep = REP;
  407. AGAIN;
  408. case 0xEC: /* IN AL, DX */
  409. SET_8(regs->eax, vm86_inb(regs->edx & 0xFFFF));
  410. INC_IP(1);
  411. break;
  412. case 0xED: /* IN AX, DX */
  413. if (pref_66)
  414. regs->eax = vm86_inl(regs->edx & 0xFFFF);
  415. else
  416. SET_16(regs->eax, vm86_inw(regs->edx & 0xFFFF));
  417. INC_IP(1);
  418. break;
  419. case 0xE4: /* IN AL, imm8 */
  420. SET_8(regs->eax,
  421. vm86_inb(Vm86Memory
  422. (vi, MAKE_POINTER(regs->cs, regs->eip + 1))));
  423. INC_IP(2);
  424. break;
  425. case 0xE5: /* IN AX, imm8 */
  426. if (pref_66)
  427. regs->eax =
  428. vm86_inl(Vm86Memory
  429. (vi,
  430. MAKE_POINTER(regs->cs, regs->eip + 1)));
  431. else
  432. SET_16(regs->eax,
  433. vm86_inw(Vm86Memory
  434. (vi,
  435. MAKE_POINTER(regs->cs,
  436. regs->eip + 1))));
  437. INC_IP(2);
  438. break;
  439. case 0x6C: /* INSB */
  440. case 0x6D: /* INSW */
  441. if (opcode == 0x6C) {
  442. Vm86WriteMemory(vi, MAKE_POINTER(regs->es, regs->edi),
  443. vm86_inb(regs->edx & 0xFFFF));
  444. size = 1;
  445. } else if (pref_66) {
  446. Vm86WriteMemoryL(vi, MAKE_POINTER(regs->es, regs->edi),
  447. vm86_inl(regs->edx & 0xFFFF));
  448. size = 4;
  449. } else {
  450. Vm86WriteMemoryW(vi, MAKE_POINTER(regs->es, regs->edi),
  451. vm86_inw(regs->edx & 0xFFFF));
  452. size = 2;
  453. }
  454. if (regs->eflags & (1 << 10))
  455. regs->edi -= size;
  456. else
  457. regs->edi += size;
  458. if (pref_rep) {
  459. if (pref_66) {
  460. regs->ecx--;
  461. if (regs->ecx != 0)
  462. goto again;
  463. } else {
  464. SET_16(regs->ecx, regs->ecx - 1);
  465. if ((regs->ecx & 0xFFFF) != 0)
  466. goto again;
  467. }
  468. }
  469. INC_IP(1);
  470. break;
  471. case 0xEE: /* OUT DX, AL */
  472. vm86_outb(regs->edx & 0xFFFF, regs->eax & 0xFF);
  473. INC_IP(1);
  474. break;
  475. case 0xEF: /* OUT DX, AX */
  476. if (pref_66)
  477. vm86_outl(regs->edx & 0xFFFF, regs->eax);
  478. else
  479. vm86_outw(regs->edx & 0xFFFF, regs->eax & 0xFFFF);
  480. INC_IP(1);
  481. break;
  482. case 0xE6: /* OUT imm8, AL */
  483. vm86_outb(Vm86Memory(vi, MAKE_POINTER(regs->cs, regs->eip + 1)),
  484. regs->eax & 0xFF);
  485. INC_IP(2);
  486. break;
  487. case 0xE7: /* OUT imm8, AX */
  488. if (pref_66)
  489. vm86_outl(Vm86Memory
  490. (vi, MAKE_POINTER(regs->cs, regs->eip + 1)),
  491. regs->eax);
  492. else
  493. vm86_outw(Vm86Memory
  494. (vi, MAKE_POINTER(regs->cs, regs->eip + 1)),
  495. regs->eax & 0xFFFF);
  496. INC_IP(2);
  497. break;
  498. case 0x6E: /* OUTSB */
  499. case 0x6F: /* OUTSW */
  500. if (opcode == 0x6E) {
  501. vm86_outb(regs->edx & 0xFFFF,
  502. Vm86Memory(vi,
  503. MAKE_POINTER(regs->es,
  504. regs->edi)));
  505. size = 1;
  506. } else if (pref_66) {
  507. vm86_outl(regs->edx & 0xFFFF,
  508. Vm86Memory(vi,
  509. MAKE_POINTER(regs->es,
  510. regs->edi)));
  511. size = 4;
  512. } else {
  513. vm86_outw(regs->edx & 0xFFFF,
  514. Vm86Memory(vi,
  515. MAKE_POINTER(regs->es,
  516. regs->edi)));
  517. size = 2;
  518. }
  519. if (regs->eflags & (1 << 10))
  520. regs->edi -= size;
  521. else
  522. regs->edi += size;
  523. if (pref_rep) {
  524. if (pref_66) {
  525. regs->ecx--;
  526. if (regs->ecx != 0)
  527. goto again;
  528. } else {
  529. SET_16(regs->ecx, regs->ecx - 1);
  530. if ((regs->ecx & 0xFFFF) != 0)
  531. goto again;
  532. }
  533. }
  534. INC_IP(1);
  535. break;
  536. case 0x0F:
  537. ErrorF("Hit 0F trap in VM86 code\n");
  538. return -1;
  539. case 0xF0:
  540. ErrorF("Hit lock prefix in VM86 code\n");
  541. return -1;
  542. case 0xF4:
  543. ErrorF("Hit HLT in VM86 code\n");
  544. return -1;
  545. default:
  546. ErrorF("Unhandled GP fault in VM86 code (opcode = 0x%02X)\n",
  547. opcode);
  548. return -1;
  549. }
  550. return 0;
  551. }
  552. #undef SEG_CS
  553. #undef SEG_DS
  554. #undef SEG_ES
  555. #undef SEG_SS
  556. #undef SEG_GS
  557. #undef SEG_FS
  558. #undef REP
  559. #undef REPNZ
  560. #undef SET_8
  561. #undef SET_16
  562. #undef INC_IP
  563. #undef AGAIN
  564. static void Vm86Debug(Vm86InfoPtr vi)
  565. {
  566. struct vm86_regs *regs = &vi->vms.regs;
  567. int i;
  568. ErrorF("eax=0x%08lX ebx=0x%08lX ecx=0x%08lX edx=0x%08lX\n",
  569. regs->eax, regs->ebx, regs->ecx, regs->edx);
  570. ErrorF("esi=0x%08lX edi=0x%08lX ebp=0x%08lX\n",
  571. regs->esi, regs->edi, regs->ebp);
  572. ErrorF("eip=0x%08lX esp=0x%08lX eflags=0x%08lX\n",
  573. regs->eip, regs->esp, regs->eflags);
  574. ErrorF
  575. ("cs=0x%04X ds=0x%04X es=0x%04X fs=0x%04X gs=0x%04X\n",
  576. regs->cs, regs->ds, regs->es, regs->fs, regs->gs);
  577. for (i = -7; i < 8; i++) {
  578. ErrorF(" %s%02X",
  579. i == 0 ? "->" : "",
  580. Vm86Memory(vi, MAKE_POINTER(regs->cs, regs->eip + i)));
  581. }
  582. ErrorF("\n");
  583. }
  584. static int vm86_loop(Vm86InfoPtr vi)
  585. {
  586. int code;
  587. while (1) {
  588. code = vm86old(&vi->vms);
  589. switch (VM86_TYPE(code)) {
  590. case VM86_SIGNAL:
  591. continue;
  592. case VM86_UNKNOWN:
  593. code = vm86_emulate(vi);
  594. if (code < 0) {
  595. Vm86Debug(vi);
  596. return -1;
  597. }
  598. break;
  599. case VM86_INTx:
  600. if (VM86_ARG(code) == 0xFF)
  601. return 0;
  602. else {
  603. PUSHW(vi, vi->vms.regs.eflags)
  604. PUSHW(vi, vi->vms.regs.cs);
  605. PUSHW(vi, vi->vms.regs.eip);
  606. vi->vms.regs.cs =
  607. MMW(vi, VM86_ARG(code) * 4 + 2);
  608. vi->vms.regs.eip = MMW(vi, VM86_ARG(code) * 4);
  609. }
  610. break;
  611. case VM86_STI:
  612. ErrorF("VM86 code enabled interrupts\n");
  613. Vm86Debug(vi);
  614. return -1;
  615. default:
  616. ErrorF("Unexpected result code 0x%X from vm86\n", code);
  617. Vm86Debug(vi);
  618. return -1;
  619. }
  620. }
  621. }
  622. U8 Vm86Memory(Vm86InfoPtr vi, U32 i)
  623. {
  624. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  625. return MM(vi, i);
  626. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  627. return LM(vi, i);
  628. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  629. return HM(vi, i);
  630. else {
  631. ErrorF("Reading unmapped memory at 0x%08X\n", i);
  632. return 0;
  633. }
  634. }
  635. U16 Vm86MemoryW(Vm86InfoPtr vi, U32 i)
  636. {
  637. if (i >= MAGICMEM_BASE && i < MAGICMEM_BASE + MAGICMEM_SIZE)
  638. return MMW(vi, i);
  639. else if (i >= LOMEM_BASE && i < LOMEM_BASE + LOMEM_SIZE)
  640. return LMW(vi, i);
  641. else if (i >= HIMEM_BASE && i < HIMEM_BASE + HIMEM_SIZE)
  642. return HMW(vi, i);
  643. else {
  644. ErrorF("Reading unmapped memory at 0x%08X\n", i);
  645. return 0;
  646. }
  647. }
  648. int Vm86AllocateMemory(Vm86InfoPtr vi, int n)
  649. {
  650. int ret;
  651. if (n < 0) {
  652. ErrorF("Asked to allocate negative amount of memory\n");
  653. return vi->brk;
  654. }
  655. n = (n + 15) & ~15;
  656. if (vi->brk + n > LOMEM_BASE + LOMEM_SIZE) {
  657. ErrorF("Out of low memory\n");
  658. exit(2);
  659. }
  660. ret = vi->brk;
  661. vi->brk += n;
  662. return ret;
  663. }
  664. int Vm86MarkMemory(Vm86InfoPtr vi)
  665. {
  666. return vi->brk;
  667. }
  668. void Vm86ReleaseMemory(Vm86InfoPtr vi, int mark)
  669. {
  670. vi->brk = mark;
  671. }
  672. static int vm86old(struct vm86_struct *vm)
  673. {
  674. int res = -1;
  675. #ifndef __x86_64__
  676. asm volatile ("pushl %%ebx\n\t"
  677. "movl %2, %%ebx\n\t"
  678. "movl %1,%%eax\n\t"
  679. "int $0x80\n\t"
  680. "popl %%ebx":"=a" (res):"n"(113), "r"(vm));
  681. #endif
  682. if (res < 0) {
  683. errno = -res;
  684. res = -1;
  685. } else
  686. errno = 0;
  687. return res;
  688. }