process.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/smp_lock.h>
  10. #include <linux/unistd.h>
  11. #include <linux/user.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include <linux/tick.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <asm/blackfin.h>
  19. #include <asm/fixed_code.h>
  20. #include <asm/mem_map.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. /*
  36. * Powermanagement idle function, if any..
  37. */
  38. void (*pm_idle)(void) = NULL;
  39. EXPORT_SYMBOL(pm_idle);
  40. void (*pm_power_off)(void) = NULL;
  41. EXPORT_SYMBOL(pm_power_off);
  42. /*
  43. * The idle loop on BFIN
  44. */
  45. #ifdef CONFIG_IDLE_L1
  46. static void default_idle(void)__attribute__((l1_text));
  47. void cpu_idle(void)__attribute__((l1_text));
  48. #endif
  49. /*
  50. * This is our default idle handler. We need to disable
  51. * interrupts here to ensure we don't miss a wakeup call.
  52. */
  53. static void default_idle(void)
  54. {
  55. #ifdef CONFIG_IPIPE
  56. ipipe_suspend_domain();
  57. #endif
  58. local_irq_disable_hw();
  59. if (!need_resched())
  60. idle_with_irq_disabled();
  61. local_irq_enable_hw();
  62. }
  63. /*
  64. * The idle thread. We try to conserve power, while trying to keep
  65. * overall latency low. The architecture specific idle is passed
  66. * a value to indicate the level of "idleness" of the system.
  67. */
  68. void cpu_idle(void)
  69. {
  70. /* endless idle loop with no priority at all */
  71. while (1) {
  72. void (*idle)(void) = pm_idle;
  73. #ifdef CONFIG_HOTPLUG_CPU
  74. if (cpu_is_offline(smp_processor_id()))
  75. cpu_die();
  76. #endif
  77. if (!idle)
  78. idle = default_idle;
  79. tick_nohz_stop_sched_tick(1);
  80. while (!need_resched())
  81. idle();
  82. tick_nohz_restart_sched_tick();
  83. preempt_enable_no_resched();
  84. schedule();
  85. preempt_disable();
  86. }
  87. }
  88. /*
  89. * This gets run with P1 containing the
  90. * function to call, and R1 containing
  91. * the "args". Note P0 is clobbered on the way here.
  92. */
  93. void kernel_thread_helper(void);
  94. __asm__(".section .text\n"
  95. ".align 4\n"
  96. "_kernel_thread_helper:\n\t"
  97. "\tsp += -12;\n\t"
  98. "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
  99. /*
  100. * Create a kernel thread.
  101. */
  102. pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
  103. {
  104. struct pt_regs regs;
  105. memset(&regs, 0, sizeof(regs));
  106. regs.r1 = (unsigned long)arg;
  107. regs.p1 = (unsigned long)fn;
  108. regs.pc = (unsigned long)kernel_thread_helper;
  109. regs.orig_p0 = -1;
  110. /* Set bit 2 to tell ret_from_fork we should be returning to kernel
  111. mode. */
  112. regs.ipend = 0x8002;
  113. __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
  114. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
  115. NULL);
  116. }
  117. EXPORT_SYMBOL(kernel_thread);
  118. /*
  119. * Do necessary setup to start up a newly executed thread.
  120. *
  121. * pass the data segment into user programs if it exists,
  122. * it can't hurt anything as far as I can tell
  123. */
  124. void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  125. {
  126. set_fs(USER_DS);
  127. regs->pc = new_ip;
  128. if (current->mm)
  129. regs->p5 = current->mm->start_data;
  130. #ifndef CONFIG_SMP
  131. task_thread_info(current)->l1_task_info.stack_start =
  132. (void *)current->mm->context.stack_start;
  133. task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
  134. memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
  135. sizeof(*L1_SCRATCH_TASK_INFO));
  136. #endif
  137. wrusp(new_sp);
  138. }
  139. EXPORT_SYMBOL_GPL(start_thread);
  140. void flush_thread(void)
  141. {
  142. }
  143. asmlinkage int bfin_vfork(struct pt_regs *regs)
  144. {
  145. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
  146. NULL);
  147. }
  148. asmlinkage int bfin_clone(struct pt_regs *regs)
  149. {
  150. unsigned long clone_flags;
  151. unsigned long newsp;
  152. #ifdef __ARCH_SYNC_CORE_DCACHE
  153. if (current->rt.nr_cpus_allowed == num_possible_cpus()) {
  154. current->cpus_allowed = cpumask_of_cpu(smp_processor_id());
  155. current->rt.nr_cpus_allowed = 1;
  156. }
  157. #endif
  158. /* syscall2 puts clone_flags in r0 and usp in r1 */
  159. clone_flags = regs->r0;
  160. newsp = regs->r1;
  161. if (!newsp)
  162. newsp = rdusp();
  163. else
  164. newsp -= 12;
  165. return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
  166. }
  167. int
  168. copy_thread(unsigned long clone_flags,
  169. unsigned long usp, unsigned long topstk,
  170. struct task_struct *p, struct pt_regs *regs)
  171. {
  172. struct pt_regs *childregs;
  173. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  174. *childregs = *regs;
  175. childregs->r0 = 0;
  176. p->thread.usp = usp;
  177. p->thread.ksp = (unsigned long)childregs;
  178. p->thread.pc = (unsigned long)ret_from_fork;
  179. return 0;
  180. }
  181. /*
  182. * sys_execve() executes a new program.
  183. */
  184. asmlinkage int sys_execve(const char __user *name,
  185. const char __user *const __user *argv,
  186. const char __user *const __user *envp)
  187. {
  188. int error;
  189. char *filename;
  190. struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
  191. filename = getname(name);
  192. error = PTR_ERR(filename);
  193. if (IS_ERR(filename))
  194. return error;
  195. error = do_execve(filename, argv, envp, regs);
  196. putname(filename);
  197. return error;
  198. }
  199. unsigned long get_wchan(struct task_struct *p)
  200. {
  201. unsigned long fp, pc;
  202. unsigned long stack_page;
  203. int count = 0;
  204. if (!p || p == current || p->state == TASK_RUNNING)
  205. return 0;
  206. stack_page = (unsigned long)p;
  207. fp = p->thread.usp;
  208. do {
  209. if (fp < stack_page + sizeof(struct thread_info) ||
  210. fp >= 8184 + stack_page)
  211. return 0;
  212. pc = ((unsigned long *)fp)[1];
  213. if (!in_sched_functions(pc))
  214. return pc;
  215. fp = *(unsigned long *)fp;
  216. }
  217. while (count++ < 16);
  218. return 0;
  219. }
  220. void finish_atomic_sections (struct pt_regs *regs)
  221. {
  222. int __user *up0 = (int __user *)regs->p0;
  223. switch (regs->pc) {
  224. default:
  225. /* not in middle of an atomic step, so resume like normal */
  226. return;
  227. case ATOMIC_XCHG32 + 2:
  228. put_user(regs->r1, up0);
  229. break;
  230. case ATOMIC_CAS32 + 2:
  231. case ATOMIC_CAS32 + 4:
  232. if (regs->r0 == regs->r1)
  233. case ATOMIC_CAS32 + 6:
  234. put_user(regs->r2, up0);
  235. break;
  236. case ATOMIC_ADD32 + 2:
  237. regs->r0 = regs->r1 + regs->r0;
  238. /* fall through */
  239. case ATOMIC_ADD32 + 4:
  240. put_user(regs->r0, up0);
  241. break;
  242. case ATOMIC_SUB32 + 2:
  243. regs->r0 = regs->r1 - regs->r0;
  244. /* fall through */
  245. case ATOMIC_SUB32 + 4:
  246. put_user(regs->r0, up0);
  247. break;
  248. case ATOMIC_IOR32 + 2:
  249. regs->r0 = regs->r1 | regs->r0;
  250. /* fall through */
  251. case ATOMIC_IOR32 + 4:
  252. put_user(regs->r0, up0);
  253. break;
  254. case ATOMIC_AND32 + 2:
  255. regs->r0 = regs->r1 & regs->r0;
  256. /* fall through */
  257. case ATOMIC_AND32 + 4:
  258. put_user(regs->r0, up0);
  259. break;
  260. case ATOMIC_XOR32 + 2:
  261. regs->r0 = regs->r1 ^ regs->r0;
  262. /* fall through */
  263. case ATOMIC_XOR32 + 4:
  264. put_user(regs->r0, up0);
  265. break;
  266. }
  267. /*
  268. * We've finished the atomic section, and the only thing left for
  269. * userspace is to do a RTS, so we might as well handle that too
  270. * since we need to update the PC anyways.
  271. */
  272. regs->pc = regs->rets;
  273. }
  274. static inline
  275. int in_mem(unsigned long addr, unsigned long size,
  276. unsigned long start, unsigned long end)
  277. {
  278. return addr >= start && addr + size <= end;
  279. }
  280. static inline
  281. int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
  282. unsigned long const_addr, unsigned long const_size)
  283. {
  284. return const_size &&
  285. in_mem(addr, size, const_addr + off, const_addr + const_size);
  286. }
  287. static inline
  288. int in_mem_const(unsigned long addr, unsigned long size,
  289. unsigned long const_addr, unsigned long const_size)
  290. {
  291. return in_mem_const_off(addr, size, 0, const_addr, const_size);
  292. }
  293. #define ASYNC_ENABLED(bnum, bctlnum) \
  294. ({ \
  295. (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? 0 : \
  296. bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? 0 : \
  297. 1; \
  298. })
  299. /*
  300. * We can't read EBIU banks that aren't enabled or we end up hanging
  301. * on the access to the async space. Make sure we validate accesses
  302. * that cross async banks too.
  303. * 0 - found, but unusable
  304. * 1 - found & usable
  305. * 2 - not found
  306. */
  307. static
  308. int in_async(unsigned long addr, unsigned long size)
  309. {
  310. if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE) {
  311. if (!ASYNC_ENABLED(0, 0))
  312. return 0;
  313. if (addr + size <= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE)
  314. return 1;
  315. size -= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE - addr;
  316. addr = ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE;
  317. }
  318. if (addr >= ASYNC_BANK1_BASE && addr < ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE) {
  319. if (!ASYNC_ENABLED(1, 0))
  320. return 0;
  321. if (addr + size <= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE)
  322. return 1;
  323. size -= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE - addr;
  324. addr = ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE;
  325. }
  326. if (addr >= ASYNC_BANK2_BASE && addr < ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE) {
  327. if (!ASYNC_ENABLED(2, 1))
  328. return 0;
  329. if (addr + size <= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE)
  330. return 1;
  331. size -= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE - addr;
  332. addr = ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE;
  333. }
  334. if (addr >= ASYNC_BANK3_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE) {
  335. if (ASYNC_ENABLED(3, 1))
  336. return 0;
  337. if (addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
  338. return 1;
  339. return 0;
  340. }
  341. /* not within async bounds */
  342. return 2;
  343. }
  344. int bfin_mem_access_type(unsigned long addr, unsigned long size)
  345. {
  346. int cpu = raw_smp_processor_id();
  347. /* Check that things do not wrap around */
  348. if (addr > ULONG_MAX - size)
  349. return -EFAULT;
  350. if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
  351. return BFIN_MEM_ACCESS_CORE;
  352. if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
  353. return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
  354. if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  355. return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
  356. if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
  357. return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  358. if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
  359. return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  360. #ifdef COREB_L1_CODE_START
  361. if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
  362. return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
  363. if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  364. return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
  365. if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
  366. return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  367. if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
  368. return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
  369. #endif
  370. if (in_mem_const(addr, size, L2_START, L2_LENGTH))
  371. return BFIN_MEM_ACCESS_CORE;
  372. if (addr >= SYSMMR_BASE)
  373. return BFIN_MEM_ACCESS_CORE_ONLY;
  374. switch (in_async(addr, size)) {
  375. case 0: return -EFAULT;
  376. case 1: return BFIN_MEM_ACCESS_CORE;
  377. case 2: /* fall through */;
  378. }
  379. if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
  380. return BFIN_MEM_ACCESS_CORE;
  381. if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
  382. return BFIN_MEM_ACCESS_DMA;
  383. return -EFAULT;
  384. }
  385. #if defined(CONFIG_ACCESS_CHECK)
  386. #ifdef CONFIG_ACCESS_OK_L1
  387. __attribute__((l1_text))
  388. #endif
  389. /* Return 1 if access to memory range is OK, 0 otherwise */
  390. int _access_ok(unsigned long addr, unsigned long size)
  391. {
  392. int aret;
  393. if (size == 0)
  394. return 1;
  395. /* Check that things do not wrap around */
  396. if (addr > ULONG_MAX - size)
  397. return 0;
  398. if (segment_eq(get_fs(), KERNEL_DS))
  399. return 1;
  400. #ifdef CONFIG_MTD_UCLINUX
  401. if (1)
  402. #else
  403. if (0)
  404. #endif
  405. {
  406. if (in_mem(addr, size, memory_start, memory_end))
  407. return 1;
  408. if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
  409. return 1;
  410. # ifndef CONFIG_ROMFS_ON_MTD
  411. if (0)
  412. # endif
  413. /* For XIP, allow user space to use pointers within the ROMFS. */
  414. if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
  415. return 1;
  416. } else {
  417. if (in_mem(addr, size, memory_start, physical_mem_end))
  418. return 1;
  419. }
  420. if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
  421. return 1;
  422. if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
  423. return 1;
  424. if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
  425. return 1;
  426. if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
  427. return 1;
  428. if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
  429. return 1;
  430. #ifdef COREB_L1_CODE_START
  431. if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
  432. return 1;
  433. if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
  434. return 1;
  435. if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
  436. return 1;
  437. if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
  438. return 1;
  439. #endif
  440. aret = in_async(addr, size);
  441. if (aret < 2)
  442. return aret;
  443. if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
  444. return 1;
  445. if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
  446. return 1;
  447. if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
  448. return 1;
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(_access_ok);
  452. #endif /* CONFIG_ACCESS_CHECK */