sys_info.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(), sizeof(out->hcd));
  68. }
  69. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  70. return 1;
  71. }
  72. int sys_storage_info(struct sys_storage_info *out)
  73. {
  74. sys_fs_enter(); /* vfs_mount_all() reads disks */
  75. vfs_mount_all();
  76. sys_fs_exit();
  77. out->ahci_present = ahci_controller_present() ? 1 : 0;
  78. out->blkdev_count = blk_dev_count;
  79. return 0;
  80. }
  81. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  82. {
  83. const blk_dev_t *d;
  84. if (index >= blk_dev_count)
  85. return 0;
  86. d = &blk_devs[index];
  87. copy_str(out->name, d->name, sizeof(out->name));
  88. copy_str(out->model, d->model, sizeof(out->model));
  89. out->kind = (unsigned int)d->kind;
  90. out->present = d->present ? 1 : 0;
  91. out->total_sectors = d->total_sectors;
  92. return 1;
  93. }
  94. int sys_cputemp(struct sys_cputemp *out)
  95. {
  96. unsigned int i;
  97. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  98. "sys_cputemp.cpu_temp_c is too small");
  99. if (!out)
  100. return -1;
  101. out->temp_c = thermal_max_c();
  102. out->tjmax_c = thermal_tjmax();
  103. out->delta_c = -1;
  104. out->status = 0;
  105. if (thermal_status_this_cpu(&out->status) == 0)
  106. out->delta_c = (int)((out->status >> 16) & 0x7F);
  107. out->ncpus = cpu_count;
  108. for (i = 0; i < SYS_CPU_MAX; i++)
  109. out->cpu_temp_c[i] = cpus[i].temp_c;
  110. return 0;
  111. }
  112. int sys_sensors(struct sys_sensors *out)
  113. {
  114. struct it87_info info;
  115. unsigned int i;
  116. if (!out)
  117. return -1;
  118. it87_read(&info);
  119. out->present = info.present ? 1u : 0u;
  120. out->chip_id = info.chip_id;
  121. out->base = info.base;
  122. out->temp_enable = info.temp_enable;
  123. out->temp_extra = info.temp_extra;
  124. for (i = 0; i < 3; i++) {
  125. out->temp_c[i] = info.temp_c[i];
  126. out->fan_rpm[i] = info.fan_rpm[i];
  127. }
  128. return 0;
  129. }