tt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <setjmp.h>
  13. #include <sys/time.h>
  14. #include <sys/ptrace.h>
  15. #include <linux/ptrace.h>
  16. #include <sys/wait.h>
  17. #include <sys/mman.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/unistd.h>
  20. #include <asm/page.h>
  21. #include "user_util.h"
  22. #include "kern_util.h"
  23. #include "user.h"
  24. #include "signal_kern.h"
  25. #include "signal_user.h"
  26. #include "sysdep/ptrace.h"
  27. #include "sysdep/sigcontext.h"
  28. #include "irq_user.h"
  29. #include "ptrace_user.h"
  30. #include "time_user.h"
  31. #include "init.h"
  32. #include "os.h"
  33. #include "uml-config.h"
  34. #include "choose-mode.h"
  35. #include "mode.h"
  36. #include "tempfile.h"
  37. int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
  38. int must_succeed)
  39. {
  40. int err;
  41. err = os_protect_memory((void *) addr, len, r, w, x);
  42. if(err < 0){
  43. if(must_succeed)
  44. panic("protect failed, err = %d", -err);
  45. else return(err);
  46. }
  47. return(0);
  48. }
  49. /*
  50. *-------------------------
  51. * only for tt mode (will be deleted in future...)
  52. *-------------------------
  53. */
  54. struct tramp {
  55. int (*tramp)(void *);
  56. void *tramp_data;
  57. unsigned long temp_stack;
  58. int flags;
  59. int pid;
  60. };
  61. /* See above for why sigkill is here */
  62. int sigkill = SIGKILL;
  63. int outer_tramp(void *arg)
  64. {
  65. struct tramp *t;
  66. int sig = sigkill;
  67. t = arg;
  68. t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
  69. t->flags, t->tramp_data);
  70. if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
  71. kill(os_getpid(), sig);
  72. _exit(0);
  73. }
  74. int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
  75. int clone_flags, int (*tramp)(void *))
  76. {
  77. struct tramp arg;
  78. unsigned long sp;
  79. int new_pid, status, err;
  80. /* The trampoline will run on the temporary stack */
  81. sp = stack_sp(temp_stack);
  82. clone_flags |= CLONE_FILES | SIGCHLD;
  83. arg.tramp = tramp;
  84. arg.tramp_data = thread_arg;
  85. arg.temp_stack = temp_stack;
  86. arg.flags = clone_flags;
  87. /* Start the process and wait for it to kill itself */
  88. new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
  89. if(new_pid < 0)
  90. return(new_pid);
  91. CATCH_EINTR(err = waitpid(new_pid, &status, 0));
  92. if(err < 0)
  93. panic("Waiting for outer trampoline failed - errno = %d",
  94. errno);
  95. if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
  96. panic("outer trampoline didn't exit with SIGKILL, "
  97. "status = %d", status);
  98. return(arg.pid);
  99. }
  100. void forward_pending_sigio(int target)
  101. {
  102. sigset_t sigs;
  103. if(sigpending(&sigs))
  104. panic("forward_pending_sigio : sigpending failed");
  105. if(sigismember(&sigs, SIGIO))
  106. kill(target, SIGIO);
  107. }