4 * Copyright (C) 1994 Linus Torvalds
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>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
16 #include <asm/uaccess.h>
17 #include <asm/pgtable.h>
23 * Interrupt handling is not guaranteed:
24 * - a real x86 will disable all interrupts for one instruction
25 * after a "mov ss,xx" to make stack handling atomic even without
26 * the 'lss' instruction. We can't guarantee this in v86 mode,
27 * as the next instruction might result in a page fault or similar.
28 * - a real x86 will have interrupts disabled for one instruction
29 * past the 'sti' that enables them. We don't bother with all the
32 * Let's hope these problems do not actually matter for anything.
36 #define KVM86 ((struct kernel_vm86_struct *)regs)
37 #define VMPI KVM86->vm86plus
41 * 8- and 16-bit register defines..
43 #define AL(regs) (((unsigned char *)&((regs)->eax))[0])
44 #define AH(regs) (((unsigned char *)&((regs)->eax))[1])
45 #define IP(regs) (*(unsigned short *)&((regs)->eip))
46 #define SP(regs) (*(unsigned short *)&((regs)->esp))
49 * virtual flags (16 and 32-bit versions)
51 #define VFLAGS (*(unsigned short *)&(current->thread.v86flags))
52 #define VEFLAGS (current->thread.v86flags)
54 #define set_flags(X,new,mask) \
55 ((X) = ((X) & ~(mask)) | ((new) & (mask)))
57 #define SAFE_MASK (0xDD5)
58 #define RETURN_MASK (0xDFF)
60 #define VM86_REGS_PART2 orig_eax
61 #define VM86_REGS_SIZE1 \
62 ( (unsigned)( & (((struct kernel_vm86_regs *)0)->VM86_REGS_PART2) ) )
63 #define VM86_REGS_SIZE2 (sizeof(struct kernel_vm86_regs) - VM86_REGS_SIZE1)
65 asmlinkage
struct pt_regs
* FASTCALL(save_v86_state(struct kernel_vm86_regs
* regs
));
66 struct pt_regs
* save_v86_state(struct kernel_vm86_regs
* regs
)
68 struct tss_struct
*tss
;
73 if (!current
->thread
.vm86_info
) {
74 printk("no vm86_info: BAD\n");
77 set_flags(regs
->eflags
, VEFLAGS
, VIF_MASK
| current
->thread
.v86mask
);
78 tmp
= copy_to_user(¤t
->thread
.vm86_info
->regs
,regs
, VM86_REGS_SIZE1
);
79 tmp
+= copy_to_user(¤t
->thread
.vm86_info
->regs
.VM86_REGS_PART2
,
80 ®s
->VM86_REGS_PART2
, VM86_REGS_SIZE2
);
81 tmp
+= put_user(current
->thread
.screen_bitmap
,¤t
->thread
.vm86_info
->screen_bitmap
);
83 printk("vm86: could not access userspace vm86_info\n");
86 tss
= init_tss
+ smp_processor_id();
87 tss
->esp0
= current
->thread
.esp0
= current
->thread
.saved_esp0
;
88 current
->thread
.saved_esp0
= 0;
94 static void mark_screen_rdonly(struct task_struct
* tsk
)
101 pgd
= pgd_offset(tsk
->mm
, 0xA0000);
105 printk("vm86: bad pgd entry [%p]:%08lx\n", pgd
, pgd_val(*pgd
));
109 pmd
= pmd_offset(pgd
, 0xA0000);
113 printk("vm86: bad pmd entry [%p]:%08lx\n", pmd
, pmd_val(*pmd
));
117 pte
= pte_offset(pmd
, 0xA0000);
118 for (i
= 0; i
< 32; i
++) {
119 if (pte_present(*pte
))
120 set_pte(pte
, pte_wrprotect(*pte
));
128 static int do_vm86_irq_handling(int subfunction
, int irqnumber
);
129 static void do_sys_vm86(struct kernel_vm86_struct
*info
, struct task_struct
*tsk
);
131 asmlinkage
int sys_vm86old(struct vm86_struct
* v86
)
133 struct kernel_vm86_struct info
; /* declare this _on top_,
134 * this avoids wasting of stack space.
135 * This remains on the stack until we
136 * return to 32 bit user space.
138 struct task_struct
*tsk
;
139 int tmp
, ret
= -EPERM
;
143 if (tsk
->thread
.saved_esp0
)
145 tmp
= copy_from_user(&info
, v86
, VM86_REGS_SIZE1
);
146 tmp
+= copy_from_user(&info
.regs
.VM86_REGS_PART2
, &v86
->regs
.VM86_REGS_PART2
,
147 (long)&info
.vm86plus
- (long)&info
.regs
.VM86_REGS_PART2
);
151 memset(&info
.vm86plus
, 0, (int)&info
.regs32
- (int)&info
.vm86plus
);
152 info
.regs32
= (struct pt_regs
*) &v86
;
153 tsk
->thread
.vm86_info
= v86
;
154 do_sys_vm86(&info
, tsk
);
155 ret
= 0; /* we never return here */
162 asmlinkage
int sys_vm86(unsigned long subfunction
, struct vm86plus_struct
* v86
)
164 struct kernel_vm86_struct info
; /* declare this _on top_,
165 * this avoids wasting of stack space.
166 * This remains on the stack until we
167 * return to 32 bit user space.
169 struct task_struct
*tsk
;
174 switch (subfunction
) {
175 case VM86_REQUEST_IRQ
:
177 case VM86_GET_IRQ_BITS
:
178 case VM86_GET_AND_RESET_IRQ
:
179 ret
= do_vm86_irq_handling(subfunction
,(int)v86
);
181 case VM86_PLUS_INSTALL_CHECK
:
182 /* NOTE: on old vm86 stuff this will return the error
183 from verify_area(), because the subfunction is
184 interpreted as (invalid) address to vm86_struct.
185 So the installation check works.
191 /* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
193 if (tsk
->thread
.saved_esp0
)
195 tmp
= copy_from_user(&info
, v86
, VM86_REGS_SIZE1
);
196 tmp
+= copy_from_user(&info
.regs
.VM86_REGS_PART2
, &v86
->regs
.VM86_REGS_PART2
,
197 (long)&info
.regs32
- (long)&info
.regs
.VM86_REGS_PART2
);
201 info
.regs32
= (struct pt_regs
*) &subfunction
;
202 info
.vm86plus
.is_vm86pus
= 1;
203 tsk
->thread
.vm86_info
= (struct vm86_struct
*)v86
;
204 do_sys_vm86(&info
, tsk
);
205 ret
= 0; /* we never return here */
212 static void do_sys_vm86(struct kernel_vm86_struct
*info
, struct task_struct
*tsk
)
214 struct tss_struct
*tss
;
216 * make sure the vm86() system call doesn't try to do anything silly
218 info
->regs
.__null_ds
= 0;
219 info
->regs
.__null_es
= 0;
221 /* we are clearing fs,gs later just before "jmp ret_from_sys_call",
222 * because starting with Linux 2.1.x they aren't no longer saved/restored
226 * The eflags register is also special: we cannot trust that the user
227 * has set it up safely, so this makes sure interrupt etc flags are
228 * inherited from protected mode.
230 VEFLAGS
= info
->regs
.eflags
;
231 info
->regs
.eflags
&= SAFE_MASK
;
232 info
->regs
.eflags
|= info
->regs32
->eflags
& ~SAFE_MASK
;
233 info
->regs
.eflags
|= VM_MASK
;
235 switch (info
->cpu_type
) {
237 tsk
->thread
.v86mask
= 0;
240 tsk
->thread
.v86mask
= NT_MASK
| IOPL_MASK
;
243 tsk
->thread
.v86mask
= AC_MASK
| NT_MASK
| IOPL_MASK
;
246 tsk
->thread
.v86mask
= ID_MASK
| AC_MASK
| NT_MASK
| IOPL_MASK
;
251 * Save old state, set default return value (%eax) to 0
253 info
->regs32
->eax
= 0;
254 tsk
->thread
.saved_esp0
= tsk
->thread
.esp0
;
255 tss
= init_tss
+ smp_processor_id();
256 tss
->esp0
= tsk
->thread
.esp0
= (unsigned long) &info
->VM86_TSS_ESP0
;
258 tsk
->thread
.screen_bitmap
= info
->screen_bitmap
;
259 if (info
->flags
& VM86_SCREEN_BITMAP
)
260 mark_screen_rdonly(tsk
);
262 __asm__
__volatile__(
263 "xorl %%eax,%%eax; movl %%ax,%%fs; movl %%ax,%%gs\n\t"
265 "jmp ret_from_sys_call"
267 :"r" (&info
->regs
), "b" (tsk
) : "ax");
268 /* we never return here */
271 static inline void return_to_32bit(struct kernel_vm86_regs
* regs16
, int retval
)
273 struct pt_regs
* regs32
;
275 regs32
= save_v86_state(regs16
);
276 regs32
->eax
= retval
;
278 __asm__
__volatile__("movl %0,%%esp\n\t"
279 "jmp ret_from_sys_call"
280 : : "r" (regs32
), "b" (current
));
283 static inline void set_IF(struct kernel_vm86_regs
* regs
)
286 if (VEFLAGS
& VIP_MASK
)
287 return_to_32bit(regs
, VM86_STI
);
290 static inline void clear_IF(struct kernel_vm86_regs
* regs
)
292 VEFLAGS
&= ~VIF_MASK
;
295 static inline void clear_TF(struct kernel_vm86_regs
* regs
)
297 regs
->eflags
&= ~TF_MASK
;
300 static inline void set_vflags_long(unsigned long eflags
, struct kernel_vm86_regs
* regs
)
302 set_flags(VEFLAGS
, eflags
, current
->thread
.v86mask
);
303 set_flags(regs
->eflags
, eflags
, SAFE_MASK
);
304 if (eflags
& IF_MASK
)
308 static inline void set_vflags_short(unsigned short flags
, struct kernel_vm86_regs
* regs
)
310 set_flags(VFLAGS
, flags
, current
->thread
.v86mask
);
311 set_flags(regs
->eflags
, flags
, SAFE_MASK
);
316 static inline unsigned long get_vflags(struct kernel_vm86_regs
* regs
)
318 unsigned long flags
= regs
->eflags
& RETURN_MASK
;
320 if (VEFLAGS
& VIF_MASK
)
322 return flags
| (VEFLAGS
& current
->thread
.v86mask
);
325 static inline int is_revectored(int nr
, struct revectored_struct
* bitmap
)
327 __asm__
__volatile__("btl %2,%1\n\tsbbl %0,%0"
329 :"m" (*bitmap
),"r" (nr
));
334 * Boy are these ugly, but we need to do the correct 16-bit arithmetic.
335 * Gcc makes a mess of it, so we do it inline and use non-obvious calling
338 #define pushb(base, ptr, val) \
339 __asm__ __volatile__( \
343 : "r" (base), "q" (val), "0" (ptr))
345 #define pushw(base, ptr, val) \
346 __asm__ __volatile__( \
348 "movb %h2,0(%1,%0)\n\t" \
350 "movb %b2,0(%1,%0)" \
352 : "r" (base), "q" (val), "0" (ptr))
354 #define pushl(base, ptr, val) \
355 __asm__ __volatile__( \
358 "movb %h2,0(%1,%0)\n\t" \
360 "movb %b2,0(%1,%0)\n\t" \
363 "movb %h2,0(%1,%0)\n\t" \
365 "movb %b2,0(%1,%0)" \
367 : "r" (base), "q" (val), "0" (ptr))
369 #define popb(base, ptr) \
370 ({ unsigned long __res; \
371 __asm__ __volatile__( \
372 "movb 0(%1,%0),%b2\n\t" \
374 : "=r" (ptr), "=r" (base), "=q" (__res) \
375 : "0" (ptr), "1" (base), "2" (0)); \
378 #define popw(base, ptr) \
379 ({ unsigned long __res; \
380 __asm__ __volatile__( \
381 "movb 0(%1,%0),%b2\n\t" \
383 "movb 0(%1,%0),%h2\n\t" \
385 : "=r" (ptr), "=r" (base), "=q" (__res) \
386 : "0" (ptr), "1" (base), "2" (0)); \
389 #define popl(base, ptr) \
390 ({ unsigned long __res; \
391 __asm__ __volatile__( \
392 "movb 0(%1,%0),%b2\n\t" \
394 "movb 0(%1,%0),%h2\n\t" \
397 "movb 0(%1,%0),%b2\n\t" \
399 "movb 0(%1,%0),%h2\n\t" \
402 : "=r" (ptr), "=r" (base), "=q" (__res) \
403 : "0" (ptr), "1" (base)); \
406 static void do_int(struct kernel_vm86_regs
*regs
, int i
, unsigned char * ssp
, unsigned long sp
)
408 unsigned long *intr_ptr
, segoffs
;
410 if (regs
->cs
== BIOSSEG
)
412 if (is_revectored(i
, &KVM86
->int_revectored
))
414 if (i
==0x21 && is_revectored(AH(regs
),&KVM86
->int21_revectored
))
416 intr_ptr
= (unsigned long *) (i
<< 2);
417 if (get_user(segoffs
, intr_ptr
))
419 if ((segoffs
>> 16) == BIOSSEG
)
421 pushw(ssp
, sp
, get_vflags(regs
));
422 pushw(ssp
, sp
, regs
->cs
);
423 pushw(ssp
, sp
, IP(regs
));
424 regs
->cs
= segoffs
>> 16;
426 IP(regs
) = segoffs
& 0xffff;
432 return_to_32bit(regs
, VM86_INTx
+ (i
<< 8));
435 /* This must be called with the kernel lock held. */
436 int handle_vm86_trap(struct kernel_vm86_regs
* regs
, long error_code
, int trapno
)
438 if (VMPI
.is_vm86pus
) {
439 if ( (trapno
==3) || (trapno
==1) )
440 return_to_32bit(regs
, VM86_TRAP
+ (trapno
<< 8));
441 do_int(regs
, trapno
, (unsigned char *) (regs
->ss
<< 4), SP(regs
));
445 return 1; /* we let this handle by the calling routine */
446 if (current
->flags
& PF_PTRACED
) {
448 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
449 sigdelset(¤t
->blocked
, SIGTRAP
);
450 recalc_sigpending(current
);
451 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
453 send_sig(SIGTRAP
, current
, 1);
454 current
->thread
.trap_no
= trapno
;
455 current
->thread
.error_code
= error_code
;
459 /* This must be called with the kernel lock held. */
460 void handle_vm86_fault(struct kernel_vm86_regs
* regs
, long error_code
)
462 unsigned char *csp
, *ssp
;
463 unsigned long ip
, sp
;
465 #define CHECK_IF_IN_TRAP \
466 if (VMPI.vm86dbg_active && VMPI.vm86dbg_TFpendig) \
467 pushw(ssp,sp,popw(ssp,sp) | TF_MASK);
468 #define VM86_FAULT_RETURN \
469 if (VMPI.force_return_for_pic && (VEFLAGS & IF_MASK)) \
470 return_to_32bit(regs, VM86_PICRETURN); \
473 csp
= (unsigned char *) (regs
->cs
<< 4);
474 ssp
= (unsigned char *) (regs
->ss
<< 4);
478 switch (popb(csp
, ip
)) {
480 /* operand size override */
482 switch (popb(csp
, ip
)) {
488 pushl(ssp
, sp
, get_vflags(regs
));
496 set_vflags_long(popl(ssp
, sp
), regs
);
502 IP(regs
) = (unsigned short)popl(ssp
, sp
);
503 regs
->cs
= (unsigned short)popl(ssp
, sp
);
505 set_vflags_long(popl(ssp
, sp
), regs
);
507 /* need this to avoid a fallthrough */
509 return_to_32bit(regs
, VM86_UNKNOWN
);
516 pushw(ssp
, sp
, get_vflags(regs
));
524 set_vflags_short(popw(ssp
, sp
), regs
);
529 int intno
=popb(csp
, ip
);
531 if (VMPI
.vm86dbg_active
) {
532 if ( (1 << (intno
&7)) & VMPI
.vm86dbg_intxxtab
[intno
>> 3] )
533 return_to_32bit(regs
, VM86_INTx
+ (intno
<< 8));
535 do_int(regs
, intno
, ssp
, sp
);
542 IP(regs
) = popw(ssp
, sp
);
543 regs
->cs
= popw(ssp
, sp
);
545 set_vflags_short(popw(ssp
, sp
), regs
);
556 * Damn. This is incorrect: the 'sti' instruction should actually
557 * enable interrupts after the /next/ instruction. Not good.
559 * Probably needs some horsing around with the TF flag. Aiee..
567 return_to_32bit(regs
, VM86_UNKNOWN
);
571 /* ---------------- vm86 special IRQ passing stuff ----------------- */
573 #define VM86_IRQNAME "vm86irq"
575 static struct vm86_irqs
{
576 struct task_struct
*tsk
;
578 } vm86_irqs
[16] = {{0},};
579 static int irqbits
=0;
581 #define ALLOWED_SIGS ( 1 /* 0 = don't send a signal */ \
582 | (1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGIO) | (1 << SIGURG) \
585 static void irq_handler(int intno
, void *dev_id
, struct pt_regs
* regs
) {
592 irq_bit
= 1 << intno
;
593 if ((irqbits
& irq_bit
) || ! vm86_irqs
[intno
].tsk
)
596 if (vm86_irqs
[intno
].sig
)
597 send_sig(vm86_irqs
[intno
].sig
, vm86_irqs
[intno
].tsk
, 1);
598 /* else user will poll for IRQs */
600 restore_flags(flags
);
604 static inline void free_vm86_irq(int irqnumber
)
606 free_irq(irqnumber
,0);
607 vm86_irqs
[irqnumber
].tsk
= 0;
608 irqbits
&= ~(1 << irqnumber
);
611 static inline int task_valid(struct task_struct
*tsk
)
613 struct task_struct
*p
;
616 read_lock(&tasklist_lock
);
618 if ((p
== tsk
) && (p
->sig
)) {
623 read_unlock(&tasklist_lock
);
627 static inline void handle_irq_zombies(void)
630 for (i
=3; i
<16; i
++) {
631 if (vm86_irqs
[i
].tsk
) {
632 if (task_valid(vm86_irqs
[i
].tsk
)) continue;
638 static inline int get_and_reset_irq(int irqnumber
)
643 if ( (irqnumber
<3) || (irqnumber
>15) ) return 0;
644 if (vm86_irqs
[irqnumber
].tsk
!= current
) return 0;
647 bit
= irqbits
& (1 << irqnumber
);
649 restore_flags(flags
);
654 static int do_vm86_irq_handling(int subfunction
, int irqnumber
)
657 switch (subfunction
) {
658 case VM86_GET_AND_RESET_IRQ
: {
659 return get_and_reset_irq(irqnumber
);
661 case VM86_GET_IRQ_BITS
: {
664 case VM86_REQUEST_IRQ
: {
665 int sig
= irqnumber
>> 8;
666 int irq
= irqnumber
& 255;
667 handle_irq_zombies();
668 if (!capable(CAP_SYS_ADMIN
)) return -EPERM
;
669 if (!((1 << sig
) & ALLOWED_SIGS
)) return -EPERM
;
670 if ( (irq
<3) || (irq
>15) ) return -EPERM
;
671 if (vm86_irqs
[irq
].tsk
) return -EPERM
;
672 ret
= request_irq(irq
, &irq_handler
, 0, VM86_IRQNAME
, 0);
674 vm86_irqs
[irq
].sig
= sig
;
675 vm86_irqs
[irq
].tsk
= current
;
678 case VM86_FREE_IRQ
: {
679 handle_irq_zombies();
680 if ( (irqnumber
<3) || (irqnumber
>15) ) return -EPERM
;
681 if (!vm86_irqs
[irqnumber
].tsk
) return 0;
682 if (vm86_irqs
[irqnumber
].tsk
!= current
) return -EPERM
;
683 free_vm86_irq(irqnumber
);