vm86_32.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. return_to_32bit(regs, VM86_TRAP + (trapno << 8));
  491. do_int(regs, trapno, (unsigned char __user *) (regs->pt.ss << 4), SP(regs));
  492. return 0;
  493. }
  494. if (trapno != 1)
  495. return 1; /* we let this handle by the calling routine */
  496. current->thread.trap_no = trapno;
  497. current->thread.error_code = error_code;
  498. force_sig(SIGTRAP, current);
  499. return 0;
  500. }
  501. void handle_vm86_fault(struct kernel_vm86_regs *regs, long error_code)
  502. {
  503. unsigned char opcode;
  504. unsigned char __user *csp;
  505. unsigned char __user *ssp;
  506. unsigned short ip, sp, orig_flags;
  507. int data32, pref_done;
  508. #define CHECK_IF_IN_TRAP \
  509. if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
  510. newflags |= X86_EFLAGS_TF
  511. #define VM86_FAULT_RETURN do { \
  512. if (VMPI.force_return_for_pic && (VEFLAGS & (X86_EFLAGS_IF | X86_EFLAGS_VIF))) \
  513. return_to_32bit(regs, VM86_PICRETURN); \
  514. if (orig_flags & X86_EFLAGS_TF) \
  515. handle_vm86_trap(regs, 0, 1); \
  516. return; } while (0)
  517. orig_flags = *(unsigned short *)&regs->pt.flags;
  518. csp = (unsigned char __user *) (regs->pt.cs << 4);
  519. ssp = (unsigned char __user *) (regs->pt.ss << 4);
  520. sp = SP(regs);
  521. ip = IP(regs);
  522. data32 = 0;
  523. pref_done = 0;
  524. do {
  525. switch (opcode = popb(csp, ip, simulate_sigsegv)) {
  526. case 0x66: /* 32-bit data */ data32 = 1; break;
  527. case 0x67: /* 32-bit address */ break;
  528. case 0x2e: /* CS */ break;
  529. case 0x3e: /* DS */ break;
  530. case 0x26: /* ES */ break;
  531. case 0x36: /* SS */ break;
  532. case 0x65: /* GS */ break;
  533. case 0x64: /* FS */ break;
  534. case 0xf2: /* repnz */ break;
  535. case 0xf3: /* rep */ break;
  536. default: pref_done = 1;
  537. }
  538. } while (!pref_done);
  539. switch (opcode) {
  540. /* pushf */
  541. case 0x9c:
  542. if (data32) {
  543. pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
  544. SP(regs) -= 4;
  545. } else {
  546. pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
  547. SP(regs) -= 2;
  548. }
  549. IP(regs) = ip;
  550. VM86_FAULT_RETURN;
  551. /* popf */
  552. case 0x9d:
  553. {
  554. unsigned long newflags;
  555. if (data32) {
  556. newflags = popl(ssp, sp, simulate_sigsegv);
  557. SP(regs) += 4;
  558. } else {
  559. newflags = popw(ssp, sp, simulate_sigsegv);
  560. SP(regs) += 2;
  561. }
  562. IP(regs) = ip;
  563. CHECK_IF_IN_TRAP;
  564. if (data32)
  565. set_vflags_long(newflags, regs);
  566. else
  567. set_vflags_short(newflags, regs);
  568. VM86_FAULT_RETURN;
  569. }
  570. /* int xx */
  571. case 0xcd: {
  572. int intno = popb(csp, ip, simulate_sigsegv);
  573. IP(regs) = ip;
  574. if (VMPI.vm86dbg_active) {
  575. if ((1 << (intno & 7)) & VMPI.vm86dbg_intxxtab[intno >> 3])
  576. return_to_32bit(regs, VM86_INTx + (intno << 8));
  577. }
  578. do_int(regs, intno, ssp, sp);
  579. return;
  580. }
  581. /* iret */
  582. case 0xcf:
  583. {
  584. unsigned long newip;
  585. unsigned long newcs;
  586. unsigned long newflags;
  587. if (data32) {
  588. newip = popl(ssp, sp, simulate_sigsegv);
  589. newcs = popl(ssp, sp, simulate_sigsegv);
  590. newflags = popl(ssp, sp, simulate_sigsegv);
  591. SP(regs) += 12;
  592. } else {
  593. newip = popw(ssp, sp, simulate_sigsegv);
  594. newcs = popw(ssp, sp, simulate_sigsegv);
  595. newflags = popw(ssp, sp, simulate_sigsegv);
  596. SP(regs) += 6;
  597. }
  598. IP(regs) = newip;
  599. regs->pt.cs = newcs;
  600. CHECK_IF_IN_TRAP;
  601. if (data32) {
  602. set_vflags_long(newflags, regs);
  603. } else {
  604. set_vflags_short(newflags, regs);
  605. }
  606. VM86_FAULT_RETURN;
  607. }
  608. /* cli */
  609. case 0xfa:
  610. IP(regs) = ip;
  611. clear_IF(regs);
  612. VM86_FAULT_RETURN;
  613. /* sti */
  614. /*
  615. * Damn. This is incorrect: the 'sti' instruction should actually
  616. * enable interrupts after the /next/ instruction. Not good.
  617. *
  618. * Probably needs some horsing around with the TF flag. Aiee..
  619. */
  620. case 0xfb:
  621. IP(regs) = ip;
  622. set_IF(regs);
  623. VM86_FAULT_RETURN;
  624. default:
  625. return_to_32bit(regs, VM86_UNKNOWN);
  626. }
  627. return;
  628. simulate_sigsegv:
  629. /* FIXME: After a long discussion with Stas we finally
  630. * agreed, that this is wrong. Here we should
  631. * really send a SIGSEGV to the user program.
  632. * But how do we create the correct context? We
  633. * are inside a general protection fault handler
  634. * and has just returned from a page fault handler.
  635. * The correct context for the signal handler
  636. * should be a mixture of the two, but how do we
  637. * get the information? [KD]
  638. */
  639. return_to_32bit(regs, VM86_UNKNOWN);
  640. }
  641. /* ---------------- vm86 special IRQ passing stuff ----------------- */
  642. #define VM86_IRQNAME "vm86irq"
  643. static struct vm86_irqs {
  644. struct task_struct *tsk;
  645. int sig;
  646. } vm86_irqs[16];
  647. static DEFINE_SPINLOCK(irqbits_lock);
  648. static int irqbits;
  649. #define ALLOWED_SIGS (1 /* 0 = don't send a signal */ \
  650. | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
  651. | (1 << SIGUNUSED))
  652. static irqreturn_t irq_handler(int intno, void *dev_id)
  653. {
  654. int irq_bit;
  655. unsigned long flags;
  656. spin_lock_irqsave(&irqbits_lock, flags);
  657. irq_bit = 1 << intno;
  658. if ((irqbits & irq_bit) || !vm86_irqs[intno].tsk)
  659. goto out;
  660. irqbits |= irq_bit;
  661. if (vm86_irqs[intno].sig)
  662. send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
  663. /*
  664. * IRQ will be re-enabled when user asks for the irq (whether
  665. * polling or as a result of the signal)
  666. */
  667. disable_irq_nosync(intno);
  668. spin_unlock_irqrestore(&irqbits_lock, flags);
  669. return IRQ_HANDLED;
  670. out:
  671. spin_unlock_irqrestore(&irqbits_lock, flags);
  672. return IRQ_NONE;
  673. }
  674. static inline void free_vm86_irq(int irqnumber)
  675. {
  676. unsigned long flags;
  677. free_irq(irqnumber, NULL);
  678. vm86_irqs[irqnumber].tsk = NULL;
  679. spin_lock_irqsave(&irqbits_lock, flags);
  680. irqbits &= ~(1 << irqnumber);
  681. spin_unlock_irqrestore(&irqbits_lock, flags);
  682. }
  683. void release_vm86_irqs(struct task_struct *task)
  684. {
  685. int i;
  686. for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
  687. if (vm86_irqs[i].tsk == task)
  688. free_vm86_irq(i);
  689. }
  690. static inline int get_and_reset_irq(int irqnumber)
  691. {
  692. int bit;
  693. unsigned long flags;
  694. int ret = 0;
  695. if (invalid_vm86_irq(irqnumber)) return 0;
  696. if (vm86_irqs[irqnumber].tsk != current) return 0;
  697. spin_lock_irqsave(&irqbits_lock, flags);
  698. bit = irqbits & (1 << irqnumber);
  699. irqbits &= ~bit;
  700. if (bit) {
  701. enable_irq(irqnumber);
  702. ret = 1;
  703. }
  704. spin_unlock_irqrestore(&irqbits_lock, flags);
  705. return ret;
  706. }
  707. static int do_vm86_irq_handling(int subfunction, int irqnumber)
  708. {
  709. int ret;
  710. switch (subfunction) {
  711. case VM86_GET_AND_RESET_IRQ: {
  712. return get_and_reset_irq(irqnumber);
  713. }
  714. case VM86_GET_IRQ_BITS: {
  715. return irqbits;
  716. }
  717. case VM86_REQUEST_IRQ: {
  718. int sig = irqnumber >> 8;
  719. int irq = irqnumber & 255;
  720. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  721. if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
  722. if (invalid_vm86_irq(irq)) return -EPERM;
  723. if (vm86_irqs[irq].tsk) return -EPERM;
  724. ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
  725. if (ret) return ret;
  726. vm86_irqs[irq].sig = sig;
  727. vm86_irqs[irq].tsk = current;
  728. return irq;
  729. }
  730. case VM86_FREE_IRQ: {
  731. if (invalid_vm86_irq(irqnumber)) return -EPERM;
  732. if (!vm86_irqs[irqnumber].tsk) return 0;
  733. if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
  734. free_vm86_irq(irqnumber);
  735. return 0;
  736. }
  737. }
  738. return -EINVAL;
  739. }