info.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <shim_internal.h>
  4. #include <shim_fs.h>
  5. #include <pal.h>
  6. #include <pal_error.h>
  7. #include <errno.h>
  8. #include <linux/stat.h>
  9. #include <linux/fcntl.h>
  10. #include <asm/fcntl.h>
  11. #include <asm/mman.h>
  12. #include <asm/unistd.h>
  13. #include <asm/prctl.h>
  14. static int proc_info_mode (const char * name, mode_t * mode)
  15. {
  16. *mode = 0444;
  17. return 0;
  18. }
  19. static int proc_info_stat (const char * name, struct stat * buf)
  20. {
  21. memset(buf, 0, sizeof(struct stat));
  22. buf->st_dev = buf->st_ino = 1;
  23. buf->st_mode = 0444|S_IFDIR;
  24. buf->st_uid = 0;
  25. buf->st_gid = 0;
  26. buf->st_size = 0;
  27. return 0;
  28. }
  29. static int proc_meminfo_open (struct shim_handle * hdl, const char * name,
  30. int flags)
  31. {
  32. if (flags & (O_WRONLY|O_RDWR))
  33. return -EACCES;
  34. int len, max = 128;
  35. char * str = NULL;
  36. struct { const char * fmt; unsigned long val; }
  37. meminfo[] = {
  38. { "MemTotal: %8lu kB\n", pal_control.mem_info.mem_total / 1024, },
  39. { "MemFree: %8lu kB\n", DkMemoryAvailableQuota() / 1024, },
  40. };
  41. retry:
  42. max *= 2;
  43. len = 0;
  44. free(str);
  45. str = malloc(max);
  46. if (!str)
  47. return -ENOMEM;
  48. for (int i = 0 ; i < sizeof(meminfo) / sizeof(meminfo[0]) ; i++) {
  49. int ret = snprintf(str + len, max - len, meminfo[i].fmt,
  50. meminfo[i].val);
  51. if (len + ret == max)
  52. goto retry;
  53. len += ret;
  54. }
  55. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  56. if (!data) {
  57. free(str);
  58. return -ENOMEM;
  59. }
  60. memset(data, 0, sizeof(struct shim_str_data));
  61. data->str = str;
  62. data->len = len;
  63. hdl->type = TYPE_STR;
  64. hdl->flags = flags & ~O_RDONLY;
  65. hdl->acc_mode = MAY_READ;
  66. hdl->info.str.data = data;
  67. return 0;
  68. }
  69. static int proc_cpuinfo_open (struct shim_handle * hdl, const char * name,
  70. int flags)
  71. {
  72. if (flags & (O_WRONLY|O_RDWR))
  73. return -EACCES;
  74. int len, max = 128;
  75. char * str = NULL;
  76. struct { const char * fmt; unsigned long val; }
  77. cpuinfo[] = {
  78. { "processor : %lu\n", 0, },
  79. { "vendor_id : %s\n", (unsigned long) pal_control.cpu_info.cpu_vendor, },
  80. { "cpu_family : %lu\n", pal_control.cpu_info.cpu_family, },
  81. { "model : %lu\n", pal_control.cpu_info.cpu_model, },
  82. { "model name : %s\n", (unsigned long) pal_control.cpu_info.cpu_brand, },
  83. { "stepping : %lu\n", pal_control.cpu_info.cpu_stepping, },
  84. { "core id : %lu\n", 0, },
  85. { "cpu_core : %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. };