info.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <asm/fcntl.h>
  2. #include <asm/mman.h>
  3. #include <asm/prctl.h>
  4. #include <asm/unistd.h>
  5. #include <errno.h>
  6. #include <linux/fcntl.h>
  7. #include <pal.h>
  8. #include <pal_error.h>
  9. #include <shim_fs.h>
  10. #include <shim_internal.h>
  11. // TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
  12. // should investigate and fix this behavior.
  13. #include <linux/stat.h>
  14. static int proc_info_mode(const char* name, mode_t* mode) {
  15. // The path is implicitly set by calling this function
  16. __UNUSED(name);
  17. *mode = 0444;
  18. return 0;
  19. }
  20. static int proc_info_stat(const char* name, struct stat* buf) {
  21. // The path is implicitly set by calling this function
  22. __UNUSED(name);
  23. memset(buf, 0, sizeof(struct stat));
  24. buf->st_dev = buf->st_ino = 1;
  25. buf->st_mode = 0444 | S_IFREG;
  26. buf->st_uid = 0;
  27. buf->st_gid = 0;
  28. buf->st_size = 0;
  29. return 0;
  30. }
  31. static int proc_meminfo_open(struct shim_handle* hdl, const char* name, int flags) {
  32. // This function only serves one file
  33. __UNUSED(name);
  34. if (flags & (O_WRONLY | O_RDWR))
  35. return -EACCES;
  36. int len, max = 128;
  37. char* str = NULL;
  38. struct {
  39. const char* fmt;
  40. unsigned long val;
  41. } meminfo[] = {
  42. {
  43. "MemTotal: %8lu kB\n",
  44. pal_control.mem_info.mem_total / 1024,
  45. },
  46. {
  47. "MemFree: %8lu kB\n",
  48. DkMemoryAvailableQuota() / 1024,
  49. },
  50. };
  51. retry:
  52. max *= 2;
  53. len = 0;
  54. free(str);
  55. str = malloc(max);
  56. if (!str)
  57. return -ENOMEM;
  58. for (size_t i = 0; i < sizeof(meminfo) / sizeof(meminfo[0]); i++) {
  59. int ret = snprintf(str + len, max - len, meminfo[i].fmt, meminfo[i].val);
  60. if (len + ret == max)
  61. goto retry;
  62. len += ret;
  63. }
  64. struct shim_str_data* data = malloc(sizeof(struct shim_str_data));
  65. if (!data) {
  66. free(str);
  67. return -ENOMEM;
  68. }
  69. memset(data, 0, sizeof(struct shim_str_data));
  70. data->str = str;
  71. data->len = len;
  72. hdl->type = TYPE_STR;
  73. hdl->flags = flags & ~O_RDONLY;
  74. hdl->acc_mode = MAY_READ;
  75. hdl->info.str.data = data;
  76. return 0;
  77. }
  78. static int proc_cpuinfo_open(struct shim_handle* hdl, const char* name, int flags) {
  79. // This function only serves one file
  80. __UNUSED(name);
  81. if (flags & (O_WRONLY | O_RDWR))
  82. return -EACCES;
  83. int len, max = 128;
  84. char* str = NULL;
  85. struct {
  86. const char* fmt;
  87. unsigned long val;
  88. }
  89. /* below strings must match exactly the strings retrieved from
  90. * /proc/cpuinfo (see Linux's arch/x86/kernel/cpu/proc.c) */
  91. cpuinfo[] = {
  92. {
  93. "processor\t: %lu\n",
  94. 0,
  95. },
  96. {
  97. "vendor_id\t: %s\n",
  98. (unsigned long)pal_control.cpu_info.cpu_vendor,
  99. },
  100. {
  101. "cpu family\t: %lu\n",
  102. pal_control.cpu_info.cpu_family,
  103. },
  104. {
  105. "model\t\t: %lu\n",
  106. pal_control.cpu_info.cpu_model,
  107. },
  108. {
  109. "model name\t: %s\n",
  110. (unsigned long)pal_control.cpu_info.cpu_brand,
  111. },
  112. {
  113. "stepping\t: %lu\n",
  114. pal_control.cpu_info.cpu_stepping,
  115. },
  116. {
  117. "core id\t\t: %lu\n",
  118. 0,
  119. },
  120. {
  121. "cpu cores\t: %lu\n",
  122. pal_control.cpu_info.cpu_num,
  123. },
  124. };
  125. retry:
  126. max *= 2;
  127. len = 0;
  128. free(str);
  129. str = malloc(max);
  130. if (!str)
  131. return -ENOMEM;
  132. for (size_t n = 0; n < pal_control.cpu_info.cpu_num; n++) {
  133. cpuinfo[0].val = n;
  134. cpuinfo[6].val = n;
  135. for (size_t i = 0; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]); i++) {
  136. int ret = snprintf(str + len, max - len, cpuinfo[i].fmt, cpuinfo[i].val);
  137. if (len + ret == max)
  138. goto retry;
  139. len += ret;
  140. }
  141. if (len >= max - 1)
  142. goto retry;
  143. str[len++] = '\n';
  144. str[len] = 0;
  145. }
  146. struct shim_str_data* data = calloc(1, sizeof(struct shim_str_data));
  147. if (!data) {
  148. free(str);
  149. return -ENOMEM;
  150. }
  151. data->str = str;
  152. data->len = len;
  153. hdl->type = TYPE_STR;
  154. hdl->flags = flags & ~O_RDONLY;
  155. hdl->acc_mode = MAY_READ;
  156. hdl->info.str.data = data;
  157. return 0;
  158. }
  159. struct proc_fs_ops fs_meminfo = {
  160. .mode = &proc_info_mode,
  161. .stat = &proc_info_stat,
  162. .open = &proc_meminfo_open,
  163. };
  164. struct proc_fs_ops fs_cpuinfo = {
  165. .mode = &proc_info_mode,
  166. .stat = &proc_info_stat,
  167. .open = &proc_cpuinfo_open,
  168. };