/*
|
/*
|
* linux/kernel/vm86.c
|
* linux/kernel/vm86.c
|
*
|
*
|
* Copyright (C) 1994 Linus Torvalds
|
* Copyright (C) 1994 Linus Torvalds
|
*/
|
*/
|
#include <linux/errno.h>
|
#include <linux/errno.h>
|
#include <linux/sched.h>
|
#include <linux/sched.h>
|
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
#include <linux/signal.h>
|
#include <linux/signal.h>
|
#include <linux/string.h>
|
#include <linux/string.h>
|
#include <linux/ptrace.h>
|
#include <linux/ptrace.h>
|
#include <linux/mm.h>
|
#include <linux/mm.h>
|
|
|
#include <asm/segment.h>
|
#include <asm/segment.h>
|
#include <asm/pgtable.h>
|
#include <asm/pgtable.h>
|
#include <asm/io.h>
|
#include <asm/io.h>
|
|
|
/*
|
/*
|
* Known problems:
|
* Known problems:
|
*
|
*
|
* Interrupt handling is not guaranteed:
|
* Interrupt handling is not guaranteed:
|
* - a real x86 will disable all interrupts for one instruction
|
* - a real x86 will disable all interrupts for one instruction
|
* after a "mov ss,xx" to make stack handling atomic even without
|
* after a "mov ss,xx" to make stack handling atomic even without
|
* the 'lss' instruction. We can't guarantee this in v86 mode,
|
* the 'lss' instruction. We can't guarantee this in v86 mode,
|
* as the next instruction might result in a page fault or similar.
|
* as the next instruction might result in a page fault or similar.
|
* - a real x86 will have interrupts disabled for one instruction
|
* - a real x86 will have interrupts disabled for one instruction
|
* past the 'sti' that enables them. We don't bother with all the
|
* past the 'sti' that enables them. We don't bother with all the
|
* details yet..
|
* details yet..
|
*
|
*
|
* Hopefully these problems do not actually matter for anything.
|
* Hopefully these problems do not actually matter for anything.
|
*/
|
*/
|
|
|
|
|
#define KVM86 ((struct kernel_vm86_struct *)regs)
|
#define KVM86 ((struct kernel_vm86_struct *)regs)
|
#define VMPI KVM86->vm86plus
|
#define VMPI KVM86->vm86plus
|
|
|
|
|
/*
|
/*
|
* 8- and 16-bit register defines..
|
* 8- and 16-bit register defines..
|
*/
|
*/
|
#define AL(regs) (((unsigned char *)&((regs)->eax))[0])
|
#define AL(regs) (((unsigned char *)&((regs)->eax))[0])
|
#define AH(regs) (((unsigned char *)&((regs)->eax))[1])
|
#define AH(regs) (((unsigned char *)&((regs)->eax))[1])
|
#define IP(regs) (*(unsigned short *)&((regs)->eip))
|
#define IP(regs) (*(unsigned short *)&((regs)->eip))
|
#define SP(regs) (*(unsigned short *)&((regs)->esp))
|
#define SP(regs) (*(unsigned short *)&((regs)->esp))
|
|
|
/*
|
/*
|
* virtual flags (16 and 32-bit versions)
|
* virtual flags (16 and 32-bit versions)
|
*/
|
*/
|
#define VFLAGS (*(unsigned short *)&(current->tss.v86flags))
|
#define VFLAGS (*(unsigned short *)&(current->tss.v86flags))
|
#define VEFLAGS (current->tss.v86flags)
|
#define VEFLAGS (current->tss.v86flags)
|
|
|
#define set_flags(X,new,mask) \
|
#define set_flags(X,new,mask) \
|
((X) = ((X) & ~(mask)) | ((new) & (mask)))
|
((X) = ((X) & ~(mask)) | ((new) & (mask)))
|
|
|
#define SAFE_MASK (0xDD5)
|
#define SAFE_MASK (0xDD5)
|
#define RETURN_MASK (0xDFF)
|
#define RETURN_MASK (0xDFF)
|
|
|
|
|
asmlinkage struct pt_regs * save_v86_state(struct vm86_regs * regs)
|
asmlinkage struct pt_regs * save_v86_state(struct vm86_regs * regs)
|
{
|
{
|
if (!current->tss.vm86_info) {
|
if (!current->tss.vm86_info) {
|
printk("no vm86_info: BAD\n");
|
printk("no vm86_info: BAD\n");
|
do_exit(SIGSEGV);
|
do_exit(SIGSEGV);
|
}
|
}
|
set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->tss.v86mask);
|
set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->tss.v86mask);
|
memcpy_tofs(¤t->tss.vm86_info->regs,regs,sizeof(*regs));
|
memcpy_tofs(¤t->tss.vm86_info->regs,regs,sizeof(*regs));
|
put_fs_long(current->tss.screen_bitmap,¤t->tss.vm86_info->screen_bitmap);
|
put_fs_long(current->tss.screen_bitmap,¤t->tss.vm86_info->screen_bitmap);
|
current->tss.esp0 = current->saved_kernel_stack;
|
current->tss.esp0 = current->saved_kernel_stack;
|
current->saved_kernel_stack = 0;
|
current->saved_kernel_stack = 0;
|
return KVM86->regs32;
|
return KVM86->regs32;
|
}
|
}
|
|
|
static void mark_screen_rdonly(struct task_struct * tsk)
|
static void mark_screen_rdonly(struct task_struct * tsk)
|
{
|
{
|
pgd_t *pgd;
|
pgd_t *pgd;
|
pmd_t *pmd;
|
pmd_t *pmd;
|
pte_t *pte;
|
pte_t *pte;
|
int i;
|
int i;
|
|
|
pgd = pgd_offset(tsk->mm, 0xA0000);
|
pgd = pgd_offset(tsk->mm, 0xA0000);
|
if (pgd_none(*pgd))
|
if (pgd_none(*pgd))
|
return;
|
return;
|
if (pgd_bad(*pgd)) {
|
if (pgd_bad(*pgd)) {
|
printk("vm86: bad pgd entry [%p]:%08lx\n", pgd, pgd_val(*pgd));
|
printk("vm86: bad pgd entry [%p]:%08lx\n", pgd, pgd_val(*pgd));
|
pgd_clear(pgd);
|
pgd_clear(pgd);
|
return;
|
return;
|
}
|
}
|
pmd = pmd_offset(pgd, 0xA0000);
|
pmd = pmd_offset(pgd, 0xA0000);
|
if (pmd_none(*pmd))
|
if (pmd_none(*pmd))
|
return;
|
return;
|
if (pmd_bad(*pmd)) {
|
if (pmd_bad(*pmd)) {
|
printk("vm86: bad pmd entry [%p]:%08lx\n", pmd, pmd_val(*pmd));
|
printk("vm86: bad pmd entry [%p]:%08lx\n", pmd, pmd_val(*pmd));
|
pmd_clear(pmd);
|
pmd_clear(pmd);
|
return;
|
return;
|
}
|
}
|
pte = pte_offset(pmd, 0xA0000);
|
pte = pte_offset(pmd, 0xA0000);
|
for (i = 0; i < 32; i++) {
|
for (i = 0; i < 32; i++) {
|
if (pte_present(*pte))
|
if (pte_present(*pte))
|
set_pte(pte, pte_wrprotect(*pte));
|
set_pte(pte, pte_wrprotect(*pte));
|
pte++;
|
pte++;
|
}
|
}
|
flush_tlb();
|
flush_tlb();
|
}
|
}
|
|
|
|
|
|
|
static int do_vm86_irq_handling(int subfunction, int irqnumber);
|
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 void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
|
|
|
asmlinkage int sys_vm86old(struct vm86_struct * v86)
|
asmlinkage int sys_vm86old(struct vm86_struct * v86)
|
{
|
{
|
struct kernel_vm86_struct info; /* declare this _on top_,
|
struct kernel_vm86_struct info; /* declare this _on top_,
|
* this avoids wasting of stack space.
|
* this avoids wasting of stack space.
|
* This remains on the stack until we
|
* This remains on the stack until we
|
* return to 32 bit user space.
|
* return to 32 bit user space.
|
*/
|
*/
|
struct task_struct *tsk = current;
|
struct task_struct *tsk = current;
|
int error;
|
int error;
|
|
|
if (tsk->saved_kernel_stack)
|
if (tsk->saved_kernel_stack)
|
return -EPERM;
|
return -EPERM;
|
/* v86 must be readable (now) and writable (for save_v86_state) */
|
/* v86 must be readable (now) and writable (for save_v86_state) */
|
error = verify_area(VERIFY_WRITE,v86,sizeof(*v86));
|
error = verify_area(VERIFY_WRITE,v86,sizeof(*v86));
|
if (error)
|
if (error)
|
return error;
|
return error;
|
memcpy_fromfs(&info,v86,sizeof(struct vm86_struct));
|
memcpy_fromfs(&info,v86,sizeof(struct vm86_struct));
|
memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
|
memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
|
info.regs32 = (struct pt_regs *) &v86;
|
info.regs32 = (struct pt_regs *) &v86;
|
tsk->tss.vm86_info = v86;
|
tsk->tss.vm86_info = v86;
|
do_sys_vm86(&info, tsk);
|
do_sys_vm86(&info, tsk);
|
return 0; /* we never return here */
|
return 0; /* we never return here */
|
}
|
}
|
|
|
|
|
asmlinkage int sys_vm86(unsigned long subfunction, struct vm86plus_struct * v86)
|
asmlinkage int sys_vm86(unsigned long subfunction, struct vm86plus_struct * v86)
|
{
|
{
|
struct kernel_vm86_struct info; /* declare this _on top_,
|
struct kernel_vm86_struct info; /* declare this _on top_,
|
* this avoids wasting of stack space.
|
* this avoids wasting of stack space.
|
* This remains on the stack until we
|
* This remains on the stack until we
|
* return to 32 bit user space.
|
* return to 32 bit user space.
|
*/
|
*/
|
struct task_struct *tsk = current;
|
struct task_struct *tsk = current;
|
int error;
|
int error;
|
|
|
switch (subfunction) {
|
switch (subfunction) {
|
case VM86_REQUEST_IRQ:
|
case VM86_REQUEST_IRQ:
|
case VM86_FREE_IRQ:
|
case VM86_FREE_IRQ:
|
case VM86_GET_IRQ_BITS:
|
case VM86_GET_IRQ_BITS:
|
case VM86_GET_AND_RESET_IRQ:
|
case VM86_GET_AND_RESET_IRQ:
|
return do_vm86_irq_handling(subfunction,(int)v86);
|
return do_vm86_irq_handling(subfunction,(int)v86);
|
case VM86_PLUS_INSTALL_CHECK:
|
case VM86_PLUS_INSTALL_CHECK:
|
/* NOTE: on old vm86 stuff this will return the error
|
/* NOTE: on old vm86 stuff this will return the error
|
from verify_area(), because the subfunction is
|
from verify_area(), because the subfunction is
|
interpreted as (invalid) address to vm86_struct.
|
interpreted as (invalid) address to vm86_struct.
|
So the installation check works.
|
So the installation check works.
|
*/
|
*/
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
|
/* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
|
if (tsk->saved_kernel_stack)
|
if (tsk->saved_kernel_stack)
|
return -EPERM;
|
return -EPERM;
|
/* v86 must be readable (now) and writable (for save_v86_state) */
|
/* v86 must be readable (now) and writable (for save_v86_state) */
|
error = verify_area(VERIFY_WRITE,v86,sizeof(struct vm86plus_struct));
|
error = verify_area(VERIFY_WRITE,v86,sizeof(struct vm86plus_struct));
|
if (error)
|
if (error)
|
return error;
|
return error;
|
memcpy_fromfs(&info,v86,sizeof(struct vm86plus_struct));
|
memcpy_fromfs(&info,v86,sizeof(struct vm86plus_struct));
|
info.regs32 = (struct pt_regs *) &subfunction;
|
info.regs32 = (struct pt_regs *) &subfunction;
|
info.vm86plus.is_vm86pus = 1;
|
info.vm86plus.is_vm86pus = 1;
|
tsk->tss.vm86_info = (struct vm86_struct *)v86;
|
tsk->tss.vm86_info = (struct vm86_struct *)v86;
|
do_sys_vm86(&info, tsk);
|
do_sys_vm86(&info, tsk);
|
return 0; /* we never return here */
|
return 0; /* we never return here */
|
}
|
}
|
|
|
|
|
static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
|
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
|
* make sure the vm86() system call doesn't try to do anything silly
|
*/
|
*/
|
info->regs.__null_ds = 0;
|
info->regs.__null_ds = 0;
|
info->regs.__null_es = 0;
|
info->regs.__null_es = 0;
|
info->regs.__null_fs = 0;
|
info->regs.__null_fs = 0;
|
info->regs.__null_gs = 0;
|
info->regs.__null_gs = 0;
|
/*
|
/*
|
* The eflags register is also special: we cannot trust that the user
|
* 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
|
* has set it up safely, so this makes sure interrupt etc flags are
|
* inherited from protected mode.
|
* inherited from protected mode.
|
*/
|
*/
|
VEFLAGS = info->regs.eflags;
|
VEFLAGS = info->regs.eflags;
|
info->regs.eflags &= SAFE_MASK;
|
info->regs.eflags &= SAFE_MASK;
|
info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
|
info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
|
info->regs.eflags |= VM_MASK;
|
info->regs.eflags |= VM_MASK;
|
|
|
switch (info->cpu_type) {
|
switch (info->cpu_type) {
|
case CPU_286:
|
case CPU_286:
|
tsk->tss.v86mask = 0;
|
tsk->tss.v86mask = 0;
|
break;
|
break;
|
case CPU_386:
|
case CPU_386:
|
tsk->tss.v86mask = NT_MASK | IOPL_MASK;
|
tsk->tss.v86mask = NT_MASK | IOPL_MASK;
|
break;
|
break;
|
case CPU_486:
|
case CPU_486:
|
tsk->tss.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
|
tsk->tss.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
|
break;
|
break;
|
default:
|
default:
|
tsk->tss.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
|
tsk->tss.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
|
break;
|
break;
|
}
|
}
|
|
|
/*
|
/*
|
* Save old state, set default return value (%eax) to 0
|
* Save old state, set default return value (%eax) to 0
|
*/
|
*/
|
info->regs32->eax = 0;
|
info->regs32->eax = 0;
|
tsk->saved_kernel_stack = tsk->tss.esp0;
|
tsk->saved_kernel_stack = tsk->tss.esp0;
|
tsk->tss.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
|
tsk->tss.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
|
|
|
tsk->tss.screen_bitmap = info->screen_bitmap;
|
tsk->tss.screen_bitmap = info->screen_bitmap;
|
if (info->flags & VM86_SCREEN_BITMAP)
|
if (info->flags & VM86_SCREEN_BITMAP)
|
mark_screen_rdonly(tsk);
|
mark_screen_rdonly(tsk);
|
__asm__ __volatile__(
|
__asm__ __volatile__(
|
"movl %0,%%esp\n\t"
|
"movl %0,%%esp\n\t"
|
"jmp ret_from_sys_call"
|
"jmp ret_from_sys_call"
|
: /* no outputs */
|
: /* no outputs */
|
:"r" (&info->regs));
|
:"r" (&info->regs));
|
/* we never return here */
|
/* we never return here */
|
}
|
}
|
|
|
static inline void return_to_32bit(struct vm86_regs * regs16, int retval)
|
static inline void return_to_32bit(struct vm86_regs * regs16, int retval)
|
{
|
{
|
struct pt_regs * regs32;
|
struct pt_regs * regs32;
|
|
|
regs32 = save_v86_state(regs16);
|
regs32 = save_v86_state(regs16);
|
regs32->eax = retval;
|
regs32->eax = retval;
|
__asm__ __volatile__("movl %0,%%esp\n\t"
|
__asm__ __volatile__("movl %0,%%esp\n\t"
|
"jmp ret_from_sys_call"
|
"jmp ret_from_sys_call"
|
: : "r" (regs32));
|
: : "r" (regs32));
|
}
|
}
|
|
|
static inline void set_IF(struct vm86_regs * regs)
|
static inline void set_IF(struct vm86_regs * regs)
|
{
|
{
|
VEFLAGS |= VIF_MASK;
|
VEFLAGS |= VIF_MASK;
|
if (VEFLAGS & VIP_MASK)
|
if (VEFLAGS & VIP_MASK)
|
return_to_32bit(regs, VM86_STI);
|
return_to_32bit(regs, VM86_STI);
|
}
|
}
|
|
|
static inline void clear_IF(struct vm86_regs * regs)
|
static inline void clear_IF(struct vm86_regs * regs)
|
{
|
{
|
VEFLAGS &= ~VIF_MASK;
|
VEFLAGS &= ~VIF_MASK;
|
}
|
}
|
|
|
static inline void clear_TF(struct vm86_regs * regs)
|
static inline void clear_TF(struct vm86_regs * regs)
|
{
|
{
|
regs->eflags &= ~TF_MASK;
|
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 vm86_regs * regs)
|
{
|
{
|
set_flags(VEFLAGS, eflags, current->tss.v86mask);
|
set_flags(VEFLAGS, eflags, current->tss.v86mask);
|
set_flags(regs->eflags, eflags, SAFE_MASK);
|
set_flags(regs->eflags, eflags, SAFE_MASK);
|
if (eflags & IF_MASK)
|
if (eflags & IF_MASK)
|
set_IF(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 vm86_regs * regs)
|
{
|
{
|
set_flags(VFLAGS, flags, current->tss.v86mask);
|
set_flags(VFLAGS, flags, current->tss.v86mask);
|
set_flags(regs->eflags, flags, SAFE_MASK);
|
set_flags(regs->eflags, flags, SAFE_MASK);
|
if (flags & IF_MASK)
|
if (flags & IF_MASK)
|
set_IF(regs);
|
set_IF(regs);
|
}
|
}
|
|
|
static inline unsigned long get_vflags(struct vm86_regs * regs)
|
static inline unsigned long get_vflags(struct vm86_regs * regs)
|
{
|
{
|
unsigned long flags = regs->eflags & RETURN_MASK;
|
unsigned long flags = regs->eflags & RETURN_MASK;
|
|
|
if (VEFLAGS & VIF_MASK)
|
if (VEFLAGS & VIF_MASK)
|
flags |= IF_MASK;
|
flags |= IF_MASK;
|
return flags | (VEFLAGS & current->tss.v86mask);
|
return flags | (VEFLAGS & current->tss.v86mask);
|
}
|
}
|
|
|
static inline int is_revectored(int nr, struct revectored_struct * bitmap)
|
static inline int is_revectored(int nr, struct revectored_struct * bitmap)
|
{
|
{
|
__asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
|
__asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
|
:"=r" (nr)
|
:"=r" (nr)
|
:"m" (*bitmap),"r" (nr));
|
:"m" (*bitmap),"r" (nr));
|
return nr;
|
return nr;
|
}
|
}
|
|
|
/*
|
/*
|
* Boy are these ugly, but we need to do the correct 16-bit arithmetic.
|
* Boy are these ugly, but we need to do the correct 16-bit arithmetic.
|
* Gcc makes a mess of it, so we do it inline and use non-obvious calling
|
* Gcc makes a mess of it, so we do it inline and use non-obvious calling
|
* conventions..
|
* conventions..
|
*/
|
*/
|
#define pushb(base, ptr, val) \
|
#define pushb(base, ptr, val) \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"movb %2,%%fs:0(%1,%0)" \
|
"movb %2,%%fs:0(%1,%0)" \
|
: "=r" (ptr) \
|
: "=r" (ptr) \
|
: "r" (base), "q" (val), "0" (ptr))
|
: "r" (base), "q" (val), "0" (ptr))
|
|
|
#define pushw(base, ptr, val) \
|
#define pushw(base, ptr, val) \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"movb %b2,%%fs:0(%1,%0)" \
|
"movb %b2,%%fs:0(%1,%0)" \
|
: "=r" (ptr) \
|
: "=r" (ptr) \
|
: "r" (base), "q" (val), "0" (ptr))
|
: "r" (base), "q" (val), "0" (ptr))
|
|
|
#define pushl(base, ptr, val) \
|
#define pushl(base, ptr, val) \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"rorl $16,%2\n\t" \
|
"rorl $16,%2\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"movb %b2,%%fs:0(%1,%0)\n\t" \
|
"movb %b2,%%fs:0(%1,%0)\n\t" \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"rorl $16,%2\n\t" \
|
"rorl $16,%2\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"movb %h2,%%fs:0(%1,%0)\n\t" \
|
"decw %w0\n\t" \
|
"decw %w0\n\t" \
|
"movb %b2,%%fs:0(%1,%0)" \
|
"movb %b2,%%fs:0(%1,%0)" \
|
: "=r" (ptr) \
|
: "=r" (ptr) \
|
: "r" (base), "q" (val), "0" (ptr))
|
: "r" (base), "q" (val), "0" (ptr))
|
|
|
#define popb(base, ptr) \
|
#define popb(base, ptr) \
|
({ unsigned long __res; \
|
({ unsigned long __res; \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"incw %w0" \
|
"incw %w0" \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "0" (ptr), "1" (base), "2" (0)); \
|
: "0" (ptr), "1" (base), "2" (0)); \
|
__res; })
|
__res; })
|
|
|
#define popw(base, ptr) \
|
#define popw(base, ptr) \
|
({ unsigned long __res; \
|
({ unsigned long __res; \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"incw %w0\n\t" \
|
"incw %w0\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"incw %w0" \
|
"incw %w0" \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "0" (ptr), "1" (base), "2" (0)); \
|
: "0" (ptr), "1" (base), "2" (0)); \
|
__res; })
|
__res; })
|
|
|
#define popl(base, ptr) \
|
#define popl(base, ptr) \
|
({ unsigned long __res; \
|
({ unsigned long __res; \
|
__asm__ __volatile__( \
|
__asm__ __volatile__( \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"incw %w0\n\t" \
|
"incw %w0\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"incw %w0\n\t" \
|
"incw %w0\n\t" \
|
"rorl $16,%2\n\t" \
|
"rorl $16,%2\n\t" \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"movb %%fs:0(%1,%0),%b2\n\t" \
|
"incw %w0\n\t" \
|
"incw %w0\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"movb %%fs:0(%1,%0),%h2\n\t" \
|
"incw %w0\n\t" \
|
"incw %w0\n\t" \
|
"rorl $16,%2" \
|
"rorl $16,%2" \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "=r" (ptr), "=r" (base), "=q" (__res) \
|
: "0" (ptr), "1" (base)); \
|
: "0" (ptr), "1" (base)); \
|
__res; })
|
__res; })
|
|
|
static void do_int(struct vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp)
|
static void do_int(struct vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp)
|
{
|
{
|
unsigned long *intr_ptr, segoffs;
|
unsigned long *intr_ptr, segoffs;
|
|
|
if (regs->cs == BIOSSEG)
|
if (regs->cs == BIOSSEG)
|
goto cannot_handle;
|
goto cannot_handle;
|
if (is_revectored(i, &KVM86->int_revectored))
|
if (is_revectored(i, &KVM86->int_revectored))
|
goto cannot_handle;
|
goto cannot_handle;
|
if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
|
if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
|
goto cannot_handle;
|
goto cannot_handle;
|
intr_ptr = (unsigned long *) (i << 2);
|
intr_ptr = (unsigned long *) (i << 2);
|
if (verify_area(VERIFY_READ, intr_ptr, 4) < 0)
|
if (verify_area(VERIFY_READ, intr_ptr, 4) < 0)
|
goto cannot_handle;
|
goto cannot_handle;
|
segoffs = get_fs_long(intr_ptr);
|
segoffs = get_fs_long(intr_ptr);
|
if ((segoffs >> 16) == BIOSSEG)
|
if ((segoffs >> 16) == BIOSSEG)
|
goto cannot_handle;
|
goto cannot_handle;
|
pushw(ssp, sp, get_vflags(regs));
|
pushw(ssp, sp, get_vflags(regs));
|
pushw(ssp, sp, regs->cs);
|
pushw(ssp, sp, regs->cs);
|
pushw(ssp, sp, IP(regs));
|
pushw(ssp, sp, IP(regs));
|
regs->cs = segoffs >> 16;
|
regs->cs = segoffs >> 16;
|
SP(regs) -= 6;
|
SP(regs) -= 6;
|
IP(regs) = segoffs & 0xffff;
|
IP(regs) = segoffs & 0xffff;
|
clear_TF(regs);
|
clear_TF(regs);
|
clear_IF(regs);
|
clear_IF(regs);
|
return;
|
return;
|
|
|
cannot_handle:
|
cannot_handle:
|
return_to_32bit(regs, VM86_INTx + (i << 8));
|
return_to_32bit(regs, VM86_INTx + (i << 8));
|
}
|
}
|
|
|
|
|
|
|
int handle_vm86_trap(struct vm86_regs * regs, long error_code, int trapno)
|
int handle_vm86_trap(struct vm86_regs * regs, long error_code, int trapno)
|
{
|
{
|
if (VMPI.is_vm86pus) {
|
if (VMPI.is_vm86pus) {
|
if ( (trapno==3) || (trapno==1) )
|
if ( (trapno==3) || (trapno==1) )
|
return_to_32bit(regs, VM86_TRAP + (trapno << 8));
|
return_to_32bit(regs, VM86_TRAP + (trapno << 8));
|
do_int(regs, trapno, (unsigned char *) (regs->ss << 4), SP(regs));
|
do_int(regs, trapno, (unsigned char *) (regs->ss << 4), SP(regs));
|
return 0;
|
return 0;
|
}
|
}
|
if (trapno !=1)
|
if (trapno !=1)
|
return 1; /* we let this handle by the calling routine */
|
return 1; /* we let this handle by the calling routine */
|
if (current->flags & PF_PTRACED)
|
if (current->flags & PF_PTRACED)
|
current->blocked &= ~(1 << (SIGTRAP-1));
|
current->blocked &= ~(1 << (SIGTRAP-1));
|
send_sig(SIGTRAP, current, 1);
|
send_sig(SIGTRAP, current, 1);
|
current->tss.trap_no = trapno;
|
current->tss.trap_no = trapno;
|
current->tss.error_code = error_code;
|
current->tss.error_code = error_code;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|
void handle_vm86_fault(struct vm86_regs * regs, long error_code)
|
void handle_vm86_fault(struct vm86_regs * regs, long error_code)
|
{
|
{
|
unsigned char *csp, *ssp;
|
unsigned char *csp, *ssp;
|
unsigned long ip, sp;
|
unsigned long ip, sp;
|
|
|
#define CHECK_IF_IN_TRAP \
|
#define CHECK_IF_IN_TRAP \
|
if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
|
if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
|
pushw(ssp,sp,popw(ssp,sp) | TF_MASK);
|
pushw(ssp,sp,popw(ssp,sp) | TF_MASK);
|
#define VM86_FAULT_RETURN \
|
#define VM86_FAULT_RETURN \
|
if (VMPI.force_return_for_pic && (VEFLAGS & IF_MASK)) \
|
if (VMPI.force_return_for_pic && (VEFLAGS & IF_MASK)) \
|
return_to_32bit(regs, VM86_PICRETURN); \
|
return_to_32bit(regs, VM86_PICRETURN); \
|
return;
|
return;
|
|
|
csp = (unsigned char *) (regs->cs << 4);
|
csp = (unsigned char *) (regs->cs << 4);
|
ssp = (unsigned char *) (regs->ss << 4);
|
ssp = (unsigned char *) (regs->ss << 4);
|
sp = SP(regs);
|
sp = SP(regs);
|
ip = IP(regs);
|
ip = IP(regs);
|
|
|
switch (popb(csp, ip)) {
|
switch (popb(csp, ip)) {
|
|
|
/* operand size override */
|
/* operand size override */
|
case 0x66:
|
case 0x66:
|
switch (popb(csp, ip)) {
|
switch (popb(csp, ip)) {
|
|
|
/* pushfd */
|
/* pushfd */
|
case 0x9c:
|
case 0x9c:
|
SP(regs) -= 4;
|
SP(regs) -= 4;
|
IP(regs) += 2;
|
IP(regs) += 2;
|
pushl(ssp, sp, get_vflags(regs));
|
pushl(ssp, sp, get_vflags(regs));
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* popfd */
|
/* popfd */
|
case 0x9d:
|
case 0x9d:
|
SP(regs) += 4;
|
SP(regs) += 4;
|
IP(regs) += 2;
|
IP(regs) += 2;
|
CHECK_IF_IN_TRAP
|
CHECK_IF_IN_TRAP
|
set_vflags_long(popl(ssp, sp), regs);
|
set_vflags_long(popl(ssp, sp), regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* iretd */
|
/* iretd */
|
case 0xcf:
|
case 0xcf:
|
SP(regs) += 12;
|
SP(regs) += 12;
|
IP(regs) = (unsigned short)popl(ssp, sp);
|
IP(regs) = (unsigned short)popl(ssp, sp);
|
regs->cs = (unsigned short)popl(ssp, sp);
|
regs->cs = (unsigned short)popl(ssp, sp);
|
CHECK_IF_IN_TRAP
|
CHECK_IF_IN_TRAP
|
set_vflags_long(popl(ssp, sp), regs);
|
set_vflags_long(popl(ssp, sp), regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
/* need this to avoid a fallthrough */
|
/* need this to avoid a fallthrough */
|
default:
|
default:
|
return_to_32bit(regs, VM86_UNKNOWN);
|
return_to_32bit(regs, VM86_UNKNOWN);
|
}
|
}
|
|
|
/* pushf */
|
/* pushf */
|
case 0x9c:
|
case 0x9c:
|
SP(regs) -= 2;
|
SP(regs) -= 2;
|
IP(regs)++;
|
IP(regs)++;
|
pushw(ssp, sp, get_vflags(regs));
|
pushw(ssp, sp, get_vflags(regs));
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* popf */
|
/* popf */
|
case 0x9d:
|
case 0x9d:
|
SP(regs) += 2;
|
SP(regs) += 2;
|
IP(regs)++;
|
IP(regs)++;
|
CHECK_IF_IN_TRAP
|
CHECK_IF_IN_TRAP
|
set_vflags_short(popw(ssp, sp), regs);
|
set_vflags_short(popw(ssp, sp), regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* int xx */
|
/* int xx */
|
case 0xcd: {
|
case 0xcd: {
|
int intno=popb(csp, ip);
|
int intno=popb(csp, ip);
|
IP(regs) += 2;
|
IP(regs) += 2;
|
if (VMPI.vm86dbg_active) {
|
if (VMPI.vm86dbg_active) {
|
if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
|
if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
|
return_to_32bit(regs, VM86_INTx + (intno << 8));
|
return_to_32bit(regs, VM86_INTx + (intno << 8));
|
}
|
}
|
do_int(regs, intno, ssp, sp);
|
do_int(regs, intno, ssp, sp);
|
return;
|
return;
|
}
|
}
|
|
|
/* iret */
|
/* iret */
|
case 0xcf:
|
case 0xcf:
|
SP(regs) += 6;
|
SP(regs) += 6;
|
IP(regs) = popw(ssp, sp);
|
IP(regs) = popw(ssp, sp);
|
regs->cs = popw(ssp, sp);
|
regs->cs = popw(ssp, sp);
|
CHECK_IF_IN_TRAP
|
CHECK_IF_IN_TRAP
|
set_vflags_short(popw(ssp, sp), regs);
|
set_vflags_short(popw(ssp, sp), regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* cli */
|
/* cli */
|
case 0xfa:
|
case 0xfa:
|
IP(regs)++;
|
IP(regs)++;
|
clear_IF(regs);
|
clear_IF(regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
/* sti */
|
/* sti */
|
/*
|
/*
|
* Damn. This is incorrect: the 'sti' instruction should actually
|
* Damn. This is incorrect: the 'sti' instruction should actually
|
* enable interrupts after the /next/ instruction. Not good.
|
* enable interrupts after the /next/ instruction. Not good.
|
*
|
*
|
* Probably needs some horsing around with the TF flag. Aiee..
|
* Probably needs some horsing around with the TF flag. Aiee..
|
*/
|
*/
|
case 0xfb:
|
case 0xfb:
|
IP(regs)++;
|
IP(regs)++;
|
set_IF(regs);
|
set_IF(regs);
|
VM86_FAULT_RETURN;
|
VM86_FAULT_RETURN;
|
|
|
default:
|
default:
|
return_to_32bit(regs, VM86_UNKNOWN);
|
return_to_32bit(regs, VM86_UNKNOWN);
|
}
|
}
|
}
|
}
|
|
|
/* ---------------- vm86 special IRQ passing stuff ----------------- */
|
/* ---------------- vm86 special IRQ passing stuff ----------------- */
|
|
|
#define VM86_IRQNAME "vm86irq"
|
#define VM86_IRQNAME "vm86irq"
|
|
|
static struct vm86_irqs {
|
static struct vm86_irqs {
|
struct task_struct *tsk;
|
struct task_struct *tsk;
|
int sig;
|
int sig;
|
} vm86_irqs[16] = {{0},};
|
} vm86_irqs[16] = {{0},};
|
static int irqbits=0;
|
static int irqbits=0;
|
|
|
#define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
|
#define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
|
| (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
|
| (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
|
| (1 << SIGUNUSED) )
|
| (1 << SIGUNUSED) )
|
|
|
static void irq_handler(int intno, void *dev_id, struct pt_regs * regs) {
|
static void irq_handler(int intno, void *dev_id, struct pt_regs * regs) {
|
int irq_bit;
|
int irq_bit;
|
unsigned long flags;
|
unsigned long flags;
|
|
|
save_flags(flags);
|
save_flags(flags);
|
cli();
|
cli();
|
irq_bit = 1 << intno;
|
irq_bit = 1 << intno;
|
if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk) {
|
if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk) {
|
restore_flags(flags);
|
restore_flags(flags);
|
return;
|
return;
|
}
|
}
|
irqbits |= irq_bit;
|
irqbits |= irq_bit;
|
if (vm86_irqs[intno].sig)
|
if (vm86_irqs[intno].sig)
|
send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
|
send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
|
/* else user will poll for IRQs */
|
/* else user will poll for IRQs */
|
restore_flags(flags);
|
restore_flags(flags);
|
}
|
}
|
|
|
static inline void free_vm86_irq(int irqnumber)
|
static inline void free_vm86_irq(int irqnumber)
|
{
|
{
|
free_irq(irqnumber,0);
|
free_irq(irqnumber,0);
|
vm86_irqs[irqnumber].tsk = 0;
|
vm86_irqs[irqnumber].tsk = 0;
|
irqbits &= ~(1 << irqnumber);
|
irqbits &= ~(1 << irqnumber);
|
}
|
}
|
|
|
static inline int task_valid(struct task_struct *tsk)
|
static inline int task_valid(struct task_struct *tsk)
|
{
|
{
|
struct task_struct *p;
|
struct task_struct *p;
|
|
|
for_each_task(p) {
|
for_each_task(p) {
|
if ((p == tsk) && (p->sig)) return 1;
|
if ((p == tsk) && (p->sig)) return 1;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static inline void handle_irq_zombies(void)
|
static inline void handle_irq_zombies(void)
|
{
|
{
|
int i;
|
int i;
|
for (i=3; i<16; i++) {
|
for (i=3; i<16; i++) {
|
if (vm86_irqs[i].tsk) {
|
if (vm86_irqs[i].tsk) {
|
if (task_valid(vm86_irqs[i].tsk)) continue;
|
if (task_valid(vm86_irqs[i].tsk)) continue;
|
free_vm86_irq(i);
|
free_vm86_irq(i);
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
static inline int get_and_reset_irq(int irqnumber)
|
static inline int get_and_reset_irq(int irqnumber)
|
{
|
{
|
int bit;
|
int bit;
|
unsigned long flags;
|
unsigned long flags;
|
|
|
if ( (irqnumber<3) || (irqnumber>15) ) return 0;
|
if ( (irqnumber<3) || (irqnumber>15) ) return 0;
|
if (vm86_irqs[irqnumber].tsk != current) return 0;
|
if (vm86_irqs[irqnumber].tsk != current) return 0;
|
save_flags(flags);
|
save_flags(flags);
|
cli();
|
cli();
|
bit = irqbits & (1 << irqnumber);
|
bit = irqbits & (1 << irqnumber);
|
irqbits &= ~bit;
|
irqbits &= ~bit;
|
restore_flags(flags);
|
restore_flags(flags);
|
return bit;
|
return bit;
|
}
|
}
|
|
|
|
|
static int do_vm86_irq_handling(int subfunction, int irqnumber)
|
static int do_vm86_irq_handling(int subfunction, int irqnumber)
|
{
|
{
|
int ret;
|
int ret;
|
switch (subfunction) {
|
switch (subfunction) {
|
case VM86_GET_AND_RESET_IRQ: {
|
case VM86_GET_AND_RESET_IRQ: {
|
return get_and_reset_irq(irqnumber);
|
return get_and_reset_irq(irqnumber);
|
}
|
}
|
case VM86_GET_IRQ_BITS: {
|
case VM86_GET_IRQ_BITS: {
|
return irqbits;
|
return irqbits;
|
}
|
}
|
case VM86_REQUEST_IRQ: {
|
case VM86_REQUEST_IRQ: {
|
int sig = irqnumber >> 8;
|
int sig = irqnumber >> 8;
|
int irq = irqnumber & 255;
|
int irq = irqnumber & 255;
|
handle_irq_zombies();
|
handle_irq_zombies();
|
if (!suser() || securelevel > 0) return -EPERM;
|
if (!suser() || securelevel > 0) return -EPERM;
|
if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
|
if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
|
if ( (irq<3) || (irq>15) ) return -EPERM;
|
if ( (irq<3) || (irq>15) ) return -EPERM;
|
if (vm86_irqs[irq].tsk) return -EPERM;
|
if (vm86_irqs[irq].tsk) return -EPERM;
|
ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, 0);
|
ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, 0);
|
if (ret) return ret;
|
if (ret) return ret;
|
vm86_irqs[irq].sig = sig;
|
vm86_irqs[irq].sig = sig;
|
vm86_irqs[irq].tsk = current;
|
vm86_irqs[irq].tsk = current;
|
return irq;
|
return irq;
|
}
|
}
|
case VM86_FREE_IRQ: {
|
case VM86_FREE_IRQ: {
|
handle_irq_zombies();
|
handle_irq_zombies();
|
if ( (irqnumber<3) || (irqnumber>15) ) return -EPERM;
|
if ( (irqnumber<3) || (irqnumber>15) ) return -EPERM;
|
if (!vm86_irqs[irqnumber].tsk) return 0;
|
if (!vm86_irqs[irqnumber].tsk) return 0;
|
if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
|
if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
|
free_vm86_irq(irqnumber);
|
free_vm86_irq(irqnumber);
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
return -EINVAL;
|
return -EINVAL;
|
}
|
}
|
|
|
|
|