info.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. *mode = 0444;
  15. return 0;
  16. }
  17. static int proc_info_stat (const char * name, struct stat * buf)
  18. {
  19. memset(buf, 0, sizeof(struct stat));
  20. buf->st_dev = buf->st_ino = 1;
  21. buf->st_mode = 0444|S_IFREG;
  22. buf->st_uid = 0;
  23. buf->st_gid = 0;
  24. buf->st_size = 0;
  25. return 0;
  26. }
  27. static int proc_meminfo_open (struct shim_handle * hdl, const char * name,
  28. int flags)
  29. {
  30. if (flags & (O_WRONLY|O_RDWR))
  31. return -EACCES;
  32. int len, max = 128;
  33. char * str = NULL;
  34. struct { const char * fmt; unsigned long val; }
  35. meminfo[] = {
  36. { "MemTotal: %8lu kB\n", pal_control.mem_info.mem_total / 1024, },
  37. { "MemFree: %8lu kB\n", DkMemoryAvailableQuota() / 1024, },
  38. };
  39. retry:
  40. max *= 2;
  41. len = 0;
  42. free(str);
  43. str = malloc(max);
  44. if (!str)
  45. return -ENOMEM;
  46. for (int i = 0 ; i < sizeof(meminfo) / sizeof(meminfo[0]) ; i++) {
  47. int ret = snprintf(str + len, max - len, meminfo[i].fmt,
  48. meminfo[i].val);
  49. if (len + ret == max)
  50. goto retry;
  51. len += ret;
  52. }
  53. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  54. if (!data) {
  55. free(str);
  56. return -ENOMEM;
  57. }
  58. memset(data, 0, sizeof(struct shim_str_data));
  59. data->str = str;
  60. data->len = len;
  61. hdl->type = TYPE_STR;
  62. hdl->flags = flags & ~O_RDONLY;
  63. hdl->acc_mode = MAY_READ;
  64. hdl->info.str.data = data;
  65. return 0;
  66. }
  67. static int proc_cpuinfo_open (struct shim_handle * hdl, const char * name,
  68. int flags)
  69. {
  70. if (flags & (O_WRONLY|O_RDWR))
  71. return -EACCES;
  72. int len, max = 128;
  73. char * str = NULL;
  74. struct { const char * fmt; unsigned long val; }
  75. /* below strings must match exactly the strings retrieved from
  76. * /proc/cpuinfo (see Linux's arch/x86/kernel/cpu/proc.c) */
  77. cpuinfo[] = {
  78. { "processor\t: %lu\n", 0, },
  79. { "vendor_id\t: %s\n", (unsigned long) pal_control.cpu_info.cpu_vendor, },
  80. { "cpu family\t: %lu\n", pal_control.cpu_info.cpu_family, },
  81. { "model\t\t: %lu\n", pal_control.cpu_info.cpu_model, },
  82. { "model name\t: %s\n", (unsigned long) pal_control.cpu_info.cpu_brand, },
  83. { "stepping\t: %lu\n", pal_control.cpu_info.cpu_stepping, },
  84. { "core id\t\t: %lu\n", 0, },
  85. { "cpu cores\t: %lu\n", pal_control.cpu_info.cpu_num, },
  86. };
  87. retry:
  88. max *= 2;
  89. len = 0;
  90. free(str);
  91. str = malloc(max);
  92. if (!str)
  93. return -ENOMEM;
  94. for (int n = 0 ; n < pal_control.cpu_info.cpu_num ; n++) {
  95. cpuinfo[0].val = n;
  96. cpuinfo[6].val = n;
  97. for (int i = 0 ; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]) ; i++) {
  98. int ret = snprintf(str + len, max - len, cpuinfo[i].fmt,
  99. cpuinfo[i].val);
  100. if (len + ret == max)
  101. goto retry;
  102. len += ret;
  103. }
  104. if (len >= max - 1)
  105. goto retry;
  106. str[len++] = '\n';
  107. str[len] = 0;
  108. }
  109. struct shim_str_data * data = calloc(1, sizeof(struct shim_str_data));
  110. if (!data) {
  111. free(str);
  112. return -ENOMEM;
  113. }
  114. data->str = str;
  115. data->len = len;
  116. hdl->type = TYPE_STR;
  117. hdl->flags = flags & ~O_RDONLY;
  118. hdl->acc_mode = MAY_READ;
  119. hdl->info.str.data = data;
  120. return 0;
  121. }
  122. struct proc_fs_ops fs_meminfo = {
  123. .mode = &proc_info_mode,
  124. .stat = &proc_info_stat,
  125. .open = &proc_meminfo_open,
  126. };
  127. struct proc_fs_ops fs_cpuinfo = {
  128. .mode = &proc_info_mode,
  129. .stat = &proc_info_stat,
  130. .open = &proc_cpuinfo_open,
  131. };