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