process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. void show_regs(struct pt_regs *regs)
  119. {
  120. printk(KERN_NOTICE "\n");
  121. printk(KERN_NOTICE
  122. "PC: %08lu Status: %04lu SysStatus: %04lu RETS: %08lu\n",
  123. regs->pc, regs->astat, regs->seqstat, regs->rets);
  124. printk(KERN_NOTICE
  125. "A0.x: %08lx A0.w: %08lx A1.x: %08lx A1.w: %08lx\n",
  126. regs->a0x, regs->a0w, regs->a1x, regs->a1w);
  127. printk(KERN_NOTICE "P0: %08lx P1: %08lx P2: %08lx P3: %08lx\n",
  128. regs->p0, regs->p1, regs->p2, regs->p3);
  129. printk(KERN_NOTICE "P4: %08lx P5: %08lx\n", regs->p4, regs->p5);
  130. printk(KERN_NOTICE "R0: %08lx R1: %08lx R2: %08lx R3: %08lx\n",
  131. regs->r0, regs->r1, regs->r2, regs->r3);
  132. printk(KERN_NOTICE "R4: %08lx R5: %08lx R6: %08lx R7: %08lx\n",
  133. regs->r4, regs->r5, regs->r6, regs->r7);
  134. if (!regs->ipend)
  135. printk(KERN_NOTICE "USP: %08lx\n", rdusp());
  136. }
  137. /* Fill in the fpu structure for a core dump. */
  138. int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
  139. {
  140. return 1;
  141. }
  142. /*
  143. * This gets run with P1 containing the
  144. * function to call, and R1 containing
  145. * the "args". Note P0 is clobbered on the way here.
  146. */
  147. void kernel_thread_helper(void);
  148. __asm__(".section .text\n"
  149. ".align 4\n"
  150. "_kernel_thread_helper:\n\t"
  151. "\tsp += -12;\n\t"
  152. "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
  153. /*
  154. * Create a kernel thread.
  155. */
  156. pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
  157. {
  158. struct pt_regs regs;
  159. memset(&regs, 0, sizeof(regs));
  160. regs.r1 = (unsigned long)arg;
  161. regs.p1 = (unsigned long)fn;
  162. regs.pc = (unsigned long)kernel_thread_helper;
  163. regs.orig_p0 = -1;
  164. /* Set bit 2 to tell ret_from_fork we should be returning to kernel
  165. mode. */
  166. regs.ipend = 0x8002;
  167. __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
  168. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
  169. NULL);
  170. }
  171. void flush_thread(void)
  172. {
  173. }
  174. asmlinkage int bfin_vfork(struct pt_regs *regs)
  175. {
  176. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
  177. NULL);
  178. }
  179. asmlinkage int bfin_clone(struct pt_regs *regs)
  180. {
  181. unsigned long clone_flags;
  182. unsigned long newsp;
  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(int nr, 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. * fill in the user structure for a core dump..
  208. */
  209. void dump_thread(struct pt_regs *regs, struct user *dump)
  210. {
  211. dump->magic = CMAGIC;
  212. dump->start_code = 0;
  213. dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
  214. dump->u_tsize = ((unsigned long)current->mm->end_code) >> PAGE_SHIFT;
  215. dump->u_dsize = ((unsigned long)(current->mm->brk +
  216. (PAGE_SIZE - 1))) >> PAGE_SHIFT;
  217. dump->u_dsize -= dump->u_tsize;
  218. dump->u_ssize = 0;
  219. if (dump->start_stack < TASK_SIZE)
  220. dump->u_ssize =
  221. ((unsigned long)(TASK_SIZE -
  222. dump->start_stack)) >> PAGE_SHIFT;
  223. dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
  224. dump->regs.r0 = regs->r0;
  225. dump->regs.r1 = regs->r1;
  226. dump->regs.r2 = regs->r2;
  227. dump->regs.r3 = regs->r3;
  228. dump->regs.r4 = regs->r4;
  229. dump->regs.r5 = regs->r5;
  230. dump->regs.r6 = regs->r6;
  231. dump->regs.r7 = regs->r7;
  232. dump->regs.p0 = regs->p0;
  233. dump->regs.p1 = regs->p1;
  234. dump->regs.p2 = regs->p2;
  235. dump->regs.p3 = regs->p3;
  236. dump->regs.p4 = regs->p4;
  237. dump->regs.p5 = regs->p5;
  238. dump->regs.orig_p0 = regs->orig_p0;
  239. dump->regs.a0w = regs->a0w;
  240. dump->regs.a1w = regs->a1w;
  241. dump->regs.a0x = regs->a0x;
  242. dump->regs.a1x = regs->a1x;
  243. dump->regs.rets = regs->rets;
  244. dump->regs.astat = regs->astat;
  245. dump->regs.pc = regs->pc;
  246. }
  247. /*
  248. * sys_execve() executes a new program.
  249. */
  250. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  251. {
  252. int error;
  253. char *filename;
  254. struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
  255. lock_kernel();
  256. filename = getname(name);
  257. error = PTR_ERR(filename);
  258. if (IS_ERR(filename))
  259. goto out;
  260. error = do_execve(filename, argv, envp, regs);
  261. putname(filename);
  262. out:
  263. unlock_kernel();
  264. return error;
  265. }
  266. unsigned long get_wchan(struct task_struct *p)
  267. {
  268. unsigned long fp, pc;
  269. unsigned long stack_page;
  270. int count = 0;
  271. if (!p || p == current || p->state == TASK_RUNNING)
  272. return 0;
  273. stack_page = (unsigned long)p;
  274. fp = p->thread.usp;
  275. do {
  276. if (fp < stack_page + sizeof(struct thread_info) ||
  277. fp >= 8184 + stack_page)
  278. return 0;
  279. pc = ((unsigned long *)fp)[1];
  280. if (!in_sched_functions(pc))
  281. return pc;
  282. fp = *(unsigned long *)fp;
  283. }
  284. while (count++ < 16);
  285. return 0;
  286. }
  287. void finish_atomic_sections (struct pt_regs *regs)
  288. {
  289. if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
  290. return;
  291. switch (regs->pc) {
  292. case ATOMIC_XCHG32 + 2:
  293. put_user(regs->r1, (int *)regs->p0);
  294. regs->pc += 2;
  295. break;
  296. case ATOMIC_CAS32 + 2:
  297. case ATOMIC_CAS32 + 4:
  298. if (regs->r0 == regs->r1)
  299. put_user(regs->r2, (int *)regs->p0);
  300. regs->pc = ATOMIC_CAS32 + 8;
  301. break;
  302. case ATOMIC_CAS32 + 6:
  303. put_user(regs->r2, (int *)regs->p0);
  304. regs->pc += 2;
  305. break;
  306. case ATOMIC_ADD32 + 2:
  307. regs->r0 = regs->r1 + regs->r0;
  308. /* fall through */
  309. case ATOMIC_ADD32 + 4:
  310. put_user(regs->r0, (int *)regs->p0);
  311. regs->pc = ATOMIC_ADD32 + 6;
  312. break;
  313. case ATOMIC_SUB32 + 2:
  314. regs->r0 = regs->r1 - regs->r0;
  315. /* fall through */
  316. case ATOMIC_SUB32 + 4:
  317. put_user(regs->r0, (int *)regs->p0);
  318. regs->pc = ATOMIC_SUB32 + 6;
  319. break;
  320. case ATOMIC_IOR32 + 2:
  321. regs->r0 = regs->r1 | regs->r0;
  322. /* fall through */
  323. case ATOMIC_IOR32 + 4:
  324. put_user(regs->r0, (int *)regs->p0);
  325. regs->pc = ATOMIC_IOR32 + 6;
  326. break;
  327. case ATOMIC_AND32 + 2:
  328. regs->r0 = regs->r1 & regs->r0;
  329. /* fall through */
  330. case ATOMIC_AND32 + 4:
  331. put_user(regs->r0, (int *)regs->p0);
  332. regs->pc = ATOMIC_AND32 + 6;
  333. break;
  334. case ATOMIC_XOR32 + 2:
  335. regs->r0 = regs->r1 ^ regs->r0;
  336. /* fall through */
  337. case ATOMIC_XOR32 + 4:
  338. put_user(regs->r0, (int *)regs->p0);
  339. regs->pc = ATOMIC_XOR32 + 6;
  340. break;
  341. }
  342. }
  343. #if defined(CONFIG_ACCESS_CHECK)
  344. int _access_ok(unsigned long addr, unsigned long size)
  345. {
  346. if (size == 0)
  347. return 1;
  348. if (addr > (addr + size))
  349. return 0;
  350. if (segment_eq(get_fs(), KERNEL_DS))
  351. return 1;
  352. #ifdef CONFIG_MTD_UCLINUX
  353. if (addr >= memory_start && (addr + size) <= memory_end)
  354. return 1;
  355. if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
  356. return 1;
  357. #else
  358. if (addr >= memory_start && (addr + size) <= physical_mem_end)
  359. return 1;
  360. #endif
  361. if (addr >= (unsigned long)__init_begin &&
  362. addr + size <= (unsigned long)__init_end)
  363. return 1;
  364. if (addr >= L1_SCRATCH_START
  365. && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
  366. return 1;
  367. #if L1_CODE_LENGTH != 0
  368. if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
  369. && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
  370. return 1;
  371. #endif
  372. #if L1_DATA_A_LENGTH != 0
  373. if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
  374. && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
  375. return 1;
  376. #endif
  377. #if L1_DATA_B_LENGTH != 0
  378. if (addr >= L1_DATA_B_START
  379. && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
  380. return 1;
  381. #endif
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(_access_ok);
  385. #endif /* CONFIG_ACCESS_CHECK */