📄 process_64.c
字号:
/* * Copyright (C) 1995 Linus Torvalds * * Pentium III FXSR, SSE support * Gareth Hughes <gareth@valinux.com>, May 2000 * * X86-64 port * Andi Kleen. * * CPU hotplug support - ashok.raj@intel.com *//* * This file handles the architecture-dependent parts of process handling.. */#include <stdarg.h>#include <linux/cpu.h>#include <linux/errno.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/fs.h>#include <linux/elfcore.h>#include <linux/smp.h>#include <linux/slab.h>#include <linux/user.h>#include <linux/module.h>#include <linux/a.out.h>#include <linux/interrupt.h>#include <linux/delay.h>#include <linux/ptrace.h>#include <linux/utsname.h>#include <linux/random.h>#include <linux/notifier.h>#include <linux/kprobes.h>#include <linux/kdebug.h>#include <linux/tick.h>#include <asm/uaccess.h>#include <asm/pgtable.h>#include <asm/system.h>#include <asm/io.h>#include <asm/processor.h>#include <asm/i387.h>#include <asm/mmu_context.h>#include <asm/pda.h>#include <asm/prctl.h>#include <asm/desc.h>#include <asm/proto.h>#include <asm/ia32.h>#include <asm/idle.h>asmlinkage extern void ret_from_fork(void);unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;unsigned long boot_option_idle_override = 0;EXPORT_SYMBOL(boot_option_idle_override);/* * Powermanagement idle function, if any.. */void (*pm_idle)(void);EXPORT_SYMBOL(pm_idle);static DEFINE_PER_CPU(unsigned int, cpu_idle_state);static ATOMIC_NOTIFIER_HEAD(idle_notifier);void idle_notifier_register(struct notifier_block *n){ atomic_notifier_chain_register(&idle_notifier, n);}EXPORT_SYMBOL_GPL(idle_notifier_register);void idle_notifier_unregister(struct notifier_block *n){ atomic_notifier_chain_unregister(&idle_notifier, n);}EXPORT_SYMBOL(idle_notifier_unregister);void enter_idle(void){ write_pda(isidle, 1); atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);}static void __exit_idle(void){ if (test_and_clear_bit_pda(0, isidle) == 0) return; atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);}/* Called from interrupts to signify idle end */void exit_idle(void){ /* idle loop has pid 0 */ if (current->pid) return; __exit_idle();}/* * We use this if we don't have any better * idle routine.. */static void default_idle(void){ current_thread_info()->status &= ~TS_POLLING; /* * TS_POLLING-cleared state must be visible before we * test NEED_RESCHED: */ smp_mb(); local_irq_disable(); if (!need_resched()) { /* Enables interrupts one instruction before HLT. x86 special cases this so there is no race. */ safe_halt(); } else local_irq_enable(); current_thread_info()->status |= TS_POLLING;}/* * On SMP it's slightly faster (but much more power-consuming!) * to poll the ->need_resched flag instead of waiting for the * cross-CPU IPI to arrive. Use this option with caution. */static void poll_idle (void){ local_irq_enable(); cpu_relax();}static void do_nothing(void *unused){}void cpu_idle_wait(void){ unsigned int cpu, this_cpu = get_cpu(); cpumask_t map, tmp = current->cpus_allowed; set_cpus_allowed(current, cpumask_of_cpu(this_cpu)); put_cpu(); cpus_clear(map); for_each_online_cpu(cpu) { per_cpu(cpu_idle_state, cpu) = 1; cpu_set(cpu, map); } __get_cpu_var(cpu_idle_state) = 0; wmb(); do { ssleep(1); for_each_online_cpu(cpu) { if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu)) cpu_clear(cpu, map); } cpus_and(map, map, cpu_online_map); /* * We waited 1 sec, if a CPU still did not call idle * it may be because it is in idle and not waking up * because it has nothing to do. * Give all the remaining CPUS a kick. */ smp_call_function_mask(map, do_nothing, 0, 0); } while (!cpus_empty(map)); set_cpus_allowed(current, tmp);}EXPORT_SYMBOL_GPL(cpu_idle_wait);#ifdef CONFIG_HOTPLUG_CPUDECLARE_PER_CPU(int, cpu_state);#include <asm/nmi.h>/* We halt the CPU with physical CPU hotplug */static inline void play_dead(void){ idle_task_exit(); wbinvd(); mb(); /* Ack it */ __get_cpu_var(cpu_state) = CPU_DEAD; local_irq_disable(); while (1) halt();}#elsestatic inline void play_dead(void){ BUG();}#endif /* CONFIG_HOTPLUG_CPU *//* * The idle thread. There's no useful work to be * done, so just try to conserve power and have a * low exit latency (ie sit in a loop waiting for * somebody to say that they'd like to reschedule) */void cpu_idle (void){ current_thread_info()->status |= TS_POLLING; /* endless idle loop with no priority at all */ while (1) { while (!need_resched()) { void (*idle)(void); if (__get_cpu_var(cpu_idle_state)) __get_cpu_var(cpu_idle_state) = 0; tick_nohz_stop_sched_tick(); rmb(); idle = pm_idle; if (!idle) idle = default_idle; if (cpu_is_offline(smp_processor_id())) play_dead(); /* * Idle routines should keep interrupts disabled * from here on, until they go to idle. * Otherwise, idle callbacks can misfire. */ local_irq_disable(); enter_idle(); idle(); /* In many cases the interrupt that ended idle has already called exit_idle. But some idle loops can be woken up without interrupt. */ __exit_idle(); } tick_nohz_restart_sched_tick(); preempt_enable_no_resched(); schedule(); preempt_disable(); }}/* * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, * which can obviate IPI to trigger checking of need_resched. * We execute MONITOR against need_resched and enter optimized wait state * through MWAIT. Whenever someone changes need_resched, we would be woken * up from MWAIT (without an IPI). * * New with Core Duo processors, MWAIT can take some hints based on CPU * capability. */void mwait_idle_with_hints(unsigned long eax, unsigned long ecx){ if (!need_resched()) { __monitor((void *)¤t_thread_info()->flags, 0, 0); smp_mb(); if (!need_resched()) __mwait(eax, ecx); }}/* Default MONITOR/MWAIT with no hints, used for default C1 state */static void mwait_idle(void){ if (!need_resched()) { __monitor((void *)¤t_thread_info()->flags, 0, 0); smp_mb(); if (!need_resched()) __sti_mwait(0, 0); else local_irq_enable(); } else { local_irq_enable(); }}void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c){ static int printed; if (cpu_has(c, X86_FEATURE_MWAIT)) { /* * Skip, if setup has overridden idle. * One CPU supports mwait => All CPUs supports mwait */ if (!pm_idle) { if (!printed) { printk(KERN_INFO "using mwait in idle threads.\n"); printed = 1; } pm_idle = mwait_idle; } }}static int __init idle_setup (char *str){ if (!strcmp(str, "poll")) { printk("using polling idle threads.\n"); pm_idle = poll_idle; } else if (!strcmp(str, "mwait")) force_mwait = 1; else return -1; boot_option_idle_override = 1; return 0;}early_param("idle", idle_setup);/* Prints also some state that isn't saved in the pt_regs */ void __show_regs(struct pt_regs * regs){ unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs; unsigned long d0, d1, d2, d3, d6, d7; unsigned int fsindex,gsindex; unsigned int ds,cs,es; printk("\n"); print_modules(); printk("Pid: %d, comm: %.20s %s %s %.*s\n", current->pid, current->comm, print_tainted(), init_utsname()->release, (int)strcspn(init_utsname()->version, " "), init_utsname()->version); printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->rip); printk_address(regs->rip); printk("RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->rsp, regs->eflags); printk("RAX: %016lx RBX: %016lx RCX: %016lx\n", regs->rax, regs->rbx, regs->rcx); printk("RDX: %016lx RSI: %016lx RDI: %016lx\n", regs->rdx, regs->rsi, regs->rdi); printk("RBP: %016lx R08: %016lx R09: %016lx\n", regs->rbp, regs->r8, regs->r9); printk("R10: %016lx R11: %016lx R12: %016lx\n", regs->r10, regs->r11, regs->r12); printk("R13: %016lx R14: %016lx R15: %016lx\n", regs->r13, regs->r14, regs->r15); asm("movl %%ds,%0" : "=r" (ds)); asm("movl %%cs,%0" : "=r" (cs)); asm("movl %%es,%0" : "=r" (es)); asm("movl %%fs,%0" : "=r" (fsindex)); asm("movl %%gs,%0" : "=r" (gsindex)); rdmsrl(MSR_FS_BASE, fs); rdmsrl(MSR_GS_BASE, gs); rdmsrl(MSR_KERNEL_GS_BASE, shadowgs); cr0 = read_cr0(); cr2 = read_cr2(); cr3 = read_cr3(); cr4 = read_cr4(); printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n", fs,fsindex,gs,gsindex,shadowgs); printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0); printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4); get_debugreg(d0, 0); get_debugreg(d1, 1); get_debugreg(d2, 2); printk("DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2); get_debugreg(d3, 3); get_debugreg(d6, 6); get_debugreg(d7, 7); printk("DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);}void show_regs(struct pt_regs *regs){ printk("CPU %d:", smp_processor_id()); __show_regs(regs); show_trace(NULL, regs, (void *)(regs + 1));}/* * Free current thread data structures etc.. */void exit_thread(void){ struct task_struct *me = current; struct thread_struct *t = &me->thread; if (me->thread.io_bitmap_ptr) { struct tss_struct *tss = &per_cpu(init_tss, get_cpu()); kfree(t->io_bitmap_ptr); t->io_bitmap_ptr = NULL; clear_thread_flag(TIF_IO_BITMAP); /* * Careful, clear this in the TSS too: */ memset(tss->io_bitmap, 0xff, t->io_bitmap_max); t->io_bitmap_max = 0; put_cpu(); }}void flush_thread(void){ struct task_struct *tsk = current; if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) { clear_tsk_thread_flag(tsk, TIF_ABI_PENDING); if (test_tsk_thread_flag(tsk, TIF_IA32)) { clear_tsk_thread_flag(tsk, TIF_IA32); } else { set_tsk_thread_flag(tsk, TIF_IA32); current_thread_info()->status |= TS_COMPAT; } } clear_tsk_thread_flag(tsk, TIF_DEBUG); tsk->thread.debugreg0 = 0; tsk->thread.debugreg1 = 0; tsk->thread.debugreg2 = 0; tsk->thread.debugreg3 = 0; tsk->thread.debugreg6 = 0; tsk->thread.debugreg7 = 0; memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); /* * Forget coprocessor state.. */ clear_fpu(tsk); clear_used_math();}void release_thread(struct task_struct *dead_task){ if (dead_task->mm) { if (dead_task->mm->context.size) { printk("WARNING: dead process %8s still has LDT? <%p/%d>\n", dead_task->comm, dead_task->mm->context.ldt, dead_task->mm->context.size); BUG(); } }}static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr){ struct user_desc ud = { .base_addr = addr, .limit = 0xfffff, .seg_32bit = 1, .limit_in_pages = 1, .useable = 1, }; struct n_desc_struct *desc = (void *)t->thread.tls_array;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -