ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / vm86.c
1 /*
2  *  linux/kernel/vm86.c
3  *
4  *  Copyright (C) 1994  Linus Torvalds
5  *
6  *  29 dec 2001 - Fixed oopses caused by unchecked access to the vm86
7  *                stack - Manfred Spraul <manfreds@colorfullife.com>
8  *
9  *  22 mar 2002 - Manfred detected the stackfaults, but didn't handle
10  *                them correctly. Now the emulation will be in a
11  *                consistent state after stackfaults - Kasper Dupont
12  *                <kasperd@daimi.au.dk>
13  *
14  *  22 mar 2002 - Added missing clear_IF in set_vflags_* Kasper Dupont
15  *                <kasperd@daimi.au.dk>
16  *
17  *  ?? ??? 2002 - Fixed premature returns from handle_vm86_fault
18  *                caused by Kasper Dupont's changes - Stas Sergeev
19  *
20  *   4 apr 2002 - Fixed CHECK_IF_IN_TRAP broken by Stas' changes.
21  *                Kasper Dupont <kasperd@daimi.au.dk>
22  *
23  *   9 apr 2002 - Changed syntax of macros in handle_vm86_fault.
24  *                Kasper Dupont <kasperd@daimi.au.dk>
25  *
26  *   9 apr 2002 - Changed stack access macros to jump to a label
27  *                instead of returning to userspace. This simplifies
28  *                do_int, and is needed by handle_vm6_fault. Kasper
29  *                Dupont <kasperd@daimi.au.dk>
30  *
31  */
32
33 #include <linux/config.h>
34 #include <linux/errno.h>
35 #include <linux/interrupt.h>
36 #include <linux/sched.h>
37 #include <linux/kernel.h>
38 #include <linux/signal.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/smp.h>
42 #include <linux/smp_lock.h>
43 #include <linux/highmem.h>
44 #include <linux/ptrace.h>
45
46 #include <asm/uaccess.h>
47 #include <asm/pgalloc.h>
48 #include <asm/io.h>
49 #include <asm/tlbflush.h>
50 #include <asm/irq.h>
51
52 /*
53  * Known problems:
54  *
55  * Interrupt handling is not guaranteed:
56  * - a real x86 will disable all interrupts for one instruction
57  *   after a "mov ss,xx" to make stack handling atomic even without
58  *   the 'lss' instruction. We can't guarantee this in v86 mode,
59  *   as the next instruction might result in a page fault or similar.
60  * - a real x86 will have interrupts disabled for one instruction
61  *   past the 'sti' that enables them. We don't bother with all the
62  *   details yet.
63  *
64  * Let's hope these problems do not actually matter for anything.
65  */
66
67
68 #define KVM86   ((struct kernel_vm86_struct *)regs)
69 #define VMPI    KVM86->vm86plus
70
71
72 /*
73  * 8- and 16-bit register defines..
74  */
75 #define AL(regs)        (((unsigned char *)&((regs)->eax))[0])
76 #define AH(regs)        (((unsigned char *)&((regs)->eax))[1])
77 #define IP(regs)        (*(unsigned short *)&((regs)->eip))
78 #define SP(regs)        (*(unsigned short *)&((regs)->esp))
79
80 /*
81  * virtual flags (16 and 32-bit versions)
82  */
83 #define VFLAGS  (*(unsigned short *)&(current->thread.v86flags))
84 #define VEFLAGS (current->thread.v86flags)
85
86 #define set_flags(X,new,mask) \
87 ((X) = ((X) & ~(mask)) | ((new) & (mask)))
88
89 #define SAFE_MASK       (0xDD5)
90 #define RETURN_MASK     (0xDFF)
91
92 #define VM86_REGS_PART2 orig_eax
93 #define VM86_REGS_SIZE1 \
94         ( (unsigned)( & (((struct kernel_vm86_regs *)0)->VM86_REGS_PART2) ) )
95 #define VM86_REGS_SIZE2 (sizeof(struct kernel_vm86_regs) - VM86_REGS_SIZE1)
96
97 struct pt_regs * FASTCALL(save_v86_state(struct kernel_vm86_regs * regs));
98 struct pt_regs * fastcall save_v86_state(struct kernel_vm86_regs * regs)
99 {
100         struct tss_struct *tss;
101         struct pt_regs *ret;
102         unsigned long tmp;
103
104         /*
105          * This gets called from entry.S with interrupts disabled, but
106          * from process context. Enable interrupts here, before trying
107          * to access user space.
108          */
109         local_irq_enable();
110
111         if (!current->thread.vm86_info) {
112                 printk("no vm86_info: BAD\n");
113                 do_exit(SIGSEGV);
114         }
115         set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->thread.v86mask);
116         tmp = copy_to_user(&current->thread.vm86_info->regs,regs, VM86_REGS_SIZE1);
117         tmp += copy_to_user(&current->thread.vm86_info->regs.VM86_REGS_PART2,
118                 &regs->VM86_REGS_PART2, VM86_REGS_SIZE2);
119         tmp += put_user(current->thread.screen_bitmap,&current->thread.vm86_info->screen_bitmap);
120         if (tmp) {
121                 printk("vm86: could not access userspace vm86_info\n");
122                 do_exit(SIGSEGV);
123         }
124
125         tss = init_tss + get_cpu();
126         current->thread.esp0 = current->thread.saved_esp0;
127         current->thread.sysenter_cs = __KERNEL_CS;
128         load_esp0(tss, &current->thread);
129         current->thread.saved_esp0 = 0;
130         put_cpu();
131
132         loadsegment(fs, current->thread.saved_fs);
133         loadsegment(gs, current->thread.saved_gs);
134         ret = KVM86->regs32;
135         return ret;
136 }
137
138 static void mark_screen_rdonly(struct task_struct * tsk)
139 {
140         pgd_t *pgd;
141         pmd_t *pmd;
142         pte_t *pte, *mapped;
143         int i;
144
145         preempt_disable();
146         spin_lock(&tsk->mm->page_table_lock);
147         pgd = pgd_offset(tsk->mm, 0xA0000);
148         if (pgd_none(*pgd))
149                 goto out;
150         if (pgd_bad(*pgd)) {
151                 pgd_ERROR(*pgd);
152                 pgd_clear(pgd);
153                 goto out;
154         }
155         pmd = pmd_offset(pgd, 0xA0000);
156         if (pmd_none(*pmd))
157                 goto out;
158         if (pmd_bad(*pmd)) {
159                 pmd_ERROR(*pmd);
160                 pmd_clear(pmd);
161                 goto out;
162         }
163         pte = mapped = pte_offset_map(pmd, 0xA0000);
164         for (i = 0; i < 32; i++) {
165                 if (pte_present(*pte))
166                         set_pte(pte, pte_wrprotect(*pte));
167                 pte++;
168         }
169         pte_unmap(mapped);
170 out:
171         spin_unlock(&tsk->mm->page_table_lock);
172         preempt_enable();
173         flush_tlb();
174 }
175
176
177
178 static int do_vm86_irq_handling(int subfunction, int irqnumber);
179 static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
180
181 asmlinkage int sys_vm86old(struct vm86_struct __user * v86)
182 {
183         struct kernel_vm86_struct info; /* declare this _on top_,
184                                          * this avoids wasting of stack space.
185                                          * This remains on the stack until we
186                                          * return to 32 bit user space.
187                                          */
188         struct task_struct *tsk;
189         int tmp, ret = -EPERM;
190
191         tsk = current;
192         if (tsk->thread.saved_esp0)
193                 goto out;
194         tmp  = copy_from_user(&info, v86, VM86_REGS_SIZE1);
195         tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,
196                 (long)&info.vm86plus - (long)&info.regs.VM86_REGS_PART2);
197         ret = -EFAULT;
198         if (tmp)
199                 goto out;
200         memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
201         info.regs32 = (struct pt_regs *) &v86;
202         tsk->thread.vm86_info = v86;
203         do_sys_vm86(&info, tsk);
204         ret = 0;        /* we never return here */
205 out:
206         return ret;
207 }
208
209
210 asmlinkage int sys_vm86(unsigned long subfunction, struct vm86plus_struct __user * v86)
211 {
212         struct kernel_vm86_struct info; /* declare this _on top_,
213                                          * this avoids wasting of stack space.
214                                          * This remains on the stack until we
215                                          * return to 32 bit user space.
216                                          */
217         struct task_struct *tsk;
218         int tmp, ret;
219
220         tsk = current;
221         switch (subfunction) {
222                 case VM86_REQUEST_IRQ:
223                 case VM86_FREE_IRQ:
224                 case VM86_GET_IRQ_BITS:
225                 case VM86_GET_AND_RESET_IRQ:
226                         ret = do_vm86_irq_handling(subfunction,(int)v86);
227                         goto out;
228                 case VM86_PLUS_INSTALL_CHECK:
229                         /* NOTE: on old vm86 stuff this will return the error
230                            from verify_area(), because the subfunction is
231                            interpreted as (invalid) address to vm86_struct.
232                            So the installation check works.
233                          */
234                         ret = 0;
235                         goto out;
236         }
237
238         /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
239         ret = -EPERM;
240         if (tsk->thread.saved_esp0)
241                 goto out;
242         tmp  = copy_from_user(&info, v86, VM86_REGS_SIZE1);
243         tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,
244                 (long)&info.regs32 - (long)&info.regs.VM86_REGS_PART2);
245         ret = -EFAULT;
246         if (tmp)
247                 goto out;
248         info.regs32 = (struct pt_regs *) &subfunction;
249         info.vm86plus.is_vm86pus = 1;
250         tsk->thread.vm86_info = (struct vm86_struct __user *)v86;
251         do_sys_vm86(&info, tsk);
252         ret = 0;        /* we never return here */
253 out:
254         return ret;
255 }
256
257
258 static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
259 {
260         struct tss_struct *tss;
261 /*
262  * make sure the vm86() system call doesn't try to do anything silly
263  */
264         info->regs.__null_ds = 0;
265         info->regs.__null_es = 0;
266
267 /* we are clearing fs,gs later just before "jmp resume_userspace",
268  * because starting with Linux 2.1.x they aren't no longer saved/restored
269  */
270
271 /*
272  * The eflags register is also special: we cannot trust that the user
273  * has set it up safely, so this makes sure interrupt etc flags are
274  * inherited from protected mode.
275  */
276         VEFLAGS = info->regs.eflags;
277         info->regs.eflags &= SAFE_MASK;
278         info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
279         info->regs.eflags |= VM_MASK;
280
281         switch (info->cpu_type) {
282                 case CPU_286:
283                         tsk->thread.v86mask = 0;
284                         break;
285                 case CPU_386:
286                         tsk->thread.v86mask = NT_MASK | IOPL_MASK;
287                         break;
288                 case CPU_486:
289                         tsk->thread.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
290                         break;
291                 default:
292                         tsk->thread.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
293                         break;
294         }
295
296 /*
297  * Save old state, set default return value (%eax) to 0
298  */
299         info->regs32->eax = 0;
300         tsk->thread.saved_esp0 = tsk->thread.esp0;
301         asm volatile("movl %%fs,%0":"=m" (tsk->thread.saved_fs));
302         asm volatile("movl %%gs,%0":"=m" (tsk->thread.saved_gs));
303
304         tss = init_tss + get_cpu();
305         tsk->thread.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
306         if (cpu_has_sep)
307                 tsk->thread.sysenter_cs = 0;
308         load_esp0(tss, &tsk->thread);
309         put_cpu();
310
311         tsk->thread.screen_bitmap = info->screen_bitmap;
312         if (info->flags & VM86_SCREEN_BITMAP)
313                 mark_screen_rdonly(tsk);
314         __asm__ __volatile__(
315                 "xorl %%eax,%%eax; movl %%eax,%%fs; movl %%eax,%%gs\n\t"
316                 "movl %0,%%esp\n\t"
317                 "movl %1,%%ebp\n\t"
318                 "jmp resume_userspace"
319                 : /* no outputs */
320                 :"r" (&info->regs), "r" (tsk->thread_info) : "ax");
321         /* we never return here */
322 }
323
324 static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval)
325 {
326         struct pt_regs * regs32;
327
328         regs32 = save_v86_state(regs16);
329         regs32->eax = retval;
330         __asm__ __volatile__("movl %0,%%esp\n\t"
331                 "movl %1,%%ebp\n\t"
332                 "jmp resume_userspace"
333                 : : "r" (regs32), "r" (current_thread_info()));
334 }
335
336 static inline void set_IF(struct kernel_vm86_regs * regs)
337 {
338         VEFLAGS |= VIF_MASK;
339         if (VEFLAGS & VIP_MASK)
340                 return_to_32bit(regs, VM86_STI);
341 }
342
343 static inline void clear_IF(struct kernel_vm86_regs * regs)
344 {
345         VEFLAGS &= ~VIF_MASK;
346 }
347
348 static inline void clear_TF(struct kernel_vm86_regs * regs)
349 {
350         regs->eflags &= ~TF_MASK;
351 }
352
353 static inline void clear_AC(struct kernel_vm86_regs * regs)
354 {
355         regs->eflags &= ~AC_MASK;
356 }
357
358 /* It is correct to call set_IF(regs) from the set_vflags_*
359  * functions. However someone forgot to call clear_IF(regs)
360  * in the opposite case.
361  * After the command sequence CLI PUSHF STI POPF you should
362  * end up with interrups disabled, but you ended up with
363  * interrupts enabled.
364  *  ( I was testing my own changes, but the only bug I
365  *    could find was in a function I had not changed. )
366  * [KD]
367  */
368
369 static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs)
370 {
371         set_flags(VEFLAGS, eflags, current->thread.v86mask);
372         set_flags(regs->eflags, eflags, SAFE_MASK);
373         if (eflags & IF_MASK)
374                 set_IF(regs);
375         else
376                 clear_IF(regs);
377 }
378
379 static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs)
380 {
381         set_flags(VFLAGS, flags, current->thread.v86mask);
382         set_flags(regs->eflags, flags, SAFE_MASK);
383         if (flags & IF_MASK)
384                 set_IF(regs);
385         else
386                 clear_IF(regs);
387 }
388
389 static inline unsigned long get_vflags(struct kernel_vm86_regs * regs)
390 {
391         unsigned long flags = regs->eflags & RETURN_MASK;
392
393         if (VEFLAGS & VIF_MASK)
394                 flags |= IF_MASK;
395         return flags | (VEFLAGS & current->thread.v86mask);
396 }
397
398 static inline int is_revectored(int nr, struct revectored_struct * bitmap)
399 {
400         __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
401                 :"=r" (nr)
402                 :"m" (*bitmap),"r" (nr));
403         return nr;
404 }
405
406 #define val_byte(val, n) (((__u8 *)&val)[n])
407
408 #define pushb(base, ptr, val, err_label) \
409         do { \
410                 __u8 __val = val; \
411                 ptr--; \
412                 if (put_user(__val, base + ptr) < 0) \
413                         goto err_label; \
414         } while(0)
415
416 #define pushw(base, ptr, val, err_label) \
417         do { \
418                 __u16 __val = val; \
419                 ptr--; \
420                 if (put_user(val_byte(__val, 1), base + ptr) < 0) \
421                         goto err_label; \
422                 ptr--; \
423                 if (put_user(val_byte(__val, 0), base + ptr) < 0) \
424                         goto err_label; \
425         } while(0)
426
427 #define pushl(base, ptr, val, err_label) \
428         do { \
429                 __u32 __val = val; \
430                 ptr--; \
431                 if (put_user(val_byte(__val, 3), base + ptr) < 0) \
432                         goto err_label; \
433                 ptr--; \
434                 if (put_user(val_byte(__val, 2), base + ptr) < 0) \
435                         goto err_label; \
436                 ptr--; \
437                 if (put_user(val_byte(__val, 1), base + ptr) < 0) \
438                         goto err_label; \
439                 ptr--; \
440                 if (put_user(val_byte(__val, 0), base + ptr) < 0) \
441                         goto err_label; \
442         } while(0)
443
444 #define popb(base, ptr, err_label) \
445         ({ \
446                 __u8 __res; \
447                 if (get_user(__res, base + ptr) < 0) \
448                         goto err_label; \
449                 ptr++; \
450                 __res; \
451         })
452
453 #define popw(base, ptr, err_label) \
454         ({ \
455                 __u16 __res; \
456                 if (get_user(val_byte(__res, 0), base + ptr) < 0) \
457                         goto err_label; \
458                 ptr++; \
459                 if (get_user(val_byte(__res, 1), base + ptr) < 0) \
460                         goto err_label; \
461                 ptr++; \
462                 __res; \
463         })
464
465 #define popl(base, ptr, err_label) \
466         ({ \
467                 __u32 __res; \
468                 if (get_user(val_byte(__res, 0), base + ptr) < 0) \
469                         goto err_label; \
470                 ptr++; \
471                 if (get_user(val_byte(__res, 1), base + ptr) < 0) \
472                         goto err_label; \
473                 ptr++; \
474                 if (get_user(val_byte(__res, 2), base + ptr) < 0) \
475                         goto err_label; \
476                 ptr++; \
477                 if (get_user(val_byte(__res, 3), base + ptr) < 0) \
478                         goto err_label; \
479                 ptr++; \
480                 __res; \
481         })
482
483 /* There are so many possible reasons for this function to return
484  * VM86_INTx, so adding another doesn't bother me. We can expect
485  * userspace programs to be able to handle it. (Getting a problem
486  * in userspace is always better than an Oops anyway.) [KD]
487  */
488 static void do_int(struct kernel_vm86_regs *regs, int i,
489     unsigned char * ssp, unsigned short sp)
490 {
491         unsigned long *intr_ptr, segoffs;
492
493         if (regs->cs == BIOSSEG)
494                 goto cannot_handle;
495         if (is_revectored(i, &KVM86->int_revectored))
496                 goto cannot_handle;
497         if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
498                 goto cannot_handle;
499         intr_ptr = (unsigned long *) (i << 2);
500         if (get_user(segoffs, intr_ptr))
501                 goto cannot_handle;
502         if ((segoffs >> 16) == BIOSSEG)
503                 goto cannot_handle;
504         pushw(ssp, sp, get_vflags(regs), cannot_handle);
505         pushw(ssp, sp, regs->cs, cannot_handle);
506         pushw(ssp, sp, IP(regs), cannot_handle);
507         regs->cs = segoffs >> 16;
508         SP(regs) -= 6;
509         IP(regs) = segoffs & 0xffff;
510         clear_TF(regs);
511         clear_IF(regs);
512         clear_AC(regs);
513         return;
514
515 cannot_handle:
516         return_to_32bit(regs, VM86_INTx + (i << 8));
517 }
518
519 int handle_vm86_trap(struct kernel_vm86_regs * regs, long error_code, int trapno)
520 {
521         if (VMPI.is_vm86pus) {
522                 if ( (trapno==3) || (trapno==1) )
523                         return_to_32bit(regs, VM86_TRAP + (trapno << 8));
524                 do_int(regs, trapno, (unsigned char *) (regs->ss << 4), SP(regs));
525                 return 0;
526         }
527         if (trapno !=1)
528                 return 1; /* we let this handle by the calling routine */
529         if (current->ptrace & PT_PTRACED) {
530                 unsigned long flags;
531                 spin_lock_irqsave(&current->sighand->siglock, flags);
532                 sigdelset(&current->blocked, SIGTRAP);
533                 recalc_sigpending();
534                 spin_unlock_irqrestore(&current->sighand->siglock, flags);
535         }
536         send_sig(SIGTRAP, current, 1);
537         current->thread.trap_no = trapno;
538         current->thread.error_code = error_code;
539         return 0;
540 }
541
542 void handle_vm86_fault(struct kernel_vm86_regs * regs, long error_code)
543 {
544         unsigned char *csp, *ssp, opcode;
545         unsigned short ip, sp;
546         int data32, pref_done;
547
548 #define CHECK_IF_IN_TRAP \
549         if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
550                 newflags |= TF_MASK
551 #define VM86_FAULT_RETURN do { \
552         if (VMPI.force_return_for_pic  && (VEFLAGS & (IF_MASK | VIF_MASK))) \
553                 return_to_32bit(regs, VM86_PICRETURN); \
554         return; } while (0)
555
556         csp = (unsigned char *) (regs->cs << 4);
557         ssp = (unsigned char *) (regs->ss << 4);
558         sp = SP(regs);
559         ip = IP(regs);
560
561         data32 = 0;
562         pref_done = 0;
563         do {
564                 switch (opcode = popb(csp, ip, simulate_sigsegv)) {
565                         case 0x66:      /* 32-bit data */     data32=1; break;
566                         case 0x67:      /* 32-bit address */  break;
567                         case 0x2e:      /* CS */              break;
568                         case 0x3e:      /* DS */              break;
569                         case 0x26:      /* ES */              break;
570                         case 0x36:      /* SS */              break;
571                         case 0x65:      /* GS */              break;
572                         case 0x64:      /* FS */              break;
573                         case 0xf2:      /* repnz */       break;
574                         case 0xf3:      /* rep */             break;
575                         default: pref_done = 1;
576                 }
577         } while (!pref_done);
578
579         switch (opcode) {
580
581         /* pushf */
582         case 0x9c:
583                 if (data32) {
584                         pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
585                         SP(regs) -= 4;
586                 } else {
587                         pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
588                         SP(regs) -= 2;
589                 }
590                 IP(regs) = ip;
591                 VM86_FAULT_RETURN;
592
593         /* popf */
594         case 0x9d:
595                 {
596                 unsigned long newflags;
597                 if (data32) {
598                         newflags=popl(ssp, sp, simulate_sigsegv);
599                         SP(regs) += 4;
600                 } else {
601                         newflags = popw(ssp, sp, simulate_sigsegv);
602                         SP(regs) += 2;
603                 }
604                 IP(regs) = ip;
605                 CHECK_IF_IN_TRAP;
606                 if (data32) {
607                         set_vflags_long(newflags, regs);
608                 } else {
609                         set_vflags_short(newflags, regs);
610                 }
611                 VM86_FAULT_RETURN;
612                 }
613
614         /* int xx */
615         case 0xcd: {
616                 int intno=popb(csp, ip, simulate_sigsegv);
617                 IP(regs) = ip;
618                 if (VMPI.vm86dbg_active) {
619                         if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
620                                 return_to_32bit(regs, VM86_INTx + (intno << 8));
621                 }
622                 do_int(regs, intno, ssp, sp);
623                 return;
624         }
625
626         /* iret */
627         case 0xcf:
628                 {
629                 unsigned long newip;
630                 unsigned long newcs;
631                 unsigned long newflags;
632                 if (data32) {
633                         newip=popl(ssp, sp, simulate_sigsegv);
634                         newcs=popl(ssp, sp, simulate_sigsegv);
635                         newflags=popl(ssp, sp, simulate_sigsegv);
636                         SP(regs) += 12;
637                 } else {
638                         newip = popw(ssp, sp, simulate_sigsegv);
639                         newcs = popw(ssp, sp, simulate_sigsegv);
640                         newflags = popw(ssp, sp, simulate_sigsegv);
641                         SP(regs) += 6;
642                 }
643                 IP(regs) = newip;
644                 regs->cs = newcs;
645                 CHECK_IF_IN_TRAP;
646                 if (data32) {
647                         set_vflags_long(newflags, regs);
648                 } else {
649                         set_vflags_short(newflags, regs);
650                 }
651                 VM86_FAULT_RETURN;
652                 }
653
654         /* cli */
655         case 0xfa:
656                 IP(regs) = ip;
657                 clear_IF(regs);
658                 VM86_FAULT_RETURN;
659
660         /* sti */
661         /*
662          * Damn. This is incorrect: the 'sti' instruction should actually
663          * enable interrupts after the /next/ instruction. Not good.
664          *
665          * Probably needs some horsing around with the TF flag. Aiee..
666          */
667         case 0xfb:
668                 IP(regs) = ip;
669                 set_IF(regs);
670                 VM86_FAULT_RETURN;
671
672         default:
673                 return_to_32bit(regs, VM86_UNKNOWN);
674         }
675
676         return;
677
678 simulate_sigsegv:
679         /* FIXME: After a long discussion with Stas we finally
680          *        agreed, that this is wrong. Here we should
681          *        really send a SIGSEGV to the user program.
682          *        But how do we create the correct context? We
683          *        are inside a general protection fault handler
684          *        and has just returned from a page fault handler.
685          *        The correct context for the signal handler
686          *        should be a mixture of the two, but how do we
687          *        get the information? [KD]
688          */
689         return_to_32bit(regs, VM86_UNKNOWN);
690 }
691
692 /* ---------------- vm86 special IRQ passing stuff ----------------- */
693
694 #define VM86_IRQNAME            "vm86irq"
695
696 static struct vm86_irqs {
697         struct task_struct *tsk;
698         int sig;
699 } vm86_irqs[16];
700
701 static spinlock_t irqbits_lock = SPIN_LOCK_UNLOCKED;
702 static int irqbits;
703
704 #define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
705         | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO)  | (1 << SIGURG) \
706         | (1 << SIGUNUSED) )
707         
708 static irqreturn_t irq_handler(int intno, void *dev_id, struct pt_regs * regs)
709 {
710         int irq_bit;
711         unsigned long flags;
712
713         spin_lock_irqsave(&irqbits_lock, flags);        
714         irq_bit = 1 << intno;
715         if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk)
716                 goto out;
717         irqbits |= irq_bit;
718         if (vm86_irqs[intno].sig)
719                 send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
720         /* else user will poll for IRQs */
721 out:
722         spin_unlock_irqrestore(&irqbits_lock, flags);   
723         return IRQ_NONE;
724 }
725
726 static inline void free_vm86_irq(int irqnumber)
727 {
728         unsigned long flags;
729
730         free_irq(irqnumber,0);
731         vm86_irqs[irqnumber].tsk = 0;
732
733         spin_lock_irqsave(&irqbits_lock, flags);        
734         irqbits &= ~(1 << irqnumber);
735         spin_unlock_irqrestore(&irqbits_lock, flags);   
736 }
737
738 void release_x86_irqs(struct task_struct *task)
739 {
740         int i;
741         for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
742             if (vm86_irqs[i].tsk == task)
743                 free_vm86_irq(i);
744 }
745
746 static inline int get_and_reset_irq(int irqnumber)
747 {
748         int bit;
749         unsigned long flags;
750         
751         if (invalid_vm86_irq(irqnumber)) return 0;
752         if (vm86_irqs[irqnumber].tsk != current) return 0;
753         spin_lock_irqsave(&irqbits_lock, flags);        
754         bit = irqbits & (1 << irqnumber);
755         irqbits &= ~bit;
756         spin_unlock_irqrestore(&irqbits_lock, flags);   
757         if (!bit)
758                 return 0;
759         enable_irq(irqnumber);
760         return 1;
761 }
762
763
764 static int do_vm86_irq_handling(int subfunction, int irqnumber)
765 {
766         int ret;
767         switch (subfunction) {
768                 case VM86_GET_AND_RESET_IRQ: {
769                         return get_and_reset_irq(irqnumber);
770                 }
771                 case VM86_GET_IRQ_BITS: {
772                         return irqbits;
773                 }
774                 case VM86_REQUEST_IRQ: {
775                         int sig = irqnumber >> 8;
776                         int irq = irqnumber & 255;
777                         if (!capable(CAP_SYS_ADMIN)) return -EPERM;
778                         if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
779                         if (invalid_vm86_irq(irq)) return -EPERM;
780                         if (vm86_irqs[irq].tsk) return -EPERM;
781                         ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, 0);
782                         if (ret) return ret;
783                         vm86_irqs[irq].sig = sig;
784                         vm86_irqs[irq].tsk = current;
785                         return irq;
786                 }
787                 case  VM86_FREE_IRQ: {
788                         if (invalid_vm86_irq(irqnumber)) return -EPERM;
789                         if (!vm86_irqs[irqnumber].tsk) return 0;
790                         if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
791                         free_vm86_irq(irqnumber);
792                         return 0;
793                 }
794         }
795         return -EINVAL;
796 }
797