process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Blackfin architecture-dependent process handling
  3. *
  4. * Copyright 2004-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #include <linux/module.h>
  9. #include <linux/unistd.h>
  10. #include <linux/user.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/mm_types.h>
  18. #include <linux/tick.h>
  19. #include <linux/fs.h>
  20. #include <linux/err.h>
  21. #include <asm/blackfin.h>
  22. #include <asm/fixed_code.h>
  23. #include <asm/mem_map.h>
  24. #include <asm/irq.h>
  25. asmlinkage void ret_from_fork(void);
  26. /* Points to the SDRAM backup memory for the stack that is currently in
  27. * L1 scratchpad memory.
  28. */
  29. void *current_l1_stack_save;
  30. /* The number of tasks currently using a L1 stack area. The SRAM is
  31. * allocated/deallocated whenever this changes from/to zero.
  32. */
  33. int nr_l1stack_tasks;
  34. /* Start and length of the area in L1 scratchpad memory which we've allocated
  35. * for process stacks.
  36. */
  37. void *l1_stack_base;
  38. unsigned long l1_stack_len;
  39. void (*pm_power_off)(void) = NULL;
  40. EXPORT_SYMBOL(pm_power_off);
  41. /*
  42. * The idle loop on BFIN
  43. */
  44. #ifdef CONFIG_IDLE_L1
  45. void arch_cpu_idle(void)__attribute__((l1_text));
  46. #endif
  47. /*
  48. * This is our default idle handler. We need to disable
  49. * interrupts here to ensure we don't miss a wakeup call.
  50. */
  51. void arch_cpu_idle(void)
  52. {
  53. #ifdef CONFIG_IPIPE
  54. ipipe_suspend_domain();
  55. #endif
  56. hard_local_irq_disable();
  57. if (!need_resched())
  58. idle_with_irq_disabled();
  59. hard_local_irq_enable();
  60. }
  61. #ifdef CONFIG_HOTPLUG_CPU
  62. void arch_cpu_idle_dead(void)
  63. {
  64. cpu_die();
  65. }
  66. #endif
  67. /*
  68. * Do necessary setup to start up a newly executed thread.
  69. *
  70. * pass the data segment into user programs if it exists,
  71. * it can't hurt anything as far as I can tell
  72. */
  73. void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  74. {
  75. regs->pc = new_ip;
  76. if (current->mm)
  77. regs->p5 = current->mm->start_data;
  78. #ifndef CONFIG_SMP
  79. task_thread_info(current)->l1_task_info.stack_start =
  80. (void *)current->mm->context.stack_start;
  81. task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
  82. memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
  83. sizeof(*L1_SCRATCH_TASK_INFO));
  84. #endif
  85. wrusp(new_sp);
  86. }
  87. EXPORT_SYMBOL_GPL(start_thread);
  88. void flush_thread(void)
  89. {
  90. }
  91. asmlinkage int bfin_clone(unsigned long clone_flags, unsigned long newsp)
  92. {
  93. #ifdef __ARCH_SYNC_CORE_DCACHE
  94. if (current->nr_cpus_allowed == num_possible_cpus())
  95. set_cpus_allowed_ptr(current, cpumask_of(smp_processor_id()));
  96. #endif
  97. if (newsp)
  98. newsp -= 12;
  99. return do_fork(clone_flags, newsp, 0, NULL, NULL);
  100. }
  101. int
  102. copy_thread(unsigned long clone_flags,
  103. unsigned long usp, unsigned long topstk,
  104. struct task_struct *p)
  105. {
  106. struct pt_regs *childregs;
  107. unsigned long *v;
  108. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  109. v = ((unsigned long *)childregs) - 2;
  110. if (unlikely(p->flags & PF_KTHREAD)) {
  111. memset(childregs, 0, sizeof(struct pt_regs));
  112. v[0] = usp;
  113. v[1] = topstk;
  114. childregs->orig_p0 = -1;
  115. childregs->ipend = 0x8000;
  116. __asm__ __volatile__("%0 = syscfg;":"=da"(childregs->syscfg):);
  117. p->thread.usp = 0;
  118. } else {
  119. *childregs = *current_pt_regs();
  120. childregs->r0 = 0;
  121. p->thread.usp = usp ? : rdusp();
  122. v[0] = v[1] = 0;
  123. }
  124. p->thread.ksp = (unsigned long)v;
  125. p->thread.pc = (unsigned long)ret_from_fork;
  126. return 0;
  127. }
  128. unsigned long get_wchan(struct task_struct *p)
  129. {
  130. unsigned long fp, pc;
  131. unsigned long stack_page;
  132. int count = 0;
  133. if (!p || p == current || p->state == TASK_RUNNING)
  134. return 0;
  135. stack_page = (unsigned long)p;
  136. fp = p->thread.usp;
  137. do {
  138. if (fp < stack_page + sizeof(struct thread_info) ||
  139. fp >= 8184 + stack_page)
  140. return 0;
  141. pc = ((unsigned long *)fp)[1];
  142. if (!in_sched_functions(pc))
  143. return pc;
  144. fp = *(unsigned long *)fp;
  145. }
  146. while (count++ < 16);
  147. return 0;
  148. }
  149. void finish_atomic_sections (struct pt_regs *regs)
  150. {
  151. int __user *up0 = (int __user *)regs->p0;
  152. switch (regs->pc) {
  153. default:
  154. /* not in middle of an atomic step, so resume like normal */
  155. return;
  156. case ATOMIC_XCHG32 + 2:
  157. put_user(regs->r1, up0);
  158. break;
  159. case ATOMIC_CAS32 + 2:
  160. case ATOMIC_CAS32 + 4:
  161. if (regs->r0 == regs->r1)
  162. case ATOMIC_CAS32 + 6:
  163. put_user(regs->r2, up0);
  164. break;
  165. case ATOMIC_ADD32 + 2:
  166. regs->r0 = regs->r1 + regs->r0;
  167. /* fall through */
  168. case ATOMIC_ADD32 + 4:
  169. put_user(regs->r0, up0);
  170. break;
  171. case ATOMIC_SUB32 + 2:
  172. regs->r0 = regs->r1 - regs->r0;
  173. /* fall through */
  174. case ATOMIC_SUB32 + 4:
  175. put_user(regs->r0, up0);
  176. break;
  177. case ATOMIC_IOR32 + 2:
  178. regs->r0 = regs->r1 | regs->r0;
  179. /* fall through */
  180. case ATOMIC_IOR32 + 4:
  181. put_user(regs->r0, up0);
  182. break;
  183. case ATOMIC_AND32 + 2:
  184. regs->r0 = regs->r1 & regs->r0;
  185. /* fall through */
  186. case ATOMIC_AND32 + 4:
  187. put_user(regs->r0, up0);
  188. break;
  189. case ATOMIC_XOR32 + 2:
  190. regs->r0 = regs->r1 ^ regs->r0;
  191. /* fall through */
  192. case ATOMIC_XOR32 + 4:
  193. put_user(regs->r0, up0);
  194. break;
  195. }
  196. /*
  197. * We've finished the atomic section, and the only thing left for
  198. * userspace is to do a RTS, so we might as well handle that too
  199. * since we need to update the PC anyways.
  200. */
  201. regs->pc = regs->rets;
  202. }
  203. static inline
  204. int in_mem(unsigned long addr, unsigned long size,
  205. unsigned long start, unsigned long end)
  206. {
  207. return addr >= start && addr + size <= end;
  208. }
  209. static inline
  210. int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
  211. unsigned long const_addr, unsigned long const_size)
  212. {
  213. return const_size &&
  214. in_mem(addr, size, const_addr + off, const_addr + const_size);
  215. }
  216. static inline
  217. int in_mem_const(unsigned long addr, unsigned long size,
  218. unsigned long const_addr, unsigned long const_size)
  219. {
  220. return in_mem_const_off(addr, size, 0, const_addr, const_size);
  221. }
  222. #ifdef CONFIG_BF60x
  223. #define ASYNC_ENABLED(bnum, bctlnum) 1
  224. #else
  225. #define ASYNC_ENABLED(bnum, bctlnum) \
  226. ({ \
  227. (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? 0 : \
  228. bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? 0 : \
  229. 1; \
  230. })
  231. #endif
  232. /*
  233. * We can't read EBIU banks that aren't enabled or we end up hanging
  234. * on the access to the async space. Make sure we validate accesses
  235. * that cross async banks too.
  236. * 0 - found, but unusable
  237. * 1 - found & usable
  238. * 2 - not found
  239. */
  240. static
  241. int in_async(unsigned long addr, unsigned long size)
  242. {
  243. if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE) {
  244. if (!ASYNC_ENABLED(0, 0))
  245. return 0;
  246. if (addr + size <= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE)
  247. return 1;
  248. size -= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE - addr;
  249. addr = ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE;
  250. }
  251. if (addr >= ASYNC_BANK1_BASE && addr < ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE) {
  252. if (!ASYNC_ENABLED(1, 0))
  253. return 0;
  254. if (addr + size <= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE)
  255. return 1;
  256. size -= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE - addr;
  257. addr = ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE;
  258. }
  259. if (addr >= ASYNC_BANK2_BASE && addr < ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE) {
  260. if (!ASYNC_ENABLED(2, 1))
  261. return 0;
  262. if (addr + size <= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE)
  263. return 1;
  264. size -= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE - addr;
  265. addr = ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE;
  266. }
  267. if (addr >= ASYNC_BANK3_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE) {
  268. if (ASYNC_ENABLED(3, 1))
  269. return 0;
  270. if (addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
  271. return 1;
  272. return 0;
  273. }
  274. /* not within async bounds */
  275. return 2;
  276. }
  277. int bfin_mem_access_type(unsigned long addr, unsigned long size)
  278. {
  279. int cpu = raw_smp_processor_id();
  280. /* Check that things do not wrap around */
  281. if (addr > ULONG_MAX - size)
  282. return -EFAULT;
  283. if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
  284. return BFIN_MEM_ACCESS_CORE;
  285. if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
  286. return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
  287. if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  288. return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
  289. if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
  290. return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  291. if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
  292. return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  293. #ifdef COREB_L1_CODE_START
  294. if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
  295. return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
  296. if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  297. return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
  298. if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
  299. return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  300. if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
  301. return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  302. #endif
  303. if (in_mem_const(addr, size, L2_START, L2_LENGTH))
  304. return BFIN_MEM_ACCESS_CORE;
  305. if (addr >= SYSMMR_BASE)
  306. return BFIN_MEM_ACCESS_CORE_ONLY;
  307. switch (in_async(addr, size)) {
  308. case 0: return -EFAULT;
  309. case 1: return BFIN_MEM_ACCESS_CORE;
  310. case 2: /* fall through */;
  311. }
  312. if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
  313. return BFIN_MEM_ACCESS_CORE;
  314. if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
  315. return BFIN_MEM_ACCESS_DMA;
  316. return -EFAULT;
  317. }
  318. #if defined(CONFIG_ACCESS_CHECK)
  319. #ifdef CONFIG_ACCESS_OK_L1
  320. __attribute__((l1_text))
  321. #endif
  322. /* Return 1 if access to memory range is OK, 0 otherwise */
  323. int _access_ok(unsigned long addr, unsigned long size)
  324. {
  325. int aret;
  326. if (size == 0)
  327. return 1;
  328. /* Check that things do not wrap around */
  329. if (addr > ULONG_MAX - size)
  330. return 0;
  331. if (uaccess_kernel())
  332. return 1;
  333. #ifdef CONFIG_MTD_UCLINUX
  334. if (1)
  335. #else
  336. if (0)
  337. #endif
  338. {
  339. if (in_mem(addr, size, memory_start, memory_end))
  340. return 1;
  341. if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
  342. return 1;
  343. # ifndef CONFIG_ROMFS_ON_MTD
  344. if (0)
  345. # endif
  346. /* For XIP, allow user space to use pointers within the ROMFS. */
  347. if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
  348. return 1;
  349. } else {
  350. if (in_mem(addr, size, memory_start, physical_mem_end))
  351. return 1;
  352. }
  353. if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
  354. return 1;
  355. if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
  356. return 1;
  357. if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
  358. return 1;
  359. if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
  360. return 1;
  361. if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
  362. return 1;
  363. #ifdef COREB_L1_CODE_START
  364. if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
  365. return 1;
  366. if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  367. return 1;
  368. if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
  369. return 1;
  370. if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
  371. return 1;
  372. #endif
  373. #ifndef CONFIG_EXCEPTION_L1_SCRATCH
  374. if (in_mem_const(addr, size, (unsigned long)l1_stack_base, l1_stack_len))
  375. return 1;
  376. #endif
  377. aret = in_async(addr, size);
  378. if (aret < 2)
  379. return aret;
  380. if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
  381. return 1;
  382. if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
  383. return 1;
  384. if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
  385. return 1;
  386. return 0;
  387. }
  388. EXPORT_SYMBOL(_access_ok);
  389. #endif /* CONFIG_ACCESS_CHECK */