hw-breakpoints: use the new wrapper routines to access debug registers in process...
[linux-2.6-block.git] / arch / x86 / kernel / process_64.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1995 Linus Torvalds
3 *
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6612538c 6 *
1da177e4
LT
7 * X86-64 port
8 * Andi Kleen.
76e4f660
AR
9 *
10 * CPU hotplug support - ashok.raj@intel.com
1da177e4
LT
11 */
12
13/*
14 * This file handles the architecture-dependent parts of process handling..
15 */
16
17#include <stdarg.h>
18
42059429 19#include <linux/stackprotector.h>
76e4f660 20#include <linux/cpu.h>
1da177e4
LT
21#include <linux/errno.h>
22#include <linux/sched.h>
6612538c 23#include <linux/fs.h>
1da177e4
LT
24#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/elfcore.h>
27#include <linux/smp.h>
28#include <linux/slab.h>
29#include <linux/user.h>
1da177e4 30#include <linux/interrupt.h>
6612538c 31#include <linux/utsname.h>
1da177e4 32#include <linux/delay.h>
6612538c 33#include <linux/module.h>
1da177e4 34#include <linux/ptrace.h>
1da177e4 35#include <linux/random.h>
95833c83 36#include <linux/notifier.h>
c6fd91f0 37#include <linux/kprobes.h>
1eeb66a1 38#include <linux/kdebug.h>
02290683 39#include <linux/tick.h>
529e25f6 40#include <linux/prctl.h>
7de08b4e
GP
41#include <linux/uaccess.h>
42#include <linux/io.h>
8b96f011 43#include <linux/ftrace.h>
48ec4d95 44#include <linux/dmi.h>
1da177e4 45
1da177e4
LT
46#include <asm/pgtable.h>
47#include <asm/system.h>
1da177e4
LT
48#include <asm/processor.h>
49#include <asm/i387.h>
50#include <asm/mmu_context.h>
1da177e4 51#include <asm/prctl.h>
1da177e4
LT
52#include <asm/desc.h>
53#include <asm/proto.h>
54#include <asm/ia32.h>
95833c83 55#include <asm/idle.h>
bbc1f698 56#include <asm/syscalls.h>
bf53de90 57#include <asm/ds.h>
66cb5917
P
58#include <asm/debugreg.h>
59#include <asm/hw_breakpoint.h>
1da177e4
LT
60
61asmlinkage extern void ret_from_fork(void);
62
c6f5e0ac
BG
63DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
64EXPORT_PER_CPU_SYMBOL(current_task);
65
3d1e42a7 66DEFINE_PER_CPU(unsigned long, old_rsp);
c2558e0e 67static DEFINE_PER_CPU(unsigned char, is_idle);
3d1e42a7 68
1da177e4
LT
69unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
70
e041c683 71static ATOMIC_NOTIFIER_HEAD(idle_notifier);
95833c83
AK
72
73void idle_notifier_register(struct notifier_block *n)
74{
e041c683 75 atomic_notifier_chain_register(&idle_notifier, n);
95833c83 76}
c7d87d79
VP
77EXPORT_SYMBOL_GPL(idle_notifier_register);
78
79void idle_notifier_unregister(struct notifier_block *n)
80{
81 atomic_notifier_chain_unregister(&idle_notifier, n);
82}
83EXPORT_SYMBOL_GPL(idle_notifier_unregister);
95833c83 84
95833c83
AK
85void enter_idle(void)
86{
c2558e0e 87 percpu_write(is_idle, 1);
e041c683 88 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
95833c83
AK
89}
90
91static void __exit_idle(void)
92{
c2558e0e 93 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
a15da49d 94 return;
e041c683 95 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
95833c83
AK
96}
97
98/* Called from interrupts to signify idle end */
99void exit_idle(void)
100{
a15da49d
AK
101 /* idle loop has pid 0 */
102 if (current->pid)
95833c83
AK
103 return;
104 __exit_idle();
105}
106
913da64b 107#ifndef CONFIG_SMP
76e4f660
AR
108static inline void play_dead(void)
109{
110 BUG();
111}
913da64b 112#endif
76e4f660 113
1da177e4
LT
114/*
115 * The idle thread. There's no useful work to be
116 * done, so just try to conserve power and have a
117 * low exit latency (ie sit in a loop waiting for
118 * somebody to say that they'd like to reschedule)
119 */
b10db7f0 120void cpu_idle(void)
1da177e4 121{
495ab9c0 122 current_thread_info()->status |= TS_POLLING;
ce22bd92 123
ce22bd92 124 /*
5c79d2a5
TH
125 * If we're the non-boot CPU, nothing set the stack canary up
126 * for us. CPU0 already has it initialized but no harm in
127 * doing it again. This is a good place for updating it, as
128 * we wont ever return from this function (so the invalid
129 * canaries already on the stack wont ever trigger).
ce22bd92 130 */
18aa8bb1
IM
131 boot_init_stack_canary();
132
1da177e4
LT
133 /* endless idle loop with no priority at all */
134 while (1) {
b8f8c3cf 135 tick_nohz_stop_sched_tick(1);
1da177e4 136 while (!need_resched()) {
1da177e4 137
1da177e4 138 rmb();
6ddd2a27 139
76e4f660
AR
140 if (cpu_is_offline(smp_processor_id()))
141 play_dead();
d331e739
VP
142 /*
143 * Idle routines should keep interrupts disabled
144 * from here on, until they go to idle.
145 * Otherwise, idle callbacks can misfire.
146 */
147 local_irq_disable();
95833c83 148 enter_idle();
81d68a96
SR
149 /* Don't trace irqs off for idle */
150 stop_critical_timings();
6ddd2a27 151 pm_idle();
81d68a96 152 start_critical_timings();
a15da49d
AK
153 /* In many cases the interrupt that ended idle
154 has already called exit_idle. But some idle
155 loops can be woken up without interrupt. */
95833c83 156 __exit_idle();
1da177e4
LT
157 }
158
02290683 159 tick_nohz_restart_sched_tick();
5bfb5d69 160 preempt_enable_no_resched();
1da177e4 161 schedule();
5bfb5d69 162 preempt_disable();
1da177e4
LT
163 }
164}
165
6612538c 166/* Prints also some state that isn't saved in the pt_regs */
e2ce07c8 167void __show_regs(struct pt_regs *regs, int all)
1da177e4
LT
168{
169 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
bb1995d5 170 unsigned long d0, d1, d2, d3, d6, d7;
6612538c
HS
171 unsigned int fsindex, gsindex;
172 unsigned int ds, cs, es;
48ec4d95 173 const char *board;
1da177e4
LT
174
175 printk("\n");
176 print_modules();
48ec4d95
KM
177 board = dmi_get_system_info(DMI_PRODUCT_NAME);
178 if (!board)
179 board = "";
180 printk(KERN_INFO "Pid: %d, comm: %.20s %s %s %.*s %s\n",
9acf23c4 181 current->pid, current->comm, print_tainted(),
96b644bd
SH
182 init_utsname()->release,
183 (int)strcspn(init_utsname()->version, " "),
48ec4d95 184 init_utsname()->version, board);
8092c654 185 printk(KERN_INFO "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
aafbd7eb 186 printk_address(regs->ip, 1);
8092c654
GP
187 printk(KERN_INFO "RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss,
188 regs->sp, regs->flags);
189 printk(KERN_INFO "RAX: %016lx RBX: %016lx RCX: %016lx\n",
65ea5b03 190 regs->ax, regs->bx, regs->cx);
8092c654 191 printk(KERN_INFO "RDX: %016lx RSI: %016lx RDI: %016lx\n",
65ea5b03 192 regs->dx, regs->si, regs->di);
8092c654 193 printk(KERN_INFO "RBP: %016lx R08: %016lx R09: %016lx\n",
65ea5b03 194 regs->bp, regs->r8, regs->r9);
8092c654 195 printk(KERN_INFO "R10: %016lx R11: %016lx R12: %016lx\n",
7de08b4e 196 regs->r10, regs->r11, regs->r12);
8092c654 197 printk(KERN_INFO "R13: %016lx R14: %016lx R15: %016lx\n",
7de08b4e 198 regs->r13, regs->r14, regs->r15);
1da177e4 199
7de08b4e
GP
200 asm("movl %%ds,%0" : "=r" (ds));
201 asm("movl %%cs,%0" : "=r" (cs));
202 asm("movl %%es,%0" : "=r" (es));
1da177e4
LT
203 asm("movl %%fs,%0" : "=r" (fsindex));
204 asm("movl %%gs,%0" : "=r" (gsindex));
205
206 rdmsrl(MSR_FS_BASE, fs);
7de08b4e
GP
207 rdmsrl(MSR_GS_BASE, gs);
208 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
1da177e4 209
e2ce07c8
PE
210 if (!all)
211 return;
1da177e4 212
f51c9452
GOC
213 cr0 = read_cr0();
214 cr2 = read_cr2();
215 cr3 = read_cr3();
216 cr4 = read_cr4();
1da177e4 217
8092c654 218 printk(KERN_INFO "FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
7de08b4e 219 fs, fsindex, gs, gsindex, shadowgs);
8092c654
GP
220 printk(KERN_INFO "CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
221 es, cr0);
222 printk(KERN_INFO "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
223 cr4);
bb1995d5
AS
224
225 get_debugreg(d0, 0);
226 get_debugreg(d1, 1);
227 get_debugreg(d2, 2);
8092c654 228 printk(KERN_INFO "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
bb1995d5
AS
229 get_debugreg(d3, 3);
230 get_debugreg(d6, 6);
231 get_debugreg(d7, 7);
8092c654 232 printk(KERN_INFO "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
1da177e4
LT
233}
234
235void show_regs(struct pt_regs *regs)
236{
8092c654 237 printk(KERN_INFO "CPU %d:", smp_processor_id());
e2ce07c8 238 __show_regs(regs, 1);
bc850d6b 239 show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
1da177e4
LT
240}
241
1da177e4
LT
242void release_thread(struct task_struct *dead_task)
243{
244 if (dead_task->mm) {
245 if (dead_task->mm->context.size) {
246 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
247 dead_task->comm,
248 dead_task->mm->context.ldt,
249 dead_task->mm->context.size);
250 BUG();
251 }
252 }
66cb5917
P
253 if (unlikely(dead_task->thread.debugreg7))
254 flush_thread_hw_breakpoint(dead_task);
1da177e4
LT
255}
256
257static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
258{
6612538c 259 struct user_desc ud = {
1da177e4
LT
260 .base_addr = addr,
261 .limit = 0xfffff,
262 .seg_32bit = 1,
263 .limit_in_pages = 1,
264 .useable = 1,
265 };
ade1af77 266 struct desc_struct *desc = t->thread.tls_array;
1da177e4 267 desc += tls;
80fbb69a 268 fill_ldt(desc, &ud);
1da177e4
LT
269}
270
271static inline u32 read_32bit_tls(struct task_struct *t, int tls)
272{
91394eb0 273 return get_desc_base(&t->thread.tls_array[tls]);
1da177e4
LT
274}
275
276/*
277 * This gets called before we allocate a new thread and copy
278 * the current task into it.
279 */
280void prepare_to_copy(struct task_struct *tsk)
281{
282 unlazy_fpu(tsk);
283}
284
6f2c55b8 285int copy_thread(unsigned long clone_flags, unsigned long sp,
1da177e4 286 unsigned long unused,
7de08b4e 287 struct task_struct *p, struct pt_regs *regs)
1da177e4
LT
288{
289 int err;
7de08b4e 290 struct pt_regs *childregs;
1da177e4
LT
291 struct task_struct *me = current;
292
a88cde13 293 childregs = ((struct pt_regs *)
57eafdc2 294 (THREAD_SIZE + task_stack_page(p))) - 1;
1da177e4
LT
295 *childregs = *regs;
296
65ea5b03
PA
297 childregs->ax = 0;
298 childregs->sp = sp;
299 if (sp == ~0UL)
300 childregs->sp = (unsigned long)childregs;
1da177e4 301
faca6227
PA
302 p->thread.sp = (unsigned long) childregs;
303 p->thread.sp0 = (unsigned long) (childregs+1);
304 p->thread.usersp = me->thread.usersp;
1da177e4 305
e4f17c43 306 set_tsk_thread_flag(p, TIF_FORK);
1da177e4
LT
307
308 p->thread.fs = me->thread.fs;
309 p->thread.gs = me->thread.gs;
66cb5917 310 p->thread.io_bitmap_ptr = NULL;
1da177e4 311
ada85708
JF
312 savesegment(gs, p->thread.gsindex);
313 savesegment(fs, p->thread.fsindex);
314 savesegment(es, p->thread.es);
315 savesegment(ds, p->thread.ds);
1da177e4 316
66cb5917
P
317 err = -ENOMEM;
318 if (unlikely(test_tsk_thread_flag(me, TIF_DEBUG)))
319 if (copy_thread_hw_breakpoint(me, p, clone_flags))
320 goto out;
321
d3a4f48d 322 if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
1da177e4
LT
323 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
324 if (!p->thread.io_bitmap_ptr) {
325 p->thread.io_bitmap_max = 0;
326 return -ENOMEM;
327 }
a88cde13
AK
328 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
329 IO_BITMAP_BYTES);
d3a4f48d 330 set_tsk_thread_flag(p, TIF_IO_BITMAP);
6612538c 331 }
1da177e4
LT
332
333 /*
334 * Set a new TLS for the child thread?
335 */
336 if (clone_flags & CLONE_SETTLS) {
337#ifdef CONFIG_IA32_EMULATION
338 if (test_thread_flag(TIF_IA32))
efd1ca52 339 err = do_set_thread_area(p, -1,
65ea5b03 340 (struct user_desc __user *)childregs->si, 0);
7de08b4e
GP
341 else
342#endif
343 err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
344 if (err)
1da177e4
LT
345 goto out;
346 }
bf53de90 347
2311f0de
MM
348 clear_tsk_thread_flag(p, TIF_DS_AREA_MSR);
349 p->thread.ds_ctx = NULL;
bf53de90
MM
350
351 clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR);
352 p->thread.debugctlmsr = 0;
353
1da177e4
LT
354 err = 0;
355out:
356 if (err && p->thread.io_bitmap_ptr) {
357 kfree(p->thread.io_bitmap_ptr);
358 p->thread.io_bitmap_max = 0;
359 }
66cb5917
P
360 if (err)
361 flush_thread_hw_breakpoint(p);
362
1da177e4
LT
363 return err;
364}
365
513ad84b
IM
366void
367start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
368{
ada85708
JF
369 loadsegment(fs, 0);
370 loadsegment(es, 0);
371 loadsegment(ds, 0);
513ad84b
IM
372 load_gs_index(0);
373 regs->ip = new_ip;
374 regs->sp = new_sp;
3d1e42a7 375 percpu_write(old_rsp, new_sp);
513ad84b
IM
376 regs->cs = __USER_CS;
377 regs->ss = __USER_DS;
378 regs->flags = 0x200;
379 set_fs(USER_DS);
aa283f49
SS
380 /*
381 * Free the old FP and other extended state
382 */
383 free_thread_xstate(current);
513ad84b
IM
384}
385EXPORT_SYMBOL_GPL(start_thread);
386
1da177e4
LT
387/*
388 * switch_to(x,y) should switch tasks from x to y.
389 *
6612538c 390 * This could still be optimized:
1da177e4
LT
391 * - fold all the options into a flag word and test it with a single test.
392 * - could test fs/gs bitsliced
099f318b
AK
393 *
394 * Kprobes not supported here. Set the probe on schedule instead.
8b96f011 395 * Function graph tracer not supported too.
1da177e4 396 */
8b96f011 397__notrace_funcgraph struct task_struct *
a88cde13 398__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
1da177e4 399{
87b935a0
JF
400 struct thread_struct *prev = &prev_p->thread;
401 struct thread_struct *next = &next_p->thread;
6612538c 402 int cpu = smp_processor_id();
1da177e4 403 struct tss_struct *tss = &per_cpu(init_tss, cpu);
478de5a9 404 unsigned fsindex, gsindex;
1da177e4 405
e07e23e1 406 /* we're going to use this soon, after a few expensive things */
7de08b4e 407 if (next_p->fpu_counter > 5)
61c4628b 408 prefetch(next->xstate);
e07e23e1 409
1da177e4
LT
410 /*
411 * Reload esp0, LDT and the page table pointer:
412 */
7818a1e0 413 load_sp0(tss, next);
1da177e4 414
7de08b4e 415 /*
1da177e4
LT
416 * Switch DS and ES.
417 * This won't pick up thread selector changes, but I guess that is ok.
418 */
ada85708 419 savesegment(es, prev->es);
1da177e4 420 if (unlikely(next->es | prev->es))
7de08b4e 421 loadsegment(es, next->es);
ada85708
JF
422
423 savesegment(ds, prev->ds);
1da177e4
LT
424 if (unlikely(next->ds | prev->ds))
425 loadsegment(ds, next->ds);
426
478de5a9
JF
427
428 /* We must save %fs and %gs before load_TLS() because
429 * %fs and %gs may be cleared by load_TLS().
430 *
431 * (e.g. xen_load_tls())
432 */
433 savesegment(fs, fsindex);
434 savesegment(gs, gsindex);
435
1da177e4
LT
436 load_TLS(next, cpu);
437
3fe0a63e
JF
438 /*
439 * Leave lazy mode, flushing any hypercalls made here.
440 * This must be done before restoring TLS segments so
441 * the GDT and LDT are properly updated, and must be
442 * done before math_state_restore, so the TS bit is up
443 * to date.
444 */
445 arch_leave_lazy_cpu_mode();
446
7de08b4e 447 /*
1da177e4 448 * Switch FS and GS.
87b935a0
JF
449 *
450 * Segment register != 0 always requires a reload. Also
451 * reload when it has changed. When prev process used 64bit
452 * base always reload to avoid an information leak.
1da177e4 453 */
87b935a0
JF
454 if (unlikely(fsindex | next->fsindex | prev->fs)) {
455 loadsegment(fs, next->fsindex);
7de08b4e 456 /*
87b935a0
JF
457 * Check if the user used a selector != 0; if yes
458 * clear 64bit base, since overloaded base is always
459 * mapped to the Null selector
460 */
461 if (fsindex)
7de08b4e 462 prev->fs = 0;
1da177e4 463 }
87b935a0
JF
464 /* when next process has a 64bit base use it */
465 if (next->fs)
466 wrmsrl(MSR_FS_BASE, next->fs);
467 prev->fsindex = fsindex;
468
469 if (unlikely(gsindex | next->gsindex | prev->gs)) {
470 load_gs_index(next->gsindex);
471 if (gsindex)
7de08b4e 472 prev->gs = 0;
1da177e4 473 }
87b935a0
JF
474 if (next->gs)
475 wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
476 prev->gsindex = gsindex;
1da177e4 477
0a5ace2a
AK
478 /* Must be after DS reload */
479 unlazy_fpu(prev_p);
480
7de08b4e 481 /*
45948d77 482 * Switch the PDA and FPU contexts.
1da177e4 483 */
3d1e42a7
BG
484 prev->usersp = percpu_read(old_rsp);
485 percpu_write(old_rsp, next->usersp);
c6f5e0ac 486 percpu_write(current_task, next_p);
18bd057b 487
9af45651 488 percpu_write(kernel_stack,
87b935a0 489 (unsigned long)task_stack_page(next_p) +
9af45651 490 THREAD_SIZE - KERNEL_STACK_OFFSET);
1da177e4
LT
491
492 /*
d3a4f48d 493 * Now maybe reload the debug registers and handle I/O bitmaps
1da177e4 494 */
eee3af4a
MM
495 if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
496 task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
d3a4f48d 497 __switch_to_xtra(prev_p, next_p, tss);
1da177e4 498
e07e23e1
AV
499 /* If the task has used fpu the last 5 timeslices, just do a full
500 * restore of the math state immediately to avoid the trap; the
501 * chances of needing FPU soon are obviously high now
870568b3
SS
502 *
503 * tsk_used_math() checks prevent calling math_state_restore(),
504 * which can sleep in the case of !tsk_used_math()
e07e23e1 505 */
870568b3 506 if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
e07e23e1 507 math_state_restore();
66cb5917
P
508 /*
509 * There's a problem with moving the arch_install_thread_hw_breakpoint()
510 * call before current is updated. Suppose a kernel breakpoint is
511 * triggered in between the two, the hw-breakpoint handler will see that
512 * the 'current' task does not have TIF_DEBUG flag set and will think it
513 * is leftover from an old task (lazy switching) and will erase it. Then
514 * until the next context switch, no user-breakpoints will be installed.
515 *
516 * The real problem is that it's impossible to update both current and
517 * physical debug registers at the same instant, so there will always be
518 * a window in which they disagree and a breakpoint might get triggered.
519 * Since we use lazy switching, we are forced to assume that a
520 * disagreement means that current is correct and the exception is due
521 * to lazy debug register switching.
522 */
523 if (unlikely(test_tsk_thread_flag(next_p, TIF_DEBUG)))
524 arch_install_thread_hw_breakpoint(next_p);
525
1da177e4
LT
526 return prev_p;
527}
528
529/*
530 * sys_execve() executes a new program.
531 */
6612538c 532asmlinkage
1da177e4 533long sys_execve(char __user *name, char __user * __user *argv,
5d119b2c 534 char __user * __user *envp, struct pt_regs *regs)
1da177e4
LT
535{
536 long error;
7de08b4e 537 char *filename;
1da177e4
LT
538
539 filename = getname(name);
540 error = PTR_ERR(filename);
5d119b2c 541 if (IS_ERR(filename))
1da177e4 542 return error;
5d119b2c 543 error = do_execve(filename, argv, envp, regs);
1da177e4
LT
544 putname(filename);
545 return error;
546}
547
548void set_personality_64bit(void)
549{
550 /* inherit personality from parent */
551
552 /* Make sure to be in 64bit mode */
6612538c 553 clear_thread_flag(TIF_IA32);
1da177e4
LT
554
555 /* TBD: overwrites user setup. Should have two bits.
556 But 64bit processes have always behaved this way,
557 so it's not too bad. The main problem is just that
6612538c 558 32bit childs are affected again. */
1da177e4
LT
559 current->personality &= ~READ_IMPLIES_EXEC;
560}
561
a88cde13
AK
562asmlinkage long
563sys_clone(unsigned long clone_flags, unsigned long newsp,
564 void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
1da177e4
LT
565{
566 if (!newsp)
65ea5b03 567 newsp = regs->sp;
1da177e4
LT
568 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
569}
570
1da177e4
LT
571unsigned long get_wchan(struct task_struct *p)
572{
573 unsigned long stack;
7de08b4e 574 u64 fp, ip;
1da177e4
LT
575 int count = 0;
576
7de08b4e
GP
577 if (!p || p == current || p->state == TASK_RUNNING)
578 return 0;
57eafdc2 579 stack = (unsigned long)task_stack_page(p);
e1e23bb0 580 if (p->thread.sp < stack || p->thread.sp >= stack+THREAD_SIZE)
1da177e4 581 return 0;
faca6227 582 fp = *(u64 *)(p->thread.sp);
7de08b4e 583 do {
a88cde13 584 if (fp < (unsigned long)stack ||
e1e23bb0 585 fp >= (unsigned long)stack+THREAD_SIZE)
7de08b4e 586 return 0;
65ea5b03
PA
587 ip = *(u64 *)(fp+8);
588 if (!in_sched_functions(ip))
589 return ip;
7de08b4e
GP
590 fp = *(u64 *)fp;
591 } while (count++ < 16);
1da177e4
LT
592 return 0;
593}
594
595long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
7de08b4e
GP
596{
597 int ret = 0;
1da177e4
LT
598 int doit = task == current;
599 int cpu;
600
7de08b4e 601 switch (code) {
1da177e4 602 case ARCH_SET_GS:
84929801 603 if (addr >= TASK_SIZE_OF(task))
7de08b4e 604 return -EPERM;
1da177e4 605 cpu = get_cpu();
7de08b4e 606 /* handle small bases via the GDT because that's faster to
1da177e4 607 switch. */
7de08b4e
GP
608 if (addr <= 0xffffffff) {
609 set_32bit_tls(task, GS_TLS, addr);
610 if (doit) {
1da177e4 611 load_TLS(&task->thread, cpu);
7de08b4e 612 load_gs_index(GS_TLS_SEL);
1da177e4 613 }
7de08b4e 614 task->thread.gsindex = GS_TLS_SEL;
1da177e4 615 task->thread.gs = 0;
7de08b4e 616 } else {
1da177e4
LT
617 task->thread.gsindex = 0;
618 task->thread.gs = addr;
619 if (doit) {
a88cde13
AK
620 load_gs_index(0);
621 ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
7de08b4e 622 }
1da177e4
LT
623 }
624 put_cpu();
625 break;
626 case ARCH_SET_FS:
627 /* Not strictly needed for fs, but do it for symmetry
628 with gs */
84929801 629 if (addr >= TASK_SIZE_OF(task))
6612538c 630 return -EPERM;
1da177e4 631 cpu = get_cpu();
6612538c 632 /* handle small bases via the GDT because that's faster to
1da177e4 633 switch. */
6612538c 634 if (addr <= 0xffffffff) {
1da177e4 635 set_32bit_tls(task, FS_TLS, addr);
6612538c
HS
636 if (doit) {
637 load_TLS(&task->thread, cpu);
ada85708 638 loadsegment(fs, FS_TLS_SEL);
1da177e4
LT
639 }
640 task->thread.fsindex = FS_TLS_SEL;
641 task->thread.fs = 0;
6612538c 642 } else {
1da177e4
LT
643 task->thread.fsindex = 0;
644 task->thread.fs = addr;
645 if (doit) {
646 /* set the selector to 0 to not confuse
647 __switch_to */
ada85708 648 loadsegment(fs, 0);
a88cde13 649 ret = checking_wrmsrl(MSR_FS_BASE, addr);
1da177e4
LT
650 }
651 }
652 put_cpu();
653 break;
6612538c
HS
654 case ARCH_GET_FS: {
655 unsigned long base;
1da177e4
LT
656 if (task->thread.fsindex == FS_TLS_SEL)
657 base = read_32bit_tls(task, FS_TLS);
a88cde13 658 else if (doit)
1da177e4 659 rdmsrl(MSR_FS_BASE, base);
a88cde13 660 else
1da177e4 661 base = task->thread.fs;
6612538c
HS
662 ret = put_user(base, (unsigned long __user *)addr);
663 break;
1da177e4 664 }
6612538c 665 case ARCH_GET_GS: {
1da177e4 666 unsigned long base;
97c2803c 667 unsigned gsindex;
1da177e4
LT
668 if (task->thread.gsindex == GS_TLS_SEL)
669 base = read_32bit_tls(task, GS_TLS);
97c2803c 670 else if (doit) {
ada85708 671 savesegment(gs, gsindex);
97c2803c
JB
672 if (gsindex)
673 rdmsrl(MSR_KERNEL_GS_BASE, base);
674 else
675 base = task->thread.gs;
7de08b4e 676 } else
1da177e4 677 base = task->thread.gs;
6612538c 678 ret = put_user(base, (unsigned long __user *)addr);
1da177e4
LT
679 break;
680 }
681
682 default:
683 ret = -EINVAL;
684 break;
6612538c 685 }
1da177e4 686
6612538c
HS
687 return ret;
688}
1da177e4
LT
689
690long sys_arch_prctl(int code, unsigned long addr)
691{
692 return do_arch_prctl(current, code, addr);
1da177e4
LT
693}
694
695unsigned long arch_align_stack(unsigned long sp)
696{
c16b63e0 697 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1da177e4
LT
698 sp -= get_random_int() % 8192;
699 return sp & ~0xf;
700}
c1d171a0
JK
701
702unsigned long arch_randomize_brk(struct mm_struct *mm)
703{
704 unsigned long range_end = mm->brk + 0x02000000;
705 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
706}