tt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <sched.h>
  9. #include <errno.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <sys/time.h>
  13. #include <sys/ptrace.h>
  14. #include <linux/ptrace.h>
  15. #include <sys/wait.h>
  16. #include <sys/mman.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/unistd.h>
  19. #include "kern_util.h"
  20. #include "user.h"
  21. #include "signal_kern.h"
  22. #include "sysdep/ptrace.h"
  23. #include "sysdep/sigcontext.h"
  24. #include "irq_user.h"
  25. #include "ptrace_user.h"
  26. #include "init.h"
  27. #include "os.h"
  28. #include "uml-config.h"
  29. #include "choose-mode.h"
  30. #include "mode.h"
  31. #include "tempfile.h"
  32. #include "kern_constants.h"
  33. int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
  34. int must_succeed)
  35. {
  36. int err;
  37. err = os_protect_memory((void *) addr, len, r, w, x);
  38. if(err < 0){
  39. if(must_succeed)
  40. panic("protect failed, err = %d", -err);
  41. else return(err);
  42. }
  43. return(0);
  44. }
  45. void kill_child_dead(int pid)
  46. {
  47. kill(pid, SIGKILL);
  48. kill(pid, SIGCONT);
  49. do {
  50. int n;
  51. CATCH_EINTR(n = waitpid(pid, NULL, 0));
  52. if (n > 0)
  53. kill(pid, SIGCONT);
  54. else
  55. break;
  56. } while(1);
  57. }
  58. void stop(void)
  59. {
  60. while(1) sleep(1000000);
  61. }
  62. int wait_for_stop(int pid, int sig, int cont_type, void *relay)
  63. {
  64. sigset_t *relay_signals = relay;
  65. int status, ret;
  66. while(1){
  67. CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
  68. if((ret < 0) ||
  69. !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
  70. if(ret < 0){
  71. printk("wait failed, errno = %d\n",
  72. errno);
  73. }
  74. else if(WIFEXITED(status))
  75. printk("process %d exited with status %d\n",
  76. pid, WEXITSTATUS(status));
  77. else if(WIFSIGNALED(status))
  78. printk("process %d exited with signal %d\n",
  79. pid, WTERMSIG(status));
  80. else if((WSTOPSIG(status) == SIGVTALRM) ||
  81. (WSTOPSIG(status) == SIGALRM) ||
  82. (WSTOPSIG(status) == SIGIO) ||
  83. (WSTOPSIG(status) == SIGPROF) ||
  84. (WSTOPSIG(status) == SIGCHLD) ||
  85. (WSTOPSIG(status) == SIGWINCH) ||
  86. (WSTOPSIG(status) == SIGINT)){
  87. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  88. continue;
  89. }
  90. else if((relay_signals != NULL) &&
  91. sigismember(relay_signals, WSTOPSIG(status))){
  92. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  93. continue;
  94. }
  95. else printk("process %d stopped with signal %d\n",
  96. pid, WSTOPSIG(status));
  97. panic("wait_for_stop failed to wait for %d to stop "
  98. "with %d\n", pid, sig);
  99. }
  100. return(status);
  101. }
  102. }
  103. void forward_ipi(int fd, int pid)
  104. {
  105. int err;
  106. err = os_set_owner(fd, pid);
  107. if(err < 0)
  108. printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
  109. "target = %d, err = %d\n", fd, os_getpid(), pid, -err);
  110. }
  111. /*
  112. *-------------------------
  113. * only for tt mode (will be deleted in future...)
  114. *-------------------------
  115. */
  116. struct tramp {
  117. int (*tramp)(void *);
  118. void *tramp_data;
  119. unsigned long temp_stack;
  120. int flags;
  121. int pid;
  122. };
  123. /* See above for why sigkill is here */
  124. int sigkill = SIGKILL;
  125. int outer_tramp(void *arg)
  126. {
  127. struct tramp *t;
  128. int sig = sigkill;
  129. t = arg;
  130. t->pid = clone(t->tramp, (void *) t->temp_stack + UM_KERN_PAGE_SIZE/2,
  131. t->flags, t->tramp_data);
  132. if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
  133. kill(os_getpid(), sig);
  134. _exit(0);
  135. }
  136. int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
  137. int clone_flags, int (*tramp)(void *))
  138. {
  139. struct tramp arg;
  140. unsigned long sp;
  141. int new_pid, status, err;
  142. /* The trampoline will run on the temporary stack */
  143. sp = stack_sp(temp_stack);
  144. clone_flags |= CLONE_FILES | SIGCHLD;
  145. arg.tramp = tramp;
  146. arg.tramp_data = thread_arg;
  147. arg.temp_stack = temp_stack;
  148. arg.flags = clone_flags;
  149. /* Start the process and wait for it to kill itself */
  150. new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
  151. if(new_pid < 0)
  152. return(new_pid);
  153. CATCH_EINTR(err = waitpid(new_pid, &status, 0));
  154. if(err < 0)
  155. panic("Waiting for outer trampoline failed - errno = %d",
  156. errno);
  157. if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
  158. panic("outer trampoline didn't exit with SIGKILL, "
  159. "status = %d", status);
  160. return(arg.pid);
  161. }
  162. void forward_pending_sigio(int target)
  163. {
  164. sigset_t sigs;
  165. if(sigpending(&sigs))
  166. panic("forward_pending_sigio : sigpending failed");
  167. if(sigismember(&sigs, SIGIO))
  168. kill(target, SIGIO);
  169. }