info.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <asm/fcntl.h>
  8. #include <asm/mman.h>
  9. #include <asm/unistd.h>
  10. #include <asm/prctl.h>
  11. #include <errno.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_IFDIR;
  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. if (str) free(str);
  41. max *= 2;
  42. len = 0;
  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. cpuinfo[] = {
  76. { "processor : %lu\n", 0, },
  77. { "vendor_id : %s\n", (unsigned long) pal_control.cpu_info.cpu_vendor, },
  78. { "cpu_family : %lu\n", pal_control.cpu_info.cpu_family, },
  79. { "model : %lu\n", pal_control.cpu_info.cpu_model, },
  80. { "model name : %s\n", (unsigned long) pal_control.cpu_info.cpu_brand, },
  81. { "stepping : %lu\n", pal_control.cpu_info.cpu_stepping, },
  82. { "core id : %lu\n", 0, },
  83. { "cpu_core : %lu\n", pal_control.cpu_info.cpu_num, },
  84. };
  85. retry:
  86. if (str) free(str);
  87. max *= 2;
  88. len = 0;
  89. str = malloc(max);
  90. if (!str)
  91. return -ENOMEM;
  92. for (int n = 0 ; n < pal_control.cpu_info.cpu_num ; n++) {
  93. cpuinfo[0].val = n;
  94. cpuinfo[6].val = n;
  95. for (int i = 0 ; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]) ; i++) {
  96. int ret = snprintf(str + len, max - len, cpuinfo[i].fmt,
  97. cpuinfo[i].val);
  98. if (len + ret == max)
  99. goto retry;
  100. len += ret;
  101. }
  102. if (len >= max - 1)
  103. goto retry;
  104. str[len++] = '\n';
  105. str[len] = 0;
  106. }
  107. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  108. if (!data) {
  109. free(str);
  110. return -ENOMEM;
  111. }
  112. memset(data, 0, sizeof(struct shim_str_data));
  113. data->str = str;
  114. data->len = len;
  115. hdl->type = TYPE_STR;
  116. hdl->flags = flags & ~O_RDONLY;
  117. hdl->acc_mode = MAY_READ;
  118. hdl->info.str.data = data;
  119. return 0;
  120. }
  121. struct proc_fs_ops fs_meminfo = {
  122. .mode = &proc_info_mode,
  123. .stat = &proc_info_stat,
  124. .open = &proc_meminfo_open,
  125. };
  126. struct proc_fs_ops fs_cpuinfo = {
  127. .mode = &proc_info_mode,
  128. .stat = &proc_info_stat,
  129. .open = &proc_cpuinfo_open,
  130. };