process.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/a.out.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/fs.h>
  36. #include <linux/err.h>
  37. #include <asm/blackfin.h>
  38. #include <asm/fixed_code.h>
  39. asmlinkage void ret_from_fork(void);
  40. /* Points to the SDRAM backup memory for the stack that is currently in
  41. * L1 scratchpad memory.
  42. */
  43. void *current_l1_stack_save;
  44. /* The number of tasks currently using a L1 stack area. The SRAM is
  45. * allocated/deallocated whenever this changes from/to zero.
  46. */
  47. int nr_l1stack_tasks;
  48. /* Start and length of the area in L1 scratchpad memory which we've allocated
  49. * for process stacks.
  50. */
  51. void *l1_stack_base;
  52. unsigned long l1_stack_len;
  53. /*
  54. * Powermanagement idle function, if any..
  55. */
  56. void (*pm_idle)(void) = NULL;
  57. EXPORT_SYMBOL(pm_idle);
  58. void (*pm_power_off)(void) = NULL;
  59. EXPORT_SYMBOL(pm_power_off);
  60. /*
  61. * The idle loop on BFIN
  62. */
  63. #ifdef CONFIG_IDLE_L1
  64. void default_idle(void)__attribute__((l1_text));
  65. void cpu_idle(void)__attribute__((l1_text));
  66. #endif
  67. void default_idle(void)
  68. {
  69. while (!need_resched()) {
  70. local_irq_disable();
  71. if (likely(!need_resched()))
  72. idle_with_irq_disabled();
  73. local_irq_enable();
  74. }
  75. }
  76. void (*idle)(void) = default_idle;
  77. /*
  78. * The idle thread. There's no useful work to be
  79. * done, so just try to conserve power and have a
  80. * low exit latency (ie sit in a loop waiting for
  81. * somebody to say that they'd like to reschedule)
  82. */
  83. void cpu_idle(void)
  84. {
  85. /* endless idle loop with no priority at all */
  86. while (1) {
  87. idle();
  88. preempt_enable_no_resched();
  89. schedule();
  90. preempt_disable();
  91. }
  92. }
  93. /* Fill in the fpu structure for a core dump. */
  94. int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
  95. {
  96. return 1;
  97. }
  98. /*
  99. * This gets run with P1 containing the
  100. * function to call, and R1 containing
  101. * the "args". Note P0 is clobbered on the way here.
  102. */
  103. void kernel_thread_helper(void);
  104. __asm__(".section .text\n"
  105. ".align 4\n"
  106. "_kernel_thread_helper:\n\t"
  107. "\tsp += -12;\n\t"
  108. "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
  109. /*
  110. * Create a kernel thread.
  111. */
  112. pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
  113. {
  114. struct pt_regs regs;
  115. memset(&regs, 0, sizeof(regs));
  116. regs.r1 = (unsigned long)arg;
  117. regs.p1 = (unsigned long)fn;
  118. regs.pc = (unsigned long)kernel_thread_helper;
  119. regs.orig_p0 = -1;
  120. /* Set bit 2 to tell ret_from_fork we should be returning to kernel
  121. mode. */
  122. regs.ipend = 0x8002;
  123. __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
  124. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
  125. NULL);
  126. }
  127. void flush_thread(void)
  128. {
  129. }
  130. asmlinkage int bfin_vfork(struct pt_regs *regs)
  131. {
  132. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
  133. NULL);
  134. }
  135. asmlinkage int bfin_clone(struct pt_regs *regs)
  136. {
  137. unsigned long clone_flags;
  138. unsigned long newsp;
  139. /* syscall2 puts clone_flags in r0 and usp in r1 */
  140. clone_flags = regs->r0;
  141. newsp = regs->r1;
  142. if (!newsp)
  143. newsp = rdusp();
  144. else
  145. newsp -= 12;
  146. return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
  147. }
  148. int
  149. copy_thread(int nr, unsigned long clone_flags,
  150. unsigned long usp, unsigned long topstk,
  151. struct task_struct *p, struct pt_regs *regs)
  152. {
  153. struct pt_regs *childregs;
  154. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  155. *childregs = *regs;
  156. childregs->r0 = 0;
  157. p->thread.usp = usp;
  158. p->thread.ksp = (unsigned long)childregs;
  159. p->thread.pc = (unsigned long)ret_from_fork;
  160. return 0;
  161. }
  162. /*
  163. * sys_execve() executes a new program.
  164. */
  165. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  166. {
  167. int error;
  168. char *filename;
  169. struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
  170. lock_kernel();
  171. filename = getname(name);
  172. error = PTR_ERR(filename);
  173. if (IS_ERR(filename))
  174. goto out;
  175. error = do_execve(filename, argv, envp, regs);
  176. putname(filename);
  177. out:
  178. unlock_kernel();
  179. return error;
  180. }
  181. unsigned long get_wchan(struct task_struct *p)
  182. {
  183. unsigned long fp, pc;
  184. unsigned long stack_page;
  185. int count = 0;
  186. if (!p || p == current || p->state == TASK_RUNNING)
  187. return 0;
  188. stack_page = (unsigned long)p;
  189. fp = p->thread.usp;
  190. do {
  191. if (fp < stack_page + sizeof(struct thread_info) ||
  192. fp >= 8184 + stack_page)
  193. return 0;
  194. pc = ((unsigned long *)fp)[1];
  195. if (!in_sched_functions(pc))
  196. return pc;
  197. fp = *(unsigned long *)fp;
  198. }
  199. while (count++ < 16);
  200. return 0;
  201. }
  202. void finish_atomic_sections (struct pt_regs *regs)
  203. {
  204. if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
  205. return;
  206. switch (regs->pc) {
  207. case ATOMIC_XCHG32 + 2:
  208. put_user(regs->r1, (int *)regs->p0);
  209. regs->pc += 2;
  210. break;
  211. case ATOMIC_CAS32 + 2:
  212. case ATOMIC_CAS32 + 4:
  213. if (regs->r0 == regs->r1)
  214. put_user(regs->r2, (int *)regs->p0);
  215. regs->pc = ATOMIC_CAS32 + 8;
  216. break;
  217. case ATOMIC_CAS32 + 6:
  218. put_user(regs->r2, (int *)regs->p0);
  219. regs->pc += 2;
  220. break;
  221. case ATOMIC_ADD32 + 2:
  222. regs->r0 = regs->r1 + regs->r0;
  223. /* fall through */
  224. case ATOMIC_ADD32 + 4:
  225. put_user(regs->r0, (int *)regs->p0);
  226. regs->pc = ATOMIC_ADD32 + 6;
  227. break;
  228. case ATOMIC_SUB32 + 2:
  229. regs->r0 = regs->r1 - regs->r0;
  230. /* fall through */
  231. case ATOMIC_SUB32 + 4:
  232. put_user(regs->r0, (int *)regs->p0);
  233. regs->pc = ATOMIC_SUB32 + 6;
  234. break;
  235. case ATOMIC_IOR32 + 2:
  236. regs->r0 = regs->r1 | regs->r0;
  237. /* fall through */
  238. case ATOMIC_IOR32 + 4:
  239. put_user(regs->r0, (int *)regs->p0);
  240. regs->pc = ATOMIC_IOR32 + 6;
  241. break;
  242. case ATOMIC_AND32 + 2:
  243. regs->r0 = regs->r1 & regs->r0;
  244. /* fall through */
  245. case ATOMIC_AND32 + 4:
  246. put_user(regs->r0, (int *)regs->p0);
  247. regs->pc = ATOMIC_AND32 + 6;
  248. break;
  249. case ATOMIC_XOR32 + 2:
  250. regs->r0 = regs->r1 ^ regs->r0;
  251. /* fall through */
  252. case ATOMIC_XOR32 + 4:
  253. put_user(regs->r0, (int *)regs->p0);
  254. regs->pc = ATOMIC_XOR32 + 6;
  255. break;
  256. }
  257. }
  258. #if defined(CONFIG_ACCESS_CHECK)
  259. /* Return 1 if access to memory range is OK, 0 otherwise */
  260. int _access_ok(unsigned long addr, unsigned long size)
  261. {
  262. if (size == 0)
  263. return 1;
  264. if (addr > (addr + size))
  265. return 0;
  266. if (segment_eq(get_fs(), KERNEL_DS))
  267. return 1;
  268. #ifdef CONFIG_MTD_UCLINUX
  269. if (addr >= memory_start && (addr + size) <= memory_end)
  270. return 1;
  271. if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
  272. return 1;
  273. #else
  274. if (addr >= memory_start && (addr + size) <= physical_mem_end)
  275. return 1;
  276. #endif
  277. if (addr >= (unsigned long)__init_begin &&
  278. addr + size <= (unsigned long)__init_end)
  279. return 1;
  280. if (addr >= L1_SCRATCH_START
  281. && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
  282. return 1;
  283. #if L1_CODE_LENGTH != 0
  284. if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
  285. && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
  286. return 1;
  287. #endif
  288. #if L1_DATA_A_LENGTH != 0
  289. if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
  290. && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
  291. return 1;
  292. #endif
  293. #if L1_DATA_B_LENGTH != 0
  294. if (addr >= L1_DATA_B_START
  295. && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
  296. return 1;
  297. #endif
  298. return 0;
  299. }
  300. EXPORT_SYMBOL(_access_ok);
  301. #endif /* CONFIG_ACCESS_CHECK */