C6X: process management
[deliverable/linux.git] / arch / c6x / kernel / process.c
CommitLineData
687b12ba
AJ
1/*
2 * Port on Texas Instruments TMS320C6x architecture
3 *
4 * Copyright (C) 2004, 2006, 2009, 2010, 2011 Texas Instruments Incorporated
5 * Author: Aurelien Jacquiot (aurelien.jacquiot@jaluna.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12#include <linux/module.h>
13#include <linux/unistd.h>
14#include <linux/ptrace.h>
15#include <linux/init_task.h>
16#include <linux/tick.h>
17#include <linux/mqueue.h>
18#include <linux/syscalls.h>
19#include <linux/reboot.h>
20
21#include <asm/syscalls.h>
22
23/* hooks for board specific support */
24void (*c6x_restart)(void);
25void (*c6x_halt)(void);
26
27extern asmlinkage void ret_from_fork(void);
28
29static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
30static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
31
32/*
33 * Initial thread structure.
34 */
35union thread_union init_thread_union __init_task_data = {
36 INIT_THREAD_INFO(init_task)
37};
38
39/*
40 * Initial task structure.
41 */
42struct task_struct init_task = INIT_TASK(init_task);
43EXPORT_SYMBOL(init_task);
44
45/*
46 * power off function, if any
47 */
48void (*pm_power_off)(void);
49EXPORT_SYMBOL(pm_power_off);
50
51static void c6x_idle(void)
52{
53 unsigned long tmp;
54
55 /*
56 * Put local_irq_enable and idle in same execute packet
57 * to make them atomic and avoid race to idle with
58 * interrupts enabled.
59 */
60 asm volatile (" mvc .s2 CSR,%0\n"
61 " or .d2 1,%0,%0\n"
62 " mvc .s2 %0,CSR\n"
63 "|| idle\n"
64 : "=b"(tmp));
65}
66
67/*
68 * The idle loop for C64x
69 */
70void cpu_idle(void)
71{
72 /* endless idle loop with no priority at all */
73 while (1) {
74 tick_nohz_stop_sched_tick(1);
75 while (1) {
76 local_irq_disable();
77 if (need_resched()) {
78 local_irq_enable();
79 break;
80 }
81 c6x_idle(); /* enables local irqs */
82 }
83 tick_nohz_restart_sched_tick();
84
85 preempt_enable_no_resched();
86 schedule();
87 preempt_disable();
88 }
89}
90
91static void halt_loop(void)
92{
93 printk(KERN_EMERG "System Halted, OK to turn off power\n");
94 local_irq_disable();
95 while (1)
96 asm volatile("idle\n");
97}
98
99void machine_restart(char *__unused)
100{
101 if (c6x_restart)
102 c6x_restart();
103 halt_loop();
104}
105
106void machine_halt(void)
107{
108 if (c6x_halt)
109 c6x_halt();
110 halt_loop();
111}
112
113void machine_power_off(void)
114{
115 if (pm_power_off)
116 pm_power_off();
117 halt_loop();
118}
119
120static void kernel_thread_helper(int dummy, void *arg, int (*fn)(void *))
121{
122 do_exit(fn(arg));
123}
124
125/*
126 * Create a kernel thread
127 */
128int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
129{
130 struct pt_regs regs;
131
132 /*
133 * copy_thread sets a4 to zero (child return from fork)
134 * so we can't just set things up to directly return to
135 * fn.
136 */
137 memset(&regs, 0, sizeof(regs));
138 regs.b4 = (unsigned long) arg;
139 regs.a6 = (unsigned long) fn;
140 regs.pc = (unsigned long) kernel_thread_helper;
141 local_save_flags(regs.csr);
142 regs.csr |= 1;
143 regs.tsr = 5; /* Set GEE and GIE in TSR */
144
145 /* Ok, create the new process.. */
146 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, -1, &regs,
147 0, NULL, NULL);
148}
149EXPORT_SYMBOL(kernel_thread);
150
151void flush_thread(void)
152{
153}
154
155void exit_thread(void)
156{
157}
158
159SYSCALL_DEFINE1(c6x_clone, struct pt_regs *, regs)
160{
161 unsigned long clone_flags;
162 unsigned long newsp;
163
164 /* syscall puts clone_flags in A4 and usp in B4 */
165 clone_flags = regs->orig_a4;
166 if (regs->b4)
167 newsp = regs->b4;
168 else
169 newsp = regs->sp;
170
171 return do_fork(clone_flags, newsp, regs, 0, (int __user *)regs->a6,
172 (int __user *)regs->b6);
173}
174
175/*
176 * Do necessary setup to start up a newly executed thread.
177 */
178void start_thread(struct pt_regs *regs, unsigned int pc, unsigned long usp)
179{
180 /*
181 * The binfmt loader will setup a "full" stack, but the C6X
182 * operates an "empty" stack. So we adjust the usp so that
183 * argc doesn't get destroyed if an interrupt is taken before
184 * it is read from the stack.
185 *
186 * NB: Library startup code needs to match this.
187 */
188 usp -= 8;
189
190 set_fs(USER_DS);
191 regs->pc = pc;
192 regs->sp = usp;
193 regs->tsr |= 0x40; /* set user mode */
194 current->thread.usp = usp;
195}
196
197/*
198 * Copy a new thread context in its stack.
199 */
200int copy_thread(unsigned long clone_flags, unsigned long usp,
201 unsigned long ustk_size,
202 struct task_struct *p, struct pt_regs *regs)
203{
204 struct pt_regs *childregs;
205
206 childregs = task_pt_regs(p);
207
208 *childregs = *regs;
209 childregs->a4 = 0;
210
211 if (usp == -1)
212 /* case of __kernel_thread: we return to supervisor space */
213 childregs->sp = (unsigned long)(childregs + 1);
214 else
215 /* Otherwise use the given stack */
216 childregs->sp = usp;
217
218 /* Set usp/ksp */
219 p->thread.usp = childregs->sp;
220 /* switch_to uses stack to save/restore 14 callee-saved regs */
221 thread_saved_ksp(p) = (unsigned long)childregs - 8;
222 p->thread.pc = (unsigned int) ret_from_fork;
223 p->thread.wchan = (unsigned long) ret_from_fork;
224#ifdef __DSBT__
225 {
226 unsigned long dp;
227
228 asm volatile ("mv .S2 b14,%0\n" : "=b"(dp));
229
230 thread_saved_dp(p) = dp;
231 if (usp == -1)
232 childregs->dp = dp;
233 }
234#endif
235 return 0;
236}
237
238/*
239 * c6x_execve() executes a new program.
240 */
241SYSCALL_DEFINE4(c6x_execve, const char __user *, name,
242 const char __user *const __user *, argv,
243 const char __user *const __user *, envp,
244 struct pt_regs *, regs)
245{
246 int error;
247 char *filename;
248
249 filename = getname(name);
250 error = PTR_ERR(filename);
251 if (IS_ERR(filename))
252 goto out;
253
254 error = do_execve(filename, argv, envp, regs);
255 putname(filename);
256out:
257 return error;
258}
259
260unsigned long get_wchan(struct task_struct *p)
261{
262 return p->thread.wchan;
263}
This page took 0.032926 seconds and 5 git commands to generate.