vm86_32.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. *
  4. * 29 dec 2001 - Fixed oopses caused by unchecked access to the vm86
  5. * stack - Manfred Spraul <manfred@colorfullife.com>
  6. *
  7. * 22 mar 2002 - Manfred detected the stackfaults, but didn't handle
  8. * them correctly. Now the emulation will be in a
  9. * consistent state after stackfaults - Kasper Dupont
  10. * <kasperd@daimi.au.dk>
  11. *
  12. * 22 mar 2002 - Added missing clear_IF in set_vflags_* Kasper Dupont
  13. * <kasperd@daimi.au.dk>
  14. *
  15. * ?? ??? 2002 - Fixed premature returns from handle_vm86_fault
  16. * caused by Kasper Dupont's changes - Stas Sergeev
  17. *
  18. * 4 apr 2002 - Fixed CHECK_IF_IN_TRAP broken by Stas' changes.
  19. * Kasper Dupont <kasperd@daimi.au.dk>
  20. *
  21. * 9 apr 2002 - Changed syntax of macros in handle_vm86_fault.
  22. * Kasper Dupont <kasperd@daimi.au.dk>
  23. *
  24. * 9 apr 2002 - Changed stack access macros to jump to a label
  25. * instead of returning to userspace. This simplifies
  26. * do_int, and is needed by handle_vm6_fault. Kasper
  27. * Dupont <kasperd@daimi.au.dk>
  28. *
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/capability.h>
  32. #include <linux/errno.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/syscalls.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/highmem.h>
  42. #include <linux/ptrace.h>
  43. #include <linux/audit.h>
  44. #include <linux/stddef.h>
  45. #include <linux/slab.h>
  46. #include <linux/security.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/io.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/irq.h>
  51. #include <asm/traps.h>
  52. #include <asm/vm86.h>
  53. /*
  54. * Known problems:
  55. *
  56. * Interrupt handling is not guaranteed:
  57. * - a real x86 will disable all interrupts for one instruction
  58. * after a "mov ss,xx" to make stack handling atomic even without
  59. * the 'lss' instruction. We can't guarantee this in v86 mode,
  60. * as the next instruction might result in a page fault or similar.
  61. * - a real x86 will have interrupts disabled for one instruction
  62. * past the 'sti' that enables them. We don't bother with all the
  63. * details yet.
  64. *
  65. * Let's hope these problems do not actually matter for anything.
  66. */
  67. /*
  68. * 8- and 16-bit register defines..
  69. */
  70. #define AL(regs) (((unsigned char *)&((regs)->pt.ax))[0])
  71. #define AH(regs) (((unsigned char *)&((regs)->pt.ax))[1])
  72. #define IP(regs) (*(unsigned short *)&((regs)->pt.ip))
  73. #define SP(regs) (*(unsigned short *)&((regs)->pt.sp))
  74. /*
  75. * virtual flags (16 and 32-bit versions)
  76. */
  77. #define VFLAGS (*(unsigned short *)&(current->thread.vm86->veflags))
  78. #define VEFLAGS (current->thread.vm86->veflags)
  79. #define set_flags(X, new, mask) \
  80. ((X) = ((X) & ~(mask)) | ((new) & (mask)))
  81. #define SAFE_MASK (0xDD5)
  82. #define RETURN_MASK (0xDFF)
  83. void save_v86_state(struct kernel_vm86_regs *regs, int retval)
  84. {
  85. struct tss_struct *tss;
  86. struct task_struct *tsk = current;
  87. struct vm86plus_struct __user *user;
  88. struct vm86 *vm86 = current->thread.vm86;
  89. long err = 0;
  90. /*
  91. * This gets called from entry.S with interrupts disabled, but
  92. * from process context. Enable interrupts here, before trying
  93. * to access user space.
  94. */
  95. local_irq_enable();
  96. if (!vm86 || !vm86->user_vm86) {
  97. pr_alert("no user_vm86: BAD\n");
  98. do_exit(SIGSEGV);
  99. }
  100. set_flags(regs->pt.flags, VEFLAGS, X86_EFLAGS_VIF | vm86->veflags_mask);
  101. user = vm86->user_vm86;
  102. if (!access_ok(VERIFY_WRITE, user, vm86->vm86plus.is_vm86pus ?
  103. sizeof(struct vm86plus_struct) :
  104. sizeof(struct vm86_struct))) {
  105. pr_alert("could not access userspace vm86 info\n");
  106. do_exit(SIGSEGV);
  107. }
  108. put_user_try {
  109. put_user_ex(regs->pt.bx, &user->regs.ebx);
  110. put_user_ex(regs->pt.cx, &user->regs.ecx);
  111. put_user_ex(regs->pt.dx, &user->regs.edx);
  112. put_user_ex(regs->pt.si, &user->regs.esi);
  113. put_user_ex(regs->pt.di, &user->regs.edi);
  114. put_user_ex(regs->pt.bp, &user->regs.ebp);
  115. put_user_ex(regs->pt.ax, &user->regs.eax);
  116. put_user_ex(regs->pt.ip, &user->regs.eip);
  117. put_user_ex(regs->pt.cs, &user->regs.cs);
  118. put_user_ex(regs->pt.flags, &user->regs.eflags);
  119. put_user_ex(regs->pt.sp, &user->regs.esp);
  120. put_user_ex(regs->pt.ss, &user->regs.ss);
  121. put_user_ex(regs->es, &user->regs.es);
  122. put_user_ex(regs->ds, &user->regs.ds);
  123. put_user_ex(regs->fs, &user->regs.fs);
  124. put_user_ex(regs->gs, &user->regs.gs);
  125. put_user_ex(vm86->screen_bitmap, &user->screen_bitmap);
  126. } put_user_catch(err);
  127. if (err) {
  128. pr_alert("could not access userspace vm86 info\n");
  129. do_exit(SIGSEGV);
  130. }
  131. tss = &per_cpu(cpu_tss, get_cpu());
  132. tsk->thread.sp0 = vm86->saved_sp0;
  133. tsk->thread.sysenter_cs = __KERNEL_CS;
  134. load_sp0(tss, &tsk->thread);
  135. vm86->saved_sp0 = 0;
  136. put_cpu();
  137. memcpy(&regs->pt, &vm86->regs32, sizeof(struct pt_regs));
  138. lazy_load_gs(vm86->regs32.gs);
  139. regs->pt.ax = retval;
  140. }
  141. static void mark_screen_rdonly(struct mm_struct *mm)
  142. {
  143. struct vm_area_struct *vma;
  144. spinlock_t *ptl;
  145. pgd_t *pgd;
  146. pud_t *pud;
  147. pmd_t *pmd;
  148. pte_t *pte;
  149. int i;
  150. down_write(&mm->mmap_sem);
  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_trans_huge(*pmd)) {
  159. vma = find_vma(mm, 0xA0000);
  160. split_huge_pmd(vma, pmd, 0xA0000);
  161. }
  162. if (pmd_none_or_clear_bad(pmd))
  163. goto out;
  164. pte = pte_offset_map_lock(mm, pmd, 0xA0000, &ptl);
  165. for (i = 0; i < 32; i++) {
  166. if (pte_present(*pte))
  167. set_pte(pte, pte_wrprotect(*pte));
  168. pte++;
  169. }
  170. pte_unmap_unlock(pte, ptl);
  171. out:
  172. up_write(&mm->mmap_sem);
  173. flush_tlb_mm_range(mm, 0xA0000, 0xA0000 + 32*PAGE_SIZE, 0UL);
  174. }
  175. static int do_vm86_irq_handling(int subfunction, int irqnumber);
  176. static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus);
  177. SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, user_vm86)
  178. {
  179. return do_sys_vm86((struct vm86plus_struct __user *) user_vm86, false);
  180. }
  181. SYSCALL_DEFINE2(vm86, unsigned long, cmd, unsigned long, arg)
  182. {
  183. switch (cmd) {
  184. case VM86_REQUEST_IRQ:
  185. case VM86_FREE_IRQ:
  186. case VM86_GET_IRQ_BITS:
  187. case VM86_GET_AND_RESET_IRQ:
  188. return do_vm86_irq_handling(cmd, (int)arg);
  189. case VM86_PLUS_INSTALL_CHECK:
  190. /*
  191. * NOTE: on old vm86 stuff this will return the error
  192. * from access_ok(), because the subfunction is
  193. * interpreted as (invalid) address to vm86_struct.
  194. * So the installation check works.
  195. */
  196. return 0;
  197. }
  198. /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
  199. return do_sys_vm86((struct vm86plus_struct __user *) arg, true);
  200. }
  201. static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
  202. {
  203. struct tss_struct *tss;
  204. struct task_struct *tsk = current;
  205. struct vm86 *vm86 = tsk->thread.vm86;
  206. struct kernel_vm86_regs vm86regs;
  207. struct pt_regs *regs = current_pt_regs();
  208. unsigned long err = 0;
  209. err = security_mmap_addr(0);
  210. if (err) {
  211. /*
  212. * vm86 cannot virtualize the address space, so vm86 users
  213. * need to manage the low 1MB themselves using mmap. Given
  214. * that BIOS places important data in the first page, vm86
  215. * is essentially useless if mmap_min_addr != 0. DOSEMU,
  216. * for example, won't even bother trying to use vm86 if it
  217. * can't map a page at virtual address 0.
  218. *
  219. * To reduce the available kernel attack surface, simply
  220. * disallow vm86(old) for users who cannot mmap at va 0.
  221. *
  222. * The implementation of security_mmap_addr will allow
  223. * suitably privileged users to map va 0 even if
  224. * vm.mmap_min_addr is set above 0, and we want this
  225. * behavior for vm86 as well, as it ensures that legacy
  226. * tools like vbetool will not fail just because of
  227. * vm.mmap_min_addr.
  228. */
  229. pr_info_once("Denied a call to vm86(old) from %s[%d] (uid: %d). Set the vm.mmap_min_addr sysctl to 0 and/or adjust LSM mmap_min_addr policy to enable vm86 if you are using a vm86-based DOS emulator.\n",
  230. current->comm, task_pid_nr(current),
  231. from_kuid_munged(&init_user_ns, current_uid()));
  232. return -EPERM;
  233. }
  234. if (!vm86) {
  235. if (!(vm86 = kzalloc(sizeof(*vm86), GFP_KERNEL)))
  236. return -ENOMEM;
  237. tsk->thread.vm86 = vm86;
  238. }
  239. if (vm86->saved_sp0)
  240. return -EPERM;
  241. if (!access_ok(VERIFY_READ, user_vm86, plus ?
  242. sizeof(struct vm86_struct) :
  243. sizeof(struct vm86plus_struct)))
  244. return -EFAULT;
  245. memset(&vm86regs, 0, sizeof(vm86regs));
  246. get_user_try {
  247. unsigned short seg;
  248. get_user_ex(vm86regs.pt.bx, &user_vm86->regs.ebx);
  249. get_user_ex(vm86regs.pt.cx, &user_vm86->regs.ecx);
  250. get_user_ex(vm86regs.pt.dx, &user_vm86->regs.edx);
  251. get_user_ex(vm86regs.pt.si, &user_vm86->regs.esi);
  252. get_user_ex(vm86regs.pt.di, &user_vm86->regs.edi);
  253. get_user_ex(vm86regs.pt.bp, &user_vm86->regs.ebp);
  254. get_user_ex(vm86regs.pt.ax, &user_vm86->regs.eax);
  255. get_user_ex(vm86regs.pt.ip, &user_vm86->regs.eip);
  256. get_user_ex(seg, &user_vm86->regs.cs);
  257. vm86regs.pt.cs = seg;
  258. get_user_ex(vm86regs.pt.flags, &user_vm86->regs.eflags);
  259. get_user_ex(vm86regs.pt.sp, &user_vm86->regs.esp);
  260. get_user_ex(seg, &user_vm86->regs.ss);
  261. vm86regs.pt.ss = seg;
  262. get_user_ex(vm86regs.es, &user_vm86->regs.es);
  263. get_user_ex(vm86regs.ds, &user_vm86->regs.ds);
  264. get_user_ex(vm86regs.fs, &user_vm86->regs.fs);
  265. get_user_ex(vm86regs.gs, &user_vm86->regs.gs);
  266. get_user_ex(vm86->flags, &user_vm86->flags);
  267. get_user_ex(vm86->screen_bitmap, &user_vm86->screen_bitmap);
  268. get_user_ex(vm86->cpu_type, &user_vm86->cpu_type);
  269. } get_user_catch(err);
  270. if (err)
  271. return err;
  272. if (copy_from_user(&vm86->int_revectored,
  273. &user_vm86->int_revectored,
  274. sizeof(struct revectored_struct)))
  275. return -EFAULT;
  276. if (copy_from_user(&vm86->int21_revectored,
  277. &user_vm86->int21_revectored,
  278. sizeof(struct revectored_struct)))
  279. return -EFAULT;
  280. if (plus) {
  281. if (copy_from_user(&vm86->vm86plus, &user_vm86->vm86plus,
  282. sizeof(struct vm86plus_info_struct)))
  283. return -EFAULT;
  284. vm86->vm86plus.is_vm86pus = 1;
  285. } else
  286. memset(&vm86->vm86plus, 0,
  287. sizeof(struct vm86plus_info_struct));
  288. memcpy(&vm86->regs32, regs, sizeof(struct pt_regs));
  289. vm86->user_vm86 = user_vm86;
  290. /*
  291. * The flags register is also special: we cannot trust that the user
  292. * has set it up safely, so this makes sure interrupt etc flags are
  293. * inherited from protected mode.
  294. */
  295. VEFLAGS = vm86regs.pt.flags;
  296. vm86regs.pt.flags &= SAFE_MASK;
  297. vm86regs.pt.flags |= regs->flags & ~SAFE_MASK;
  298. vm86regs.pt.flags |= X86_VM_MASK;
  299. vm86regs.pt.orig_ax = regs->orig_ax;
  300. switch (vm86->cpu_type) {
  301. case CPU_286:
  302. vm86->veflags_mask = 0;
  303. break;
  304. case CPU_386:
  305. vm86->veflags_mask = X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  306. break;
  307. case CPU_486:
  308. vm86->veflags_mask = X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  309. break;
  310. default:
  311. vm86->veflags_mask = X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
  312. break;
  313. }
  314. /*
  315. * Save old state
  316. */
  317. vm86->saved_sp0 = tsk->thread.sp0;
  318. lazy_save_gs(vm86->regs32.gs);
  319. tss = &per_cpu(cpu_tss, get_cpu());
  320. /* make room for real-mode segments */
  321. tsk->thread.sp0 += 16;
  322. if (static_cpu_has(X86_FEATURE_SEP))
  323. tsk->thread.sysenter_cs = 0;
  324. load_sp0(tss, &tsk->thread);
  325. put_cpu();
  326. if (vm86->flags & VM86_SCREEN_BITMAP)
  327. mark_screen_rdonly(tsk->mm);
  328. memcpy((struct kernel_vm86_regs *)regs, &vm86regs, sizeof(vm86regs));
  329. force_iret();
  330. return regs->ax;
  331. }
  332. static inline void set_IF(struct kernel_vm86_regs *regs)
  333. {
  334. VEFLAGS |= X86_EFLAGS_VIF;
  335. }
  336. static inline void clear_IF(struct kernel_vm86_regs *regs)
  337. {
  338. VEFLAGS &= ~X86_EFLAGS_VIF;
  339. }
  340. static inline void clear_TF(struct kernel_vm86_regs *regs)
  341. {
  342. regs->pt.flags &= ~X86_EFLAGS_TF;
  343. }
  344. static inline void clear_AC(struct kernel_vm86_regs *regs)
  345. {
  346. regs->pt.flags &= ~X86_EFLAGS_AC;
  347. }
  348. /*
  349. * It is correct to call set_IF(regs) from the set_vflags_*
  350. * functions. However someone forgot to call clear_IF(regs)
  351. * in the opposite case.
  352. * After the command sequence CLI PUSHF STI POPF you should
  353. * end up with interrupts disabled, but you ended up with
  354. * interrupts enabled.
  355. * ( I was testing my own changes, but the only bug I
  356. * could find was in a function I had not changed. )
  357. * [KD]
  358. */
  359. static inline void set_vflags_long(unsigned long flags, struct kernel_vm86_regs *regs)
  360. {
  361. set_flags(VEFLAGS, flags, current->thread.vm86->veflags_mask);
  362. set_flags(regs->pt.flags, flags, SAFE_MASK);
  363. if (flags & X86_EFLAGS_IF)
  364. set_IF(regs);
  365. else
  366. clear_IF(regs);
  367. }
  368. static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs *regs)
  369. {
  370. set_flags(VFLAGS, flags, current->thread.vm86->veflags_mask);
  371. set_flags(regs->pt.flags, flags, SAFE_MASK);
  372. if (flags & X86_EFLAGS_IF)
  373. set_IF(regs);
  374. else
  375. clear_IF(regs);
  376. }
  377. static inline unsigned long get_vflags(struct kernel_vm86_regs *regs)
  378. {
  379. unsigned long flags = regs->pt.flags & RETURN_MASK;
  380. if (VEFLAGS & X86_EFLAGS_VIF)
  381. flags |= X86_EFLAGS_IF;
  382. flags |= X86_EFLAGS_IOPL;
  383. return flags | (VEFLAGS & current->thread.vm86->veflags_mask);
  384. }
  385. static inline int is_revectored(int nr, struct revectored_struct *bitmap)
  386. {
  387. return test_bit(nr, bitmap->__map);
  388. }
  389. #define val_byte(val, n) (((__u8 *)&val)[n])
  390. #define pushb(base, ptr, val, err_label) \
  391. do { \
  392. __u8 __val = val; \
  393. ptr--; \
  394. if (put_user(__val, base + ptr) < 0) \
  395. goto err_label; \
  396. } while (0)
  397. #define pushw(base, ptr, val, err_label) \
  398. do { \
  399. __u16 __val = val; \
  400. ptr--; \
  401. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  402. goto err_label; \
  403. ptr--; \
  404. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  405. goto err_label; \
  406. } while (0)
  407. #define pushl(base, ptr, val, err_label) \
  408. do { \
  409. __u32 __val = val; \
  410. ptr--; \
  411. if (put_user(val_byte(__val, 3), base + ptr) < 0) \
  412. goto err_label; \
  413. ptr--; \
  414. if (put_user(val_byte(__val, 2), base + ptr) < 0) \
  415. goto err_label; \
  416. ptr--; \
  417. if (put_user(val_byte(__val, 1), base + ptr) < 0) \
  418. goto err_label; \
  419. ptr--; \
  420. if (put_user(val_byte(__val, 0), base + ptr) < 0) \
  421. goto err_label; \
  422. } while (0)
  423. #define popb(base, ptr, err_label) \
  424. ({ \
  425. __u8 __res; \
  426. if (get_user(__res, base + ptr) < 0) \
  427. goto err_label; \
  428. ptr++; \
  429. __res; \
  430. })
  431. #define popw(base, ptr, err_label) \
  432. ({ \
  433. __u16 __res; \
  434. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  435. goto err_label; \
  436. ptr++; \
  437. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  438. goto err_label; \
  439. ptr++; \
  440. __res; \
  441. })
  442. #define popl(base, ptr, err_label) \
  443. ({ \
  444. __u32 __res; \
  445. if (get_user(val_byte(__res, 0), base + ptr) < 0) \
  446. goto err_label; \
  447. ptr++; \
  448. if (get_user(val_byte(__res, 1), base + ptr) < 0) \
  449. goto err_label; \
  450. ptr++; \
  451. if (get_user(val_byte(__res, 2), base + ptr) < 0) \
  452. goto err_label; \
  453. ptr++; \
  454. if (get_user(val_byte(__res, 3), base + ptr) < 0) \
  455. goto err_label; \
  456. ptr++; \
  457. __res; \
  458. })
  459. /* There are so many possible reasons for this function to return
  460. * VM86_INTx, so adding another doesn't bother me. We can expect
  461. * userspace programs to be able to handle it. (Getting a problem
  462. * in userspace is always better than an Oops anyway.) [KD]
  463. */
  464. static void do_int(struct kernel_vm86_regs *regs, int i,
  465. unsigned char __user *ssp, unsigned short sp)
  466. {
  467. unsigned long __user *intr_ptr;
  468. unsigned long segoffs;
  469. struct vm86 *vm86 = current->thread.vm86;
  470. if (regs->pt.cs == BIOSSEG)
  471. goto cannot_handle;
  472. if (is_revectored(i, &vm86->int_revectored))
  473. goto cannot_handle;
  474. if (i == 0x21 && is_revectored(AH(regs), &vm86->int21_revectored))
  475. goto cannot_handle;
  476. intr_ptr = (unsigned long __user *) (i << 2);
  477. if (get_user(segoffs, intr_ptr))
  478. goto cannot_handle;
  479. if ((segoffs >> 16) == BIOSSEG)
  480. goto cannot_handle;
  481. pushw(ssp, sp, get_vflags(regs), cannot_handle);
  482. pushw(ssp, sp, regs->pt.cs, cannot_handle);
  483. pushw(ssp, sp, IP(regs), cannot_handle);
  484. regs->pt.cs = segoffs >> 16;
  485. SP(regs) -= 6;
  486. IP(regs) = segoffs & 0xffff;
  487. clear_TF(regs);
  488. clear_IF(regs);
  489. clear_AC(regs);
  490. return;
  491. cannot_handle:
  492. save_v86_state(regs, VM86_INTx + (i << 8));
  493. }
  494. int handle_vm86_trap(struct kernel_vm86_regs *regs, long error_code, int trapno)
  495. {
  496. struct vm86 *vm86 = current->thread.vm86;
  497. if (vm86->vm86plus.is_vm86pus) {
  498. if ((trapno == 3) || (trapno == 1)) {
  499. save_v86_state(regs, VM86_TRAP + (trapno << 8));
  500. return 0;
  501. }
  502. do_int(regs, trapno, (unsigned char __user *) (regs->pt.ss << 4), SP(regs));
  503. return 0;
  504. }
  505. if (trapno != 1)
  506. return 1; /* we let this handle by the calling routine */
  507. current->thread.trap_nr = trapno;
  508. current->thread.error_code = error_code;
  509. force_sig(SIGTRAP, current);
  510. return 0;
  511. }
  512. void handle_vm86_fault(struct kernel_vm86_regs *regs, long error_code)
  513. {
  514. unsigned char opcode;
  515. unsigned char __user *csp;
  516. unsigned char __user *ssp;
  517. unsigned short ip, sp, orig_flags;
  518. int data32, pref_done;
  519. struct vm86plus_info_struct *vmpi = &current->thread.vm86->vm86plus;
  520. #define CHECK_IF_IN_TRAP \
  521. if (vmpi->vm86dbg_active && vmpi->vm86dbg_TFpendig) \
  522. newflags |= X86_EFLAGS_TF
  523. orig_flags = *(unsigned short *)&regs->pt.flags;
  524. csp = (unsigned char __user *) (regs->pt.cs << 4);
  525. ssp = (unsigned char __user *) (regs->pt.ss << 4);
  526. sp = SP(regs);
  527. ip = IP(regs);
  528. data32 = 0;
  529. pref_done = 0;
  530. do {
  531. switch (opcode = popb(csp, ip, simulate_sigsegv)) {
  532. case 0x66: /* 32-bit data */ data32 = 1; break;
  533. case 0x67: /* 32-bit address */ break;
  534. case 0x2e: /* CS */ break;
  535. case 0x3e: /* DS */ break;
  536. case 0x26: /* ES */ break;
  537. case 0x36: /* SS */ break;
  538. case 0x65: /* GS */ break;
  539. case 0x64: /* FS */ break;
  540. case 0xf2: /* repnz */ break;
  541. case 0xf3: /* rep */ break;
  542. default: pref_done = 1;
  543. }
  544. } while (!pref_done);
  545. switch (opcode) {
  546. /* pushf */
  547. case 0x9c:
  548. if (data32) {
  549. pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
  550. SP(regs) -= 4;
  551. } else {
  552. pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
  553. SP(regs) -= 2;
  554. }
  555. IP(regs) = ip;
  556. goto vm86_fault_return;
  557. /* popf */
  558. case 0x9d:
  559. {
  560. unsigned long newflags;
  561. if (data32) {
  562. newflags = popl(ssp, sp, simulate_sigsegv);
  563. SP(regs) += 4;
  564. } else {
  565. newflags = popw(ssp, sp, simulate_sigsegv);
  566. SP(regs) += 2;
  567. }
  568. IP(regs) = ip;
  569. CHECK_IF_IN_TRAP;
  570. if (data32)
  571. set_vflags_long(newflags, regs);
  572. else
  573. set_vflags_short(newflags, regs);
  574. goto check_vip;
  575. }
  576. /* int xx */
  577. case 0xcd: {
  578. int intno = popb(csp, ip, simulate_sigsegv);
  579. IP(regs) = ip;
  580. if (vmpi->vm86dbg_active) {
  581. if ((1 << (intno & 7)) & vmpi->vm86dbg_intxxtab[intno >> 3]) {
  582. save_v86_state(regs, VM86_INTx + (intno << 8));
  583. return;
  584. }
  585. }
  586. do_int(regs, intno, ssp, sp);
  587. return;
  588. }
  589. /* iret */
  590. case 0xcf:
  591. {
  592. unsigned long newip;
  593. unsigned long newcs;
  594. unsigned long newflags;
  595. if (data32) {
  596. newip = popl(ssp, sp, simulate_sigsegv);
  597. newcs = popl(ssp, sp, simulate_sigsegv);
  598. newflags = popl(ssp, sp, simulate_sigsegv);
  599. SP(regs) += 12;
  600. } else {
  601. newip = popw(ssp, sp, simulate_sigsegv);
  602. newcs = popw(ssp, sp, simulate_sigsegv);
  603. newflags = popw(ssp, sp, simulate_sigsegv);
  604. SP(regs) += 6;
  605. }
  606. IP(regs) = newip;
  607. regs->pt.cs = newcs;
  608. CHECK_IF_IN_TRAP;
  609. if (data32) {
  610. set_vflags_long(newflags, regs);
  611. } else {
  612. set_vflags_short(newflags, regs);
  613. }
  614. goto check_vip;
  615. }
  616. /* cli */
  617. case 0xfa:
  618. IP(regs) = ip;
  619. clear_IF(regs);
  620. goto vm86_fault_return;
  621. /* sti */
  622. /*
  623. * Damn. This is incorrect: the 'sti' instruction should actually
  624. * enable interrupts after the /next/ instruction. Not good.
  625. *
  626. * Probably needs some horsing around with the TF flag. Aiee..
  627. */
  628. case 0xfb:
  629. IP(regs) = ip;
  630. set_IF(regs);
  631. goto check_vip;
  632. default:
  633. save_v86_state(regs, VM86_UNKNOWN);
  634. }
  635. return;
  636. check_vip:
  637. if ((VEFLAGS & (X86_EFLAGS_VIP | X86_EFLAGS_VIF)) ==
  638. (X86_EFLAGS_VIP | X86_EFLAGS_VIF)) {
  639. save_v86_state(regs, VM86_STI);
  640. return;
  641. }
  642. vm86_fault_return:
  643. if (vmpi->force_return_for_pic && (VEFLAGS & (X86_EFLAGS_IF | X86_EFLAGS_VIF))) {
  644. save_v86_state(regs, VM86_PICRETURN);
  645. return;
  646. }
  647. if (orig_flags & X86_EFLAGS_TF)
  648. handle_vm86_trap(regs, 0, X86_TRAP_DB);
  649. return;
  650. simulate_sigsegv:
  651. /* FIXME: After a long discussion with Stas we finally
  652. * agreed, that this is wrong. Here we should
  653. * really send a SIGSEGV to the user program.
  654. * But how do we create the correct context? We
  655. * are inside a general protection fault handler
  656. * and has just returned from a page fault handler.
  657. * The correct context for the signal handler
  658. * should be a mixture of the two, but how do we
  659. * get the information? [KD]
  660. */
  661. save_v86_state(regs, VM86_UNKNOWN);
  662. }
  663. /* ---------------- vm86 special IRQ passing stuff ----------------- */
  664. #define VM86_IRQNAME "vm86irq"
  665. static struct vm86_irqs {
  666. struct task_struct *tsk;
  667. int sig;
  668. } vm86_irqs[16];
  669. static DEFINE_SPINLOCK(irqbits_lock);
  670. static int irqbits;
  671. #define ALLOWED_SIGS (1 /* 0 = don't send a signal */ \
  672. | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
  673. | (1 << SIGUNUSED))
  674. static irqreturn_t irq_handler(int intno, void *dev_id)
  675. {
  676. int irq_bit;
  677. unsigned long flags;
  678. spin_lock_irqsave(&irqbits_lock, flags);
  679. irq_bit = 1 << intno;
  680. if ((irqbits & irq_bit) || !vm86_irqs[intno].tsk)
  681. goto out;
  682. irqbits |= irq_bit;
  683. if (vm86_irqs[intno].sig)
  684. send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
  685. /*
  686. * IRQ will be re-enabled when user asks for the irq (whether
  687. * polling or as a result of the signal)
  688. */
  689. disable_irq_nosync(intno);
  690. spin_unlock_irqrestore(&irqbits_lock, flags);
  691. return IRQ_HANDLED;
  692. out:
  693. spin_unlock_irqrestore(&irqbits_lock, flags);
  694. return IRQ_NONE;
  695. }
  696. static inline void free_vm86_irq(int irqnumber)
  697. {
  698. unsigned long flags;
  699. free_irq(irqnumber, NULL);
  700. vm86_irqs[irqnumber].tsk = NULL;
  701. spin_lock_irqsave(&irqbits_lock, flags);
  702. irqbits &= ~(1 << irqnumber);
  703. spin_unlock_irqrestore(&irqbits_lock, flags);
  704. }
  705. void release_vm86_irqs(struct task_struct *task)
  706. {
  707. int i;
  708. for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
  709. if (vm86_irqs[i].tsk == task)
  710. free_vm86_irq(i);
  711. }
  712. static inline int get_and_reset_irq(int irqnumber)
  713. {
  714. int bit;
  715. unsigned long flags;
  716. int ret = 0;
  717. if (invalid_vm86_irq(irqnumber)) return 0;
  718. if (vm86_irqs[irqnumber].tsk != current) return 0;
  719. spin_lock_irqsave(&irqbits_lock, flags);
  720. bit = irqbits & (1 << irqnumber);
  721. irqbits &= ~bit;
  722. if (bit) {
  723. enable_irq(irqnumber);
  724. ret = 1;
  725. }
  726. spin_unlock_irqrestore(&irqbits_lock, flags);
  727. return ret;
  728. }
  729. static int do_vm86_irq_handling(int subfunction, int irqnumber)
  730. {
  731. int ret;
  732. switch (subfunction) {
  733. case VM86_GET_AND_RESET_IRQ: {
  734. return get_and_reset_irq(irqnumber);
  735. }
  736. case VM86_GET_IRQ_BITS: {
  737. return irqbits;
  738. }
  739. case VM86_REQUEST_IRQ: {
  740. int sig = irqnumber >> 8;
  741. int irq = irqnumber & 255;
  742. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  743. if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
  744. if (invalid_vm86_irq(irq)) return -EPERM;
  745. if (vm86_irqs[irq].tsk) return -EPERM;
  746. ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
  747. if (ret) return ret;
  748. vm86_irqs[irq].sig = sig;
  749. vm86_irqs[irq].tsk = current;
  750. return irq;
  751. }
  752. case VM86_FREE_IRQ: {
  753. if (invalid_vm86_irq(irqnumber)) return -EPERM;
  754. if (!vm86_irqs[irqnumber].tsk) return 0;
  755. if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
  756. free_vm86_irq(irqnumber);
  757. return 0;
  758. }
  759. }
  760. return -EINVAL;
  761. }