2
0

bsd-proc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * BSD process related system call helpers
  3. *
  4. * Copyright (c) 2013-14 Stacey D. Son
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #include <sys/cpuset.h>
  23. #include <sys/resource.h>
  24. #include <sys/wait.h>
  25. #include "qemu.h"
  26. #include "qemu-bsd.h"
  27. #include "signal-common.h"
  28. #include "bsd-proc.h"
  29. /*
  30. * resource/rusage conversion
  31. */
  32. int target_to_host_resource(int code)
  33. {
  34. return code;
  35. }
  36. rlim_t target_to_host_rlim(abi_llong target_rlim)
  37. {
  38. return tswap64(target_rlim);
  39. }
  40. abi_llong host_to_target_rlim(rlim_t rlim)
  41. {
  42. return tswap64(rlim);
  43. }
  44. void h2g_rusage(const struct rusage *rusage,
  45. struct target_freebsd_rusage *target_rusage)
  46. {
  47. __put_user(rusage->ru_utime.tv_sec, &target_rusage->ru_utime.tv_sec);
  48. __put_user(rusage->ru_utime.tv_usec, &target_rusage->ru_utime.tv_usec);
  49. __put_user(rusage->ru_stime.tv_sec, &target_rusage->ru_stime.tv_sec);
  50. __put_user(rusage->ru_stime.tv_usec, &target_rusage->ru_stime.tv_usec);
  51. __put_user(rusage->ru_maxrss, &target_rusage->ru_maxrss);
  52. __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
  53. __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
  54. __put_user(rusage->ru_isrss, &target_rusage->ru_isrss);
  55. __put_user(rusage->ru_minflt, &target_rusage->ru_minflt);
  56. __put_user(rusage->ru_majflt, &target_rusage->ru_majflt);
  57. __put_user(rusage->ru_nswap, &target_rusage->ru_nswap);
  58. __put_user(rusage->ru_inblock, &target_rusage->ru_inblock);
  59. __put_user(rusage->ru_oublock, &target_rusage->ru_oublock);
  60. __put_user(rusage->ru_msgsnd, &target_rusage->ru_msgsnd);
  61. __put_user(rusage->ru_msgrcv, &target_rusage->ru_msgrcv);
  62. __put_user(rusage->ru_nsignals, &target_rusage->ru_nsignals);
  63. __put_user(rusage->ru_nvcsw, &target_rusage->ru_nvcsw);
  64. __put_user(rusage->ru_nivcsw, &target_rusage->ru_nivcsw);
  65. }
  66. abi_long host_to_target_rusage(abi_ulong target_addr,
  67. const struct rusage *rusage)
  68. {
  69. struct target_freebsd_rusage *target_rusage;
  70. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) {
  71. return -TARGET_EFAULT;
  72. }
  73. h2g_rusage(rusage, target_rusage);
  74. unlock_user_struct(target_rusage, target_addr, 1);
  75. return 0;
  76. }
  77. abi_long host_to_target_wrusage(abi_ulong target_addr,
  78. const struct __wrusage *wrusage)
  79. {
  80. struct target_freebsd__wrusage *target_wrusage;
  81. if (!lock_user_struct(VERIFY_WRITE, target_wrusage, target_addr, 0)) {
  82. return -TARGET_EFAULT;
  83. }
  84. h2g_rusage(&wrusage->wru_self, &target_wrusage->wru_self);
  85. h2g_rusage(&wrusage->wru_children, &target_wrusage->wru_children);
  86. unlock_user_struct(target_wrusage, target_addr, 1);
  87. return 0;
  88. }
  89. /*
  90. * wait status conversion.
  91. *
  92. * Map host to target signal numbers for the wait family of syscalls.
  93. * Assume all other status bits are the same.
  94. */
  95. int host_to_target_waitstatus(int status)
  96. {
  97. if (WIFSIGNALED(status)) {
  98. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  99. }
  100. if (WIFSTOPPED(status)) {
  101. return (host_to_target_signal(WSTOPSIG(status)) << 8) | (status & 0xff);
  102. }
  103. return status;
  104. }
  105. int bsd_get_ncpu(void)
  106. {
  107. int ncpu = -1;
  108. cpuset_t mask;
  109. CPU_ZERO(&mask);
  110. if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
  111. &mask) == 0) {
  112. ncpu = CPU_COUNT(&mask);
  113. }
  114. if (ncpu == -1) {
  115. ncpu = sysconf(_SC_NPROCESSORS_ONLN);
  116. }
  117. if (ncpu == -1) {
  118. gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
  119. ncpu = 1;
  120. }
  121. return ncpu;
  122. }