int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp, unsigned long stack_top, struct task_struct * p, struct pt_regs *regs) { void (*handler)(int); if(current->thread.forking){ memcpy(&p->thread.regs.regs.skas, ®s->regs.skas, sizeof(p->thread.regs.regs.skas)); REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0); if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp; handler = fork_handler; arch_copy_thread(¤t->thread.arch, &p->thread.arch); } else { init_thread_registers(&p->thread.regs.regs); p->thread.request.u.thread = current->thread.request.u.thread; handler = new_thread_handler; } new_thread(task_stack_page(p), &p->thread.mode.skas.switch_buf, &p->thread.mode.skas.fork_buf, handler); return(0); }
static int __die(const char *str, int err, struct pt_regs *regs) { struct task_struct *tsk = current; static int die_counter; int ret; pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n", str, err, ++die_counter); /* trap and error numbers are mostly meaningless on ARM */ ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV); if (ret == NOTIFY_STOP) return ret; print_modules(); __show_regs(regs); pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n", TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk)); if (!user_mode(regs)) { dump_mem(KERN_EMERG, "Stack: ", regs->sp, THREAD_SIZE + (unsigned long)task_stack_page(tsk)); dump_backtrace(regs, tsk); dump_instr(KERN_EMERG, regs); } return ret; }
/* * Return saved PC of a blocked thread. used in kernel/sched. * resume in entry.S does not create a new stack frame, it * just stores the registers %r6-%r15 to the frame given by * schedule. We want to return the address of the caller of * schedule, so we have to walk the backchain one time to * find the frame schedule() store its return address. */ unsigned long thread_saved_pc(struct task_struct *tsk) { struct stack_frame *sf, *low, *high; if (!tsk || !task_stack_page(tsk)) return 0; low = task_stack_page(tsk); high = (struct stack_frame *) task_pt_regs(tsk); sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN); if (sf <= low || sf > high) return 0; sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN); if (sf <= low || sf > high) return 0; return sf->gprs[8]; }
int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle) { int ret; /* * We need to tell the secondary core where to find its stack and the * page tables. */ secondary_data.stack = task_stack_page(idle) + THREAD_START_SP; __flush_dcache_area(&secondary_data, sizeof(secondary_data)); /* * Now bring the CPU into our world. */ ret = boot_secondary(cpu, idle); if (ret == 0) { /* * CPU was successfully started, wait for it to come online or * time out. */ wait_for_completion_timeout(&cpu_running, msecs_to_jiffies(1000)); if (!cpu_online(cpu)) { pr_crit("CPU%u: failed to come online\n", cpu); ret = -EIO; } } else { pr_err("CPU%u: failed to boot: %d\n", cpu, ret); } secondary_data.stack = NULL; return ret; }
unsigned long get_wchan(struct task_struct *p) { unsigned long bp, sp, ip; unsigned long stack_page; int count = 0; if (!p || p == current || p->state == TASK_RUNNING) return 0; stack_page = (unsigned long)task_stack_page(p); sp = p->thread.sp; if (!stack_page || sp < stack_page || sp > top_esp+stack_page) return 0; /* L4Linux has a different layout in switch_to(), but * the only difference is that we push a return * address after ebp. So we simply adjust the esp to * reflect that. And we leave the different name for * esp to catch direct usage of thread data. */ sp += 4;/* add 4 to remove return address */ /* include/asm-i386/system.h:switch_to() pushes bp last. */ bp = *(unsigned long *) sp; do { if (bp < stack_page || bp > top_ebp+stack_page) return 0; ip = *(unsigned long *) (bp+4); if (!in_sched_functions(ip)) return ip; bp = *(unsigned long *) bp; } while (count++ < 16); return 0; }
static enum stack_type analyze_stack(int cpu, struct task_struct *task, unsigned long *stack, unsigned long **stack_end, unsigned long *irq_stack, unsigned *used, char **id) { unsigned long addr; addr = ((unsigned long)stack & (~(THREAD_SIZE - 1))); if ((unsigned long)task_stack_page(task) == addr) return STACK_IS_NORMAL; *stack_end = in_exception_stack(cpu, (unsigned long)stack, used, id); if (*stack_end) return STACK_IS_EXCEPTION; if (!irq_stack) return STACK_IS_NORMAL; *stack_end = irq_stack; irq_stack = irq_stack - irq_stack_size; if (in_irq_stack(stack, irq_stack, *stack_end)) return STACK_IS_IRQ; return STACK_IS_UNKNOWN; }
void __noreturn die(const char *str, struct pt_regs *regs, long err, unsigned long addr) { static int die_counter; oops_enter(); spin_lock_irq(&die_lock); console_verbose(); bust_spinlocks(1); pr_err("%s: err %04lx (%s) addr %08lx [#%d]\n", str, err & 0xffff, trap_name(err & 0xffff), addr, ++die_counter); print_modules(); show_regs(regs); pr_err("Process: %s (pid: %d, stack limit = %p)\n", current->comm, task_pid_nr(current), task_stack_page(current) + THREAD_SIZE); bust_spinlocks(0); add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); if (kexec_should_crash(current)) crash_kexec(regs); if (in_interrupt()) panic("Fatal exception in interrupt"); if (panic_on_oops) panic("Fatal exception"); spin_unlock_irq(&die_lock); oops_exit(); do_exit(SIGSEGV); }
bool in_task_stack(unsigned long *stack, struct task_struct *task, struct stack_info *info) { unsigned long *begin = task_stack_page(task); unsigned long *end = task_stack_page(task) + THREAD_SIZE; if (stack < begin || stack >= end) return false; info->type = STACK_TYPE_TASK; info->begin = begin; info->end = end; info->next_sp = NULL; return true; }
static void __kasan_unpoison_stack(struct task_struct *task, const void *sp) { void *base = task_stack_page(task); size_t size = sp - base; kasan_unpoison_shadow(base, size); }
/* * get_wchan - a maintenance nightmare^W^Wpain in the ass ... */ unsigned long get_wchan(struct task_struct *task) { unsigned long pc = 0; #ifdef CONFIG_KALLSYMS unsigned long sp; unsigned long ra = 0; #endif if (!task || task == current || task->state == TASK_RUNNING) goto out; if (!task_stack_page(task)) goto out; pc = thread_saved_pc(task); #ifdef CONFIG_KALLSYMS sp = task->thread.reg29 + schedule_mfi.frame_size; while (in_sched_functions(pc)) pc = unwind_stack(task, &sp, pc, &ra); #endif out: return pc; }
static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs) { unsigned long sp, stack; struct task_struct *tsk = thread->task; static int die_counter; int ret; pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n", str, err, ++die_counter); /* trap and error numbers are mostly meaningless on ARM */ ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV); if (ret == NOTIFY_STOP) return ret; print_modules(); __show_regs(regs); pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n", TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1); if (!user_mode(regs) || in_interrupt()) { sp = regs->sp; stack = (unsigned long)task_stack_page(tsk); dump_mem(KERN_EMERG, "Stack: ", sp, ALIGN(sp, THREAD_SIZE)); if (sp < stack || (sp - stack) > THREAD_SIZE) { printk(KERN_EMERG "Invalid sp[%lx] or stack address[%lx]\n", sp, stack); dump_mem(KERN_EMERG, "Stack(backup) ", stack, THREAD_SIZE + stack); } dump_backtrace(regs, tsk); dump_instr(KERN_EMERG, regs); } return ret; }
/* * Write contents of register REGNO in task TASK. */ static inline int put_reg(struct task_struct *task, int regno, unsigned long data) { char *reg_ptr; struct pt_regs *regs = (struct pt_regs *)((unsigned long)task_stack_page(task) + (THREAD_SIZE - sizeof(struct pt_regs))); reg_ptr = (char *)regs; switch (regno) { case PT_PC: /*********************************************************************/ /* At this point the kernel is most likely in exception. */ /* The RETX register will be used to populate the pc of the process. */ /*********************************************************************/ regs->retx = data; regs->pc = data; break; case PT_RETX: break; /* regs->retx = data; break; */ case PT_USP: regs->usp = data; task->thread.usp = data; break; default: if (regno <= 216) *(long *)(reg_ptr + regno) = data; } return 0; }
/* * Checks if a given pointer and length is contained by the current * stack frame (if possible). * * 0: not at all on the stack * 1: fully within a valid stack frame * 2: fully on the stack (when can't do frame-checking) * -1: error condition (invalid stack position or bad stack frame) */ static noinline int check_stack_object(const void *obj, unsigned long len) { const void * const stack = task_stack_page(current); const void * const stackend = stack + THREAD_SIZE; int ret; /* Object is not on the stack at all. */ if (obj + len <= stack || stackend <= obj) return NOT_STACK; /* * Reject: object partially overlaps the stack (passing the * the check above means at least one end is within the stack, * so if this check fails, the other end is outside the stack). */ if (obj < stack || stackend < obj + len) return BAD_STACK; /* Check if object is safely within a valid frame. */ ret = arch_within_stack_frames(stack, stackend, obj, len); if (ret) return ret; return GOOD_STACK; }
unsigned long get_wchan(struct task_struct *p) { unsigned long fp, lr; unsigned long stack_start, stack_end; int count = 0; if (!p || p == current || p->state == TASK_RUNNING) return 0; if (IS_ENABLED(CONFIG_FRAME_POINTER)) { stack_start = (unsigned long)end_of_stack(p); stack_end = (unsigned long)task_stack_page(p) + THREAD_SIZE; fp = thread_saved_fp(p); do { if (fp < stack_start || fp > stack_end) return 0; lr = ((unsigned long *)fp)[0]; if (!in_sched_functions(lr)) return lr; fp = *(unsigned long *)(fp + 4); } while (count++ < 16); } return 0; }
void dump_trace(struct task_struct *task, struct pt_regs *regs, unsigned long *stack, unsigned long bp, const struct stacktrace_ops *ops, void *data) { int graph = 0; if (!task) task = current; if (!stack) { unsigned long dummy; stack = &dummy; if (task && task != current) stack = (unsigned long *)task->thread.sp; } if (!bp) bp = stack_frame(task, regs); for (;;) { void *stack_start = (void *)((unsigned long)stack & ~(THREAD_SIZE-1)); bp = ops->walk_stack(task, stack_start, stack, bp, ops, data, NULL, &graph); if (stack_start == task_stack_page(task)) break; stack = *(unsigned long **)stack_start; if (ops->stack(data, "IRQ") < 0) break; touch_nmi_watchdog(); } }
static void save_context_stack(struct stack_trace *trace, struct pt_regs *regs) { unsigned long sp = regs->regs[29]; #ifdef CONFIG_KALLSYMS unsigned long ra = regs->regs[31]; unsigned long pc = regs->cp0_epc; if (raw_show_trace || !__kernel_text_address(pc)) { unsigned long stack_page = (unsigned long)task_stack_page(current); if (stack_page && sp >= stack_page && sp <= stack_page + THREAD_SIZE - 32) save_raw_context_stack(trace, sp); return; } do { if (trace->skip > 0) trace->skip--; else trace->entries[trace->nr_entries++] = pc; if (trace->nr_entries >= trace->max_entries) break; pc = unwind_stack(current, &sp, pc, &ra); } while (pc); #else save_raw_context_stack(trace, sp); #endif }
void save_stack_trace(struct stack_trace *trace) { unsigned long low, high; unsigned long fp; struct stackframe *frame; int skip = trace->skip; low = (unsigned long)task_stack_page(current); high = low + THREAD_SIZE; fp = current_frame_pointer; while (fp >= low && fp <= (high - sizeof(*frame))) { frame = (struct stackframe *)fp; if (skip) { skip--; } else { trace->entries[trace->nr_entries++] = frame->rets; if (trace->nr_entries >= trace->max_entries) break; } /* */ low = fp + sizeof(*frame); fp = frame->fp; } }
static int __cpuinit xen_cpu_up(unsigned int cpu) { struct task_struct *idle = idle_task(cpu); int rc; per_cpu(current_task, cpu) = idle; #ifdef CONFIG_X86_32 irq_ctx_init(cpu); #else clear_tsk_thread_flag(idle, TIF_FORK); per_cpu(kernel_stack, cpu) = (unsigned long)task_stack_page(idle) - KERNEL_STACK_OFFSET + THREAD_SIZE; per_cpu(kernel_stack8k, cpu) = (unsigned long)task_stack_page(idle) - KERNEL_STACK_OFFSET + THREAD_SIZE - 8192; #endif xen_setup_runstate_info(cpu); xen_setup_timer(cpu); xen_init_lock_cpu(cpu); per_cpu(cpu_state, cpu) = CPU_UP_PREPARE; /* make sure interrupts start blocked */ per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1; rc = cpu_initialize_context(cpu, idle); if (rc) return rc; if (num_online_cpus() == 1) alternatives_smp_switch(1); rc = xen_smp_intr_init(cpu); if (rc) return rc; rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL); BUG_ON(rc); while(per_cpu(cpu_state, cpu) != CPU_ONLINE) { HYPERVISOR_sched_op(SCHEDOP_yield, NULL); barrier(); } return 0; }
int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long unused, struct task_struct *p, struct pt_regs *regs) { struct pt_regs *childregs; struct pt_regs *kregs; unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; struct thread_info *ti; unsigned long top_of_kernel_stack; top_of_kernel_stack = sp; p->set_child_tid = p->clear_child_tid = NULL; sp -= STACK_FRAME_OVERHEAD; sp -= sizeof(struct pt_regs); childregs = (struct pt_regs *)sp; *childregs = *regs; if ((childregs->sr & SPR_SR_SM) == 1) { childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; childregs->gpr[10] = (unsigned long)task_thread_info(p); } else { childregs->sp = usp; } childregs->gpr[11] = 0; sp -= STACK_FRAME_OVERHEAD; sp -= sizeof(struct pt_regs); kregs = (struct pt_regs *)sp; ti = task_thread_info(p); ti->ksp = sp; kregs->sp = top_of_kernel_stack; kregs->gpr[3] = (unsigned long)current; kregs->gpr[10] = (unsigned long)task_thread_info(p); kregs->gpr[9] = (unsigned long)ret_from_fork; return 0; }
static bool in_task_stack(unsigned long sp, struct task_struct *task, struct stack_info *info) { unsigned long stack; stack = (unsigned long) task_stack_page(task); return in_stack(sp, info, STACK_TYPE_TASK, stack, stack + THREAD_SIZE); }
int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp, unsigned long stack_top, struct task_struct * p, struct pt_regs *regs) { int (*tramp)(void *); int new_pid, err; unsigned long stack; if(current->thread.forking) tramp = fork_tramp; else { tramp = new_thread_proc; p->thread.request.u.thread = current->thread.request.u.thread; } err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1); if(err < 0){ printk("copy_thread : pipe failed, err = %d\n", -err); return(err); } stack = alloc_stack(0, 0); if(stack == 0){ printk(KERN_ERR "copy_thread : failed to allocate " "temporary stack\n"); return(-ENOMEM); } clone_flags &= CLONE_VM; p->thread.temp_stack = stack; new_pid = start_fork_tramp(task_stack_page(p), stack, clone_flags, tramp); if(new_pid < 0){ printk(KERN_ERR "copy_thread : clone failed - errno = %d\n", -new_pid); return(new_pid); } if(current->thread.forking){ sc_to_sc(UPT_SC(&p->thread.regs.regs), UPT_SC(®s->regs)); SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0); if(sp != 0) SC_SP(UPT_SC(&p->thread.regs.regs)) = sp; } p->thread.mode.tt.extern_pid = new_pid; current->thread.request.op = OP_FORK; current->thread.request.u.fork.pid = new_pid; os_usr1_process(os_getpid()); /* Enable the signal and then disable it to ensure that it is handled * here, and nowhere else. */ change_sig(SIGUSR1, 1); change_sig(SIGUSR1, 0); err = 0; return(err); }
int start_uml_tt(void) { void *sp; int pages; pages = (1 << CONFIG_KERNEL_STACK_ORDER); sp = task_stack_page(&init_task) + pages * PAGE_SIZE - sizeof(unsigned long); return(tracer(start_kernel_proc, sp)); }
unsigned long get_wchan(struct task_struct *task) { if (!task || task == current || task->state == TASK_RUNNING) return 0; if (!task_stack_page(task)) return 0; return task_pt_regs(task)->cp0_epc; }
/* * Called from fs/proc with a reference on @p to find the function * which called into schedule(). This needs to be done carefully * because the task might wake up and we might look at a stack * changing under us. */ unsigned long get_wchan(struct task_struct *p) { unsigned long start, bottom, top, sp, fp, ip, ret = 0; int count = 0; if (!p || p == current || p->state == TASK_RUNNING) return 0; if (!try_get_task_stack(p)) return 0; start = (unsigned long)task_stack_page(p); if (!start) goto out; /* * Layout of the stack page: * * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long) * PADDING * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING * stack * ----------- bottom = start * * The tasks stack pointer points at the location where the * framepointer is stored. The data on the stack is: * ... IP FP ... IP FP * * We need to read FP and IP, so we need to adjust the upper * bound by another unsigned long. */ top = start + THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING; top -= 2 * sizeof(unsigned long); bottom = start; sp = READ_ONCE(p->thread.sp); if (sp < bottom || sp > top) goto out; fp = READ_ONCE_NOCHECK(((struct inactive_task_frame *)sp)->bp); do { if (fp < bottom || fp > top) goto out; ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long))); if (!in_sched_functions(ip)) { ret = ip; goto out; } fp = READ_ONCE_NOCHECK(*(unsigned long *)fp); } while (count++ < 16 && p->state != TASK_RUNNING); out: put_task_stack(p); return ret; }
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) { unsigned long sp, low, high; sp = tsk->thread.ksp & PSW_ADDR_INSN; low = (unsigned long) task_stack_page(tsk); high = (unsigned long) task_pt_regs(tsk); save_context_stack(trace, sp, low, high, 0); if (trace->nr_entries < trace->max_entries) trace->entries[trace->nr_entries++] = ULONG_MAX; }
/* * Copy architecture-specific thread state */ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long kthread_arg, struct task_struct *p) { struct pt_regs *cregs = &(p->thread.regs); void *stack = task_stack_page(p); /* We have to use void * instead of a function pointer, because * function pointers aren't a pointer to the function on 64-bit. * Make them const so the compiler knows they live in .text */ extern void * const ret_from_kernel_thread; extern void * const child_return; if (unlikely(p->flags & PF_KTHREAD)) { /* kernel thread */ memset(cregs, 0, sizeof(struct pt_regs)); if (!usp) /* idle thread */ return 0; /* Must exit via ret_from_kernel_thread in order * to call schedule_tail() */ cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE; cregs->kpc = (unsigned long) &ret_from_kernel_thread; /* * Copy function and argument to be called from * ret_from_kernel_thread. */ #ifdef CONFIG_64BIT cregs->gr[27] = ((unsigned long *)usp)[3]; cregs->gr[26] = ((unsigned long *)usp)[2]; #else cregs->gr[26] = usp; #endif cregs->gr[25] = kthread_arg; } else { /* user thread */ /* usp must be word aligned. This also prevents users from * passing in the value 1 (which is the signal for a special * return for a kernel thread) */ if (usp) { usp = ALIGN(usp, 4); if (likely(usp)) cregs->gr[30] = usp; } cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE; cregs->kpc = (unsigned long) &child_return; /* Setup thread TLS area from the 4th parameter in clone */ if (clone_flags & CLONE_SETTLS) cregs->cr27 = cregs->gr[23]; } return 0; }
static inline int valid_stack_ptr(struct task_struct *task, void *p, unsigned int size, void *end) { void *t = task_stack_page(task); if (end) { if (p < end && p >= (end-THREAD_SIZE)) return 1; else return 0; } return p >= t && p < t + THREAD_SIZE - size; }
/* Test that VMAP_STACK is actually allocating with a leading guard page */ void lkdtm_STACK_GUARD_PAGE_LEADING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack - 1; volatile unsigned char byte; pr_info("attempting bad read from page below current stack\n"); byte = *ptr; pr_err("FAIL: accessed page before stack!\n"); }
/* Test that VMAP_STACK is actually allocating with a trailing guard page */ void lkdtm_STACK_GUARD_PAGE_TRAILING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack + THREAD_SIZE; volatile unsigned char byte; pr_info("attempting bad read from page above current stack\n"); byte = *ptr; pr_err("FAIL: accessed page after stack!\n"); }
/* * set up the kernel stack for a new thread and copy arch-specific thread * control information */ int copy_thread(unsigned long clone_flags, unsigned long c_usp, unsigned long ustk_size, struct task_struct *p, struct pt_regs *kregs) { struct thread_info *ti = task_thread_info(p); struct pt_regs *c_uregs, *c_kregs, *uregs; unsigned long c_ksp; uregs = current->thread.uregs; c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE; /* allocate the userspace exception frame and set it up */ c_ksp -= sizeof(struct pt_regs); c_uregs = (struct pt_regs *) c_ksp; p->thread.uregs = c_uregs; *c_uregs = *uregs; c_uregs->sp = c_usp; c_uregs->epsw &= ~EPSW_FE; /* my FPU */ c_ksp -= 12; /* allocate function call ABI slack */ /* the new TLS pointer is passed in as arg #5 to sys_clone() */ if (clone_flags & CLONE_SETTLS) c_uregs->e2 = current_frame()->d3; /* set up the return kernel frame if called from kernel_thread() */ c_kregs = c_uregs; if (kregs != uregs) { c_ksp -= sizeof(struct pt_regs); c_kregs = (struct pt_regs *) c_ksp; *c_kregs = *kregs; c_kregs->sp = c_usp; c_kregs->next = c_uregs; #ifdef CONFIG_MN10300_CURRENT_IN_E2 c_kregs->e2 = (unsigned long) p; /* current */ #endif c_ksp -= 12; /* allocate function call ABI slack */ } /* set up things up so the scheduler can start the new task */ ti->frame = c_kregs; p->thread.a3 = (unsigned long) c_kregs; p->thread.sp = c_ksp; p->thread.pc = (unsigned long) ret_from_fork; p->thread.wchan = (unsigned long) ret_from_fork; p->thread.usp = c_usp; return 0; }