info.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <stdarg.h>
  10. #include <pal.h>
  11. #include <pal_error.h>
  12. #include <shim_fs.h>
  13. #include <shim_internal.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 < ARRAY_SIZE(meminfo); 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. // FIXME: remove once global realloc is enabled
  79. static void* realloc(void* ptr, size_t old_size, size_t new_size) {
  80. void* tmp = malloc(new_size);
  81. if (!tmp) {
  82. return NULL;
  83. }
  84. memcpy(tmp, ptr, old_size);
  85. free(ptr);
  86. return tmp;
  87. }
  88. static int print_to_str(char** str, size_t off, size_t* size, const char* fmt, ...) {
  89. int ret;
  90. va_list ap;
  91. retry:
  92. va_start(ap, fmt);
  93. ret = vsnprintf(*str + off, *size - off, fmt, ap);
  94. va_end(ap);
  95. if (ret < 0) {
  96. return -EINVAL;
  97. }
  98. if ((size_t)ret >= *size - off) {
  99. char* tmp = realloc(*str, *size, *size + 128);
  100. if (!tmp) {
  101. return -ENOMEM;
  102. }
  103. *size += 128;
  104. *str = tmp;
  105. goto retry;
  106. }
  107. return ret;
  108. }
  109. static int proc_cpuinfo_open(struct shim_handle* hdl, const char* name, int flags) {
  110. // This function only serves one file
  111. __UNUSED(name);
  112. if (flags & (O_WRONLY | O_RDWR))
  113. return -EACCES;
  114. size_t len = 0,
  115. max = 128;
  116. char* str = malloc(max);
  117. if (!str) {
  118. return -ENOMEM;
  119. }
  120. #define ADD_INFO(fmt, ...) do { \
  121. int ret = print_to_str(&str, len, &max, fmt, ##__VA_ARGS__); \
  122. if (ret < 0) { \
  123. free(str); \
  124. return ret; \
  125. } \
  126. len += ret; \
  127. } while (0)
  128. for (size_t n = 0; n < pal_control.cpu_info.cpu_num; n++) {
  129. /* Below strings must match exactly the strings retrieved from /proc/cpuinfo
  130. * (see Linux's arch/x86/kernel/cpu/proc.c) */
  131. ADD_INFO("processor\t: %lu\n", n);
  132. ADD_INFO("vendor_id\t: %s\n", pal_control.cpu_info.cpu_vendor);
  133. ADD_INFO("cpu family\t: %lu\n", pal_control.cpu_info.cpu_family);
  134. ADD_INFO("model\t\t: %lu\n", pal_control.cpu_info.cpu_model);
  135. ADD_INFO("model name\t: %s\n", pal_control.cpu_info.cpu_brand);
  136. ADD_INFO("stepping\t: %lu\n", pal_control.cpu_info.cpu_stepping);
  137. ADD_INFO("core id\t\t: %lu\n", n);
  138. ADD_INFO("cpu cores\t: %lu\n", pal_control.cpu_info.cpu_num);
  139. double bogomips = pal_control.cpu_info.cpu_bogomips;
  140. // Apparently graphene snprintf cannot into floats.
  141. ADD_INFO("bogomips\t: %lu.%02lu\n",
  142. (unsigned long)bogomips,
  143. (unsigned long)(bogomips * 100.0 + 0.5) % 100);
  144. ADD_INFO("\n");
  145. }
  146. #undef ADD_INFO
  147. struct shim_str_data* data = calloc(1, sizeof(struct shim_str_data));
  148. if (!data) {
  149. free(str);
  150. return -ENOMEM;
  151. }
  152. data->str = str;
  153. data->len = len;
  154. hdl->type = TYPE_STR;
  155. hdl->flags = flags & ~O_RDONLY;
  156. hdl->acc_mode = MAY_READ;
  157. hdl->info.str.data = data;
  158. return 0;
  159. }
  160. struct proc_fs_ops fs_meminfo = {
  161. .mode = &proc_info_mode,
  162. .stat = &proc_info_stat,
  163. .open = &proc_meminfo_open,
  164. };
  165. struct proc_fs_ops fs_cpuinfo = {
  166. .mode = &proc_info_mode,
  167. .stat = &proc_info_stat,
  168. .open = &proc_cpuinfo_open,
  169. };