]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - arch/x86/kernel/vm86_32.c
x86/vm86: Move fields from 'struct kernel_vm86_struct' to 'struct vm86'
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / vm86_32.c
index fc9db6ef2a95937b1abd4f37f3a466043ba818d4..f71b4b9452f104bdcbb60d39170375caa53d0604 100644 (file)
@@ -44,6 +44,7 @@
 #include <linux/ptrace.h>
 #include <linux/audit.h>
 #include <linux/stddef.h>
+#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
@@ -67,7 +68,6 @@
 
 
 #define KVM86  ((struct kernel_vm86_struct *)regs)
-#define VMPI   KVM86->vm86plus
 
 
 /*
@@ -81,8 +81,8 @@
 /*
  * virtual flags (16 and 32-bit versions)
  */
-#define VFLAGS (*(unsigned short *)&(current->thread.v86flags))
-#define VEFLAGS        (current->thread.v86flags)
+#define VFLAGS (*(unsigned short *)&(current->thread.vm86->v86flags))
+#define VEFLAGS        (current->thread.vm86->v86flags)
 
 #define set_flags(X, new, mask) \
 ((X) = ((X) & ~(mask)) | ((new) & (mask)))
 #define SAFE_MASK      (0xDD5)
 #define RETURN_MASK    (0xDFF)
 
