info.c 4.5 KB

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