process.c 8.5 KB

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