xref: /linux/arch/hexagon/kernel/process.c (revision 7c66e12136c2fa421ae75497e02728f252108a1b)
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 	/* We want to zero all data-containing registers. Is this overkill? */
41 	memset(regs, 0, sizeof(*regs));
42 	/* We might want to also zero all Processor registers here */
43 	pt_set_usermode(regs);
44 	pt_set_elr(regs, pc);
45 	pt_set_rte_sp(regs, sp);
46 }
47 
48 /*
49  *  Spin, or better still, do a hardware or VM wait instruction
50  *  If hardware or VM offer wait termination even though interrupts
51  *  are disabled.
52  */
53 void arch_cpu_idle(void)
54 {
55 	__vmwait();
56 	/*  interrupts wake us up, but irqs are still disabled */
57 	local_irq_enable();
58 }
59 
60 /*
61  *  Return saved PC of a blocked thread
62  */
63 unsigned long thread_saved_pc(struct task_struct *tsk)
64 {
65 	return 0;
66 }
67 
68 /*
69  * Copy architecture-specific thread state
70  */
71 int copy_thread(unsigned long clone_flags, unsigned long usp,
72 		unsigned long arg, struct task_struct *p)
73 {
74 	struct thread_info *ti = task_thread_info(p);
75 	struct hexagon_switch_stack *ss;
76 	struct pt_regs *childregs;
77 	asmlinkage void ret_from_fork(void);
78 
79 	childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
80 					sizeof(*childregs));
81 
82 	ti->regs = childregs;
83 
84 	/*
85 	 * Establish kernel stack pointer and initial PC for new thread
86 	 * Note that unlike the usual situation, we do not copy the
87 	 * parent's callee-saved here; those are in pt_regs and whatever
88 	 * we leave here will be overridden on return to userland.
89 	 */
90 	ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
91 						    sizeof(*ss));
92 	ss->lr = (unsigned long)ret_from_fork;
93 	p->thread.switch_sp = ss;
94 	if (unlikely(p->flags & PF_KTHREAD)) {
95 		memset(childregs, 0, sizeof(struct pt_regs));
96 		/* r24 <- fn, r25 <- arg */
97 		ss->r24 = usp;
98 		ss->r25 = arg;
99 		pt_set_kmode(childregs);
100 		return 0;
101 	}
102 	memcpy(childregs, current_pt_regs(), sizeof(*childregs));
103 	ss->r2524 = 0;
104 
105 	if (usp)
106 		pt_set_rte_sp(childregs, usp);
107 
108 	/* Child sees zero return value */
109 	childregs->r00 = 0;
110 
111 	/*
112 	 * The clone syscall has the C signature:
113 	 * int [r0] clone(int flags [r0],
114 	 *           void *child_frame [r1],
115 	 *           void *parent_tid [r2],
116 	 *           void *child_tid [r3],
117 	 *           void *thread_control_block [r4]);
118 	 * ugp is used to provide TLS support.
119 	 */
120 	if (clone_flags & CLONE_SETTLS)
121 		childregs->ugp = childregs->r04;
122 
123 	/*
124 	 * Parent sees new pid -- not necessary, not even possible at
125 	 * this point in the fork process
126 	 * Might also want to set things like ti->addr_limit
127 	 */
128 
129 	return 0;
130 }
131 
132 /*
133  * Release any architecture-specific resources locked by thread
134  */
135 void release_thread(struct task_struct *dead_task)
136 {
137 }
138 
139 /*
140  * Some archs flush debug and FPU info here
141  */
142 void flush_thread(void)
143 {
144 }
145 
146 /*
147  * The "wait channel" terminology is archaic, but what we want
148  * is an identification of the point at which the scheduler
149  * was invoked by a blocked thread.
150  */
151 unsigned long get_wchan(struct task_struct *p)
152 {
153 	unsigned long fp, pc;
154 	unsigned long stack_page;
155 	int count = 0;
156 	if (!p || p == current || p->state == TASK_RUNNING)
157 		return 0;
158 
159 	stack_page = (unsigned long)task_stack_page(p);
160 	fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
161 	do {
162 		if (fp < (stack_page + sizeof(struct thread_info)) ||
163 			fp >= (THREAD_SIZE - 8 + stack_page))
164 			return 0;
165 		pc = ((unsigned long *)fp)[1];
166 		if (!in_sched_functions(pc))
167 			return pc;
168 		fp = *(unsigned long *) fp;
169 	} while (count++ < 16);
170 
171 	return 0;
172 }
173 
174 /*
175  * Required placeholder.
176  */
177 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
178 {
179 	return 0;
180 }
181 
182 
183 /*
184  * Called on the exit path of event entry; see vm_entry.S
185  *
186  * Interrupts will already be disabled.
187  *
188  * Returns 0 if there's no need to re-check for more work.
189  */
190 
191 int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
192 {
193 	if (!(thread_info_flags & _TIF_WORK_MASK)) {
194 		return 0;
195 	}  /* shortcut -- no work to be done */
196 
197 	local_irq_enable();
198 
199 	if (thread_info_flags & _TIF_NEED_RESCHED) {
200 		schedule();
201 		return 1;
202 	}
203 
204 	if (thread_info_flags & _TIF_SIGPENDING) {
205 		do_signal(regs);
206 		return 1;
207 	}
208 
209 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
210 		clear_thread_flag(TIF_NOTIFY_RESUME);
211 		tracehook_notify_resume(regs);
212 		return 1;
213 	}
214 
215 	/* Should not even reach here */
216 	panic("%s: bad thread_info flags 0x%08x\n", __func__,
217 		thread_info_flags);
218 }
219