2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
15 #include <linux/sched.h>
16 #include <linux/preempt.h>
17 #include <linux/module.h>
19 #include <linux/kprobes.h>
20 #include <linux/elfcore.h>
21 #include <linux/tick.h>
22 #include <linux/init.h>
24 #include <linux/compat.h>
25 #include <linux/hardirq.h>
26 #include <linux/syscalls.h>
27 #include <linux/kernel.h>
28 #include <linux/tracehook.h>
29 #include <linux/signal.h>
30 #include <asm/stack.h>
31 #include <asm/switch_to.h>
32 #include <asm/homecache.h>
33 #include <asm/syscalls.h>
34 #include <asm/traps.h>
35 #include <asm/setup.h>
36 #ifdef CONFIG_HARDWALL
37 #include <asm/hardwall.h>
39 #include <arch/chip.h>
41 #include <arch/sim_def.h>
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.
48 static int __init
idle_setup(char *str
)
53 if (!strcmp(str
, "poll")) {
54 pr_info("using polling idle threads.\n");
55 cpu_idle_poll_ctrl(true);
57 } else if (!strcmp(str
, "halt")) {
62 early_param("idle", idle_setup
);
64 void arch_cpu_idle(void)
66 __get_cpu_var(irq_stat
).idle_timestamp
= jiffies
;
71 * Release a thread_info structure
73 void arch_release_thread_info(struct thread_info
*info
)
75 struct single_step_state
*step_state
= info
->step_state
;
77 #ifdef CONFIG_HARDWALL
79 * We free a thread_info from the context of the task that has
80 * been scheduled next, so the original task is already dead.
81 * Calling deactivate here just frees up the data structures.
82 * If the task we're freeing held the last reference to a
83 * hardwall fd, it would have been released prior to this point
84 * anyway via exit_files(), and the hardwall_task.info pointers
85 * would be NULL by now.
87 hardwall_deactivate_all(info
->task
);
93 * FIXME: we don't munmap step_state->buffer
94 * because the mm_struct for this process (info->task->mm)
95 * has already been zeroed in exit_mm(). Keeping a
96 * reference to it here seems like a bad move, so this
97 * means we can't munmap() the buffer, and therefore if we
98 * ptrace multiple threads in a process, we will slowly
99 * leak user memory. (Note that as soon as the last
100 * thread in a process dies, we will reclaim all user
101 * memory including single-step buffers in the usual way.)
102 * We should either assign a kernel VA to this buffer
103 * somehow, or we should associate the buffer(s) with the
104 * mm itself so we can clean them up that way.
110 static void save_arch_state(struct thread_struct
*t
);
112 int copy_thread(unsigned long clone_flags
, unsigned long sp
,
113 unsigned long arg
, struct task_struct
*p
)
115 struct pt_regs
*childregs
= task_pt_regs(p
);
117 unsigned long *callee_regs
;
120 * Set up the stack and stack pointer appropriately for the
121 * new child to find itself woken up in __switch_to().
122 * The callee-saved registers must be on the stack to be read;
123 * the new task will then jump to assembly support to handle
124 * calling schedule_tail(), etc., and (for userspace tasks)
125 * returning to the context set up in the pt_regs.
127 ksp
= (unsigned long) childregs
;
128 ksp
-= C_ABI_SAVE_AREA_SIZE
; /* interrupt-entry save area */
129 ((long *)ksp
)[0] = ((long *)ksp
)[1] = 0;
130 ksp
-= CALLEE_SAVED_REGS_COUNT
* sizeof(unsigned long);
131 callee_regs
= (unsigned long *)ksp
;
132 ksp
-= C_ABI_SAVE_AREA_SIZE
; /* __switch_to() save area */
133 ((long *)ksp
)[0] = ((long *)ksp
)[1] = 0;
136 /* Record the pid of the task that created this one. */
137 p
->thread
.creator_pid
= current
->pid
;
139 if (unlikely(p
->flags
& PF_KTHREAD
)) {
141 memset(childregs
, 0, sizeof(struct pt_regs
));
142 memset(&callee_regs
[2], 0,
143 (CALLEE_SAVED_REGS_COUNT
- 2) * sizeof(unsigned long));
144 callee_regs
[0] = sp
; /* r30 = function */
145 callee_regs
[1] = arg
; /* r31 = arg */
146 childregs
->ex1
= PL_ICS_EX1(KERNEL_PL
, 0);
147 p
->thread
.pc
= (unsigned long) ret_from_kernel_thread
;
152 * Start new thread in ret_from_fork so it schedules properly
153 * and then return from interrupt like the parent.
155 p
->thread
.pc
= (unsigned long) ret_from_fork
;
158 * Do not clone step state from the parent; each thread
159 * must make its own lazily.
161 task_thread_info(p
)->step_state
= NULL
;
164 * Copy the registers onto the kernel stack so the
165 * return-from-interrupt code will reload it into registers.
167 *childregs
= *current_pt_regs();
168 childregs
->regs
[0] = 0; /* return value is zero */
170 childregs
->sp
= sp
; /* override with new user stack pointer */
171 memcpy(callee_regs
, &childregs
->regs
[CALLEE_SAVED_FIRST_REG
],
172 CALLEE_SAVED_REGS_COUNT
* sizeof(unsigned long));
174 /* Save user stack top pointer so we can ID the stack vm area later. */
175 p
->thread
.usp0
= childregs
->sp
;
178 * If CLONE_SETTLS is set, set "tp" in the new task to "r4",
179 * which is passed in as arg #5 to sys_clone().
181 if (clone_flags
& CLONE_SETTLS
)
182 childregs
->tp
= childregs
->regs
[4];
185 #if CHIP_HAS_TILE_DMA()
187 * No DMA in the new thread. We model this on the fact that
188 * fork() clears the pending signals, alarms, and aio for the child.
190 memset(&p
->thread
.tile_dma_state
, 0, sizeof(struct tile_dma_state
));
191 memset(&p
->thread
.dma_async_tlb
, 0, sizeof(struct async_tlb
));
194 #if CHIP_HAS_SN_PROC()
195 /* Likewise, the new thread is not running static processor code. */
196 p
->thread
.sn_proc_running
= 0;
197 memset(&p
->thread
.sn_async_tlb
, 0, sizeof(struct async_tlb
));
200 #if CHIP_HAS_PROC_STATUS_SPR()
201 /* New thread has its miscellaneous processor state bits clear. */
202 p
->thread
.proc_status
= 0;
205 #ifdef CONFIG_HARDWALL
206 /* New thread does not own any networks. */
207 memset(&p
->thread
.hardwall
[0], 0,
208 sizeof(struct hardwall_task
) * HARDWALL_TYPES
);
213 * Start the new thread with the current architecture state
214 * (user interrupt masks, etc.).
216 save_arch_state(&p
->thread
);
222 * Return "current" if it looks plausible, or else a pointer to a dummy.
223 * This can be helpful if we are just trying to emit a clean panic.
225 struct task_struct
*validate_current(void)
227 static struct task_struct corrupt
= { .comm
= "<corrupt>" };
228 struct task_struct
*tsk
= current
;
229 if (unlikely((unsigned long)tsk
< PAGE_OFFSET
||
230 (high_memory
&& (void *)tsk
> high_memory
) ||
231 ((unsigned long)tsk
& (__alignof__(*tsk
) - 1)) != 0)) {
232 pr_err("Corrupt 'current' %p (sp %#lx)\n", tsk
, stack_pointer
);
238 /* Take and return the pointer to the previous task, for schedule_tail(). */
239 struct task_struct
*sim_notify_fork(struct task_struct
*prev
)
241 struct task_struct
*tsk
= current
;
242 __insn_mtspr(SPR_SIM_CONTROL
, SIM_CONTROL_OS_FORK_PARENT
|
243 (tsk
->thread
.creator_pid
<< _SIM_CONTROL_OPERATOR_BITS
));
244 __insn_mtspr(SPR_SIM_CONTROL
, SIM_CONTROL_OS_FORK
|
245 (tsk
->pid
<< _SIM_CONTROL_OPERATOR_BITS
));
249 int dump_task_regs(struct task_struct
*tsk
, elf_gregset_t
*regs
)
251 struct pt_regs
*ptregs
= task_pt_regs(tsk
);
252 elf_core_copy_regs(regs
, ptregs
);
256 #if CHIP_HAS_TILE_DMA()
258 /* Allow user processes to access the DMA SPRs */
259 void grant_dma_mpls(void)
261 #if CONFIG_KERNEL_PL == 2
262 __insn_mtspr(SPR_MPL_DMA_CPL_SET_1
, 1);
263 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1
, 1);
265 __insn_mtspr(SPR_MPL_DMA_CPL_SET_0
, 1);
266 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_0
, 1);
270 /* Forbid user processes from accessing the DMA SPRs */
271 void restrict_dma_mpls(void)
273 #if CONFIG_KERNEL_PL == 2
274 __insn_mtspr(SPR_MPL_DMA_CPL_SET_2
, 1);
275 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_2
, 1);
277 __insn_mtspr(SPR_MPL_DMA_CPL_SET_1
, 1);
278 __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1
, 1);
282 /* Pause the DMA engine, then save off its state registers. */
283 static void save_tile_dma_state(struct tile_dma_state
*dma
)
285 unsigned long state
= __insn_mfspr(SPR_DMA_USER_STATUS
);
286 unsigned long post_suspend_state
;
288 /* If we're running, suspend the engine. */
289 if ((state
& DMA_STATUS_MASK
) == SPR_DMA_STATUS__RUNNING_MASK
)
290 __insn_mtspr(SPR_DMA_CTR
, SPR_DMA_CTR__SUSPEND_MASK
);
293 * Wait for the engine to idle, then save regs. Note that we
294 * want to record the "running" bit from before suspension,
295 * and the "done" bit from after, so that we can properly
296 * distinguish a case where the user suspended the engine from
297 * the case where the kernel suspended as part of the context
301 post_suspend_state
= __insn_mfspr(SPR_DMA_USER_STATUS
);
302 } while (post_suspend_state
& SPR_DMA_STATUS__BUSY_MASK
);
304 dma
->src
= __insn_mfspr(SPR_DMA_SRC_ADDR
);
305 dma
->src_chunk
= __insn_mfspr(SPR_DMA_SRC_CHUNK_ADDR
);
306 dma
->dest
= __insn_mfspr(SPR_DMA_DST_ADDR
);
307 dma
->dest_chunk
= __insn_mfspr(SPR_DMA_DST_CHUNK_ADDR
);
308 dma
->strides
= __insn_mfspr(SPR_DMA_STRIDE
);
309 dma
->chunk_size
= __insn_mfspr(SPR_DMA_CHUNK_SIZE
);
310 dma
->byte
= __insn_mfspr(SPR_DMA_BYTE
);
311 dma
->status
= (state
& SPR_DMA_STATUS__RUNNING_MASK
) |
312 (post_suspend_state
& SPR_DMA_STATUS__DONE_MASK
);
315 /* Restart a DMA that was running before we were context-switched out. */
316 static void restore_tile_dma_state(struct thread_struct
*t
)
318 const struct tile_dma_state
*dma
= &t
->tile_dma_state
;
321 * The only way to restore the done bit is to run a zero
322 * length transaction.
324 if ((dma
->status
& SPR_DMA_STATUS__DONE_MASK
) &&
325 !(__insn_mfspr(SPR_DMA_USER_STATUS
) & SPR_DMA_STATUS__DONE_MASK
)) {
326 __insn_mtspr(SPR_DMA_BYTE
, 0);
327 __insn_mtspr(SPR_DMA_CTR
, SPR_DMA_CTR__REQUEST_MASK
);
328 while (__insn_mfspr(SPR_DMA_USER_STATUS
) &
329 SPR_DMA_STATUS__BUSY_MASK
)
333 __insn_mtspr(SPR_DMA_SRC_ADDR
, dma
->src
);
334 __insn_mtspr(SPR_DMA_SRC_CHUNK_ADDR
, dma
->src_chunk
);
335 __insn_mtspr(SPR_DMA_DST_ADDR
, dma
->dest
);
336 __insn_mtspr(SPR_DMA_DST_CHUNK_ADDR
, dma
->dest_chunk
);
337 __insn_mtspr(SPR_DMA_STRIDE
, dma
->strides
);
338 __insn_mtspr(SPR_DMA_CHUNK_SIZE
, dma
->chunk_size
);
339 __insn_mtspr(SPR_DMA_BYTE
, dma
->byte
);
342 * Restart the engine if we were running and not done.
343 * Clear a pending async DMA fault that we were waiting on return
344 * to user space to execute, since we expect the DMA engine
345 * to regenerate those faults for us now. Note that we don't
346 * try to clear the TIF_ASYNC_TLB flag, since it's relatively
347 * harmless if set, and it covers both DMA and the SN processor.
349 if ((dma
->status
& DMA_STATUS_MASK
) == SPR_DMA_STATUS__RUNNING_MASK
) {
350 t
->dma_async_tlb
.fault_num
= 0;
351 __insn_mtspr(SPR_DMA_CTR
, SPR_DMA_CTR__REQUEST_MASK
);
357 static void save_arch_state(struct thread_struct
*t
)
359 #if CHIP_HAS_SPLIT_INTR_MASK()
360 t
->interrupt_mask
= __insn_mfspr(SPR_INTERRUPT_MASK_0_0
) |
361 ((u64
)__insn_mfspr(SPR_INTERRUPT_MASK_0_1
) << 32);
363 t
->interrupt_mask
= __insn_mfspr(SPR_INTERRUPT_MASK_0
);
365 t
->ex_context
[0] = __insn_mfspr(SPR_EX_CONTEXT_0_0
);
366 t
->ex_context
[1] = __insn_mfspr(SPR_EX_CONTEXT_0_1
);
367 t
->system_save
[0] = __insn_mfspr(SPR_SYSTEM_SAVE_0_0
);
368 t
->system_save
[1] = __insn_mfspr(SPR_SYSTEM_SAVE_0_1
);
369 t
->system_save
[2] = __insn_mfspr(SPR_SYSTEM_SAVE_0_2
);
370 t
->system_save
[3] = __insn_mfspr(SPR_SYSTEM_SAVE_0_3
);
371 t
->intctrl_0
= __insn_mfspr(SPR_INTCTRL_0_STATUS
);
372 #if CHIP_HAS_PROC_STATUS_SPR()
373 t
->proc_status
= __insn_mfspr(SPR_PROC_STATUS
);
375 #if !CHIP_HAS_FIXED_INTVEC_BASE()
376 t
->interrupt_vector_base
= __insn_mfspr(SPR_INTERRUPT_VECTOR_BASE_0
);
378 #if CHIP_HAS_TILE_RTF_HWM()
379 t
->tile_rtf_hwm
= __insn_mfspr(SPR_TILE_RTF_HWM
);
381 #if CHIP_HAS_DSTREAM_PF()
382 t
->dstream_pf
= __insn_mfspr(SPR_DSTREAM_PF
);
386 static void restore_arch_state(const struct thread_struct
*t
)
388 #if CHIP_HAS_SPLIT_INTR_MASK()
389 __insn_mtspr(SPR_INTERRUPT_MASK_0_0
, (u32
) t
->interrupt_mask
);
390 __insn_mtspr(SPR_INTERRUPT_MASK_0_1
, t
->interrupt_mask
>> 32);
392 __insn_mtspr(SPR_INTERRUPT_MASK_0
, t
->interrupt_mask
);
394 __insn_mtspr(SPR_EX_CONTEXT_0_0
, t
->ex_context
[0]);
395 __insn_mtspr(SPR_EX_CONTEXT_0_1
, t
->ex_context
[1]);
396 __insn_mtspr(SPR_SYSTEM_SAVE_0_0
, t
->system_save
[0]);
397 __insn_mtspr(SPR_SYSTEM_SAVE_0_1
, t
->system_save
[1]);
398 __insn_mtspr(SPR_SYSTEM_SAVE_0_2
, t
->system_save
[2]);
399 __insn_mtspr(SPR_SYSTEM_SAVE_0_3
, t
->system_save
[3]);
400 __insn_mtspr(SPR_INTCTRL_0_STATUS
, t
->intctrl_0
);
401 #if CHIP_HAS_PROC_STATUS_SPR()
402 __insn_mtspr(SPR_PROC_STATUS
, t
->proc_status
);
404 #if !CHIP_HAS_FIXED_INTVEC_BASE()
405 __insn_mtspr(SPR_INTERRUPT_VECTOR_BASE_0
, t
->interrupt_vector_base
);
407 #if CHIP_HAS_TILE_RTF_HWM()
408 __insn_mtspr(SPR_TILE_RTF_HWM
, t
->tile_rtf_hwm
);
410 #if CHIP_HAS_DSTREAM_PF()
411 __insn_mtspr(SPR_DSTREAM_PF
, t
->dstream_pf
);
416 void _prepare_arch_switch(struct task_struct
*next
)
418 #if CHIP_HAS_SN_PROC()
421 #if CHIP_HAS_TILE_DMA()
422 struct tile_dma_state
*dma
= ¤t
->thread
.tile_dma_state
;
424 save_tile_dma_state(dma
);
426 #if CHIP_HAS_SN_PROC()
428 * Suspend the static network processor if it was running.
429 * We do not suspend the fabric itself, just like we don't
430 * try to suspend the UDN.
432 snctl
= __insn_mfspr(SPR_SNCTL
);
433 current
->thread
.sn_proc_running
=
434 (snctl
& SPR_SNCTL__FRZPROC_MASK
) == 0;
435 if (current
->thread
.sn_proc_running
)
436 __insn_mtspr(SPR_SNCTL
, snctl
| SPR_SNCTL__FRZPROC_MASK
);
441 struct task_struct
*__sched
_switch_to(struct task_struct
*prev
,
442 struct task_struct
*next
)
444 /* DMA state is already saved; save off other arch state. */
445 save_arch_state(&prev
->thread
);
447 #if CHIP_HAS_TILE_DMA()
449 * Restore DMA in new task if desired.
450 * Note that it is only safe to restart here since interrupts
451 * are disabled, so we can't take any DMATLB miss or access
452 * interrupts before we have finished switching stacks.
454 if (next
->thread
.tile_dma_state
.enabled
) {
455 restore_tile_dma_state(&next
->thread
);
462 /* Restore other arch state. */
463 restore_arch_state(&next
->thread
);
465 #if CHIP_HAS_SN_PROC()
467 * Restart static network processor in the new process
468 * if it was running before.
470 if (next
->thread
.sn_proc_running
) {
471 int snctl
= __insn_mfspr(SPR_SNCTL
);
472 __insn_mtspr(SPR_SNCTL
, snctl
& ~SPR_SNCTL__FRZPROC_MASK
);
476 #ifdef CONFIG_HARDWALL
477 /* Enable or disable access to the network registers appropriately. */
478 hardwall_switch_tasks(prev
, next
);
482 * Switch kernel SP, PC, and callee-saved registers.
483 * In the context of the new task, return the old task pointer
484 * (i.e. the task that actually called __switch_to).
485 * Pass the value to use for SYSTEM_SAVE_K_0 when we reset our sp.
487 return __switch_to(prev
, next
, next_current_ksp0(next
));
491 * This routine is called on return from interrupt if any of the
492 * TIF_WORK_MASK flags are set in thread_info->flags. It is
493 * entered with interrupts disabled so we don't miss an event
494 * that modified the thread_info flags. If any flag is set, we
495 * handle it and return, and the calling assembly code will
496 * re-disable interrupts, reload the thread flags, and call back
497 * if more flags need to be handled.
499 * We return whether we need to check the thread_info flags again
500 * or not. Note that we don't clear TIF_SINGLESTEP here, so it's
501 * important that it be tested last, and then claim that we don't
502 * need to recheck the flags.
504 int do_work_pending(struct pt_regs
*regs
, u32 thread_info_flags
)
506 /* If we enter in kernel mode, do nothing and exit the caller loop. */
507 if (!user_mode(regs
))
510 /* Enable interrupts; they are disabled again on return to caller. */
513 if (thread_info_flags
& _TIF_NEED_RESCHED
) {
517 #if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
518 if (thread_info_flags
& _TIF_ASYNC_TLB
) {
519 do_async_page_fault(regs
);
523 if (thread_info_flags
& _TIF_SIGPENDING
) {
527 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
528 clear_thread_flag(TIF_NOTIFY_RESUME
);
529 tracehook_notify_resume(regs
);
532 if (thread_info_flags
& _TIF_SINGLESTEP
) {
533 single_step_once(regs
);
536 panic("work_pending: bad flags %#x\n", thread_info_flags
);
539 unsigned long get_wchan(struct task_struct
*p
)
541 struct KBacktraceIterator kbt
;
543 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
546 for (KBacktraceIterator_init(&kbt
, p
, NULL
);
547 !KBacktraceIterator_end(&kbt
);
548 KBacktraceIterator_next(&kbt
)) {
549 if (!in_sched_functions(kbt
.it
.pc
))
556 /* Flush thread state. */
557 void flush_thread(void)
563 * Free current thread data structures etc..
565 void exit_thread(void)
570 void show_regs(struct pt_regs
*regs
)
572 struct task_struct
*tsk
= validate_current();
576 show_regs_print_info(KERN_ERR
);
578 for (i
= 0; i
< 51; i
+= 3)
579 pr_err(" r%-2d: "REGFMT
" r%-2d: "REGFMT
" r%-2d: "REGFMT
"\n",
580 i
, regs
->regs
[i
], i
+1, regs
->regs
[i
+1],
581 i
+2, regs
->regs
[i
+2]);
582 pr_err(" r51: "REGFMT
" r52: "REGFMT
" tp : "REGFMT
"\n",
583 regs
->regs
[51], regs
->regs
[52], regs
->tp
);
584 pr_err(" sp : "REGFMT
" lr : "REGFMT
"\n", regs
->sp
, regs
->lr
);
586 for (i
= 0; i
< 52; i
+= 4)
587 pr_err(" r%-2d: "REGFMT
" r%-2d: "REGFMT
588 " r%-2d: "REGFMT
" r%-2d: "REGFMT
"\n",
589 i
, regs
->regs
[i
], i
+1, regs
->regs
[i
+1],
590 i
+2, regs
->regs
[i
+2], i
+3, regs
->regs
[i
+3]);
591 pr_err(" r52: "REGFMT
" tp : "REGFMT
" sp : "REGFMT
" lr : "REGFMT
"\n",
592 regs
->regs
[52], regs
->tp
, regs
->sp
, regs
->lr
);
594 pr_err(" pc : "REGFMT
" ex1: %ld faultnum: %ld\n",
595 regs
->pc
, regs
->ex1
, regs
->faultnum
);
597 dump_stack_regs(regs
);