-/* convert kernel_vm86_regs to vm86_regs */
-static int copy_vm86_regs_to_user(struct vm86_regs __user *user,
-                                 const struct kernel_vm86_regs *regs)
-{
-       int ret = 0;
-
-       /*
-        * kernel_vm86_regs is missing gs, so copy everything up to
-        * (but not including) orig_eax, and then rest including orig_eax.
-        */
-       ret += copy_to_user(user, regs, offsetof(struct kernel_vm86_regs, pt.orig_ax));
-       ret += copy_to_user(&user->orig_eax, &regs->pt.orig_ax,
-                           sizeof(struct kernel_vm86_regs) -
-                           offsetof(struct kernel_vm86_regs, pt.orig_ax));
-
-       return ret;
-}
-
-/* convert vm86_regs to kernel_vm86_regs */
-static int copy_vm86_regs_from_user(struct kernel_vm86_regs *regs,
-                                   const struct vm86_regs __user *user,
-                                   unsigned extra)
-{
-       int ret = 0;
-
-       /* copy ax-fs inclusive */
-       ret += copy_from_user(regs, user, offsetof(struct kernel_vm86_regs, pt.orig_ax));
-       /* copy orig_ax-__gsh+extra */
-       ret += copy_from_user(&regs->pt.orig_ax, &user->orig_eax,
-                             sizeof(struct kernel_vm86_regs) -
-                             offsetof(struct kernel_vm86_regs, pt.orig_ax) +
-                             extra);
-       return ret;
-}
-
 struct pt_regs *save_v86_state(struct kernel_vm86_regs *regs)
 {
        struct tss_struct *tss;
        struct pt_regs *ret;
-       unsigned long tmp;
+       struct task_struct *tsk = current;
+       struct vm86plus_struct __user *user;
+       struct vm86 *vm86 = current->thread.vm86;
+       long err = 0;
 
        /*
         * This gets called from entry.S with interrupts disabled, but
@@ -138,29 +106,55 @@ struct pt_regs *save_v86_state(struct kernel_vm86_regs *regs)
         */
        local_irq_enable();
 
-       if (!current->thread.vm86_info) {
+       if (!vm86 || !vm86->vm86_info) {
                pr_alert("no vm86_info: BAD\n");
                do_exit(SIGSEGV);
        }
-       set_flags(regs->pt.flags, VEFLAGS, X86_EFLAGS_VIF | current->thread.v86mask);
-       tmp = copy_vm86_regs_to_user(&current->thread.vm86_info->regs, regs);
-       tmp += put_user(current->thread.screen_bitmap, &current->thread.vm86_info->screen_bitmap);
-       if (tmp) {
+       set_flags(regs->pt.flags, VEFLAGS, X86_EFLAGS_VIF | vm86->v86mask);
+       user = vm86->vm86_info;
+
+       if (!access_ok(VERIFY_WRITE, user, vm86->vm86plus.is_vm86pus ?
+                      sizeof(struct vm86plus_struct) :
+                      sizeof(struct vm86_struct))) {
+               pr_alert("could not access userspace vm86_info\n");
+               do_exit(SIGSEGV);
+       }
+
+       put_user_try {
+               put_user_ex(regs->pt.bx, &user->regs.ebx);
+               put_user_ex(regs->pt.cx, &user->regs.ecx);
+               put_user_ex(regs->pt.dx, &user->regs.edx);
+               put_user_ex(regs->pt.si, &user->regs.esi);
+               put_user_ex(regs->pt.di, &user->regs.edi);
+               put_user_ex(regs->pt.bp, &user->regs.ebp);
+               put_user_ex(regs->pt.ax, &user->regs.eax);
+               put_user_ex(regs->pt.ip, &user->regs.eip);
+               put_user_ex(regs->pt.cs, &user->regs.cs);
+               put_user_ex(regs->pt.flags, &user->regs.eflags);
+               put_user_ex(regs->pt.sp, &user->regs.esp);
+               put_user_ex(regs->pt.ss, &user->regs.ss);
+               put_user_ex(regs->es, &user->regs.es);
+               put_user_ex(regs->ds, &user->regs.ds);
+               put_user_ex(regs->fs, &user->regs.fs);
+               put_user_ex(regs->gs, &user->regs.gs);
+
+               put_user_ex(vm86->screen_bitmap, &user->screen_bitmap);
+       } put_user_catch(err);
+       if (err) {
                pr_alert("could not access userspace vm86_info\n");
                do_exit(SIGSEGV);
        }
 
        tss = &per_cpu(cpu_tss, get_cpu());
-       current->thread.sp0 = current->thread.saved_sp0;
-       current->thread.sysenter_cs = __KERNEL_CS;
-       load_sp0(tss, &current->thread);
-       current->thread.saved_sp0 = 0;
+       tsk->thread.sp0 = vm86->saved_sp0;
+       tsk->thread.sysenter_cs = __KERNEL_CS;
+       load_sp0(tss, &tsk->thread);
+       vm86->saved_sp0 = 0;
        put_cpu();
 
        ret = KVM86->regs32;
 
-       ret->fs = current->thread.saved_fs;
-       set_user_gs(ret, current->thread.saved_gs);
+       lazy_load_gs(ret->gs);
 
        return ret;
 }
@@ -200,7 +194,8 @@ out:
 
 
 static int do_vm86_irq_handling(int subfunction, int irqnumber);
-static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
+static long do_sys_vm86(struct vm86plus_struct __user *v86, bool plus,
+                       struct kernel_vm86_struct *info);
 
 SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86)
 {
@@ -209,21 +204,8 @@ SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86)
                                         * This remains on the stack until we
                                         * return to 32 bit user space.
                                         */
-       struct task_struct *tsk = current;
-       int tmp;
 
-       if (tsk->thread.saved_sp0)
-               return -EPERM;
-       tmp = copy_vm86_regs_from_user(&info.regs, &v86->regs,
-                                      offsetof(struct kernel_vm86_struct, vm86plus) -
-                                      sizeof(info.regs));
-       if (tmp)
-               return -EFAULT;
-       memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
-       info.regs32 = current_pt_regs();
-       tsk->thread.vm86_info = v86;
-       do_sys_vm86(&info, tsk);
-       return 0;       /* we never return here */
+       return do_sys_vm86((struct vm86plus_struct __user *) v86, false, &info);
 }
 
 
@@ -234,11 +216,7 @@ SYSCALL_DEFINE2(vm86, unsigned long, cmd, unsigned long, arg)
                                         * This remains on the stack until we
                                         * return to 32 bit user space.
                                         */
