vm86.c 21 KB

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