root/kernel/vm86.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. save_v86_state
  2. mark_screen_rdonly
  3. sys_vm86
  4. return_to_32bit
  5. set_IF
  6. clear_IF
  7. clear_TF
  8. set_vflags_long
  9. set_vflags_short
  10. get_vflags
  11. is_revectored
  12. do_int
  13. handle_vm86_fault

   1 /*
   2  *  linux/kernel/vm86.c
   3  *
   4  *  Copyright (C) 1994  Linus Torvalds
   5  */
   6 #include <linux/errno.h>
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/signal.h>
  10 #include <linux/string.h>
  11 #include <linux/ptrace.h>
  12 
  13 #include <asm/segment.h>
  14 #include <asm/io.h>
  15 
  16 /*
  17  * 16-bit register defines..
  18  */
  19 #define IP(regs)        (*(unsigned short *)&((regs)->eip))
  20 #define SP(regs)        (*(unsigned short *)&((regs)->esp))
  21 
  22 /*
  23  * virtual flags (16 and 32-bit versions)
  24  */
  25 #define VFLAGS  (*(unsigned short *)&(current->v86flags))
  26 #define VEFLAGS (current->v86flags)
  27 
  28 #define set_flags(X,new,mask) \
  29 ((X) = ((X) & ~(mask)) | ((new) & (mask)))
  30 
  31 #define SAFE_MASK       (0xDD5)
  32 #define RETURN_MASK     (0xDFF)
  33 
  34 asmlinkage struct pt_regs * save_v86_state(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         unsigned long tmp;
  37 
  38         if (!current->vm86_info) {
  39                 printk("no vm86_info: BAD\n");
  40                 do_exit(SIGSEGV);
  41         }
  42         set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->v86mask);
  43         memcpy_tofs(&current->vm86_info->regs,regs,sizeof(*regs));
  44         put_fs_long(current->screen_bitmap,&current->vm86_info->screen_bitmap);
  45         tmp = current->tss.esp0;
  46         current->tss.esp0 = current->saved_kernel_stack;
  47         current->saved_kernel_stack = 0;
  48         return (struct pt_regs *) tmp;
  49 }
  50 
  51 static void mark_screen_rdonly(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53         unsigned long tmp;
  54         unsigned long *pg_table;
  55 
  56         if ((tmp = tsk->tss.cr3) != 0) {
  57                 tmp = *(unsigned long *) tmp;
  58                 if (tmp & PAGE_PRESENT) {
  59                         tmp &= PAGE_MASK;
  60                         pg_table = (0xA0000 >> PAGE_SHIFT) + (unsigned long *) tmp;
  61                         tmp = 32;
  62                         while (tmp--) {
  63                                 if (PAGE_PRESENT & *pg_table)
  64                                         *pg_table &= ~PAGE_RW;
  65                                 pg_table++;
  66                         }
  67                 }
  68         }
  69 }
  70 
  71 asmlinkage int sys_vm86(struct vm86_struct * v86)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73         struct vm86_struct info;
  74         struct pt_regs * pt_regs = (struct pt_regs *) &v86;
  75         int error;
  76 
  77         if (current->saved_kernel_stack)
  78                 return -EPERM;
  79         /* v86 must be readable (now) and writable (for save_v86_state) */
  80         error = verify_area(VERIFY_WRITE,v86,sizeof(*v86));
  81         if (error)
  82                 return error;
  83         memcpy_fromfs(&info,v86,sizeof(info));
  84 /*
  85  * make sure the vm86() system call doesn't try to do anything silly
  86  */
  87         info.regs.__null_ds = 0;
  88         info.regs.__null_es = 0;
  89         info.regs.__null_fs = 0;
  90         info.regs.__null_gs = 0;
  91 /*
  92  * The eflags register is also special: we cannot trust that the user
  93  * has set it up safely, so this makes sure interrupt etc flags are
  94  * inherited from protected mode.
  95  */
  96         VEFLAGS = info.regs.eflags;
  97         info.regs.eflags &= SAFE_MASK;
  98         info.regs.eflags |= pt_regs->eflags & ~SAFE_MASK;
  99         info.regs.eflags |= VM_MASK;
 100 
 101         switch (info.cpu_type) {
 102                 case CPU_286:
 103                         current->v86mask = 0;
 104                         break;
 105                 case CPU_386:
 106                         current->v86mask = NT_MASK | IOPL_MASK;
 107                         break;
 108                 case CPU_486:
 109                         current->v86mask = AC_MASK | NT_MASK | IOPL_MASK;
 110                         break;
 111                 default:
 112                         current->v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
 113                         break;
 114         }
 115 
 116 /*
 117  * Save old state, set default return value (%eax) to 0
 118  */
 119         pt_regs->eax = 0;
 120         current->saved_kernel_stack = current->tss.esp0;
 121         current->tss.esp0 = (unsigned long) pt_regs;
 122         current->vm86_info = v86;
 123 
 124         current->screen_bitmap = info.screen_bitmap;
 125         if (info.flags & VM86_SCREEN_BITMAP)
 126                 mark_screen_rdonly(current);
 127         __asm__ __volatile__("movl %0,%%esp\n\t"
 128                 "jmp ret_from_sys_call"
 129                 : /* no outputs */
 130                 :"r" (&info.regs));
 131         return 0;
 132 }
 133 
 134 static inline void return_to_32bit(struct vm86_regs * regs16, int retval)
     /* [previous][next][first][last][top][bottom][index][help] */
 135 {
 136         struct pt_regs * regs32;
 137 
 138         regs32 = save_v86_state(regs16);
 139         regs32->eax = retval;
 140         __asm__ __volatile__("movl %0,%%esp\n\t"
 141                 "jmp ret_from_sys_call"
 142                 : : "r" (regs32));
 143 }
 144 
 145 static inline void set_IF(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147         VEFLAGS |= VIF_MASK;
 148         if (VEFLAGS & VIP_MASK)
 149                 return_to_32bit(regs, VM86_STI);
 150 }
 151 
 152 static inline void clear_IF(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         VEFLAGS &= ~VIF_MASK;
 155 }
 156 
 157 static inline void clear_TF(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         regs->eflags &= ~TF_MASK;
 160 }
 161 
 162 static inline void set_vflags_long(unsigned long eflags, struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164         set_flags(VEFLAGS, eflags, current->v86mask);
 165         set_flags(regs->eflags, eflags, SAFE_MASK);
 166         if (eflags & IF_MASK)
 167                 set_IF(regs);
 168 }
 169 
 170 static inline void set_vflags_short(unsigned short flags, struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 171 {
 172         set_flags(VFLAGS, flags, current->v86mask);
 173         set_flags(regs->eflags, flags, SAFE_MASK);
 174         if (flags & IF_MASK)
 175                 set_IF(regs);
 176 }
 177 
 178 static inline unsigned long get_vflags(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180         unsigned long flags = regs->eflags & RETURN_MASK;
 181 
 182         if (VEFLAGS & VIF_MASK)
 183                 flags |= IF_MASK;
 184         return flags | (VEFLAGS & current->v86mask);
 185 }
 186 
 187 static inline int is_revectored(int nr, struct revectored_struct * bitmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 188 {
 189         __asm__ __volatile__("btl %2,%%fs:%1\n\tsbbl %0,%0"
 190                 :"=r" (nr)
 191                 :"m" (*bitmap),"r" (nr));
 192         return nr;
 193 }
 194 
 195 /*
 196  * Boy are these ugly, but we need to do the correct 16-bit arithmetic.
 197  * Gcc makes a mess of it, so we do it inline and use non-obvious calling
 198  * conventions..
 199  */
 200 #define pushb(base, ptr, val) \
 201 __asm__ __volatile__( \
 202         "decw %w0\n\t" \
 203         "movb %2,%%fs:0(%1,%0)" \
 204         : "=r" (ptr) \
 205         : "r" (base), "q" (val), "0" (ptr))
 206 
 207 #define pushw(base, ptr, val) \
 208 __asm__ __volatile__( \
 209         "decw %w0\n\t" \
 210         "movb %h2,%%fs:0(%1,%0)\n\t" \
 211         "decw %w0\n\t" \
 212         "movb %b2,%%fs:0(%1,%0)" \
 213         : "=r" (ptr) \
 214         : "r" (base), "q" (val), "0" (ptr))
 215 
 216 #define pushl(base, ptr, val) \
 217 __asm__ __volatile__( \
 218         "decw %w0\n\t" \
 219         "rorl $16,%2\n\t" \
 220         "movb %h2,%%fs:0(%1,%0)\n\t" \
 221         "decw %w0\n\t" \
 222         "movb %b2,%%fs:0(%1,%0)\n\t" \
 223         "decw %w0\n\t" \
 224         "rorl $16,%2\n\t" \
 225         "movb %h2,%%fs:0(%1,%0)\n\t" \
 226         "decw %w0\n\t" \
 227         "movb %b2,%%fs:0(%1,%0)" \
 228         : "=r" (ptr) \
 229         : "r" (base), "q" (val), "0" (ptr))
 230 
 231 #define popb(base, ptr) \
 232 ({ unsigned long __res; \
 233 __asm__ __volatile__( \
 234         "movb %%fs:0(%1,%0),%b2\n\t" \
 235         "incw %w0" \
 236         : "=r" (ptr), "=r" (base), "=r" (__res) \
 237         : "0" (ptr), "1" (base), "2" (0)); \
 238 __res; })
 239 
 240 #define popw(base, ptr) \
 241 ({ unsigned long __res; \
 242 __asm__ __volatile__( \
 243         "movb %%fs:0(%1,%0),%b2\n\t" \
 244         "incw %w0\n\t" \
 245         "movb %%fs:0(%1,%0),%h2\n\t" \
 246         "incw %w0" \
 247         : "=r" (ptr), "=r" (base), "=r" (__res) \
 248         : "0" (ptr), "1" (base), "2" (0)); \
 249 __res; })
 250 
 251 #define popl(base, ptr) \
 252 ({ unsigned long __res; \
 253 __asm__ __volatile__( \
 254         "movb %%fs:0(%1,%0),%b2\n\t" \
 255         "incw %w0\n\t" \
 256         "movb %%fs:0(%1,%0),%h2\n\t" \
 257         "incw %w0\n\t" \
 258         "rorl $16,%2\n\t" \
 259         "movb %%fs:0(%1,%0),%b2\n\t" \
 260         "incw %w0\n\t" \
 261         "movb %%fs:0(%1,%0),%h2\n\t" \
 262         "incw %w0\n\t" \
 263         "rorl $16,%2" \
 264         : "=r" (ptr), "=r" (base), "=r" (__res) \
 265         : "0" (ptr), "1" (base)); \
 266 __res; })
 267 
 268 static void do_int(struct vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp)
     /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270         unsigned short seg = get_fs_word((void *) ((i<<2)+2));
 271 
 272         if (seg == BIOSSEG || regs->cs == BIOSSEG ||
 273             is_revectored(i, &current->vm86_info->int_revectored))
 274                 return_to_32bit(regs, VM86_INTx + (i << 8));
 275         if (i==0x21 && is_revectored((regs->eax >> 4) & 0xff,&current->vm86_info->int21_revectored)) {
 276                 return_to_32bit(regs, VM86_INTx + (i << 8));
 277         }
 278         pushw(ssp, sp, get_vflags(regs));
 279         pushw(ssp, sp, regs->cs);
 280         pushw(ssp, sp, IP(regs));
 281         regs->cs = seg;
 282         SP(regs) -= 6;
 283         IP(regs) = get_fs_word((void *) (i<<2));
 284         clear_TF(regs);
 285         clear_IF(regs);
 286         return;
 287 }
 288 
 289 
 290 void handle_vm86_fault(struct vm86_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292         unsigned char *csp, *ssp;
 293         unsigned long ip, sp;
 294 
 295         csp = (unsigned char *) (regs->cs << 4);
 296         ssp = (unsigned char *) (regs->ss << 4);
 297         sp = SP(regs);
 298         ip = IP(regs);
 299 
 300         switch (popb(csp, ip)) {
 301 
 302         /* operand size override */
 303         case 0x66:
 304                 switch (popb(csp, ip)) {
 305 
 306                 /* pushfd */
 307                 case 0x9c:
 308                         SP(regs) -= 4;
 309                         IP(regs) += 2;
 310                         pushl(ssp, sp, get_vflags(regs));
 311                         return;
 312 
 313                 /* popfd */
 314                 case 0x9d:
 315                         SP(regs) += 4;
 316                         IP(regs) += 2;
 317                         set_vflags_long(popl(ssp, sp), regs);
 318                         return;
 319                 }
 320 
 321         /* pushf */
 322         case 0x9c:
 323                 SP(regs) -= 2;
 324                 IP(regs)++;
 325                 pushw(ssp, sp, get_vflags(regs));
 326                 return;
 327 
 328         /* popf */
 329         case 0x9d:
 330                 SP(regs) += 2;
 331                 IP(regs)++;
 332                 set_vflags_short(popw(ssp, sp), regs);
 333                 return;
 334 
 335         /* int 3 */
 336         case 0xcc:
 337                 IP(regs)++;
 338                 do_int(regs, 3, ssp, sp);
 339                 return;
 340 
 341         /* int xx */
 342         case 0xcd:
 343                 IP(regs) += 2;
 344                 do_int(regs, popb(csp, ip), ssp, sp);
 345                 return;
 346 
 347         /* iret */
 348         case 0xcf:
 349                 SP(regs) += 6;
 350                 IP(regs) = popw(ssp, sp);
 351                 regs->cs = popw(ssp, sp);
 352                 set_vflags_short(popw(ssp, sp), regs);
 353                 return;
 354 
 355         /* cli */
 356         case 0xfa:
 357                 IP(regs)++;
 358                 clear_IF(regs);
 359                 return;
 360 
 361         /* sti */
 362         case 0xfb:
 363                 IP(regs)++;
 364                 set_IF(regs);
 365                 return;
 366 
 367         default:
 368                 return_to_32bit(regs, VM86_UNKNOWN);
 369         }
 370 }

/* [previous][next][first][last][top][bottom][index][help] */