vm86.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * linux/kernel/vm86.c
  3. *
  4. * Copyright (C) 1994 Linus Torvalds
  5. *
  6. * 29 dec 2001 - Fixed oopses caused by unchecked access to the vm86
  7. * stack - Manfred Spraul <manfred@colorfullife.com>
  8. *
  9. * 22 mar 2002 - Manfred detected the stackfaults, but didn't handle
  10. * them correctly. Now the emulation will be in a
  11. * consistent state after stackfaults - Kasper Dupont
  12. * <kasperd@daimi.au.dk>
  13. *
  14. * 22 mar 2002 - Added missing clear_IF in set_vflags_* Kasper Dupont
  15. * <kasperd@daimi.au.dk>
  16. *
  17. * ?? ??? 2002 - Fixed premature returns from handle_vm86_fault
  18. * caused by Kasper Dupont's changes - Stas Sergeev
  19. *
  20. * 4 apr 2002 - Fixed CHECK_IF_IN_TRAP broken by Stas' changes.
  21. * Kasper Dupont <kasperd@daimi.au.dk>
  22. *
  23. * 9 apr 2002 - Changed syntax of macros in handle_vm86_fault.
  24. * Kasper Dupont <kasperd@daimi.au.dk>
  25. *
  26. * 9 apr 2002 - Changed stack access macros to jump to a label
  27. * instead of returning to userspace. This simplifies
  28. * do_int, and is needed by handle_vm6_fault. Kasper
  29. * Dupont <kasperd@daimi.au.dk>
  30. *
  31. */
  32. #include <linux/capability.h>
  33. #include <linux/config.h>
  34. #include <linux/errno.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/sched.h>
  37. #include <linux/kernel.h>
  38. #include <linux/signal.h>
  39. #include <linux/string.h>
  40. #include <linux/mm.h>
  41. #include <linux/smp.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/highmem.h>
  44. #include <linux/ptrace.h>
  45. #include <linux/audit.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/io.h>
  48. #include <asm/tlbflush.h>
  49. #include <asm/irq.h>
  50. /*
  51. * Known problems:
  52. *
  53. * Interrupt handling is not guaranteed:
  54. * - a real x86 will disable all interrupts for one instruction
  55. * after a "mov ss,xx" to make stack handling atomic even without
  56. * the 'lss' instruction. We can't guarantee this in v86 mode,
  57. * as the next instruction might result in a page fault or similar.
  58. * - a real x86 will have interrupts disabled for one instruction
  59. * past the 'sti' that enables them. We don't bother with all the
  60. * details yet.
  61. *
  62. * Let's hope these problems do not actually matter for anything.
  63. */
  64. #define KVM86 ((struct kernel_vm86_struct *)regs)
  65. #define VMPI KVM86->vm86plus
  66. /*
  67. * 8- and 16-bit register defines..
  68. */
  69. #define AL(regs) (((unsigned char *)&((regs)->eax))[0])
  70. #define AH(regs) (((unsigned char *)&((regs)->eax))[1])
  71. #define IP(regs) (*(unsigned short *)&((regs)->eip))
  72. #define SP(regs) (*(unsigned short *)&((regs)->esp))
  73. /*
  74. * virtual flags (16 and 32-bit versions)
  75. */
  76. #define VFLAGS (*(unsigned short *)&(current->thread.v86flags))
  77. #define VEFLAGS (current->thread.v86flags)
  78. #define set_flags(X,new,mask) \
  79. ((X) = ((X) & ~(mask)) | ((new) & (mask)))
  80. #define SAFE_MASK (0xDD5)
  81. #define RETURN_MASK (0xDFF)
  82. #define VM86_REGS_PART2 orig_eax
  83. #define VM86_REGS_SIZE1 \
  84. ( (unsigned)( & (((struct kernel_vm86_regs *)0)->VM86_REGS_PART2) ) )
  85. #define VM86_REGS_SIZE2 (sizeof(struct kernel_vm86_regs) - VM86_REGS_SIZE1)
  86. struct pt_regs * FASTCALL(save_v86_state(struct kernel_vm86_regs * regs));
  87. struct pt_regs * fastcall save_v86_state(struct kernel_vm86_regs * regs)
  88. {
  89. struct tss_struct *tss;
  90. struct pt_regs *ret;
  91. unsigned long tmp;
  92. /*
  93. * This gets called from entry.S with interrupts disabled, but
  94. * from process context. Enable interrupts here, before trying
  95. * to access user space.
  96. */
  97. local_irq_enable();
  98. if (!current->thread.vm86_info) {
  99. printk("no vm86_info: BAD\n");
  100. do_exit(SIGSEGV);
  101. }
  102. set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->thread.v86mask);
  103. tmp = copy_to_user(&current->thread.vm86_info->regs,regs, VM86_REGS_SIZE1);
  104. tmp += copy_to_user(&current->thread.vm86_info->regs.VM86_REGS_PART2,
  105. &regs->VM86_REGS_PART2, VM86_REGS_SIZE2);
  106. tmp += put_user(current->thread.screen_bitmap,&current->thread.vm86_info->screen_bitmap);
  107. if (tmp) {
  108. printk("vm86: could not access userspace vm86_info\n");
  109. do_exit(SIGSEGV);
  110. }
  111. tss = &per_cpu(init_tss, get_cpu());
  112. current->thread.esp0 = current->thread.saved_esp0;
  113. current->thread.sysenter_cs = __KERNEL_CS;
  114. load_esp0(tss, &current->thread);
  115. current->thread.saved_esp0 = 0;
  116. put_cpu();
  117. loadsegment(fs, current->thread.saved_fs);
  118. loadsegment(gs, current->thread.saved_gs);
  119. ret = KVM86->regs32;
  120. return ret;
  121. }
  122. static void mark_screen_rdonly(struct mm_struct *mm)
  123. {
  124. pgd_t *pgd;
  125. pud_t *pud;
  126. pmd_t *pmd;
  127. pte_t *pte;
  128. spinlock_t *ptl;
  129. int i;
  130. pgd = pgd_offset(mm, 0xA0000);
  131. if (pgd_none_or_clear_bad(pgd))
  132. goto out;
  133. pud = pud_offset(pgd, 0xA0000);
  134. if (pud_none_or_clear_bad(pud))
  135. goto out;
  136. pmd = pmd_offset(pud, 0xA0000);
  137. if (pmd_none_or_clear_bad(pmd))
  138. goto out;
  139. pte = pte_offset_map_lock(mm, pmd, 0xA0000, &ptl);
  140. for (i = 0; i < 32; i++) {
  141. if (pte_present(*pte))
  142. set_pte(pte, pte_wrprotect(*pte));
  143. pte++;
  144. }
  145. pte_unmap_unlock(pte, ptl);
  146. out:
  147. flush_tlb();
  148. }
  149. static int do_vm86_irq_handling(int subfunction, int irqnumber);
  150. static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
  151. asmlinkage int sys_vm86old(struct pt_regs regs)
  152. {
  153. struct vm86_struct __user *v86 = (struct vm86_struct __user *)regs.ebx;
  154. struct kernel_vm86_struct info; /* declare this _on top_,
  155. * this avoids wasting of stack space.
  156. * This remains on the stack until we
  157. * return to 32 bit user space.
  158. */
  159. struct task_struct *tsk;
  160. int tmp, ret = -EPERM;
  161. tsk = current;
  162. if (tsk->thread.saved_esp0)
  163. goto out;
  164. tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1);
  165. tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,
  166. (long)&info.vm86plus - (long)&info.regs.VM86_REGS_PART2);
  167. ret = -EFAULT;
  168. if (tmp)
  169. goto out;
  170. memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
  171. info.regs32 = &regs;
  172. tsk->thread.vm86_info = v86;
  173. do_sys_vm86(&info, tsk);
  174. ret = 0; /* we never return here */
  175. out:
  176. return ret;
  177. }
  178. asmlinkage int sys_vm86(struct pt_regs regs)
  179. {
  180. struct kernel_vm86_struct info; /* declare this _on top_,
  181. * this avoids wasting of stack space.
  182. * This remains on the stack until we
  183. * return to 32 bit user space.
  184. */
  185. struct task_struct *tsk;
  186. int tmp, ret;
  187. struct vm86plus_struct __user *v86;
  188. tsk = current;
  189. switch (regs.ebx) {
  190. case VM86_REQUEST_IRQ:
  191. case VM86_FREE_IRQ:
  192. case VM86_GET_IRQ_BITS:
  193. case VM86_GET_AND_RESET_IRQ:
  194. ret = do_vm86_irq_handling(regs.ebx, (int)regs.ecx);
  195. goto out;
  196. case VM86_PLUS_INSTALL_CHECK:
  197. /* NOTE: on old vm86 stuff this will return the error
  198. from access_ok(), because the subfunction is
  199. interpreted as (invalid) address to vm86_struct.
  200. So the installation check works.
  201. */
  202. ret = 0;
  203. goto out;
  204. }
  205. /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
  206. ret = -EPERM;
  207. if (tsk->thread.saved_esp0)
  208. goto out;
  209. v86 = (struct vm86plus_struct __user *)regs.ecx;
  210. tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1);
  211. tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,
  212. (long)&info.regs32 - (long)&info.regs.VM86_REGS_PART2);
  213. ret = -EFAULT;
  214. if (tmp)
  215. goto out;
  216. info.regs32 = &regs;
  217. info.vm86plus.is_vm86pus = 1;
  218. tsk->thread.vm86_info = (struct vm86_struct __user *)v86;
  219. do_sys_vm86(&info, tsk);
  220. ret = 0; /* we never return here */
  221. out:
  222. return ret;
  223. }
  224. static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
  225. {
  226. struct tss_struct *tss;
  227. long eax;
  228. /*
  229. * make sure the vm86() system call doesn't try to do anything silly
  230. */
  231. info->regs.__null_ds = 0;
  232. info->regs.__null_es = 0;
  233. /* we are clearing fs,gs later just before "jmp resume_userspace",
  234. * because starting with Linux 2.1.x they aren't no longer saved/restored
  235. */
  236. /*
  237. * The eflags register is also special: we cannot trust that the user
  238. * has set it up safely, so this makes sure interrupt etc flags are
  239. * inherited from protected mode.
  240. */
  241. VEFLAGS = info->regs.eflags;
  242. info->regs.eflags &= SAFE_MASK;
  243. info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
  244. info->regs.eflags |= VM_MASK;
  245. switch (info->cpu_type) {
  246. case CPU_286:
  247. tsk->thread.v86mask = 0;
  248. break;
  249. case CPU_386:
  250. tsk->thread.v86mask = NT_MASK | IOPL_MASK;
  251. break;
  252. case CPU_486:
  253. tsk->thread.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
  254. break;
  255. default:
  256. tsk->thread.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
  257. break;
  258. }
  259. /*
  260. * Save old state, set default return value (%eax) to 0
  261. */
  262. info->regs32->eax = 0;
  263. tsk->thread.saved_esp0 = tsk->thread.esp0;
  264. savesegment(fs, tsk->thread.saved_fs);
  265. savesegment(gs, tsk->thread.saved_gs);
  266. tss = &per_cpu(init_tss, get_cpu());
  267. tsk->thread.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
  268. if (cpu_has_sep)
  269. tsk->thread.sysenter_cs = 0;
  270. load_esp0(tss, &tsk->thread);
  271. put_cpu();
  272. tsk->thread.screen_bitmap = info->screen_bitmap;
  273. if (info->flags & VM86_SCREEN_BITMAP)
  274. mark_screen_rdonly(tsk->mm);
  275. __asm__ __volatile__("xorl %eax,%eax; movl %eax,%fs; movl %eax,%gs\n\t");
  276. __asm__ __volatile__("movl %%eax, %0\n" :"=r"(eax));
  277. /*call audit_syscall_exit since we do not exit via the normal paths */
  278. if (unlikely(current->audit_context))
  279. audit_syscall_exit(current, AUDITSC_RESULT(eax), eax);
  280. __asm__ __volatile__(
  281. "movl %0,%%esp\n\t"
  282. "movl %1,%%ebp\n\t"
  283. "jmp resume_userspace"
  284. : /* no outputs */
  285. :"r" (&info->regs), "r" (task_thread_info(tsk)));
  286. /* we never return here */
  287. }
  288. static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval)
  289. {
  290. struct pt_regs * regs32;
  291. regs32 = save_v86_state(regs16);
  292. regs32->eax = retval;
  293. __asm__ __volatile__("movl %0,%%esp\n\t"
  294. "movl %1,%%ebp\n\t"
  295. "jmp resume_userspace"
  296. : : "r" (regs32), "r" (current_thread_info()));
  297. }
  298. static inline void set_IF(struct kernel_vm86_regs * regs)
  299. {
  300. VEFLAGS |= VIF_MASK;
  301. if (VEFLAGS & VIP_MASK)
  302. return_to_32bit(regs, VM86_STI);
  303. }
  304. static inline void clear_IF(struct kernel_vm86_regs * regs)
  305. {
  306. VEFLAGS &= ~VIF_MASK;
  307. }
  308. static inline void clear_TF(struct kernel_vm86_regs * regs)
  309. {
  310. regs->eflags &= ~TF_MASK;
  311. }
  312. static inline void clear_AC(struct kernel_vm86_regs * regs)
  313. {
  314. regs->eflags &= ~AC_MASK;
  315. }
  316. /* It is correct to call set_IF(regs) from the set_vflags_*
  317. * functions. However someone forgot to call clear_IF(regs)
  318. * in the opposite case.
  319. * After the command sequence CLI PUSHF STI POPF you should
  320. * end up with interrups disabled, but you ended up with
  321. * interrupts enabled.
  322. * ( I was testing my own changes, but the only bug I
  323. * could find was in a function I had not changed. )
  324. * [KD]
  325. */
  326. static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs)
  327. {
  328. set_flags(VEFLAGS, eflags, current->thread.v86mask);
  329. set_flags(regs->eflags, eflags, SAFE_MASK);
  330. if (eflags & IF_MASK)
  331. set_IF(regs);
  332. else
  333. clear_IF(regs);
  334. }
  335. static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs)
  336. {
  337. set_flags(VFLAGS, flags, current->thread.v86mask);
  338. set_flags(regs->eflags, flags, SAFE_MASK);
  339. if (flags & IF_MASK)
  340. set_IF(regs);
  341. else
  342. clear_IF(regs);
  343. }
  344. static inline unsigned long get_vflags(struct kernel_vm86_regs * regs)
  345. {
  346. unsigned long flags = regs->eflags & RETURN_MASK;
  347. if (VEFLAGS & VIF_MASK)
  348. flags |= IF_MASK;
  349. flags |= IOPL_MASK;
  350. return flags | (VEFLAGS & current->thread.v86mask);
  351. }
  352. static inline int is_revectored(int nr, struct revectored_struct * bitmap)
  353. {
  354. __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
  355. :"=r" (nr)
  356. :"m" (*bitmap),"r" (nr));
  357. return nr;
  358. }
  359. #define val_byte(val, n) (((__u8 *)&val)[n])
  360. #define pushb(base, ptr, val, err_label) \
  361. do { \
  362. __u8 __val = val; \
  363. ptr--; \
  364. if (put_user(__val, base + ptr) < 0) \
  365. goto err_label; \
  366. } while(0)
  367. #define pushw(base, ptr, val, err_label) \
  368. do { \
  369. __u16 __val = val; \
  370. ptr--; \
  371. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  372. goto err_label; \
  373. ptr--; \
  374. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  375. goto err_label; \
  376. } while(0)
  377. #define pushl(base, ptr, val, err_label) \
  378. do { \
  379. __u32 __val = val; \
  380. ptr--; \
  381. if (put_user(val_byte(__val, 3), base + ptr) < 0) \
  382. goto err_label; \
  383. ptr--; \
  384. if (put_user(val_byte(__val, 2), base + ptr) < 0) \
  385. goto err_label; \
  386. ptr--; \
  387. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  388. goto err_label; \
  389. ptr--; \
  390. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  391. goto err_label; \
  392. } while(0)
  393. #define popb(base, ptr, err_label) \
  394. ({ \
  395. __u8 __res; \
  396. if (get_user(__res, base + ptr) < 0) \
  397. goto err_label; \
  398. ptr++; \
  399. __res; \
  400. })
  401. #define popw(base, ptr, err_label) \
  402. ({ \
  403. __u16 __res; \
  404. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  405. goto err_label; \
  406. ptr++; \
  407. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  408. goto err_label; \
  409. ptr++; \
  410. __res; \
  411. })
  412. #define popl(base, ptr, err_label) \
  413. ({ \
  414. __u32 __res; \
  415. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  416. goto err_label; \
  417. ptr++; \
  418. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  419. goto err_label; \
  420. ptr++; \
  421. if (get_user(val_byte(__res, 2), base + ptr) < 0) \
  422. goto err_label; \
  423. ptr++; \
  424. if (get_user(val_byte(__res, 3), base + ptr) < 0) \
  425. goto err_label; \
  426. ptr++; \
  427. __res; \
  428. })
  429. /* There are so many possible reasons for this function to return
  430. * VM86_INTx, so adding another doesn't bother me. We can expect
  431. * userspace programs to be able to handle it. (Getting a problem
  432. * in userspace is always better than an Oops anyway.) [KD]
  433. */
  434. static void do_int(struct kernel_vm86_regs *regs, int i,
  435. unsigned char __user * ssp, unsigned short sp)
  436. {
  437. unsigned long __user *intr_ptr;
  438. unsigned long segoffs;
  439. if (regs->cs == BIOSSEG)
  440. goto cannot_handle;
  441. if (is_revectored(i, &KVM86->int_revectored))
  442. goto cannot_handle;
  443. if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
  444. goto cannot_handle;
  445. intr_ptr = (unsigned long __user *) (i << 2);
  446. if (get_user(segoffs, intr_ptr))
  447. goto cannot_handle;
  448. if ((segoffs >> 16) == BIOSSEG)
  449. goto cannot_handle;
  450. pushw(ssp, sp, get_vflags(regs), cannot_handle);
  451. pushw(ssp, sp, regs->cs, cannot_handle);
  452. pushw(ssp, sp, IP(regs), cannot_handle);
  453. regs->cs = segoffs >> 16;
  454. SP(regs) -= 6;
  455. IP(regs) = segoffs & 0xffff;
  456. clear_TF(regs);
  457. clear_IF(regs);
  458. clear_AC(regs);
  459. return;
  460. cannot_handle:
  461. return_to_32bit(regs, VM86_INTx + (i << 8));
  462. }
  463. int handle_vm86_trap(struct kernel_vm86_regs * regs, long error_code, int trapno)
  464. {
  465. if (VMPI.is_vm86pus) {
  466. if ( (trapno==3) || (trapno==1) )
  467. return_to_32bit(regs, VM86_TRAP + (trapno << 8));
  468. do_int(regs, trapno, (unsigned char __user *) (regs->ss << 4), SP(regs));
  469. return 0;
  470. }
  471. if (trapno !=1)
  472. return 1; /* we let this handle by the calling routine */
  473. if (current->ptrace & PT_PTRACED) {
  474. unsigned long flags;
  475. spin_lock_irqsave(&current->sighand->siglock, flags);
  476. sigdelset(&current->blocked, SIGTRAP);
  477. recalc_sigpending();
  478. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  479. }
  480. send_sig(SIGTRAP, current, 1);
  481. current->thread.trap_no = trapno;
  482. current->thread.error_code = error_code;
  483. return 0;
  484. }
  485. void handle_vm86_fault(struct kernel_vm86_regs * regs, long error_code)
  486. {
  487. unsigned char opcode;
  488. unsigned char __user *csp;
  489. unsigned char __user *ssp;
  490. unsigned short ip, sp, orig_flags;
  491. int data32, pref_done;
  492. #define CHECK_IF_IN_TRAP \
  493. if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
  494. newflags |= TF_MASK
  495. #define VM86_FAULT_RETURN do { \
  496. if (VMPI.force_return_for_pic && (VEFLAGS & (IF_MASK | VIF_MASK))) \
  497. return_to_32bit(regs, VM86_PICRETURN); \
  498. if (orig_flags & TF_MASK) \
  499. handle_vm86_trap(regs, 0, 1); \
  500. return; } while (0)
  501. orig_flags = *(unsigned short *)&regs->eflags;
  502. csp = (unsigned char __user *) (regs->cs << 4);
  503. ssp = (unsigned char __user *) (regs->ss << 4);
  504. sp = SP(regs);
  505. ip = IP(regs);
  506. data32 = 0;
  507. pref_done = 0;
  508. do {
  509. switch (opcode = popb(csp, ip, simulate_sigsegv)) {
  510. case 0x66: /* 32-bit data */ data32=1; break;
  511. case 0x67: /* 32-bit address */ break;
  512. case 0x2e: /* CS */ break;
  513. case 0x3e: /* DS */ break;
  514. case 0x26: /* ES */ break;
  515. case 0x36: /* SS */ break;
  516. case 0x65: /* GS */ break;
  517. case 0x64: /* FS */ break;
  518. case 0xf2: /* repnz */ break;
  519. case 0xf3: /* rep */ break;
  520. default: pref_done = 1;
  521. }
  522. } while (!pref_done);
  523. switch (opcode) {
  524. /* pushf */
  525. case 0x9c:
  526. if (data32) {
  527. pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
  528. SP(regs) -= 4;
  529. } else {
  530. pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
  531. SP(regs) -= 2;
  532. }
  533. IP(regs) = ip;
  534. VM86_FAULT_RETURN;
  535. /* popf */
  536. case 0x9d:
  537. {
  538. unsigned long newflags;
  539. if (data32) {
  540. newflags=popl(ssp, sp, simulate_sigsegv);
  541. SP(regs) += 4;
  542. } else {
  543. newflags = popw(ssp, sp, simulate_sigsegv);
  544. SP(regs) += 2;
  545. }
  546. IP(regs) = ip;
  547. CHECK_IF_IN_TRAP;
  548. if (data32) {
  549. set_vflags_long(newflags, regs);
  550. } else {
  551. set_vflags_short(newflags, regs);
  552. }
  553. VM86_FAULT_RETURN;
  554. }
  555. /* int xx */
  556. case 0xcd: {
  557. int intno=popb(csp, ip, simulate_sigsegv);
  558. IP(regs) = ip;
  559. if (VMPI.vm86dbg_active) {
  560. if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
  561. return_to_32bit(regs, VM86_INTx + (intno << 8));
  562. }
  563. do_int(regs, intno, ssp, sp);
  564. return;
  565. }
  566. /* iret */
  567. case 0xcf:
  568. {
  569. unsigned long newip;
  570. unsigned long newcs;
  571. unsigned long newflags;
  572. if (data32) {
  573. newip=popl(ssp, sp, simulate_sigsegv);
  574. newcs=popl(ssp, sp, simulate_sigsegv);
  575. newflags=popl(ssp, sp, simulate_sigsegv);
  576. SP(regs) += 12;
  577. } else {
  578. newip = popw(ssp, sp, simulate_sigsegv);
  579. newcs = popw(ssp, sp, simulate_sigsegv);
  580. newflags = popw(ssp, sp, simulate_sigsegv);
  581. SP(regs) += 6;
  582. }
  583. IP(regs) = newip;
  584. regs->cs = newcs;
  585. CHECK_IF_IN_TRAP;
  586. if (data32) {
  587. set_vflags_long(newflags, regs);
  588. } else {
  589. set_vflags_short(newflags, regs);
  590. }
  591. VM86_FAULT_RETURN;
  592. }
  593. /* cli */
  594. case 0xfa:
  595. IP(regs) = ip;
  596. clear_IF(regs);
  597. VM86_FAULT_RETURN;
  598. /* sti */
  599. /*
  600. * Damn. This is incorrect: the 'sti' instruction should actually
  601. * enable interrupts after the /next/ instruction. Not good.
  602. *
  603. * Probably needs some horsing around with the TF flag. Aiee..
  604. */
  605. case 0xfb:
  606. IP(regs) = ip;
  607. set_IF(regs);
  608. VM86_FAULT_RETURN;
  609. default:
  610. return_to_32bit(regs, VM86_UNKNOWN);
  611. }
  612. return;
  613. simulate_sigsegv:
  614. /* FIXME: After a long discussion with Stas we finally
  615. * agreed, that this is wrong. Here we should
  616. * really send a SIGSEGV to the user program.
  617. * But how do we create the correct context? We
  618. * are inside a general protection fault handler
  619. * and has just returned from a page fault handler.
  620. * The correct context for the signal handler
  621. * should be a mixture of the two, but how do we
  622. * get the information? [KD]
  623. */
  624. return_to_32bit(regs, VM86_UNKNOWN);
  625. }
  626. /* ---------------- vm86 special IRQ passing stuff ----------------- */
  627. #define VM86_IRQNAME "vm86irq"
  628. static struct vm86_irqs {
  629. struct task_struct *tsk;
  630. int sig;
  631. } vm86_irqs[16];
  632. static DEFINE_SPINLOCK(irqbits_lock);
  633. static int irqbits;
  634. #define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
  635. | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
  636. | (1 << SIGUNUSED) )
  637. static irqreturn_t irq_handler(int intno, void *dev_id, struct pt_regs * regs)
  638. {
  639. int irq_bit;
  640. unsigned long flags;
  641. spin_lock_irqsave(&irqbits_lock, flags);
  642. irq_bit = 1 << intno;
  643. if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk)
  644. goto out;
  645. irqbits |= irq_bit;
  646. if (vm86_irqs[intno].sig)
  647. send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
  648. /*
  649. * IRQ will be re-enabled when user asks for the irq (whether
  650. * polling or as a result of the signal)
  651. */
  652. disable_irq_nosync(intno);
  653. spin_unlock_irqrestore(&irqbits_lock, flags);
  654. return IRQ_HANDLED;
  655. out:
  656. spin_unlock_irqrestore(&irqbits_lock, flags);
  657. return IRQ_NONE;
  658. }
  659. static inline void free_vm86_irq(int irqnumber)
  660. {
  661. unsigned long flags;
  662. free_irq(irqnumber, NULL);
  663. vm86_irqs[irqnumber].tsk = NULL;
  664. spin_lock_irqsave(&irqbits_lock, flags);
  665. irqbits &= ~(1 << irqnumber);
  666. spin_unlock_irqrestore(&irqbits_lock, flags);
  667. }
  668. void release_vm86_irqs(struct task_struct *task)
  669. {
  670. int i;
  671. for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
  672. if (vm86_irqs[i].tsk == task)
  673. free_vm86_irq(i);
  674. }
  675. static inline int get_and_reset_irq(int irqnumber)
  676. {
  677. int bit;
  678. unsigned long flags;
  679. int ret = 0;
  680. if (invalid_vm86_irq(irqnumber)) return 0;
  681. if (vm86_irqs[irqnumber].tsk != current) return 0;
  682. spin_lock_irqsave(&irqbits_lock, flags);
  683. bit = irqbits & (1 << irqnumber);
  684. irqbits &= ~bit;
  685. if (bit) {
  686. enable_irq(irqnumber);
  687. ret = 1;
  688. }
  689. spin_unlock_irqrestore(&irqbits_lock, flags);
  690. return ret;
  691. }
  692. static int do_vm86_irq_handling(int subfunction, int irqnumber)
  693. {
  694. int ret;
  695. switch (subfunction) {
  696. case VM86_GET_AND_RESET_IRQ: {
  697. return get_and_reset_irq(irqnumber);
  698. }
  699. case VM86_GET_IRQ_BITS: {
  700. return irqbits;
  701. }
  702. case VM86_REQUEST_IRQ: {
  703. int sig = irqnumber >> 8;
  704. int irq = irqnumber & 255;
  705. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  706. if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
  707. if (invalid_vm86_irq(irq)) return -EPERM;
  708. if (vm86_irqs[irq].tsk) return -EPERM;
  709. ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
  710. if (ret) return ret;
  711. vm86_irqs[irq].sig = sig;
  712. vm86_irqs[irq].tsk = current;
  713. return irq;
  714. }
  715. case VM86_FREE_IRQ: {
  716. if (invalid_vm86_irq(irqnumber)) return -EPERM;
  717. if (!vm86_irqs[irqnumber].tsk) return 0;
  718. if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
  719. free_vm86_irq(irqnumber);
  720. return 0;
  721. }
  722. }
  723. return -EINVAL;
  724. }