process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008,2009,2010,2011 Imagination Technologies
  3. *
  4. * This file contains the architecture-dependent parts of process handling.
  5. *
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/export.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/unistd.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/user.h>
  15. #include <linux/reboot.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/fs.h>
  18. #include <linux/tick.h>
  19. #include <linux/slab.h>
  20. #include <linux/mman.h>
  21. #include <linux/pm.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/smp.h>
  25. #include <asm/core_reg.h>
  26. #include <asm/user_gateway.h>
  27. #include <asm/tcm.h>
  28. #include <asm/traps.h>
  29. #include <asm/switch_to.h>
  30. /*
  31. * Wait for the next interrupt and enable local interrupts
  32. */
  33. void arch_cpu_idle(void)
  34. {
  35. int tmp;
  36. /*
  37. * Quickly jump straight into the interrupt entry point without actually
  38. * triggering an interrupt. When TXSTATI gets read the processor will
  39. * block until an interrupt is triggered.
  40. */
  41. asm volatile (/* Switch into ISTAT mode */
  42. "RTH\n\t"
  43. /* Enable local interrupts */
  44. "MOV TXMASKI, %1\n\t"
  45. /*
  46. * We can't directly "SWAP PC, PCX", so we swap via a
  47. * temporary. Essentially we do:
  48. * PCX_new = 1f (the place to continue execution)
  49. * PC = PCX_old
  50. */
  51. "ADD %0, CPC0, #(1f-.)\n\t"
  52. "SWAP PCX, %0\n\t"
  53. "MOV PC, %0\n"
  54. /* Continue execution here with interrupts enabled */
  55. "1:"
  56. : "=a" (tmp)
  57. : "r" (get_trigger_mask()));
  58. }
  59. #ifdef CONFIG_HOTPLUG_CPU
  60. void arch_cpu_idle_dead(void)
  61. {
  62. cpu_die();
  63. }
  64. #endif
  65. void (*pm_power_off)(void);
  66. EXPORT_SYMBOL(pm_power_off);
  67. void (*soc_restart)(char *cmd);
  68. void (*soc_halt)(void);
  69. void machine_restart(char *cmd)
  70. {
  71. if (soc_restart)
  72. soc_restart(cmd);
  73. hard_processor_halt(HALT_OK);
  74. }
  75. void machine_halt(void)
  76. {
  77. if (soc_halt)
  78. soc_halt();
  79. smp_send_stop();
  80. hard_processor_halt(HALT_OK);
  81. }
  82. void machine_power_off(void)
  83. {
  84. if (pm_power_off)
  85. pm_power_off();
  86. smp_send_stop();
  87. hard_processor_halt(HALT_OK);
  88. }
  89. #define FLAG_Z 0x8
  90. #define FLAG_N 0x4
  91. #define FLAG_O 0x2
  92. #define FLAG_C 0x1
  93. void show_regs(struct pt_regs *regs)
  94. {
  95. int i;
  96. const char *AX0_names[] = {"A0StP", "A0FrP"};
  97. const char *AX1_names[] = {"A1GbP", "A1LbP"};
  98. const char *DX0_names[] = {
  99. "D0Re0",
  100. "D0Ar6",
  101. "D0Ar4",
  102. "D0Ar2",
  103. "D0FrT",
  104. "D0.5 ",
  105. "D0.6 ",
  106. "D0.7 "
  107. };
  108. const char *DX1_names[] = {
  109. "D1Re0",
  110. "D1Ar5",
  111. "D1Ar3",
  112. "D1Ar1",
  113. "D1RtP",
  114. "D1.5 ",
  115. "D1.6 ",
  116. "D1.7 "
  117. };
  118. show_regs_print_info(KERN_INFO);
  119. pr_info(" pt_regs @ %p\n", regs);
  120. pr_info(" SaveMask = 0x%04hx\n", regs->ctx.SaveMask);
  121. pr_info(" Flags = 0x%04hx (%c%c%c%c)\n", regs->ctx.Flags,
  122. regs->ctx.Flags & FLAG_Z ? 'Z' : 'z',
  123. regs->ctx.Flags & FLAG_N ? 'N' : 'n',
  124. regs->ctx.Flags & FLAG_O ? 'O' : 'o',
  125. regs->ctx.Flags & FLAG_C ? 'C' : 'c');
  126. pr_info(" TXRPT = 0x%08x\n", regs->ctx.CurrRPT);
  127. pr_info(" PC = 0x%08x\n", regs->ctx.CurrPC);
  128. /* AX regs */
  129. for (i = 0; i < 2; i++) {
  130. pr_info(" %s = 0x%08x ",
  131. AX0_names[i],
  132. regs->ctx.AX[i].U0);
  133. printk(" %s = 0x%08x\n",
  134. AX1_names[i],
  135. regs->ctx.AX[i].U1);
  136. }
  137. if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
  138. pr_warn(" Extended state present - AX2.[01] will be WRONG\n");
  139. /* Special place with AXx.2 */
  140. pr_info(" A0.2 = 0x%08x ",
  141. regs->ctx.Ext.AX2.U0);
  142. printk(" A1.2 = 0x%08x\n",
  143. regs->ctx.Ext.AX2.U1);
  144. /* 'extended' AX regs (nominally, just AXx.3) */
  145. for (i = 0; i < (TBICTX_AX_REGS - 3); i++) {
  146. pr_info(" A0.%d = 0x%08x ", i + 3, regs->ctx.AX3[i].U0);
  147. printk(" A1.%d = 0x%08x\n", i + 3, regs->ctx.AX3[i].U1);
  148. }
  149. for (i = 0; i < 8; i++) {
  150. pr_info(" %s = 0x%08x ", DX0_names[i], regs->ctx.DX[i].U0);
  151. printk(" %s = 0x%08x\n", DX1_names[i], regs->ctx.DX[i].U1);
  152. }
  153. show_trace(NULL, (unsigned long *)regs->ctx.AX[0].U0, regs);
  154. }
  155. /*
  156. * Copy architecture-specific thread state
  157. */
  158. int copy_thread(unsigned long clone_flags, unsigned long usp,
  159. unsigned long kthread_arg, struct task_struct *tsk)
  160. {
  161. struct pt_regs *childregs = task_pt_regs(tsk);
  162. void *kernel_context = ((void *) childregs +
  163. sizeof(struct pt_regs));
  164. unsigned long global_base;
  165. BUG_ON(((unsigned long)childregs) & 0x7);
  166. BUG_ON(((unsigned long)kernel_context) & 0x7);
  167. memset(&tsk->thread.kernel_context, 0,
  168. sizeof(tsk->thread.kernel_context));
  169. tsk->thread.kernel_context = __TBISwitchInit(kernel_context,
  170. ret_from_fork,
  171. 0, 0);
  172. if (unlikely(tsk->flags & PF_KTHREAD)) {
  173. /*
  174. * Make sure we don't leak any kernel data to child's regs
  175. * if kernel thread becomes a userspace thread in the future
  176. */
  177. memset(childregs, 0 , sizeof(struct pt_regs));
  178. global_base = __core_reg_get(A1GbP);
  179. childregs->ctx.AX[0].U1 = (unsigned long) global_base;
  180. childregs->ctx.AX[0].U0 = (unsigned long) kernel_context;
  181. /* Set D1Ar1=kthread_arg and D1RtP=usp (fn) */
  182. childregs->ctx.DX[4].U1 = usp;
  183. childregs->ctx.DX[3].U1 = kthread_arg;
  184. tsk->thread.int_depth = 2;
  185. return 0;
  186. }
  187. /*
  188. * Get a pointer to where the new child's register block should have
  189. * been pushed.
  190. * The Meta's stack grows upwards, and the context is the the first
  191. * thing to be pushed by TBX (phew)
  192. */
  193. *childregs = *current_pt_regs();
  194. /* Set the correct stack for the clone mode */
  195. if (usp)
  196. childregs->ctx.AX[0].U0 = ALIGN(usp, 8);
  197. tsk->thread.int_depth = 1;
  198. /* set return value for child process */
  199. childregs->ctx.DX[0].U0 = 0;
  200. /* The TLS pointer is passed as an argument to sys_clone. */
  201. if (clone_flags & CLONE_SETTLS)
  202. tsk->thread.tls_ptr =
  203. (__force void __user *)childregs->ctx.DX[1].U1;
  204. #ifdef CONFIG_METAG_FPU
  205. if (tsk->thread.fpu_context) {
  206. struct meta_fpu_context *ctx;
  207. ctx = kmemdup(tsk->thread.fpu_context,
  208. sizeof(struct meta_fpu_context), GFP_ATOMIC);
  209. tsk->thread.fpu_context = ctx;
  210. }
  211. #endif
  212. #ifdef CONFIG_METAG_DSP
  213. if (tsk->thread.dsp_context) {
  214. struct meta_ext_context *ctx;
  215. int i;
  216. ctx = kmemdup(tsk->thread.dsp_context,
  217. sizeof(struct meta_ext_context), GFP_ATOMIC);
  218. for (i = 0; i < 2; i++)
  219. ctx->ram[i] = kmemdup(ctx->ram[i], ctx->ram_sz[i],
  220. GFP_ATOMIC);
  221. tsk->thread.dsp_context = ctx;
  222. }
  223. #endif
  224. return 0;
  225. }
  226. #ifdef CONFIG_METAG_FPU
  227. static void alloc_fpu_context(struct thread_struct *thread)
  228. {
  229. thread->fpu_context = kzalloc(sizeof(struct meta_fpu_context),
  230. GFP_ATOMIC);
  231. }
  232. static void clear_fpu(struct thread_struct *thread)
  233. {
  234. thread->user_flags &= ~TBICTX_FPAC_BIT;
  235. kfree(thread->fpu_context);
  236. thread->fpu_context = NULL;
  237. }
  238. #else
  239. static void clear_fpu(struct thread_struct *thread)
  240. {
  241. }
  242. #endif
  243. #ifdef CONFIG_METAG_DSP
  244. static void clear_dsp(struct thread_struct *thread)
  245. {
  246. if (thread->dsp_context) {
  247. kfree(thread->dsp_context->ram[0]);
  248. kfree(thread->dsp_context->ram[1]);
  249. kfree(thread->dsp_context);
  250. thread->dsp_context = NULL;
  251. }
  252. __core_reg_set(D0.8, 0);
  253. }
  254. #else
  255. static void clear_dsp(struct thread_struct *thread)
  256. {
  257. }
  258. #endif
  259. struct task_struct *__sched __switch_to(struct task_struct *prev,
  260. struct task_struct *next)
  261. {
  262. TBIRES to, from;
  263. to.Switch.pCtx = next->thread.kernel_context;
  264. to.Switch.pPara = prev;
  265. #ifdef CONFIG_METAG_FPU
  266. if (prev->thread.user_flags & TBICTX_FPAC_BIT) {
  267. struct pt_regs *regs = task_pt_regs(prev);
  268. TBIRES state;
  269. state.Sig.SaveMask = prev->thread.user_flags;
  270. state.Sig.pCtx = &regs->ctx;
  271. if (!prev->thread.fpu_context)
  272. alloc_fpu_context(&prev->thread);
  273. if (prev->thread.fpu_context)
  274. __TBICtxFPUSave(state, prev->thread.fpu_context);
  275. }
  276. /*
  277. * Force a restore of the FPU context next time this process is
  278. * scheduled.
  279. */
  280. if (prev->thread.fpu_context)
  281. prev->thread.fpu_context->needs_restore = true;
  282. #endif
  283. from = __TBISwitch(to, &prev->thread.kernel_context);
  284. /* Restore TLS pointer for this process. */
  285. set_gateway_tls(current->thread.tls_ptr);
  286. return (struct task_struct *) from.Switch.pPara;
  287. }
  288. void flush_thread(void)
  289. {
  290. clear_fpu(&current->thread);
  291. clear_dsp(&current->thread);
  292. }
  293. /*
  294. * Free current thread data structures etc.
  295. */
  296. void exit_thread(void)
  297. {
  298. clear_fpu(&current->thread);
  299. clear_dsp(&current->thread);
  300. }
  301. /* TODO: figure out how to unwind the kernel stack here to figure out
  302. * where we went to sleep. */
  303. unsigned long get_wchan(struct task_struct *p)
  304. {
  305. return 0;
  306. }
  307. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  308. {
  309. /* Returning 0 indicates that the FPU state was not stored (as it was
  310. * not in use) */
  311. return 0;
  312. }
  313. #ifdef CONFIG_METAG_USER_TCM
  314. #define ELF_MIN_ALIGN PAGE_SIZE
  315. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  316. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  317. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  318. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  319. unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
  320. struct elf_phdr *eppnt, int prot, int type,
  321. unsigned long total_size)
  322. {
  323. unsigned long map_addr, size;
  324. unsigned long page_off = ELF_PAGEOFFSET(eppnt->p_vaddr);
  325. unsigned long raw_size = eppnt->p_filesz + page_off;
  326. unsigned long off = eppnt->p_offset - page_off;
  327. unsigned int tcm_tag;
  328. addr = ELF_PAGESTART(addr);
  329. size = ELF_PAGEALIGN(raw_size);
  330. /* mmap() will return -EINVAL if given a zero size, but a
  331. * segment with zero filesize is perfectly valid */
  332. if (!size)
  333. return addr;
  334. tcm_tag = tcm_lookup_tag(addr);
  335. if (tcm_tag != TCM_INVALID_TAG)
  336. type &= ~MAP_FIXED;
  337. /*
  338. * total_size is the size of the ELF (interpreter) image.
  339. * The _first_ mmap needs to know the full size, otherwise
  340. * randomization might put this image into an overlapping
  341. * position with the ELF binary image. (since size < total_size)
  342. * So we first map the 'big' image - and unmap the remainder at
  343. * the end. (which unmap is needed for ELF images with holes.)
  344. */
  345. if (total_size) {
  346. total_size = ELF_PAGEALIGN(total_size);
  347. map_addr = vm_mmap(filep, addr, total_size, prot, type, off);
  348. if (!BAD_ADDR(map_addr))
  349. vm_munmap(map_addr+size, total_size-size);
  350. } else
  351. map_addr = vm_mmap(filep, addr, size, prot, type, off);
  352. if (!BAD_ADDR(map_addr) && tcm_tag != TCM_INVALID_TAG) {
  353. struct tcm_allocation *tcm;
  354. unsigned long tcm_addr;
  355. tcm = kmalloc(sizeof(*tcm), GFP_KERNEL);
  356. if (!tcm)
  357. return -ENOMEM;
  358. tcm_addr = tcm_alloc(tcm_tag, raw_size);
  359. if (tcm_addr != addr) {
  360. kfree(tcm);
  361. return -ENOMEM;
  362. }
  363. tcm->tag = tcm_tag;
  364. tcm->addr = tcm_addr;
  365. tcm->size = raw_size;
  366. list_add(&tcm->list, &current->mm->context.tcm);
  367. eppnt->p_vaddr = map_addr;
  368. if (copy_from_user((void *) addr, (void __user *) map_addr,
  369. raw_size))
  370. return -EFAULT;
  371. }
  372. return map_addr;
  373. }
  374. #endif