| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Process creation support for Hexagon |
| 3 | * |
| Richard Kuo | e1858b2 | 2012-09-19 16:22:02 -0500 | [diff] [blame] | 4 | * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/tick.h> |
| 25 | #include <linux/uaccess.h> |
| 26 | #include <linux/slab.h> |
| 27 | |
| 28 | /* |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 29 | * Program thread launch. Often defined as a macro in processor.h, |
| 30 | * but we're shooting for a small footprint and it's not an inner-loop |
| 31 | * performance-critical operation. |
| 32 | * |
| 33 | * The Hexagon ABI specifies that R28 is zero'ed before program launch, |
| 34 | * so that gets automatically done here. If we ever stop doing that here, |
| 35 | * we'll probably want to define the ELF_PLAT_INIT macro. |
| 36 | */ |
| 37 | void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) |
| 38 | { |
| 39 | /* Set to run with user-mode data segmentation */ |
| 40 | set_fs(USER_DS); |
| 41 | /* We want to zero all data-containing registers. Is this overkill? */ |
| 42 | memset(regs, 0, sizeof(*regs)); |
| 43 | /* We might want to also zero all Processor registers here */ |
| 44 | pt_set_usermode(regs); |
| 45 | pt_set_elr(regs, pc); |
| 46 | pt_set_rte_sp(regs, sp); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Spin, or better still, do a hardware or VM wait instruction |
| 51 | * If hardware or VM offer wait termination even though interrupts |
| 52 | * are disabled. |
| 53 | */ |
| 54 | static void default_idle(void) |
| 55 | { |
| 56 | __vmwait(); |
| 57 | } |
| 58 | |
| 59 | void (*idle_sleep)(void) = default_idle; |
| 60 | |
| 61 | void cpu_idle(void) |
| 62 | { |
| 63 | while (1) { |
| Richard Kuo | e8e42a5 | 2012-03-07 15:27:23 -0600 | [diff] [blame] | 64 | tick_nohz_idle_enter(); |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 65 | local_irq_disable(); |
| 66 | while (!need_resched()) { |
| 67 | idle_sleep(); |
| 68 | /* interrupts wake us up, but aren't serviced */ |
| 69 | local_irq_enable(); /* service interrupt */ |
| 70 | local_irq_disable(); |
| 71 | } |
| 72 | local_irq_enable(); |
| Richard Kuo | e8e42a5 | 2012-03-07 15:27:23 -0600 | [diff] [blame] | 73 | tick_nohz_idle_exit(); |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 74 | schedule(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Return saved PC of a blocked thread |
| 80 | */ |
| 81 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Copy architecture-specific thread state |
| 88 | */ |
| 89 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
| Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame^] | 90 | unsigned long arg, struct task_struct *p) |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 91 | { |
| 92 | struct thread_info *ti = task_thread_info(p); |
| 93 | struct hexagon_switch_stack *ss; |
| 94 | struct pt_regs *childregs; |
| 95 | asmlinkage void ret_from_fork(void); |
| 96 | |
| 97 | childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) - |
| 98 | sizeof(*childregs)); |
| 99 | |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 100 | ti->regs = childregs; |
| 101 | |
| 102 | /* |
| 103 | * Establish kernel stack pointer and initial PC for new thread |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 104 | * Note that unlike the usual situation, we do not copy the |
| 105 | * parent's callee-saved here; those are in pt_regs and whatever |
| 106 | * we leave here will be overridden on return to userland. |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 107 | */ |
| 108 | ss = (struct hexagon_switch_stack *) ((unsigned long) childregs - |
| 109 | sizeof(*ss)); |
| 110 | ss->lr = (unsigned long)ret_from_fork; |
| 111 | p->thread.switch_sp = ss; |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 112 | if (unlikely(p->flags & PF_KTHREAD)) { |
| 113 | memset(childregs, 0, sizeof(struct pt_regs)); |
| 114 | /* r24 <- fn, r25 <- arg */ |
| 115 | ss->r2524 = usp | ((u64)arg << 32); |
| 116 | pt_set_kmode(childregs); |
| 117 | return 0; |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 118 | } |
| Al Viro | f01acea | 2012-10-26 19:25:46 -0400 | [diff] [blame] | 119 | memcpy(childregs, current_pt_regs(), sizeof(*childregs)); |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 120 | ss->r2524 = 0; |
| 121 | |
| Al Viro | f01acea | 2012-10-26 19:25:46 -0400 | [diff] [blame] | 122 | if (usp) |
| 123 | pt_set_rte_sp(childregs, usp); |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 124 | |
| 125 | /* Child sees zero return value */ |
| 126 | childregs->r00 = 0; |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 127 | |
| 128 | /* |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 129 | * The clone syscall has the C signature: |
| 130 | * int [r0] clone(int flags [r0], |
| 131 | * void *child_frame [r1], |
| 132 | * void *parent_tid [r2], |
| 133 | * void *child_tid [r3], |
| 134 | * void *thread_control_block [r4]); |
| 135 | * ugp is used to provide TLS support. |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 136 | */ |
| Al Viro | 9952185 | 2012-10-18 22:45:24 -0400 | [diff] [blame] | 137 | if (clone_flags & CLONE_SETTLS) |
| 138 | childregs->ugp = childregs->r04; |
| 139 | |
| 140 | /* |
| 141 | * Parent sees new pid -- not necessary, not even possible at |
| 142 | * this point in the fork process |
| 143 | * Might also want to set things like ti->addr_limit |
| 144 | */ |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Release any architecture-specific resources locked by thread |
| 151 | */ |
| 152 | void release_thread(struct task_struct *dead_task) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Free any architecture-specific thread data structures, etc. |
| 158 | */ |
| 159 | void exit_thread(void) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Some archs flush debug and FPU info here |
| 165 | */ |
| 166 | void flush_thread(void) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * The "wait channel" terminology is archaic, but what we want |
| 172 | * is an identification of the point at which the scheduler |
| 173 | * was invoked by a blocked thread. |
| 174 | */ |
| 175 | unsigned long get_wchan(struct task_struct *p) |
| 176 | { |
| 177 | unsigned long fp, pc; |
| 178 | unsigned long stack_page; |
| 179 | int count = 0; |
| 180 | if (!p || p == current || p->state == TASK_RUNNING) |
| 181 | return 0; |
| 182 | |
| 183 | stack_page = (unsigned long)task_stack_page(p); |
| 184 | fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp; |
| 185 | do { |
| 186 | if (fp < (stack_page + sizeof(struct thread_info)) || |
| 187 | fp >= (THREAD_SIZE - 8 + stack_page)) |
| 188 | return 0; |
| 189 | pc = ((unsigned long *)fp)[1]; |
| 190 | if (!in_sched_functions(pc)) |
| 191 | return pc; |
| 192 | fp = *(unsigned long *) fp; |
| 193 | } while (count++ < 16); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* |
| Richard Kuo | 4b30f96 | 2011-10-31 18:41:49 -0500 | [diff] [blame] | 199 | * Required placeholder. |
| 200 | */ |
| 201 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu) |
| 202 | { |
| 203 | return 0; |
| 204 | } |