sys_info.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "usb.h"
  14. #include "usbhid.h"
  15. #include "vfs.h"
  16. static void copy_str(char *dst, const char *src, unsigned int size)
  17. {
  18. unsigned int i = 0;
  19. while (i + 1 < size && src[i]) {
  20. dst[i] = src[i];
  21. i++;
  22. }
  23. dst[i] = '\0';
  24. }
  25. int sys_meminfo(struct sys_meminfo *out)
  26. {
  27. out->total_kb = mm_total_ram_kb();
  28. out->free_pages = mm_free_pages();
  29. out->page_size = PAGE_SIZE;
  30. out->e820_count = mm_e820_count();
  31. return 0;
  32. }
  33. int sys_e820(unsigned int index, struct sys_e820 *out)
  34. {
  35. const struct e820_entry *e = mm_e820_entry(index);
  36. if (!e)
  37. return 0;
  38. out->base = (unsigned int)e->base;
  39. out->length = (unsigned int)e->length;
  40. out->type = e->type;
  41. return 1;
  42. }
  43. int sys_mouseinfo(struct sys_mouseinfo *out)
  44. {
  45. unsigned int i;
  46. for (i = 0; i < sizeof(*out); i++)
  47. ((unsigned char *)out)[i] = 0;
  48. out->present = mouse_is_present() ? 1 : 0;
  49. out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
  50. copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
  51. if (!out->present)
  52. return 0;
  53. out->ps2 = mouse_ps2_present() ? 1 : 0;
  54. if (out->ps2) {
  55. out->ps2_id = mouse_device_id() & 0xFFu;
  56. out->ps2_packet_size = mouse_packet_size();
  57. out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
  58. }
  59. out->usb = usbhid_mouse_attached() ? 1 : 0;
  60. if (out->usb) {
  61. unsigned int ep, mps;
  62. int low;
  63. usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
  64. out->usb_ep = ep;
  65. out->usb_mps = mps;
  66. out->usb_low = (unsigned int)low;
  67. copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
  68. sizeof(out->hcd));
  69. }
  70. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  71. return 1;
  72. }
  73. int sys_storage_info(struct sys_storage_info *out)
  74. {
  75. sys_fs_enter(); /* vfs_mount_all() reads disks */
  76. vfs_mount_all();
  77. sys_fs_exit();
  78. out->ahci_present = ahci_controller_present() ? 1 : 0;
  79. out->blkdev_count = blk_dev_count;
  80. return 0;
  81. }
  82. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  83. {
  84. const blk_dev_t *d;
  85. if (index >= blk_dev_count)
  86. return 0;
  87. d = &blk_devs[index];
  88. copy_str(out->name, d->name, sizeof(out->name));
  89. copy_str(out->model, d->model, sizeof(out->model));
  90. out->kind = (unsigned int)d->kind;
  91. out->present = d->present ? 1 : 0;
  92. out->total_sectors = d->total_sectors;
  93. return 1;
  94. }
  95. int sys_cputemp(struct sys_cputemp *out)
  96. {
  97. unsigned int i;
  98. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  99. "sys_cputemp.cpu_temp_c is too small");
  100. if (!out)
  101. return -1;
  102. out->temp_c = thermal_max_c();
  103. out->tjmax_c = thermal_tjmax();
  104. out->delta_c = -1;
  105. out->status = 0;
  106. if (thermal_status_this_cpu(&out->status) == 0)
  107. out->delta_c = (int)((out->status >> 16) & 0x7F);
  108. out->ncpus = cpu_count;
  109. for (i = 0; i < SYS_CPU_MAX; i++)
  110. out->cpu_temp_c[i] = cpus[i].temp_c;
  111. return 0;
  112. }
  113. int sys_sensors(struct sys_sensors *out)
  114. {
  115. struct it87_info info;
  116. unsigned int i;
  117. if (!out)
  118. return -1;
  119. it87_read(&info);
  120. out->present = info.present ? 1u : 0u;
  121. out->chip_id = info.chip_id;
  122. out->base = info.base;
  123. out->temp_enable = info.temp_enable;
  124. out->temp_extra = info.temp_extra;
  125. for (i = 0; i < 3; i++) {
  126. out->temp_c[i] = info.temp_c[i];
  127. out->fan_rpm[i] = info.fan_rpm[i];
  128. }
  129. return 0;
  130. }