1 /* MN10300  Process handling code
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/reboot.h>
24 #include <linux/percpu.h>
25 #include <linux/err.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #include <asm/processor.h>
33 #include <asm/mmu_context.h>
34 #include <asm/fpu.h>
35 #include <asm/reset-regs.h>
36 #include <asm/gdb-stub.h>
37 #include "internal.h"
38 
39 /*
40  * power management idle function, if any..
41  */
42 void (*pm_idle)(void);
43 EXPORT_SYMBOL(pm_idle);
44 
45 /*
46  * return saved PC of a blocked thread.
47  */
thread_saved_pc(struct task_struct * tsk)48 unsigned long thread_saved_pc(struct task_struct *tsk)
49 {
50 	return ((unsigned long *) tsk->thread.sp)[3];
51 }
52 
53 /*
54  * power off function, if any
55  */
56 void (*pm_power_off)(void);
57 EXPORT_SYMBOL(pm_power_off);
58 
59 #if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
60 /*
61  * we use this if we don't have any better idle routine
62  */
default_idle(void)63 static void default_idle(void)
64 {
65 	local_irq_disable();
66 	if (!need_resched())
67 		safe_halt();
68 	else
69 		local_irq_enable();
70 }
71 
72 #else /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU  */
73 /*
74  * On SMP it's slightly faster (but much more power-consuming!)
75  * to poll the ->work.need_resched flag instead of waiting for the
76  * cross-CPU IPI to arrive. Use this option with caution.
77  */
poll_idle(void)78 static inline void poll_idle(void)
79 {
80 	int oldval;
81 
82 	local_irq_enable();
83 
84 	/*
85 	 * Deal with another CPU just having chosen a thread to
86 	 * run here:
87 	 */
88 	oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
89 
90 	if (!oldval) {
91 		set_thread_flag(TIF_POLLING_NRFLAG);
92 		while (!need_resched())
93 			cpu_relax();
94 		clear_thread_flag(TIF_POLLING_NRFLAG);
95 	} else {
96 		set_need_resched();
97 	}
98 }
99 #endif /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
100 
101 /*
102  * the idle thread
103  * - there's no useful work to be done, so just try to conserve power and have
104  *   a low exit latency (ie sit in a loop waiting for somebody to say that
105  *   they'd like to reschedule)
106  */
cpu_idle(void)107 void cpu_idle(void)
108 {
109 	/* endless idle loop with no priority at all */
110 	for (;;) {
111 		while (!need_resched()) {
112 			void (*idle)(void);
113 
114 			smp_rmb();
115 			idle = pm_idle;
116 			if (!idle) {
117 #if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU)
118 				idle = poll_idle;
119 #else  /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
120 				idle = default_idle;
121 #endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
122 			}
123 			idle();
124 		}
125 
126 		preempt_enable_no_resched();
127 		schedule();
128 		preempt_disable();
129 	}
130 }
131 
release_segments(struct mm_struct * mm)132 void release_segments(struct mm_struct *mm)
133 {
134 }
135 
machine_restart(char * cmd)136 void machine_restart(char *cmd)
137 {
138 #ifdef CONFIG_KERNEL_DEBUGGER
139 	gdbstub_exit(0);
140 #endif
141 
142 #ifdef mn10300_unit_hard_reset
143 	mn10300_unit_hard_reset();
144 #else
145 	mn10300_proc_hard_reset();
146 #endif
147 }
148 
machine_halt(void)149 void machine_halt(void)
150 {
151 #ifdef CONFIG_KERNEL_DEBUGGER
152 	gdbstub_exit(0);
153 #endif
154 }
155 
machine_power_off(void)156 void machine_power_off(void)
157 {
158 #ifdef CONFIG_KERNEL_DEBUGGER
159 	gdbstub_exit(0);
160 #endif
161 }
162 
show_regs(struct pt_regs * regs)163 void show_regs(struct pt_regs *regs)
164 {
165 }
166 
167 /*
168  * create a kernel thread
169  */
kernel_thread(int (* fn)(void *),void * arg,unsigned long flags)170 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
171 {
172 	struct pt_regs regs;
173 
174 	memset(&regs, 0, sizeof(regs));
175 
176 	regs.a2 = (unsigned long) fn;
177 	regs.d2 = (unsigned long) arg;
178 	regs.pc = (unsigned long) kernel_thread_helper;
179 	local_save_flags(regs.epsw);
180 	regs.epsw |= EPSW_IE | EPSW_IM_7;
181 
182 	/* Ok, create the new process.. */
183 	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0,
184 		       NULL, NULL);
185 }
186 EXPORT_SYMBOL(kernel_thread);
187 
188 /*
189  * free current thread data structures etc..
190  */
exit_thread(void)191 void exit_thread(void)
192 {
193 	exit_fpu();
194 }
195 
flush_thread(void)196 void flush_thread(void)
197 {
198 	flush_fpu();
199 }
200 
release_thread(struct task_struct * dead_task)201 void release_thread(struct task_struct *dead_task)
202 {
203 }
204 
205 /*
206  * we do not have to muck with descriptors here, that is
207  * done in switch_mm() as needed.
208  */
copy_segments(struct task_struct * p,struct mm_struct * new_mm)209 void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
210 {
211 }
212 
213 /*
214  * this gets called before we allocate a new thread and copy the current task
215  * into it so that we can store lazy state into memory
216  */
prepare_to_copy(struct task_struct * tsk)217 void prepare_to_copy(struct task_struct *tsk)
218 {
219 	unlazy_fpu(tsk);
220 }
221 
222 /*
223  * set up the kernel stack for a new thread and copy arch-specific thread
224  * control information
225  */
copy_thread(unsigned long clone_flags,unsigned long c_usp,unsigned long ustk_size,struct task_struct * p,struct pt_regs * kregs)226 int copy_thread(unsigned long clone_flags,
227 		unsigned long c_usp, unsigned long ustk_size,
228 		struct task_struct *p, struct pt_regs *kregs)
229 {
230 	struct thread_info *ti = task_thread_info(p);
231 	struct pt_regs *c_uregs, *c_kregs, *uregs;
232 	unsigned long c_ksp;
233 
234 	uregs = current->thread.uregs;
235 
236 	c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
237 
238 	/* allocate the userspace exception frame and set it up */
239 	c_ksp -= sizeof(struct pt_regs);
240 	c_uregs = (struct pt_regs *) c_ksp;
241 
242 	p->thread.uregs = c_uregs;
243 	*c_uregs = *uregs;
244 	c_uregs->sp = c_usp;
245 	c_uregs->epsw &= ~EPSW_FE; /* my FPU */
246 
247 	c_ksp -= 12; /* allocate function call ABI slack */
248 
249 	/* the new TLS pointer is passed in as arg #5 to sys_clone() */
250 	if (clone_flags & CLONE_SETTLS)
251 		c_uregs->e2 = current_frame()->d3;
252 
253 	/* set up the return kernel frame if called from kernel_thread() */
254 	c_kregs = c_uregs;
255 	if (kregs != uregs) {
256 		c_ksp -= sizeof(struct pt_regs);
257 		c_kregs = (struct pt_regs *) c_ksp;
258 		*c_kregs = *kregs;
259 		c_kregs->sp = c_usp;
260 		c_kregs->next = c_uregs;
261 #ifdef CONFIG_MN10300_CURRENT_IN_E2
262 		c_kregs->e2 = (unsigned long) p; /* current */
263 #endif
264 
265 		c_ksp -= 12; /* allocate function call ABI slack */
266 	}
267 
268 	/* set up things up so the scheduler can start the new task */
269 	ti->frame	= c_kregs;
270 	p->thread.a3	= (unsigned long) c_kregs;
271 	p->thread.sp	= c_ksp;
272 	p->thread.pc	= (unsigned long) ret_from_fork;
273 	p->thread.wchan	= (unsigned long) ret_from_fork;
274 	p->thread.usp	= c_usp;
275 
276 	return 0;
277 }
278 
279 /*
280  * clone a process
281  * - tlsptr is retrieved by copy_thread() from current_frame()->d3
282  */
sys_clone(unsigned long clone_flags,unsigned long newsp,int __user * parent_tidptr,int __user * child_tidptr,int __user * tlsptr)283 asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
284 			  int __user *parent_tidptr, int __user *child_tidptr,
285 			  int __user *tlsptr)
286 {
287 	return do_fork(clone_flags, newsp ?: current_frame()->sp,
288 		       current_frame(), 0, parent_tidptr, child_tidptr);
289 }
290 
sys_fork(void)291 asmlinkage long sys_fork(void)
292 {
293 	return do_fork(SIGCHLD, current_frame()->sp,
294 		       current_frame(), 0, NULL, NULL);
295 }
296 
sys_vfork(void)297 asmlinkage long sys_vfork(void)
298 {
299 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp,
300 		       current_frame(), 0, NULL, NULL);
301 }
302 
sys_execve(const char __user * name,const char __user * const __user * argv,const char __user * const __user * envp)303 asmlinkage long sys_execve(const char __user *name,
304 			   const char __user *const __user *argv,
305 			   const char __user *const __user *envp)
306 {
307 	char *filename;
308 	int error;
309 
310 	filename = getname(name);
311 	error = PTR_ERR(filename);
312 	if (IS_ERR(filename))
313 		return error;
314 	error = do_execve(filename, argv, envp, current_frame());
315 	putname(filename);
316 	return error;
317 }
318 
get_wchan(struct task_struct * p)319 unsigned long get_wchan(struct task_struct *p)
320 {
321 	return p->thread.wchan;
322 }
323