process.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:9k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  arch/s390/kernel/process.c
  3.  *
  4.  *  S390 version
  5.  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6.  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7.  *               Hartmut Penner (hp@de.ibm.com),
  8.  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  9.  *
  10.  *  Derived from "arch/i386/kernel/process.c"
  11.  *    Copyright (C) 1995, Linus Torvalds
  12.  */
  13. /*
  14.  * This file handles the architecture-dependent parts of process handling..
  15.  */
  16. #define __KERNEL_SYSCALLS__
  17. #include <stdarg.h>
  18. #include <linux/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/user.h>
  31. #include <linux/a.out.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/delay.h>
  34. #include <linux/reboot.h>
  35. #include <linux/init.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/processor.h>
  41. #include <asm/irq.h>
  42. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  43. /*
  44.  * The idle loop on a S390...
  45.  */
  46. int cpu_idle(void *unused)
  47. {
  48. psw_t wait_psw;
  49. unsigned long reg;
  50. /* endless idle loop with no priority at all */
  51.         init_idle();
  52. current->nice = 20;
  53. current->counter = -100;
  54. while (1) {
  55. if (current->need_resched) {
  56. schedule();
  57. check_pgt_cache();
  58. continue;
  59. }
  60. /* 
  61.  * Wait for external, I/O or machine check interrupt and
  62.  * switch of machine check bit after the wait has ended.
  63.  */
  64. wait_psw.mask = _WAIT_PSW_MASK;
  65. asm volatile (
  66. "    basr %0,0n"
  67. "0:  la   %0,1f-0b(%0)n"
  68. "    st   %0,4(%1)n"
  69. "    oi   4(%1),0x80n"
  70. "    lpsw 0(%1)n"
  71. "1:  la   %0,2f-1b(%0)n"
  72. "    st   %0,4(%1)n"
  73. "    oi   4(%1),0x80n"
  74. "    ni   1(%1),0xf9n"
  75. "    lpsw 0(%1)n"
  76. "2:"
  77. : "=&a" (reg) : "a" (&wait_psw) : "memory", "cc" );
  78. }
  79. }
  80. extern void show_registers(struct pt_regs *regs);
  81. extern void show_trace(unsigned long *sp);
  82. void show_regs(struct pt_regs *regs)
  83. {
  84. struct task_struct *tsk = current;
  85.         printk("CPU:    %d    %sn", tsk->processor, print_tainted());
  86.         printk("Process %s (pid: %d, task: %08lx, ksp: %08x)n",
  87.        current->comm, current->pid, (unsigned long) tsk,
  88.        tsk->thread.ksp);
  89. show_registers(regs);
  90. /* Show stack backtrace if pt_regs is from kernel mode */
  91. if (!(regs->psw.mask & PSW_PROBLEM_STATE))
  92. show_trace((unsigned long *) regs->gprs[15]);
  93. }
  94. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  95. {
  96.         int clone_arg = flags | CLONE_VM;
  97.         int retval;
  98.         __asm__ __volatile__(
  99.                 "     sr    2,2n"
  100.                 "     lr    3,%1n"
  101.                 "     l     4,%6n"     /* load kernel stack ptr of parent */
  102.                 "     svc   %b2n"                     /* Linux system call*/
  103.                 "     cl    4,%6n"    /* compare ksp's: child or parent ? */
  104.                 "     je    0fn"                          /* parent - jump*/
  105.                 "     l     15,%6n"            /* fix kernel stack pointer*/
  106.                 "     ahi   15,%7n"
  107.                 "     xc    0(96,15),0(15)n"           /* clear save area */
  108.                 "     lr    2,%4n"                        /* load argument*/
  109.                 "     lr    14,%5n"                      /* get fn-pointer*/
  110.                 "     basr  14,14n"                             /* call fn*/
  111.                 "     svc   %b3n"                     /* Linux system call*/
  112.                 "0:   lr    %0,2"
  113.                 : "=a" (retval)
  114.                 : "d" (clone_arg), "i" (__NR_clone), "i" (__NR_exit),
  115.                   "d" (arg), "d" (fn), "i" (__LC_KERNEL_STACK) , "i" (-STACK_FRAME_OVERHEAD)
  116.                 : "2", "3", "4" );
  117.         return retval;
  118. }
  119. /*
  120.  * Free current thread data structures etc..
  121.  */
  122. void exit_thread(void)
  123. {
  124. }
  125. void flush_thread(void)
  126. {
  127.         current->used_math = 0;
  128.         current->flags &= ~PF_USEDFPU;
  129. }
  130. void release_thread(struct task_struct *dead_task)
  131. {
  132. }
  133. int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
  134. unsigned long unused,
  135.         struct task_struct * p, struct pt_regs * regs)
  136. {
  137.         struct stack_frame
  138.           {
  139.             unsigned long back_chain;
  140.             unsigned long eos;
  141.             unsigned long glue1;
  142.             unsigned long glue2;
  143.             unsigned long scratch[2];
  144.             unsigned long gprs[10];    /* gprs 6 -15                       */
  145.             unsigned long fprs[4];     /* fpr 4 and 6                      */
  146.             unsigned long empty[4];
  147.             struct pt_regs childregs;
  148.           } *frame;
  149.         frame = (struct stack_frame *) (2*PAGE_SIZE + (unsigned long) p) -1;
  150.         p->thread.ksp = (unsigned long) frame;
  151.         memcpy(&frame->childregs,regs,sizeof(struct pt_regs));
  152.         frame->childregs.gprs[15] = new_stackp;
  153.         frame->back_chain = frame->eos = 0;
  154.         /* new return point is ret_from_sys_call */
  155.         frame->gprs[8] = ((unsigned long) &ret_from_fork) | 0x80000000;
  156.         /* fake return stack for resume(), don't go back to schedule */
  157.         frame->gprs[9]  = (unsigned long) frame;
  158.         /* save fprs, if used in last task */
  159. save_fp_regs(&p->thread.fp_regs);
  160.         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _SEGMENT_TABLE;
  161.         /* Don't copy debug registers */
  162.         memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
  163.         return 0;
  164. }
  165. asmlinkage int sys_fork(struct pt_regs regs)
  166. {
  167.         return do_fork(SIGCHLD, regs.gprs[15], &regs, 0);
  168. }
  169. asmlinkage int sys_clone(struct pt_regs regs)
  170. {
  171.         unsigned long clone_flags;
  172.         unsigned long newsp;
  173.         clone_flags = regs.gprs[3];
  174.         newsp = regs.orig_gpr2;
  175.         if (!newsp)
  176.                 newsp = regs.gprs[15];
  177.         return do_fork(clone_flags, newsp, &regs, 0);
  178. }
  179. /*
  180.  * This is trivial, and on the face of it looks like it
  181.  * could equally well be done in user mode.
  182.  *
  183.  * Not so, for quite unobvious reasons - register pressure.
  184.  * In user mode vfork() cannot have a stack frame, and if
  185.  * done by calling the "clone()" system call directly, you
  186.  * do not have enough call-clobbered registers to hold all
  187.  * the information you need.
  188.  */
  189. asmlinkage int sys_vfork(struct pt_regs regs)
  190. {
  191. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  192.                        regs.gprs[15], &regs, 0);
  193. }
  194. /*
  195.  * sys_execve() executes a new program.
  196.  */
  197. asmlinkage int sys_execve(struct pt_regs regs)
  198. {
  199.         int error;
  200.         char * filename;
  201.         filename = getname((char *) regs.orig_gpr2);
  202.         error = PTR_ERR(filename);
  203.         if (IS_ERR(filename))
  204.                 goto out;
  205.         error = do_execve(filename, (char **) regs.gprs[3], (char **) regs.gprs[4], &regs);
  206. if (error == 0)
  207. {
  208. current->ptrace &= ~PT_DTRACE;
  209. current->thread.fp_regs.fpc=0;
  210. if(MACHINE_HAS_IEEE)
  211. {
  212. __asm__ __volatile__
  213. ("sr  0,0nt"
  214.  "sfpc 0,0nt"
  215. :
  216.         :
  217.                                 :"0");
  218. }
  219. }
  220.         putname(filename);
  221. out:
  222.         return error;
  223. }
  224. /*
  225.  * fill in the FPU structure for a core dump.
  226.  */
  227. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  228. {
  229. save_fp_regs(fpregs);
  230. return 1;
  231. }
  232. /*
  233.  * fill in the user structure for a core dump..
  234.  */
  235. void dump_thread(struct pt_regs * regs, struct user * dump)
  236. {
  237. /* changed the size calculations - should hopefully work better. lbt */
  238. dump->magic = CMAGIC;
  239. dump->start_code = 0;
  240. dump->start_stack = regs->gprs[15] & ~(PAGE_SIZE - 1);
  241. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  242. dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  243. dump->u_dsize -= dump->u_tsize;
  244. dump->u_ssize = 0;
  245. if (dump->start_stack < TASK_SIZE)
  246. dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
  247. memcpy(&dump->regs.gprs[0],regs,sizeof(s390_regs));
  248. dump_fpu (regs, &dump->regs.fp_regs);
  249. memcpy(&dump->regs.per_info,&current->thread.per_info,sizeof(per_struct));
  250. }
  251. /*
  252.  * These bracket the sleeping functions..
  253.  */
  254. extern void scheduling_functions_start_here(void);
  255. extern void scheduling_functions_end_here(void);
  256. #define first_sched ((unsigned long) scheduling_functions_start_here)
  257. #define last_sched ((unsigned long) scheduling_functions_end_here)
  258. unsigned long get_wchan(struct task_struct *p)
  259. {
  260. unsigned long r14, r15, bc;
  261. unsigned long stack_page;
  262. int count = 0;
  263. if (!p || p == current || p->state == TASK_RUNNING)
  264. return 0;
  265. stack_page = (unsigned long) p;
  266. r15 = p->thread.ksp;
  267.         if (!stack_page || r15 < stack_page || r15 >= 8188+stack_page)
  268.                 return 0;
  269.         bc = (*(unsigned long *) r15) & 0x7fffffff;
  270. do {
  271.                 if (bc < stack_page || bc >= 8188+stack_page)
  272.                         return 0;
  273. r14 = (*(unsigned long *) (bc+56)) & 0x7fffffff;
  274. if (r14 < first_sched || r14 >= last_sched)
  275. return r14;
  276. bc = (*(unsigned long *) bc) & 0x7fffffff;
  277. } while (count++ < 16);
  278. return 0;
  279. }
  280. #undef last_sched
  281. #undef first_sched