process.c 11 KB

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