Pull utrace into release branch
[linux-2.6] / arch / ia64 / kernel / process.c
1 /*
2  * Architecture-specific setup.
3  *
4  * Copyright (C) 1998-2003 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  * 04/11/17 Ashok Raj   <ashok.raj@intel.com> Added CPU Hotplug Support
7  *
8  * 2005-10-07 Keith Owens <kaos@sgi.com>
9  *            Add notify_die() hooks.
10  */
11 #include <linux/cpu.h>
12 #include <linux/pm.h>
13 #include <linux/elf.h>
14 #include <linux/errno.h>
15 #include <linux/kallsyms.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/module.h>
19 #include <linux/notifier.h>
20 #include <linux/personality.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/stddef.h>
24 #include <linux/thread_info.h>
25 #include <linux/unistd.h>
26 #include <linux/efi.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/kdebug.h>
30 #include <linux/utsname.h>
31 #include <linux/tracehook.h>
32
33 #include <asm/cpu.h>
34 #include <asm/delay.h>
35 #include <asm/elf.h>
36 #include <asm/ia32.h>
37 #include <asm/irq.h>
38 #include <asm/kexec.h>
39 #include <asm/pgalloc.h>
40 #include <asm/processor.h>
41 #include <asm/sal.h>
42 #include <asm/tlbflush.h>
43 #include <asm/uaccess.h>
44 #include <asm/unwind.h>
45 #include <asm/user.h>
46
47 #include "entry.h"
48
49 #ifdef CONFIG_PERFMON
50 # include <asm/perfmon.h>
51 #endif
52
53 #include "sigframe.h"
54
55 void (*ia64_mark_idle)(int);
56
57 unsigned long boot_option_idle_override = 0;
58 EXPORT_SYMBOL(boot_option_idle_override);
59 unsigned long idle_halt;
60 EXPORT_SYMBOL(idle_halt);
61 unsigned long idle_nomwait;
62 EXPORT_SYMBOL(idle_nomwait);
63
64 void
65 ia64_do_show_stack (struct unw_frame_info *info, void *arg)
66 {
67         unsigned long ip, sp, bsp;
68         char buf[128];                  /* don't make it so big that it overflows the stack! */
69
70         printk("\nCall Trace:\n");
71         do {
72                 unw_get_ip(info, &ip);
73                 if (ip == 0)
74                         break;
75
76                 unw_get_sp(info, &sp);
77                 unw_get_bsp(info, &bsp);
78                 snprintf(buf, sizeof(buf),
79                          " [<%016lx>] %%s\n"
80                          "                                sp=%016lx bsp=%016lx\n",
81                          ip, sp, bsp);
82                 print_symbol(buf, ip);
83         } while (unw_unwind(info) >= 0);
84 }
85
86 void
87 show_stack (struct task_struct *task, unsigned long *sp)
88 {
89         if (!task)
90                 unw_init_running(ia64_do_show_stack, NULL);
91         else {
92                 struct unw_frame_info info;
93
94                 unw_init_from_blocked_task(&info, task);
95                 ia64_do_show_stack(&info, NULL);
96         }
97 }
98
99 void
100 dump_stack (void)
101 {
102         show_stack(NULL, NULL);
103 }
104
105 EXPORT_SYMBOL(dump_stack);
106
107 void
108 show_regs (struct pt_regs *regs)
109 {
110         unsigned long ip = regs->cr_iip + ia64_psr(regs)->ri;
111
112         print_modules();
113         printk("\nPid: %d, CPU %d, comm: %20s\n", task_pid_nr(current),
114                         smp_processor_id(), current->comm);
115         printk("psr : %016lx ifs : %016lx ip  : [<%016lx>]    %s (%s)\n",
116                regs->cr_ipsr, regs->cr_ifs, ip, print_tainted(),
117                init_utsname()->release);
118         print_symbol("ip is at %s\n", ip);
119         printk("unat: %016lx pfs : %016lx rsc : %016lx\n",
120                regs->ar_unat, regs->ar_pfs, regs->ar_rsc);
121         printk("rnat: %016lx bsps: %016lx pr  : %016lx\n",
122                regs->ar_rnat, regs->ar_bspstore, regs->pr);
123         printk("ldrs: %016lx ccv : %016lx fpsr: %016lx\n",
124                regs->loadrs, regs->ar_ccv, regs->ar_fpsr);
125         printk("csd : %016lx ssd : %016lx\n", regs->ar_csd, regs->ar_ssd);
126         printk("b0  : %016lx b6  : %016lx b7  : %016lx\n", regs->b0, regs->b6, regs->b7);
127         printk("f6  : %05lx%016lx f7  : %05lx%016lx\n",
128                regs->f6.u.bits[1], regs->f6.u.bits[0],
129                regs->f7.u.bits[1], regs->f7.u.bits[0]);
130         printk("f8  : %05lx%016lx f9  : %05lx%016lx\n",
131                regs->f8.u.bits[1], regs->f8.u.bits[0],
132                regs->f9.u.bits[1], regs->f9.u.bits[0]);
133         printk("f10 : %05lx%016lx f11 : %05lx%016lx\n",
134                regs->f10.u.bits[1], regs->f10.u.bits[0],
135                regs->f11.u.bits[1], regs->f11.u.bits[0]);
136
137         printk("r1  : %016lx r2  : %016lx r3  : %016lx\n", regs->r1, regs->r2, regs->r3);
138         printk("r8  : %016lx r9  : %016lx r10 : %016lx\n", regs->r8, regs->r9, regs->r10);
139         printk("r11 : %016lx r12 : %016lx r13 : %016lx\n", regs->r11, regs->r12, regs->r13);
140         printk("r14 : %016lx r15 : %016lx r16 : %016lx\n", regs->r14, regs->r15, regs->r16);
141         printk("r17 : %016lx r18 : %016lx r19 : %016lx\n", regs->r17, regs->r18, regs->r19);
142         printk("r20 : %016lx r21 : %016lx r22 : %016lx\n", regs->r20, regs->r21, regs->r22);
143         printk("r23 : %016lx r24 : %016lx r25 : %016lx\n", regs->r23, regs->r24, regs->r25);
144         printk("r26 : %016lx r27 : %016lx r28 : %016lx\n", regs->r26, regs->r27, regs->r28);
145         printk("r29 : %016lx r30 : %016lx r31 : %016lx\n", regs->r29, regs->r30, regs->r31);
146
147         if (user_mode(regs)) {
148                 /* print the stacked registers */
149                 unsigned long val, *bsp, ndirty;
150                 int i, sof, is_nat = 0;
151
152                 sof = regs->cr_ifs & 0x7f;      /* size of frame */
153                 ndirty = (regs->loadrs >> 19);
154                 bsp = ia64_rse_skip_regs((unsigned long *) regs->ar_bspstore, ndirty);
155                 for (i = 0; i < sof; ++i) {
156                         get_user(val, (unsigned long __user *) ia64_rse_skip_regs(bsp, i));
157                         printk("r%-3u:%c%016lx%s", 32 + i, is_nat ? '*' : ' ', val,
158                                ((i == sof - 1) || (i % 3) == 2) ? "\n" : " ");
159                 }
160         } else
161                 show_stack(NULL, NULL);
162 }
163
164 void
165 do_notify_resume_user(sigset_t *unused, struct sigscratch *scr, long in_syscall)
166 {
167         if (fsys_mode(current, &scr->pt)) {
168                 /*
169                  * defer signal-handling etc. until we return to
170                  * privilege-level 0.
171                  */
172                 if (!ia64_psr(&scr->pt)->lp)
173                         ia64_psr(&scr->pt)->lp = 1;
174                 return;
175         }
176
177 #ifdef CONFIG_PERFMON
178         if (current->thread.pfm_needs_checking)
179                 /*
180                  * Note: pfm_handle_work() allow us to call it with interrupts
181                  * disabled, and may enable interrupts within the function.
182                  */
183                 pfm_handle_work();
184 #endif
185
186         /* deal with pending signal delivery */
187         if (test_thread_flag(TIF_SIGPENDING)) {
188                 local_irq_enable();     /* force interrupt enable */
189                 ia64_do_signal(scr, in_syscall);
190         }
191
192         if (test_thread_flag(TIF_NOTIFY_RESUME)) {
193                 clear_thread_flag(TIF_NOTIFY_RESUME);
194                 tracehook_notify_resume(&scr->pt);
195         }
196
197         /* copy user rbs to kernel rbs */
198         if (unlikely(test_thread_flag(TIF_RESTORE_RSE))) {
199                 local_irq_enable();     /* force interrupt enable */
200                 ia64_sync_krbs();
201         }
202
203         local_irq_disable();    /* force interrupt disable */
204 }
205
206 static int pal_halt        = 1;
207 static int can_do_pal_halt = 1;
208
209 static int __init nohalt_setup(char * str)
210 {
211         pal_halt = can_do_pal_halt = 0;
212         return 1;
213 }
214 __setup("nohalt", nohalt_setup);
215
216 void
217 update_pal_halt_status(int status)
218 {
219         can_do_pal_halt = pal_halt && status;
220 }
221
222 /*
223  * We use this if we don't have any better idle routine..
224  */
225 void
226 default_idle (void)
227 {
228         local_irq_enable();
229         while (!need_resched()) {
230                 if (can_do_pal_halt) {
231                         local_irq_disable();
232                         if (!need_resched()) {
233                                 safe_halt();
234                         }
235                         local_irq_enable();
236                 } else
237                         cpu_relax();
238         }
239 }
240
241 #ifdef CONFIG_HOTPLUG_CPU
242 /* We don't actually take CPU down, just spin without interrupts. */
243 static inline void play_dead(void)
244 {
245         extern void ia64_cpu_local_tick (void);
246         unsigned int this_cpu = smp_processor_id();
247
248         /* Ack it */
249         __get_cpu_var(cpu_state) = CPU_DEAD;
250
251         max_xtp();
252         local_irq_disable();
253         idle_task_exit();
254         ia64_jump_to_sal(&sal_boot_rendez_state[this_cpu]);
255         /*
256          * The above is a point of no-return, the processor is
257          * expected to be in SAL loop now.
258          */
259         BUG();
260 }
261 #else
262 static inline void play_dead(void)
263 {
264         BUG();
265 }
266 #endif /* CONFIG_HOTPLUG_CPU */
267
268 static void do_nothing(void *unused)
269 {
270 }
271
272 /*
273  * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
274  * pm_idle and update to new pm_idle value. Required while changing pm_idle
275  * handler on SMP systems.
276  *
277  * Caller must have changed pm_idle to the new value before the call. Old
278  * pm_idle value will not be used by any CPU after the return of this function.
279  */
280 void cpu_idle_wait(void)
281 {
282         smp_mb();
283         /* kick all the CPUs so that they exit out of pm_idle */
284         smp_call_function(do_nothing, NULL, 1);
285 }
286 EXPORT_SYMBOL_GPL(cpu_idle_wait);
287
288 void __attribute__((noreturn))
289 cpu_idle (void)
290 {
291         void (*mark_idle)(int) = ia64_mark_idle;
292         int cpu = smp_processor_id();
293
294         /* endless idle loop with no priority at all */
295         while (1) {
296                 if (can_do_pal_halt) {
297                         current_thread_info()->status &= ~TS_POLLING;
298                         /*
299                          * TS_POLLING-cleared state must be visible before we
300                          * test NEED_RESCHED:
301                          */
302                         smp_mb();
303                 } else {
304                         current_thread_info()->status |= TS_POLLING;
305                 }
306
307                 if (!need_resched()) {
308                         void (*idle)(void);
309 #ifdef CONFIG_SMP
310                         min_xtp();
311 #endif
312                         rmb();
313                         if (mark_idle)
314                                 (*mark_idle)(1);
315
316                         idle = pm_idle;
317                         if (!idle)
318                                 idle = default_idle;
319                         (*idle)();
320                         if (mark_idle)
321                                 (*mark_idle)(0);
322 #ifdef CONFIG_SMP
323                         normal_xtp();
324 #endif
325                 }
326                 preempt_enable_no_resched();
327                 schedule();
328                 preempt_disable();
329                 check_pgt_cache();
330                 if (cpu_is_offline(cpu))
331                         play_dead();
332         }
333 }
334
335 void
336 ia64_save_extra (struct task_struct *task)
337 {
338 #ifdef CONFIG_PERFMON
339         unsigned long info;
340 #endif
341
342         if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
343                 ia64_save_debug_regs(&task->thread.dbr[0]);
344
345 #ifdef CONFIG_PERFMON
346         if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
347                 pfm_save_regs(task);
348
349         info = __get_cpu_var(pfm_syst_info);
350         if (info & PFM_CPUINFO_SYST_WIDE)
351                 pfm_syst_wide_update_task(task, info, 0);
352 #endif
353
354 #ifdef CONFIG_IA32_SUPPORT
355         if (IS_IA32_PROCESS(task_pt_regs(task)))
356                 ia32_save_state(task);
357 #endif
358 }
359
360 void
361 ia64_load_extra (struct task_struct *task)
362 {
363 #ifdef CONFIG_PERFMON
364         unsigned long info;
365 #endif
366
367         if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
368                 ia64_load_debug_regs(&task->thread.dbr[0]);
369
370 #ifdef CONFIG_PERFMON
371         if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
372                 pfm_load_regs(task);
373
374         info = __get_cpu_var(pfm_syst_info);
375         if (info & PFM_CPUINFO_SYST_WIDE) 
376                 pfm_syst_wide_update_task(task, info, 1);
377 #endif
378
379 #ifdef CONFIG_IA32_SUPPORT
380         if (IS_IA32_PROCESS(task_pt_regs(task)))
381                 ia32_load_state(task);
382 #endif
383 }
384
385 /*
386  * Copy the state of an ia-64 thread.
387  *
388  * We get here through the following  call chain:
389  *
390  *      from user-level:        from kernel:
391  *
392  *      <clone syscall>         <some kernel call frames>
393  *      sys_clone                  :
394  *      do_fork                 do_fork
395  *      copy_thread             copy_thread
396  *
397  * This means that the stack layout is as follows:
398  *
399  *      +---------------------+ (highest addr)
400  *      |   struct pt_regs    |
401  *      +---------------------+
402  *      | struct switch_stack |
403  *      +---------------------+
404  *      |                     |
405  *      |    memory stack     |
406  *      |                     | <-- sp (lowest addr)
407  *      +---------------------+
408  *
409  * Observe that we copy the unat values that are in pt_regs and switch_stack.  Spilling an
410  * integer to address X causes bit N in ar.unat to be set to the NaT bit of the register,
411  * with N=(X & 0x1ff)/8.  Thus, copying the unat value preserves the NaT bits ONLY if the
412  * pt_regs structure in the parent is congruent to that of the child, modulo 512.  Since
413  * the stack is page aligned and the page size is at least 4KB, this is always the case,
414  * so there is nothing to worry about.
415  */
416 int
417 copy_thread (int nr, unsigned long clone_flags,
418              unsigned long user_stack_base, unsigned long user_stack_size,
419              struct task_struct *p, struct pt_regs *regs)
420 {
421         extern char ia64_ret_from_clone, ia32_ret_from_clone;
422         struct switch_stack *child_stack, *stack;
423         unsigned long rbs, child_rbs, rbs_size;
424         struct pt_regs *child_ptregs;
425         int retval = 0;
426
427 #ifdef CONFIG_SMP
428         /*
429          * For SMP idle threads, fork_by_hand() calls do_fork with
430          * NULL regs.
431          */
432         if (!regs)
433                 return 0;
434 #endif
435
436         stack = ((struct switch_stack *) regs) - 1;
437
438         child_ptregs = (struct pt_regs *) ((unsigned long) p + IA64_STK_OFFSET) - 1;
439         child_stack = (struct switch_stack *) child_ptregs - 1;
440
441         /* copy parent's switch_stack & pt_regs to child: */
442         memcpy(child_stack, stack, sizeof(*child_ptregs) + sizeof(*child_stack));
443
444         rbs = (unsigned long) current + IA64_RBS_OFFSET;
445         child_rbs = (unsigned long) p + IA64_RBS_OFFSET;
446         rbs_size = stack->ar_bspstore - rbs;
447
448         /* copy the parent's register backing store to the child: */
449         memcpy((void *) child_rbs, (void *) rbs, rbs_size);
450
451         if (likely(user_mode(child_ptregs))) {
452                 if ((clone_flags & CLONE_SETTLS) && !IS_IA32_PROCESS(regs))
453                         child_ptregs->r13 = regs->r16;  /* see sys_clone2() in entry.S */
454                 if (user_stack_base) {
455                         child_ptregs->r12 = user_stack_base + user_stack_size - 16;
456                         child_ptregs->ar_bspstore = user_stack_base;
457                         child_ptregs->ar_rnat = 0;
458                         child_ptregs->loadrs = 0;
459                 }
460         } else {
461                 /*
462                  * Note: we simply preserve the relative position of
463                  * the stack pointer here.  There is no need to
464                  * allocate a scratch area here, since that will have
465                  * been taken care of by the caller of sys_clone()
466                  * already.
467                  */
468                 child_ptregs->r12 = (unsigned long) child_ptregs - 16; /* kernel sp */
469                 child_ptregs->r13 = (unsigned long) p;          /* set `current' pointer */
470         }
471         child_stack->ar_bspstore = child_rbs + rbs_size;
472         if (IS_IA32_PROCESS(regs))
473                 child_stack->b0 = (unsigned long) &ia32_ret_from_clone;
474         else
475                 child_stack->b0 = (unsigned long) &ia64_ret_from_clone;
476
477         /* copy parts of thread_struct: */
478         p->thread.ksp = (unsigned long) child_stack - 16;
479
480         /* stop some PSR bits from being inherited.
481          * the psr.up/psr.pp bits must be cleared on fork but inherited on execve()
482          * therefore we must specify them explicitly here and not include them in
483          * IA64_PSR_BITS_TO_CLEAR.
484          */
485         child_ptregs->cr_ipsr = ((child_ptregs->cr_ipsr | IA64_PSR_BITS_TO_SET)
486                                  & ~(IA64_PSR_BITS_TO_CLEAR | IA64_PSR_PP | IA64_PSR_UP));
487
488         /*
489          * NOTE: The calling convention considers all floating point
490          * registers in the high partition (fph) to be scratch.  Since
491          * the only way to get to this point is through a system call,
492          * we know that the values in fph are all dead.  Hence, there
493          * is no need to inherit the fph state from the parent to the
494          * child and all we have to do is to make sure that
495          * IA64_THREAD_FPH_VALID is cleared in the child.
496          *
497          * XXX We could push this optimization a bit further by
498          * clearing IA64_THREAD_FPH_VALID on ANY system call.
499          * However, it's not clear this is worth doing.  Also, it
500          * would be a slight deviation from the normal Linux system
501          * call behavior where scratch registers are preserved across
502          * system calls (unless used by the system call itself).
503          */
504 #       define THREAD_FLAGS_TO_CLEAR    (IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID \
505                                          | IA64_THREAD_PM_VALID)
506 #       define THREAD_FLAGS_TO_SET      0
507         p->thread.flags = ((current->thread.flags & ~THREAD_FLAGS_TO_CLEAR)
508                            | THREAD_FLAGS_TO_SET);
509         ia64_drop_fpu(p);       /* don't pick up stale state from a CPU's fph */
510 #ifdef CONFIG_IA32_SUPPORT
511         /*
512          * If we're cloning an IA32 task then save the IA32 extra
513          * state from the current task to the new task
514          */
515         if (IS_IA32_PROCESS(task_pt_regs(current))) {
516                 ia32_save_state(p);
517                 if (clone_flags & CLONE_SETTLS)
518                         retval = ia32_clone_tls(p, child_ptregs);
519
520                 /* Copy partially mapped page list */
521                 if (!retval)
522                         retval = ia32_copy_ia64_partial_page_list(p,
523                                                                 clone_flags);
524         }
525 #endif
526
527 #ifdef CONFIG_PERFMON
528         if (current->thread.pfm_context)
529                 pfm_inherit(p, child_ptregs);
530 #endif
531         return retval;
532 }
533
534 static void
535 do_copy_task_regs (struct task_struct *task, struct unw_frame_info *info, void *arg)
536 {
537         unsigned long mask, sp, nat_bits = 0, ar_rnat, urbs_end, cfm;
538         unsigned long uninitialized_var(ip);    /* GCC be quiet */
539         elf_greg_t *dst = arg;
540         struct pt_regs *pt;
541         char nat;
542         int i;
543
544         memset(dst, 0, sizeof(elf_gregset_t));  /* don't leak any kernel bits to user-level */
545
546         if (unw_unwind_to_user(info) < 0)
547                 return;
548
549         unw_get_sp(info, &sp);
550         pt = (struct pt_regs *) (sp + 16);
551
552         urbs_end = ia64_get_user_rbs_end(task, pt, &cfm);
553
554         if (ia64_sync_user_rbs(task, info->sw, pt->ar_bspstore, urbs_end) < 0)
555                 return;
556
557         ia64_peek(task, info->sw, urbs_end, (long) ia64_rse_rnat_addr((long *) urbs_end),
558                   &ar_rnat);
559
560         /*
561          * coredump format:
562          *      r0-r31
563          *      NaT bits (for r0-r31; bit N == 1 iff rN is a NaT)
564          *      predicate registers (p0-p63)
565          *      b0-b7
566          *      ip cfm user-mask
567          *      ar.rsc ar.bsp ar.bspstore ar.rnat
568          *      ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec
569          */
570
571         /* r0 is zero */
572         for (i = 1, mask = (1UL << i); i < 32; ++i) {
573                 unw_get_gr(info, i, &dst[i], &nat);
574                 if (nat)
575                         nat_bits |= mask;
576                 mask <<= 1;
577         }
578         dst[32] = nat_bits;
579         unw_get_pr(info, &dst[33]);
580
581         for (i = 0; i < 8; ++i)
582                 unw_get_br(info, i, &dst[34 + i]);
583
584         unw_get_rp(info, &ip);
585         dst[42] = ip + ia64_psr(pt)->ri;
586         dst[43] = cfm;
587         dst[44] = pt->cr_ipsr & IA64_PSR_UM;
588
589         unw_get_ar(info, UNW_AR_RSC, &dst[45]);
590         /*
591          * For bsp and bspstore, unw_get_ar() would return the kernel
592          * addresses, but we need the user-level addresses instead:
593          */
594         dst[46] = urbs_end;     /* note: by convention PT_AR_BSP points to the end of the urbs! */
595         dst[47] = pt->ar_bspstore;
596         dst[48] = ar_rnat;
597         unw_get_ar(info, UNW_AR_CCV, &dst[49]);
598         unw_get_ar(info, UNW_AR_UNAT, &dst[50]);
599         unw_get_ar(info, UNW_AR_FPSR, &dst[51]);
600         dst[52] = pt->ar_pfs;   /* UNW_AR_PFS is == to pt->cr_ifs for interrupt frames */
601         unw_get_ar(info, UNW_AR_LC, &dst[53]);
602         unw_get_ar(info, UNW_AR_EC, &dst[54]);
603         unw_get_ar(info, UNW_AR_CSD, &dst[55]);
604         unw_get_ar(info, UNW_AR_SSD, &dst[56]);
605 }
606
607 void
608 do_dump_task_fpu (struct task_struct *task, struct unw_frame_info *info, void *arg)
609 {
610         elf_fpreg_t *dst = arg;
611         int i;
612
613         memset(dst, 0, sizeof(elf_fpregset_t)); /* don't leak any "random" bits */
614
615         if (unw_unwind_to_user(info) < 0)
616                 return;
617
618         /* f0 is 0.0, f1 is 1.0 */
619
620         for (i = 2; i < 32; ++i)
621                 unw_get_fr(info, i, dst + i);
622
623         ia64_flush_fph(task);
624         if ((task->thread.flags & IA64_THREAD_FPH_VALID) != 0)
625                 memcpy(dst + 32, task->thread.fph, 96*16);
626 }
627
628 void
629 do_copy_regs (struct unw_frame_info *info, void *arg)
630 {
631         do_copy_task_regs(current, info, arg);
632 }
633
634 void
635 do_dump_fpu (struct unw_frame_info *info, void *arg)
636 {
637         do_dump_task_fpu(current, info, arg);
638 }
639
640 void
641 ia64_elf_core_copy_regs (struct pt_regs *pt, elf_gregset_t dst)
642 {
643         unw_init_running(do_copy_regs, dst);
644 }
645
646 int
647 dump_fpu (struct pt_regs *pt, elf_fpregset_t dst)
648 {
649         unw_init_running(do_dump_fpu, dst);
650         return 1;       /* f0-f31 are always valid so we always return 1 */
651 }
652
653 long
654 sys_execve (char __user *filename, char __user * __user *argv, char __user * __user *envp,
655             struct pt_regs *regs)
656 {
657         char *fname;
658         int error;
659
660         fname = getname(filename);
661         error = PTR_ERR(fname);
662         if (IS_ERR(fname))
663                 goto out;
664         error = do_execve(fname, argv, envp, regs);
665         putname(fname);
666 out:
667         return error;
668 }
669
670 pid_t
671 kernel_thread (int (*fn)(void *), void *arg, unsigned long flags)
672 {
673         extern void start_kernel_thread (void);
674         unsigned long *helper_fptr = (unsigned long *) &start_kernel_thread;
675         struct {
676                 struct switch_stack sw;
677                 struct pt_regs pt;
678         } regs;
679
680         memset(&regs, 0, sizeof(regs));
681         regs.pt.cr_iip = helper_fptr[0];        /* set entry point (IP) */
682         regs.pt.r1 = helper_fptr[1];            /* set GP */
683         regs.pt.r9 = (unsigned long) fn;        /* 1st argument */
684         regs.pt.r11 = (unsigned long) arg;      /* 2nd argument */
685         /* Preserve PSR bits, except for bits 32-34 and 37-45, which we can't read.  */
686         regs.pt.cr_ipsr = ia64_getreg(_IA64_REG_PSR) | IA64_PSR_BN;
687         regs.pt.cr_ifs = 1UL << 63;             /* mark as valid, empty frame */
688         regs.sw.ar_fpsr = regs.pt.ar_fpsr = ia64_getreg(_IA64_REG_AR_FPSR);
689         regs.sw.ar_bspstore = (unsigned long) current + IA64_RBS_OFFSET;
690         regs.sw.pr = (1 << PRED_KERNEL_STACK);
691         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs.pt, 0, NULL, NULL);
692 }
693 EXPORT_SYMBOL(kernel_thread);
694
695 /* This gets called from kernel_thread() via ia64_invoke_thread_helper().  */
696 int
697 kernel_thread_helper (int (*fn)(void *), void *arg)
698 {
699 #ifdef CONFIG_IA32_SUPPORT
700         if (IS_IA32_PROCESS(task_pt_regs(current))) {
701                 /* A kernel thread is always a 64-bit process. */
702                 current->thread.map_base  = DEFAULT_MAP_BASE;
703                 current->thread.task_size = DEFAULT_TASK_SIZE;
704                 ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
705                 ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
706         }
707 #endif
708         return (*fn)(arg);
709 }
710
711 /*
712  * Flush thread state.  This is called when a thread does an execve().
713  */
714 void
715 flush_thread (void)
716 {
717         /* drop floating-point and debug-register state if it exists: */
718         current->thread.flags &= ~(IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID);
719         ia64_drop_fpu(current);
720 #ifdef CONFIG_IA32_SUPPORT
721         if (IS_IA32_PROCESS(task_pt_regs(current))) {
722                 ia32_drop_ia64_partial_page_list(current);
723                 current->thread.task_size = IA32_PAGE_OFFSET;
724                 set_fs(USER_DS);
725                 memset(current->thread.tls_array, 0, sizeof(current->thread.tls_array));
726         }
727 #endif
728 }
729
730 /*
731  * Clean up state associated with current thread.  This is called when
732  * the thread calls exit().
733  */
734 void
735 exit_thread (void)
736 {
737
738         ia64_drop_fpu(current);
739 #ifdef CONFIG_PERFMON
740        /* if needed, stop monitoring and flush state to perfmon context */
741         if (current->thread.pfm_context)
742                 pfm_exit_thread(current);
743
744         /* free debug register resources */
745         if (current->thread.flags & IA64_THREAD_DBG_VALID)
746                 pfm_release_debug_registers(current);
747 #endif
748         if (IS_IA32_PROCESS(task_pt_regs(current)))
749                 ia32_drop_ia64_partial_page_list(current);
750 }
751
752 unsigned long
753 get_wchan (struct task_struct *p)
754 {
755         struct unw_frame_info info;
756         unsigned long ip;
757         int count = 0;
758
759         if (!p || p == current || p->state == TASK_RUNNING)
760                 return 0;
761
762         /*
763          * Note: p may not be a blocked task (it could be current or
764          * another process running on some other CPU.  Rather than
765          * trying to determine if p is really blocked, we just assume
766          * it's blocked and rely on the unwind routines to fail
767          * gracefully if the process wasn't really blocked after all.
768          * --davidm 99/12/15
769          */
770         unw_init_from_blocked_task(&info, p);
771         do {
772                 if (p->state == TASK_RUNNING)
773                         return 0;
774                 if (unw_unwind(&info) < 0)
775                         return 0;
776                 unw_get_ip(&info, &ip);
777                 if (!in_sched_functions(ip))
778                         return ip;
779         } while (count++ < 16);
780         return 0;
781 }
782
783 void
784 cpu_halt (void)
785 {
786         pal_power_mgmt_info_u_t power_info[8];
787         unsigned long min_power;
788         int i, min_power_state;
789
790         if (ia64_pal_halt_info(power_info) != 0)
791                 return;
792
793         min_power_state = 0;
794         min_power = power_info[0].pal_power_mgmt_info_s.power_consumption;
795         for (i = 1; i < 8; ++i)
796                 if (power_info[i].pal_power_mgmt_info_s.im
797                     && power_info[i].pal_power_mgmt_info_s.power_consumption < min_power) {
798                         min_power = power_info[i].pal_power_mgmt_info_s.power_consumption;
799                         min_power_state = i;
800                 }
801
802         while (1)
803                 ia64_pal_halt(min_power_state);
804 }
805
806 void machine_shutdown(void)
807 {
808 #ifdef CONFIG_HOTPLUG_CPU
809         int cpu;
810
811         for_each_online_cpu(cpu) {
812                 if (cpu != smp_processor_id())
813                         cpu_down(cpu);
814         }
815 #endif
816 #ifdef CONFIG_KEXEC
817         kexec_disable_iosapic();
818 #endif
819 }
820
821 void
822 machine_restart (char *restart_cmd)
823 {
824         (void) notify_die(DIE_MACHINE_RESTART, restart_cmd, NULL, 0, 0, 0);
825         (*efi.reset_system)(EFI_RESET_WARM, 0, 0, NULL);
826 }
827
828 void
829 machine_halt (void)
830 {
831         (void) notify_die(DIE_MACHINE_HALT, "", NULL, 0, 0, 0);
832         cpu_halt();
833 }
834
835 void
836 machine_power_off (void)
837 {
838         if (pm_power_off)
839                 pm_power_off();
840         machine_halt();
841 }
842