added some suse-specific patches to the kernel.
[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_IA64_SGI_SN
37 #include <asm/sn/idle.h>
38 #endif
39
40 #ifdef CONFIG_PERFMON
41 # include <asm/perfmon.h>
42 #endif
43
44 #include "sigframe.h"
45
46 static void
47 do_show_stack (struct unw_frame_info *info, void *arg)
48 {
49         unsigned long ip, sp, bsp;
50         char buf[80];                   /* don't make it so big that it overflows the stack! */
51
52         printk("\nCall Trace:\n");
53         do {
54                 unw_get_ip(info, &ip);
55                 if (ip == 0)
56                         break;
57
58                 unw_get_sp(info, &sp);
59                 unw_get_bsp(info, &bsp);
60                 snprintf(buf, sizeof(buf), " [<%016lx>] %%s sp=0x%016lx bsp=0x%016lx\n",
61                          ip, sp, bsp);
62                 print_symbol(buf, ip);
63         } while (unw_unwind(info) >= 0);
64 }
65
66 void
67 show_trace_task (struct task_struct *task)
68 {
69         show_stack(task);
70 }
71
72 void
73 show_stack (struct task_struct *task)
74 {
75         if (!task)
76                 unw_init_running(do_show_stack, 0);
77         else {
78                 struct unw_frame_info info;
79
80                 unw_init_from_blocked_task(&info, task);
81                 do_show_stack(&info, 0);
82         }
83 }
84
85 void
86 dump_stack (void)
87 {
88         show_stack(NULL);
89 }
90
91 void
92 show_regs (struct pt_regs *regs)
93 {
94         unsigned long ip = regs->cr_iip + ia64_psr(regs)->ri;
95
96         printk("\nPid: %d, CPU %d, comm: %20s\n", current->pid, smp_processor_id(), current->comm);
97         printk("psr : %016lx ifs : %016lx ip  : [<%016lx>]    %s\n",
98                regs->cr_ipsr, regs->cr_ifs, ip, print_tainted());
99         print_symbol("ip is at %s\n", ip);
100         printk("unat: %016lx pfs : %016lx rsc : %016lx\n",
101                regs->ar_unat, regs->ar_pfs, regs->ar_rsc);
102         printk("rnat: %016lx bsps: %016lx pr  : %016lx\n",
103                regs->ar_rnat, regs->ar_bspstore, regs->pr);
104         printk("ldrs: %016lx ccv : %016lx fpsr: %016lx\n",
105                regs->loadrs, regs->ar_ccv, regs->ar_fpsr);
106         printk("b0  : %016lx b6  : %016lx b7  : %016lx\n", regs->b0, regs->b6, regs->b7);
107         printk("f6  : %05lx%016lx f7  : %05lx%016lx\n",
108                regs->f6.u.bits[1], regs->f6.u.bits[0],
109                regs->f7.u.bits[1], regs->f7.u.bits[0]);
110         printk("f8  : %05lx%016lx f9  : %05lx%016lx\n",
111                regs->f8.u.bits[1], regs->f8.u.bits[0],
112                regs->f9.u.bits[1], regs->f9.u.bits[0]);
113
114         printk("r1  : %016lx r2  : %016lx r3  : %016lx\n", regs->r1, regs->r2, regs->r3);
115         printk("r8  : %016lx r9  : %016lx r10 : %016lx\n", regs->r8, regs->r9, regs->r10);
116         printk("r11 : %016lx r12 : %016lx r13 : %016lx\n", regs->r11, regs->r12, regs->r13);
117         printk("r14 : %016lx r15 : %016lx r16 : %016lx\n", regs->r14, regs->r15, regs->r16);
118         printk("r17 : %016lx r18 : %016lx r19 : %016lx\n", regs->r17, regs->r18, regs->r19);
119         printk("r20 : %016lx r21 : %016lx r22 : %016lx\n", regs->r20, regs->r21, regs->r22);
120         printk("r23 : %016lx r24 : %016lx r25 : %016lx\n", regs->r23, regs->r24, regs->r25);
121         printk("r26 : %016lx r27 : %016lx r28 : %016lx\n", regs->r26, regs->r27, regs->r28);
122         printk("r29 : %016lx r30 : %016lx r31 : %016lx\n", regs->r29, regs->r30, regs->r31);
123
124         if (user_mode(regs)) {
125                 /* print the stacked registers */
126                 unsigned long val, sof, *bsp, ndirty;
127                 int i, is_nat = 0;
128
129                 sof = regs->cr_ifs & 0x7f;      /* size of frame */
130                 ndirty = (regs->loadrs >> 19);
131                 bsp = ia64_rse_skip_regs((unsigned long *) regs->ar_bspstore, ndirty);
132                 for (i = 0; i < sof; ++i) {
133                         get_user(val, ia64_rse_skip_regs(bsp, i));
134                         printk("r%-3u:%c%016lx%s", 32 + i, is_nat ? '*' : ' ', val,
135                                ((i == sof - 1) || (i % 3) == 2) ? "\n" : " ");
136                 }
137         } else
138                 show_stack(0);
139 }
140
141 void
142 do_notify_resume_user (sigset_t *oldset, struct sigscratch *scr, long in_syscall)
143 {
144 #ifdef CONFIG_FSYS
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 #endif
152
153 #ifdef CONFIG_PERFMON
154         if (current->thread.pfm_ovfl_block_reset)
155                 pfm_ovfl_block_reset();
156 #endif
157
158         /* deal with pending signal delivery */
159         if (test_thread_flag(TIF_SIGPENDING))
160                 ia64_do_signal(oldset, scr, in_syscall);
161 }
162
163 /*
164  * We use this if we don't have any better idle routine..
165  */
166 void
167 default_idle (void)
168 {
169 #ifdef CONFIG_IA64_PAL_IDLE
170         if (!need_resched())
171                 safe_halt();
172 #endif
173 }
174
175 void __attribute__((noreturn))
176 cpu_idle (void *unused)
177 {
178         /* endless idle loop with no priority at all */
179         while (1) {
180                 void (*idle)(void) = pm_idle;
181                 if (!idle)
182                         idle = default_idle;
183
184 #ifdef CONFIG_SMP
185                 if (!need_resched())
186                         min_xtp();
187 #endif
188
189                 while (!need_resched()) {
190 #ifdef CONFIG_IA64_SGI_SN
191                         snidle();
192 #endif
193                         (*idle)();
194                 }
195
196 #ifdef CONFIG_IA64_SGI_SN
197                 snidleoff();
198 #endif
199
200 #ifdef CONFIG_SMP
201                 normal_xtp();
202 #endif
203                 schedule();
204                 check_pgt_cache();
205         }
206 }
207
208 void
209 ia64_save_extra (struct task_struct *task)
210 {
211 #ifdef CONFIG_PERFMON
212         unsigned long info;
213 #endif
214
215         if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
216                 ia64_save_debug_regs(&task->thread.dbr[0]);
217
218 #ifdef CONFIG_PERFMON
219         if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
220                 pfm_save_regs(task);
221
222         info = __get_cpu_var(pfm_syst_info);
223         if (info & PFM_CPUINFO_SYST_WIDE)
224                 pfm_syst_wide_update_task(task, info, 0);
225 #endif
226
227 #ifdef CONFIG_IA32_SUPPORT
228         if (IS_IA32_PROCESS(ia64_task_regs(task)))
229                 ia32_save_state(task);
230 #endif
231 }
232
233 void
234 ia64_load_extra (struct task_struct *task)
235 {
236 #ifdef CONFIG_PERFMON
237         unsigned long info;
238 #endif
239
240         if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
241                 ia64_load_debug_regs(&task->thread.dbr[0]);
242
243 #ifdef CONFIG_PERFMON
244         if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
245                 pfm_load_regs(task);
246
247         info = __get_cpu_var(pfm_syst_info);
248         if (info & PFM_CPUINFO_SYST_WIDE) 
249                 pfm_syst_wide_update_task(task, info, 1);
250 #endif
251
252 #ifdef CONFIG_IA32_SUPPORT
253         if (IS_IA32_PROCESS(ia64_task_regs(task)))
254                 ia32_load_state(task);
255 #endif
256 }
257
258 /*
259  * Copy the state of an ia-64 thread.
260  *
261  * We get here through the following  call chain:
262  *
263  *      <clone syscall>
264  *      sys_clone
265  *      do_fork
266  *      copy_thread
267  *
268  * This means that the stack layout is as follows:
269  *
270  *      +---------------------+ (highest addr)
271  *      |   struct pt_regs    |
272  *      +---------------------+
273  *      | struct switch_stack |
274  *      +---------------------+
275  *      |                     |
276  *      |    memory stack     |
277  *      |                     | <-- sp (lowest addr)
278  *      +---------------------+
279  *
280  * Note: if we get called through kernel_thread() then the memory above "(highest addr)"
281  * is valid kernel stack memory that needs to be copied as well.
282  *
283  * Observe that we copy the unat values that are in pt_regs and switch_stack.  Spilling an
284  * integer to address X causes bit N in ar.unat to be set to the NaT bit of the register,
285  * with N=(X & 0x1ff)/8.  Thus, copying the unat value preserves the NaT bits ONLY if the
286  * pt_regs structure in the parent is congruent to that of the child, modulo 512.  Since
287  * the stack is page aligned and the page size is at least 4KB, this is always the case,
288  * so there is nothing to worry about.
289  */
290 int
291 copy_thread (int nr, unsigned long clone_flags,
292              unsigned long user_stack_base, unsigned long user_stack_size,
293              struct task_struct *p, struct pt_regs *regs)
294 {
295         unsigned long rbs, child_rbs, rbs_size, stack_offset, stack_top, stack_used;
296         struct switch_stack *child_stack, *stack;
297         extern char ia64_ret_from_clone, ia32_ret_from_clone;
298         struct pt_regs *child_ptregs;
299         int retval = 0;
300
301 #ifdef CONFIG_SMP
302         /*
303          * For SMP idle threads, fork_by_hand() calls do_fork with
304          * NULL regs.
305          */
306         if (!regs)
307                 return 0;
308 #endif
309
310         stack_top = (unsigned long) current + IA64_STK_OFFSET;
311         stack = ((struct switch_stack *) regs) - 1;
312         stack_used = stack_top - (unsigned long) stack;
313         stack_offset = IA64_STK_OFFSET - stack_used;
314
315         child_stack = (struct switch_stack *) ((unsigned long) p + stack_offset);
316         child_ptregs = (struct pt_regs *) (child_stack + 1);
317
318         /* copy parent's switch_stack & pt_regs to child: */
319         memcpy(child_stack, stack, stack_used);
320
321         rbs = (unsigned long) current + IA64_RBS_OFFSET;
322         child_rbs = (unsigned long) p + IA64_RBS_OFFSET;
323         rbs_size = stack->ar_bspstore - rbs;
324
325         /* copy the parent's register backing store to the child: */
326         memcpy((void *) child_rbs, (void *) rbs, rbs_size);
327
328         if (user_mode(child_ptregs)) {
329                 if (clone_flags & CLONE_SETTLS)
330                         child_ptregs->r13 = regs->r16;  /* see sys_clone2() in entry.S */
331                 if (user_stack_base) {
332                         child_ptregs->r12 = user_stack_base + user_stack_size - 16;
333                         child_ptregs->ar_bspstore = user_stack_base;
334                         child_ptregs->ar_rnat = 0;
335                         child_ptregs->loadrs = 0;
336                 }
337         } else {
338                 /*
339                  * Note: we simply preserve the relative position of
340                  * the stack pointer here.  There is no need to
341                  * allocate a scratch area here, since that will have
342                  * been taken care of by the caller of sys_clone()
343                  * already.
344                  */
345                 child_ptregs->r12 = (unsigned long) (child_ptregs + 1); /* kernel sp */
346                 child_ptregs->r13 = (unsigned long) p;          /* set `current' pointer */
347         }
348         if (IS_IA32_PROCESS(regs))
349                 child_stack->b0 = (unsigned long) &ia32_ret_from_clone;
350         else
351                 child_stack->b0 = (unsigned long) &ia64_ret_from_clone;
352         child_stack->ar_bspstore = child_rbs + rbs_size;
353
354         /* copy parts of thread_struct: */
355         p->thread.ksp = (unsigned long) child_stack - 16;
356
357         /* stop some PSR bits from being inherited: */
358         child_ptregs->cr_ipsr =  ((child_ptregs->cr_ipsr | IA64_PSR_BITS_TO_SET)
359                                   & ~IA64_PSR_BITS_TO_CLEAR);
360
361         /*
362          * NOTE: The calling convention considers all floating point
363          * registers in the high partition (fph) to be scratch.  Since
364          * the only way to get to this point is through a system call,
365          * we know that the values in fph are all dead.  Hence, there
366          * is no need to inherit the fph state from the parent to the
367          * child and all we have to do is to make sure that
368          * IA64_THREAD_FPH_VALID is cleared in the child.
369          *
370          * XXX We could push this optimization a bit further by
371          * clearing IA64_THREAD_FPH_VALID on ANY system call.
372          * However, it's not clear this is worth doing.  Also, it
373          * would be a slight deviation from the normal Linux system
374          * call behavior where scratch registers are preserved across
375          * system calls (unless used by the system call itself).
376          */
377 #       define THREAD_FLAGS_TO_CLEAR    (IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID \
378                                          | IA64_THREAD_PM_VALID)
379 #       define THREAD_FLAGS_TO_SET      0
380         p->thread.flags = ((current->thread.flags & ~THREAD_FLAGS_TO_CLEAR)
381                            | THREAD_FLAGS_TO_SET);
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, tid;
582
583         tid = clone(flags | CLONE_VM | CLONE_UNTRACED, 0);
584         if (parent != current) {
585 #ifdef CONFIG_IA32_SUPPORT
586                 if (IS_IA32_PROCESS(ia64_task_regs(current))) {
587                         /* A kernel thread is always a 64-bit process. */
588                         current->thread.map_base  = DEFAULT_MAP_BASE;
589                         current->thread.task_size = DEFAULT_TASK_SIZE;
590                         ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
591                         ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
592                 }
593 #endif
594                 result = (*fn)(arg);
595                 _exit(result);
596         }
597         return tid;
598 }
599
600 /*
601  * Flush thread state.  This is called when a thread does an execve().
602  */
603 void
604 flush_thread (void)
605 {
606         /* drop floating-point and debug-register state if it exists: */
607         current->thread.flags &= ~(IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID);
608
609 #ifndef CONFIG_SMP
610         if (ia64_get_fpu_owner() == current)
611                 ia64_set_fpu_owner(0);
612 #endif
613 }
614
615 #ifdef CONFIG_PERFMON
616 /*
617  * by the time we get here, the task is detached from the tasklist. This is important
618  * because it means that no other tasks can ever find it as a notified task, therfore there
619  * is no race condition between this code and let's say a pfm_context_create().
620  * Conversely, the pfm_cleanup_notifiers() cannot try to access a task's pfm context if this
621  * other task is in the middle of its own pfm_context_exit() because it would already be out of
622  * the task list. Note that this case is very unlikely between a direct child and its parents
623  * (if it is the notified process) because of the way the exit is notified via SIGCHLD.
624  */
625
626 void
627 release_thread (struct task_struct *task)
628 {
629         if (task->thread.pfm_context)
630                 pfm_context_exit(task);
631
632         if (atomic_read(&task->thread.pfm_notifiers_check) > 0)
633                 pfm_cleanup_notifiers(task);
634
635         if (atomic_read(&task->thread.pfm_owners_check) > 0)
636                 pfm_cleanup_owners(task);
637
638         if (task->thread.pfm_smpl_buf_list)
639                 pfm_cleanup_smpl_buf(task);
640 }
641 #endif
642
643 /*
644  * Clean up state associated with current thread.  This is called when
645  * the thread calls exit().
646  */
647 void
648 exit_thread (void)
649 {
650 #ifndef CONFIG_SMP
651         if (ia64_get_fpu_owner() == current)
652                 ia64_set_fpu_owner(0);
653 #endif
654 #ifdef CONFIG_PERFMON
655        /* if needed, stop monitoring and flush state to perfmon context */
656         if (current->thread.pfm_context) 
657                 pfm_flush_regs(current);
658
659         /* free debug register resources */
660         if (current->thread.flags & IA64_THREAD_DBG_VALID)
661                 pfm_release_debug_registers(current);
662 #endif
663 }
664
665 unsigned long
666 get_wchan (struct task_struct *p)
667 {
668         struct unw_frame_info info;
669         unsigned long ip;
670         int count = 0;
671         /*
672          * These bracket the sleeping functions..
673          */
674         extern void scheduling_functions_start_here(void);
675         extern void scheduling_functions_end_here(void);
676 #       define first_sched      ((unsigned long) scheduling_functions_start_here)
677 #       define last_sched       ((unsigned long) scheduling_functions_end_here)
678
679         /*
680          * Note: p may not be a blocked task (it could be current or
681          * another process running on some other CPU.  Rather than
682          * trying to determine if p is really blocked, we just assume
683          * it's blocked and rely on the unwind routines to fail
684          * gracefully if the process wasn't really blocked after all.
685          * --davidm 99/12/15
686          */
687         unw_init_from_blocked_task(&info, p);
688         do {
689                 if (unw_unwind(&info) < 0)
690                         return 0;
691                 unw_get_ip(&info, &ip);
692                 if (ip < first_sched || ip >= last_sched)
693                         return ip;
694         } while (count++ < 16);
695         return 0;
696 #       undef first_sched
697 #       undef last_sched
698 }
699
700 void
701 cpu_halt (void)
702 {
703         pal_power_mgmt_info_u_t power_info[8];
704         unsigned long min_power;
705         int i, min_power_state;
706
707         if (ia64_pal_halt_info(power_info) != 0)
708                 return;
709
710         min_power_state = 0;
711         min_power = power_info[0].pal_power_mgmt_info_s.power_consumption;
712         for (i = 1; i < 8; ++i)
713                 if (power_info[i].pal_power_mgmt_info_s.im
714                     && power_info[i].pal_power_mgmt_info_s.power_consumption < min_power) {
715                         min_power = power_info[i].pal_power_mgmt_info_s.power_consumption;
716                         min_power_state = i;
717                 }
718
719         while (1)
720                 ia64_pal_halt(min_power_state);
721 }
722
723 void
724 machine_restart (char *restart_cmd)
725 {
726         (*efi.reset_system)(EFI_RESET_WARM, 0, 0, 0);
727 }
728
729 void
730 machine_halt (void)
731 {
732         cpu_halt();
733 }
734
735 void
736 machine_power_off (void)
737 {
738         if (pm_power_off)
739                 pm_power_off();
740         machine_halt();
741 }
742
743 void __init
744 init_task_struct_cache (void)
745 {
746 }
747
748 struct task_struct *
749 dup_task_struct(struct task_struct *orig)
750 {
751         struct task_struct *tsk;
752
753         tsk = (void *) __get_free_pages(GFP_KERNEL, KERNEL_STACK_SIZE_ORDER);
754         if (!tsk)
755                 return NULL;
756
757         memcpy(tsk, orig, sizeof(struct task_struct) + sizeof(struct thread_info));
758         tsk->thread_info = (struct thread_info *) ((char *) tsk + IA64_TASK_SIZE);
759         atomic_set(&tsk->usage, 2);
760         return tsk;
761 }
762
763 void
764 free_task_struct (struct task_struct *tsk)
765 {
766         free_pages((unsigned long) tsk, KERNEL_STACK_SIZE_ORDER);
767 }