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