xref: /linux/arch/xtensa/kernel/process.c (revision ed3174d93c342b8b2eeba6bbd124707d55304a7b)
1 /*
2  * arch/xtensa/kernel/process.c
3  *
4  * Xtensa Processor version.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  *
10  * Copyright (C) 2001 - 2005 Tensilica Inc.
11  *
12  * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
13  * Chris Zankel <chris@zankel.net>
14  * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
15  * Kevin Chea
16  */
17 
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/elf.h>
28 #include <linux/init.h>
29 #include <linux/prctl.h>
30 #include <linux/init_task.h>
31 #include <linux/module.h>
32 #include <linux/mqueue.h>
33 #include <linux/fs.h>
34 
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/platform.h>
41 #include <asm/mmu.h>
42 #include <asm/irq.h>
43 #include <asm/atomic.h>
44 #include <asm/asm-offsets.h>
45 #include <asm/regs.h>
46 
47 extern void ret_from_fork(void);
48 
49 struct task_struct *current_set[NR_CPUS] = {&init_task, };
50 
51 void (*pm_power_off)(void) = NULL;
52 EXPORT_SYMBOL(pm_power_off);
53 
54 
55 /*
56  * Powermanagement idle function, if any is provided by the platform.
57  */
58 
59 void cpu_idle(void)
60 {
61   	local_irq_enable();
62 
63 	/* endless idle loop with no priority at all */
64 	while (1) {
65 		while (!need_resched())
66 			platform_idle();
67 		preempt_enable_no_resched();
68 		schedule();
69 		preempt_disable();
70 	}
71 }
72 
73 /*
74  * Free current thread data structures etc..
75  */
76 
77 void exit_thread(void)
78 {
79 }
80 
81 void flush_thread(void)
82 {
83 }
84 
85 /*
86  * Copy thread.
87  *
88  * The stack layout for the new thread looks like this:
89  *
90  *	+------------------------+ <- sp in childregs (= tos)
91  *	|       childregs        |
92  *	+------------------------+ <- thread.sp = sp in dummy-frame
93  *	|      dummy-frame       |    (saved in dummy-frame spill-area)
94  *	+------------------------+
95  *
96  * We create a dummy frame to return to ret_from_fork:
97  *   a0 points to ret_from_fork (simulating a call4)
98  *   sp points to itself (thread.sp)
99  *   a2, a3 are unused.
100  *
101  * Note: This is a pristine frame, so we don't need any spill region on top of
102  *       childregs.
103  */
104 
105 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
106 		unsigned long unused,
107                 struct task_struct * p, struct pt_regs * regs)
108 {
109 	struct pt_regs *childregs;
110 	unsigned long tos;
111 	int user_mode = user_mode(regs);
112 
113 	/* Set up new TSS. */
114 	tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
115 	if (user_mode)
116 		childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
117 	else
118 		childregs = (struct pt_regs*)tos - 1;
119 
120 	*childregs = *regs;
121 
122 	/* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
123 	*((int*)childregs - 3) = (unsigned long)childregs;
124 	*((int*)childregs - 4) = 0;
125 
126 	childregs->areg[1] = tos;
127 	childregs->areg[2] = 0;
128 	p->set_child_tid = p->clear_child_tid = NULL;
129 	p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
130 	p->thread.sp = (unsigned long)childregs;
131 	if (user_mode(regs)) {
132 
133 		int len = childregs->wmask & ~0xf;
134 		childregs->areg[1] = usp;
135 		memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
136 		       &regs->areg[XCHAL_NUM_AREGS - len/4], len);
137 
138 		if (clone_flags & CLONE_SETTLS)
139 			childregs->areg[2] = childregs->areg[6];
140 
141 	} else {
142 		/* In kernel space, we start a new thread with a new stack. */
143 		childregs->wmask = 1;
144 	}
145 	return 0;
146 }
147 
148 
149 /*
150  * These bracket the sleeping functions..
151  */
152 
153 unsigned long get_wchan(struct task_struct *p)
154 {
155 	unsigned long sp, pc;
156 	unsigned long stack_page = (unsigned long) task_stack_page(p);
157 	int count = 0;
158 
159 	if (!p || p == current || p->state == TASK_RUNNING)
160 		return 0;
161 
162 	sp = p->thread.sp;
163 	pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
164 
165 	do {
166 		if (sp < stack_page + sizeof(struct task_struct) ||
167 		    sp >= (stack_page + THREAD_SIZE) ||
168 		    pc == 0)
169 			return 0;
170 		if (!in_sched_functions(pc))
171 			return pc;
172 
173 		/* Stack layout: sp-4: ra, sp-3: sp' */
174 
175 		pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
176 		sp = *(unsigned long *)sp - 3;
177 	} while (count++ < 16);
178 	return 0;
179 }
180 
181 /*
182  * do_copy_regs() gathers information from 'struct pt_regs' and
183  * 'current->thread.areg[]' to fill in the xtensa_gregset_t
184  * structure.
185  *
186  * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
187  * of processor registers.  Besides different ordering,
188  * xtensa_gregset_t contains non-live register information that
189  * 'struct pt_regs' does not.  Exception handling (primarily) uses
190  * 'struct pt_regs'.  Core files and ptrace use xtensa_gregset_t.
191  *
192  */
193 
194 void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
195 		   struct task_struct *tsk)
196 {
197 	/* Note:  PS.EXCM is not set while user task is running; its
198 	 * being set in regs->ps is for exception handling convenience.
199 	 */
200 
201 	elfregs->pc		= regs->pc;
202 	elfregs->ps		= (regs->ps & ~(1 << PS_EXCM_BIT));
203 	elfregs->lbeg		= regs->lbeg;
204 	elfregs->lend		= regs->lend;
205 	elfregs->lcount		= regs->lcount;
206 	elfregs->sar		= regs->sar;
207 
208 	memcpy (elfregs->a, regs->areg, sizeof(elfregs->a));
209 }
210 
211 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
212 {
213 	do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
214 }
215 
216 
217 /* The inverse of do_copy_regs().  No error or sanity checking. */
218 
219 void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
220 		      struct task_struct *tsk)
221 {
222 	const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
223 	unsigned long ps;
224 
225 	/* Note:  PS.EXCM is not set while user task is running; it
226 	 * needs to be set in regs->ps is for exception handling convenience.
227 	 */
228 
229 	ps = (regs->ps & ~ps_mask) | (elfregs->ps & ps_mask) | (1<<PS_EXCM_BIT);
230 	regs->ps		= ps;
231 	regs->pc		= elfregs->pc;
232 	regs->lbeg		= elfregs->lbeg;
233 	regs->lend		= elfregs->lend;
234 	regs->lcount		= elfregs->lcount;
235 	regs->sar		= elfregs->sar;
236 
237 	memcpy (regs->areg, elfregs->a, sizeof(regs->areg));
238 }
239 
240 /*
241  * do_save_fpregs() gathers information from 'struct pt_regs' and
242  * 'current->thread' to fill in the elf_fpregset_t structure.
243  *
244  * Core files and ptrace use elf_fpregset_t.
245  */
246 
247 void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
248 		     struct task_struct *tsk)
249 {
250 #if XCHAL_HAVE_CP
251 
252 	extern unsigned char	_xtensa_reginfo_tables[];
253 	extern unsigned		_xtensa_reginfo_table_size;
254 	int i;
255 	unsigned long flags;
256 
257 	/* Before dumping coprocessor state from memory,
258 	 * ensure any live coprocessor contents for this
259 	 * task are first saved to memory:
260 	 */
261 	local_irq_save(flags);
262 
263 	for (i = 0; i < XCHAL_CP_MAX; i++) {
264 		if (tsk == coprocessor_info[i].owner) {
265 			enable_coprocessor(i);
266 			save_coprocessor_registers(
267 			    tsk->thread.cp_save+coprocessor_info[i].offset,i);
268 			disable_coprocessor(i);
269 		}
270 	}
271 
272 	local_irq_restore(flags);
273 
274 	/* Now dump coprocessor & extra state: */
275 	memcpy((unsigned char*)fpregs,
276 		_xtensa_reginfo_tables, _xtensa_reginfo_table_size);
277 	memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
278 		tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
279 #endif
280 }
281 
282 /*
283  * The inverse of do_save_fpregs().
284  * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
285  * Returns 0 on success, non-zero if layout doesn't match.
286  */
287 
288 int  do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
289 		        struct task_struct *tsk)
290 {
291 #if XCHAL_HAVE_CP
292 
293 	extern unsigned char	_xtensa_reginfo_tables[];
294 	extern unsigned		_xtensa_reginfo_table_size;
295 	int i;
296 	unsigned long flags;
297 
298 	/* Make sure save area layouts match.
299 	 * FIXME:  in the future we could allow restoring from
300 	 * a different layout of the same registers, by comparing
301 	 * fpregs' table with _xtensa_reginfo_tables and matching
302 	 * entries and copying registers one at a time.
303 	 * Not too sure yet whether that's very useful.
304 	 */
305 
306 	if( memcmp((unsigned char*)fpregs,
307 		_xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
308 	    return -1;
309 	}
310 
311 	/* Before restoring coprocessor state from memory,
312 	 * ensure any live coprocessor contents for this
313 	 * task are first invalidated.
314 	 */
315 
316 	local_irq_save(flags);
317 
318 	for (i = 0; i < XCHAL_CP_MAX; i++) {
319 		if (tsk == coprocessor_info[i].owner) {
320 			enable_coprocessor(i);
321 			save_coprocessor_registers(
322 			    tsk->thread.cp_save+coprocessor_info[i].offset,i);
323 			coprocessor_info[i].owner = 0;
324 			disable_coprocessor(i);
325 		}
326 	}
327 
328 	local_irq_restore(flags);
329 
330 	/*  Now restore coprocessor & extra state:  */
331 
332 	memcpy(tsk->thread.cp_save,
333 		(unsigned char*)fpregs + _xtensa_reginfo_table_size,
334 		XTENSA_CP_EXTRA_SIZE);
335 #endif
336 	return 0;
337 }
338 /*
339  * Fill in the CP structure for a core dump for a particular task.
340  */
341 
342 int
343 dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
344 {
345 	return 0;	/* no coprocessors active on this processor */
346 }
347 
348 /*
349  * Fill in the CP structure for a core dump.
350  * This includes any FPU coprocessor.
351  * Here, we dump all coprocessors, and other ("extra") custom state.
352  *
353  * This function is called by elf_core_dump() in fs/binfmt_elf.c
354  * (in which case 'regs' comes from calls to do_coredump, see signals.c).
355  */
356 int  dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
357 {
358 	return dump_task_fpu(regs, current, r);
359 }
360 
361 asmlinkage
362 long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
363                   void __user *parent_tid, void *child_tls,
364                   void __user *child_tid, long a5,
365                   struct pt_regs *regs)
366 {
367         if (!newsp)
368                 newsp = regs->areg[1];
369         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
370 }
371 
372 /*
373  *  * xtensa_execve() executes a new program.
374  *   */
375 
376 asmlinkage
377 long xtensa_execve(char __user *name, char __user * __user *argv,
378                    char __user * __user *envp,
379                    long a3, long a4, long a5,
380                    struct pt_regs *regs)
381 {
382 	long error;
383 	char * filename;
384 
385 	filename = getname(name);
386 	error = PTR_ERR(filename);
387 	if (IS_ERR(filename))
388 		goto out;
389 	// FIXME: release coprocessor??
390 	error = do_execve(filename, argv, envp, regs);
391 	if (error == 0) {
392 		task_lock(current);
393 		current->ptrace &= ~PT_DTRACE;
394 		task_unlock(current);
395 	}
396 	putname(filename);
397 out:
398 	return error;
399 }
400 
401