]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/metag/kernel/process.c
2 * Copyright (C) 2005,2006,2007,2008,2009,2010,2011 Imagination Technologies
4 * This file contains the architecture-dependent parts of process handling.
8 #include <linux/errno.h>
9 #include <linux/export.h>
10 #include <linux/sched.h>
11 #include <linux/sched/debug.h>
12 #include <linux/sched/task.h>
13 #include <linux/kernel.h>
15 #include <linux/unistd.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/reboot.h>
19 #include <linux/elfcore.h>
21 #include <linux/tick.h>
22 #include <linux/slab.h>
23 #include <linux/mman.h>
25 #include <linux/syscalls.h>
26 #include <linux/uaccess.h>
27 #include <linux/smp.h>
28 #include <asm/core_reg.h>
29 #include <asm/user_gateway.h>
31 #include <asm/traps.h>
32 #include <asm/switch_to.h>
35 * Wait for the next interrupt and enable local interrupts
37 void arch_cpu_idle(void)
42 * Quickly jump straight into the interrupt entry point without actually
43 * triggering an interrupt. When TXSTATI gets read the processor will
44 * block until an interrupt is triggered.
46 asm volatile (/* Switch into ISTAT mode */
48 /* Enable local interrupts */
51 * We can't directly "SWAP PC, PCX", so we swap via a
52 * temporary. Essentially we do:
53 * PCX_new = 1f (the place to continue execution)
56 "ADD %0, CPC0, #(1f-.)\n\t"
59 /* Continue execution here with interrupts enabled */
62 : "r" (get_trigger_mask()));
65 #ifdef CONFIG_HOTPLUG_CPU
66 void arch_cpu_idle_dead(void)
72 void (*pm_power_off
)(void);
73 EXPORT_SYMBOL(pm_power_off
);
75 void (*soc_restart
)(char *cmd
);
76 void (*soc_halt
)(void);
78 void machine_restart(char *cmd
)
82 hard_processor_halt(HALT_OK
);
85 void machine_halt(void)
90 hard_processor_halt(HALT_OK
);
93 void machine_power_off(void)
98 hard_processor_halt(HALT_OK
);
106 void show_regs(struct pt_regs
*regs
)
109 const char *AX0_names
[] = {"A0StP", "A0FrP"};
110 const char *AX1_names
[] = {"A1GbP", "A1LbP"};
112 const char *DX0_names
[] = {
123 const char *DX1_names
[] = {
134 show_regs_print_info(KERN_INFO
);
136 pr_info(" pt_regs @ %p\n", regs
);
137 pr_info(" SaveMask = 0x%04hx\n", regs
->ctx
.SaveMask
);
138 pr_info(" Flags = 0x%04hx (%c%c%c%c)\n", regs
->ctx
.Flags
,
139 regs
->ctx
.Flags
& FLAG_Z
? 'Z' : 'z',
140 regs
->ctx
.Flags
& FLAG_N
? 'N' : 'n',
141 regs
->ctx
.Flags
& FLAG_O
? 'O' : 'o',
142 regs
->ctx
.Flags
& FLAG_C
? 'C' : 'c');
143 pr_info(" TXRPT = 0x%08x\n", regs
->ctx
.CurrRPT
);
144 pr_info(" PC = 0x%08x\n", regs
->ctx
.CurrPC
);
147 for (i
= 0; i
< 2; i
++) {
148 pr_info(" %s = 0x%08x ",
151 printk(" %s = 0x%08x\n",
156 if (regs
->ctx
.SaveMask
& TBICTX_XEXT_BIT
)
157 pr_warn(" Extended state present - AX2.[01] will be WRONG\n");
159 /* Special place with AXx.2 */
160 pr_info(" A0.2 = 0x%08x ",
161 regs
->ctx
.Ext
.AX2
.U0
);
162 printk(" A1.2 = 0x%08x\n",
163 regs
->ctx
.Ext
.AX2
.U1
);
165 /* 'extended' AX regs (nominally, just AXx.3) */
166 for (i
= 0; i
< (TBICTX_AX_REGS
- 3); i
++) {
167 pr_info(" A0.%d = 0x%08x ", i
+ 3, regs
->ctx
.AX3
[i
].U0
);
168 printk(" A1.%d = 0x%08x\n", i
+ 3, regs
->ctx
.AX3
[i
].U1
);
171 for (i
= 0; i
< 8; i
++) {
172 pr_info(" %s = 0x%08x ", DX0_names
[i
], regs
->ctx
.DX
[i
].U0
);
173 printk(" %s = 0x%08x\n", DX1_names
[i
], regs
->ctx
.DX
[i
].U1
);
176 show_trace(NULL
, (unsigned long *)regs
->ctx
.AX
[0].U0
, regs
);
180 * Copy architecture-specific thread state
182 int copy_thread(unsigned long clone_flags
, unsigned long usp
,
183 unsigned long kthread_arg
, struct task_struct
*tsk
)
185 struct pt_regs
*childregs
= task_pt_regs(tsk
);
186 void *kernel_context
= ((void *) childregs
+
187 sizeof(struct pt_regs
));
188 unsigned long global_base
;
190 BUG_ON(((unsigned long)childregs
) & 0x7);
191 BUG_ON(((unsigned long)kernel_context
) & 0x7);
193 memset(&tsk
->thread
.kernel_context
, 0,
194 sizeof(tsk
->thread
.kernel_context
));
196 tsk
->thread
.kernel_context
= __TBISwitchInit(kernel_context
,
200 if (unlikely(tsk
->flags
& PF_KTHREAD
)) {
202 * Make sure we don't leak any kernel data to child's regs
203 * if kernel thread becomes a userspace thread in the future
205 memset(childregs
, 0 , sizeof(struct pt_regs
));
207 global_base
= __core_reg_get(A1GbP
);
208 childregs
->ctx
.AX
[0].U1
= (unsigned long) global_base
;
209 childregs
->ctx
.AX
[0].U0
= (unsigned long) kernel_context
;
210 /* Set D1Ar1=kthread_arg and D1RtP=usp (fn) */
211 childregs
->ctx
.DX
[4].U1
= usp
;
212 childregs
->ctx
.DX
[3].U1
= kthread_arg
;
213 tsk
->thread
.int_depth
= 2;
218 * Get a pointer to where the new child's register block should have
220 * The Meta's stack grows upwards, and the context is the the first
221 * thing to be pushed by TBX (phew)
223 *childregs
= *current_pt_regs();
224 /* Set the correct stack for the clone mode */
226 childregs
->ctx
.AX
[0].U0
= ALIGN(usp
, 8);
227 tsk
->thread
.int_depth
= 1;
229 /* set return value for child process */
230 childregs
->ctx
.DX
[0].U0
= 0;
232 /* The TLS pointer is passed as an argument to sys_clone. */
233 if (clone_flags
& CLONE_SETTLS
)
234 tsk
->thread
.tls_ptr
=
235 (__force
void __user
*)childregs
->ctx
.DX
[1].U1
;
237 #ifdef CONFIG_METAG_FPU
238 if (tsk
->thread
.fpu_context
) {
239 struct meta_fpu_context
*ctx
;
241 ctx
= kmemdup(tsk
->thread
.fpu_context
,
242 sizeof(struct meta_fpu_context
), GFP_ATOMIC
);
243 tsk
->thread
.fpu_context
= ctx
;
247 #ifdef CONFIG_METAG_DSP
248 if (tsk
->thread
.dsp_context
) {
249 struct meta_ext_context
*ctx
;
252 ctx
= kmemdup(tsk
->thread
.dsp_context
,
253 sizeof(struct meta_ext_context
), GFP_ATOMIC
);
254 for (i
= 0; i
< 2; i
++)
255 ctx
->ram
[i
] = kmemdup(ctx
->ram
[i
], ctx
->ram_sz
[i
],
257 tsk
->thread
.dsp_context
= ctx
;
264 #ifdef CONFIG_METAG_FPU
265 static void alloc_fpu_context(struct thread_struct
*thread
)
267 thread
->fpu_context
= kzalloc(sizeof(struct meta_fpu_context
),
271 static void clear_fpu(struct thread_struct
*thread
)
273 thread
->user_flags
&= ~TBICTX_FPAC_BIT
;
274 kfree(thread
->fpu_context
);
275 thread
->fpu_context
= NULL
;
278 static void clear_fpu(struct thread_struct
*thread
)
283 #ifdef CONFIG_METAG_DSP
284 static void clear_dsp(struct thread_struct
*thread
)
286 if (thread
->dsp_context
) {
287 kfree(thread
->dsp_context
->ram
[0]);
288 kfree(thread
->dsp_context
->ram
[1]);
290 kfree(thread
->dsp_context
);
292 thread
->dsp_context
= NULL
;
295 __core_reg_set(D0
.8
, 0);
298 static void clear_dsp(struct thread_struct
*thread
)
303 struct task_struct
*__sched
__switch_to(struct task_struct
*prev
,
304 struct task_struct
*next
)
308 to
.Switch
.pCtx
= next
->thread
.kernel_context
;
309 to
.Switch
.pPara
= prev
;
311 #ifdef CONFIG_METAG_FPU
312 if (prev
->thread
.user_flags
& TBICTX_FPAC_BIT
) {
313 struct pt_regs
*regs
= task_pt_regs(prev
);
316 state
.Sig
.SaveMask
= prev
->thread
.user_flags
;
317 state
.Sig
.pCtx
= ®s
->ctx
;
319 if (!prev
->thread
.fpu_context
)
320 alloc_fpu_context(&prev
->thread
);
321 if (prev
->thread
.fpu_context
)
322 __TBICtxFPUSave(state
, prev
->thread
.fpu_context
);
325 * Force a restore of the FPU context next time this process is
328 if (prev
->thread
.fpu_context
)
329 prev
->thread
.fpu_context
->needs_restore
= true;
333 from
= __TBISwitch(to
, &prev
->thread
.kernel_context
);
335 /* Restore TLS pointer for this process. */
336 set_gateway_tls(current
->thread
.tls_ptr
);
338 return (struct task_struct
*) from
.Switch
.pPara
;
341 void flush_thread(void)
343 clear_fpu(¤t
->thread
);
344 clear_dsp(¤t
->thread
);
348 * Free current thread data structures etc.
350 void exit_thread(struct task_struct
*tsk
)
352 clear_fpu(&tsk
->thread
);
353 clear_dsp(&tsk
->thread
);
356 /* TODO: figure out how to unwind the kernel stack here to figure out
357 * where we went to sleep. */
358 unsigned long get_wchan(struct task_struct
*p
)
363 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
365 /* Returning 0 indicates that the FPU state was not stored (as it was
370 #ifdef CONFIG_METAG_USER_TCM
372 #define ELF_MIN_ALIGN PAGE_SIZE
374 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
375 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
376 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
378 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
380 unsigned long __metag_elf_map(struct file
*filep
, unsigned long addr
,
381 struct elf_phdr
*eppnt
, int prot
, int type
,
382 unsigned long total_size
)
384 unsigned long map_addr
, size
;
385 unsigned long page_off
= ELF_PAGEOFFSET(eppnt
->p_vaddr
);
386 unsigned long raw_size
= eppnt
->p_filesz
+ page_off
;
387 unsigned long off
= eppnt
->p_offset
- page_off
;
388 unsigned int tcm_tag
;
389 addr
= ELF_PAGESTART(addr
);
390 size
= ELF_PAGEALIGN(raw_size
);
392 /* mmap() will return -EINVAL if given a zero size, but a
393 * segment with zero filesize is perfectly valid */
397 tcm_tag
= tcm_lookup_tag(addr
);
399 if (tcm_tag
!= TCM_INVALID_TAG
)
403 * total_size is the size of the ELF (interpreter) image.
404 * The _first_ mmap needs to know the full size, otherwise
405 * randomization might put this image into an overlapping
406 * position with the ELF binary image. (since size < total_size)
407 * So we first map the 'big' image - and unmap the remainder at
408 * the end. (which unmap is needed for ELF images with holes.)
411 total_size
= ELF_PAGEALIGN(total_size
);
412 map_addr
= vm_mmap(filep
, addr
, total_size
, prot
, type
, off
);
413 if (!BAD_ADDR(map_addr
))
414 vm_munmap(map_addr
+size
, total_size
-size
);
416 map_addr
= vm_mmap(filep
, addr
, size
, prot
, type
, off
);
418 if (!BAD_ADDR(map_addr
) && tcm_tag
!= TCM_INVALID_TAG
) {
419 struct tcm_allocation
*tcm
;
420 unsigned long tcm_addr
;
422 tcm
= kmalloc(sizeof(*tcm
), GFP_KERNEL
);
426 tcm_addr
= tcm_alloc(tcm_tag
, raw_size
);
427 if (tcm_addr
!= addr
) {
433 tcm
->addr
= tcm_addr
;
434 tcm
->size
= raw_size
;
436 list_add(&tcm
->list
, ¤t
->mm
->context
.tcm
);
438 eppnt
->p_vaddr
= map_addr
;
439 if (copy_from_user((void *) addr
, (void __user
*) map_addr
,