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

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. "    larl  %0,0fn"
  67. "    stg   %0,8(%1)n"
  68. "    lpswe 0(%1)n"
  69. "0:  larl  %0,1fn"
  70. "    stg   %0,8(%1)n"
  71. "    ni    1(%1),0xf9n"
  72. "    lpswe 0(%1)n"
  73. "1:"
  74. : "=&a" (reg) : "a" (&wait_psw) : "memory", "cc" );
  75. }
  76. }
  77. extern void show_registers(struct pt_regs *regs);
  78. extern void show_trace(unsigned long *sp);
  79. void show_regs(struct pt_regs *regs)
  80. {
  81. struct task_struct *tsk = current;
  82.         printk("CPU:    %d    %sn", tsk->processor, print_tainted());
  83.         printk("Process %s (pid: %d, task: %016lx, ksp: %016lx)n",
  84.        current->comm, current->pid, (unsigned long) tsk,
  85.        tsk->thread.ksp);
  86. show_registers(regs);
  87. /* Show stack backtrace if pt_regs is from kernel mode */
  88. if (!(regs->psw.mask & PSW_PROBLEM_STATE))
  89. show_trace((unsigned long *) regs->gprs[15]);
  90. }
  91. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  92. {
  93.         int clone_arg = flags | CLONE_VM;
  94.         int retval;
  95.         __asm__ __volatile__(
  96.                 "     slgr  2,2n"
  97.                 "     lgr   3,%1n"
  98.                 "     lg    4,%6n"     /* load kernel stack ptr of parent */
  99.                 "     svc   %b2n"                     /* Linux system call*/
  100.                 "     clg   4,%6n"    /* compare ksp's: child or parent ? */
  101.                 "     je    0fn"                          /* parent - jump*/
  102.                 "     lg    15,%6n"            /* fix kernel stack pointer*/
  103.                 "     aghi  15,%7n"
  104.                 "     xc    0(160,15),0(15)n"          /* clear save area */
  105.                 "     lgr   2,%4n"                        /* load argument*/
  106.                 "     basr  14,%5n"                             /* call fn*/
  107.                 "     svc   %b3n"                     /* Linux system call*/
  108.                 "0:   lgr   %0,2"
  109.                 : "=a" (retval)
  110.                 : "d" (clone_arg), "i" (__NR_clone), "i" (__NR_exit),
  111.                   "d" (arg), "a" (fn), "i" (__LC_KERNEL_STACK) ,
  112.                   "i" (-STACK_FRAME_OVERHEAD)
  113.                 : "2", "3", "4" );
  114.         return retval;
  115. }
  116. /*
  117.  * Free current thread data structures etc..
  118.  */
  119. void exit_thread(void)
  120. {
  121. }
  122. void flush_thread(void)
  123. {
  124.         current->used_math = 0;
  125.         current->flags &= ~PF_USEDFPU;
  126. }
  127. void release_thread(struct task_struct *dead_task)
  128. {
  129. }
  130. int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
  131. unsigned long unused,
  132.         struct task_struct * p, struct pt_regs * regs)
  133. {
  134.         struct stack_frame
  135.           {
  136.             unsigned long back_chain;
  137.             unsigned long eos;
  138.             unsigned long glue1;
  139.             unsigned long glue2;
  140.             unsigned long scratch[2];
  141.             unsigned long gprs[10];    /* gprs 6 -15                       */
  142.             unsigned long fprs[2];     /* fpr 4 and 6                      */
  143.             unsigned long empty[2];
  144.             struct pt_regs childregs;
  145.           } *frame;
  146.         frame = (struct stack_frame *) (4*PAGE_SIZE + (unsigned long) p) -1;
  147.         p->thread.ksp = (unsigned long) frame;
  148.         frame->childregs = *regs;
  149.         frame->childregs.gprs[15] = new_stackp;
  150.         frame->back_chain = frame->eos = 0;
  151.         /* new return point is ret_from_sys_call */
  152.         frame->gprs[8] = (unsigned long) &ret_from_fork;
  153.         /* fake return stack for resume(), don't go back to schedule */
  154.         frame->gprs[9] = (unsigned long) frame;
  155.         /* save fprs, if used in last task */
  156. save_fp_regs(&p->thread.fp_regs);
  157.         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _REGION_TABLE;
  158.         /* Don't copy debug registers */
  159.         memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
  160.         return 0;
  161. }
  162. /* 
  163.  * Allocation and freeing of basic task resources. 
  164.  * The task struct and the stack go together.
  165.  *
  166.  * NOTE: An order-2 allocation can easily fail.  If this
  167.  *       happens we fall back to using vmalloc ...
  168.  */
  169. struct task_struct *alloc_task_struct(void)
  170. {
  171. struct task_struct *tsk = __get_free_pages(GFP_KERNEL, 2);
  172. if (!tsk)
  173. tsk = vmalloc(16384);
  174. if (!tsk)
  175. return NULL;
  176.         atomic_set((atomic_t *)(tsk + 1), 1);
  177.         return tsk;
  178. }
  179. void free_task_struct(struct task_struct *tsk)
  180. {
  181. if (atomic_dec_and_test((atomic_t *)(tsk + 1)))
  182. {
  183. if ((unsigned long)tsk < VMALLOC_START)
  184. free_pages((unsigned long)tsk, 2);
  185. else
  186. vfree(tsk);
  187. }
  188. }
  189. void get_task_struct(struct task_struct *tsk)
  190. {
  191. atomic_inc((atomic_t *)(tsk + 1));
  192. }
  193. asmlinkage int sys_fork(struct pt_regs regs)
  194. {
  195.         return do_fork(SIGCHLD, regs.gprs[15], &regs, 0);
  196. }
  197. asmlinkage int sys_clone(struct pt_regs regs)
  198. {
  199.         unsigned long clone_flags;
  200.         unsigned long newsp;
  201.         clone_flags = regs.gprs[3];
  202.         newsp = regs.orig_gpr2;
  203.         if (!newsp)
  204.                 newsp = regs.gprs[15];
  205.         return do_fork(clone_flags, newsp, &regs, 0);
  206. }
  207. /*
  208.  * This is trivial, and on the face of it looks like it
  209.  * could equally well be done in user mode.
  210.  *
  211.  * Not so, for quite unobvious reasons - register pressure.
  212.  * In user mode vfork() cannot have a stack frame, and if
  213.  * done by calling the "clone()" system call directly, you
  214.  * do not have enough call-clobbered registers to hold all
  215.  * the information you need.
  216.  */
  217. asmlinkage int sys_vfork(struct pt_regs regs)
  218. {
  219. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  220.                        regs.gprs[15], &regs, 0);
  221. }
  222. /*
  223.  * sys_execve() executes a new program.
  224.  */
  225. asmlinkage int sys_execve(struct pt_regs regs)
  226. {
  227.         int error;
  228.         char * filename;
  229.         filename = getname((char *) regs.orig_gpr2);
  230.         error = PTR_ERR(filename);
  231.         if (IS_ERR(filename))
  232.                 goto out;
  233.         error = do_execve(filename, (char **) regs.gprs[3], (char **) regs.gprs[4], &regs);
  234. if (error == 0)
  235. {
  236. current->ptrace &= ~PT_DTRACE;
  237. current->thread.fp_regs.fpc=0;
  238. __asm__ __volatile__
  239.         ("sr  0,0nt"
  240.          "sfpc 0,0nt"
  241.  : : :"0");
  242. }
  243.         putname(filename);
  244. out:
  245.         return error;
  246. }
  247. /*
  248.  * fill in the FPU structure for a core dump.
  249.  */
  250. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  251. {
  252. save_fp_regs(fpregs);
  253. return 1;
  254. }
  255. /*
  256.  * fill in the user structure for a core dump..
  257.  */
  258. void dump_thread(struct pt_regs * regs, struct user * dump)
  259. {
  260. /* changed the size calculations - should hopefully work better. lbt */
  261. dump->magic = CMAGIC;
  262. dump->start_code = 0;
  263. dump->start_stack = regs->gprs[15] & ~(PAGE_SIZE - 1);
  264. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  265. dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  266. dump->u_dsize -= dump->u_tsize;
  267. dump->u_ssize = 0;
  268. if (dump->start_stack < TASK_SIZE)
  269. dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
  270. memcpy(&dump->regs.gprs[0],regs,sizeof(s390_regs));
  271. dump_fpu (regs, &dump->regs.fp_regs);
  272. memcpy(&dump->regs.per_info,&current->thread.per_info,sizeof(per_struct));
  273. }
  274. /*
  275.  * These bracket the sleeping functions..
  276.  */
  277. extern void scheduling_functions_start_here(void);
  278. extern void scheduling_functions_end_here(void);
  279. #define first_sched ((unsigned long) scheduling_functions_start_here)
  280. #define last_sched ((unsigned long) scheduling_functions_end_here)
  281. unsigned long get_wchan(struct task_struct *p)
  282. {
  283.         unsigned long r14, r15, bc;
  284.         unsigned long stack_page;
  285.         int count = 0;
  286.         if (!p || p == current || p->state == TASK_RUNNING)
  287.                 return 0;
  288.         stack_page = (unsigned long) p;
  289.         r15 = p->thread.ksp;
  290.         if (!stack_page || r15 < stack_page || r15 >= 16380+stack_page)
  291.                 return 0;
  292.         bc = *(unsigned long *) r15;
  293.         do {
  294.                 if (bc < stack_page || bc >= 16380+stack_page)
  295.                         return 0;
  296.                 r14 = *(unsigned long *) (bc+112);
  297.                 if (r14 < first_sched || r14 >= last_sched)
  298.                         return r14;
  299.                 bc = *(unsigned long *) bc;
  300.         } while (count++ < 16);
  301.         return 0;
  302. }
  303. #undef last_sched
  304. #undef first_sched