x86/vm86: First round of vm86 cleanups
Subsystems (3)
cover x86/vm86: First round of vm86 cleanups
The goal of this set of patches is to change vm86 support to return to userspace with the normal exit paths instead of leaving data on the kernel stack and jumping directly into the exit asm routines. This fixes issues like ptrace and syscall auditing not working with vm86, and makes possible cleanups in the syscall exit work code.
arch/x86/entry/entry_32.S | 24 +--- arch/x86/include/asm/processor.h | 11 +- arch/x86/include/asm/thread_info.h | 11 +- arch/x86/include/asm/vm86.h | 36 ++--- arch/x86/kernel/process.c | 7 + arch/x86/kernel/signal.c | 3 + arch/x86/kernel/vm86_32.c | 265 ++++++++++++++++--------------------- arch/x86/mm/fault.c | 4 +- 8 files changed, 152 insertions(+), 209 deletions(-)
PATCH 1/5 x86/vm86: Move userspace accesses to do_sys_vm86()
Move the userspace accesses down into the common function in preparation for the next set of patches.
--- arch/x86/kernel/vm86_32.c | 61 +++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 34 deletions(-)
arch/x86/kernel/vm86_32.c +27 -34
@@ -200,7 +200,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 +210,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 +222,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,25 +240,34 @@ 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; +struct task_struct *tsk = current; + +if (tsk->thread.saved_sp0) +return -EPERM ; +if (plus) { +if (copy_vm86_regs_from_user (&info->regs, &v86->regs, + offsetof(struct kernel_vm86_struct , regs32) - +sizeof (info->regs))) +return -EFAULT ; + info->vm86plus.is_vm86pus =1 ; + }else { +if (copy_vm86_regs_from_user (&info->regs, &v86->regs, + offsetof(struct kernel_vm86_struct , vm86plus) - +sizeof (info->regs))) +return -EFAULT ; +memset (&info->vm86plus,0 ,sizeof (struct vm86plus_info_struct )); + } + info->regs32 =current_pt_regs (); + tsk->thread.vm86_info = (struct vm86_struct __user *) v86; +/* * make sure the vm86() system call doesn't try to do anything silly */
@@ -344,7 +337,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 */ +return 0 ;/* we never return here */ }static inline void return_to_32bit (struct kernel_vm86_regs *regs16,int retval)
Signature · 1 line
2.4.3
Re: [PATCH 1/5] x86/vm86: Move userspace accesses to do_sys_vm86()
Quoted 3 lines · Brian Gerst
Move the userspace accesses down into the common function in preparation for the next set of patches.
One thing I don't like about the current code that makes these patches harder to review is the bizarre approach to copying. If you changed this:
arch/x86/kernel/vm86_32.c SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86) · line 219
Quoted 3 lines
arch/x86/kernel/vm86_32.c +0
-3- tmp =copy_vm86_regs_from_user (&info.regs, &v86->regs, - offsetof(struct kernel_vm86_struct , vm86plus) - -sizeof (info.regs));
into a normal field-by-field get_user / copy_from_user (the latter for the big regs struct) then it would be clear what the ABI is and it would be much easier to read the patches and confirm that you aren't accidentally changing the ABI.
You could also get rid of the constraint that certain fields in apparently kernel-internal structs had to be in a certain order.
Other than that, patches 1-4 look good on cursory inspection. I'll look more carefully later. I need to think about patch 5 more.
--Andy
Re: [PATCH 1/5] x86/vm86: Move userspace accesses to do_sys_vm86()
Quoted 18 lines · Andy Lutomirski
Quoted 3 lines · Brian Gerst
Move the userspace accesses down into the common function in preparation for the next set of patches.
One thing I don't like about the current code that makes these patches harder to review is the bizarre approach to copying. If you changed this:
arch/x86/kernel/vm86_32.c SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86) · line 219
Quoted 3 lines
arch/x86/kernel/vm86_32.c +0
-3- tmp =copy_vm86_regs_from_user (&info.regs, &v86->regs, - offsetof(struct kernel_vm86_struct , vm86plus) - -sizeof (info.regs));into a normal field-by-field
get_user/copy_from_user(the latter for the big regs struct) then it would be clear what the ABI is and it would be much easier to read the patches and confirm that you aren't accidentally changing the ABI.
Quoted 7 lines
You could also get rid of the constraint that certain fields in apparently kernel-internal structs had to be in a certain order.
Other than that, patches 1-4 look good on cursory inspection. I'll look more carefully later. I need to think about patch 5 more.
--Andy
Any other comments before I start working on v2?
--
Brian Gerst
Re: [PATCH 1/5] x86/vm86: Move userspace accesses to do_sys_vm86()
Quoted 22 lines · Brian Gerst
Quoted 21 lines · Andy Lutomirski
Quoted 3 lines · Brian Gerst
Move the userspace accesses down into the common function in preparation for the next set of patches.
One thing I don't like about the current code that makes these patches harder to review is the bizarre approach to copying. If you changed this:
arch/x86/kernel/vm86_32.c SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86) · line 219
Quoted 3 lines
arch/x86/kernel/vm86_32.c +0
-3- tmp =copy_vm86_regs_from_user (&info.regs, &v86->regs, - offsetof(struct kernel_vm86_struct , vm86plus) - -sizeof (info.regs));into a normal field-by-field
get_user/copy_from_user(the latter for the big regs struct) then it would be clear what the ABI is and it would be much easier to read the patches and confirm that you aren't accidentally changing the ABI.You could also get rid of the constraint that certain fields in apparently kernel-internal structs had to be in a certain order.
Quoted 7 lines
Quoted 4 lines
Other than that, patches 1-4 look good on cursory inspection. I'll look more carefully later. I need to think about patch 5 more.
--Andy
Any other comments before I start working on v2?
Nothing major. I'm a bit nervous about leaving ds, es, fs, and gs in pt_regs more or less undefined until save_v86_state happens, but it's unlikely that there's any ABI to break there. The results from perf might be a bit odd with your patches applied. Of course, they're probably useless without your patch.
It might also be worth renaming save_v86_state in patch 5.
Do your patches pass my upgraded entry_from_vm86 test? You're changing handle_vm86_trap so it always returns, which may have unexpected side effects (or I missed something in your patch).
--Andy
Re: [PATCH 1/5] x86/vm86: Move userspace accesses to do_sys_vm86()
Quoted 26 lines · Andy Lutomirski
Quoted 25 lines · Brian Gerst
Quoted 24 lines · Andy Lutomirski
Quoted 3 lines · Brian Gerst
Move the userspace accesses down into the common function in preparation for the next set of patches.
One thing I don't like about the current code that makes these patches harder to review is the bizarre approach to copying. If you changed this:
arch/x86/kernel/vm86_32.c SYSCALL_DEFINE1(vm86old, struct vm86_struct __user *, v86) · line 219
Quoted 3 lines
arch/x86/kernel/vm86_32.c +0
-3- tmp =copy_vm86_regs_from_user (&info.regs, &v86->regs, - offsetof(struct kernel_vm86_struct , vm86plus) - -sizeof (info.regs));into a normal field-by-field
get_user/copy_from_user(the latter for the big regs struct) then it would be clear what the ABI is and it would be much easier to read the patches and confirm that you aren't accidentally changing the ABI.You could also get rid of the constraint that certain fields in apparently kernel-internal structs had to be in a certain order.
Other than that, patches 1-4 look good on cursory inspection. I'll look more carefully later. I need to think about patch 5 more.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
--Andy
Any other comments before I start working on v2?
Nothing major. I'm a bit nervous about leaving ds, es, fs, and gs in
pt_regsmore or less undefined untilsave_v86_statehappens, but it's unlikely that there's any ABI to break there.
They are not undefined. The CPU sets them to NULL on exit from vm86 mode, after pushing them before the normal exception frame. The entry code then pushes these NULL values into the normal segment slots in pt_regs. The NULL values would be visible to ptrace (which should be taught to look at the alternate slots), but I don't see any other problems.
Quoted 9 lines
The results from perf
might be a bit odd with your patches applied. Of course, they're probably useless without your patch.It might also be worth renaming
save_v86_statein patch 5.Do your patches pass my upgraded
entry_from_vm86test? You're changinghandle_vm86_trapso it always returns, which may have unexpected side effects (or I missed something in your patch).
Yes, it passes all the tests. Changing the trap/fault handlers to always return (instead of switching the stack pointer and jumping to the exit asm) is the whole point of this patch set. save_v86_state() now just swaps the register state in place and sets the syscall return value. This allows returning from the exception handler in the normal manner (skipping the signal if the exception was handled). An unhandled exception like #UD will raise a signal, which then is handled by the change in handle_signal(), as well as external signals.
--
Brian Gerst
PATCH 2/5 x86/vm86: Move vm86 fields out of thread_struct
In reply to Brian GerstAllocate a separate structure for the vm86 fields.
--- arch/x86/include/asm/processor.h | 11 ++------ arch/x86/include/asm/vm86.h | 10 ++++++++ arch/x86/kernel/process.c | 7 +++++ arch/x86/kernel/vm86_32.c | 55 +++++++++++++++++++++++----------------- arch/x86/mm/fault.c | 4 +-- 5 files changed, 53 insertions(+), 34 deletions(-)
arch/x86/include/asm/processor.h +2 -9
@@ -403,15 +403,9 @@ struct thread_struct {unsigned long cr2;unsigned long trap_nr;unsigned long error_code; -#ifdef CONFIG_X86_32 +#ifdef CONFIG_VM86 /* Virtual 86 mode info */ -struct vm86_struct __user *vm86_info; -unsigned long screen_bitmap; -unsigned long v86flags; -unsigned long v86mask; -unsigned long saved_sp0; -unsigned int saved_fs; -unsigned int saved_gs; +struct kernel_vm86_info *vm86;#endif /* IO permissions: */ unsigned long *io_bitmap_ptr;
@@ -716,7 +710,6 @@ static inline void spin_lock_prefetch(const void *x)#define INIT_THREAD { \ .sp0 = TOP_OF_INIT_STACK, \ - .vm86_info = NULL, \ .sysenter_cs = __KERNEL_CS, \ .io_bitmap_ptr = NULL, \ }
arch/x86/include/asm/vm86.h +10 -0
@@ -58,6 +58,16 @@ struct kernel_vm86_struct {*/ }; +struct kernel_vm86_info { +struct vm86_struct __user *vm86_info; +unsigned long screen_bitmap; +unsigned long v86flags; +unsigned long v86mask; +unsigned long saved_sp0; +unsigned int saved_fs; +unsigned int saved_gs; +}; +#ifdef CONFIG_VM86 void handle_vm86_fault (struct kernel_vm86_regs *,long );
arch/x86/kernel/process.c +7 -0
@@ -110,6 +110,13 @@ void exit_thread(void)kfree (bp); } +#ifdef CONFIG_VM86 +if (t->vm86) { +kfree (t->vm86); + t->vm86 =NULL ; + } +#endif +fpu__drop (fpu); }
arch/x86/kernel/vm86_32.c +32 -23
149 lines not on this page · raw message
arch/x86/mm/fault.c +2 -2
@@ -315,12 +315,12 @@ check_v8086_mode(struct pt_regs *regs, unsigned long address, {unsigned long bit; -if (!v8086_mode (regs)) +if (!v8086_mode (regs) || !tsk->thread.vm86)return ; bit = (address -0xA0000 ) >>PAGE_SHIFT ;if (bit <32 ) - tsk->thread.screen_bitmap |=1 << bit; + tsk->thread.vm86->screen_bitmap |=1 << bit; }static bool low_pfn (unsigned long pfn)
Signature · 1 line
2.4.3
PATCH 3/5 x86/vm86: Move fields from kernel_vm86_struct
In reply to Brian GerstMove the non-regs fields to the off-stack data.
--- arch/x86/include/asm/vm86.h | 17 ++++++++-------- arch/x86/kernel/vm86_32.c | 48 ++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 32 deletions(-)
arch/x86/include/asm/vm86.h +9 -8
@@ -38,13 +38,7 @@ struct kernel_vm86_struct {* Therefore, pt_regs in fact points to a complete 'kernel_vm86_struct' * in kernelspace, hence we need not reget the data from userspace. */ -#define VM86_TSS_ESP0 flags -unsigned long flags; -unsigned long screen_bitmap; -unsigned long cpu_type; -struct revectored_struct int_revectored; -struct revectored_struct int21_revectored; -struct vm86plus_info_struct vm86plus; +#define VM86_TSS_ESP0 regs32struct pt_regs *regs32;/* here we save the pointer to the old regs */ /* * The below is not part of the structure, but the stack layout continues
@@ -60,12 +54,19 @@ struct kernel_vm86_struct {struct kernel_vm86_info {struct vm86_struct __user *vm86_info; -unsigned long screen_bitmap;unsigned long v86flags;unsigned long v86mask;unsigned long saved_sp0;unsigned int saved_fs;unsigned int saved_gs; + +/* Must match vm86plus_struct after regs */ +unsigned long flags; +unsigned long screen_bitmap; +unsigned long cpu_type; +struct revectored_struct int_revectored; +struct revectored_struct int21_revectored; +struct vm86plus_info_struct vm86plus; };#ifdef CONFIG_VM86
arch/x86/kernel/vm86_32.c +24 -24
121 lines not on this page · raw message
Signature · 1 line
2.4.3
PATCH 4/5 x86/vm86: Eliminate kernel_vm86_struct
In reply to Brian GerstNow there is no vm86-specific data left on the kernel stack while in userspace, except for the 32-bit regs.
--- arch/x86/include/asm/vm86.h | 25 +---------------- arch/x86/kernel/vm86_32.c | 68 +++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 64 deletions(-)
arch/x86/include/asm/vm86.h +1 -24
@@ -28,32 +28,9 @@ struct kernel_vm86_regs {unsigned short gs, __gsh; }; -struct kernel_vm86_struct { -struct kernel_vm86_regs regs; -/* -* the below part remains on the kernel stack while we are in VM86 mode. -* 'tss.esp0' then contains the address of VM86_TSS_ESP0 below, and when we -* get forced back from VM86, the CPU and "SAVE_ALL" will restore the above -* 'struct kernel_vm86_regs' with the then actual values. -* Therefore, pt_regs in fact points to a complete 'kernel_vm86_struct' -* in kernelspace, hence we need not reget the data from userspace. -*/ -#define VM86_TSS_ESP0 regs32 -struct pt_regs *regs32;/* here we save the pointer to the old regs */ -/* -* The below is not part of the structure, but the stack layout continues -* this way. In front of 'return-eip' may be some data, depending on -* compilation, so we don't rely on this and save the pointer to 'oldregs' -* in 'regs32' above. -* However, with GCC-2.7.2 and the current CFLAGS you see exactly this: - -long return-eip; from call to vm86() -struct pt_regs oldregs; user space registers as saved by syscall -*/ -}; -struct kernel_vm86_info {struct vm86_struct __user *vm86_info; +struct pt_regs *regs32;unsigned long v86flags;unsigned long v86mask;unsigned long saved_sp0;
arch/x86/kernel/vm86_32.c +28 -40
155 lines not on this page · raw message
Signature · 1 line
2.4.3
PATCH 5/5 x86/vm86: Use the normal pt_regs area for vm86
In reply to Brian GerstChange to use the normal pt_regs area to enter and exit vm86 mode. This is done by increasing the padding at the top of the stack to make room for the extra vm86 segment slots in the IRET frame. It then saves the 32-bit regs in the off-stack vm86 data, and copies in the vm86 regs. Exiting back to 32-bit mode does the reverse. This allows removing the hacks to jump directly into the exit asm code due to having to change the stack pointer. Returning normally from the vm86 syscall and the exception handlers allows things like ptrace and auditing to work properly.
--- arch/x86/entry/entry_32.S | 24 +------ arch/x86/include/asm/thread_info.h | 11 ++-- arch/x86/include/asm/vm86.h | 6 +- arch/x86/kernel/signal.c | 3 + arch/x86/kernel/vm86_32.c | 129 ++++++++++++++++--------------------- 5 files changed, 69 insertions(+), 104 deletions(-)
arch/x86/entry/entry_32.S +1 -23
@@ -525,34 +525,12 @@ work_resched: work_notifysig: # deal with pending signals and # notify-resume requests -#ifdef CONFIG_VM86 - testl $X86_EFLAGS_VM, PT_EFLAGS(%esp) - movl %esp, %eax - jnz work_notifysig_v86 # returning to kernel-space or - # vm86-space -1: -#else - movl %esp, %eax -#endif TRACE_IRQS_ON ENABLE_INTERRUPTS(CLBR_NONE) - movb PT_CS(%esp), %bl - andb $SEGMENT_RPL_MASK, %bl - cmpb $USER_RPL, %bl - jb resume_kernel + movl %esp, %eax xorl %edx, %edx call do_notify_resume jmp resume_userspace - -#ifdef CONFIG_VM86 - ALIGN -work_notifysig_v86: - pushl %ecx # save ti_flags for do_notify_resume - call save_v86_state # %eax contains pt_regs pointer - popl %ecx - movl %eax, %esp - jmp 1b -#endif END(work_pending) # perform syscall exit tracing
arch/x86/include/asm/thread_info.h +7 -4
@@ -27,14 +27,17 @@* Without this offset, that can result in a page fault. (We are * careful that, in this case, the value we read doesn't matter.) * -* In vm86 mode, the hardware frame is much longer still, but we neither -* access the extra members from NMI context, nor do we write such a -* frame at sp0 at all. +* In vm86 mode, the hardware frame is much longer still, so add 16 +* bytes to make room for the real-mode segments. * * x86_64 has a fixed-length stack frame. */ #ifdef CONFIG_X86_32 -# define TOP_OF_KERNEL_STACK_PADDING 8 +# ifdef CONFIG_VM86 +# define TOP_OF_KERNEL_STACK_PADDING 16 +# else +# define TOP_OF_KERNEL_STACK_PADDING 8 +# endif #else # define TOP_OF_KERNEL_STACK_PADDING 0#endif
arch/x86/include/asm/vm86.h +4 -2
@@ -30,7 +30,7 @@ struct kernel_vm86_regs {struct kernel_vm86_info {struct vm86_struct __user *vm86_info; -struct pt_regs *regs32; +struct pt_regs regs32;unsigned long v86flags;unsigned long v86mask;unsigned long saved_sp0;
@@ -50,7 +50,7 @@ struct kernel_vm86_info {void handle_vm86_fault (struct kernel_vm86_regs *,long );int handle_vm86_trap (struct kernel_vm86_regs *,long ,int ); -struct pt_regs *save_v86_state (struct kernel_vm86_regs *); +void save_v86_state (struct kernel_vm86_regs *,int );struct task_struct ;void release_vm86_irqs (struct task_struct *);
@@ -65,6 +65,8 @@ static inline int handle_vm86_trap(struct kernel_vm86_regs *a, long b, int c)return 0 ; } +static inline void save_v86_state (struct kernel_vm86_regs *,int ) { } +#endif /* CONFIG_VM86 */ #endif /* _ASM_X86_VM86_H */
arch/x86/kernel/signal.c +3 -0
@@ -635,6 +635,9 @@ handle_signal(struct ksignal *ksig, struct pt_regs *regs)bool stepping, failed;struct fpu *fpu = ¤t->thread.fpu; +if (v8086_mode (regs)) +save_v86_state ((struct kernel_vm86_regs *) regs,VM86_SIGNAL ); +/* Are we from a system call? */ if (syscall_get_nr (current, regs) >=0 ) {/* If so, check system call restarting.. */
arch/x86/kernel/vm86_32.c +54 -75
268 lines not on this page · raw message
Signature · 1 line
2.4.3