From 19c9bba94152148523ba0f7ef7cffe3d45656b11 Mon Sep 17 00:00:00 2001 From: Ralf Baechle Date: Tue, 29 Apr 1997 21:13:14 +0000 Subject: Import of Linux/MIPS 2.1.36 --- arch/i386/kernel/vm86.c | 361 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 298 insertions(+), 63 deletions(-) (limited to 'arch/i386/kernel/vm86.c') diff --git a/arch/i386/kernel/vm86.c b/arch/i386/kernel/vm86.c index 6cceba36c..bfba24327 100644 --- a/arch/i386/kernel/vm86.c +++ b/arch/i386/kernel/vm86.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include @@ -30,6 +32,11 @@ * Hopefully these problems do not actually matter for anything. */ + +#define KVM86 ((struct kernel_vm86_struct *)regs) +#define VMPI KVM86->vm86plus + + /* * 8- and 16-bit register defines.. */ @@ -50,25 +57,35 @@ #define SAFE_MASK (0xDD5) #define RETURN_MASK (0xDFF) -asmlinkage struct pt_regs * save_v86_state(struct vm86_regs * regs) +#define VM86_REGS_PART2 orig_eax +#define VM86_REGS_SIZE1 \ + ( (unsigned)( & (((struct kernel_vm86_regs *)0)->VM86_REGS_PART2) ) ) +#define VM86_REGS_SIZE2 (sizeof(struct kernel_vm86_regs) - VM86_REGS_SIZE1) + +asmlinkage struct pt_regs * save_v86_state(struct kernel_vm86_regs * regs) { + struct pt_regs *ret; unsigned long tmp; + lock_kernel(); if (!current->tss.vm86_info) { printk("no vm86_info: BAD\n"); do_exit(SIGSEGV); } set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->tss.v86mask); - tmp = copy_to_user(¤t->tss.vm86_info->regs,regs,sizeof(*regs)); + tmp = copy_to_user(¤t->tss.vm86_info->regs,regs, VM86_REGS_SIZE1); + tmp += copy_to_user(¤t->tss.vm86_info->regs.VM86_REGS_PART2, + ®s->VM86_REGS_PART2, VM86_REGS_SIZE2); tmp += put_user(current->tss.screen_bitmap,¤t->tss.vm86_info->screen_bitmap); if (tmp) { printk("vm86: could not access userspace vm86_info\n"); do_exit(SIGSEGV); } - tmp = current->tss.esp0; current->tss.esp0 = current->saved_kernel_stack; current->saved_kernel_stack = 0; - return (struct pt_regs *) tmp; + ret = KVM86->regs32; + unlock_kernel(); + return ret; } static void mark_screen_rdonly(struct task_struct * tsk) @@ -103,37 +120,115 @@ static void mark_screen_rdonly(struct task_struct * tsk) flush_tlb(); } -asmlinkage int sys_vm86(struct vm86_struct * v86) + + +static do_vm86_irq_handling(int subfunction, int irqnumber); +static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk); + +asmlinkage int sys_vm86old(struct vm86_struct * v86) { - struct vm86_struct info; - struct task_struct *tsk = current; - struct pt_regs * pt_regs = (struct pt_regs *) &v86; + struct kernel_vm86_struct info; /* declare this _on top_, + * this avoids wasting of stack space. + * This remains on the stack until we + * return to 32 bit user space. + */ + struct task_struct *tsk; + int tmp, ret = -EPERM; + + lock_kernel(); + tsk = current; + if (tsk->saved_kernel_stack) + goto out; + tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1); + tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2, + (long)&info.vm86plus - (long)&info.regs.VM86_REGS_PART2); + ret = -EFAULT; + if (tmp) + goto out; + memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus); + info.regs32 = (struct pt_regs *) &v86; + tsk->tss.vm86_info = v86; + do_sys_vm86(&info, tsk); + ret = 0; /* we never return here */ +out: + unlock_kernel(); + return ret; +} + +asmlinkage int sys_vm86(unsigned long subfunction, struct vm86plus_struct * v86) +{ + struct kernel_vm86_struct info; /* declare this _on top_, + * this avoids wasting of stack space. + * This remains on the stack until we + * return to 32 bit user space. + */ + struct task_struct *tsk; + int tmp, ret; + + lock_kernel(); + tsk = current; + switch (subfunction) { + case VM86_REQUEST_IRQ: + case VM86_FREE_IRQ: + case VM86_GET_IRQ_BITS: + case VM86_GET_AND_RESET_IRQ: + ret = do_vm86_irq_handling(subfunction,(int)v86); + goto out; + case VM86_PLUS_INSTALL_CHECK: + /* NOTE: on old vm86 stuff this will return the error + from verify_area(), because the subfunction is + interpreted as (invalid) address to vm86_struct. + So the installation check works. + */ + ret = 0; + goto out; + } + + /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */ + ret = -EPERM; if (tsk->saved_kernel_stack) - return -EPERM; - if (copy_from_user(&info,v86,sizeof(info))) - return -EFAULT; + goto out; + tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1); + tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2, + (long)&info.regs32 - (long)&info.regs.VM86_REGS_PART2); + ret = -EFAULT; + if (tmp) + goto out; + info.regs32 = (struct pt_regs *) &subfunction; + info.vm86plus.is_vm86pus = 1; + tsk->tss.vm86_info = (struct vm86_struct *)v86; + do_sys_vm86(&info, tsk); + ret = 0; /* we never return here */ +out: + unlock_kernel(); + return ret; +} + + +static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk) +{ /* * make sure the vm86() system call doesn't try to do anything silly */ - info.regs.__null_ds = 0; - info.regs.__null_es = 0; + info->regs.__null_ds = 0; + info->regs.__null_es = 0; /* we are clearing fs,gs later just before "jmp ret_from_sys_call", * because starting with Linux 2.1.x they aren't no longer saved/restored */ - + /* * The eflags register is also special: we cannot trust that the user * has set it up safely, so this makes sure interrupt etc flags are * inherited from protected mode. */ - VEFLAGS = info.regs.eflags; - info.regs.eflags &= SAFE_MASK; - info.regs.eflags |= pt_regs->eflags & ~SAFE_MASK; - info.regs.eflags |= VM_MASK; + VEFLAGS = info->regs.eflags; + info->regs.eflags &= SAFE_MASK; + info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK; + info->regs.eflags |= VM_MASK; - switch (info.cpu_type) { + switch (info->cpu_type) { case CPU_286: tsk->tss.v86mask = 0; break; @@ -151,52 +246,53 @@ asmlinkage int sys_vm86(struct vm86_struct * v86) /* * Save old state, set default return value (%eax) to 0 */ - pt_regs->eax = 0; + info->regs32->eax = 0; tsk->saved_kernel_stack = tsk->tss.esp0; - tsk->tss.esp0 = (unsigned long) pt_regs; - tsk->tss.vm86_info = v86; + tsk->tss.esp0 = (unsigned long) &info->VM86_TSS_ESP0; - tsk->tss.screen_bitmap = info.screen_bitmap; - if (info.flags & VM86_SCREEN_BITMAP) + tsk->tss.screen_bitmap = info->screen_bitmap; + if (info->flags & VM86_SCREEN_BITMAP) mark_screen_rdonly(tsk); + unlock_kernel(); __asm__ __volatile__( "xorl %%eax,%%eax; mov %%ax,%%fs; mov %%ax,%%gs\n\t" "movl %0,%%esp\n\t" "jmp ret_from_sys_call" : /* no outputs */ - :"r" (&info.regs), "b" (tsk) : "ax"); - return 0; + :"r" (&info->regs), "b" (tsk) : "ax"); + /* we never return here */ } -static inline void return_to_32bit(struct vm86_regs * regs16, int retval) +static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval) { struct pt_regs * regs32; regs32 = save_v86_state(regs16); regs32->eax = retval; + unlock_kernel(); __asm__ __volatile__("movl %0,%%esp\n\t" "jmp ret_from_sys_call" : : "r" (regs32), "b" (current)); } -static inline void set_IF(struct vm86_regs * regs) +static inline void set_IF(struct kernel_vm86_regs * regs) { VEFLAGS |= VIF_MASK; if (VEFLAGS & VIP_MASK) return_to_32bit(regs, VM86_STI); } -static inline void clear_IF(struct vm86_regs * regs) +static inline void clear_IF(struct kernel_vm86_regs * regs) { VEFLAGS &= ~VIF_MASK; } -static inline void clear_TF(struct vm86_regs * regs) +static inline void clear_TF(struct kernel_vm86_regs * regs) { regs->eflags &= ~TF_MASK; } -static inline void set_vflags_long(unsigned long eflags, struct vm86_regs * regs) +static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs) { set_flags(VEFLAGS, eflags, current->tss.v86mask); set_flags(regs->eflags, eflags, SAFE_MASK); @@ -204,7 +300,7 @@ static inline void set_vflags_long(unsigned long eflags, struct vm86_regs * regs set_IF(regs); } -static inline void set_vflags_short(unsigned short flags, struct vm86_regs * regs) +static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs) { set_flags(VFLAGS, flags, current->tss.v86mask); set_flags(regs->eflags, flags, SAFE_MASK); @@ -212,7 +308,7 @@ static inline void set_vflags_short(unsigned short flags, struct vm86_regs * reg set_IF(regs); } -static inline unsigned long get_vflags(struct vm86_regs * regs) +static inline unsigned long get_vflags(struct kernel_vm86_regs * regs) { unsigned long flags = regs->eflags & RETURN_MASK; @@ -223,10 +319,10 @@ static inline unsigned long get_vflags(struct vm86_regs * regs) static inline int is_revectored(int nr, struct revectored_struct * bitmap) { - unsigned long map; - if (get_user(map, bitmap->__map + (nr >> 5))) - return 1; - return test_bit(nr & ((1 << 5)-1), &map); + __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0" + :"=r" (nr) + :"m" (*bitmap),"r" (nr)); + return nr; } /* @@ -302,15 +398,15 @@ __asm__ __volatile__( \ : "0" (ptr), "1" (base)); \ __res; }) -static void do_int(struct vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp) +static void do_int(struct kernel_vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp) { unsigned long *intr_ptr, segoffs; - + if (regs->cs == BIOSSEG) goto cannot_handle; - if (is_revectored(i, ¤t->tss.vm86_info->int_revectored)) + if (is_revectored(i, &KVM86->int_revectored)) goto cannot_handle; - if (i==0x21 && is_revectored(AH(regs),¤t->tss.vm86_info->int21_revectored)) + if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored)) goto cannot_handle; intr_ptr = (unsigned long *) (i << 2); if (get_user(segoffs, intr_ptr)) @@ -331,24 +427,39 @@ cannot_handle: return_to_32bit(regs, VM86_INTx + (i << 8)); } -void handle_vm86_debug(struct vm86_regs * regs, long error_code) +/* This must be called with the kernel lock held. */ +int handle_vm86_trap(struct kernel_vm86_regs * regs, long error_code, int trapno) { -#if 0 - do_int(regs, 1, (unsigned char *) (regs->ss << 4), SP(regs)); -#else + if (VMPI.is_vm86pus) { + if ( (trapno==3) || (trapno==1) ) + return_to_32bit(regs, VM86_TRAP + (trapno << 8)); + do_int(regs, trapno, (unsigned char *) (regs->ss << 4), SP(regs)); + return 0; + } + if (trapno !=1) + return 1; /* we let this handle by the calling routine */ if (current->flags & PF_PTRACED) current->blocked &= ~(1 << (SIGTRAP-1)); send_sig(SIGTRAP, current, 1); - current->tss.trap_no = 1; + current->tss.trap_no = trapno; current->tss.error_code = error_code; -#endif + return 0; } -void handle_vm86_fault(struct vm86_regs * regs, long error_code) +/* This must be called with the kernel lock held. */ +void handle_vm86_fault(struct kernel_vm86_regs * regs, long error_code) { unsigned char *csp, *ssp; unsigned long ip, sp; +#define CHECK_IF_IN_TRAP \ + if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \ + pushw(ssp,sp,popw(ssp,sp) | TF_MASK); +#define VM86_FAULT_RETURN \ + if (VMPI.force_return_for_pic && (VEFLAGS & IF_MASK)) \ + return_to_32bit(regs, VM86_PICRETURN); \ + return; + csp = (unsigned char *) (regs->cs << 4); ssp = (unsigned char *) (regs->ss << 4); sp = SP(regs); @@ -365,58 +476,70 @@ void handle_vm86_fault(struct vm86_regs * regs, long error_code) SP(regs) -= 4; IP(regs) += 2; pushl(ssp, sp, get_vflags(regs)); - return; + VM86_FAULT_RETURN; /* popfd */ case 0x9d: SP(regs) += 4; IP(regs) += 2; + CHECK_IF_IN_TRAP set_vflags_long(popl(ssp, sp), regs); - return; + VM86_FAULT_RETURN; /* iretd */ case 0xcf: SP(regs) += 12; IP(regs) = (unsigned short)popl(ssp, sp); regs->cs = (unsigned short)popl(ssp, sp); + CHECK_IF_IN_TRAP set_vflags_long(popl(ssp, sp), regs); - return; + VM86_FAULT_RETURN; + /* need this to avoid a fallthrough */ + default: + return_to_32bit(regs, VM86_UNKNOWN); } - break; /* pushf */ case 0x9c: SP(regs) -= 2; IP(regs)++; pushw(ssp, sp, get_vflags(regs)); - return; + VM86_FAULT_RETURN; /* popf */ case 0x9d: SP(regs) += 2; IP(regs)++; + CHECK_IF_IN_TRAP set_vflags_short(popw(ssp, sp), regs); - return; + VM86_FAULT_RETURN; /* int xx */ - case 0xcd: + case 0xcd: { + int intno=popb(csp, ip); IP(regs) += 2; - do_int(regs, popb(csp, ip), ssp, sp); + 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); return; + } /* iret */ case 0xcf: SP(regs) += 6; IP(regs) = popw(ssp, sp); regs->cs = popw(ssp, sp); + CHECK_IF_IN_TRAP set_vflags_short(popw(ssp, sp), regs); - return; + VM86_FAULT_RETURN; /* cli */ case 0xfa: IP(regs)++; clear_IF(regs); - return; + VM86_FAULT_RETURN; /* sti */ /* @@ -428,11 +551,123 @@ void handle_vm86_fault(struct vm86_regs * regs, long error_code) case 0xfb: IP(regs)++; set_IF(regs); - return; + VM86_FAULT_RETURN; + + default: + return_to_32bit(regs, VM86_UNKNOWN); } +} - /* - * We didn't recognize it, let the emulator take care of it.. - */ - return_to_32bit(regs, VM86_UNKNOWN); +/* ---------------- vm86 special IRQ passing stuff ----------------- */ + +#define VM86_IRQNAME "vm86irq" + +static struct vm86_irqs { + struct task_struct *tsk; + int sig; +} vm86_irqs[16] = {{0},}; +static int irqbits=0; + +#define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \ + | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \ + | (1 << SIGUNUSED) ) + +static void irq_handler(int intno, void *dev_id, struct pt_regs * regs) { + int irq_bit; + unsigned long flags; + + lock_kernel(); + save_flags(flags); + cli(); + irq_bit = 1 << intno; + if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk) + goto out; + irqbits |= irq_bit; + if (vm86_irqs[intno].sig) + send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1); + /* else user will poll for IRQs */ +out: + restore_flags(flags); + unlock_kernel(); +} + +static inline void free_vm86_irq(int irqnumber) +{ + free_irq(irqnumber,0); + vm86_irqs[irqnumber].tsk = 0; + irqbits &= ~(1 << irqnumber); +} + +static inline int task_valid(struct task_struct *tsk) +{ + struct task_struct *p; + + for_each_task(p) { + if ((p == tsk) && (p->sig)) return 1; + } + return 0; +} + +static inline void handle_irq_zombies(void) +{ + int i; + for (i=3; i<16; i++) { + if (vm86_irqs[i].tsk) { + if (task_valid(vm86_irqs[i].tsk)) continue; + free_vm86_irq(i); + } + } +} + +static inline int get_and_reset_irq(int irqnumber) +{ + int bit; + unsigned long flags; + + if ( (irqnumber<3) || (irqnumber>15) ) return 0; + if (vm86_irqs[irqnumber].tsk != current) return 0; + save_flags(flags); + cli(); + bit = irqbits & (1 << irqnumber); + irqbits &= ~bit; + restore_flags(flags); + return bit; +} + + +static int do_vm86_irq_handling(int subfunction, int irqnumber) +{ + int ret; + switch (subfunction) { + case VM86_GET_AND_RESET_IRQ: { + return get_and_reset_irq(irqnumber); + } + case VM86_GET_IRQ_BITS: { + return irqbits; + } + case VM86_REQUEST_IRQ: { + int sig = irqnumber >> 8; + int irq = irqnumber & 255; + handle_irq_zombies(); + if (!suser()) return -EPERM; + if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM; + if ( (irq<3) || (irq>15) ) return -EPERM; + if (vm86_irqs[irq].tsk) return -EPERM; + ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, 0); + if (ret) return ret; + vm86_irqs[irq].sig = sig; + vm86_irqs[irq].tsk = current; + return irq; + } + case VM86_FREE_IRQ: { + handle_irq_zombies(); + if ( (irqnumber<3) || (irqnumber>15) ) return -EPERM; + if (!vm86_irqs[irqnumber].tsk) return 0; + if (vm86_irqs[irqnumber].tsk != current) return -EPERM; + free_vm86_irq(irqnumber); + return 0; + } + } + return -EINVAL; } + -- cgit v1.2.3