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/kernel.h>
13 #include <linux/unistd.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/reboot.h>
17 #include <linux/elfcore.h>
19 #include <linux/tick.h>
20 #include <linux/slab.h>
21 #include <linux/mman.h>
23 #include <linux/syscalls.h>
24 #include <linux/uaccess.h>
25 #include <linux/smp.h>
26 #include <asm/core_reg.h>
27 #include <asm/user_gateway.h>
29 #include <asm/traps.h>
30 #include <asm/switch_to.h>
33 * Wait for the next interrupt and enable local interrupts
35 void arch_cpu_idle(void)
40 * Quickly jump straight into the interrupt entry point without actually
41 * triggering an interrupt. When TXSTATI gets read the processor will
42 * block until an interrupt is triggered.
44 asm volatile (/* Switch into ISTAT mode */
46 /* Enable local interrupts */
49 * We can't directly "SWAP PC, PCX", so we swap via a
50 * temporary. Essentially we do:
51 * PCX_new = 1f (the place to continue execution)
54 "ADD %0, CPC0, #(1f-.)\n\t"
57 /* Continue execution here with interrupts enabled */
60 : "r" (get_trigger_mask()));
63 #ifdef CONFIG_HOTPLUG_CPU
64 void arch_cpu_idle_dead(void)
70 void (*pm_power_off
)(void);
71 EXPORT_SYMBOL(pm_power_off
);
73 void (*soc_restart
)(char *cmd
);
74 void (*soc_halt
)(void);
76 void machine_restart(char *cmd
)
80 hard_processor_halt(HALT_OK
);
83 void machine_halt(void)
88 hard_processor_halt(HALT_OK
);
91 void machine_power_off(void)
96 hard_processor_halt(HALT_OK
);
104 void show_regs(struct pt_regs
*regs
)
107 const char *AX0_names
[] = {"A0StP", "A0FrP"};
108 const char *AX1_names
[] = {"A1GbP", "A1LbP"};
110 const char *DX0_names
[] = {
121 const char *DX1_names
[] = {
132 show_regs_print_info(KERN_INFO
);
134 pr_info(" pt_regs @ %p\n", regs
);
135 pr_info(" SaveMask = 0x%04hx\n", regs
->ctx
.SaveMask
);
136 pr_info(" Flags = 0x%04hx (%c%c%c%c)\n", regs
->ctx
.Flags
,
137 regs
->ctx
.Flags
& FLAG_Z
? 'Z' : 'z',
138 regs
->ctx
.Flags
& FLAG_N
? 'N' : 'n',
139 regs
->ctx
.Flags
& FLAG_O
? 'O' : 'o',
140 regs
->ctx
.Flags
& FLAG_C
? 'C' : 'c');
141 pr_info(" TXRPT = 0x%08x\n", regs
->ctx
.CurrRPT
);
142 pr_info(" PC = 0x%08x\n", regs
->ctx
.CurrPC
);
145 for (i
= 0; i
< 2; i
++) {
146 pr_info(" %s = 0x%08x ",
149 printk(" %s = 0x%08x\n",
154 if (regs
->ctx
.SaveMask
& TBICTX_XEXT_BIT
)
155 pr_warn(" Extended state present - AX2.[01] will be WRONG\n");
157 /* Special place with AXx.2 */
158 pr_info(" A0.2 = 0x%08x ",
159 regs
->ctx
.Ext
.AX2
.U0
);
160 printk(" A1.2 = 0x%08x\n",
161 regs
->ctx
.Ext
.AX2
.U1
);
163 /* 'extended' AX regs (nominally, just AXx.3) */
164 for (i
= 0; i
< (TBICTX_AX_REGS
- 3); i
++) {
165 pr_info(" A0.%d = 0x%08x ", i
+ 3, regs
->ctx
.AX3
[i
].U0
);
166 printk(" A1.%d = 0x%08x\n", i
+ 3, regs
->ctx
.AX3
[i
].U1
);
169 for (i
= 0; i
< 8; i
++) {
170 pr_info(" %s = 0x%08x ", DX0_names
[i
], regs
->ctx
.DX
[i
].U0
);
171 printk(" %s = 0x%08x\n", DX1_names
[i
], regs
->ctx
.DX
[i
].U1
);
174 show_trace(NULL
, (unsigned long *)regs
->ctx
.AX
[0].U0
, regs
);
178 * Copy architecture-specific thread state
180 int copy_thread(unsigned long clone_flags
, unsigned long usp
,
181 unsigned long kthread_arg
, struct task_struct
*tsk
)
183 struct pt_regs
*childregs
= task_pt_regs(tsk
);
184 void *kernel_context
= ((void *) childregs
+
185 sizeof(struct pt_regs
));
186 unsigned long global_base
;
188 BUG_ON(((unsigned long)childregs
) & 0x7);
189 BUG_ON(((unsigned long)kernel_context
) & 0x7);
191 memset(&tsk
->thread
.kernel_context
, 0,
192 sizeof(tsk
->thread
.kernel_context
));
194 tsk
->thread
.kernel_context
= __TBISwitchInit(kernel_context
,
198 if (unlikely(tsk
->flags
& PF_KTHREAD
)) {
200 * Make sure we don't leak any kernel data to child's regs
201 * if kernel thread becomes a userspace thread in the future
203 memset(childregs
, 0 , sizeof(struct pt_regs
));
205 global_base
= __core_reg_get(A1GbP
);
206 childregs
->ctx
.AX
[0].U1
= (unsigned long) global_base
;
207 childregs
->ctx
.AX
[0].U0
= (unsigned long) kernel_context
;
208 /* Set D1Ar1=kthread_arg and D1RtP=usp (fn) */
209 childregs
->ctx
.DX
[4].U1
= usp
;
210 childregs
->ctx
.DX
[3].U1
= kthread_arg
;
211 tsk
->thread
.int_depth
= 2;
216 * Get a pointer to where the new child's register block should have
218 * The Meta's stack grows upwards, and the context is the the first
219 * thing to be pushed by TBX (phew)
221 *childregs
= *current_pt_regs();
222 /* Set the correct stack for the clone mode */
224 childregs
->ctx
.AX
[0].U0
= ALIGN(usp
, 8);
225 tsk
->thread
.int_depth
= 1;
227 /* set return value for child process */
228 childregs
->ctx
.DX
[0].U0
= 0;
230 /* The TLS pointer is passed as an argument to sys_clone. */
231 if (clone_flags
& CLONE_SETTLS
)
232 tsk
->thread
.tls_ptr
=
233 (__force
void __user
*)childregs
->ctx
.DX
[1].U1
;
235 #ifdef CONFIG_METAG_FPU
236 if (tsk
->thread
.fpu_context
) {
237 struct meta_fpu_context
*ctx
;
239 ctx
= kmemdup(tsk
->thread
.fpu_context
,
240 sizeof(struct meta_fpu_context
), GFP_ATOMIC
);
241 tsk
->thread
.fpu_context
= ctx
;
245 #ifdef CONFIG_METAG_DSP
246 if (tsk
->thread
.dsp_context
) {
247 struct meta_ext_context
*ctx
;
250 ctx
= kmemdup(tsk
->thread
.dsp_context
,
251 sizeof(struct meta_ext_context
), GFP_ATOMIC
);
252 for (i
= 0; i
< 2; i
++)
253 ctx
->ram
[i
] = kmemdup(ctx
->ram
[i
], ctx
->ram_sz
[i
],
255 tsk
->thread
.dsp_context
= ctx
;
262 #ifdef CONFIG_METAG_FPU
263 static void alloc_fpu_context(struct thread_struct
*thread
)
265 thread
->fpu_context
= kzalloc(sizeof(struct meta_fpu_context
),
269 static void clear_fpu(struct thread_struct
*thread
)
271 thread
->user_flags
&= ~TBICTX_FPAC_BIT
;
272 kfree(thread
->fpu_context
);
273 thread
->fpu_context
= NULL
;
276 static void clear_fpu(struct thread_struct
*thread
)
281 #ifdef CONFIG_METAG_DSP
282 static void clear_dsp(struct thread_struct
*thread
)
284 if (thread
->dsp_context
) {
285 kfree(thread
->dsp_context
->ram
[0]);
286 kfree(thread
->dsp_context
->ram
[1]);
288 kfree(thread
->dsp_context
);
290 thread
->dsp_context
= NULL
;
293 __core_reg_set(D0
.8
, 0);
296 static void clear_dsp(struct thread_struct
*thread
)
301 struct task_struct
*__sched
__switch_to(struct task_struct
*prev
,
302 struct task_struct
*next
)
306 to
.Switch
.pCtx
= next
->thread
.kernel_context
;
307 to
.Switch
.pPara
= prev
;
309 #ifdef CONFIG_METAG_FPU
310 if (prev
->thread
.user_flags
& TBICTX_FPAC_BIT
) {
311 struct pt_regs
*regs
= task_pt_regs(prev
);
314 state
.Sig
.SaveMask
= prev
->thread
.user_flags
;
315 state
.Sig
.pCtx
= ®s
->ctx
;
317 if (!prev
->thread
.fpu_context
)
318 alloc_fpu_context(&prev
->thread
);
319 if (prev
->thread
.fpu_context
)
320 __TBICtxFPUSave(state
, prev
->thread
.fpu_context
);
323 * Force a restore of the FPU context next time this process is
326 if (prev
->thread
.fpu_context
)
327 prev
->thread
.fpu_context
->needs_restore
= true;
331 from
= __TBISwitch(to
, &prev
->thread
.kernel_context
);
333 /* Restore TLS pointer for this process. */
334 set_gateway_tls(current
->thread
.tls_ptr
);
336 return (struct task_struct
*) from
.Switch
.pPara
;
339 void flush_thread(void)
341 clear_fpu(¤t
->thread
);
342 clear_dsp(¤t
->thread
);
346 * Free current thread data structures etc.
348 void exit_thread(void)
350 clear_fpu(¤t
->thread
);
351 clear_dsp(¤t
->thread
);
354 /* TODO: figure out how to unwind the kernel stack here to figure out
355 * where we went to sleep. */
356 unsigned long get_wchan(struct task_struct
*p
)
361 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
363 /* Returning 0 indicates that the FPU state was not stored (as it was
368 #ifdef CONFIG_METAG_USER_TCM
370 #define ELF_MIN_ALIGN PAGE_SIZE
372 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
373 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
374 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
376 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
378 unsigned long __metag_elf_map(struct file
*filep
, unsigned long addr
,
379 struct elf_phdr
*eppnt
, int prot
, int type
,
380 unsigned long total_size
)
382 unsigned long map_addr
, size
;
383 unsigned long page_off
= ELF_PAGEOFFSET(eppnt
->p_vaddr
);
384 unsigned long raw_size
= eppnt
->p_filesz
+ page_off
;
385 unsigned long off
= eppnt
->p_offset
- page_off
;
386 unsigned int tcm_tag
;
387 addr
= ELF_PAGESTART(addr
);
388 size
= ELF_PAGEALIGN(raw_size
);
390 /* mmap() will return -EINVAL if given a zero size, but a
391 * segment with zero filesize is perfectly valid */
395 tcm_tag
= tcm_lookup_tag(addr
);
397 if (tcm_tag
!= TCM_INVALID_TAG
)
401 * total_size is the size of the ELF (interpreter) image.
402 * The _first_ mmap needs to know the full size, otherwise
403 * randomization might put this image into an overlapping
404 * position with the ELF binary image. (since size < total_size)
405 * So we first map the 'big' image - and unmap the remainder at
406 * the end. (which unmap is needed for ELF images with holes.)
409 total_size
= ELF_PAGEALIGN(total_size
);
410 map_addr
= vm_mmap(filep
, addr
, total_size
, prot
, type
, off
);
411 if (!BAD_ADDR(map_addr
))
412 vm_munmap(map_addr
+size
, total_size
-size
);
414 map_addr
= vm_mmap(filep
, addr
, size
, prot
, type
, off
);
416 if (!BAD_ADDR(map_addr
) && tcm_tag
!= TCM_INVALID_TAG
) {
417 struct tcm_allocation
*tcm
;
418 unsigned long tcm_addr
;
420 tcm
= kmalloc(sizeof(*tcm
), GFP_KERNEL
);
424 tcm_addr
= tcm_alloc(tcm_tag
, raw_size
);
425 if (tcm_addr
!= addr
) {
431 tcm
->addr
= tcm_addr
;
432 tcm
->size
= raw_size
;
434 list_add(&tcm
->list
, ¤t
->mm
->context
.tcm
);
436 eppnt
->p_vaddr
= map_addr
;
437 if (copy_from_user((void *) addr
, (void __user
*) map_addr
,