PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/kern/linux/linux.hh

https://gitlab.com/mauro.belluschi/0-stats
C++ Header | 244 lines | 152 code | 31 blank | 61 comment | 1 complexity | a14723734fb81a5895bc3a7cdd8dc462 MD5 | raw file
  1. /*
  2. * Copyright (c) 2004-2009 The Regents of The University of Michigan
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met: redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer;
  9. * redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution;
  12. * neither the name of the copyright holders nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Authors: Ali Saidi
  29. */
  30. #ifndef __LINUX_HH__
  31. #define __LINUX_HH__
  32. #include "base/types.hh"
  33. #include <string>
  34. #include "kern/operatingsystem.hh"
  35. class ThreadContext;
  36. class LiveProcess;
  37. ///
  38. /// This class encapsulates the types, structures, constants,
  39. /// functions, and syscall-number mappings specific to the Alpha Linux
  40. /// syscall interface.
  41. ///
  42. class Linux : public OperatingSystem
  43. {
  44. public:
  45. //@{
  46. /// Basic Linux types.
  47. typedef uint64_t size_t;
  48. typedef uint64_t off_t;
  49. typedef int64_t time_t;
  50. typedef int64_t clock_t;
  51. typedef uint32_t uid_t;
  52. typedef uint32_t gid_t;
  53. //@}
  54. /// Stat buffer. Note that we can't call it 'stat' since that
  55. /// gets #defined to something else on some systems. This type
  56. /// can be specialized by architecture specific "Linux" classes
  57. typedef struct {
  58. uint32_t st_dev; //!< device
  59. uint32_t st_ino; //!< inode
  60. uint32_t st_mode; //!< mode
  61. uint32_t st_nlink; //!< link count
  62. uint32_t st_uid; //!< owner's user ID
  63. uint32_t st_gid; //!< owner's group ID
  64. uint32_t st_rdev; //!< device number
  65. int32_t _pad1; //!< for alignment
  66. int64_t st_size; //!< file size in bytes
  67. uint64_t st_atimeX; //!< time of last access
  68. uint64_t st_mtimeX; //!< time of last modification
  69. uint64_t st_ctimeX; //!< time of last status change
  70. uint32_t st_blksize; //!< optimal I/O block size
  71. int32_t st_blocks; //!< number of blocks allocated
  72. uint32_t st_flags; //!< flags
  73. uint32_t st_gen; //!< unknown
  74. } tgt_stat;
  75. // same for stat64
  76. typedef struct {
  77. uint64_t st_dev;
  78. uint64_t st_ino;
  79. uint64_t st_rdev;
  80. int64_t st_size;
  81. uint64_t st_blocks;
  82. uint32_t st_mode;
  83. uint32_t st_uid;
  84. uint32_t st_gid;
  85. uint32_t st_blksize;
  86. uint32_t st_nlink;
  87. uint32_t __pad0;
  88. uint64_t st_atimeX;
  89. uint64_t st_atime_nsec;
  90. uint64_t st_mtimeX;
  91. uint64_t st_mtime_nsec;
  92. uint64_t st_ctimeX;
  93. uint64_t st_ctime_nsec;
  94. int64_t ___unused[3];
  95. } tgt_stat64;
  96. /// Length of strings in struct utsname (plus 1 for null char).
  97. static const int _SYS_NMLN = 65;
  98. /// Interface struct for uname().
  99. struct utsname {
  100. char sysname[_SYS_NMLN]; //!< System name.
  101. char nodename[_SYS_NMLN]; //!< Node name.
  102. char release[_SYS_NMLN]; //!< OS release.
  103. char version[_SYS_NMLN]; //!< OS version.
  104. char machine[_SYS_NMLN]; //!< Machine type.
  105. };
  106. /// Limit struct for getrlimit/setrlimit.
  107. struct rlimit {
  108. uint64_t rlim_cur; //!< soft limit
  109. uint64_t rlim_max; //!< hard limit
  110. };
  111. /// For gettimeofday().
  112. struct timeval {
  113. int64_t tv_sec; //!< seconds
  114. int64_t tv_usec; //!< microseconds
  115. };
  116. /// For clock_gettime().
  117. struct timespec {
  118. time_t tv_sec; //!< seconds
  119. int64_t tv_nsec; //!< nanoseconds
  120. };
  121. /// Clock ticks per second, for times().
  122. static const int M5_SC_CLK_TCK = 100;
  123. /// For times().
  124. struct tms {
  125. int64_t tms_utime; //!< user time
  126. int64_t tms_stime; //!< system time
  127. int64_t tms_cutime; //!< user time of children
  128. int64_t tms_cstime; //!< system time of children
  129. };
  130. // For writev/readv
  131. struct tgt_iovec {
  132. uint64_t iov_base; // void *
  133. uint64_t iov_len;
  134. };
  135. //@{
  136. /// ioctl() command codes.
  137. static const unsigned TGT_TCGETS = 0x5401;
  138. static const unsigned TGT_TCGETA = 0x5405;
  139. static const unsigned TGT_TCSETAW = 0x5407;
  140. static const unsigned TGT_FIONREAD = 0x541B;
  141. //@}
  142. /// Return true for the ioctl codes for which we return ENOTTY
  143. /// *without* printing a warning, since we know that ENOTTY is the
  144. /// correct thing to return (and not just a sign that we don't
  145. /// recognize the ioctl code.
  146. static bool
  147. isTtyReq(unsigned req)
  148. {
  149. switch (req) {
  150. case TGT_FIONREAD:
  151. case TGT_TCSETAW:
  152. case TGT_TCGETS:
  153. case TGT_TCGETA:
  154. return true;
  155. default:
  156. return false;
  157. }
  158. }
  159. /// Resource constants for getrlimit().
  160. static const unsigned TGT_RLIMIT_CPU = 0;
  161. static const unsigned TGT_RLIMIT_FSIZE = 1;
  162. static const unsigned TGT_RLIMIT_DATA = 2;
  163. static const unsigned TGT_RLIMIT_STACK = 3;
  164. static const unsigned TGT_RLIMIT_CORE = 4;
  165. static const unsigned TGT_RLIMIT_RSS = 5;
  166. static const unsigned TGT_RLIMIT_NPROC = 6;
  167. static const unsigned TGT_RLIMIT_NOFILE = 7;
  168. static const unsigned TGT_RLIMIT_MEMLOCK = 8;
  169. static const unsigned TGT_RLIMIT_AS = 9;
  170. static const unsigned TGT_RLIMIT_LOCKS = 10;
  171. static const unsigned TGT_RLIMIT_SIGPENDING = 11;
  172. static const unsigned TGT_RLIMIT_MSGQUEUE = 12;
  173. static const unsigned TGT_RLIMIT_NICE = 13;
  174. static const unsigned TGT_RLIMIT_RTPRIO = 14;
  175. static const unsigned TGT_RLIMIT_RTTIME = 15;
  176. static const unsigned TGT_RLIM_NLIMITS = 16;
  177. /// For getrusage().
  178. static const int TGT_RUSAGE_SELF = 0;
  179. static const int TGT_RUSAGE_CHILDREN = -1;
  180. static const int TGT_RUSAGE_BOTH = -2;
  181. struct rusage {
  182. struct timeval ru_utime; //!< user time used
  183. struct timeval ru_stime; //!< system time used
  184. int64_t ru_maxrss; //!< max rss
  185. int64_t ru_ixrss; //!< integral shared memory size
  186. int64_t ru_idrss; //!< integral unshared data "
  187. int64_t ru_isrss; //!< integral unshared stack "
  188. int64_t ru_minflt; //!< page reclaims - total vmfaults
  189. int64_t ru_majflt; //!< page faults
  190. int64_t ru_nswap; //!< swaps
  191. int64_t ru_inblock; //!< block input operations
  192. int64_t ru_oublock; //!< block output operations
  193. int64_t ru_msgsnd; //!< messages sent
  194. int64_t ru_msgrcv; //!< messages received
  195. int64_t ru_nsignals; //!< signals received
  196. int64_t ru_nvcsw; //!< voluntary context switches
  197. int64_t ru_nivcsw; //!< involuntary "
  198. };
  199. static int openSpecialFile(std::string path, LiveProcess *process, ThreadContext *tc);
  200. static std::string procMeminfo(LiveProcess *process, ThreadContext *tc);
  201. // For futex system call
  202. static const unsigned TGT_FUTEX_WAIT = 0;
  203. static const unsigned TGT_FUTEX_WAKE = 1;
  204. static const unsigned TGT_EAGAIN = 11;
  205. static const unsigned TGT_EWOULDBLOCK = TGT_EAGAIN;
  206. static const unsigned TGT_FUTEX_PRIVATE_FLAG = 128;
  207. // for *at syscalls
  208. static const int TGT_AT_FDCWD = -100;
  209. // for MREMAP
  210. static const unsigned TGT_MREMAP_MAYMOVE = 0x1;
  211. static const unsigned TGT_MREMAP_FIXED = 0x2;
  212. }; // class Linux
  213. #endif // __LINUX_HH__