process.c 11 KB

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