blob: 3baf9b9f4c87e9f941223b7ca0e862f52f3a818c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995 Linus Torvalds
3 *
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
Hiroshi Shimamoto66125382008-01-30 13:31:03 +01006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * X86-64 port
8 * Andi Kleen.
Ashok Raj76e4f662005-06-25 14:55:00 -07009 *
10 * CPU hotplug support - ashok.raj@intel.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/*
14 * This file handles the architecture-dependent parts of process handling..
15 */
16
17#include <stdarg.h>
18
Ashok Raj76e4f662005-06-25 14:55:00 -070019#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/sched.h>
Hiroshi Shimamoto66125382008-01-30 13:31:03 +010022#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/elfcore.h>
26#include <linux/smp.h>
27#include <linux/slab.h>
28#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/utsname.h>
Hiroshi Shimamoto66125382008-01-30 13:31:03 +010031#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/random.h>
Andi Kleen95833c82006-01-11 22:44:36 +010035#include <linux/notifier.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080036#include <linux/kprobes.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070037#include <linux/kdebug.h>
Chris Wright02290682007-10-12 23:04:07 +020038#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/uaccess.h>
41#include <asm/pgtable.h>
42#include <asm/system.h>
43#include <asm/io.h>
44#include <asm/processor.h>
45#include <asm/i387.h>
46#include <asm/mmu_context.h>
47#include <asm/pda.h>
48#include <asm/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/desc.h>
50#include <asm/proto.h>
51#include <asm/ia32.h>
Andi Kleen95833c82006-01-11 22:44:36 +010052#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54asmlinkage extern void ret_from_fork(void);
55
56unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058unsigned long boot_option_idle_override = 0;
59EXPORT_SYMBOL(boot_option_idle_override);
60
61/*
62 * Powermanagement idle function, if any..
63 */
64void (*pm_idle)(void);
Andi Kleen2ee60e172006-06-26 13:59:44 +020065EXPORT_SYMBOL(pm_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
67
Alan Sterne041c682006-03-27 01:16:30 -080068static ATOMIC_NOTIFIER_HEAD(idle_notifier);
Andi Kleen95833c82006-01-11 22:44:36 +010069
70void idle_notifier_register(struct notifier_block *n)
71{
Alan Sterne041c682006-03-27 01:16:30 -080072 atomic_notifier_chain_register(&idle_notifier, n);
Andi Kleen95833c82006-01-11 22:44:36 +010073}
Andi Kleen95833c82006-01-11 22:44:36 +010074
Andi Kleen95833c82006-01-11 22:44:36 +010075void enter_idle(void)
76{
Andi Kleena15da492006-09-26 10:52:40 +020077 write_pda(isidle, 1);
Alan Sterne041c682006-03-27 01:16:30 -080078 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
Andi Kleen95833c82006-01-11 22:44:36 +010079}
80
81static void __exit_idle(void)
82{
Andi Kleen94468682006-11-14 16:57:46 +010083 if (test_and_clear_bit_pda(0, isidle) == 0)
Andi Kleena15da492006-09-26 10:52:40 +020084 return;
Alan Sterne041c682006-03-27 01:16:30 -080085 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
Andi Kleen95833c82006-01-11 22:44:36 +010086}
87
88/* Called from interrupts to signify idle end */
89void exit_idle(void)
90{
Andi Kleena15da492006-09-26 10:52:40 +020091 /* idle loop has pid 0 */
92 if (current->pid)
Andi Kleen95833c82006-01-11 22:44:36 +010093 return;
94 __exit_idle();
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * We use this if we don't have any better
99 * idle routine..
100 */
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100101void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Andi Kleen495ab9c2006-06-26 13:59:11 +0200103 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800104 /*
105 * TS_POLLING-cleared state must be visible before we
106 * test NEED_RESCHED:
107 */
108 smp_mb();
Andi Kleen72690a22006-12-07 02:14:03 +0100109 local_irq_disable();
110 if (!need_resched()) {
Ingo Molnar5ee613b2008-01-30 13:30:06 +0100111 ktime_t t0, t1;
112 u64 t0n, t1n;
113
114 t0 = ktime_get();
115 t0n = ktime_to_ns(t0);
116 safe_halt(); /* enables interrupts racelessly */
117 local_irq_disable();
118 t1 = ktime_get();
119 t1n = ktime_to_ns(t1);
120 sched_clock_idle_wakeup_event(t1n - t0n);
Hiroshi Shimamoto39d44a52008-01-30 13:30:06 +0100121 }
122 local_irq_enable();
Andi Kleen495ab9c2006-06-26 13:59:11 +0200123 current_thread_info()->status |= TS_POLLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * On SMP it's slightly faster (but much more power-consuming!)
128 * to poll the ->need_resched flag instead of waiting for the
129 * cross-CPU IPI to arrive. Use this option with caution.
130 */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100131static void poll_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Venkatesh Pallipadid331e732006-12-07 02:14:13 +0100133 local_irq_enable();
Andi Kleen72690a22006-12-07 02:14:03 +0100134 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Ashok Raj76e4f662005-06-25 14:55:00 -0700137#ifdef CONFIG_HOTPLUG_CPU
138DECLARE_PER_CPU(int, cpu_state);
139
140#include <asm/nmi.h>
Shaohua Li1fa744e2006-01-06 00:12:20 -0800141/* We halt the CPU with physical CPU hotplug */
Ashok Raj76e4f662005-06-25 14:55:00 -0700142static inline void play_dead(void)
143{
144 idle_task_exit();
145 wbinvd();
146 mb();
147 /* Ack it */
148 __get_cpu_var(cpu_state) = CPU_DEAD;
149
Shaohua Li1fa744e2006-01-06 00:12:20 -0800150 local_irq_disable();
Ashok Raj76e4f662005-06-25 14:55:00 -0700151 while (1)
Shaohua Li1fa744e2006-01-06 00:12:20 -0800152 halt();
Ashok Raj76e4f662005-06-25 14:55:00 -0700153}
154#else
155static inline void play_dead(void)
156{
157 BUG();
158}
159#endif /* CONFIG_HOTPLUG_CPU */
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * The idle thread. There's no useful work to be
163 * done, so just try to conserve power and have a
164 * low exit latency (ie sit in a loop waiting for
165 * somebody to say that they'd like to reschedule)
166 */
Pavel Machekb10db7f2008-01-30 13:30:00 +0100167void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Andi Kleen495ab9c2006-06-26 13:59:11 +0200169 current_thread_info()->status |= TS_POLLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* endless idle loop with no priority at all */
171 while (1) {
Hiroshi Shimamoto3d977752008-01-30 13:33:00 +0100172 tick_nohz_stop_sched_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 while (!need_resched()) {
174 void (*idle)(void);
175
176 if (__get_cpu_var(cpu_idle_state))
177 __get_cpu_var(cpu_idle_state) = 0;
178
179 rmb();
180 idle = pm_idle;
181 if (!idle)
182 idle = default_idle;
Ashok Raj76e4f662005-06-25 14:55:00 -0700183 if (cpu_is_offline(smp_processor_id()))
184 play_dead();
Venkatesh Pallipadid331e732006-12-07 02:14:13 +0100185 /*
186 * Idle routines should keep interrupts disabled
187 * from here on, until they go to idle.
188 * Otherwise, idle callbacks can misfire.
189 */
190 local_irq_disable();
Andi Kleen95833c82006-01-11 22:44:36 +0100191 enter_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 idle();
Andi Kleena15da492006-09-26 10:52:40 +0200193 /* In many cases the interrupt that ended idle
194 has already called exit_idle. But some idle
195 loops can be woken up without interrupt. */
Andi Kleen95833c82006-01-11 22:44:36 +0100196 __exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Chris Wright02290682007-10-12 23:04:07 +0200199 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800200 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800202 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204}
205
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100206static void do_nothing(void *unused)
207{
208}
209
210void cpu_idle_wait(void)
211{
212 unsigned int cpu, this_cpu = get_cpu();
213 cpumask_t map, tmp = current->cpus_allowed;
214
215 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
216 put_cpu();
217
218 cpus_clear(map);
219 for_each_online_cpu(cpu) {
220 per_cpu(cpu_idle_state, cpu) = 1;
221 cpu_set(cpu, map);
222 }
223
224 __get_cpu_var(cpu_idle_state) = 0;
225
226 wmb();
227 do {
228 ssleep(1);
229 for_each_online_cpu(cpu) {
230 if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
231 cpu_clear(cpu, map);
232 }
233 cpus_and(map, map, cpu_online_map);
234 /*
235 * We waited 1 sec, if a CPU still did not call idle
236 * it may be because it is in idle and not waking up
237 * because it has nothing to do.
238 * Give all the remaining CPUS a kick.
239 */
240 smp_call_function_mask(map, do_nothing, 0, 0);
241 } while (!cpus_empty(map));
242
243 set_cpus_allowed(current, tmp);
244}
245EXPORT_SYMBOL_GPL(cpu_idle_wait);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
249 * which can obviate IPI to trigger checking of need_resched.
250 * We execute MONITOR against need_resched and enter optimized wait state
251 * through MWAIT. Whenever someone changes need_resched, we would be woken
252 * up from MWAIT (without an IPI).
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700253 *
254 * New with Core Duo processors, MWAIT can take some hints based on CPU
255 * capability.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100257void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700258{
259 if (!need_resched()) {
260 __monitor((void *)&current_thread_info()->flags, 0, 0);
261 smp_mb();
262 if (!need_resched())
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100263 __mwait(ax, cx);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700264 }
265}
266
267/* Default MONITOR/MWAIT with no hints, used for default C1 state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void mwait_idle(void)
269{
Venkatesh Pallipadid331e732006-12-07 02:14:13 +0100270 if (!need_resched()) {
271 __monitor((void *)&current_thread_info()->flags, 0, 0);
272 smp_mb();
273 if (!need_resched())
274 __sti_mwait(0, 0);
275 else
276 local_irq_enable();
277 } else {
278 local_irq_enable();
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Andi Kleen0c07ee32008-01-30 13:33:16 +0100282
Sam Ravnborg4c02ad12008-01-30 13:33:37 +0100283static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
Andi Kleen0c07ee32008-01-30 13:33:16 +0100284{
285 if (force_mwait)
286 return 1;
287 /* Any C1 states supported? */
288 return c->cpuid_level >= 5 && ((cpuid_edx(5) >> 4) & 0xf) > 0;
289}
290
Ashok Raje6982c62005-06-25 14:54:58 -0700291void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Hiroshi Shimamoto27415a42008-01-30 13:33:18 +0100293 static int selected;
294
295 if (selected)
296 return;
297#ifdef CONFIG_X86_SMP
298 if (pm_idle == poll_idle && smp_num_siblings > 1) {
299 printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
300 " performance may degrade.\n");
301 }
302#endif
Andi Kleen0c07ee32008-01-30 13:33:16 +0100303 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
305 * Skip, if setup has overridden idle.
306 * One CPU supports mwait => All CPUs supports mwait
307 */
308 if (!pm_idle) {
Hiroshi Shimamoto27415a42008-01-30 13:33:18 +0100309 printk(KERN_INFO "using mwait in idle threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 pm_idle = mwait_idle;
311 }
312 }
Hiroshi Shimamoto27415a42008-01-30 13:33:18 +0100313 selected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100316static int __init idle_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Andi Kleenf039b752007-05-02 19:27:12 +0200318 if (!strcmp(str, "poll")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 printk("using polling idle threads.\n");
320 pm_idle = poll_idle;
Andi Kleenf039b752007-05-02 19:27:12 +0200321 } else if (!strcmp(str, "mwait"))
322 force_mwait = 1;
323 else
324 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 boot_option_idle_override = 1;
Andi Kleenf039b752007-05-02 19:27:12 +0200327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
Andi Kleenf039b752007-05-02 19:27:12 +0200329early_param("idle", idle_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100331/* Prints also some state that isn't saved in the pt_regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332void __show_regs(struct pt_regs * regs)
333{
334 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
Alan Sternbb1995d2007-07-21 17:10:42 +0200335 unsigned long d0, d1, d2, d3, d6, d7;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100336 unsigned int fsindex, gsindex;
337 unsigned int ds, cs, es;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 printk("\n");
340 print_modules();
Andi Kleen9acf23c2005-09-12 18:49:24 +0200341 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
342 current->pid, current->comm, print_tainted(),
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700343 init_utsname()->release,
344 (int)strcspn(init_utsname()->version, " "),
345 init_utsname()->version);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100346 printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100347 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100348 printk("RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->sp,
349 regs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100351 regs->ax, regs->bx, regs->cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100353 regs->dx, regs->si, regs->di);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 printk("RBP: %016lx R08: %016lx R09: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100355 regs->bp, regs->r8, regs->r9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk("R10: %016lx R11: %016lx R12: %016lx\n",
357 regs->r10, regs->r11, regs->r12);
358 printk("R13: %016lx R14: %016lx R15: %016lx\n",
359 regs->r13, regs->r14, regs->r15);
360
361 asm("movl %%ds,%0" : "=r" (ds));
362 asm("movl %%cs,%0" : "=r" (cs));
363 asm("movl %%es,%0" : "=r" (es));
364 asm("movl %%fs,%0" : "=r" (fsindex));
365 asm("movl %%gs,%0" : "=r" (gsindex));
366
367 rdmsrl(MSR_FS_BASE, fs);
368 rdmsrl(MSR_GS_BASE, gs);
369 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
370
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200371 cr0 = read_cr0();
372 cr2 = read_cr2();
373 cr3 = read_cr3();
374 cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
377 fs,fsindex,gs,gsindex,shadowgs);
378 printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
379 printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
Alan Sternbb1995d2007-07-21 17:10:42 +0200380
381 get_debugreg(d0, 0);
382 get_debugreg(d1, 1);
383 get_debugreg(d2, 2);
384 printk("DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
385 get_debugreg(d3, 3);
386 get_debugreg(d6, 6);
387 get_debugreg(d7, 7);
388 printk("DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391void show_regs(struct pt_regs *regs)
392{
Zwane Mwaikamboc078d322005-09-06 15:16:16 -0700393 printk("CPU %d:", smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 __show_regs(regs);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100395 show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
399 * Free current thread data structures etc..
400 */
401void exit_thread(void)
402{
403 struct task_struct *me = current;
404 struct thread_struct *t = &me->thread;
Rusty Lynch73649da2005-06-23 00:09:23 -0700405
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100406 if (me->thread.io_bitmap_ptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
408
409 kfree(t->io_bitmap_ptr);
410 t->io_bitmap_ptr = NULL;
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200411 clear_thread_flag(TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Careful, clear this in the TSS too:
414 */
415 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
416 t->io_bitmap_max = 0;
417 put_cpu();
418 }
419}
420
421void flush_thread(void)
422{
423 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Mathieu Desnoyers303cd152007-03-18 01:26:11 -0800425 if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
426 clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
427 if (test_tsk_thread_flag(tsk, TIF_IA32)) {
428 clear_tsk_thread_flag(tsk, TIF_IA32);
429 } else {
430 set_tsk_thread_flag(tsk, TIF_IA32);
Andi Kleen4d9bc792006-06-26 13:57:19 +0200431 current_thread_info()->status |= TS_COMPAT;
Mathieu Desnoyers303cd152007-03-18 01:26:11 -0800432 }
Andi Kleen4d9bc792006-06-26 13:57:19 +0200433 }
Mathieu Desnoyers303cd152007-03-18 01:26:11 -0800434 clear_tsk_thread_flag(tsk, TIF_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 tsk->thread.debugreg0 = 0;
437 tsk->thread.debugreg1 = 0;
438 tsk->thread.debugreg2 = 0;
439 tsk->thread.debugreg3 = 0;
440 tsk->thread.debugreg6 = 0;
441 tsk->thread.debugreg7 = 0;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100442 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Forget coprocessor state..
445 */
446 clear_fpu(tsk);
447 clear_used_math();
448}
449
450void release_thread(struct task_struct *dead_task)
451{
452 if (dead_task->mm) {
453 if (dead_task->mm->context.size) {
454 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
455 dead_task->comm,
456 dead_task->mm->context.ldt,
457 dead_task->mm->context.size);
458 BUG();
459 }
460 }
461}
462
463static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
464{
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100465 struct user_desc ud = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .base_addr = addr,
467 .limit = 0xfffff,
468 .seg_32bit = 1,
469 .limit_in_pages = 1,
470 .useable = 1,
471 };
Jan Engelhardtade1af72008-01-30 13:33:23 +0100472 struct desc_struct *desc = t->thread.tls_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 desc += tls;
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100474 fill_ldt(desc, &ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static inline u32 read_32bit_tls(struct task_struct *t, int tls)
478{
Roland McGrath91394eb2008-01-30 13:30:45 +0100479 return get_desc_base(&t->thread.tls_array[tls]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/*
483 * This gets called before we allocate a new thread and copy
484 * the current task into it.
485 */
486void prepare_to_copy(struct task_struct *tsk)
487{
488 unlazy_fpu(tsk);
489}
490
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100491int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned long unused,
493 struct task_struct * p, struct pt_regs * regs)
494{
495 int err;
496 struct pt_regs * childregs;
497 struct task_struct *me = current;
498
Andi Kleena88cde12005-11-05 17:25:54 +0100499 childregs = ((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800500 (THREAD_SIZE + task_stack_page(p))) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *childregs = *regs;
502
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100503 childregs->ax = 0;
504 childregs->sp = sp;
505 if (sp == ~0UL)
506 childregs->sp = (unsigned long)childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100508 p->thread.sp = (unsigned long) childregs;
509 p->thread.sp0 = (unsigned long) (childregs+1);
510 p->thread.usersp = me->thread.usersp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Al Viroe4f17c42006-01-12 01:05:38 -0800512 set_tsk_thread_flag(p, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 p->thread.fs = me->thread.fs;
515 p->thread.gs = me->thread.gs;
516
H. J. Lufd51f662005-05-01 08:58:48 -0700517 asm("mov %%gs,%0" : "=m" (p->thread.gsindex));
518 asm("mov %%fs,%0" : "=m" (p->thread.fsindex));
519 asm("mov %%es,%0" : "=m" (p->thread.es));
520 asm("mov %%ds,%0" : "=m" (p->thread.ds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200522 if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
524 if (!p->thread.io_bitmap_ptr) {
525 p->thread.io_bitmap_max = 0;
526 return -ENOMEM;
527 }
Andi Kleena88cde12005-11-05 17:25:54 +0100528 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
529 IO_BITMAP_BYTES);
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200530 set_tsk_thread_flag(p, TIF_IO_BITMAP);
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /*
534 * Set a new TLS for the child thread?
535 */
536 if (clone_flags & CLONE_SETTLS) {
537#ifdef CONFIG_IA32_EMULATION
538 if (test_thread_flag(TIF_IA32))
Roland McGrathefd1ca52008-01-30 13:30:46 +0100539 err = do_set_thread_area(p, -1,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100540 (struct user_desc __user *)childregs->si, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 else
542#endif
543 err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
544 if (err)
545 goto out;
546 }
547 err = 0;
548out:
549 if (err && p->thread.io_bitmap_ptr) {
550 kfree(p->thread.io_bitmap_ptr);
551 p->thread.io_bitmap_max = 0;
552 }
553 return err;
554}
555
556/*
557 * This special macro can be used to load a debugging register
558 */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100559#define loaddebug(thread, r) set_debugreg(thread->debugreg ## r, r)
560
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200561static inline void __switch_to_xtra(struct task_struct *prev_p,
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100562 struct task_struct *next_p,
563 struct tss_struct *tss)
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200564{
565 struct thread_struct *prev, *next;
Markus Metzgereee3af42008-01-30 13:31:09 +0100566 unsigned long debugctl;
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200567
568 prev = &prev_p->thread,
569 next = &next_p->thread;
570
Markus Metzgereee3af42008-01-30 13:31:09 +0100571 debugctl = prev->debugctlmsr;
572 if (next->ds_area_msr != prev->ds_area_msr) {
573 /* we clear debugctl to make sure DS
574 * is not in use when we change it */
575 debugctl = 0;
576 wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
577 wrmsrl(MSR_IA32_DS_AREA, next->ds_area_msr);
578 }
579
580 if (next->debugctlmsr != debugctl)
Roland McGrath7e991602008-01-30 13:30:54 +0100581 wrmsrl(MSR_IA32_DEBUGCTLMSR, next->debugctlmsr);
582
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200583 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
584 loaddebug(next, 0);
585 loaddebug(next, 1);
586 loaddebug(next, 2);
587 loaddebug(next, 3);
588 /* no 4 and 5 */
589 loaddebug(next, 6);
590 loaddebug(next, 7);
591 }
592
593 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
594 /*
595 * Copy the relevant range of the IO bitmap.
596 * Normally this is 128 bytes or less:
597 */
598 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
599 max(prev->io_bitmap_max, next->io_bitmap_max));
600 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
601 /*
602 * Clear any possible leftover bits:
603 */
604 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
605 }
Markus Metzgereee3af42008-01-30 13:31:09 +0100606
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100607#ifdef X86_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +0100608 if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
609 ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
610
611 if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
612 ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100613#endif
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * switch_to(x,y) should switch tasks from x to y.
618 *
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100619 * This could still be optimized:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * - fold all the options into a flag word and test it with a single test.
621 * - could test fs/gs bitsliced
Andi Kleen099f3182006-02-03 21:51:38 +0100622 *
623 * Kprobes not supported here. Set the probe on schedule instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700625struct task_struct *
Andi Kleena88cde12005-11-05 17:25:54 +0100626__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct thread_struct *prev = &prev_p->thread,
629 *next = &next_p->thread;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100630 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct tss_struct *tss = &per_cpu(init_tss, cpu);
632
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200633 /* we're going to use this soon, after a few expensive things */
634 if (next_p->fpu_counter>5)
635 prefetch(&next->i387.fxsave);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Reload esp0, LDT and the page table pointer:
639 */
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100640 load_sp0(tss, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /*
643 * Switch DS and ES.
644 * This won't pick up thread selector changes, but I guess that is ok.
645 */
H. J. Lufd51f662005-05-01 08:58:48 -0700646 asm volatile("mov %%es,%0" : "=m" (prev->es));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (unlikely(next->es | prev->es))
648 loadsegment(es, next->es);
649
H. J. Lufd51f662005-05-01 08:58:48 -0700650 asm volatile ("mov %%ds,%0" : "=m" (prev->ds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (unlikely(next->ds | prev->ds))
652 loadsegment(ds, next->ds);
653
654 load_TLS(next, cpu);
655
656 /*
657 * Switch FS and GS.
658 */
659 {
660 unsigned fsindex;
661 asm volatile("movl %%fs,%0" : "=r" (fsindex));
662 /* segment register != 0 always requires a reload.
663 also reload when it has changed.
664 when prev process used 64bit base always reload
665 to avoid an information leak. */
666 if (unlikely(fsindex | next->fsindex | prev->fs)) {
667 loadsegment(fs, next->fsindex);
668 /* check if the user used a selector != 0
669 * if yes clear 64bit base, since overloaded base
670 * is always mapped to the Null selector
671 */
672 if (fsindex)
673 prev->fs = 0;
674 }
675 /* when next process has a 64bit base use it */
676 if (next->fs)
677 wrmsrl(MSR_FS_BASE, next->fs);
678 prev->fsindex = fsindex;
679 }
680 {
681 unsigned gsindex;
682 asm volatile("movl %%gs,%0" : "=r" (gsindex));
683 if (unlikely(gsindex | next->gsindex | prev->gs)) {
684 load_gs_index(next->gsindex);
685 if (gsindex)
686 prev->gs = 0;
687 }
688 if (next->gs)
689 wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
690 prev->gsindex = gsindex;
691 }
692
Andi Kleen0a5ace22006-10-05 18:47:22 +0200693 /* Must be after DS reload */
694 unlazy_fpu(prev_p);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
Jan Beulich45948d72006-03-25 16:29:25 +0100697 * Switch the PDA and FPU contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100699 prev->usersp = read_pda(oldrsp);
700 write_pda(oldrsp, next->usersp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 write_pda(pcurrent, next_p);
Andi Kleen18bd0572006-04-20 02:36:45 +0200702
Andi Kleena88cde12005-11-05 17:25:54 +0100703 write_pda(kernelstack,
Andi Kleen7b0bda72006-09-26 10:52:39 +0200704 (unsigned long)task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
Arjan van de Ven0a425402006-09-26 10:52:38 +0200705#ifdef CONFIG_CC_STACKPROTECTOR
706 write_pda(stack_canary, next_p->stack_canary);
707 /*
708 * Build time only check to make sure the stack_canary is at
709 * offset 40 in the pda; this is a gcc ABI requirement
710 */
711 BUILD_BUG_ON(offsetof(struct x8664_pda, stack_canary) != 40);
712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /*
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200715 * Now maybe reload the debug registers and handle I/O bitmaps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Markus Metzgereee3af42008-01-30 13:31:09 +0100717 if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
718 task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200719 __switch_to_xtra(prev_p, next_p, tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200721 /* If the task has used fpu the last 5 timeslices, just do a full
722 * restore of the math state immediately to avoid the trap; the
723 * chances of needing FPU soon are obviously high now
724 */
725 if (next_p->fpu_counter>5)
726 math_state_restore();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return prev_p;
728}
729
730/*
731 * sys_execve() executes a new program.
732 */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100733asmlinkage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734long sys_execve(char __user *name, char __user * __user *argv,
Ingo Molnar5d119b22008-02-26 12:55:57 +0100735 char __user * __user *envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 long error;
738 char * filename;
739
740 filename = getname(name);
741 error = PTR_ERR(filename);
Ingo Molnar5d119b22008-02-26 12:55:57 +0100742 if (IS_ERR(filename))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return error;
Ingo Molnar5d119b22008-02-26 12:55:57 +0100744 error = do_execve(filename, argv, envp, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 putname(filename);
746 return error;
747}
748
749void set_personality_64bit(void)
750{
751 /* inherit personality from parent */
752
753 /* Make sure to be in 64bit mode */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100754 clear_thread_flag(TIF_IA32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* TBD: overwrites user setup. Should have two bits.
757 But 64bit processes have always behaved this way,
758 so it's not too bad. The main problem is just that
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100759 32bit childs are affected again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 current->personality &= ~READ_IMPLIES_EXEC;
761}
762
763asmlinkage long sys_fork(struct pt_regs *regs)
764{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100765 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Andi Kleena88cde12005-11-05 17:25:54 +0100768asmlinkage long
769sys_clone(unsigned long clone_flags, unsigned long newsp,
770 void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 if (!newsp)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100773 newsp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
775}
776
777/*
778 * This is trivial, and on the face of it looks like it
779 * could equally well be done in user mode.
780 *
781 * Not so, for quite unobvious reasons - register pressure.
782 * In user mode vfork() cannot have a stack frame, and if
783 * done by calling the "clone()" system call directly, you
784 * do not have enough call-clobbered registers to hold all
785 * the information you need.
786 */
787asmlinkage long sys_vfork(struct pt_regs *regs)
788{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100789 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 NULL, NULL);
791}
792
793unsigned long get_wchan(struct task_struct *p)
794{
795 unsigned long stack;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100796 u64 fp,ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int count = 0;
798
799 if (!p || p == current || p->state==TASK_RUNNING)
800 return 0;
Al Viro57eafdc2006-01-12 01:05:39 -0800801 stack = (unsigned long)task_stack_page(p);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100802 if (p->thread.sp < stack || p->thread.sp > stack+THREAD_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100804 fp = *(u64 *)(p->thread.sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 do {
Andi Kleena88cde12005-11-05 17:25:54 +0100806 if (fp < (unsigned long)stack ||
807 fp > (unsigned long)stack+THREAD_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100809 ip = *(u64 *)(fp+8);
810 if (!in_sched_functions(ip))
811 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 fp = *(u64 *)fp;
813 } while (count++ < 16);
814 return 0;
815}
816
817long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
818{
819 int ret = 0;
820 int doit = task == current;
821 int cpu;
822
823 switch (code) {
824 case ARCH_SET_GS:
Suresh Siddha84929802005-06-21 17:14:32 -0700825 if (addr >= TASK_SIZE_OF(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -EPERM;
827 cpu = get_cpu();
828 /* handle small bases via the GDT because that's faster to
829 switch. */
830 if (addr <= 0xffffffff) {
831 set_32bit_tls(task, GS_TLS, addr);
832 if (doit) {
833 load_TLS(&task->thread, cpu);
834 load_gs_index(GS_TLS_SEL);
835 }
836 task->thread.gsindex = GS_TLS_SEL;
837 task->thread.gs = 0;
838 } else {
839 task->thread.gsindex = 0;
840 task->thread.gs = addr;
841 if (doit) {
Andi Kleena88cde12005-11-05 17:25:54 +0100842 load_gs_index(0);
843 ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
846 put_cpu();
847 break;
848 case ARCH_SET_FS:
849 /* Not strictly needed for fs, but do it for symmetry
850 with gs */
Suresh Siddha84929802005-06-21 17:14:32 -0700851 if (addr >= TASK_SIZE_OF(task))
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100852 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 cpu = get_cpu();
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100854 /* handle small bases via the GDT because that's faster to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 switch. */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100856 if (addr <= 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 set_32bit_tls(task, FS_TLS, addr);
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100858 if (doit) {
859 load_TLS(&task->thread, cpu);
Andi Kleena88cde12005-11-05 17:25:54 +0100860 asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 task->thread.fsindex = FS_TLS_SEL;
863 task->thread.fs = 0;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100864 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 task->thread.fsindex = 0;
866 task->thread.fs = addr;
867 if (doit) {
868 /* set the selector to 0 to not confuse
869 __switch_to */
Andi Kleena88cde12005-11-05 17:25:54 +0100870 asm volatile("movl %0,%%fs" :: "r" (0));
871 ret = checking_wrmsrl(MSR_FS_BASE, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 }
874 put_cpu();
875 break;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100876 case ARCH_GET_FS: {
877 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (task->thread.fsindex == FS_TLS_SEL)
879 base = read_32bit_tls(task, FS_TLS);
Andi Kleena88cde12005-11-05 17:25:54 +0100880 else if (doit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 rdmsrl(MSR_FS_BASE, base);
Andi Kleena88cde12005-11-05 17:25:54 +0100882 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 base = task->thread.fs;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100884 ret = put_user(base, (unsigned long __user *)addr);
885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100887 case ARCH_GET_GS: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned long base;
John Blackwood97c28032006-04-07 19:50:25 +0200889 unsigned gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (task->thread.gsindex == GS_TLS_SEL)
891 base = read_32bit_tls(task, GS_TLS);
John Blackwood97c28032006-04-07 19:50:25 +0200892 else if (doit) {
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100893 asm("movl %%gs,%0" : "=r" (gsindex));
John Blackwood97c28032006-04-07 19:50:25 +0200894 if (gsindex)
895 rdmsrl(MSR_KERNEL_GS_BASE, base);
896 else
897 base = task->thread.gs;
898 }
Andi Kleena88cde12005-11-05 17:25:54 +0100899 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 base = task->thread.gs;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100901 ret = put_user(base, (unsigned long __user *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903 }
904
905 default:
906 ret = -EINVAL;
907 break;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100910 return ret;
911}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913long sys_arch_prctl(int code, unsigned long addr)
914{
915 return do_arch_prctl(current, code, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918unsigned long arch_align_stack(unsigned long sp)
919{
Andi Kleenc16b63e2006-09-26 10:52:28 +0200920 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sp -= get_random_int() % 8192;
922 return sp & ~0xf;
923}
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100924
925unsigned long arch_randomize_brk(struct mm_struct *mm)
926{
927 unsigned long range_end = mm->brk + 0x02000000;
928 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
929}