⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vm86.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/kernel/vm86.c * *  Copyright (C) 1994  Linus Torvalds */#include <linux/errno.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/signal.h>#include <linux/string.h>#include <linux/ptrace.h>#include <linux/mm.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <asm/uaccess.h>#include <asm/pgalloc.h>#include <asm/io.h>/* * Known problems: * * Interrupt handling is not guaranteed: * - a real x86 will disable all interrupts for one instruction *   after a "mov ss,xx" to make stack handling atomic even without *   the 'lss' instruction. We can't guarantee this in v86 mode, *   as the next instruction might result in a page fault or similar. * - a real x86 will have interrupts disabled for one instruction *   past the 'sti' that enables them. We don't bother with all the *   details yet. * * Let's hope these problems do not actually matter for anything. */#define KVM86	((struct kernel_vm86_struct *)regs)#define VMPI 	KVM86->vm86plus/* * 8- and 16-bit register defines.. */#define AL(regs)	(((unsigned char *)&((regs)->eax))[0])#define AH(regs)	(((unsigned char *)&((regs)->eax))[1])#define IP(regs)	(*(unsigned short *)&((regs)->eip))#define SP(regs)	(*(unsigned short *)&((regs)->esp))/* * virtual flags (16 and 32-bit versions) */#define VFLAGS	(*(unsigned short *)&(current->thread.v86flags))#define VEFLAGS	(current->thread.v86flags)#define set_flags(X,new,mask) \((X) = ((X) & ~(mask)) | ((new) & (mask)))#define SAFE_MASK	(0xDD5)#define RETURN_MASK	(0xDFF)#define VM86_REGS_PART2 orig_eax#define VM86_REGS_SIZE1 \        ( (unsigned)( & (((struct kernel_vm86_regs *)0)->VM86_REGS_PART2) ) )#define VM86_REGS_SIZE2 (sizeof(struct kernel_vm86_regs) - VM86_REGS_SIZE1)asmlinkage struct pt_regs * FASTCALL(save_v86_state(struct kernel_vm86_regs * regs));struct pt_regs * save_v86_state(struct kernel_vm86_regs * regs){	struct tss_struct *tss;	struct pt_regs *ret;	unsigned long tmp;	if (!current->thread.vm86_info) {		printk("no vm86_info: BAD\n");		do_exit(SIGSEGV);	}	set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->thread.v86mask);	tmp = copy_to_user(&current->thread.vm86_info->regs,regs, VM86_REGS_SIZE1);	tmp += copy_to_user(&current->thread.vm86_info->regs.VM86_REGS_PART2,		&regs->VM86_REGS_PART2, VM86_REGS_SIZE2);	tmp += put_user(current->thread.screen_bitmap,&current->thread.vm86_info->screen_bitmap);	if (tmp) {		printk("vm86: could not access userspace vm86_info\n");		do_exit(SIGSEGV);	}	tss = init_tss + smp_processor_id();	tss->esp0 = current->thread.esp0 = current->thread.saved_esp0;	current->thread.saved_esp0 = 0;	ret = KVM86->regs32;	return ret;}static void mark_screen_rdonly(struct task_struct * tsk){	pgd_t *pgd;	pmd_t *pmd;	pte_t *pte;	int i;	pgd = pgd_offset(tsk->mm, 0xA0000);	if (pgd_none(*pgd))		return;	if (pgd_bad(*pgd)) {		pgd_ERROR(*pgd);		pgd_clear(pgd);		return;	}	pmd = pmd_offset(pgd, 0xA0000);	if (pmd_none(*pmd))		return;	if (pmd_bad(*pmd)) {		pmd_ERROR(*pmd);		pmd_clear(pmd);		return;	}	pte = pte_offset(pmd, 0xA0000);	for (i = 0; i < 32; i++) {		if (pte_present(*pte))			set_pte(pte, pte_wrprotect(*pte));		pte++;	}	flush_tlb();}static int do_vm86_irq_handling(int subfunction, int irqnumber);static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk);asmlinkage int sys_vm86old(struct vm86_struct * v86){	struct kernel_vm86_struct info; /* declare this _on top_,					 * this avoids wasting of stack space.					 * This remains on the stack until we					 * return to 32 bit user space.					 */	struct task_struct *tsk;	int tmp, ret = -EPERM;	tsk = current;	if (tsk->thread.saved_esp0)		goto out;	tmp  = copy_from_user(&info, v86, VM86_REGS_SIZE1);	tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,		(long)&info.vm86plus - (long)&info.regs.VM86_REGS_PART2);	ret = -EFAULT;	if (tmp)		goto out;	memset(&info.vm86plus, 0, (int)&info.regs32 - (int)&info.vm86plus);	info.regs32 = (struct pt_regs *) &v86;	tsk->thread.vm86_info = v86;	do_sys_vm86(&info, tsk);	ret = 0;	/* we never return here */out:	return ret;}asmlinkage int sys_vm86(unsigned long subfunction, struct vm86plus_struct * v86){	struct kernel_vm86_struct info; /* declare this _on top_,					 * this avoids wasting of stack space.					 * This remains on the stack until we					 * return to 32 bit user space.					 */	struct task_struct *tsk;	int tmp, ret;	tsk = current;	switch (subfunction) {		case VM86_REQUEST_IRQ:		case VM86_FREE_IRQ:		case VM86_GET_IRQ_BITS:		case VM86_GET_AND_RESET_IRQ:			ret = do_vm86_irq_handling(subfunction,(int)v86);			goto out;		case VM86_PLUS_INSTALL_CHECK:			/* NOTE: on old vm86 stuff this will return the error			   from verify_area(), because the subfunction is			   interpreted as (invalid) address to vm86_struct.			   So the installation check works.			 */			ret = 0;			goto out;	}	/* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */	ret = -EPERM;	if (tsk->thread.saved_esp0)		goto out;	tmp  = copy_from_user(&info, v86, VM86_REGS_SIZE1);	tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,		(long)&info.regs32 - (long)&info.regs.VM86_REGS_PART2);	ret = -EFAULT;	if (tmp)		goto out;	info.regs32 = (struct pt_regs *) &subfunction;	info.vm86plus.is_vm86pus = 1;	tsk->thread.vm86_info = (struct vm86_struct *)v86;	do_sys_vm86(&info, tsk);	ret = 0;	/* we never return here */out:	return ret;}static void do_sys_vm86(struct kernel_vm86_struct *info, struct task_struct *tsk){	struct tss_struct *tss;/* * make sure the vm86() system call doesn't try to do anything silly */	info->regs.__null_ds = 0;	info->regs.__null_es = 0;/* we are clearing fs,gs later just before "jmp ret_from_sys_call", * because starting with Linux 2.1.x they aren't no longer saved/restored *//* * 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 * inherited from protected mode. */ 	VEFLAGS = info->regs.eflags;	info->regs.eflags &= SAFE_MASK;	info->regs.eflags |= info->regs32->eflags & ~SAFE_MASK;	info->regs.eflags |= VM_MASK;	switch (info->cpu_type) {		case CPU_286:			tsk->thread.v86mask = 0;			break;		case CPU_386:			tsk->thread.v86mask = NT_MASK | IOPL_MASK;			break;		case CPU_486:			tsk->thread.v86mask = AC_MASK | NT_MASK | IOPL_MASK;			break;		default:			tsk->thread.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;			break;	}/* * Save old state, set default return value (%eax) to 0 */	info->regs32->eax = 0;	tsk->thread.saved_esp0 = tsk->thread.esp0;	tss = init_tss + smp_processor_id();	tss->esp0 = tsk->thread.esp0 = (unsigned long) &info->VM86_TSS_ESP0;	tsk->thread.screen_bitmap = info->screen_bitmap;	if (info->flags & VM86_SCREEN_BITMAP)		mark_screen_rdonly(tsk);	__asm__ __volatile__(		"xorl %%eax,%%eax; movl %%eax,%%fs; movl %%eax,%%gs\n\t"		"movl %0,%%esp\n\t"		"jmp ret_from_sys_call"		: /* no outputs */		:"r" (&info->regs), "b" (tsk) : "ax");	/* we never return here */}static inline void return_to_32bit(struct kernel_vm86_regs * regs16, int retval){	struct pt_regs * regs32;	regs32 = save_v86_state(regs16);	regs32->eax = retval;	__asm__ __volatile__("movl %0,%%esp\n\t"		"jmp ret_from_sys_call"		: : "r" (regs32), "b" (current));}static inline void set_IF(struct kernel_vm86_regs * regs){	VEFLAGS |= VIF_MASK;	if (VEFLAGS & VIP_MASK)		return_to_32bit(regs, VM86_STI);}static inline void clear_IF(struct kernel_vm86_regs * regs){	VEFLAGS &= ~VIF_MASK;}static inline void clear_TF(struct kernel_vm86_regs * regs){	regs->eflags &= ~TF_MASK;}static inline void set_vflags_long(unsigned long eflags, struct kernel_vm86_regs * regs){	set_flags(VEFLAGS, eflags, current->thread.v86mask);	set_flags(regs->eflags, eflags, SAFE_MASK);	if (eflags & IF_MASK)		set_IF(regs);}static inline void set_vflags_short(unsigned short flags, struct kernel_vm86_regs * regs){	set_flags(VFLAGS, flags, current->thread.v86mask);	set_flags(regs->eflags, flags, SAFE_MASK);	if (flags & IF_MASK)		set_IF(regs);}static inline unsigned long get_vflags(struct kernel_vm86_regs * regs){	unsigned long flags = regs->eflags & RETURN_MASK;	if (VEFLAGS & VIF_MASK)		flags |= IF_MASK;	return flags | (VEFLAGS & current->thread.v86mask);}static inline int is_revectored(int nr, struct revectored_struct * bitmap){	__asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"		:"=r" (nr)		:"m" (*bitmap),"r" (nr));	return nr;}/* * 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 * conventions.. */#define pushb(base, ptr, val) \__asm__ __volatile__( \	"decw %w0\n\t" \	"movb %2,0(%1,%0)" \	: "=r" (ptr) \	: "r" (base), "q" (val), "0" (ptr))#define pushw(base, ptr, val) \__asm__ __volatile__( \	"decw %w0\n\t" \

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -