vm86_32.c 22 KB

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