vm86.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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/config.h>
  34. #include <linux/errno.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/sched.h>
  37. #include <linux/kernel.h>
  38. #include <linux/signal.h>
  39. #include <linux/string.h>
  40. #include <linux/mm.h>
  41. #include <linux/smp.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/highmem.h>
  44. #include <linux/ptrace.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. /*
  227. * make sure the vm86() system call doesn't try to do anything silly
  228. */
  229. info->regs.__null_ds = 0;
  230. info->regs.__null_es = 0;
  231. /* we are clearing fs,gs later just before "jmp resume_userspace",
  232. * because starting with Linux 2.1.x they aren't no longer saved/restored
  233. */
  234. /*
  235. * The eflags register is also special: we cannot trust that the user
  236. * has set it up safely, so this makes sure interrupt etc flags are
  237. * inherited from protected mode.
  238. */
  239. VEFLAGS = info->regs.eflags;
  240. info->regs.eflags &= SAFE_MASK;
  241. info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
  242. info->regs.eflags |= VM_MASK;
  243. switch (info->cpu_type) {
  244. case CPU_286:
  245. tsk->thread.v86mask = 0;
  246. break;
  247. case CPU_386:
  248. tsk->thread.v86mask = NT_MASK | IOPL_MASK;
  249. break;
  250. case CPU_486:
  251. tsk->thread.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
  252. break;
  253. default:
  254. tsk->thread.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
  255. break;
  256. }
  257. /*
  258. * Save old state, set default return value (%eax) to 0
  259. */
  260. info->regs32->eax = 0;
  261. tsk->thread.saved_esp0 = tsk->thread.esp0;
  262. savesegment(fs, tsk->thread.saved_fs);
  263. savesegment(gs, tsk->thread.saved_gs);
  264. tss = &per_cpu(init_tss, get_cpu());
  265. tsk->thread.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
  266. if (cpu_has_sep)
  267. tsk->thread.sysenter_cs = 0;
  268. load_esp0(tss, &tsk->thread);
  269. put_cpu();
  270. tsk->thread.screen_bitmap = info->screen_bitmap;
  271. if (info->flags & VM86_SCREEN_BITMAP)
  272. mark_screen_rdonly(tsk->mm);
  273. __asm__ __volatile__(
  274. "xorl %%eax,%%eax; movl %%eax,%%fs; movl %%eax,%%gs\n\t"
  275. "movl %0,%%esp\n\t"
  276. "movl %1,%%ebp\n\t"
  277. "jmp resume_userspace"
  278. : /* no outputs */
  279. :"r" (&info->regs), "r" (task_thread_info(tsk)) : "ax");
  280. /* we never return here */
  281. }
  282. static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval)
  283. {
  284. struct pt_regs * regs32;
  285. regs32 = save_v86_state(regs16);
  286. regs32->eax = retval;
  287. __asm__ __volatile__("movl %0,%%esp\n\t"
  288. "movl %1,%%ebp\n\t"
  289. "jmp resume_userspace"
  290. : : "r" (regs32), "r" (current_thread_info()));
  291. }
  292. static inline void set_IF(struct kernel_vm86_regs * regs)
  293. {
  294. VEFLAGS |= VIF_MASK;
  295. if (VEFLAGS & VIP_MASK)
  296. return_to_32bit(regs, VM86_STI);
  297. }
  298. static inline void clear_IF(struct kernel_vm86_regs * regs)
  299. {
  300. VEFLAGS &= ~VIF_MASK;
  301. }
  302. static inline void clear_TF(struct kernel_vm86_regs * regs)
  303. {
  304. regs->eflags &= ~TF_MASK;
  305. }
  306. static inline void clear_AC(struct kernel_vm86_regs * regs)
  307. {
  308. regs->eflags &= ~AC_MASK;
  309. }
  310. /* It is correct to call set_IF(regs) from the set_vflags_*
  311. * functions. However someone forgot to call clear_IF(regs)
  312. * in the opposite case.
  313. * After the command sequence CLI PUSHF STI POPF you should
  314. * end up with interrups disabled, but you ended up with
  315. * interrupts enabled.
  316. * ( I was testing my own changes, but the only bug I
  317. * could find was in a function I had not changed. )
  318. * [KD]
  319. */
  320. static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs)
  321. {
  322. set_flags(VEFLAGS, eflags, current->thread.v86mask);
  323. set_flags(regs->eflags, eflags, SAFE_MASK);
  324. if (eflags & IF_MASK)
  325. set_IF(regs);
  326. else
  327. clear_IF(regs);
  328. }
  329. static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs)
  330. {
  331. set_flags(VFLAGS, flags, current->thread.v86mask);
  332. set_flags(regs->eflags, flags, SAFE_MASK);
  333. if (flags & IF_MASK)
  334. set_IF(regs);
  335. else
  336. clear_IF(regs);
  337. }
  338. static inline unsigned long get_vflags(struct kernel_vm86_regs * regs)
  339. {
  340. unsigned long flags = regs->eflags & RETURN_MASK;
  341. if (VEFLAGS & VIF_MASK)
  342. flags |= IF_MASK;
  343. flags |= IOPL_MASK;
  344. return flags | (VEFLAGS & current->thread.v86mask);
  345. }
  346. static inline int is_revectored(int nr, struct revectored_struct * bitmap)
  347. {
  348. __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
  349. :"=r" (nr)
  350. :"m" (*bitmap),"r" (nr));
  351. return nr;
  352. }
  353. #define val_byte(val, n) (((__u8 *)&val)[n])
  354. #define pushb(base, ptr, val, err_label) \
  355. do { \
  356. __u8 __val = val; \
  357. ptr--; \
  358. if (put_user(__val, base + ptr) < 0) \
  359. goto err_label; \
  360. } while(0)
  361. #define pushw(base, ptr, val, err_label) \
  362. do { \
  363. __u16 __val = val; \
  364. ptr--; \
  365. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  366. goto err_label; \
  367. ptr--; \
  368. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  369. goto err_label; \
  370. } while(0)
  371. #define pushl(base, ptr, val, err_label) \
  372. do { \
  373. __u32 __val = val; \
  374. ptr--; \
  375. if (put_user(val_byte(__val, 3), base + ptr) < 0) \
  376. goto err_label; \
  377. ptr--; \
  378. if (put_user(val_byte(__val, 2), base + ptr) < 0) \
  379. goto err_label; \
  380. ptr--; \
  381. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  382. goto err_label; \
  383. ptr--; \
  384. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  385. goto err_label; \
  386. } while(0)
  387. #define popb(base, ptr, err_label) \
  388. ({ \
  389. __u8 __res; \
  390. if (get_user(__res, base + ptr) < 0) \
  391. goto err_label; \
  392. ptr++; \
  393. __res; \
  394. })
  395. #define popw(base, ptr, err_label) \
  396. ({ \
  397. __u16 __res; \
  398. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  399. goto err_label; \
  400. ptr++; \
  401. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  402. goto err_label; \
  403. ptr++; \
  404. __res; \
  405. })
  406. #define popl(base, ptr, err_label) \
  407. ({ \
  408. __u32 __res; \
  409. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  410. goto err_label; \
  411. ptr++; \
  412. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  413. goto err_label; \
  414. ptr++; \
  415. if (get_user(val_byte(__res, 2), base + ptr) < 0) \
  416. goto err_label; \
  417. ptr++; \
  418. if (get_user(val_byte(__res, 3), base + ptr) < 0) \
  419. goto err_label; \
  420. ptr++; \
  421. __res; \
  422. })
  423. /* There are so many possible reasons for this function to return
  424. * VM86_INTx, so adding another doesn't bother me. We can expect
  425. * userspace programs to be able to handle it. (Getting a problem
  426. * in userspace is always better than an Oops anyway.) [KD]
  427. */
  428. static void do_int(struct kernel_vm86_regs *regs, int i,
  429. unsigned char __user * ssp, unsigned short sp)
  430. {
  431. unsigned long __user *intr_ptr;
  432. unsigned long segoffs;
  433. if (regs->cs == BIOSSEG)
  434. goto cannot_handle;
  435. if (is_revectored(i, &KVM86->int_revectored))
  436. goto cannot_handle;
  437. if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
  438. goto cannot_handle;
  439. intr_ptr = (unsigned long __user *) (i << 2);
  440. if (get_user(segoffs, intr_ptr))
  441. goto cannot_handle;
  442. if ((segoffs >> 16) == BIOSSEG)
  443. goto cannot_handle;
  444. pushw(ssp, sp, get_vflags(regs), cannot_handle);
  445. pushw(ssp, sp, regs->cs, cannot_handle);
  446. pushw(ssp, sp, IP(regs), cannot_handle);
  447. regs->cs = segoffs >> 16;
  448. SP(regs) -= 6;
  449. IP(regs) = segoffs & 0xffff;
  450. clear_TF(regs);
  451. clear_IF(regs);
  452. clear_AC(regs);
  453. return;
  454. cannot_handle:
  455. return_to_32bit(regs, VM86_INTx + (i << 8));
  456. }
  457. int handle_vm86_trap(struct kernel_vm86_regs * regs, long error_code, int trapno)
  458. {
  459. if (VMPI.is_vm86pus) {
  460. if ( (trapno==3) || (trapno==1) )
  461. return_to_32bit(regs, VM86_TRAP + (trapno << 8));
  462. do_int(regs, trapno, (unsigned char __user *) (regs->ss << 4), SP(regs));
  463. return 0;
  464. }
  465. if (trapno !=1)
  466. return 1; /* we let this handle by the calling routine */
  467. if (current->ptrace & PT_PTRACED) {
  468. unsigned long flags;
  469. spin_lock_irqsave(&current->sighand->siglock, flags);
  470. sigdelset(&current->blocked, SIGTRAP);
  471. recalc_sigpending();
  472. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  473. }
  474. send_sig(SIGTRAP, current, 1);
  475. current->thread.trap_no = trapno;
  476. current->thread.error_code = error_code;
  477. return 0;
  478. }
  479. void handle_vm86_fault(struct kernel_vm86_regs * regs, long error_code)
  480. {
  481. unsigned char opcode;
  482. unsigned char __user *csp;
  483. unsigned char __user *ssp;
  484. unsigned short ip, sp, orig_flags;
  485. int data32, pref_done;
  486. #define CHECK_IF_IN_TRAP \
  487. if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
  488. newflags |= TF_MASK
  489. #define VM86_FAULT_RETURN do { \
  490. if (VMPI.force_return_for_pic && (VEFLAGS & (IF_MASK | VIF_MASK))) \
  491. return_to_32bit(regs, VM86_PICRETURN); \
  492. if (orig_flags & TF_MASK) \
  493. handle_vm86_trap(regs, 0, 1); \
  494. return; } while (0)
  495. orig_flags = *(unsigned short *)&regs->eflags;
  496. csp = (unsigned char __user *) (regs->cs << 4);
  497. ssp = (unsigned char __user *) (regs->ss << 4);
  498. sp = SP(regs);
  499. ip = IP(regs);
  500. data32 = 0;
  501. pref_done = 0;
  502. do {
  503. switch (opcode = popb(csp, ip, simulate_sigsegv)) {
  504. case 0x66: /* 32-bit data */ data32=1; break;
  505. case 0x67: /* 32-bit address */ break;
  506. case 0x2e: /* CS */ break;
  507. case 0x3e: /* DS */ break;
  508. case 0x26: /* ES */ break;
  509. case 0x36: /* SS */ break;
  510. case 0x65: /* GS */ break;
  511. case 0x64: /* FS */ break;
  512. case 0xf2: /* repnz */ break;
  513. case 0xf3: /* rep */ break;
  514. default: pref_done = 1;
  515. }
  516. } while (!pref_done);
  517. switch (opcode) {
  518. /* pushf */
  519. case 0x9c:
  520. if (data32) {
  521. pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
  522. SP(regs) -= 4;
  523. } else {
  524. pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
  525. SP(regs) -= 2;
  526. }
  527. IP(regs) = ip;
  528. VM86_FAULT_RETURN;
  529. /* popf */
  530. case 0x9d:
  531. {
  532. unsigned long newflags;
  533. if (data32) {
  534. newflags=popl(ssp, sp, simulate_sigsegv);
  535. SP(regs) += 4;
  536. } else {
  537. newflags = popw(ssp, sp, simulate_sigsegv);
  538. SP(regs) += 2;
  539. }
  540. IP(regs) = ip;
  541. CHECK_IF_IN_TRAP;
  542. if (data32) {
  543. set_vflags_long(newflags, regs);
  544. } else {
  545. set_vflags_short(newflags, regs);
  546. }
  547. VM86_FAULT_RETURN;
  548. }
  549. /* int xx */
  550. case 0xcd: {
  551. int intno=popb(csp, ip, simulate_sigsegv);
  552. IP(regs) = ip;
  553. if (VMPI.vm86dbg_active) {
  554. if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
  555. return_to_32bit(regs, VM86_INTx + (intno << 8));
  556. }
  557. do_int(regs, intno, ssp, sp);
  558. return;
  559. }
  560. /* iret */
  561. case 0xcf:
  562. {
  563. unsigned long newip;
  564. unsigned long newcs;
  565. unsigned long newflags;
  566. if (data32) {
  567. newip=popl(ssp, sp, simulate_sigsegv);
  568. newcs=popl(ssp, sp, simulate_sigsegv);
  569. newflags=popl(ssp, sp, simulate_sigsegv);
  570. SP(regs) += 12;
  571. } else {
  572. newip = popw(ssp, sp, simulate_sigsegv);
  573. newcs = popw(ssp, sp, simulate_sigsegv);
  574. newflags = popw(ssp, sp, simulate_sigsegv);
  575. SP(regs) += 6;
  576. }
  577. IP(regs) = newip;
  578. regs->cs = newcs;
  579. CHECK_IF_IN_TRAP;
  580. if (data32) {
  581. set_vflags_long(newflags, regs);
  582. } else {
  583. set_vflags_short(newflags, regs);
  584. }
  585. VM86_FAULT_RETURN;
  586. }
  587. /* cli */
  588. case 0xfa:
  589. IP(regs) = ip;
  590. clear_IF(regs);
  591. VM86_FAULT_RETURN;
  592. /* sti */
  593. /*
  594. * Damn. This is incorrect: the 'sti' instruction should actually
  595. * enable interrupts after the /next/ instruction. Not good.
  596. *
  597. * Probably needs some horsing around with the TF flag. Aiee..
  598. */
  599. case 0xfb:
  600. IP(regs) = ip;
  601. set_IF(regs);
  602. VM86_FAULT_RETURN;
  603. default:
  604. return_to_32bit(regs, VM86_UNKNOWN);
  605. }
  606. return;
  607. simulate_sigsegv:
  608. /* FIXME: After a long discussion with Stas we finally
  609. * agreed, that this is wrong. Here we should
  610. * really send a SIGSEGV to the user program.
  611. * But how do we create the correct context? We
  612. * are inside a general protection fault handler
  613. * and has just returned from a page fault handler.
  614. * The correct context for the signal handler
  615. * should be a mixture of the two, but how do we
  616. * get the information? [KD]
  617. */
  618. return_to_32bit(regs, VM86_UNKNOWN);
  619. }
  620. /* ---------------- vm86 special IRQ passing stuff ----------------- */
  621. #define VM86_IRQNAME "vm86irq"
  622. static struct vm86_irqs {
  623. struct task_struct *tsk;
  624. int sig;
  625. } vm86_irqs[16];
  626. static DEFINE_SPINLOCK(irqbits_lock);
  627. static int irqbits;
  628. #define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
  629. | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
  630. | (1 << SIGUNUSED) )
  631. static irqreturn_t irq_handler(int intno, void *dev_id, struct pt_regs * regs)
  632. {
  633. int irq_bit;
  634. unsigned long flags;
  635. spin_lock_irqsave(&irqbits_lock, flags);
  636. irq_bit = 1 << intno;
  637. if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk)
  638. goto out;
  639. irqbits |= irq_bit;
  640. if (vm86_irqs[intno].sig)
  641. send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
  642. /*
  643. * IRQ will be re-enabled when user asks for the irq (whether
  644. * polling or as a result of the signal)
  645. */
  646. disable_irq_nosync(intno);
  647. spin_unlock_irqrestore(&irqbits_lock, flags);
  648. return IRQ_HANDLED;
  649. out:
  650. spin_unlock_irqrestore(&irqbits_lock, flags);
  651. return IRQ_NONE;
  652. }
  653. static inline void free_vm86_irq(int irqnumber)
  654. {
  655. unsigned long flags;
  656. free_irq(irqnumber, NULL);
  657. vm86_irqs[irqnumber].tsk = NULL;
  658. spin_lock_irqsave(&irqbits_lock, flags);
  659. irqbits &= ~(1 << irqnumber);
  660. spin_unlock_irqrestore(&irqbits_lock, flags);
  661. }
  662. void release_vm86_irqs(struct task_struct *task)
  663. {
  664. int i;
  665. for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
  666. if (vm86_irqs[i].tsk == task)
  667. free_vm86_irq(i);
  668. }
  669. static inline int get_and_reset_irq(int irqnumber)
  670. {
  671. int bit;
  672. unsigned long flags;
  673. int ret = 0;
  674. if (invalid_vm86_irq(irqnumber)) return 0;
  675. if (vm86_irqs[irqnumber].tsk != current) return 0;
  676. spin_lock_irqsave(&irqbits_lock, flags);
  677. bit = irqbits & (1 << irqnumber);
  678. irqbits &= ~bit;
  679. if (bit) {
  680. enable_irq(irqnumber);
  681. ret = 1;
  682. }
  683. spin_unlock_irqrestore(&irqbits_lock, flags);
  684. return ret;
  685. }
  686. static int do_vm86_irq_handling(int subfunction, int irqnumber)
  687. {
  688. int ret;
  689. switch (subfunction) {
  690. case VM86_GET_AND_RESET_IRQ: {
  691. return get_and_reset_irq(irqnumber);
  692. }
  693. case VM86_GET_IRQ_BITS: {
  694. return irqbits;
  695. }
  696. case VM86_REQUEST_IRQ: {
  697. int sig = irqnumber >> 8;
  698. int irq = irqnumber & 255;
  699. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  700. if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
  701. if (invalid_vm86_irq(irq)) return -EPERM;
  702. if (vm86_irqs[irq].tsk) return -EPERM;
  703. ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
  704. if (ret) return ret;
  705. vm86_irqs[irq].sig = sig;
  706. vm86_irqs[irq].tsk = current;
  707. return irq;
  708. }
  709. case VM86_FREE_IRQ: {
  710. if (invalid_vm86_irq(irqnumber)) return -EPERM;
  711. if (!vm86_irqs[irqnumber].tsk) return 0;
  712. if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
  713. free_vm86_irq(irqnumber);
  714. return 0;
  715. }
  716. }
  717. return -EINVAL;
  718. }