vm86_32.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. down_write(&mm->mmap_sem);
  152. pgd = pgd_offset(mm, 0xA0000);
  153. if (pgd_none_or_clear_bad(pgd))
  154. goto out;
  155. pud = pud_offset(pgd, 0xA0000);
  156. if (pud_none_or_clear_bad(pud))
  157. goto out;
  158. pmd = pmd_offset(pud, 0xA0000);
  159. split_huge_page_pmd(mm, pmd);
  160. if (pmd_none_or_clear_bad(pmd))
  161. goto out;
  162. pte = pte_offset_map_lock(mm, pmd, 0xA0000, &ptl);
  163. for (i = 0; i < 32; i++) {
  164. if (pte_present(*pte))
  165. set_pte(pte, pte_wrprotect(*pte));
  166. pte++;
  167. }
  168. pte_unmap_unlock(pte, ptl);
  169. out:
  170. up_write(&mm->mmap_sem);
  171. flush_tlb();
  172. }
  173. static int do_vm86_irq_handling(int subfunction, int irqnumber);
  174. static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
  175. int sys_vm86old(struct vm86_struct __user *v86, struct pt_regs *regs)
  176. {
  177. struct kernel_vm86_struct info; /* declare this _on top_,
  178. * this avoids wasting of stack space.
  179. * This remains on the stack until we
  180. * return to 32 bit user space.
  181. */
  182. struct task_struct *tsk;
  183. int tmp, ret = -EPERM;
  184. tsk = current;
  185. if (tsk->thread.saved_sp0)
  186. goto out;
  187. tmp = copy_vm86_regs_from_user(&info.regs, &v86->regs,
  188. offsetof(struct kernel_vm86_struct, vm86plus) -
  189. sizeof(info.regs));
  190. ret = -EFAULT;
  191. if (tmp)
  192. goto out;
  193. memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
  194. info.regs32 = regs;
  195. tsk->thread.vm86_info = v86;
  196. do_sys_vm86(&info, tsk);
  197. ret = 0; /* we never return here */
  198. out:
  199. return ret;
  200. }
  201. int sys_vm86(unsigned long cmd, unsigned long arg, struct pt_regs *regs)
  202. {
  203. struct kernel_vm86_struct info; /* declare this _on top_,
  204. * this avoids wasting of stack space.
  205. * This remains on the stack until we
  206. * return to 32 bit user space.
  207. */
  208. struct task_struct *tsk;
  209. int tmp, ret;
  210. struct vm86plus_struct __user *v86;
  211. tsk = current;
  212. switch (cmd) {
  213. case VM86_REQUEST_IRQ:
  214. case VM86_FREE_IRQ:
  215. case VM86_GET_IRQ_BITS:
  216. case VM86_GET_AND_RESET_IRQ:
  217. ret = do_vm86_irq_handling(cmd, (int)arg);
  218. goto out;
  219. case VM86_PLUS_INSTALL_CHECK:
  220. /*
  221. * NOTE: on old vm86 stuff this will return the error
  222. * from access_ok(), because the subfunction is
  223. * interpreted as (invalid) address to vm86_struct.
  224. * So the installation check works.
  225. */
  226. ret = 0;
  227. goto out;
  228. }
  229. /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
  230. ret = -EPERM;
  231. if (tsk->thread.saved_sp0)
  232. goto out;
  233. v86 = (struct vm86plus_struct __user *)arg;
  234. tmp = copy_vm86_regs_from_user(&info.regs, &v86->regs,
  235. offsetof(struct kernel_vm86_struct, regs32) -
  236. sizeof(info.regs));
  237. ret = -EFAULT;
  238. if (tmp)
  239. goto out;
  240. info.regs32 = regs;
  241. info.vm86plus.is_vm86pus = 1;
  242. tsk->thread.vm86_info = (struct vm86_struct __user *)v86;
  243. do_sys_vm86(&info, tsk);
  244. ret = 0; /* we never return here */
  245. out:
  246. return ret;
  247. }
  248. static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
  249. {
  250. struct tss_struct *tss;
  251. /*
  252. * make sure the vm86() system call doesn't try to do anything silly
  253. */
  254. info->regs.pt.ds = 0;
  255. info->regs.pt.es = 0;
  256. info->regs.pt.fs = 0;
  257. #ifndef CONFIG_X86_32_LAZY_GS
  258. info->regs.pt.gs = 0;
  259. #endif
  260. /*
  261. * The flags register is also special: we cannot trust that the user
  262. * has set it up safely, so this makes sure interrupt etc flags are
  263. * inherited from protected mode.
  264. */
  265. VEFLAGS = info->regs.pt.flags;
  266. info->regs.pt.flags &= SAFE_MASK;
  267. info->regs.pt.flags |= info->regs32->flags & ~SAFE_MASK;
  268. info->regs.pt.flags |= X86_VM_MASK;
  269. switch (info->cpu_type) {
  270. case CPU_286:
  271. tsk->thread.v86mask = 0;
  272. break;
  273. case CPU_386:
  274. tsk->thread.v86mask = X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  275. break;
  276. case CPU_486:
  277. tsk->thread.v86mask = X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  278. break;
  279. default:
  280. tsk->thread.v86mask = X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  281. break;
  282. }
  283. /*
  284. * Save old state, set default return value (%ax) to 0 (VM86_SIGNAL)
  285. */
  286. info->regs32->ax = VM86_SIGNAL;
  287. tsk->thread.saved_sp0 = tsk->thread.sp0;
  288. tsk->thread.saved_fs = info->regs32->fs;
  289. tsk->thread.saved_gs = get_user_gs(info->regs32);
  290. tss = &per_cpu(init_tss, get_cpu());
  291. tsk->thread.sp0 = (unsigned long) &info->VM86_TSS_ESP0;
  292. if (cpu_has_sep)
  293. tsk->thread.sysenter_cs = 0;
  294. load_sp0(tss, &tsk->thread);
  295. put_cpu();
  296. tsk->thread.screen_bitmap = info->screen_bitmap;
  297. if (info->flags & VM86_SCREEN_BITMAP)
  298. mark_screen_rdonly(tsk->mm);
  299. /*call __audit_syscall_exit since we do not exit via the normal paths */
  300. #ifdef CONFIG_AUDITSYSCALL
  301. if (unlikely(current->audit_context))
  302. __audit_syscall_exit(1, 0);
  303. #endif
  304. __asm__ __volatile__(
  305. "movl %0,%%esp\n\t"
  306. "movl %1,%%ebp\n\t"
  307. #ifdef CONFIG_X86_32_LAZY_GS
  308. "mov %2, %%gs\n\t"
  309. #endif
  310. "jmp resume_userspace"
  311. : /* no outputs */
  312. :"r" (&info->regs), "r" (task_thread_info(tsk)), "r" (0));
  313. /* we never return here */
  314. }
  315. static inline void return_to_32bit(struct kernel_vm86_regs *regs16, int retval)
  316. {
  317. struct pt_regs *regs32;
  318. regs32 = save_v86_state(regs16);
  319. regs32->ax = retval;
  320. __asm__ __volatile__("movl %0,%%esp\n\t"
  321. "movl %1,%%ebp\n\t"
  322. "jmp resume_userspace"
  323. : : "r" (regs32), "r" (current_thread_info()));
  324. }
  325. static inline void set_IF(struct kernel_vm86_regs *regs)
  326. {
  327. VEFLAGS |= X86_EFLAGS_VIF;
  328. if (VEFLAGS & X86_EFLAGS_VIP)
  329. return_to_32bit(regs, VM86_STI);
  330. }
  331. static inline void clear_IF(struct kernel_vm86_regs *regs)
  332. {
  333. VEFLAGS &= ~X86_EFLAGS_VIF;
  334. }
  335. static inline void clear_TF(struct kernel_vm86_regs *regs)
  336. {
  337. regs->pt.flags &= ~X86_EFLAGS_TF;
  338. }
  339. static inline void clear_AC(struct kernel_vm86_regs *regs)
  340. {
  341. regs->pt.flags &= ~X86_EFLAGS_AC;
  342. }
  343. /*
  344. * It is correct to call set_IF(regs) from the set_vflags_*
  345. * functions. However someone forgot to call clear_IF(regs)
  346. * in the opposite case.
  347. * After the command sequence CLI PUSHF STI POPF you should
  348. * end up with interrupts disabled, but you ended up with
  349. * interrupts enabled.
  350. * ( I was testing my own changes, but the only bug I
  351. * could find was in a function I had not changed. )
  352. * [KD]
  353. */
  354. static inline void set_vflags_long(unsigned long flags, struct kernel_vm86_regs *regs)
  355. {
  356. set_flags(VEFLAGS, flags, current->thread.v86mask);
  357. set_flags(regs->pt.flags, flags, SAFE_MASK);
  358. if (flags & X86_EFLAGS_IF)
  359. set_IF(regs);
  360. else
  361. clear_IF(regs);
  362. }
  363. static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs *regs)
  364. {
  365. set_flags(VFLAGS, flags, current->thread.v86mask);
  366. set_flags(regs->pt.flags, flags, SAFE_MASK);
  367. if (flags & X86_EFLAGS_IF)
  368. set_IF(regs);
  369. else
  370. clear_IF(regs);
  371. }
  372. static inline unsigned long get_vflags(struct kernel_vm86_regs *regs)
  373. {
  374. unsigned long flags = regs->pt.flags & RETURN_MASK;
  375. if (VEFLAGS & X86_EFLAGS_VIF)
  376. flags |= X86_EFLAGS_IF;
  377. flags |= X86_EFLAGS_IOPL;
  378. return flags | (VEFLAGS & current->thread.v86mask);
  379. }
  380. static inline int is_revectored(int nr, struct revectored_struct *bitmap)
  381. {
  382. __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
  383. :"=r" (nr)
  384. :"m" (*bitmap), "r" (nr));
  385. return nr;
  386. }
  387. #define val_byte(val, n) (((__u8 *)&val)[n])
  388. #define pushb(base, ptr, val, err_label) \
  389. do { \
  390. __u8 __val = val; \
  391. ptr--; \
  392. if (put_user(__val, base + ptr) < 0) \
  393. goto err_label; \
  394. } while (0)
  395. #define pushw(base, ptr, val, err_label) \
  396. do { \
  397. __u16 __val = val; \
  398. ptr--; \
  399. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  400. goto err_label; \
  401. ptr--; \
  402. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  403. goto err_label; \
  404. } while (0)
  405. #define pushl(base, ptr, val, err_label) \
  406. do { \
  407. __u32 __val = val; \
  408. ptr--; \
  409. if (put_user(val_byte(__val, 3), base + ptr) < 0) \
  410. goto err_label; \
  411. ptr--; \
  412. if (put_user(val_byte(__val, 2), base + ptr) < 0) \
  413. goto err_label; \
  414. ptr--; \
  415. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  416. goto err_label; \
  417. ptr--; \
  418. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  419. goto err_label; \
  420. } while (0)
  421. #define popb(base, ptr, err_label) \
  422. ({ \
  423. __u8 __res; \
  424. if (get_user(__res, base + ptr) < 0) \
  425. goto err_label; \
  426. ptr++; \
  427. __res; \
  428. })
  429. #define popw(base, ptr, err_label) \
  430. ({ \
  431. __u16 __res; \
  432. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  433. goto err_label; \
  434. ptr++; \
  435. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  436. goto err_label; \
  437. ptr++; \
  438. __res; \
  439. })
  440. #define popl(base, ptr, err_label) \
  441. ({ \
  442. __u32 __res; \
  443. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  444. goto err_label; \
  445. ptr++; \
  446. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  447. goto err_label; \
  448. ptr++; \
  449. if (get_user(val_byte(__res, 2), base + ptr) < 0) \
  450. goto err_label; \
  451. ptr++; \
  452. if (get_user(val_byte(__res, 3), base + ptr) < 0) \
  453. goto err_label; \
  454. ptr++; \
  455. __res; \
  456. })
  457. /* There are so many possible reasons for this function to return
  458. * VM86_INTx, so adding another doesn't bother me. We can expect
  459. * userspace programs to be able to handle it. (Getting a problem
  460. * in userspace is always better than an Oops anyway.) [KD]
  461. */
  462. static void do_int(struct kernel_vm86_regs *regs, int i,
  463. unsigned char __user *ssp, unsigned short sp)
  464. {
  465. unsigned long __user *intr_ptr;
  466. unsigned long segoffs;
  467. if (regs->pt.cs == BIOSSEG)
  468. goto cannot_handle;
  469. if (is_revectored(i, &KVM86->int_revectored))
  470. goto cannot_handle;
  471. if (i == 0x21 && is_revectored(AH(regs), &KVM86->int21_revectored))
  472. goto cannot_handle;
  473. intr_ptr = (unsigned long __user *) (i << 2);
  474. if (get_user(segoffs, intr_ptr))
  475. goto cannot_handle;
  476. if ((segoffs >> 16) == BIOSSEG)
  477. goto cannot_handle;
  478. pushw(ssp, sp, get_vflags(regs), cannot_handle);
  479. pushw(ssp, sp, regs->pt.cs, cannot_handle);
  480. pushw(ssp, sp, IP(regs), cannot_handle);
  481. regs->pt.cs = segoffs >> 16;
  482. SP(regs) -= 6;
  483. IP(regs) = segoffs & 0xffff;
  484. clear_TF(regs);
  485. clear_IF(regs);
  486. clear_AC(regs);
  487. return;
  488. cannot_handle:
  489. return_to_32bit(regs, VM86_INTx + (i << 8));
  490. }
  491. int handle_vm86_trap(struct kernel_vm86_regs *regs, long error_code, int trapno)
  492. {
  493. if (VMPI.is_vm86pus) {
  494. if ((trapno == 3) || (trapno == 1)) {
  495. KVM86->regs32->ax = VM86_TRAP + (trapno << 8);
  496. /* setting this flag forces the code in entry_32.S to
  497. call save_v86_state() and change the stack pointer
  498. to KVM86->regs32 */
  499. set_thread_flag(TIF_IRET);
  500. return 0;
  501. }
  502. do_int(regs, trapno, (unsigned char __user *) (regs->pt.ss << 4), SP(regs));
  503. return 0;
  504. }
  505. if (trapno != 1)
  506. return 1; /* we let this handle by the calling routine */
  507. current->thread.trap_nr = trapno;
  508. current->thread.error_code = error_code;
  509. force_sig(SIGTRAP, current);
  510. return 0;
  511. }
  512. void handle_vm86_fault(struct kernel_vm86_regs *regs, long error_code)
  513. {
  514. unsigned char opcode;
  515. unsigned char __user *csp;
  516. unsigned char __user *ssp;
  517. unsigned short ip, sp, orig_flags;
  518. int data32, pref_done;
  519. #define CHECK_IF_IN_TRAP \
  520. if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
  521. newflags |= X86_EFLAGS_TF
  522. #define VM86_FAULT_RETURN do { \
  523. if (VMPI.force_return_for_pic && (VEFLAGS & (X86_EFLAGS_IF | X86_EFLAGS_VIF))) \
  524. return_to_32bit(regs, VM86_PICRETURN); \
  525. if (orig_flags & X86_EFLAGS_TF) \
  526. handle_vm86_trap(regs, 0, 1); \
  527. return; } while (0)
  528. orig_flags = *(unsigned short *)&regs->pt.flags;
  529. csp = (unsigned char __user *) (regs->pt.cs << 4);
  530. ssp = (unsigned char __user *) (regs->pt.ss << 4);
  531. sp = SP(regs);
  532. ip = IP(regs);
  533. data32 = 0;
  534. pref_done = 0;
  535. do {
  536. switch (opcode = popb(csp, ip, simulate_sigsegv)) {
  537. case 0x66: /* 32-bit data */ data32 = 1; break;
  538. case 0x67: /* 32-bit address */ break;
  539. case 0x2e: /* CS */ break;
  540. case 0x3e: /* DS */ break;
  541. case 0x26: /* ES */ break;
  542. case 0x36: /* SS */ break;
  543. case 0x65: /* GS */ break;
  544. case 0x64: /* FS */ break;
  545. case 0xf2: /* repnz */ break;
  546. case 0xf3: /* rep */ break;
  547. default: pref_done = 1;
  548. }
  549. } while (!pref_done);
  550. switch (opcode) {
  551. /* pushf */
  552. case 0x9c:
  553. if (data32) {
  554. pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
  555. SP(regs) -= 4;
  556. } else {
  557. pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
  558. SP(regs) -= 2;
  559. }
  560. IP(regs) = ip;
  561. VM86_FAULT_RETURN;
  562. /* popf */
  563. case 0x9d:
  564. {
  565. unsigned long newflags;
  566. if (data32) {
  567. newflags = popl(ssp, sp, simulate_sigsegv);
  568. SP(regs) += 4;
  569. } else {
  570. newflags = popw(ssp, sp, simulate_sigsegv);
  571. SP(regs) += 2;
  572. }
  573. IP(regs) = ip;
  574. CHECK_IF_IN_TRAP;
  575. if (data32)
  576. set_vflags_long(newflags, regs);
  577. else
  578. set_vflags_short(newflags, regs);
  579. VM86_FAULT_RETURN;
  580. }
  581. /* int xx */
  582. case 0xcd: {
  583. int intno = popb(csp, ip, simulate_sigsegv);
  584. IP(regs) = ip;
  585. if (VMPI.vm86dbg_active) {
  586. if ((1 << (intno & 7)) & VMPI.vm86dbg_intxxtab[intno >> 3])
  587. return_to_32bit(regs, VM86_INTx + (intno << 8));
  588. }
  589. do_int(regs, intno, ssp, sp);
  590. return;
  591. }
  592. /* iret */
  593. case 0xcf:
  594. {
  595. unsigned long newip;
  596. unsigned long newcs;
  597. unsigned long newflags;
  598. if (data32) {
  599. newip = popl(ssp, sp, simulate_sigsegv);
  600. newcs = popl(ssp, sp, simulate_sigsegv);
  601. newflags = popl(ssp, sp, simulate_sigsegv);
  602. SP(regs) += 12;
  603. } else {
  604. newip = popw(ssp, sp, simulate_sigsegv);
  605. newcs = popw(ssp, sp, simulate_sigsegv);
  606. newflags = popw(ssp, sp, simulate_sigsegv);
  607. SP(regs) += 6;
  608. }
  609. IP(regs) = newip;
  610. regs->pt.cs = newcs;
  611. CHECK_IF_IN_TRAP;
  612. if (data32) {
  613. set_vflags_long(newflags, regs);
  614. } else {
  615. set_vflags_short(newflags, regs);
  616. }
  617. VM86_FAULT_RETURN;
  618. }
  619. /* cli */
  620. case 0xfa:
  621. IP(regs) = ip;
  622. clear_IF(regs);
  623. VM86_FAULT_RETURN;
  624. /* sti */
  625. /*
  626. * Damn. This is incorrect: the 'sti' instruction should actually
  627. * enable interrupts after the /next/ instruction. Not good.
  628. *
  629. * Probably needs some horsing around with the TF flag. Aiee..
  630. */
  631. case 0xfb:
  632. IP(regs) = ip;
  633. set_IF(regs);
  634. VM86_FAULT_RETURN;
  635. default:
  636. return_to_32bit(regs, VM86_UNKNOWN);
  637. }
  638. return;
  639. simulate_sigsegv:
  640. /* FIXME: After a long discussion with Stas we finally
  641. * agreed, that this is wrong. Here we should
  642. * really send a SIGSEGV to the user program.
  643. * But how do we create the correct context? We
  644. * are inside a general protection fault handler
  645. * and has just returned from a page fault handler.
  646. * The correct context for the signal handler
  647. * should be a mixture of the two, but how do we
  648. * get the information? [KD]
  649. */
  650. return_to_32bit(regs, VM86_UNKNOWN);
  651. }
  652. /* ---------------- vm86 special IRQ passing stuff ----------------- */
  653. #define VM86_IRQNAME "vm86irq"
  654. static struct vm86_irqs {
  655. struct task_struct *tsk;
  656. int sig;
  657. } vm86_irqs[16];
  658. static DEFINE_SPINLOCK(irqbits_lock);
  659. static int irqbits;
  660. #define ALLOWED_SIGS (1 /* 0 = don't send a signal */ \
  661. | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
  662. | (1 << SIGUNUSED))
  663. static irqreturn_t irq_handler(int intno, void *dev_id)
  664. {
  665. int irq_bit;
  666. unsigned long flags;
  667. spin_lock_irqsave(&irqbits_lock, flags);
  668. irq_bit = 1 << intno;
  669. if ((irqbits & irq_bit) || !vm86_irqs[intno].tsk)
  670. goto out;
  671. irqbits |= irq_bit;
  672. if (vm86_irqs[intno].sig)
  673. send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
  674. /*
  675. * IRQ will be re-enabled when user asks for the irq (whether
  676. * polling or as a result of the signal)
  677. */
  678. disable_irq_nosync(intno);
  679. spin_unlock_irqrestore(&irqbits_lock, flags);
  680. return IRQ_HANDLED;
  681. out:
  682. spin_unlock_irqrestore(&irqbits_lock, flags);
  683. return IRQ_NONE;
  684. }
  685. static inline void free_vm86_irq(int irqnumber)
  686. {
  687. unsigned long flags;
  688. free_irq(irqnumber, NULL);
  689. vm86_irqs[irqnumber].tsk = NULL;
  690. spin_lock_irqsave(&irqbits_lock, flags);
  691. irqbits &= ~(1 << irqnumber);
  692. spin_unlock_irqrestore(&irqbits_lock, flags);
  693. }
  694. void release_vm86_irqs(struct task_struct *task)
  695. {
  696. int i;
  697. for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
  698. if (vm86_irqs[i].tsk == task)
  699. free_vm86_irq(i);
  700. }
  701. static inline int get_and_reset_irq(int irqnumber)
  702. {
  703. int bit;
  704. unsigned long flags;
  705. int ret = 0;
  706. if (invalid_vm86_irq(irqnumber)) return 0;
  707. if (vm86_irqs[irqnumber].tsk != current) return 0;
  708. spin_lock_irqsave(&irqbits_lock, flags);
  709. bit = irqbits & (1 << irqnumber);
  710. irqbits &= ~bit;
  711. if (bit) {
  712. enable_irq(irqnumber);
  713. ret = 1;
  714. }
  715. spin_unlock_irqrestore(&irqbits_lock, flags);
  716. return ret;
  717. }
  718. static int do_vm86_irq_handling(int subfunction, int irqnumber)
  719. {
  720. int ret;
  721. switch (subfunction) {
  722. case VM86_GET_AND_RESET_IRQ: {
  723. return get_and_reset_irq(irqnumber);
  724. }
  725. case VM86_GET_IRQ_BITS: {
  726. return irqbits;
  727. }
  728. case VM86_REQUEST_IRQ: {
  729. int sig = irqnumber >> 8;
  730. int irq = irqnumber & 255;
  731. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  732. if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
  733. if (invalid_vm86_irq(irq)) return -EPERM;
  734. if (vm86_irqs[irq].tsk) return -EPERM;
  735. ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
  736. if (ret) return ret;
  737. vm86_irqs[irq].sig = sig;
  738. vm86_irqs[irq].tsk = current;
  739. return irq;
  740. }
  741. case VM86_FREE_IRQ: {
  742. if (invalid_vm86_irq(irqnumber)) return -EPERM;
  743. if (!vm86_irqs[irqnumber].tsk) return 0;
  744. if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
  745. free_vm86_irq(irqnumber);
  746. return 0;
  747. }
  748. }
  749. return -EINVAL;
  750. }