process.c 12 KB

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