-       struct task_struct *tsk;
-       int tmp;
-       struct vm86plus_struct __user *v86;
 
-       tsk = current;
        switch (cmd) {
        case VM86_REQUEST_IRQ:
        case VM86_FREE_IRQ:
@@ -256,34 +234,77 @@ SYSCALL_DEFINE2(vm86, unsigned long, cmd, unsigned long, arg)
        }
 
        /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
-       if (tsk->thread.saved_sp0)
-               return -EPERM;
-       v86 = (struct vm86plus_struct __user *)arg;
-       tmp = copy_vm86_regs_from_user(&info.regs, &v86->regs,
-                                      offsetof(struct kernel_vm86_struct, regs32) -
-                                      sizeof(info.regs));
-       if (tmp)
-               return -EFAULT;
-       info.regs32 = current_pt_regs();
-       info.vm86plus.is_vm86pus = 1;
-       tsk->thread.vm86_info = (struct vm86_struct __user *)v86;
-       do_sys_vm86(&info, tsk);
-       return 0;       /* we never return here */
+       return do_sys_vm86((struct vm86plus_struct __user *) arg, true, &info);
 }
 
 
-static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
+static long do_sys_vm86(struct vm86plus_struct __user *v86, bool plus,
+                       struct kernel_vm86_struct *info)
 {
        struct tss_struct *tss;
-/*
- * make sure the vm86() system call doesn't try to do anything silly
- */
-       info->regs.pt.ds = 0;
-       info->regs.pt.es = 0;
-       info->regs.pt.fs = 0;
-#ifndef CONFIG_X86_32_LAZY_GS
-       info->regs.pt.gs = 0;
-#endif
+       struct task_struct *tsk = current;
+       struct vm86 *vm86 = tsk->thread.vm86;
+       unsigned long err = 0;
+
+       if (!vm86) {
+               if (!(vm86 = kzalloc(sizeof(*vm86), GFP_KERNEL)))
+                       return -ENOMEM;
+               tsk->thread.vm86 = vm86;
+       }
+       if (vm86->saved_sp0)
+               return -EPERM;
+
+       if (!access_ok(VERIFY_READ, v86, plus ?
+                      sizeof(struct vm86_struct) :
+                      sizeof(struct vm86plus_struct)))
+               return -EFAULT;
+
+       memset(info, 0, sizeof(*info));
+       get_user_try {
+               unsigned short seg;
+               get_user_ex(info->regs.pt.bx, &v86->regs.ebx);
+               get_user_ex(info->regs.pt.cx, &v86->regs.ecx);
+               get_user_ex(info->regs.pt.dx, &v86->regs.edx);
+               get_user_ex(info->regs.pt.si, &v86->regs.esi);
+               get_user_ex(info->regs.pt.di, &v86->regs.edi);
+               get_user_ex(info->regs.pt.bp, &v86->regs.ebp);
+               get_user_ex(info->regs.pt.ax, &v86->regs.eax);
+               get_user_ex(info->regs.pt.ip, &v86->regs.eip);
+               get_user_ex(seg, &v86->regs.cs);
+               info->regs.pt.cs = seg;
+               get_user_ex(info->regs.pt.flags, &v86->regs.eflags);
+               get_user_ex(info->regs.pt.sp, &v86->regs.esp);
+               get_user_ex(seg, &v86->regs.ss);
+               info->regs.pt.ss = seg;
+               get_user_ex(info->regs.es, &v86->regs.es);
+               get_user_ex(info->regs.ds, &v86->regs.ds);
+               get_user_ex(info->regs.fs, &v86->regs.fs);
+               get_user_ex(info->regs.gs, &v86->regs.gs);
+
+               get_user_ex(vm86->flags, &v86->flags);
+               get_user_ex(vm86->screen_bitmap, &v86->screen_bitmap);
+               get_user_ex(vm86->cpu_type, &v86->cpu_type);
+       } get_user_catch(err);
+       if (err)
+               return err;
+
+       if (copy_from_user(&vm86->int_revectored, &v86->int_revectored,
+                          sizeof(struct revectored_struct)))
+               return -EFAULT;
+       if (copy_from_user(&vm86->int21_revectored, &v86->int21_revectored,
+                          sizeof(struct revectored_struct)))
+               return -EFAULT;
+       if (plus) {
+               if (copy_from_user(&vm86->vm86plus, &v86->vm86plus,
+                                  sizeof(struct vm86plus_info_struct)))
+                       return -EFAULT;
+               vm86->vm86plus.is_vm86pus = 1;
+       } else
+               memset(&vm86->vm86plus, 0,
+                      sizeof(struct vm86plus_info_struct));
+
+       info->regs32 = current_pt_regs();
+       vm86->vm86_info = v86;
 
 /*
  * The flags register is also special: we cannot trust that the user
@@ -295,18 +316,20 @@ static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk
        info->regs.pt.flags |= info->regs32->flags & ~SAFE_MASK;
        info->regs.pt.flags |= X86_VM_MASK;
 
-       switch (info->cpu_type) {
+       info->regs.pt.orig_ax = info->regs32->orig_ax;
+
+       switch (vm86->cpu_type) {
        case CPU_286:
-               tsk->thread.v86mask = 0;
+               vm86->v86mask = 0;
                break;
        case CPU_386:
-               tsk->thread.v86mask = X86_EFLAGS_NT | X86_EFLAGS_IOPL;
+               vm86->v86mask = X86_EFLAGS_NT | X86_EFLAGS_IOPL;
                break;
        case CPU_486:
-               tsk->thread.v86mask = X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
+               vm86->v86mask = X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
                break;
        default:
-               tsk->thread.v86mask = X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
+               vm86->v86mask = X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL;
                break;
        }
 
@@ -314,9 +337,8 @@ static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk
  * Save old state, set default return value (%ax) to 0 (VM86_SIGNAL)
  */
        info->regs32->ax = VM86_SIGNAL;
-       tsk->thread.saved_sp0 = tsk->thread.sp0;
-       tsk->thread.saved_fs = info->regs32->fs;
-       tsk->thread.saved_gs = get_user_gs(info->regs32);
+       vm86->saved_sp0 = tsk->thread.sp0;
+       lazy_save_gs(info->regs32->gs);
 
        tss = &per_cpu(cpu_tss, get_cpu());
        tsk->thread.sp0 = (unsigned long) &info->VM86_TSS_ESP0;
@@ -325,8 +347,7 @@ static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk
        load_sp0(tss, &tsk->thread);
        put_cpu();
 
-       tsk->thread.screen_bitmap = info->screen_bitmap;
-       if (info->flags & VM86_SCREEN_BITMAP)
+       if (vm86->flags & VM86_SCREEN_BITMAP)
                mark_screen_rdonly(tsk->mm);
 
        /*call __audit_syscall_exit since we do not exit via the normal paths */
@@ -344,7 +365,7 @@ static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk
                "jmp resume_userspace"
                : /* no outputs */
                :"r" (&info->regs), "r" (task_thread_info(tsk)), "r" (0));
