blob: 30caecac94dcae955928900c4d6841c154263810 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/sched.h>
16#include <linux/preempt.h>
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/kprobes.h>
20#include <linux/elfcore.h>
21#include <linux/tick.h>
22#include <linux/init.h>
23#include <linux/mm.h>
24#include <linux/compat.h>
25#include <linux/hardirq.h>
26#include <linux/syscalls.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040027#include <linux/kernel.h>
Chris Metcalf313ce672011-05-02 14:50:06 -040028#include <linux/tracehook.h>
29#include <linux/signal.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040030#include <asm/stack.h>
31#include <asm/homecache.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040032#include <asm/syscalls.h>
Chris Metcalf313ce672011-05-02 14:50:06 -040033#include <asm/traps.h>
David Howellsbd119c62012-03-28 18:30:03 +010034#include <asm/setup.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040035#ifdef CONFIG_HARDWALL
36#include <asm/hardwall.h>
37#endif
Chris Metcalf867e3592010-05-28 23:09:12 -040038#include <arch/chip.h>
39#include <arch/abi.h>
David Howellsbd119c62012-03-28 18:30:03 +010040#include <arch/sim_def.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040041
42
43/*
44 * Use the (x86) "idle=poll" option to prefer low latency when leaving the
45 * idle loop over low power while in the idle loop, e.g. if we have
46 * one thread per core and we want to get threads out of futex waits fast.
47 */
48static int no_idle_nap;
49static int __init idle_setup(char *str)
50{
51 if (!str)
52 return -EINVAL;
53
54 if (!strcmp(str, "poll")) {
Chris Metcalf0707ad32010-06-25 17:04:17 -040055 pr_info("using polling idle threads.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -040056 no_idle_nap = 1;
57 } else if (!strcmp(str, "halt"))
58 no_idle_nap = 0;
59 else
60 return -1;
61
62 return 0;
63}
64early_param("idle", idle_setup);
65
66/*
67 * The idle thread. There's no useful work to be
68 * done, so just try to conserve power and have a
69 * low exit latency (ie sit in a loop waiting for
70 * somebody to say that they'd like to reschedule)
71 */
72void cpu_idle(void)
73{
Chris Metcalf867e3592010-05-28 23:09:12 -040074 int cpu = smp_processor_id();
75
76
77 current_thread_info()->status |= TS_POLLING;
78
79 if (no_idle_nap) {
80 while (1) {
81 while (!need_resched())
82 cpu_relax();
83 schedule();
84 }
85 }
86
87 /* endless idle loop with no priority at all */
88 while (1) {
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +010089 tick_nohz_idle_enter();
90 rcu_idle_enter();
Chris Metcalf867e3592010-05-28 23:09:12 -040091 while (!need_resched()) {
92 if (cpu_is_offline(cpu))
93 BUG(); /* no HOTPLUG_CPU */
94
95 local_irq_disable();
96 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
97 current_thread_info()->status &= ~TS_POLLING;
98 /*
99 * TS_POLLING-cleared state must be visible before we
100 * test NEED_RESCHED:
101 */
102 smp_mb();
103
104 if (!need_resched())
105 _cpu_idle();
106 else
107 local_irq_enable();
108 current_thread_info()->status |= TS_POLLING;
109 }
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100110 rcu_idle_exit();
111 tick_nohz_idle_exit();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100112 schedule_preempt_disabled();
Chris Metcalf867e3592010-05-28 23:09:12 -0400113 }
114}
115
Eric Dumazetb6a84012011-03-22 16:30:42 -0700116struct thread_info *alloc_thread_info_node(struct task_struct *task, int node)
Chris Metcalf867e3592010-05-28 23:09:12 -0400117{
118 struct page *page;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400119 gfp_t flags = GFP_KERNEL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400120
121#ifdef CONFIG_DEBUG_STACK_USAGE
122 flags |= __GFP_ZERO;
123#endif
124
Eric Dumazetb6a84012011-03-22 16:30:42 -0700125 page = alloc_pages_node(node, flags, THREAD_SIZE_ORDER);
Chris Metcalf867e3592010-05-28 23:09:12 -0400126 if (!page)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400127 return NULL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400128
129 return (struct thread_info *)page_address(page);
130}
131
132/*
133 * Free a thread_info node, and all of its derivative
134 * data structures.
135 */
136void free_thread_info(struct thread_info *info)
137{
138 struct single_step_state *step_state = info->step_state;
139
Chris Metcalf0707ad32010-06-25 17:04:17 -0400140#ifdef CONFIG_HARDWALL
141 /*
142 * We free a thread_info from the context of the task that has
143 * been scheduled next, so the original task is already dead.
144 * Calling deactivate here just frees up the data structures.
145 * If the task we're freeing held the last reference to a
146 * hardwall fd, it would have been released prior to this point
147 * anyway via exit_files(), and "hardwall" would be NULL by now.
148 */
149 if (info->task->thread.hardwall)
150 hardwall_deactivate(info->task);
151#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400152
153 if (step_state) {
154
155 /*
156 * FIXME: we don't munmap step_state->buffer
157 * because the mm_struct for this process (info->task->mm)
158 * has already been zeroed in exit_mm(). Keeping a
159 * reference to it here seems like a bad move, so this
160 * means we can't munmap() the buffer, and therefore if we
161 * ptrace multiple threads in a process, we will slowly
162 * leak user memory. (Note that as soon as the last
163 * thread in a process dies, we will reclaim all user
164 * memory including single-step buffers in the usual way.)
165 * We should either assign a kernel VA to this buffer
166 * somehow, or we should associate the buffer(s) with the
167 * mm itself so we can clean them up that way.
168 */
169 kfree(step_state);
170 }
171
Chris Metcalf76c567f2011-02-28 16:37:34 -0500172 free_pages((unsigned long)info, THREAD_SIZE_ORDER);
Chris Metcalf867e3592010-05-28 23:09:12 -0400173}
174
175static void save_arch_state(struct thread_struct *t);
176
Chris Metcalf867e3592010-05-28 23:09:12 -0400177int copy_thread(unsigned long clone_flags, unsigned long sp,
178 unsigned long stack_size,
179 struct task_struct *p, struct pt_regs *regs)
180{
181 struct pt_regs *childregs;
182 unsigned long ksp;
183
184 /*
185 * When creating a new kernel thread we pass sp as zero.
186 * Assign it to a reasonable value now that we have the stack.
187 */
188 if (sp == 0 && regs->ex1 == PL_ICS_EX1(KERNEL_PL, 0))
189 sp = KSTK_TOP(p);
190
191 /*
192 * Do not clone step state from the parent; each thread
193 * must make its own lazily.
194 */
195 task_thread_info(p)->step_state = NULL;
196
197 /*
198 * Start new thread in ret_from_fork so it schedules properly
199 * and then return from interrupt like the parent.
200 */
201 p->thread.pc = (unsigned long) ret_from_fork;
202
203 /* Save user stack top pointer so we can ID the stack vm area later. */
204 p->thread.usp0 = sp;
205
206 /* Record the pid of the process that created this one. */
207 p->thread.creator_pid = current->pid;
208
209 /*
210 * Copy the registers onto the kernel stack so the
211 * return-from-interrupt code will reload it into registers.
212 */
213 childregs = task_pt_regs(p);
214 *childregs = *regs;
215 childregs->regs[0] = 0; /* return value is zero */
216 childregs->sp = sp; /* override with new user stack pointer */
217
218 /*
Chris Metcalfbc4cf2b2010-12-14 15:57:49 -0500219 * If CLONE_SETTLS is set, set "tp" in the new task to "r4",
220 * which is passed in as arg #5 to sys_clone().
221 */
222 if (clone_flags & CLONE_SETTLS)
223 childregs->tp = regs->regs[4];
224
225 /*
Chris Metcalf867e3592010-05-28 23:09:12 -0400226 * Copy the callee-saved registers from the passed pt_regs struct
227 * into the context-switch callee-saved registers area.
Chris Metcalfd6f0f222010-10-14 14:42:58 -0400228 * This way when we start the interrupt-return sequence, the
229 * callee-save registers will be correctly in registers, which
230 * is how we assume the compiler leaves them as we start doing
231 * the normal return-from-interrupt path after calling C code.
Chris Metcalf867e3592010-05-28 23:09:12 -0400232 * Zero out the C ABI save area to mark the top of the stack.
233 */
234 ksp = (unsigned long) childregs;
235 ksp -= C_ABI_SAVE_AREA_SIZE; /* interrupt-entry save area */
236 ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
237 ksp -= CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long);
238 memcpy((void *)ksp, &regs->regs[CALLEE_SAVED_FIRST_REG],
239 CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long));
240 ksp -= C_ABI_SAVE_AREA_SIZE; /* __switch_to() save area */
241 ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
242 p->thread.ksp = ksp;
243
244#if CHIP_HAS_TILE_DMA()
245 /*
246 * No DMA in the new thread. We model this on the fact that
247 * fork() clears the pending signals, alarms, and aio for the child.
248 */
249 memset(&p->thread.tile_dma_state, 0, sizeof(struct tile_dma_state));
250 memset(&p->thread.dma_async_tlb, 0, sizeof(struct async_tlb));
251#endif
252
253#if CHIP_HAS_SN_PROC()
254 /* Likewise, the new thread is not running static processor code. */
255 p->thread.sn_proc_running = 0;
256 memset(&p->thread.sn_async_tlb, 0, sizeof(struct async_tlb));
257#endif
258
259#if CHIP_HAS_PROC_STATUS_SPR()
260 /* New thread has its miscellaneous processor state bits clear. */
261 p->thread.proc_status = 0;
262#endif
263
Chris Metcalf0707ad32010-06-25 17:04:17 -0400264#ifdef CONFIG_HARDWALL
265 /* New thread does not own any networks. */
266 p->thread.hardwall = NULL;
267#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400268
269
270 /*
271 * Start the new thread with the current architecture state
272 * (user interrupt masks, etc.).
273 */
274 save_arch_state(&p->thread);
275
276 return 0;
277}
278
279/*
280 * Return "current" if it looks plausible, or else a pointer to a dummy.
281 * This can be helpful if we are just trying to emit a clean panic.
282 */
283struct task_struct *validate_current(void)
284{
285 static struct task_struct corrupt = { .comm = "<corrupt>" };
286 struct task_struct *tsk = current;
287 if (unlikely((unsigned long)tsk < PAGE_OFFSET ||
288 (void *)tsk > high_memory ||
289 ((unsigned long)tsk & (__alignof__(*tsk) - 1)) != 0)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400290 pr_err("Corrupt 'current' %p (sp %#lx)\n", tsk, stack_pointer);
Chris Metcalf867e3592010-05-28 23:09:12 -0400291 tsk = &corrupt;
292 }
293 return tsk;
294}
295
296/* Take and return the pointer to the previous task, for schedule_tail(). */
297struct task_struct *sim_notify_fork(struct task_struct *prev)
298{
299 struct task_struct *tsk = current;
300 __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK_PARENT |
301 (tsk->thread.creator_pid << _SIM_CONTROL_OPERATOR_BITS));
302 __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK |
303 (tsk->pid << _SIM_CONTROL_OPERATOR_BITS));
304 return prev;
305}
306
307int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
308{
309 struct pt_regs *ptregs = task_pt_regs(tsk);
310 elf_core_copy_regs(regs, ptregs);
311 return 1;
312}
313
314#if CHIP_HAS_TILE_DMA()
315
316/* Allow user processes to access the DMA SPRs */
317void grant_dma_mpls(void)
318{
Chris Metcalfa78c9422010-10-14 16:23:03 -0400319#if CONFIG_KERNEL_PL == 2
320 __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
321 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
322#else
Chris Metcalf867e3592010-05-28 23:09:12 -0400323 __insn_mtspr(SPR_MPL_DMA_CPL_SET_0, 1);
324 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_0, 1);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400325#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400326}
327
328/* Forbid user processes from accessing the DMA SPRs */
329void restrict_dma_mpls(void)
330{
Chris Metcalfa78c9422010-10-14 16:23:03 -0400331#if CONFIG_KERNEL_PL == 2
332 __insn_mtspr(SPR_MPL_DMA_CPL_SET_2, 1);
333 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_2, 1);
334#else
Chris Metcalf867e3592010-05-28 23:09:12 -0400335 __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
336 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400337#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400338}
339
340/* Pause the DMA engine, then save off its state registers. */
341static void save_tile_dma_state(struct tile_dma_state *dma)
342{
343 unsigned long state = __insn_mfspr(SPR_DMA_USER_STATUS);
344 unsigned long post_suspend_state;
345
346 /* If we're running, suspend the engine. */
347 if ((state & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK)
348 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
349
350 /*
351 * Wait for the engine to idle, then save regs. Note that we
352 * want to record the "running" bit from before suspension,
353 * and the "done" bit from after, so that we can properly
354 * distinguish a case where the user suspended the engine from
355 * the case where the kernel suspended as part of the context
356 * swap.
357 */
358 do {
359 post_suspend_state = __insn_mfspr(SPR_DMA_USER_STATUS);
360 } while (post_suspend_state & SPR_DMA_STATUS__BUSY_MASK);
361
362 dma->src = __insn_mfspr(SPR_DMA_SRC_ADDR);
363 dma->src_chunk = __insn_mfspr(SPR_DMA_SRC_CHUNK_ADDR);
364 dma->dest = __insn_mfspr(SPR_DMA_DST_ADDR);
365 dma->dest_chunk = __insn_mfspr(SPR_DMA_DST_CHUNK_ADDR);
366 dma->strides = __insn_mfspr(SPR_DMA_STRIDE);
367 dma->chunk_size = __insn_mfspr(SPR_DMA_CHUNK_SIZE);
368 dma->byte = __insn_mfspr(SPR_DMA_BYTE);
369 dma->status = (state & SPR_DMA_STATUS__RUNNING_MASK) |
370 (post_suspend_state & SPR_DMA_STATUS__DONE_MASK);
371}
372
373/* Restart a DMA that was running before we were context-switched out. */
374static void restore_tile_dma_state(struct thread_struct *t)
375{
376 const struct tile_dma_state *dma = &t->tile_dma_state;
377
378 /*
379 * The only way to restore the done bit is to run a zero
380 * length transaction.
381 */
382 if ((dma->status & SPR_DMA_STATUS__DONE_MASK) &&
383 !(__insn_mfspr(SPR_DMA_USER_STATUS) & SPR_DMA_STATUS__DONE_MASK)) {
384 __insn_mtspr(SPR_DMA_BYTE, 0);
385 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
386 while (__insn_mfspr(SPR_DMA_USER_STATUS) &
387 SPR_DMA_STATUS__BUSY_MASK)
388 ;
389 }
390
391 __insn_mtspr(SPR_DMA_SRC_ADDR, dma->src);
392 __insn_mtspr(SPR_DMA_SRC_CHUNK_ADDR, dma->src_chunk);
393 __insn_mtspr(SPR_DMA_DST_ADDR, dma->dest);
394 __insn_mtspr(SPR_DMA_DST_CHUNK_ADDR, dma->dest_chunk);
395 __insn_mtspr(SPR_DMA_STRIDE, dma->strides);
396 __insn_mtspr(SPR_DMA_CHUNK_SIZE, dma->chunk_size);
397 __insn_mtspr(SPR_DMA_BYTE, dma->byte);
398
399 /*
400 * Restart the engine if we were running and not done.
401 * Clear a pending async DMA fault that we were waiting on return
402 * to user space to execute, since we expect the DMA engine
403 * to regenerate those faults for us now. Note that we don't
404 * try to clear the TIF_ASYNC_TLB flag, since it's relatively
405 * harmless if set, and it covers both DMA and the SN processor.
406 */
407 if ((dma->status & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK) {
408 t->dma_async_tlb.fault_num = 0;
409 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
410 }
411}
412
413#endif
414
415static void save_arch_state(struct thread_struct *t)
416{
417#if CHIP_HAS_SPLIT_INTR_MASK()
418 t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0_0) |
419 ((u64)__insn_mfspr(SPR_INTERRUPT_MASK_0_1) << 32);
420#else
421 t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0);
422#endif
423 t->ex_context[0] = __insn_mfspr(SPR_EX_CONTEXT_0_0);
424 t->ex_context[1] = __insn_mfspr(SPR_EX_CONTEXT_0_1);
425 t->system_save[0] = __insn_mfspr(SPR_SYSTEM_SAVE_0_0);
426 t->system_save[1] = __insn_mfspr(SPR_SYSTEM_SAVE_0_1);
427 t->system_save[2] = __insn_mfspr(SPR_SYSTEM_SAVE_0_2);
428 t->system_save[3] = __insn_mfspr(SPR_SYSTEM_SAVE_0_3);
429 t->intctrl_0 = __insn_mfspr(SPR_INTCTRL_0_STATUS);
430#if CHIP_HAS_PROC_STATUS_SPR()
431 t->proc_status = __insn_mfspr(SPR_PROC_STATUS);
432#endif
Chris Metcalfa802fc62010-09-15 11:16:10 -0400433#if !CHIP_HAS_FIXED_INTVEC_BASE()
434 t->interrupt_vector_base = __insn_mfspr(SPR_INTERRUPT_VECTOR_BASE_0);
435#endif
436#if CHIP_HAS_TILE_RTF_HWM()
437 t->tile_rtf_hwm = __insn_mfspr(SPR_TILE_RTF_HWM);
438#endif
439#if CHIP_HAS_DSTREAM_PF()
440 t->dstream_pf = __insn_mfspr(SPR_DSTREAM_PF);
441#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400442}
443
444static void restore_arch_state(const struct thread_struct *t)
445{
446#if CHIP_HAS_SPLIT_INTR_MASK()
447 __insn_mtspr(SPR_INTERRUPT_MASK_0_0, (u32) t->interrupt_mask);
448 __insn_mtspr(SPR_INTERRUPT_MASK_0_1, t->interrupt_mask >> 32);
449#else
450 __insn_mtspr(SPR_INTERRUPT_MASK_0, t->interrupt_mask);
451#endif
452 __insn_mtspr(SPR_EX_CONTEXT_0_0, t->ex_context[0]);
453 __insn_mtspr(SPR_EX_CONTEXT_0_1, t->ex_context[1]);
454 __insn_mtspr(SPR_SYSTEM_SAVE_0_0, t->system_save[0]);
455 __insn_mtspr(SPR_SYSTEM_SAVE_0_1, t->system_save[1]);
456 __insn_mtspr(SPR_SYSTEM_SAVE_0_2, t->system_save[2]);
457 __insn_mtspr(SPR_SYSTEM_SAVE_0_3, t->system_save[3]);
458 __insn_mtspr(SPR_INTCTRL_0_STATUS, t->intctrl_0);
459#if CHIP_HAS_PROC_STATUS_SPR()
460 __insn_mtspr(SPR_PROC_STATUS, t->proc_status);
461#endif
Chris Metcalfa802fc62010-09-15 11:16:10 -0400462#if !CHIP_HAS_FIXED_INTVEC_BASE()
463 __insn_mtspr(SPR_INTERRUPT_VECTOR_BASE_0, t->interrupt_vector_base);
464#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400465#if CHIP_HAS_TILE_RTF_HWM()
Chris Metcalfa802fc62010-09-15 11:16:10 -0400466 __insn_mtspr(SPR_TILE_RTF_HWM, t->tile_rtf_hwm);
467#endif
468#if CHIP_HAS_DSTREAM_PF()
469 __insn_mtspr(SPR_DSTREAM_PF, t->dstream_pf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400470#endif
471}
472
473
474void _prepare_arch_switch(struct task_struct *next)
475{
476#if CHIP_HAS_SN_PROC()
477 int snctl;
478#endif
479#if CHIP_HAS_TILE_DMA()
480 struct tile_dma_state *dma = &current->thread.tile_dma_state;
481 if (dma->enabled)
482 save_tile_dma_state(dma);
483#endif
484#if CHIP_HAS_SN_PROC()
485 /*
486 * Suspend the static network processor if it was running.
487 * We do not suspend the fabric itself, just like we don't
488 * try to suspend the UDN.
489 */
490 snctl = __insn_mfspr(SPR_SNCTL);
491 current->thread.sn_proc_running =
492 (snctl & SPR_SNCTL__FRZPROC_MASK) == 0;
493 if (current->thread.sn_proc_running)
494 __insn_mtspr(SPR_SNCTL, snctl | SPR_SNCTL__FRZPROC_MASK);
495#endif
496}
497
498
Chris Metcalf867e3592010-05-28 23:09:12 -0400499struct task_struct *__sched _switch_to(struct task_struct *prev,
500 struct task_struct *next)
501{
502 /* DMA state is already saved; save off other arch state. */
503 save_arch_state(&prev->thread);
504
505#if CHIP_HAS_TILE_DMA()
506 /*
507 * Restore DMA in new task if desired.
508 * Note that it is only safe to restart here since interrupts
509 * are disabled, so we can't take any DMATLB miss or access
510 * interrupts before we have finished switching stacks.
511 */
512 if (next->thread.tile_dma_state.enabled) {
513 restore_tile_dma_state(&next->thread);
514 grant_dma_mpls();
515 } else {
516 restrict_dma_mpls();
517 }
518#endif
519
520 /* Restore other arch state. */
521 restore_arch_state(&next->thread);
522
523#if CHIP_HAS_SN_PROC()
524 /*
525 * Restart static network processor in the new process
526 * if it was running before.
527 */
528 if (next->thread.sn_proc_running) {
529 int snctl = __insn_mfspr(SPR_SNCTL);
530 __insn_mtspr(SPR_SNCTL, snctl & ~SPR_SNCTL__FRZPROC_MASK);
531 }
532#endif
533
Chris Metcalf0707ad32010-06-25 17:04:17 -0400534#ifdef CONFIG_HARDWALL
535 /* Enable or disable access to the network registers appropriately. */
536 if (prev->thread.hardwall != NULL) {
537 if (next->thread.hardwall == NULL)
538 restrict_network_mpls();
539 } else if (next->thread.hardwall != NULL) {
540 grant_network_mpls();
541 }
542#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400543
544 /*
545 * Switch kernel SP, PC, and callee-saved registers.
546 * In the context of the new task, return the old task pointer
547 * (i.e. the task that actually called __switch_to).
Chris Metcalfa78c9422010-10-14 16:23:03 -0400548 * Pass the value to use for SYSTEM_SAVE_K_0 when we reset our sp.
Chris Metcalf867e3592010-05-28 23:09:12 -0400549 */
550 return __switch_to(prev, next, next_current_ksp0(next));
551}
552
Chris Metcalf313ce672011-05-02 14:50:06 -0400553/*
554 * This routine is called on return from interrupt if any of the
555 * TIF_WORK_MASK flags are set in thread_info->flags. It is
556 * entered with interrupts disabled so we don't miss an event
557 * that modified the thread_info flags. If any flag is set, we
558 * handle it and return, and the calling assembly code will
559 * re-disable interrupts, reload the thread flags, and call back
560 * if more flags need to be handled.
561 *
562 * We return whether we need to check the thread_info flags again
563 * or not. Note that we don't clear TIF_SINGLESTEP here, so it's
564 * important that it be tested last, and then claim that we don't
565 * need to recheck the flags.
566 */
567int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
568{
569 if (thread_info_flags & _TIF_NEED_RESCHED) {
570 schedule();
571 return 1;
572 }
573#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
574 if (thread_info_flags & _TIF_ASYNC_TLB) {
575 do_async_page_fault(regs);
576 return 1;
577 }
578#endif
579 if (thread_info_flags & _TIF_SIGPENDING) {
580 do_signal(regs);
581 return 1;
582 }
583 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
584 clear_thread_flag(TIF_NOTIFY_RESUME);
585 tracehook_notify_resume(regs);
586 if (current->replacement_session_keyring)
587 key_replace_session_keyring();
588 return 1;
589 }
590 if (thread_info_flags & _TIF_SINGLESTEP) {
591 if ((regs->ex1 & SPR_EX_CONTEXT_1_1__PL_MASK) == 0)
592 single_step_once(regs);
593 return 0;
594 }
595 panic("work_pending: bad flags %#x\n", thread_info_flags);
596}
597
Chris Metcalfbc4cf2b2010-12-14 15:57:49 -0500598/* Note there is an implicit fifth argument if (clone_flags & CLONE_SETTLS). */
Chris Metcalfd929b6a2010-10-14 14:34:33 -0400599SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
600 void __user *, parent_tidptr, void __user *, child_tidptr,
601 struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400602{
603 if (!newsp)
604 newsp = regs->sp;
605 return do_fork(clone_flags, newsp, regs, 0,
606 parent_tidptr, child_tidptr);
607}
608
Chris Metcalf867e3592010-05-28 23:09:12 -0400609/*
610 * sys_execve() executes a new program.
611 */
Chris Metcalfd929b6a2010-10-14 14:34:33 -0400612SYSCALL_DEFINE4(execve, const char __user *, path,
613 const char __user *const __user *, argv,
614 const char __user *const __user *, envp,
615 struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400616{
Chris Metcalf0707ad32010-06-25 17:04:17 -0400617 long error;
Chris Metcalf867e3592010-05-28 23:09:12 -0400618 char *filename;
619
620 filename = getname(path);
621 error = PTR_ERR(filename);
622 if (IS_ERR(filename))
623 goto out;
624 error = do_execve(filename, argv, envp, regs);
625 putname(filename);
Chris Metcalf04f7a3f2011-02-28 13:08:32 -0500626 if (error == 0)
627 single_step_execve();
Chris Metcalf867e3592010-05-28 23:09:12 -0400628out:
629 return error;
630}
631
632#ifdef CONFIG_COMPAT
Chris Metcalfd929b6a2010-10-14 14:34:33 -0400633long compat_sys_execve(const char __user *path,
Chris Metcalf18aecc22011-05-04 14:38:26 -0400634 compat_uptr_t __user *argv,
635 compat_uptr_t __user *envp,
Chris Metcalfd929b6a2010-10-14 14:34:33 -0400636 struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400637{
Chris Metcalf0707ad32010-06-25 17:04:17 -0400638 long error;
Chris Metcalf867e3592010-05-28 23:09:12 -0400639 char *filename;
640
641 filename = getname(path);
642 error = PTR_ERR(filename);
643 if (IS_ERR(filename))
644 goto out;
645 error = compat_do_execve(filename, argv, envp, regs);
646 putname(filename);
Chris Metcalf04f7a3f2011-02-28 13:08:32 -0500647 if (error == 0)
648 single_step_execve();
Chris Metcalf867e3592010-05-28 23:09:12 -0400649out:
650 return error;
651}
652#endif
653
654unsigned long get_wchan(struct task_struct *p)
655{
656 struct KBacktraceIterator kbt;
657
658 if (!p || p == current || p->state == TASK_RUNNING)
659 return 0;
660
661 for (KBacktraceIterator_init(&kbt, p, NULL);
662 !KBacktraceIterator_end(&kbt);
663 KBacktraceIterator_next(&kbt)) {
664 if (!in_sched_functions(kbt.it.pc))
665 return kbt.it.pc;
666 }
667
668 return 0;
669}
670
671/*
672 * We pass in lr as zero (cleared in kernel_thread) and the caller
673 * part of the backtrace ABI on the stack also zeroed (in copy_thread)
674 * so that backtraces will stop with this function.
675 * Note that we don't use r0, since copy_thread() clears it.
676 */
677static void start_kernel_thread(int dummy, int (*fn)(int), int arg)
678{
679 do_exit(fn(arg));
680}
681
682/*
683 * Create a kernel thread
684 */
685int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
686{
687 struct pt_regs regs;
688
689 memset(&regs, 0, sizeof(regs));
690 regs.ex1 = PL_ICS_EX1(KERNEL_PL, 0); /* run at kernel PL, no ICS */
691 regs.pc = (long) start_kernel_thread;
692 regs.flags = PT_FLAGS_CALLER_SAVES; /* need to restore r1 and r2 */
693 regs.regs[1] = (long) fn; /* function pointer */
694 regs.regs[2] = (long) arg; /* parameter register */
695
696 /* Ok, create the new process.. */
697 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs,
698 0, NULL, NULL);
699}
700EXPORT_SYMBOL(kernel_thread);
701
702/* Flush thread state. */
703void flush_thread(void)
704{
705 /* Nothing */
706}
707
708/*
709 * Free current thread data structures etc..
710 */
711void exit_thread(void)
712{
713 /* Nothing */
714}
715
Chris Metcalf867e3592010-05-28 23:09:12 -0400716void show_regs(struct pt_regs *regs)
717{
718 struct task_struct *tsk = validate_current();
Chris Metcalf0707ad32010-06-25 17:04:17 -0400719 int i;
720
721 pr_err("\n");
722 pr_err(" Pid: %d, comm: %20s, CPU: %d\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400723 tsk->pid, tsk->comm, smp_processor_id());
Chris Metcalf0707ad32010-06-25 17:04:17 -0400724#ifdef __tilegx__
725 for (i = 0; i < 51; i += 3)
726 pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
727 i, regs->regs[i], i+1, regs->regs[i+1],
728 i+2, regs->regs[i+2]);
729 pr_err(" r51: "REGFMT" r52: "REGFMT" tp : "REGFMT"\n",
730 regs->regs[51], regs->regs[52], regs->tp);
731 pr_err(" sp : "REGFMT" lr : "REGFMT"\n", regs->sp, regs->lr);
732#else
Chris Metcalf7040dea2010-09-15 11:17:05 -0400733 for (i = 0; i < 52; i += 4)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400734 pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT
735 " r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
736 i, regs->regs[i], i+1, regs->regs[i+1],
737 i+2, regs->regs[i+2], i+3, regs->regs[i+3]);
738 pr_err(" r52: "REGFMT" tp : "REGFMT" sp : "REGFMT" lr : "REGFMT"\n",
739 regs->regs[52], regs->tp, regs->sp, regs->lr);
740#endif
741 pr_err(" pc : "REGFMT" ex1: %ld faultnum: %ld\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400742 regs->pc, regs->ex1, regs->faultnum);
743
744 dump_stack_regs(regs);
745}