vm86_32.c 22 KB

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