process.c 8.4 KB

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