-       /* we never return here */
+       unreachable();  /* we never return here */
 }
 
 static inline void return_to_32bit(struct kernel_vm86_regs *regs16, int retval)
@@ -395,7 +416,7 @@ static inline void clear_AC(struct kernel_vm86_regs *regs)
 
 static inline void set_vflags_long(unsigned long flags, struct kernel_vm86_regs *regs)
 {
-       set_flags(VEFLAGS, flags, current->thread.v86mask);
+       set_flags(VEFLAGS, flags, current->thread.vm86->v86mask);
        set_flags(regs->pt.flags, flags, SAFE_MASK);
        if (flags & X86_EFLAGS_IF)
                set_IF(regs);
@@ -405,7 +426,7 @@ static inline void set_vflags_long(unsigned long flags, struct kernel_vm86_regs
 
 static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs *regs)
 {
-       set_flags(VFLAGS, flags, current->thread.v86mask);
+       set_flags(VFLAGS, flags, current->thread.vm86->v86mask);
        set_flags(regs->pt.flags, flags, SAFE_MASK);
        if (flags & X86_EFLAGS_IF)
                set_IF(regs);
@@ -420,7 +441,7 @@ static inline unsigned long get_vflags(struct kernel_vm86_regs *regs)
        if (VEFLAGS & X86_EFLAGS_VIF)
                flags |= X86_EFLAGS_IF;
        flags |= X86_EFLAGS_IOPL;
-       return flags | (VEFLAGS & current->thread.v86mask);
+       return flags | (VEFLAGS & current->thread.vm86->v86mask);
 }
 
 static inline int is_revectored(int nr, struct revectored_struct *bitmap)
@@ -518,12 +539,13 @@ static void do_int(struct kernel_vm86_regs *regs, int i,
 {
        unsigned long __user *intr_ptr;
        unsigned long segoffs;
+       struct kernel_vm86_info *vm86 = current->thread.vm86;
 
        if (regs->pt.cs == BIOSSEG)
                goto cannot_handle;
-       if (is_revectored(i, &KVM86->int_revectored))
+       if (is_revectored(i, &vm86->int_revectored))
                goto cannot_handle;
-       if (i == 0x21 && is_revectored(AH(regs), &KVM86->int21_revectored))
+       if (i == 0x21 && is_revectored(AH(regs), &vm86->int21_revectored))
                goto cannot_handle;
        intr_ptr = (unsigned long __user *) (i << 2);
        if (get_user(segoffs, intr_ptr))
@@ -547,7 +569,7 @@ cannot_handle:
 
 int handle_vm86_trap(struct kernel_vm86_regs *regs, long error_code, int trapno)
 {
-       if (VMPI.is_vm86pus) {
+       if (current->thread.vm86->vm86plus.is_vm86pus) {
                if ((trapno == 3) || (trapno == 1)) {
                        KVM86->regs32->ax = VM86_TRAP + (trapno << 8);
                        /* setting this flag forces the code in entry_32.S to
@@ -574,12 +596,13 @@ void handle_vm86_fault(struct kernel_vm86_regs *regs, long error_code)
        unsigned char __user *ssp;
        unsigned short ip, sp, orig_flags;
        int data32, pref_done;
+       struct vm86plus_info_struct *vmpi = &current->thread.vm86->vm86plus;
 
 #define CHECK_IF_IN_TRAP \
-       if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
+       if (vmpi->vm86dbg_active && vmpi->vm86dbg_TFpendig) \
                newflags |= X86_EFLAGS_TF
 #define VM86_FAULT_RETURN do { \
-       if (VMPI.force_return_for_pic  && (VEFLAGS & (X86_EFLAGS_IF | X86_EFLAGS_VIF))) \
+       if (vmpi->force_return_for_pic  && (VEFLAGS & (X86_EFLAGS_IF | X86_EFLAGS_VIF))) \
                return_to_32bit(regs, VM86_PICRETURN); \
        if (orig_flags & X86_EFLAGS_TF) \
                handle_vm86_trap(regs, 0, 1); \
@@ -649,8 +672,8 @@ void handle_vm86_fault(struct kernel_vm86_regs *regs, long error_code)
        case 0xcd: {
                int intno = popb(csp, ip, simulate_sigsegv);
                IP(regs) = ip;
-               if (VMPI.vm86dbg_active) {
-                       if ((1 << (intno & 7)) & VMPI.vm86dbg_intxxtab[intno >> 3])
+               if (vmpi->vm86dbg_active) {
+                       if ((1 << (intno & 7)) & vmpi->vm86dbg_intxxtab[intno >> 3])
                                return_to_32bit(regs, VM86_INTx + (intno << 8));
                }
                do_int(regs, intno, ssp, sp);