2 * linux/arch/x86-64/kernel/process.c
4 * Copyright (C) 1995 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
12 * $Id: process.c,v 1.38 2002/01/15 10:08:03 ak Exp $
16 * This file handles the architecture-dependent parts of process handling..
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
25 #include <linux/elfcore.h>
26 #include <linux/smp.h>
27 #include <linux/slab.h>
28 #include <linux/user.h>
29 #include <linux/module.h>
30 #include <linux/a.out.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/irq.h>
34 #include <linux/ptrace.h>
35 #include <linux/version.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/system.h>
41 #include <asm/processor.h>
43 #include <asm/mmu_context.h>
45 #include <asm/prctl.h>
46 #include <asm/kdebug.h>
48 #include <asm/proto.h>
51 asmlinkage
extern void ret_from_fork(void);
53 unsigned long kernel_thread_flags
= CLONE_VM
| CLONE_UNTRACED
;
55 atomic_t hlt_counter
= ATOMIC_INIT(0);
58 * Powermanagement idle function, if any..
60 void (*pm_idle
)(void);
62 void disable_hlt(void)
64 atomic_inc(&hlt_counter
);
67 EXPORT_SYMBOL(disable_hlt
);
71 atomic_dec(&hlt_counter
);
74 EXPORT_SYMBOL(enable_hlt
);
77 * We use this if we don't have any better
80 void default_idle(void)
82 if (!atomic_read(&hlt_counter
)) {
92 * On SMP it's slightly faster (but much more power-consuming!)
93 * to poll the ->need_resched flag instead of waiting for the
94 * cross-CPU IPI to arrive. Use this option with caution.
96 static void poll_idle (void)
103 * Deal with another CPU just having chosen a thread to
106 oldval
= test_and_clear_thread_flag(TIF_NEED_RESCHED
);
109 set_thread_flag(TIF_POLLING_NRFLAG
);
116 "i" (_TIF_NEED_RESCHED
),
117 "m" (current_thread_info()->flags
));
124 * The idle thread. There's no useful work to be
125 * done, so just try to conserve power and have a
126 * low exit latency (ie sit in a loop waiting for
127 * somebody to say that they'd like to reschedule)
131 /* endless idle loop with no priority at all */
133 while (!need_resched()) {
136 * Mark this as an RCU critical section so that
137 * synchronize_kernel() in the unload path waits
138 * for our completion.
152 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
153 * which can obviate IPI to trigger checking of need_resched.
154 * We execute MONITOR against need_resched and enter optimized wait state
155 * through MWAIT. Whenever someone changes need_resched, we would be woken
156 * up from MWAIT (without an IPI).
158 static void mwait_idle(void)
162 if (!need_resched()) {
163 set_thread_flag(TIF_POLLING_NRFLAG
);
165 __monitor((void *)¤t_thread_info()->flags
, 0, 0);
169 } while (!need_resched());
170 clear_thread_flag(TIF_POLLING_NRFLAG
);
174 void __init
select_idle_routine(const struct cpuinfo_x86
*c
)
177 if (cpu_has(c
, X86_FEATURE_MWAIT
)) {
179 * Skip, if setup has overridden idle.
180 * One CPU supports mwait => All CPUs supports mwait
184 printk("using mwait in idle threads.\n");
187 pm_idle
= mwait_idle
;
192 static int __init
idle_setup (char *str
)
194 if (!strncmp(str
, "poll", 4)) {
195 printk("using polling idle threads.\n");
202 __setup("idle=", idle_setup
);
204 /* Prints also some state that isn't saved in the pt_regs */
205 void __show_regs(struct pt_regs
* regs
)
207 unsigned long cr0
= 0L, cr2
= 0L, cr3
= 0L, cr4
= 0L, fs
, gs
, shadowgs
;
208 unsigned int fsindex
,gsindex
;
209 unsigned int ds
,cs
,es
;
213 printk("Pid: %d, comm: %.20s %s %s\n",
214 current
->pid
, current
->comm
, print_tainted(), UTS_RELEASE
);
215 printk("RIP: %04lx:[<%016lx>] ", regs
->cs
& 0xffff, regs
->rip
);
216 printk_address(regs
->rip
);
217 printk("\nRSP: %04lx:%016lx EFLAGS: %08lx\n", regs
->ss
, regs
->rsp
, regs
->eflags
);
218 printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
219 regs
->rax
, regs
->rbx
, regs
->rcx
);
220 printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
221 regs
->rdx
, regs
->rsi
, regs
->rdi
);
222 printk("RBP: %016lx R08: %016lx R09: %016lx\n",
223 regs
->rbp
, regs
->r8
, regs
->r9
);
224 printk("R10: %016lx R11: %016lx R12: %016lx\n",
225 regs
->r10
, regs
->r11
, regs
->r12
);
226 printk("R13: %016lx R14: %016lx R15: %016lx\n",
227 regs
->r13
, regs
->r14
, regs
->r15
);
229 asm("movl %%ds,%0" : "=r" (ds
));
230 asm("movl %%cs,%0" : "=r" (cs
));
231 asm("movl %%es,%0" : "=r" (es
));
232 asm("movl %%fs,%0" : "=r" (fsindex
));
233 asm("movl %%gs,%0" : "=r" (gsindex
));
235 rdmsrl(MSR_FS_BASE
, fs
);
236 rdmsrl(MSR_GS_BASE
, gs
);
237 rdmsrl(MSR_KERNEL_GS_BASE
, shadowgs
);
239 asm("movq %%cr0, %0": "=r" (cr0
));
240 asm("movq %%cr2, %0": "=r" (cr2
));
241 asm("movq %%cr3, %0": "=r" (cr3
));
242 asm("movq %%cr4, %0": "=r" (cr4
));
244 printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
245 fs
,fsindex
,gs
,gsindex
,shadowgs
);
246 printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs
, ds
, es
, cr0
);
247 printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2
, cr3
, cr4
);
250 void show_regs(struct pt_regs
*regs
)
253 show_trace(®s
->rsp
);
257 * Free current thread data structures etc..
259 void exit_thread(void)
261 struct task_struct
*me
= current
;
262 struct thread_struct
*t
= &me
->thread
;
263 if (me
->thread
.io_bitmap_ptr
) {
264 struct tss_struct
*tss
= &per_cpu(init_tss
, get_cpu());
266 kfree(t
->io_bitmap_ptr
);
267 t
->io_bitmap_ptr
= NULL
;
269 * Careful, clear this in the TSS too:
271 memset(tss
->io_bitmap
, 0xff, t
->io_bitmap_max
);
272 t
->io_bitmap_max
= 0;
277 void flush_thread(void)
279 struct task_struct
*tsk
= current
;
280 struct thread_info
*t
= current_thread_info();
282 if (t
->flags
& _TIF_ABI_PENDING
)
283 t
->flags
^= (_TIF_ABI_PENDING
| _TIF_IA32
);
285 tsk
->thread
.debugreg0
= 0;
286 tsk
->thread
.debugreg1
= 0;
287 tsk
->thread
.debugreg2
= 0;
288 tsk
->thread
.debugreg3
= 0;
289 tsk
->thread
.debugreg6
= 0;
290 tsk
->thread
.debugreg7
= 0;
291 memset(tsk
->thread
.tls_array
, 0, sizeof(tsk
->thread
.tls_array
));
293 * Forget coprocessor state..
299 void release_thread(struct task_struct
*dead_task
)
302 if (dead_task
->mm
->context
.size
) {
303 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
305 dead_task
->mm
->context
.ldt
,
306 dead_task
->mm
->context
.size
);
312 static inline void set_32bit_tls(struct task_struct
*t
, int tls
, u32 addr
)
314 struct user_desc ud
= {
321 struct n_desc_struct
*desc
= (void *)t
->thread
.tls_array
;
323 desc
->a
= LDT_entry_a(&ud
);
324 desc
->b
= LDT_entry_b(&ud
);
327 static inline u32
read_32bit_tls(struct task_struct
*t
, int tls
)
329 struct desc_struct
*desc
= (void *)t
->thread
.tls_array
;
332 (((u32
)desc
->base1
) << 16) |
333 (((u32
)desc
->base2
) << 24);
337 * This gets called before we allocate a new thread and copy
338 * the current task into it.
340 void prepare_to_copy(struct task_struct
*tsk
)
345 int copy_thread(int nr
, unsigned long clone_flags
, unsigned long rsp
,
346 unsigned long unused
,
347 struct task_struct
* p
, struct pt_regs
* regs
)
350 struct pt_regs
* childregs
;
351 struct task_struct
*me
= current
;
353 childregs
= ((struct pt_regs
*) (THREAD_SIZE
+ (unsigned long) p
->thread_info
)) - 1;
358 childregs
->rsp
= rsp
;
360 childregs
->rsp
= (unsigned long)childregs
;
362 p
->set_child_tid
= p
->clear_child_tid
= NULL
;
364 p
->thread
.rsp
= (unsigned long) childregs
;
365 p
->thread
.rsp0
= (unsigned long) (childregs
+1);
366 p
->thread
.userrsp
= me
->thread
.userrsp
;
368 set_ti_thread_flag(p
->thread_info
, TIF_FORK
);
370 p
->thread
.fs
= me
->thread
.fs
;
371 p
->thread
.gs
= me
->thread
.gs
;
373 asm("movl %%gs,%0" : "=m" (p
->thread
.gsindex
));
374 asm("movl %%fs,%0" : "=m" (p
->thread
.fsindex
));
375 asm("movl %%es,%0" : "=m" (p
->thread
.es
));
376 asm("movl %%ds,%0" : "=m" (p
->thread
.ds
));
378 if (unlikely(me
->thread
.io_bitmap_ptr
!= NULL
)) {
379 p
->thread
.io_bitmap_ptr
= kmalloc(IO_BITMAP_BYTES
, GFP_KERNEL
);
380 if (!p
->thread
.io_bitmap_ptr
) {
381 p
->thread
.io_bitmap_max
= 0;
384 memcpy(p
->thread
.io_bitmap_ptr
, me
->thread
.io_bitmap_ptr
, IO_BITMAP_BYTES
);
388 * Set a new TLS for the child thread?
390 if (clone_flags
& CLONE_SETTLS
) {
391 #ifdef CONFIG_IA32_EMULATION
392 if (test_thread_flag(TIF_IA32
))
393 err
= ia32_child_tls(p
, childregs
);
396 err
= do_arch_prctl(p
, ARCH_SET_FS
, childregs
->r8
);
402 if (err
&& p
->thread
.io_bitmap_ptr
) {
403 kfree(p
->thread
.io_bitmap_ptr
);
404 p
->thread
.io_bitmap_max
= 0;
410 * This special macro can be used to load a debugging register
412 #define loaddebug(thread,r) set_debug(thread->debugreg ## r, r)
415 * switch_to(x,y) should switch tasks from x to y.
417 * This could still be optimized:
418 * - fold all the options into a flag word and test it with a single test.
419 * - could test fs/gs bitsliced
421 struct task_struct
*__switch_to(struct task_struct
*prev_p
, struct task_struct
*next_p
)
423 struct thread_struct
*prev
= &prev_p
->thread
,
424 *next
= &next_p
->thread
;
425 int cpu
= smp_processor_id();
426 struct tss_struct
*tss
= &per_cpu(init_tss
, cpu
);
431 * Reload esp0, LDT and the page table pointer:
433 tss
->rsp0
= next
->rsp0
;
437 * This won't pick up thread selector changes, but I guess that is ok.
439 asm volatile("movl %%es,%0" : "=m" (prev
->es
));
440 if (unlikely(next
->es
| prev
->es
))
441 loadsegment(es
, next
->es
);
443 asm volatile ("movl %%ds,%0" : "=m" (prev
->ds
));
444 if (unlikely(next
->ds
| prev
->ds
))
445 loadsegment(ds
, next
->ds
);
454 asm volatile("movl %%fs,%0" : "=g" (fsindex
));
455 /* segment register != 0 always requires a reload.
456 also reload when it has changed.
457 when prev process used 64bit base always reload
458 to avoid an information leak. */
459 if (unlikely(fsindex
| next
->fsindex
| prev
->fs
)) {
460 loadsegment(fs
, next
->fsindex
);
461 /* check if the user used a selector != 0
462 * if yes clear 64bit base, since overloaded base
463 * is always mapped to the Null selector
468 /* when next process has a 64bit base use it */
470 wrmsrl(MSR_FS_BASE
, next
->fs
);
471 prev
->fsindex
= fsindex
;
475 asm volatile("movl %%gs,%0" : "=g" (gsindex
));
476 if (unlikely(gsindex
| next
->gsindex
| prev
->gs
)) {
477 load_gs_index(next
->gsindex
);
482 wrmsrl(MSR_KERNEL_GS_BASE
, next
->gs
);
483 prev
->gsindex
= gsindex
;
487 * Switch the PDA context.
489 prev
->userrsp
= read_pda(oldrsp
);
490 write_pda(oldrsp
, next
->userrsp
);
491 write_pda(pcurrent
, next_p
);
492 write_pda(kernelstack
, (unsigned long)next_p
->thread_info
+ THREAD_SIZE
- PDA_STACKOFFSET
);
495 * Now maybe reload the debug registers
497 if (unlikely(next
->debugreg7
)) {
509 * Handle the IO bitmap
511 if (unlikely(prev
->io_bitmap_ptr
|| next
->io_bitmap_ptr
)) {
512 if (next
->io_bitmap_ptr
)
514 * Copy the relevant range of the IO bitmap.
515 * Normally this is 128 bytes or less:
517 memcpy(tss
->io_bitmap
, next
->io_bitmap_ptr
,
518 max(prev
->io_bitmap_max
, next
->io_bitmap_max
));
521 * Clear any possible leftover bits:
523 memset(tss
->io_bitmap
, 0xff, prev
->io_bitmap_max
);
531 * sys_execve() executes a new program.
534 long sys_execve(char __user
*name
, char __user
* __user
*argv
,
535 char __user
* __user
*envp
, struct pt_regs regs
)
540 filename
= getname(name
);
541 error
= PTR_ERR(filename
);
542 if (IS_ERR(filename
))
544 error
= do_execve(filename
, argv
, envp
, ®s
);
546 current
->ptrace
&= ~PT_DTRACE
;
551 void set_personality_64bit(void)
553 /* inherit personality from parent */
555 /* Make sure to be in 64bit mode */
556 clear_thread_flag(TIF_IA32
);
559 asmlinkage
long sys_fork(struct pt_regs
*regs
)
561 return do_fork(SIGCHLD
, regs
->rsp
, regs
, 0, NULL
, NULL
);
564 asmlinkage
long sys_clone(unsigned long clone_flags
, unsigned long newsp
, void __user
*parent_tid
, void __user
*child_tid
, struct pt_regs
*regs
)
568 return do_fork(clone_flags
, newsp
, regs
, 0, parent_tid
, child_tid
);
572 * This is trivial, and on the face of it looks like it
573 * could equally well be done in user mode.
575 * Not so, for quite unobvious reasons - register pressure.
576 * In user mode vfork() cannot have a stack frame, and if
577 * done by calling the "clone()" system call directly, you
578 * do not have enough call-clobbered registers to hold all
579 * the information you need.
581 asmlinkage
long sys_vfork(struct pt_regs
*regs
)
583 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->rsp
, regs
, 0,
587 unsigned long get_wchan(struct task_struct
*p
)
593 if (!p
|| p
== current
|| p
->state
==TASK_RUNNING
)
595 stack
= (unsigned long)p
->thread_info
;
596 if (p
->thread
.rsp
< stack
|| p
->thread
.rsp
> stack
+THREAD_SIZE
)
598 fp
= *(u64
*)(p
->thread
.rsp
);
600 if (fp
< (unsigned long)stack
|| fp
> (unsigned long)stack
+THREAD_SIZE
)
602 rip
= *(u64
*)(fp
+8);
603 if (!in_sched_functions(rip
))
606 } while (count
++ < 16);
610 long do_arch_prctl(struct task_struct
*task
, int code
, unsigned long addr
)
613 int doit
= task
== current
;
618 if (addr
>= TASK_SIZE
)
621 /* handle small bases via the GDT because that's faster to
623 if (addr
<= 0xffffffff) {
624 set_32bit_tls(task
, GS_TLS
, addr
);
626 load_TLS(&task
->thread
, cpu
);
627 load_gs_index(GS_TLS_SEL
);
629 task
->thread
.gsindex
= GS_TLS_SEL
;
632 task
->thread
.gsindex
= 0;
633 task
->thread
.gs
= addr
;
636 ret
= checking_wrmsrl(MSR_KERNEL_GS_BASE
, addr
);
642 /* Not strictly needed for fs, but do it for symmetry
644 if (addr
>= TASK_SIZE
)
647 /* handle small bases via the GDT because that's faster to
649 if (addr
<= 0xffffffff) {
650 set_32bit_tls(task
, FS_TLS
, addr
);
652 load_TLS(&task
->thread
, cpu
);
653 asm volatile("movl %0,%%fs" :: "r" (FS_TLS_SEL
));
655 task
->thread
.fsindex
= FS_TLS_SEL
;
658 task
->thread
.fsindex
= 0;
659 task
->thread
.fs
= addr
;
661 /* set the selector to 0 to not confuse
663 asm volatile("movl %0,%%fs" :: "r" (0));
664 ret
= checking_wrmsrl(MSR_FS_BASE
, addr
);
671 if (task
->thread
.fsindex
== FS_TLS_SEL
)
672 base
= read_32bit_tls(task
, FS_TLS
);
674 rdmsrl(MSR_FS_BASE
, base
);
676 base
= task
->thread
.fs
;
677 ret
= put_user(base
, (unsigned long __user
*)addr
);
682 if (task
->thread
.gsindex
== GS_TLS_SEL
)
683 base
= read_32bit_tls(task
, GS_TLS
);
685 rdmsrl(MSR_KERNEL_GS_BASE
, base
);
687 base
= task
->thread
.gs
;
688 ret
= put_user(base
, (unsigned long __user
*)addr
);
700 long sys_arch_prctl(int code
, unsigned long addr
)
702 return do_arch_prctl(current
, code
, addr
);
706 * Capture the user space registers if the task is not running (in user space)
708 int dump_task_regs(struct task_struct
*tsk
, elf_gregset_t
*regs
)
710 struct pt_regs
*pp
, ptregs
;
712 pp
= (struct pt_regs
*)(tsk
->thread
.rsp0
);
719 elf_core_copy_regs(regs
, &ptregs
);