vm86_32.c 22 KB

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