2 * Architecture-specific setup.
4 * Copyright (C) 1998-2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
7 #define __KERNEL_SYSCALLS__ /* see <asm/unistd.h> */
8 #include <linux/config.h>
11 #include <linux/elf.h>
12 #include <linux/errno.h>
13 #include <linux/kallsyms.h>
14 #include <linux/kernel.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>
26 #include <asm/delay.h>
28 #include <asm/perfmon.h>
29 #include <asm/pgalloc.h>
30 #include <asm/processor.h>
32 #include <asm/uaccess.h>
33 #include <asm/unwind.h>
37 # include <asm/perfmon.h>
42 void (*ia64_mark_idle)(int);
46 ia64_do_show_stack (struct unw_frame_info *info, void *arg)
48 unsigned long ip, sp, bsp;
49 char buf[80]; /* don't make it so big that it overflows the stack! */
51 printk("\nCall Trace:\n");
53 unw_get_ip(info, &ip);
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",
61 print_symbol(buf, ip);
62 } while (unw_unwind(info) >= 0);
66 show_trace_task (struct task_struct *task)
72 show_stack (struct task_struct *task)
75 unw_init_running(ia64_do_show_stack, 0);
77 struct unw_frame_info info;
79 unw_init_from_blocked_task(&info, task);
80 ia64_do_show_stack(&info, 0);
91 show_regs (struct pt_regs *regs)
93 unsigned long ip = regs->cr_iip + ia64_psr(regs)->ri;
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]);
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);
123 if (user_mode(regs)) {
124 /* print the stacked registers */
125 unsigned long val, *bsp, ndirty;
126 int i, sof, is_nat = 0;
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" : " ");
141 do_notify_resume_user (sigset_t *oldset, struct sigscratch *scr, long in_syscall)
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;
152 #ifdef CONFIG_PERFMON
153 if (current->thread.pfm_ovfl_block_reset)
154 pfm_ovfl_block_reset();
157 /* deal with pending signal delivery */
158 if (test_thread_flag(TIF_SIGPENDING))
159 ia64_do_signal(oldset, scr, in_syscall);
163 * We use this if we don't have any better idle routine..
168 #ifdef CONFIG_IA64_PAL_IDLE
174 void __attribute__((noreturn))
175 cpu_idle (void *unused)
177 void (*mark_idle)(int) = ia64_mark_idle;
179 /* endless idle loop with no priority at all */
181 void (*idle)(void) = pm_idle;
190 while (!need_resched()) {
208 ia64_save_extra (struct task_struct *task)
210 #ifdef CONFIG_PERFMON
214 if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
215 ia64_save_debug_regs(&task->thread.dbr[0]);
217 #ifdef CONFIG_PERFMON
218 if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
221 info = __get_cpu_var(pfm_syst_info);
222 if (info & PFM_CPUINFO_SYST_WIDE)
223 pfm_syst_wide_update_task(task, info, 0);
226 #ifdef CONFIG_IA32_SUPPORT
227 if (IS_IA32_PROCESS(ia64_task_regs(task)))
228 ia32_save_state(task);
233 ia64_load_extra (struct task_struct *task)
235 #ifdef CONFIG_PERFMON
239 if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0)
240 ia64_load_debug_regs(&task->thread.dbr[0]);
242 #ifdef CONFIG_PERFMON
243 if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0)
246 info = __get_cpu_var(pfm_syst_info);
247 if (info & PFM_CPUINFO_SYST_WIDE)
248 pfm_syst_wide_update_task(task, info, 1);
251 #ifdef CONFIG_IA32_SUPPORT
252 if (IS_IA32_PROCESS(ia64_task_regs(task)))
253 ia32_load_state(task);
258 * Copy the state of an ia-64 thread.
260 * We get here through the following call chain:
267 * This means that the stack layout is as follows:
269 * +---------------------+ (highest addr)
271 * +---------------------+
272 * | struct switch_stack |
273 * +---------------------+
276 * | | <-- sp (lowest addr)
277 * +---------------------+
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.
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.
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)
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;
302 * For SMP idle threads, fork_by_hand() calls do_fork with
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;
314 child_stack = (struct switch_stack *) ((unsigned long) p + stack_offset);
315 child_ptregs = (struct pt_regs *) (child_stack + 1);
317 /* copy parent's switch_stack & pt_regs to child: */
318 memcpy(child_stack, stack, stack_used);
320 rbs = (unsigned long) current + IA64_RBS_OFFSET;
321 child_rbs = (unsigned long) p + IA64_RBS_OFFSET;
322 rbs_size = stack->ar_bspstore - rbs;
324 /* copy the parent's register backing store to the child: */
325 memcpy((void *) child_rbs, (void *) rbs, rbs_size);
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;
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()
344 child_ptregs->r12 = (unsigned long) (child_ptregs + 1); /* kernel sp */
345 child_ptregs->r13 = (unsigned long) p; /* set `current' pointer */
347 if (IS_IA32_PROCESS(regs))
348 child_stack->b0 = (unsigned long) &ia32_ret_from_clone;
350 child_stack->b0 = (unsigned long) &ia64_ret_from_clone;
351 child_stack->ar_bspstore = child_rbs + rbs_size;
353 /* copy parts of thread_struct: */
354 p->thread.ksp = (unsigned long) child_stack - 16;
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);
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.
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).
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
384 * If we're cloning an IA32 task then save the IA32 extra
385 * state from the current task to the new task
387 if (IS_IA32_PROCESS(ia64_task_regs(current)))
391 #ifdef CONFIG_PERFMON
393 * reset notifiers and owner check (may not have a perfmon context)
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;
400 if (current->thread.pfm_context)
401 retval = pfm_inherit(p, child_ptregs);
407 do_copy_task_regs (struct task_struct *task, struct unw_frame_info *info, void *arg)
409 unsigned long mask, sp, nat_bits = 0, ip, ar_rnat, urbs_end, cfm;
410 elf_greg_t *dst = arg;
415 memset(dst, 0, sizeof(elf_gregset_t)); /* don't leak any kernel bits to user-level */
417 if (unw_unwind_to_user(info) < 0)
420 unw_get_sp(info, &sp);
421 pt = (struct pt_regs *) (sp + 16);
423 urbs_end = ia64_get_user_rbs_end(task, pt, &cfm);
425 if (ia64_sync_user_rbs(task, info->sw, pt->ar_bspstore, urbs_end) < 0)
428 ia64_peek(task, info->sw, urbs_end, (long) ia64_rse_rnat_addr((long *) urbs_end),
434 * NaT bits (for r0-r31; bit N == 1 iff rN is a NaT)
435 * predicate registers (p0-p63)
438 * ar.rsc ar.bsp ar.bspstore ar.rnat
439 * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec
443 for (i = 1, mask = (1UL << i); i < 32; ++i) {
444 unw_get_gr(info, i, &dst[i], &nat);
450 unw_get_pr(info, &dst[33]);
452 for (i = 0; i < 8; ++i)
453 unw_get_br(info, i, &dst[34 + i]);
455 unw_get_rp(info, &ip);
456 dst[42] = ip + ia64_psr(pt)->ri;
458 dst[44] = pt->cr_ipsr & IA64_PSR_UM;
460 unw_get_ar(info, UNW_AR_RSC, &dst[45]);
462 * For bsp and bspstore, unw_get_ar() would return the kernel
463 * addresses, but we need the user-level addresses instead:
465 dst[46] = urbs_end; /* note: by convention PT_AR_BSP points to the end of the urbs! */
466 dst[47] = pt->ar_bspstore;
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]);
477 do_dump_task_fpu (struct task_struct *task, struct unw_frame_info *info, void *arg)
479 elf_fpreg_t *dst = arg;
482 memset(dst, 0, sizeof(elf_fpregset_t)); /* don't leak any "random" bits */
484 if (unw_unwind_to_user(info) < 0)
487 /* f0 is 0.0, f1 is 1.0 */
489 for (i = 2; i < 32; ++i)
490 unw_get_fr(info, i, dst + i);
492 ia64_flush_fph(task);
493 if ((task->thread.flags & IA64_THREAD_FPH_VALID) != 0)
494 memcpy(dst + 32, task->thread.fph, 96*16);
498 do_copy_regs (struct unw_frame_info *info, void *arg)
500 do_copy_task_regs(current, info, arg);
504 do_dump_fpu (struct unw_frame_info *info, void *arg)
506 do_dump_task_fpu(current, info, arg);
510 dump_task_regs(struct task_struct *task, elf_gregset_t *regs)
512 struct unw_frame_info tcore_info;
514 if (current == task) {
515 unw_init_running(do_copy_regs, regs);
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);
525 ia64_elf_core_copy_regs (struct pt_regs *pt, elf_gregset_t dst)
527 unw_init_running(do_copy_regs, dst);
531 dump_task_fpu (struct task_struct *task, elf_fpregset_t *dst)
533 struct unw_frame_info tcore_info;
535 if (current == task) {
536 unw_init_running(do_dump_fpu, dst);
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);
546 dump_fpu (struct pt_regs *pt, elf_fpregset_t dst)
548 unw_init_running(do_dump_fpu, dst);
549 return 1; /* f0-f31 are always valid so we always return 1 */
553 sys_execve (char *filename, char **argv, char **envp, struct pt_regs *regs)
557 filename = getname(filename);
558 error = PTR_ERR(filename);
559 if (IS_ERR(filename))
561 error = do_execve(filename, argv, envp, regs);
568 ia64_set_personality (struct elf64_hdr *elf_ex, int ibcs2_interpreter)
570 set_personality(PER_LINUX);
571 if (elf_ex->e_flags & EF_IA_64_LINUX_EXECUTABLE_STACK)
572 current->thread.flags |= IA64_THREAD_XSTACK;
574 current->thread.flags &= ~IA64_THREAD_XSTACK;
578 kernel_thread (int (*fn)(void *), void *arg, unsigned long flags)
580 struct task_struct *parent = current;
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);
602 * Flush thread state. This is called when a thread does an execve().
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);
612 #ifdef CONFIG_PERFMON
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.
624 release_thread (struct task_struct *task)
626 if (task->thread.pfm_context)
627 pfm_context_exit(task);
629 if (atomic_read(&task->thread.pfm_notifiers_check) > 0)
630 pfm_cleanup_notifiers(task);
632 if (atomic_read(&task->thread.pfm_owners_check) > 0)
633 pfm_cleanup_owners(task);
635 if (task->thread.pfm_smpl_buf_list)
636 pfm_cleanup_smpl_buf(task);
641 * Clean up state associated with current thread. This is called when
642 * the thread calls exit().
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);
653 /* free debug register resources */
654 if (current->thread.flags & IA64_THREAD_DBG_VALID)
655 pfm_release_debug_registers(current);
660 get_wchan (struct task_struct *p)
662 struct unw_frame_info info;
666 * These bracket the sleeping functions..
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)
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.
681 unw_init_from_blocked_task(&info, p);
683 if (unw_unwind(&info) < 0)
685 unw_get_ip(&info, &ip);
686 if (ip < first_sched || ip >= last_sched)
688 } while (count++ < 16);
697 pal_power_mgmt_info_u_t power_info[8];
698 unsigned long min_power;
699 int i, min_power_state;
701 if (ia64_pal_halt_info(power_info) != 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;
714 ia64_pal_halt(min_power_state);
718 machine_restart (char *restart_cmd)
720 (*efi.reset_system)(EFI_RESET_WARM, 0, 0, 0);
730 machine_power_off (void)
738 init_task_struct_cache (void)
743 dup_task_struct(struct task_struct *orig)
745 struct task_struct *tsk;
747 tsk = (void *) __get_free_pages(GFP_KERNEL, KERNEL_STACK_SIZE_ORDER);
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);
758 free_task_struct (struct task_struct *tsk)
760 free_pages((unsigned long) tsk, KERNEL_STACK_SIZE_ORDER);