process.c 13 KB

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