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

📄 process_64.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	desc += tls;	desc->a = LDT_entry_a(&ud); 	desc->b = LDT_entry_b(&ud); }static inline u32 read_32bit_tls(struct task_struct *t, int tls){	struct desc_struct *desc = (void *)t->thread.tls_array;	desc += tls;	return desc->base0 | 		(((u32)desc->base1) << 16) | 		(((u32)desc->base2) << 24);}/* * This gets called before we allocate a new thread and copy * the current task into it. */void prepare_to_copy(struct task_struct *tsk){	unlazy_fpu(tsk);}int copy_thread(int nr, unsigned long clone_flags, unsigned long rsp, 		unsigned long unused,	struct task_struct * p, struct pt_regs * regs){	int err;	struct pt_regs * childregs;	struct task_struct *me = current;	childregs = ((struct pt_regs *)			(THREAD_SIZE + task_stack_page(p))) - 1;	*childregs = *regs;	childregs->rax = 0;	childregs->rsp = rsp;	if (rsp == ~0UL)		childregs->rsp = (unsigned long)childregs;	p->thread.rsp = (unsigned long) childregs;	p->thread.rsp0 = (unsigned long) (childregs+1);	p->thread.userrsp = me->thread.userrsp; 	set_tsk_thread_flag(p, TIF_FORK);	p->thread.fs = me->thread.fs;	p->thread.gs = me->thread.gs;	asm("mov %%gs,%0" : "=m" (p->thread.gsindex));	asm("mov %%fs,%0" : "=m" (p->thread.fsindex));	asm("mov %%es,%0" : "=m" (p->thread.es));	asm("mov %%ds,%0" : "=m" (p->thread.ds));	if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {		p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);		if (!p->thread.io_bitmap_ptr) {			p->thread.io_bitmap_max = 0;			return -ENOMEM;		}		memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,				IO_BITMAP_BYTES);		set_tsk_thread_flag(p, TIF_IO_BITMAP);	} 	/*	 * Set a new TLS for the child thread?	 */	if (clone_flags & CLONE_SETTLS) {#ifdef CONFIG_IA32_EMULATION		if (test_thread_flag(TIF_IA32))			err = ia32_child_tls(p, childregs); 		else 			#endif	 			err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8); 		if (err) 			goto out;	}	err = 0;out:	if (err && p->thread.io_bitmap_ptr) {		kfree(p->thread.io_bitmap_ptr);		p->thread.io_bitmap_max = 0;	}	return err;}/* * This special macro can be used to load a debugging register */#define loaddebug(thread,r) set_debugreg(thread->debugreg ## r, r)static inline void __switch_to_xtra(struct task_struct *prev_p,			     	    struct task_struct *next_p,			     	    struct tss_struct *tss){	struct thread_struct *prev, *next;	prev = &prev_p->thread,	next = &next_p->thread;	if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {		loaddebug(next, 0);		loaddebug(next, 1);		loaddebug(next, 2);		loaddebug(next, 3);		/* no 4 and 5 */		loaddebug(next, 6);		loaddebug(next, 7);	}	if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {		/*		 * Copy the relevant range of the IO bitmap.		 * Normally this is 128 bytes or less:		 */		memcpy(tss->io_bitmap, next->io_bitmap_ptr,		       max(prev->io_bitmap_max, next->io_bitmap_max));	} else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {		/*		 * Clear any possible leftover bits:		 */		memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);	}}/* *	switch_to(x,y) should switch tasks from x to y. * * This could still be optimized:  * - fold all the options into a flag word and test it with a single test. * - could test fs/gs bitsliced * * Kprobes not supported here. Set the probe on schedule instead. */struct task_struct *__switch_to(struct task_struct *prev_p, struct task_struct *next_p){	struct thread_struct *prev = &prev_p->thread,				 *next = &next_p->thread;	int cpu = smp_processor_id();  	struct tss_struct *tss = &per_cpu(init_tss, cpu);	/* we're going to use this soon, after a few expensive things */	if (next_p->fpu_counter>5)		prefetch(&next->i387.fxsave);	/*	 * Reload esp0, LDT and the page table pointer:	 */	tss->rsp0 = next->rsp0;	/* 	 * Switch DS and ES.	 * This won't pick up thread selector changes, but I guess that is ok.	 */	asm volatile("mov %%es,%0" : "=m" (prev->es));	if (unlikely(next->es | prev->es))		loadsegment(es, next->es); 		asm volatile ("mov %%ds,%0" : "=m" (prev->ds));	if (unlikely(next->ds | prev->ds))		loadsegment(ds, next->ds);	load_TLS(next, cpu);	/* 	 * Switch FS and GS.	 */	{ 		unsigned fsindex;		asm volatile("movl %%fs,%0" : "=r" (fsindex)); 		/* segment register != 0 always requires a reload. 		   also reload when it has changed. 		   when prev process used 64bit base always reload		   to avoid an information leak. */		if (unlikely(fsindex | next->fsindex | prev->fs)) {			loadsegment(fs, next->fsindex);			/* check if the user used a selector != 0	                 * if yes clear 64bit base, since overloaded base                         * is always mapped to the Null selector                         */			if (fsindex)			prev->fs = 0;						}		/* when next process has a 64bit base use it */		if (next->fs) 			wrmsrl(MSR_FS_BASE, next->fs); 		prev->fsindex = fsindex;	}	{ 		unsigned gsindex;		asm volatile("movl %%gs,%0" : "=r" (gsindex)); 		if (unlikely(gsindex | next->gsindex | prev->gs)) {			load_gs_index(next->gsindex);			if (gsindex)			prev->gs = 0;						}		if (next->gs)			wrmsrl(MSR_KERNEL_GS_BASE, next->gs); 		prev->gsindex = gsindex;	}	/* Must be after DS reload */	unlazy_fpu(prev_p);	/* 	 * Switch the PDA and FPU contexts.	 */	prev->userrsp = read_pda(oldrsp); 	write_pda(oldrsp, next->userrsp); 	write_pda(pcurrent, next_p); 	write_pda(kernelstack,	(unsigned long)task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);#ifdef CONFIG_CC_STACKPROTECTOR	write_pda(stack_canary, next_p->stack_canary);	/*	 * Build time only check to make sure the stack_canary is at	 * offset 40 in the pda; this is a gcc ABI requirement	 */	BUILD_BUG_ON(offsetof(struct x8664_pda, stack_canary) != 40);#endif	/*	 * Now maybe reload the debug registers and handle I/O bitmaps	 */	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))		__switch_to_xtra(prev_p, next_p, tss);	/* If the task has used fpu the last 5 timeslices, just do a full	 * restore of the math state immediately to avoid the trap; the	 * chances of needing FPU soon are obviously high now	 */	if (next_p->fpu_counter>5)		math_state_restore();	return prev_p;}/* * sys_execve() executes a new program. */asmlinkage long sys_execve(char __user *name, char __user * __user *argv,		char __user * __user *envp, struct pt_regs regs){	long error;	char * filename;	filename = getname(name);	error = PTR_ERR(filename);	if (IS_ERR(filename)) 		return error;	error = do_execve(filename, argv, envp, &regs); 	if (error == 0) {		task_lock(current);		current->ptrace &= ~PT_DTRACE;		task_unlock(current);	}	putname(filename);	return error;}void set_personality_64bit(void){	/* inherit personality from parent */	/* Make sure to be in 64bit mode */	clear_thread_flag(TIF_IA32); 	/* TBD: overwrites user setup. Should have two bits.	   But 64bit processes have always behaved this way,	   so it's not too bad. The main problem is just that   	   32bit childs are affected again. */	current->personality &= ~READ_IMPLIES_EXEC;}asmlinkage long sys_fork(struct pt_regs *regs){	return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);}asmlinkage longsys_clone(unsigned long clone_flags, unsigned long newsp,	  void __user *parent_tid, void __user *child_tid, struct pt_regs *regs){	if (!newsp)		newsp = regs->rsp;	return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);}/* * This is trivial, and on the face of it looks like it * could equally well be done in user mode. * * Not so, for quite unobvious reasons - register pressure. * In user mode vfork() cannot have a stack frame, and if * done by calling the "clone()" system call directly, you * do not have enough call-clobbered registers to hold all * the information you need. */asmlinkage long sys_vfork(struct pt_regs *regs){	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,		    NULL, NULL);}unsigned long get_wchan(struct task_struct *p){	unsigned long stack;	u64 fp,rip;	int count = 0;	if (!p || p == current || p->state==TASK_RUNNING)		return 0; 	stack = (unsigned long)task_stack_page(p);	if (p->thread.rsp < stack || p->thread.rsp > stack+THREAD_SIZE)		return 0;	fp = *(u64 *)(p->thread.rsp);	do { 		if (fp < (unsigned long)stack ||		    fp > (unsigned long)stack+THREAD_SIZE)			return 0; 		rip = *(u64 *)(fp+8); 		if (!in_sched_functions(rip))			return rip; 		fp = *(u64 *)fp; 	} while (count++ < 16); 	return 0;}long do_arch_prctl(struct task_struct *task, int code, unsigned long addr){ 	int ret = 0; 	int doit = task == current;	int cpu;	switch (code) { 	case ARCH_SET_GS:		if (addr >= TASK_SIZE_OF(task))			return -EPERM; 		cpu = get_cpu();		/* handle small bases via the GDT because that's faster to 		   switch. */		if (addr <= 0xffffffff) {  			set_32bit_tls(task, GS_TLS, addr); 			if (doit) { 				load_TLS(&task->thread, cpu);				load_gs_index(GS_TLS_SEL); 			}			task->thread.gsindex = GS_TLS_SEL; 			task->thread.gs = 0;		} else { 			task->thread.gsindex = 0;			task->thread.gs = addr;			if (doit) {				load_gs_index(0);				ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);			} 		}		put_cpu();		break;	case ARCH_SET_FS:		/* Not strictly needed for fs, but do it for symmetry		   with gs */		if (addr >= TASK_SIZE_OF(task))			return -EPERM; 		cpu = get_cpu();		/* handle small bases via the GDT because that's faster to 		   switch. */		if (addr <= 0xffffffff) { 			set_32bit_tls(task, FS_TLS, addr);			if (doit) { 				load_TLS(&task->thread, cpu); 				asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));			}			task->thread.fsindex = FS_TLS_SEL;			task->thread.fs = 0;		} else { 			task->thread.fsindex = 0;			task->thread.fs = addr;			if (doit) {				/* set the selector to 0 to not confuse				   __switch_to */				asm volatile("movl %0,%%fs" :: "r" (0));				ret = checking_wrmsrl(MSR_FS_BASE, addr);			}		}		put_cpu();		break;	case ARCH_GET_FS: { 		unsigned long base; 		if (task->thread.fsindex == FS_TLS_SEL)			base = read_32bit_tls(task, FS_TLS);		else if (doit)			rdmsrl(MSR_FS_BASE, base);		else			base = task->thread.fs;		ret = put_user(base, (unsigned long __user *)addr); 		break; 	}	case ARCH_GET_GS: { 		unsigned long base;		unsigned gsindex;		if (task->thread.gsindex == GS_TLS_SEL)			base = read_32bit_tls(task, GS_TLS);		else if (doit) { 			asm("movl %%gs,%0" : "=r" (gsindex));			if (gsindex)				rdmsrl(MSR_KERNEL_GS_BASE, base);			else				base = task->thread.gs;		}		else			base = task->thread.gs;		ret = put_user(base, (unsigned long __user *)addr); 		break;	}	default:		ret = -EINVAL;		break;	} 	return ret;	} long sys_arch_prctl(int code, unsigned long addr){	return do_arch_prctl(current, code, addr);} /*  * Capture the user space registers if the task is not running (in user space) */int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs){	struct pt_regs *pp, ptregs;	pp = task_pt_regs(tsk);	ptregs = *pp; 	ptregs.cs &= 0xffff;	ptregs.ss &= 0xffff;	elf_core_copy_regs(regs, &ptregs); 	return 1;}unsigned long arch_align_stack(unsigned long sp){	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)		sp -= get_random_int() % 8192;	return sp & ~0xf;}

⌨️ 快捷键说明

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