info.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <shim_internal.h>
  2. #include <shim_fs.h>
  3. #include <pal.h>
  4. #include <pal_error.h>
  5. #include <errno.h>
  6. #include <linux/stat.h>
  7. #include <linux/fcntl.h>
  8. #include <asm/fcntl.h>
  9. #include <asm/mman.h>
  10. #include <asm/unistd.h>
  11. #include <asm/prctl.h>
  12. static int proc_info_mode (const char * name, mode_t * mode)
  13. {
  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. {
  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,
  32. int flags)
  33. {
  34. // This function only serves one file
  35. __UNUSED(name);
  36. if (flags & (O_WRONLY|O_RDWR))
  37. return -EACCES;
  38. int len, max = 128;
  39. char * str = NULL;
  40. struct { const char * fmt; unsigned long val; }
  41. meminfo[] = {
  42. { "MemTotal: %8lu kB\n", pal_control.mem_info.mem_total / 1024, },
  43. { "MemFree: %8lu kB\n", DkMemoryAvailableQuota() / 1024, },
  44. };
  45. retry:
  46. max *= 2;
  47. len = 0;
  48. free(str);
  49. str = malloc(max);
  50. if (!str)
  51. return -ENOMEM;
  52. for (size_t i = 0 ; i < sizeof(meminfo) / sizeof(meminfo[0]) ; i++) {
  53. int ret = snprintf(str + len, max - len, meminfo[i].fmt,
  54. meminfo[i].val);
  55. if (len + ret == max)
  56. goto retry;
  57. len += ret;
  58. }
  59. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  60. if (!data) {
  61. free(str);
  62. return -ENOMEM;
  63. }
  64. memset(data, 0, sizeof(struct shim_str_data));
  65. data->str = str;
  66. data->len = len;
  67. hdl->type = TYPE_STR;
  68. hdl->flags = flags & ~O_RDONLY;
  69. hdl->acc_mode = MAY_READ;
  70. hdl->info.str.data = data;
  71. return 0;
  72. }
  73. static int proc_cpuinfo_open (struct shim_handle * hdl, const char * name,
  74. int flags)
  75. {
  76. // This function only serves one file
  77. __UNUSED(name);
  78. if (flags & (O_WRONLY|O_RDWR))
  79. return -EACCES;
  80. int len, max = 128;
  81. char * str = NULL;
  82. struct { const char * fmt; unsigned long val; }
  83. /* below strings must match exactly the strings retrieved from
  84. * /proc/cpuinfo (see Linux's arch/x86/kernel/cpu/proc.c) */
  85. cpuinfo[] = {
  86. { "processor\t: %lu\n", 0, },
  87. { "vendor_id\t: %s\n", (unsigned long) pal_control.cpu_info.cpu_vendor, },
  88. { "cpu family\t: %lu\n", pal_control.cpu_info.cpu_family, },
  89. { "model\t\t: %lu\n", pal_control.cpu_info.cpu_model, },
  90. { "model name\t: %s\n", (unsigned long) pal_control.cpu_info.cpu_brand, },
  91. { "stepping\t: %lu\n", pal_control.cpu_info.cpu_stepping, },
  92. { "core id\t\t: %lu\n", 0, },
  93. { "cpu cores\t: %lu\n", pal_control.cpu_info.cpu_num, },
  94. };
  95. retry:
  96. max *= 2;
  97. len = 0;
  98. free(str);
  99. str = malloc(max);
  100. if (!str)
  101. return -ENOMEM;
  102. for (size_t n = 0 ; n < pal_control.cpu_info.cpu_num ; n++) {
  103. cpuinfo[0].val = n;
  104. cpuinfo[6].val = n;
  105. for (size_t i = 0 ; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]) ; i++) {
  106. int ret = snprintf(str + len, max - len, cpuinfo[i].fmt,
  107. cpuinfo[i].val);
  108. if (len + ret == max)
  109. goto retry;
  110. len += ret;
  111. }
  112. if (len >= max - 1)
  113. goto retry;
  114. str[len++] = '\n';
  115. str[len] = 0;
  116. }
  117. struct shim_str_data * data = calloc(1, sizeof(struct shim_str_data));
  118. if (!data) {
  119. free(str);
  120. return -ENOMEM;
  121. }
  122. data->str = str;
  123. data->len = len;
  124. hdl->type = TYPE_STR;
  125. hdl->flags = flags & ~O_RDONLY;
  126. hdl->acc_mode = MAY_READ;
  127. hdl->info.str.data = data;
  128. return 0;
  129. }
  130. struct proc_fs_ops fs_meminfo = {
  131. .mode = &proc_info_mode,
  132. .stat = &proc_info_stat,
  133. .open = &proc_meminfo_open,
  134. };
  135. struct proc_fs_ops fs_cpuinfo = {
  136. .mode = &proc_info_mode,
  137. .stat = &proc_info_stat,
  138. .open = &proc_cpuinfo_open,
  139. };