syscall_mi.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* $OpenBSD: syscall_mi.h,v 1.7 2015/07/19 04:45:25 guenther Exp $ */
  2. /*
  3. * Copyright (c) 1982, 1986, 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * @(#)kern_xxx.c 8.2 (Berkeley) 11/14/93
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/tame.h>
  35. #include <sys/proc.h>
  36. #ifdef KTRACE
  37. #include <sys/ktrace.h>
  38. #endif
  39. #include "systrace.h"
  40. #if NSYSTRACE > 0
  41. #include <dev/systrace.h>
  42. #endif
  43. /*
  44. * The MD setup for a system call has been done; here's the MI part.
  45. */
  46. static inline int
  47. mi_syscall(struct proc *p, register_t code, const struct sysent *callp,
  48. register_t *argp, register_t retval[2])
  49. {
  50. int lock = !(callp->sy_flags & SY_NOLOCK);
  51. int error, tamed, tval;
  52. /* refresh the thread's cache of the process's creds */
  53. refreshcreds(p);
  54. #ifdef SYSCALL_DEBUG
  55. KERNEL_LOCK();
  56. scdebug_call(p, code, argp);
  57. KERNEL_UNLOCK();
  58. #endif
  59. #ifdef KTRACE
  60. if (KTRPOINT(p, KTR_SYSCALL)) {
  61. KERNEL_LOCK();
  62. ktrsyscall(p, code, callp->sy_argsize, argp);
  63. KERNEL_UNLOCK();
  64. }
  65. #endif
  66. #if NSYSTRACE > 0
  67. if (ISSET(p->p_flag, P_SYSTRACE)) {
  68. KERNEL_LOCK();
  69. error = systrace_redirect(code, p, argp, retval);
  70. KERNEL_UNLOCK();
  71. return (error);
  72. }
  73. #endif
  74. if (lock)
  75. KERNEL_LOCK();
  76. tamed = (p->p_p->ps_flags & PS_TAMED);
  77. if (tamed && !(tval = tame_check(p, code))) {
  78. if (!lock)
  79. KERNEL_LOCK();
  80. error = tame_fail(p, EPERM, tval);
  81. if (!lock)
  82. KERNEL_UNLOCK();
  83. }
  84. else {
  85. error = (*callp->sy_call)(p, argp, retval);
  86. if (tamed && p->p_tameafter)
  87. tame_aftersyscall(p, code, error);
  88. }
  89. if (lock)
  90. KERNEL_UNLOCK();
  91. return (error);
  92. }
  93. /*
  94. * Finish MI stuff on return, after the registers have been set
  95. */
  96. static inline void
  97. mi_syscall_return(struct proc *p, register_t code, int error,
  98. const register_t retval[2])
  99. {
  100. #ifdef SYSCALL_DEBUG
  101. KERNEL_LOCK();
  102. scdebug_ret(p, code, error, retval);
  103. KERNEL_UNLOCK();
  104. #endif
  105. userret(p);
  106. #ifdef KTRACE
  107. if (KTRPOINT(p, KTR_SYSRET)) {
  108. KERNEL_LOCK();
  109. ktrsysret(p, code, error, retval);
  110. KERNEL_UNLOCK();
  111. }
  112. #endif
  113. }
  114. /*
  115. * Finish MI stuff for a new process/thread to return
  116. */
  117. static inline void
  118. mi_child_return(struct proc *p)
  119. {
  120. #if defined(SYSCALL_DEBUG) || defined(KTRACE)
  121. int code = (p->p_flag & P_THREAD) ? SYS___tfork :
  122. (p->p_p->ps_flags & PS_PPWAIT) ? SYS_vfork : SYS_fork;
  123. const register_t child_retval[2] = { 0, 1 };
  124. #endif
  125. #ifdef SYSCALL_DEBUG
  126. KERNEL_LOCK();
  127. scdebug_ret(p, code, 0, child_retval);
  128. KERNEL_UNLOCK();
  129. #endif
  130. userret(p);
  131. #ifdef KTRACE
  132. if (KTRPOINT(p, KTR_SYSRET)) {
  133. KERNEL_LOCK();
  134. ktrsysret(p, code, 0, child_retval);
  135. KERNEL_UNLOCK();
  136. }
  137. #endif
  138. }
  139. /*
  140. * Do the specific processing necessary for an AST
  141. */
  142. static inline void
  143. mi_ast(struct proc *p, int resched)
  144. {
  145. if (p->p_flag & P_OWEUPC) {
  146. KERNEL_LOCK();
  147. ADDUPROF(p);
  148. KERNEL_UNLOCK();
  149. }
  150. if (resched)
  151. preempt(NULL);
  152. /*
  153. * XXX could move call to userret() here, but
  154. * hppa calls ast() in syscall return and sh calls
  155. * it after userret()
  156. */
  157. }