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