ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / arch / s390 / kernel / process.c
blobabdc2b1063ed080f98b8fa50e7ba40ed00cde85a
1 /*
2 * This file handles the architecture dependent parts of process handling.
4 * Copyright IBM Corp. 1999,2009
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * Hartmut Penner <hp@de.ibm.com>,
7 * Denis Joseph Barrow,
8 */
10 #include <linux/compiler.h>
11 #include <linux/cpu.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/tick.h>
19 #include <linux/personality.h>
20 #include <linux/syscalls.h>
21 #include <linux/compat.h>
22 #include <linux/kprobes.h>
23 #include <linux/random.h>
24 #include <linux/module.h>
25 #include <asm/system.h>
26 #include <asm/io.h>
27 #include <asm/processor.h>
28 #include <asm/irq.h>
29 #include <asm/timer.h>
30 #include <asm/nmi.h>
31 #include <asm/smp.h>
32 #include "entry.h"
34 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
37 * Return saved PC of a blocked thread. used in kernel/sched.
38 * resume in entry.S does not create a new stack frame, it
39 * just stores the registers %r6-%r15 to the frame given by
40 * schedule. We want to return the address of the caller of
41 * schedule, so we have to walk the backchain one time to
42 * find the frame schedule() store its return address.
44 unsigned long thread_saved_pc(struct task_struct *tsk)
46 struct stack_frame *sf, *low, *high;
48 if (!tsk || !task_stack_page(tsk))
49 return 0;
50 low = task_stack_page(tsk);
51 high = (struct stack_frame *) task_pt_regs(tsk);
52 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
53 if (sf <= low || sf > high)
54 return 0;
55 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
56 if (sf <= low || sf > high)
57 return 0;
58 return sf->gprs[8];
62 * The idle loop on a S390...
64 static void default_idle(void)
66 if (cpu_is_offline(smp_processor_id()))
67 cpu_die();
68 local_irq_disable();
69 if (need_resched()) {
70 local_irq_enable();
71 return;
73 local_mcck_disable();
74 if (test_thread_flag(TIF_MCCK_PENDING)) {
75 local_mcck_enable();
76 local_irq_enable();
77 s390_handle_mcck();
78 return;
80 trace_hardirqs_on();
81 /* Don't trace preempt off for idle. */
82 stop_critical_timings();
83 /* Stop virtual timer and halt the cpu. */
84 vtime_stop_cpu();
85 /* Reenable preemption tracer. */
86 start_critical_timings();
89 void cpu_idle(void)
91 for (;;) {
92 tick_nohz_stop_sched_tick(1);
93 while (!need_resched())
94 default_idle();
95 tick_nohz_restart_sched_tick();
96 preempt_enable_no_resched();
97 schedule();
98 preempt_disable();
102 extern void __kprobes kernel_thread_starter(void);
104 asm(
105 ".section .kprobes.text, \"ax\"\n"
106 ".global kernel_thread_starter\n"
107 "kernel_thread_starter:\n"
108 " la 2,0(10)\n"
109 " basr 14,9\n"
110 " la 2,0\n"
111 " br 11\n"
112 ".previous\n");
114 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
116 struct pt_regs regs;
118 memset(&regs, 0, sizeof(regs));
119 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
120 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
121 regs.gprs[9] = (unsigned long) fn;
122 regs.gprs[10] = (unsigned long) arg;
123 regs.gprs[11] = (unsigned long) do_exit;
124 regs.orig_gpr2 = -1;
126 /* Ok, create the new process.. */
127 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
128 0, &regs, 0, NULL, NULL);
130 EXPORT_SYMBOL(kernel_thread);
133 * Free current thread data structures etc..
135 void exit_thread(void)
139 void flush_thread(void)
143 void release_thread(struct task_struct *dead_task)
147 int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
148 unsigned long unused,
149 struct task_struct *p, struct pt_regs *regs)
151 struct thread_info *ti;
152 struct fake_frame
154 struct stack_frame sf;
155 struct pt_regs childregs;
156 } *frame;
158 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
159 p->thread.ksp = (unsigned long) frame;
160 /* Store access registers to kernel stack of new process. */
161 frame->childregs = *regs;
162 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
163 frame->childregs.gprs[15] = new_stackp;
164 frame->sf.back_chain = 0;
166 /* new return point is ret_from_fork */
167 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
169 /* fake return stack for resume(), don't go back to schedule */
170 frame->sf.gprs[9] = (unsigned long) frame;
172 /* Save access registers to new thread structure. */
173 save_access_regs(&p->thread.acrs[0]);
175 #ifndef CONFIG_64BIT
177 * save fprs to current->thread.fp_regs to merge them with
178 * the emulated registers and then copy the result to the child.
180 save_fp_regs(&current->thread.fp_regs);
181 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
182 sizeof(s390_fp_regs));
183 /* Set a new TLS ? */
184 if (clone_flags & CLONE_SETTLS)
185 p->thread.acrs[0] = regs->gprs[6];
186 #else /* CONFIG_64BIT */
187 /* Save the fpu registers to new thread structure. */
188 save_fp_regs(&p->thread.fp_regs);
189 /* Set a new TLS ? */
190 if (clone_flags & CLONE_SETTLS) {
191 if (is_compat_task()) {
192 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
193 } else {
194 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
195 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
198 #endif /* CONFIG_64BIT */
199 /* start new process with ar4 pointing to the correct address space */
200 p->thread.mm_segment = get_fs();
201 /* Don't copy debug registers */
202 memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
203 memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
204 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
205 clear_tsk_thread_flag(p, TIF_PER_TRAP);
206 /* Initialize per thread user and system timer values */
207 ti = task_thread_info(p);
208 ti->user_timer = 0;
209 ti->system_timer = 0;
210 return 0;
213 SYSCALL_DEFINE0(fork)
215 struct pt_regs *regs = task_pt_regs(current);
216 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
219 SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
220 int __user *, parent_tidptr, int __user *, child_tidptr)
222 struct pt_regs *regs = task_pt_regs(current);
224 if (!newsp)
225 newsp = regs->gprs[15];
226 return do_fork(clone_flags, newsp, regs, 0,
227 parent_tidptr, child_tidptr);
231 * This is trivial, and on the face of it looks like it
232 * could equally well be done in user mode.
234 * Not so, for quite unobvious reasons - register pressure.
235 * In user mode vfork() cannot have a stack frame, and if
236 * done by calling the "clone()" system call directly, you
237 * do not have enough call-clobbered registers to hold all
238 * the information you need.
240 SYSCALL_DEFINE0(vfork)
242 struct pt_regs *regs = task_pt_regs(current);
243 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
244 regs->gprs[15], regs, 0, NULL, NULL);
247 asmlinkage void execve_tail(void)
249 current->thread.fp_regs.fpc = 0;
250 if (MACHINE_HAS_IEEE)
251 asm volatile("sfpc %0,%0" : : "d" (0));
255 * sys_execve() executes a new program.
257 SYSCALL_DEFINE3(execve, const char __user *, name,
258 const char __user *const __user *, argv,
259 const char __user *const __user *, envp)
261 struct pt_regs *regs = task_pt_regs(current);
262 char *filename;
263 long rc;
265 filename = getname(name);
266 rc = PTR_ERR(filename);
267 if (IS_ERR(filename))
268 return rc;
269 rc = do_execve(filename, argv, envp, regs);
270 if (rc)
271 goto out;
272 execve_tail();
273 rc = regs->gprs[2];
274 out:
275 putname(filename);
276 return rc;
280 * fill in the FPU structure for a core dump.
282 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
284 #ifndef CONFIG_64BIT
286 * save fprs to current->thread.fp_regs to merge them with
287 * the emulated registers and then copy the result to the dump.
289 save_fp_regs(&current->thread.fp_regs);
290 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
291 #else /* CONFIG_64BIT */
292 save_fp_regs(fpregs);
293 #endif /* CONFIG_64BIT */
294 return 1;
296 EXPORT_SYMBOL(dump_fpu);
298 unsigned long get_wchan(struct task_struct *p)
300 struct stack_frame *sf, *low, *high;
301 unsigned long return_address;
302 int count;
304 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
305 return 0;
306 low = task_stack_page(p);
307 high = (struct stack_frame *) task_pt_regs(p);
308 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
309 if (sf <= low || sf > high)
310 return 0;
311 for (count = 0; count < 16; count++) {
312 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
313 if (sf <= low || sf > high)
314 return 0;
315 return_address = sf->gprs[8] & PSW_ADDR_INSN;
316 if (!in_sched_functions(return_address))
317 return return_address;
319 return 0;
322 unsigned long arch_align_stack(unsigned long sp)
324 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
325 sp -= get_random_int() & ~PAGE_MASK;
326 return sp & ~0xf;
329 static inline unsigned long brk_rnd(void)
331 /* 8MB for 32bit, 1GB for 64bit */
332 if (is_32bit_task())
333 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
334 else
335 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
338 unsigned long arch_randomize_brk(struct mm_struct *mm)
340 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
342 if (ret < mm->brk)
343 return mm->brk;
344 return ret;
347 unsigned long randomize_et_dyn(unsigned long base)
349 unsigned long ret = PAGE_ALIGN(base + brk_rnd());
351 if (!(current->flags & PF_RANDOMIZE))
352 return base;
353 if (ret < base)
354 return base;
355 return ret;