Hexagon: Signal and return path fixes
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / hexagon / kernel / process.c
1 /*
2  * Process creation support for Hexagon
3  *
4  * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
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 #include <linux/tracehook.h>
28
29 /*
30  * Program thread launch.  Often defined as a macro in processor.h,
31  * but we're shooting for a small footprint and it's not an inner-loop
32  * performance-critical operation.
33  *
34  * The Hexagon ABI specifies that R28 is zero'ed before program launch,
35  * so that gets automatically done here.  If we ever stop doing that here,
36  * we'll probably want to define the ELF_PLAT_INIT macro.
37  */
38 void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
39 {
40         /* Set to run with user-mode data segmentation */
41         set_fs(USER_DS);
42         /* We want to zero all data-containing registers. Is this overkill? */
43         memset(regs, 0, sizeof(*regs));
44         /* We might want to also zero all Processor registers here */
45         pt_set_usermode(regs);
46         pt_set_elr(regs, pc);
47         pt_set_rte_sp(regs, sp);
48 }
49
50 /*
51  *  Spin, or better still, do a hardware or VM wait instruction
52  *  If hardware or VM offer wait termination even though interrupts
53  *  are disabled.
54  */
55 static void default_idle(void)
56 {
57         __vmwait();
58 }
59
60 void (*idle_sleep)(void) = default_idle;
61
62 void cpu_idle(void)
63 {
64         while (1) {
65                 tick_nohz_idle_enter();
66                 local_irq_disable();
67                 while (!need_resched()) {
68                         idle_sleep();
69                         /*  interrupts wake us up, but aren't serviced  */
70                         local_irq_enable();     /* service interrupt   */
71                         local_irq_disable();
72                 }
73                 local_irq_enable();
74                 tick_nohz_idle_exit();
75                 schedule();
76         }
77 }
78
79 /*
80  *  Return saved PC of a blocked thread
81  */
82 unsigned long thread_saved_pc(struct task_struct *tsk)
83 {
84         return 0;
85 }
86
87 /*
88  * Copy architecture-specific thread state
89  */
90 int copy_thread(unsigned long clone_flags, unsigned long usp,
91                 unsigned long arg, struct task_struct *p)
92 {
93         struct thread_info *ti = task_thread_info(p);
94         struct hexagon_switch_stack *ss;
95         struct pt_regs *childregs;
96         asmlinkage void ret_from_fork(void);
97
98         childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
99                                         sizeof(*childregs));
100
101         ti->regs = childregs;
102
103         /*
104          * Establish kernel stack pointer and initial PC for new thread
105          * Note that unlike the usual situation, we do not copy the
106          * parent's callee-saved here; those are in pt_regs and whatever
107          * we leave here will be overridden on return to userland.
108          */
109         ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
110                                                     sizeof(*ss));
111         ss->lr = (unsigned long)ret_from_fork;
112         p->thread.switch_sp = ss;
113         if (unlikely(p->flags & PF_KTHREAD)) {
114                 memset(childregs, 0, sizeof(struct pt_regs));
115                 /* r24 <- fn, r25 <- arg */
116                 ss->r2524 = usp | ((u64)arg << 32);
117                 pt_set_kmode(childregs);
118                 return 0;
119         }
120         memcpy(childregs, current_pt_regs(), sizeof(*childregs));
121         ss->r2524 = 0;
122
123         if (usp)
124                 pt_set_rte_sp(childregs, usp);
125
126         /* Child sees zero return value */
127         childregs->r00 = 0;
128
129         /*
130          * The clone syscall has the C signature:
131          * int [r0] clone(int flags [r0],
132          *           void *child_frame [r1],
133          *           void *parent_tid [r2],
134          *           void *child_tid [r3],
135          *           void *thread_control_block [r4]);
136          * ugp is used to provide TLS support.
137          */
138         if (clone_flags & CLONE_SETTLS)
139                 childregs->ugp = childregs->r04;
140
141         /*
142          * Parent sees new pid -- not necessary, not even possible at
143          * this point in the fork process
144          * Might also want to set things like ti->addr_limit
145          */
146
147         return 0;
148 }
149
150 /*
151  * Release any architecture-specific resources locked by thread
152  */
153 void release_thread(struct task_struct *dead_task)
154 {
155 }
156
157 /*
158  * Free any architecture-specific thread data structures, etc.
159  */
160 void exit_thread(void)
161 {
162 }
163
164 /*
165  * Some archs flush debug and FPU info here
166  */
167 void flush_thread(void)
168 {
169 }
170
171 /*
172  * The "wait channel" terminology is archaic, but what we want
173  * is an identification of the point at which the scheduler
174  * was invoked by a blocked thread.
175  */
176 unsigned long get_wchan(struct task_struct *p)
177 {
178         unsigned long fp, pc;
179         unsigned long stack_page;
180         int count = 0;
181         if (!p || p == current || p->state == TASK_RUNNING)
182                 return 0;
183
184         stack_page = (unsigned long)task_stack_page(p);
185         fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
186         do {
187                 if (fp < (stack_page + sizeof(struct thread_info)) ||
188                         fp >= (THREAD_SIZE - 8 + stack_page))
189                         return 0;
190                 pc = ((unsigned long *)fp)[1];
191                 if (!in_sched_functions(pc))
192                         return pc;
193                 fp = *(unsigned long *) fp;
194         } while (count++ < 16);
195
196         return 0;
197 }
198
199 /*
200  * Required placeholder.
201  */
202 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
203 {
204         return 0;
205 }
206
207
208 /*
209  * Called on the exit path of event entry; see vm_entry.S
210  *
211  * Interrupts will already be disabled.
212  *
213  * Returns 0 if there's no need to re-check for more work.
214  */
215
216 int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
217 {
218         if (!(thread_info_flags & _TIF_ALLWORK_MASK)) {
219                 return 0;
220         }  /* shortcut -- no work to be done */
221
222         local_irq_enable();
223
224         if (thread_info_flags & _TIF_NEED_RESCHED) {
225                 schedule();
226                 return 1;
227         }
228
229         if (thread_info_flags & _TIF_SIGPENDING) {
230                 do_signal(regs);
231                 return 1;
232         }
233
234         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
235                 clear_thread_flag(TIF_NOTIFY_RESUME);
236                 tracehook_notify_resume(regs);
237                 if (current->replacement_session_keyring)
238                         key_replace_session_keyring();
239                 return 1;
240         }
241
242         /* Should not even reach here */
243         panic("%s: bad thread_info flags 0x%08x\n", __func__,
244                 thread_info_flags);
245 }