sys_info.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Kernel-state info for the info commands (meminfo/mouseinfo/storage).
  2. * Small, read-only snapshots; storage additionally refreshes the mount cache,
  3. * which reads disks and therefore runs under the fs-busy gate (see sys_fs.c). */
  4. #include "syscall.h"
  5. #include "ahci.h"
  6. #include "blkdev.h"
  7. #include "it87.h"
  8. #include "mm.h"
  9. #include "mouse.h"
  10. #include "sched.h"
  11. #include "sys_fs.h"
  12. #include "thermal.h"
  13. #include "tpec.h"
  14. #include "usb.h"
  15. #include "usbhid.h"
  16. #include "vfs.h"
  17. _Static_assert(TPEC_TEMP_COUNT == SYS_TPEC_TEMP_MAX,
  18. "sys_tpec_sensors.temp_c and tpec_info.temp_c must match");
  19. static void copy_str(char *dst, const char *src, unsigned int size)
  20. {
  21. unsigned int i = 0;
  22. while (i + 1 < size && src[i]) {
  23. dst[i] = src[i];
  24. i++;
  25. }
  26. dst[i] = '\0';
  27. }
  28. int sys_meminfo(struct sys_meminfo *out)
  29. {
  30. out->total_kb = mm_total_ram_kb();
  31. out->free_pages = mm_free_pages();
  32. out->page_size = PAGE_SIZE;
  33. out->e820_count = mm_e820_count();
  34. return 0;
  35. }
  36. int sys_e820(unsigned int index, struct sys_e820 *out)
  37. {
  38. const struct e820_entry *e = mm_e820_entry(index);
  39. if (!e)
  40. return 0;
  41. out->base = (unsigned int)e->base;
  42. out->length = (unsigned int)e->length;
  43. out->type = e->type;
  44. return 1;
  45. }
  46. int sys_mouseinfo(struct sys_mouseinfo *out)
  47. {
  48. unsigned int i;
  49. for (i = 0; i < sizeof(*out); i++)
  50. ((unsigned char *)out)[i] = 0;
  51. out->present = mouse_is_present() ? 1 : 0;
  52. out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
  53. copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
  54. if (!out->present)
  55. return 0;
  56. out->ps2 = mouse_ps2_present() ? 1 : 0;
  57. if (out->ps2) {
  58. out->ps2_id = mouse_device_id() & 0xFFu;
  59. out->ps2_packet_size = mouse_packet_size();
  60. out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
  61. }
  62. out->usb = usbhid_mouse_attached() ? 1 : 0;
  63. if (out->usb) {
  64. unsigned int ep, mps;
  65. int low;
  66. usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
  67. out->usb_ep = ep;
  68. out->usb_mps = mps;
  69. out->usb_low = (unsigned int)low;
  70. copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
  71. sizeof(out->hcd));
  72. }
  73. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  74. return 1;
  75. }
  76. int sys_storage_info(struct sys_storage_info *out)
  77. {
  78. sys_fs_enter(); /* vfs_mount_all() reads disks */
  79. vfs_mount_all();
  80. sys_fs_exit();
  81. out->ahci_present = ahci_controller_present() ? 1 : 0;
  82. out->blkdev_count = blk_dev_count;
  83. return 0;
  84. }
  85. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  86. {
  87. const blk_dev_t *d;
  88. if (index >= blk_dev_count)
  89. return 0;
  90. d = &blk_devs[index];
  91. copy_str(out->name, d->name, sizeof(out->name));
  92. copy_str(out->model, d->model, sizeof(out->model));
  93. out->kind = (unsigned int)d->kind;
  94. out->present = d->present ? 1 : 0;
  95. out->total_sectors = d->total_sectors;
  96. return 1;
  97. }
  98. int sys_cputemp(struct sys_cputemp *out)
  99. {
  100. unsigned int i;
  101. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  102. "sys_cputemp.cpu_temp_c is too small");
  103. if (!out)
  104. return -1;
  105. out->temp_c = thermal_max_c();
  106. out->tjmax_c = thermal_tjmax();
  107. out->delta_c = -1;
  108. out->status = 0;
  109. if (thermal_status_this_cpu(&out->status) == 0)
  110. out->delta_c = (int)((out->status >> 16) & 0x7F);
  111. out->ncpus = cpu_count;
  112. for (i = 0; i < SYS_CPU_MAX; i++)
  113. out->cpu_temp_c[i] = cpus[i].temp_c;
  114. return 0;
  115. }
  116. int sys_sensors(struct sys_sensors *out)
  117. {
  118. struct it87_info info;
  119. unsigned int i;
  120. if (!out)
  121. return -1;
  122. it87_read(&info);
  123. out->present = info.present ? 1u : 0u;
  124. out->chip_id = info.chip_id;
  125. out->base = info.base;
  126. out->temp_enable = info.temp_enable;
  127. out->temp_extra = info.temp_extra;
  128. for (i = 0; i < 3; i++) {
  129. out->temp_c[i] = info.temp_c[i];
  130. out->fan_rpm[i] = info.fan_rpm[i];
  131. }
  132. return 0;
  133. }
  134. int sys_tpec_sensors(struct sys_tpec_sensors *out)
  135. {
  136. struct tpec_info info;
  137. unsigned int i;
  138. if (!out)
  139. return -1;
  140. tpec_read(&info);
  141. out->present = info.present ? 1u : 0u;
  142. copy_str(out->vendor, info.vendor, sizeof(out->vendor));
  143. copy_str(out->product, info.product, sizeof(out->product));
  144. copy_str(out->version, info.version, sizeof(out->version));
  145. for (i = 0; i < SYS_TPEC_TEMP_MAX; i++)
  146. out->temp_c[i] = info.temp_c[i];
  147. out->fan_rpm = info.fan_rpm;
  148. out->fan_level = info.fan_level;
  149. return 0;
  150. }