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