1/* 2 * Architecture-specific setup. 3 * 4 * Copyright (C) 1998-2002 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/kernel.h> 14#include <linux/mm.h> 15#include <linux/personality.h> 16#include <linux/sched.h> 17#include <linux/slab.h> 18#include <linux/smp_lock.h> 19#include <linux/stddef.h> 20#include <linux/unistd.h> 21#include <linux/efi.h> 22 23#include <asm/delay.h> 24#include <asm/perfmon.h> 25#include <asm/pgtable.h> 26#include <asm/processor.h> 27#include <asm/sal.h> 28#include <asm/uaccess.h> 29#include <asm/unwind.h> 30#include <asm/user.h> 31 32#ifdef CONFIG_IA64_SGI_SN 33#include <asm/sn/idle.h> 34#endif 35 36static void 37do_show_stack (struct unw_frame_info *info, void *arg) 38{ 39 unsigned long ip, sp, bsp; 40 41 printk("\nCall Trace: "); 42 do { 43 unw_get_ip(info, &ip); 44 if (ip == 0) 45 break; 46 47 unw_get_sp(info, &sp); 48 unw_get_bsp(info, &bsp); 49 printk("[<%016lx>] sp=0x%016lx bsp=0x%016lx\n", ip, sp, bsp); 50 } while (unw_unwind(info) >= 0); 51} 52 53void 54show_trace_task (struct task_struct *task) 55{ 56 struct unw_frame_info info; 57 58 unw_init_from_blocked_task(&info, task); 59 do_show_stack(&info, 0); 60} 61 62void 63show_stack (struct task_struct *task) 64{ 65 if (!task) 66 unw_init_running(do_show_stack, 0); 67 else { 68 struct unw_frame_info info; 69 70 unw_init_from_blocked_task(&info, task); 71 do_show_stack(&info, 0); 72 } 73} 74 75void 76show_regs (struct pt_regs *regs) 77{ 78 unsigned long ip = regs->cr_iip + ia64_psr(regs)->ri; 79 80 printk("\nPid: %d, comm: %20s\n", current->pid, current->comm); 81 printk("psr : %016lx ifs : %016lx ip : [<%016lx>] %s\n", 82 regs->cr_ipsr, regs->cr_ifs, ip, print_tainted()); 83 printk("unat: %016lx pfs : %016lx rsc : %016lx\n", 84 regs->ar_unat, regs->ar_pfs, regs->ar_rsc); 85 printk("rnat: %016lx bsps: %016lx pr : %016lx\n", 86 regs->ar_rnat, regs->ar_bspstore, regs->pr); 87 printk("ldrs: %016lx ccv : %016lx fpsr: %016lx\n", 88 regs->loadrs, regs->ar_ccv, regs->ar_fpsr); 89 printk("b0 : %016lx b6 : %016lx b7 : %016lx\n", regs->b0, regs->b6, regs->b7); 90 printk("f6 : %05lx%016lx f7 : %05lx%016lx\n", 91 regs->f6.u.bits[1], regs->f6.u.bits[0], 92 regs->f7.u.bits[1], regs->f7.u.bits[0]); 93 printk("f8 : %05lx%016lx f9 : %05lx%016lx\n", 94 regs->f8.u.bits[1], regs->f8.u.bits[0], 95 regs->f9.u.bits[1], regs->f9.u.bits[0]); 96 97 printk("r1 : %016lx r2 : %016lx r3 : %016lx\n", regs->r1, regs->r2, regs->r3); 98 printk("r8 : %016lx r9 : %016lx r10 : %016lx\n", regs->r8, regs->r9, regs->r10); 99 printk("r11 : %016lx r12 : %016lx r13 : %016lx\n", regs->r11, regs->r12, regs->r13); 100 printk("r14 : %016lx r15 : %016lx r16 : %016lx\n", regs->r14, regs->r15, regs->r16); 101 printk("r17 : %016lx r18 : %016lx r19 : %016lx\n", regs->r17, regs->r18, regs->r19); 102 printk("r20 : %016lx r21 : %016lx r22 : %016lx\n", regs->r20, regs->r21, regs->r22); 103 printk("r23 : %016lx r24 : %016lx r25 : %016lx\n", regs->r23, regs->r24, regs->r25); 104 printk("r26 : %016lx r27 : %016lx r28 : %016lx\n", regs->r26, regs->r27, regs->r28); 105 printk("r29 : %016lx r30 : %016lx r31 : %016lx\n", regs->r29, regs->r30, regs->r31); 106 107 if (user_mode(regs)) { 108 /* print the stacked registers */ 109 unsigned long val, sof, *bsp, ndirty; 110 int i, is_nat = 0; 111 112 sof = regs->cr_ifs & 0x7f; /* size of frame */ 113 ndirty = (regs->loadrs >> 19); 114 bsp = ia64_rse_skip_regs((unsigned long *) regs->ar_bspstore, ndirty); 115 for (i = 0; i < sof; ++i) { 116 get_user(val, ia64_rse_skip_regs(bsp, i)); 117 printk("r%-3u:%c%016lx%s", 32 + i, is_nat ? '*' : ' ', val, 118 ((i == sof - 1) || (i % 3) == 2) ? "\n" : " "); 119 } 120 } 121 if (!user_mode(regs)) 122 show_stack(0); 123} 124 125void __attribute__((noreturn)) 126cpu_idle (void *unused) 127{ 128 /* endless idle loop with no priority at all */ 129 init_idle(); 130 current->nice = 20; 131 current->counter = -100; 132 133 134 while (1) { 135#ifdef CONFIG_SMP 136 if (!current->need_resched) 137 min_xtp(); 138#endif 139 140 while (!current->need_resched) { 141#ifdef CONFIG_IA64_SGI_SN 142 snidle(); 143#endif 144 continue; 145 } 146 147#ifdef CONFIG_IA64_SGI_SN 148 snidleoff(); 149#endif 150 151#ifdef CONFIG_SMP 152 normal_xtp(); 153#endif 154 schedule(); 155 check_pgt_cache(); 156 if (pm_idle) 157 (*pm_idle)(); 158 } 159} 160 161void 162ia64_save_extra (struct task_struct *task) 163{ 164 if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0) 165 ia64_save_debug_regs(&task->thread.dbr[0]); 166 167#ifdef CONFIG_PERFMON 168 if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0) 169 pfm_save_regs(task); 170 171# ifdef CONFIG_SMP 172 if (local_cpu_data->pfm_syst_wide) 173 pfm_syst_wide_update_task(task, 0); 174# endif 175#endif 176 177#ifdef CONFIG_IA32_SUPPORT 178 if (IS_IA32_PROCESS(ia64_task_regs(task))) 179 ia32_save_state(task); 180#endif 181} 182 183void 184ia64_load_extra (struct task_struct *task) 185{ 186 if ((task->thread.flags & IA64_THREAD_DBG_VALID) != 0) 187 ia64_load_debug_regs(&task->thread.dbr[0]); 188 189#ifdef CONFIG_PERFMON 190 if ((task->thread.flags & IA64_THREAD_PM_VALID) != 0) 191 pfm_load_regs(task); 192 193# ifdef CONFIG_SMP 194 if (local_cpu_data->pfm_syst_wide) 195 pfm_syst_wide_update_task(task, 1); 196# endif 197#endif 198 199#ifdef CONFIG_IA32_SUPPORT 200 if (IS_IA32_PROCESS(ia64_task_regs(task))) 201 ia32_load_state(task); 202#endif 203} 204 205/* 206 * Copy the state of an ia-64 thread. 207 * 208 * We get here through the following call chain: 209 * 210 * <clone syscall> 211 * sys_clone 212 * do_fork 213 * copy_thread 214 * 215 * This means that the stack layout is as follows: 216 * 217 * +---------------------+ (highest addr) 218 * | struct pt_regs | 219 * +---------------------+ 220 * | struct switch_stack | 221 * +---------------------+ 222 * | | 223 * | memory stack | 224 * | | <-- sp (lowest addr) 225 * +---------------------+ 226 * 227 * Note: if we get called through kernel_thread() then the memory 228 * above "(highest addr)" is valid kernel stack memory that needs to 229 * be copied as well. 230 * 231 * Observe that we copy the unat values that are in pt_regs and 232 * switch_stack. Spilling an integer to address X causes bit N in 233 * ar.unat to be set to the NaT bit of the register, with N=(X & 234 * 0x1ff)/8. Thus, copying the unat value preserves the NaT bits ONLY 235 * if the pt_regs structure in the parent is congruent to that of the 236 * child, modulo 512. Since the stack is page aligned and the page 237 * size is at least 4KB, this is always the case, so there is nothing 238 * to worry about. 239 */ 240int 241copy_thread (int nr, unsigned long clone_flags, 242 unsigned long user_stack_base, unsigned long user_stack_size, 243 struct task_struct *p, struct pt_regs *regs) 244{ 245 unsigned long rbs, child_rbs, rbs_size, stack_offset, stack_top, stack_used; 246 struct switch_stack *child_stack, *stack; 247 extern char ia64_ret_from_clone, ia32_ret_from_clone; 248 struct pt_regs *child_ptregs; 249 int retval = 0; 250 251#ifdef CONFIG_SMP 252 /* 253 * For SMP idle threads, fork_by_hand() calls do_fork with 254 * NULL regs. 255 */ 256 if (!regs) 257 return 0; 258#endif 259 260 stack_top = (unsigned long) current + IA64_STK_OFFSET; 261 stack = ((struct switch_stack *) regs) - 1; 262 stack_used = stack_top - (unsigned long) stack; 263 stack_offset = IA64_STK_OFFSET - stack_used; 264 265 child_stack = (struct switch_stack *) ((unsigned long) p + stack_offset); 266 child_ptregs = (struct pt_regs *) (child_stack + 1); 267 268 /* copy parent's switch_stack & pt_regs to child: */ 269 memcpy(child_stack, stack, stack_used); 270 271 rbs = (unsigned long) current + IA64_RBS_OFFSET; 272 child_rbs = (unsigned long) p + IA64_RBS_OFFSET; 273 rbs_size = stack->ar_bspstore - rbs; 274 275 /* copy the parent's register backing store to the child: */ 276 memcpy((void *) child_rbs, (void *) rbs, rbs_size); 277 278 if (user_mode(child_ptregs)) { 279 if (user_stack_base) { 280 child_ptregs->r12 = user_stack_base + user_stack_size - 16; 281 child_ptregs->ar_bspstore = user_stack_base; 282 child_ptregs->ar_rnat = 0; 283 child_ptregs->loadrs = 0; 284 } 285 } else { 286 /* 287 * Note: we simply preserve the relative position of 288 * the stack pointer here. There is no need to 289 * allocate a scratch area here, since that will have 290 * been taken care of by the caller of sys_clone() 291 * already. 292 */ 293 child_ptregs->r12 = (unsigned long) (child_ptregs + 1); /* kernel sp */ 294 child_ptregs->r13 = (unsigned long) p; /* set `current' pointer */ 295 } 296 if (IS_IA32_PROCESS(regs)) 297 child_stack->b0 = (unsigned long) &ia32_ret_from_clone; 298 else 299 child_stack->b0 = (unsigned long) &ia64_ret_from_clone; 300 child_stack->ar_bspstore = child_rbs + rbs_size; 301 302 /* copy parts of thread_struct: */ 303 p->thread.ksp = (unsigned long) child_stack - 16; 304 305 /* stop some PSR bits from being inherited: */ 306 child_ptregs->cr_ipsr = ((child_ptregs->cr_ipsr | IA64_PSR_BITS_TO_SET) 307 & ~IA64_PSR_BITS_TO_CLEAR); 308 309# define THREAD_FLAGS_TO_CLEAR (IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID \ 310 | IA64_THREAD_PM_VALID) 311# define THREAD_FLAGS_TO_SET 0 312 p->thread.flags = ((current->thread.flags & ~THREAD_FLAGS_TO_CLEAR) 313 | THREAD_FLAGS_TO_SET); 314#ifdef CONFIG_IA32_SUPPORT 315 /* 316 * If we're cloning an IA32 task then save the IA32 extra 317 * state from the current task to the new task 318 */ 319 if (IS_IA32_PROCESS(ia64_task_regs(current))) 320 ia32_save_state(p); 321#endif 322 323#ifdef CONFIG_PERFMON 324 /* 325 * reset notifiers and owner check (may not have a perfmon context) 326 */ 327 atomic_set(&p->thread.pfm_notifiers_check, 0); 328 atomic_set(&p->thread.pfm_owners_check, 0); 329 /* clear list of sampling buffer to free for new task */ 330 p->thread.pfm_smpl_buf_list = NULL; 331 332 if (current->thread.pfm_context) 333 retval = pfm_inherit(p, child_ptregs); 334#endif 335 return retval; 336} 337 338void 339do_copy_regs (struct unw_frame_info *info, void *arg) 340{ 341 unsigned long mask, sp, nat_bits = 0, ip, ar_rnat, urbs_end, cfm; 342 elf_greg_t *dst = arg; 343 struct pt_regs *pt; 344 char nat; 345 int i; 346 347 memset(dst, 0, sizeof(elf_gregset_t)); /* don't leak any kernel bits to user-level */ 348 349 if (unw_unwind_to_user(info) < 0) 350 return; 351 352 unw_get_sp(info, &sp); 353 pt = (struct pt_regs *) (sp + 16); 354 355 urbs_end = ia64_get_user_rbs_end(current, pt, &cfm); 356 357 if (ia64_sync_user_rbs(current, info->sw, pt->ar_bspstore, urbs_end) < 0) 358 return; 359 360 ia64_peek(current, info->sw, urbs_end, (long) ia64_rse_rnat_addr((long *) urbs_end), 361 &ar_rnat); 362 363 /* 364 * coredump format: 365 * r0-r31 366 * NaT bits (for r0-r31; bit N == 1 iff rN is a NaT) 367 * predicate registers (p0-p63) 368 * b0-b7 369 * ip cfm user-mask 370 * ar.rsc ar.bsp ar.bspstore ar.rnat 371 * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec 372 */ 373 374 /* r0 is zero */ 375 for (i = 1, mask = (1UL << i); i < 32; ++i) { 376 unw_get_gr(info, i, &dst[i], &nat); 377 if (nat) 378 nat_bits |= mask; 379 mask <<= 1; 380 } 381 dst[32] = nat_bits; 382 unw_get_pr(info, &dst[33]); 383 384 for (i = 0; i < 8; ++i) 385 unw_get_br(info, i, &dst[34 + i]); 386 387 unw_get_rp(info, &ip); 388 dst[42] = ip + ia64_psr(pt)->ri; 389 dst[43] = cfm; 390 dst[44] = pt->cr_ipsr & IA64_PSR_UM; 391 392 unw_get_ar(info, UNW_AR_RSC, &dst[45]); 393 /* 394 * For bsp and bspstore, unw_get_ar() would return the kernel 395 * addresses, but we need the user-level addresses instead: 396 */ 397 dst[46] = urbs_end; /* note: by convention PT_AR_BSP points to the end of the urbs! */ 398 dst[47] = pt->ar_bspstore; 399 dst[48] = ar_rnat; 400 unw_get_ar(info, UNW_AR_CCV, &dst[49]); 401 unw_get_ar(info, UNW_AR_UNAT, &dst[50]); 402 unw_get_ar(info, UNW_AR_FPSR, &dst[51]); 403 dst[52] = pt->ar_pfs; /* UNW_AR_PFS is == to pt->cr_ifs for interrupt frames */ 404 unw_get_ar(info, UNW_AR_LC, &dst[53]); 405 unw_get_ar(info, UNW_AR_EC, &dst[54]); 406} 407 408void 409do_dump_fpu (struct unw_frame_info *info, void *arg) 410{ 411 elf_fpreg_t *dst = arg; 412 int i; 413 414 memset(dst, 0, sizeof(elf_fpregset_t)); /* don't leak any "random" bits */ 415 416 if (unw_unwind_to_user(info) < 0) 417 return; 418 419 /* f0 is 0.0, f1 is 1.0 */ 420 421 for (i = 2; i < 32; ++i) 422 unw_get_fr(info, i, dst + i); 423 424 ia64_flush_fph(current); 425 if ((current->thread.flags & IA64_THREAD_FPH_VALID) != 0) 426 memcpy(dst + 32, current->thread.fph, 96*16); 427} 428 429void 430ia64_elf_core_copy_regs (struct pt_regs *pt, elf_gregset_t dst) 431{ 432 unw_init_running(do_copy_regs, dst); 433} 434 435int 436dump_fpu (struct pt_regs *pt, elf_fpregset_t dst) 437{ 438 unw_init_running(do_dump_fpu, dst); 439 return 1; /* f0-f31 are always valid so we always return 1 */ 440} 441 442asmlinkage long 443sys_execve (char *filename, char **argv, char **envp, struct pt_regs *regs) 444{ 445 int error; 446 447 filename = getname(filename); 448 error = PTR_ERR(filename); 449 if (IS_ERR(filename)) 450 goto out; 451 error = do_execve(filename, argv, envp, regs); 452 putname(filename); 453out: 454 return error; 455} 456 457void 458ia64_set_personality (struct elf64_hdr *elf_ex, int ibcs2_interpreter) 459{ 460 set_personality(PER_LINUX); 461 if (elf_ex->e_flags & EF_IA_64_LINUX_EXECUTABLE_STACK) 462 current->thread.flags |= IA64_THREAD_XSTACK; 463 else 464 current->thread.flags &= ~IA64_THREAD_XSTACK; 465} 466 467pid_t 468kernel_thread (int (*fn)(void *), void *arg, unsigned long flags) 469{ 470 struct task_struct *parent = current; 471 int result, tid; 472 473 tid = clone(flags | CLONE_VM, 0); 474 if (parent != current) { 475 result = (*fn)(arg); 476 _exit(result); 477 } 478 return tid; 479} 480 481/* 482 * Flush thread state. This is called when a thread does an execve(). 483 */ 484void 485flush_thread (void) 486{ 487 /* drop floating-point and debug-register state if it exists: */ 488 current->thread.flags &= ~(IA64_THREAD_FPH_VALID | IA64_THREAD_DBG_VALID); 489 490#ifndef CONFIG_SMP 491 if (ia64_get_fpu_owner() == current) 492 ia64_set_fpu_owner(0); 493#endif 494} 495 496#ifdef CONFIG_PERFMON 497/* 498 * by the time we get here, the task is detached from the tasklist. This is important 499 * because it means that no other tasks can ever find it as a notified task, therfore there 500 * is no race condition between this code and let's say a pfm_context_create(). 501 * Conversely, the pfm_cleanup_notifiers() cannot try to access a task's pfm context if this 502 * other task is in the middle of its own pfm_context_exit() because it would already be out of 503 * the task list. Note that this case is very unlikely between a direct child and its parents 504 * (if it is the notified process) because of the way the exit is notified via SIGCHLD. 505 */ 506 507void 508release_thread (struct task_struct *task) 509{ 510 if (task->thread.pfm_context) 511 pfm_context_exit(task); 512 513 if (atomic_read(&task->thread.pfm_notifiers_check) > 0) 514 pfm_cleanup_notifiers(task); 515 516 if (atomic_read(&task->thread.pfm_owners_check) > 0) 517 pfm_cleanup_owners(task); 518 519 if (task->thread.pfm_smpl_buf_list) 520 pfm_cleanup_smpl_buf(task); 521} 522#endif 523 524/* 525 * Clean up state associated with current thread. This is called when 526 * the thread calls exit(). 527 */ 528void 529exit_thread (void) 530{ 531#ifndef CONFIG_SMP 532 if (ia64_get_fpu_owner() == current) 533 ia64_set_fpu_owner(0); 534#endif 535#ifdef CONFIG_PERFMON 536 /* stop monitoring */ 537 if (current->thread.pfm_context) 538 pfm_flush_regs(current); 539 540 /* free debug register resources */ 541 if (current->thread.flags & IA64_THREAD_DBG_VALID) 542 pfm_release_debug_registers(current); 543#endif 544} 545 546unsigned long 547get_wchan (struct task_struct *p) 548{ 549 struct unw_frame_info info; 550 unsigned long ip; 551 int count = 0; 552 /* 553 * These bracket the sleeping functions.. 554 */ 555 extern void scheduling_functions_start_here(void); 556 extern void scheduling_functions_end_here(void); 557# define first_sched ((unsigned long) scheduling_functions_start_here) 558# define last_sched ((unsigned long) scheduling_functions_end_here) 559 560 /* 561 * Note: p may not be a blocked task (it could be current or 562 * another process running on some other CPU. Rather than 563 * trying to determine if p is really blocked, we just assume 564 * it's blocked and rely on the unwind routines to fail 565 * gracefully if the process wasn't really blocked after all. 566 * --davidm 99/12/15 567 */ 568 unw_init_from_blocked_task(&info, p); 569 do { 570 if (unw_unwind(&info) < 0) 571 return 0; 572 unw_get_ip(&info, &ip); 573 if (ip < first_sched || ip >= last_sched) 574 return ip; 575 } while (count++ < 16); 576 return 0; 577# undef first_sched 578# undef last_sched 579} 580 581void 582cpu_halt (void) 583{ 584 pal_power_mgmt_info_u_t power_info[8]; 585 unsigned long min_power; 586 int i, min_power_state; 587 588 if (ia64_pal_halt_info(power_info) != 0) 589 return; 590 591 min_power_state = 0; 592 min_power = power_info[0].pal_power_mgmt_info_s.power_consumption; 593 for (i = 1; i < 8; ++i) 594 if (power_info[i].pal_power_mgmt_info_s.im 595 && power_info[i].pal_power_mgmt_info_s.power_consumption < min_power) { 596 min_power = power_info[i].pal_power_mgmt_info_s.power_consumption; 597 min_power_state = i; 598 } 599 600 while (1) 601 ia64_pal_halt(min_power_state); 602} 603 604void 605machine_restart (char *restart_cmd) 606{ 607 (*efi.reset_system)(EFI_RESET_WARM, 0, 0, 0); 608} 609 610void 611machine_halt (void) 612{ 613 cpu_halt(); 614} 615 616void 617machine_power_off (void) 618{ 619 if (pm_power_off) 620 pm_power_off(); 621 machine_halt(); 622} 623