process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 <asm/blackfin.h>
  36. #include <asm/fixed_code.h>
  37. #define LED_ON 0
  38. #define LED_OFF 1
  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. * We are using a different LED from the one used to indicate timer interrupt.
  62. */
  63. #if defined(CONFIG_BFIN_IDLE_LED)
  64. static inline void leds_switch(int flag)
  65. {
  66. unsigned short tmp = 0;
  67. tmp = bfin_read_CONFIG_BFIN_IDLE_LED_PORT();
  68. SSYNC();
  69. if (flag == LED_ON)
  70. tmp &= ~CONFIG_BFIN_IDLE_LED_PIN; /* light on */
  71. else
  72. tmp |= CONFIG_BFIN_IDLE_LED_PIN; /* light off */
  73. bfin_write_CONFIG_BFIN_IDLE_LED_PORT(tmp);
  74. SSYNC();
  75. }
  76. #else
  77. static inline void leds_switch(int flag)
  78. {
  79. }
  80. #endif
  81. /*
  82. * The idle loop on BFIN
  83. */
  84. #ifdef CONFIG_IDLE_L1
  85. void default_idle(void)__attribute__((l1_text));
  86. void cpu_idle(void)__attribute__((l1_text));
  87. #endif
  88. void default_idle(void)
  89. {
  90. while (!need_resched()) {
  91. leds_switch(LED_OFF);
  92. local_irq_disable();
  93. if (likely(!need_resched()))
  94. idle_with_irq_disabled();
  95. local_irq_enable();
  96. leds_switch(LED_ON);
  97. }
  98. }
  99. void (*idle)(void) = default_idle;
  100. /*
  101. * The idle thread. There's no useful work to be
  102. * done, so just try to conserve power and have a
  103. * low exit latency (ie sit in a loop waiting for
  104. * somebody to say that they'd like to reschedule)
  105. */
  106. void cpu_idle(void)
  107. {
  108. /* endless idle loop with no priority at all */
  109. while (1) {
  110. idle();
  111. preempt_enable_no_resched();
  112. schedule();
  113. preempt_disable();
  114. }
  115. }
  116. void machine_restart(char *__unused)
  117. {
  118. #if defined(CONFIG_BLKFIN_CACHE)
  119. bfin_write_IMEM_CONTROL(0x01);
  120. SSYNC();
  121. #endif
  122. bfin_reset();
  123. /* Dont do anything till the reset occurs */
  124. while (1) {
  125. SSYNC();
  126. }
  127. }
  128. void machine_halt(void)
  129. {
  130. for (;;)
  131. asm volatile ("idle");
  132. }
  133. void machine_power_off(void)
  134. {
  135. for (;;)
  136. asm volatile ("idle");
  137. }
  138. void show_regs(struct pt_regs *regs)
  139. {
  140. printk(KERN_NOTICE "\n");
  141. printk(KERN_NOTICE
  142. "PC: %08lu Status: %04lu SysStatus: %04lu RETS: %08lu\n",
  143. regs->pc, regs->astat, regs->seqstat, regs->rets);
  144. printk(KERN_NOTICE
  145. "A0.x: %08lx A0.w: %08lx A1.x: %08lx A1.w: %08lx\n",
  146. regs->a0x, regs->a0w, regs->a1x, regs->a1w);
  147. printk(KERN_NOTICE "P0: %08lx P1: %08lx P2: %08lx P3: %08lx\n",
  148. regs->p0, regs->p1, regs->p2, regs->p3);
  149. printk(KERN_NOTICE "P4: %08lx P5: %08lx\n", regs->p4, regs->p5);
  150. printk(KERN_NOTICE "R0: %08lx R1: %08lx R2: %08lx R3: %08lx\n",
  151. regs->r0, regs->r1, regs->r2, regs->r3);
  152. printk(KERN_NOTICE "R4: %08lx R5: %08lx R6: %08lx R7: %08lx\n",
  153. regs->r4, regs->r5, regs->r6, regs->r7);
  154. if (!regs->ipend)
  155. printk(KERN_NOTICE "USP: %08lx\n", rdusp());
  156. }
  157. /* Fill in the fpu structure for a core dump. */
  158. int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
  159. {
  160. return 1;
  161. }
  162. /*
  163. * This gets run with P1 containing the
  164. * function to call, and R1 containing
  165. * the "args". Note P0 is clobbered on the way here.
  166. */
  167. void kernel_thread_helper(void);
  168. __asm__(".section .text\n"
  169. ".align 4\n"
  170. "_kernel_thread_helper:\n\t"
  171. "\tsp += -12;\n\t"
  172. "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
  173. /*
  174. * Create a kernel thread.
  175. */
  176. pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
  177. {
  178. struct pt_regs regs;
  179. memset(&regs, 0, sizeof(regs));
  180. regs.r1 = (unsigned long)arg;
  181. regs.p1 = (unsigned long)fn;
  182. regs.pc = (unsigned long)kernel_thread_helper;
  183. regs.orig_p0 = -1;
  184. /* Set bit 2 to tell ret_from_fork we should be returning to kernel
  185. mode. */
  186. regs.ipend = 0x8002;
  187. __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
  188. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
  189. NULL);
  190. }
  191. void flush_thread(void)
  192. {
  193. }
  194. asmlinkage int bfin_vfork(struct pt_regs *regs)
  195. {
  196. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
  197. NULL);
  198. }
  199. asmlinkage int bfin_clone(struct pt_regs *regs)
  200. {
  201. unsigned long clone_flags;
  202. unsigned long newsp;
  203. /* syscall2 puts clone_flags in r0 and usp in r1 */
  204. clone_flags = regs->r0;
  205. newsp = regs->r1;
  206. if (!newsp)
  207. newsp = rdusp();
  208. else
  209. newsp -= 12;
  210. return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
  211. }
  212. int
  213. copy_thread(int nr, unsigned long clone_flags,
  214. unsigned long usp, unsigned long topstk,
  215. struct task_struct *p, struct pt_regs *regs)
  216. {
  217. struct pt_regs *childregs;
  218. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  219. *childregs = *regs;
  220. childregs->r0 = 0;
  221. p->thread.usp = usp;
  222. p->thread.ksp = (unsigned long)childregs;
  223. p->thread.pc = (unsigned long)ret_from_fork;
  224. return 0;
  225. }
  226. /*
  227. * fill in the user structure for a core dump..
  228. */
  229. void dump_thread(struct pt_regs *regs, struct user *dump)
  230. {
  231. dump->magic = CMAGIC;
  232. dump->start_code = 0;
  233. dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
  234. dump->u_tsize = ((unsigned long)current->mm->end_code) >> PAGE_SHIFT;
  235. dump->u_dsize = ((unsigned long)(current->mm->brk +
  236. (PAGE_SIZE - 1))) >> PAGE_SHIFT;
  237. dump->u_dsize -= dump->u_tsize;
  238. dump->u_ssize = 0;
  239. if (dump->start_stack < TASK_SIZE)
  240. dump->u_ssize =
  241. ((unsigned long)(TASK_SIZE -
  242. dump->start_stack)) >> PAGE_SHIFT;
  243. dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
  244. dump->regs.r0 = regs->r0;
  245. dump->regs.r1 = regs->r1;
  246. dump->regs.r2 = regs->r2;
  247. dump->regs.r3 = regs->r3;
  248. dump->regs.r4 = regs->r4;
  249. dump->regs.r5 = regs->r5;
  250. dump->regs.r6 = regs->r6;
  251. dump->regs.r7 = regs->r7;
  252. dump->regs.p0 = regs->p0;
  253. dump->regs.p1 = regs->p1;
  254. dump->regs.p2 = regs->p2;
  255. dump->regs.p3 = regs->p3;
  256. dump->regs.p4 = regs->p4;
  257. dump->regs.p5 = regs->p5;
  258. dump->regs.orig_p0 = regs->orig_p0;
  259. dump->regs.a0w = regs->a0w;
  260. dump->regs.a1w = regs->a1w;
  261. dump->regs.a0x = regs->a0x;
  262. dump->regs.a1x = regs->a1x;
  263. dump->regs.rets = regs->rets;
  264. dump->regs.astat = regs->astat;
  265. dump->regs.pc = regs->pc;
  266. }
  267. /*
  268. * sys_execve() executes a new program.
  269. */
  270. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  271. {
  272. int error;
  273. char *filename;
  274. struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
  275. lock_kernel();
  276. filename = getname(name);
  277. error = PTR_ERR(filename);
  278. if (IS_ERR(filename))
  279. goto out;
  280. error = do_execve(filename, argv, envp, regs);
  281. putname(filename);
  282. out:
  283. unlock_kernel();
  284. return error;
  285. }
  286. unsigned long get_wchan(struct task_struct *p)
  287. {
  288. unsigned long fp, pc;
  289. unsigned long stack_page;
  290. int count = 0;
  291. if (!p || p == current || p->state == TASK_RUNNING)
  292. return 0;
  293. stack_page = (unsigned long)p;
  294. fp = p->thread.usp;
  295. do {
  296. if (fp < stack_page + sizeof(struct thread_info) ||
  297. fp >= 8184 + stack_page)
  298. return 0;
  299. pc = ((unsigned long *)fp)[1];
  300. if (!in_sched_functions(pc))
  301. return pc;
  302. fp = *(unsigned long *)fp;
  303. }
  304. while (count++ < 16);
  305. return 0;
  306. }
  307. void finish_atomic_sections (struct pt_regs *regs)
  308. {
  309. if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
  310. return;
  311. switch (regs->pc) {
  312. case ATOMIC_XCHG32 + 2:
  313. put_user(regs->r1, (int *)regs->p0);
  314. regs->pc += 2;
  315. break;
  316. case ATOMIC_CAS32 + 2:
  317. case ATOMIC_CAS32 + 4:
  318. if (regs->r0 == regs->r1)
  319. put_user(regs->r2, (int *)regs->p0);
  320. regs->pc = ATOMIC_CAS32 + 8;
  321. break;
  322. case ATOMIC_CAS32 + 6:
  323. put_user(regs->r2, (int *)regs->p0);
  324. regs->pc += 2;
  325. break;
  326. case ATOMIC_ADD32 + 2:
  327. regs->r0 = regs->r1 + regs->r0;
  328. /* fall through */
  329. case ATOMIC_ADD32 + 4:
  330. put_user(regs->r0, (int *)regs->p0);
  331. regs->pc = ATOMIC_ADD32 + 6;
  332. break;
  333. case ATOMIC_SUB32 + 2:
  334. regs->r0 = regs->r1 - regs->r0;
  335. /* fall through */
  336. case ATOMIC_SUB32 + 4:
  337. put_user(regs->r0, (int *)regs->p0);
  338. regs->pc = ATOMIC_SUB32 + 6;
  339. break;
  340. case ATOMIC_IOR32 + 2:
  341. regs->r0 = regs->r1 | regs->r0;
  342. /* fall through */
  343. case ATOMIC_IOR32 + 4:
  344. put_user(regs->r0, (int *)regs->p0);
  345. regs->pc = ATOMIC_IOR32 + 6;
  346. break;
  347. case ATOMIC_AND32 + 2:
  348. regs->r0 = regs->r1 & regs->r0;
  349. /* fall through */
  350. case ATOMIC_AND32 + 4:
  351. put_user(regs->r0, (int *)regs->p0);
  352. regs->pc = ATOMIC_AND32 + 6;
  353. break;
  354. case ATOMIC_XOR32 + 2:
  355. regs->r0 = regs->r1 ^ regs->r0;
  356. /* fall through */
  357. case ATOMIC_XOR32 + 4:
  358. put_user(regs->r0, (int *)regs->p0);
  359. regs->pc = ATOMIC_XOR32 + 6;
  360. break;
  361. }
  362. }
  363. #if defined(CONFIG_ACCESS_CHECK)
  364. int _access_ok(unsigned long addr, unsigned long size)
  365. {
  366. if (addr > (addr + size))
  367. return 0;
  368. if (segment_eq(get_fs(), KERNEL_DS))
  369. return 1;
  370. #ifdef CONFIG_MTD_UCLINUX
  371. if (addr >= memory_start && (addr + size) <= memory_end)
  372. return 1;
  373. if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
  374. return 1;
  375. #else
  376. if (addr >= memory_start && (addr + size) <= physical_mem_end)
  377. return 1;
  378. #endif
  379. if (addr >= (unsigned long)__init_begin &&
  380. addr + size <= (unsigned long)__init_end)
  381. return 1;
  382. if (addr >= L1_SCRATCH_START
  383. && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
  384. return 1;
  385. #if L1_CODE_LENGTH != 0
  386. if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
  387. && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
  388. return 1;
  389. #endif
  390. #if L1_DATA_A_LENGTH != 0
  391. if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
  392. && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
  393. return 1;
  394. #endif
  395. #if L1_DATA_B_LENGTH != 0
  396. if (addr >= L1_DATA_B_START
  397. && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
  398. return 1;
  399. #endif
  400. return 0;
  401. }
  402. EXPORT_SYMBOL(_access_ok);
  403. #endif /* CONFIG_ACCESS_CHECK */