source: trunk/packages/xen-3.1/xen-3.1/linux-2.6-xen-sparse/arch/i386/kernel/vm86.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 21.4 KB
Line 
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 <manfred@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/capability.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#include <linux/audit.h>
46
47#include <asm/uaccess.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
97struct pt_regs * FASTCALL(save_v86_state(struct kernel_vm86_regs * regs));
98struct pt_regs * fastcall save_v86_state(struct kernel_vm86_regs * regs)
99{
100#ifndef CONFIG_X86_NO_TSS
101        struct tss_struct *tss;
102#endif
103        struct pt_regs *ret;
104        unsigned long tmp;
105
106        /*
107         * This gets called from entry.S with interrupts disabled, but
108         * from process context. Enable interrupts here, before trying
109         * to access user space.
110         */
111        local_irq_enable();
112
113        if (!current->thread.vm86_info) {
114                printk("no vm86_info: BAD\n");
115                do_exit(SIGSEGV);
116        }
117        set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->thread.v86mask);
118        tmp = copy_to_user(&current->thread.vm86_info->regs,regs, VM86_REGS_SIZE1);
119        tmp += copy_to_user(&current->thread.vm86_info->regs.VM86_REGS_PART2,
120                &regs->VM86_REGS_PART2, VM86_REGS_SIZE2);
121        tmp += put_user(current->thread.screen_bitmap,&current->thread.vm86_info->screen_bitmap);
122        if (tmp) {
123                printk("vm86: could not access userspace vm86_info\n");
124                do_exit(SIGSEGV);
125        }
126
127#ifndef CONFIG_X86_NO_TSS
128        tss = &per_cpu(init_tss, get_cpu());
129#endif
130        current->thread.esp0 = current->thread.saved_esp0;
131        current->thread.sysenter_cs = __KERNEL_CS;
132        load_esp0(tss, &current->thread);
133        current->thread.saved_esp0 = 0;
134#ifndef CONFIG_X86_NO_TSS
135        put_cpu();
136#endif
137
138        loadsegment(fs, current->thread.saved_fs);
139        loadsegment(gs, current->thread.saved_gs);
140        ret = KVM86->regs32;
141        return ret;
142}
143
144static void mark_screen_rdonly(struct mm_struct *mm)
145{
146        pgd_t *pgd;
147        pud_t *pud;
148        pmd_t *pmd;
149        pte_t *pte;
150        spinlock_t *ptl;
151        int i;
152
153        pgd = pgd_offset(mm, 0xA0000);
154        if (pgd_none_or_clear_bad(pgd))
155                goto out;
156        pud = pud_offset(pgd, 0xA0000);
157        if (pud_none_or_clear_bad(pud))
158                goto out;
159        pmd = pmd_offset(pud, 0xA0000);
160        if (pmd_none_or_clear_bad(pmd))
161                goto out;
162        pte = pte_offset_map_lock(mm, pmd, 0xA0000, &ptl);
163        for (i = 0; i < 32; i++) {
164                if (pte_present(*pte))
165                        set_pte(pte, pte_wrprotect(*pte));
166                pte++;
167        }
168        pte_unmap_unlock(pte, ptl);
169out:
170        flush_tlb();
171}
172
173
174
175static int do_vm86_irq_handling(int subfunction, int irqnumber);
176static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);
177
178asmlinkage int sys_vm86old(struct pt_regs regs)
179{
180        struct vm86_struct __user *v86 = (struct vm86_struct __user *)regs.ebx;
181        struct kernel_vm86_struct info; /* declare this _on top_,
182                                         * this avoids wasting of stack space.
183                                         * This remains on the stack until we
184                                         * return to 32 bit user space.
185                                         */
186        struct task_struct *tsk;
187        int tmp, ret = -EPERM;
188
189        tsk = current;
190        if (tsk->thread.saved_esp0)
191                goto out;
192        tmp  = copy_from_user(&info, v86, VM86_REGS_SIZE1);
193        tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,
194                (long)&info.vm86plus - (long)&info.regs.VM86_REGS_PART2);
195        ret = -EFAULT;
196        if (tmp)
197                goto out;
198        memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);
199        info.regs32 = &regs;
200        tsk->thread.vm86_info = v86;
201        do_sys_vm86(&info, tsk);
202        ret = 0;        /* we never return here */
203out:
204        return ret;
205}
206
207
208asmlinkage int sys_vm86(struct pt_regs regs)
209{
210        struct kernel_vm86_struct info; /* declare this _on top_,
211                                         * this avoids wasting of stack space.
212                                         * This remains on the stack until we
213                                         * return to 32 bit user space.
214                                         */
215        struct task_struct *tsk;
216        int tmp, ret;
217        struct vm86plus_struct __user *v86;
218
219        tsk = current;
220        switch (regs.ebx) {
221                case VM86_REQUEST_IRQ:
222                case VM86_FREE_IRQ:
223                case VM86_GET_IRQ_BITS:
224                case VM86_GET_AND_RESET_IRQ:
225                        ret = do_vm86_irq_handling(regs.ebx, (int)regs.ecx);
226                        goto out;
227                case VM86_PLUS_INSTALL_CHECK:
228                        /* NOTE: on old vm86 stuff this will return the error
229                           from access_ok(), because the subfunction is
230                           interpreted as (invalid) address to vm86_struct.
231                           So the installation check works.
232                         */
233                        ret = 0;
234                        goto out;
235        }
236
237        /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
238        ret = -EPERM;
239        if (tsk->thread.saved_esp0)
240                goto out;
241        v86 = (struct vm86plus_struct __user *)regs.ecx;
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 = &regs;
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 */
253out:
254        return ret;
255}
256
257
258static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk)
259{
260#ifndef CONFIG_X86_NO_TSS
261        struct tss_struct *tss;
262#endif
263        long eax;
264/*
265 * make sure the vm86() system call doesn't try to do anything silly
266 */
267        info->regs.__null_ds = 0;
268        info->regs.__null_es = 0;
269
270/* we are clearing fs,gs later just before "jmp resume_userspace",
271 * because starting with Linux 2.1.x they aren't no longer saved/restored
272 */
273
274/*
275 * The eflags register is also special: we cannot trust that the user
276 * has set it up safely, so this makes sure interrupt etc flags are
277 * inherited from protected mode.
278 */
279        VEFLAGS = info->regs.eflags;
280        info->regs.eflags &= SAFE_MASK;
281        info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;
282        info->regs.eflags |= VM_MASK;
283
284        switch (info->cpu_type) {
285                case CPU_286:
286                        tsk->thread.v86mask = 0;
287                        break;
288                case CPU_386:
289                        tsk->thread.v86mask = NT_MASK | IOPL_MASK;
290                        break;
291                case CPU_486:
292                        tsk->thread.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
293                        break;
294                default:
295                        tsk->thread.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
296                        break;
297        }
298
299/*
300 * Save old state, set default return value (%eax) to 0
301 */
302        info->regs32->eax = 0;
303        tsk->thread.saved_esp0 = tsk->thread.esp0;
304        savesegment(fs, tsk->thread.saved_fs);
305        savesegment(gs, tsk->thread.saved_gs);
306
307#ifndef CONFIG_X86_NO_TSS
308        tss = &per_cpu(init_tss, get_cpu());
309#endif
310        tsk->thread.esp0 = (unsigned long) &info->VM86_TSS_ESP0;
311        if (cpu_has_sep)
312                tsk->thread.sysenter_cs = 0;
313        load_esp0(tss, &tsk->thread);
314#ifndef CONFIG_X86_NO_TSS
315        put_cpu();
316#endif
317
318        tsk->thread.screen_bitmap = info->screen_bitmap;
319        if (info->flags & VM86_SCREEN_BITMAP)
320                mark_screen_rdonly(tsk->mm);
321        __asm__ __volatile__("xorl %eax,%eax; movl %eax,%fs; movl %eax,%gs\n\t");
322        __asm__ __volatile__("movl %%eax, %0\n" :"=r"(eax));
323
324        /*call audit_syscall_exit since we do not exit via the normal paths */
325        if (unlikely(current->audit_context))
326                audit_syscall_exit(AUDITSC_RESULT(eax), eax);
327
328        __asm__ __volatile__(
329                "movl %0,%%esp\n\t"
330                "movl %1,%%ebp\n\t"
331                "jmp resume_userspace"
332                : /* no outputs */
333                :"r" (&info->regs), "r" (task_thread_info(tsk)));
334        /* we never return here */
335}
336
337static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval)
338{
339        struct pt_regs * regs32;
340
341        regs32 = save_v86_state(regs16);
342        regs32->eax = retval;
343        __asm__ __volatile__("movl %0,%%esp\n\t"
344                "movl %1,%%ebp\n\t"
345                "jmp resume_userspace"
346                : : "r" (regs32), "r" (current_thread_info()));
347}
348
349static inline void set_IF(struct kernel_vm86_regs * regs)
350{
351        VEFLAGS |= VIF_MASK;
352        if (VEFLAGS & VIP_MASK)
353                return_to_32bit(regs, VM86_STI);
354}
355
356static inline void clear_IF(struct kernel_vm86_regs * regs)
357{
358        VEFLAGS &= ~VIF_MASK;
359}
360
361static inline void clear_TF(struct kernel_vm86_regs * regs)
362{
363        regs->eflags &= ~TF_MASK;
364}
365
366static inline void clear_AC(struct kernel_vm86_regs * regs)
367{
368        regs->eflags &= ~AC_MASK;
369}
370
371/* It is correct to call set_IF(regs) from the set_vflags_*
372 * functions. However someone forgot to call clear_IF(regs)
373 * in the opposite case.
374 * After the command sequence CLI PUSHF STI POPF you should
375 * end up with interrups disabled, but you ended up with
376 * interrupts enabled.
377 *  ( I was testing my own changes, but the only bug I
378 *    could find was in a function I had not changed. )
379 * [KD]
380 */
381
382static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs)
383{
384        set_flags(VEFLAGS, eflags, current->thread.v86mask);
385        set_flags(regs->eflags, eflags, SAFE_MASK);
386        if (eflags & IF_MASK)
387                set_IF(regs);
388        else
389                clear_IF(regs);
390}
391
392static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs)
393{
394        set_flags(VFLAGS, flags, current->thread.v86mask);
395        set_flags(regs->eflags, flags, SAFE_MASK);
396        if (flags & IF_MASK)
397                set_IF(regs);
398        else
399                clear_IF(regs);
400}
401
402static inline unsigned long get_vflags(struct kernel_vm86_regs * regs)
403{
404        unsigned long flags = regs->eflags & RETURN_MASK;
405
406        if (VEFLAGS & VIF_MASK)
407                flags |= IF_MASK;
408        flags |= IOPL_MASK;
409        return flags | (VEFLAGS & current->thread.v86mask);
410}
411
412static inline int is_revectored(int nr, struct revectored_struct * bitmap)
413{
414        __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
415                :"=r" (nr)
416                :"m" (*bitmap),"r" (nr));
417        return nr;
418}
419
420#define val_byte(val, n) (((__u8 *)&val)[n])
421
422#define pushb(base, ptr, val, err_label) \
423        do { \
424                __u8 __val = val; \
425                ptr--; \
426                if (put_user(__val, base + ptr) < 0) \
427                        goto err_label; \
428        } while(0)
429
430#define pushw(base, ptr, val, err_label) \
431        do { \
432                __u16 __val = val; \
433                ptr--; \
434                if (put_user(val_byte(__val, 1), base + ptr) < 0) \
435                        goto err_label; \
436                ptr--; \
437                if (put_user(val_byte(__val, 0), base + ptr) < 0) \
438                        goto err_label; \
439        } while(0)
440
441#define pushl(base, ptr, val, err_label) \
442        do { \
443                __u32 __val = val; \
444                ptr--; \
445                if (put_user(val_byte(__val, 3), base + ptr) < 0) \
446                        goto err_label; \
447                ptr--; \
448                if (put_user(val_byte(__val, 2), base + ptr) < 0) \
449                        goto err_label; \
450                ptr--; \
451                if (put_user(val_byte(__val, 1), base + ptr) < 0) \
452                        goto err_label; \
453                ptr--; \
454                if (put_user(val_byte(__val, 0), base + ptr) < 0) \
455                        goto err_label; \
456        } while(0)
457
458#define popb(base, ptr, err_label) \
459        ({ \
460                __u8 __res; \
461                if (get_user(__res, base + ptr) < 0) \
462                        goto err_label; \
463                ptr++; \
464                __res; \
465        })
466
467#define popw(base, ptr, err_label) \
468        ({ \
469                __u16 __res; \
470                if (get_user(val_byte(__res, 0), base + ptr) < 0) \
471                        goto err_label; \
472                ptr++; \
473                if (get_user(val_byte(__res, 1), base + ptr) < 0) \
474                        goto err_label; \
475                ptr++; \
476                __res; \
477        })
478
479#define popl(base, ptr, err_label) \
480        ({ \
481                __u32 __res; \
482                if (get_user(val_byte(__res, 0), base + ptr) < 0) \
483                        goto err_label; \
484                ptr++; \
485                if (get_user(val_byte(__res, 1), base + ptr) < 0) \
486                        goto err_label; \
487                ptr++; \
488                if (get_user(val_byte(__res, 2), base + ptr) < 0) \
489                        goto err_label; \
490                ptr++; \
491                if (get_user(val_byte(__res, 3), base + ptr) < 0) \
492                        goto err_label; \
493                ptr++; \
494                __res; \
495        })
496
497/* There are so many possible reasons for this function to return
498 * VM86_INTx, so adding another doesn't bother me. We can expect
499 * userspace programs to be able to handle it. (Getting a problem
500 * in userspace is always better than an Oops anyway.) [KD]
501 */
502static void do_int(struct kernel_vm86_regs *regs, int i,
503    unsigned char __user * ssp, unsigned short sp)
504{
505        unsigned long __user *intr_ptr;
506        unsigned long segoffs;
507
508        if (regs->cs == BIOSSEG)
509                goto cannot_handle;
510        if (is_revectored(i, &KVM86->int_revectored))
511                goto cannot_handle;
512        if (i==0x21 && is_revectored(AH(regs),&KVM86->int21_revectored))
513                goto cannot_handle;
514        intr_ptr = (unsigned long __user *) (i << 2);
515        if (get_user(segoffs, intr_ptr))
516                goto cannot_handle;
517        if ((segoffs >> 16) == BIOSSEG)
518                goto cannot_handle;
519        pushw(ssp, sp, get_vflags(regs), cannot_handle);
520        pushw(ssp, sp, regs->cs, cannot_handle);
521        pushw(ssp, sp, IP(regs), cannot_handle);
522        regs->cs = segoffs >> 16;
523        SP(regs) -= 6;
524        IP(regs) = segoffs & 0xffff;
525        clear_TF(regs);
526        clear_IF(regs);
527        clear_AC(regs);
528        return;
529
530cannot_handle:
531        return_to_32bit(regs, VM86_INTx + (i << 8));
532}
533
534int handle_vm86_trap(struct kernel_vm86_regs * regs, long error_code, int trapno)
535{
536        if (VMPI.is_vm86pus) {
537                if ( (trapno==3) || (trapno==1) )
538                        return_to_32bit(regs, VM86_TRAP + (trapno << 8));
539                do_int(regs, trapno, (unsigned char __user *) (regs->ss << 4), SP(regs));
540                return 0;
541        }
542        if (trapno !=1)
543                return 1; /* we let this handle by the calling routine */
544        if (current->ptrace & PT_PTRACED) {
545                unsigned long flags;
546                spin_lock_irqsave(&current->sighand->siglock, flags);
547                sigdelset(&current->blocked, SIGTRAP);
548                recalc_sigpending();
549                spin_unlock_irqrestore(&current->sighand->siglock, flags);
550        }
551        send_sig(SIGTRAP, current, 1);
552        current->thread.trap_no = trapno;
553        current->thread.error_code = error_code;
554        return 0;
555}
556
557void handle_vm86_fault(struct kernel_vm86_regs * regs, long error_code)
558{
559        unsigned char opcode;
560        unsigned char __user *csp;
561        unsigned char __user *ssp;
562        unsigned short ip, sp, orig_flags;
563        int data32, pref_done;
564
565#define CHECK_IF_IN_TRAP \
566        if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
567                newflags |= TF_MASK
568#define VM86_FAULT_RETURN do { \
569        if (VMPI.force_return_for_pic  && (VEFLAGS & (IF_MASK | VIF_MASK))) \
570                return_to_32bit(regs, VM86_PICRETURN); \
571        if (orig_flags & TF_MASK) \
572                handle_vm86_trap(regs, 0, 1); \
573        return; } while (0)
574
575        orig_flags = *(unsigned short *)&regs->eflags;
576
577        csp = (unsigned char __user *) (regs->cs << 4);
578        ssp = (unsigned char __user *) (regs->ss << 4);
579        sp = SP(regs);
580        ip = IP(regs);
581
582        data32 = 0;
583        pref_done = 0;
584        do {
585                switch (opcode = popb(csp, ip, simulate_sigsegv)) {
586                        case 0x66:      /* 32-bit data */     data32=1; break;
587                        case 0x67:      /* 32-bit address */  break;
588                        case 0x2e:      /* CS */              break;
589                        case 0x3e:      /* DS */              break;
590                        case 0x26:      /* ES */              break;
591                        case 0x36:      /* SS */              break;
592                        case 0x65:      /* GS */              break;
593                        case 0x64:      /* FS */              break;
594                        case 0xf2:      /* repnz */       break;
595                        case 0xf3:      /* rep */             break;
596                        default: pref_done = 1;
597                }
598        } while (!pref_done);
599
600        switch (opcode) {
601
602        /* pushf */
603        case 0x9c:
604                if (data32) {
605                        pushl(ssp, sp, get_vflags(regs), simulate_sigsegv);
606                        SP(regs) -= 4;
607                } else {
608                        pushw(ssp, sp, get_vflags(regs), simulate_sigsegv);
609                        SP(regs) -= 2;
610                }
611                IP(regs) = ip;
612                VM86_FAULT_RETURN;
613
614        /* popf */
615        case 0x9d:
616                {
617                unsigned long newflags;
618                if (data32) {
619                        newflags=popl(ssp, sp, simulate_sigsegv);
620                        SP(regs) += 4;
621                } else {
622                        newflags = popw(ssp, sp, simulate_sigsegv);
623                        SP(regs) += 2;
624                }
625                IP(regs) = ip;
626                CHECK_IF_IN_TRAP;
627                if (data32) {
628                        set_vflags_long(newflags, regs);
629                } else {
630                        set_vflags_short(newflags, regs);
631                }
632                VM86_FAULT_RETURN;
633                }
634
635        /* int xx */
636        case 0xcd: {
637                int intno=popb(csp, ip, simulate_sigsegv);
638                IP(regs) = ip;
639                if (VMPI.vm86dbg_active) {
640                        if ( (1 << (intno &7)) & VMPI.vm86dbg_intxxtab[intno >> 3] )
641                                return_to_32bit(regs, VM86_INTx + (intno << 8));
642                }
643                do_int(regs, intno, ssp, sp);
644                return;
645        }
646
647        /* iret */
648        case 0xcf:
649                {
650                unsigned long newip;
651                unsigned long newcs;
652                unsigned long newflags;
653                if (data32) {
654                        newip=popl(ssp, sp, simulate_sigsegv);
655                        newcs=popl(ssp, sp, simulate_sigsegv);
656                        newflags=popl(ssp, sp, simulate_sigsegv);
657                        SP(regs) += 12;
658                } else {
659                        newip = popw(ssp, sp, simulate_sigsegv);
660                        newcs = popw(ssp, sp, simulate_sigsegv);
661                        newflags = popw(ssp, sp, simulate_sigsegv);
662                        SP(regs) += 6;
663                }
664                IP(regs) = newip;
665                regs->cs = newcs;
666                CHECK_IF_IN_TRAP;
667                if (data32) {
668                        set_vflags_long(newflags, regs);
669                } else {
670                        set_vflags_short(newflags, regs);
671                }
672                VM86_FAULT_RETURN;
673                }
674
675        /* cli */
676        case 0xfa:
677                IP(regs) = ip;
678                clear_IF(regs);
679                VM86_FAULT_RETURN;
680
681        /* sti */
682        /*
683         * Damn. This is incorrect: the 'sti' instruction should actually
684         * enable interrupts after the /next/ instruction. Not good.
685         *
686         * Probably needs some horsing around with the TF flag. Aiee..
687         */
688        case 0xfb:
689                IP(regs) = ip;
690                set_IF(regs);
691                VM86_FAULT_RETURN;
692
693        default:
694                return_to_32bit(regs, VM86_UNKNOWN);
695        }
696
697        return;
698
699simulate_sigsegv:
700        /* FIXME: After a long discussion with Stas we finally
701         *        agreed, that this is wrong. Here we should
702         *        really send a SIGSEGV to the user program.
703         *        But how do we create the correct context? We
704         *        are inside a general protection fault handler
705         *        and has just returned from a page fault handler.
706         *        The correct context for the signal handler
707         *        should be a mixture of the two, but how do we
708         *        get the information? [KD]
709         */
710        return_to_32bit(regs, VM86_UNKNOWN);
711}
712
713/* ---------------- vm86 special IRQ passing stuff ----------------- */
714
715#define VM86_IRQNAME            "vm86irq"
716
717static struct vm86_irqs {
718        struct task_struct *tsk;
719        int sig;
720} vm86_irqs[16];
721
722static DEFINE_SPINLOCK(irqbits_lock);
723static int irqbits;
724
725#define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
726        | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO)  | (1 << SIGURG) \
727        | (1 << SIGUNUSED) )
728       
729static irqreturn_t irq_handler(int intno, void *dev_id, struct pt_regs * regs)
730{
731        int irq_bit;
732        unsigned long flags;
733
734        spin_lock_irqsave(&irqbits_lock, flags);       
735        irq_bit = 1 << intno;
736        if ((irqbits & irq_bit) || ! vm86_irqs[intno].tsk)
737                goto out;
738        irqbits |= irq_bit;
739        if (vm86_irqs[intno].sig)
740                send_sig(vm86_irqs[intno].sig, vm86_irqs[intno].tsk, 1);
741        /*
742         * IRQ will be re-enabled when user asks for the irq (whether
743         * polling or as a result of the signal)
744         */
745        disable_irq_nosync(intno);
746        spin_unlock_irqrestore(&irqbits_lock, flags);
747        return IRQ_HANDLED;
748
749out:
750        spin_unlock_irqrestore(&irqbits_lock, flags);   
751        return IRQ_NONE;
752}
753
754static inline void free_vm86_irq(int irqnumber)
755{
756        unsigned long flags;
757
758        free_irq(irqnumber, NULL);
759        vm86_irqs[irqnumber].tsk = NULL;
760
761        spin_lock_irqsave(&irqbits_lock, flags);       
762        irqbits &= ~(1 << irqnumber);
763        spin_unlock_irqrestore(&irqbits_lock, flags);   
764}
765
766void release_vm86_irqs(struct task_struct *task)
767{
768        int i;
769        for (i = FIRST_VM86_IRQ ; i <= LAST_VM86_IRQ; i++)
770            if (vm86_irqs[i].tsk == task)
771                free_vm86_irq(i);
772}
773
774static inline int get_and_reset_irq(int irqnumber)
775{
776        int bit;
777        unsigned long flags;
778        int ret = 0;
779       
780        if (invalid_vm86_irq(irqnumber)) return 0;
781        if (vm86_irqs[irqnumber].tsk != current) return 0;
782        spin_lock_irqsave(&irqbits_lock, flags);       
783        bit = irqbits & (1 << irqnumber);
784        irqbits &= ~bit;
785        if (bit) {
786                enable_irq(irqnumber);
787                ret = 1;
788        }
789
790        spin_unlock_irqrestore(&irqbits_lock, flags);   
791        return ret;
792}
793
794
795static int do_vm86_irq_handling(int subfunction, int irqnumber)
796{
797        int ret;
798        switch (subfunction) {
799                case VM86_GET_AND_RESET_IRQ: {
800                        return get_and_reset_irq(irqnumber);
801                }
802                case VM86_GET_IRQ_BITS: {
803                        return irqbits;
804                }
805                case VM86_REQUEST_IRQ: {
806                        int sig = irqnumber >> 8;
807                        int irq = irqnumber & 255;
808                        if (!capable(CAP_SYS_ADMIN)) return -EPERM;
809                        if (!((1 << sig) & ALLOWED_SIGS)) return -EPERM;
810                        if (invalid_vm86_irq(irq)) return -EPERM;
811                        if (vm86_irqs[irq].tsk) return -EPERM;
812                        ret = request_irq(irq, &irq_handler, 0, VM86_IRQNAME, NULL);
813                        if (ret) return ret;
814                        vm86_irqs[irq].sig = sig;
815                        vm86_irqs[irq].tsk = current;
816                        return irq;
817                }
818                case  VM86_FREE_IRQ: {
819                        if (invalid_vm86_irq(irqnumber)) return -EPERM;
820                        if (!vm86_irqs[irqnumber].tsk) return 0;
821                        if (vm86_irqs[irqnumber].tsk != current) return -EPERM;
822                        free_vm86_irq(irqnumber);
823                        return 0;
824                }
825        }
826        return -EINVAL;
827}
828
Note: See TracBrowser for help on using the repository browser.