sys_info.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "touchpad.h"
  14. #include "tpec.h"
  15. #include "usb.h"
  16. #include "usbhid.h"
  17. #include "vfs.h"
  18. _Static_assert(TPEC_TEMP_COUNT == SYS_TPEC_TEMP_MAX,
  19. "sys_tpec_sensors.temp_c and tpec_info.temp_c must match");
  20. _Static_assert(TPEC_BATTERY_COUNT == SYS_POWER_BATT_MAX,
  21. "sys_powerinfo.battery and tpec_power.battery must match");
  22. static void copy_str(char *dst, const char *src, unsigned int size)
  23. {
  24. unsigned int i = 0;
  25. while (i + 1 < size && src[i]) {
  26. dst[i] = src[i];
  27. i++;
  28. }
  29. dst[i] = '\0';
  30. }
  31. int sys_meminfo(struct sys_meminfo *out)
  32. {
  33. out->total_kb = mm_total_ram_kb();
  34. out->free_pages = mm_free_pages();
  35. out->page_size = PAGE_SIZE;
  36. out->e820_count = mm_e820_count();
  37. return 0;
  38. }
  39. int sys_e820(unsigned int index, struct sys_e820 *out)
  40. {
  41. const struct e820_entry *e = mm_e820_entry(index);
  42. if (!e)
  43. return 0;
  44. out->base = (unsigned int)e->base;
  45. out->length = (unsigned int)e->length;
  46. out->type = e->type;
  47. return 1;
  48. }
  49. int sys_mouseinfo(struct sys_mouseinfo *out)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < sizeof(*out); i++)
  53. ((unsigned char *)out)[i] = 0;
  54. out->present = mouse_is_present() ? 1 : 0;
  55. out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
  56. copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
  57. if (!out->present)
  58. return 0;
  59. out->ps2 = mouse_ps2_present() ? 1 : 0;
  60. if (out->ps2) {
  61. out->ps2_id = mouse_device_id() & 0xFFu;
  62. out->ps2_packet_size = mouse_packet_size();
  63. out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
  64. }
  65. out->usb = usbhid_mouse_attached() ? 1 : 0;
  66. if (out->usb) {
  67. unsigned int ep, mps;
  68. int low;
  69. usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
  70. out->usb_ep = ep;
  71. out->usb_mps = mps;
  72. out->usb_low = (unsigned int)low;
  73. copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
  74. sizeof(out->hcd));
  75. }
  76. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  77. out->touchpad = touchpad_present() ? 1 : 0;
  78. if (out->touchpad) {
  79. out->tp_vendor = touchpad_vendor();
  80. out->tp_product = touchpad_product();
  81. }
  82. return 1;
  83. }
  84. int sys_storage_info(struct sys_storage_info *out)
  85. {
  86. sys_fs_enter(); /* vfs_mount_all() reads disks */
  87. vfs_mount_all();
  88. sys_fs_exit();
  89. out->ahci_present = ahci_controller_present() ? 1 : 0;
  90. out->blkdev_count = blk_dev_count;
  91. return 0;
  92. }
  93. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  94. {
  95. const blk_dev_t *d;
  96. if (index >= blk_dev_count)
  97. return 0;
  98. d = &blk_devs[index];
  99. copy_str(out->name, d->name, sizeof(out->name));
  100. copy_str(out->model, d->model, sizeof(out->model));
  101. out->kind = (unsigned int)d->kind;
  102. out->present = d->present ? 1 : 0;
  103. out->total_sectors = d->total_sectors;
  104. return 1;
  105. }
  106. int sys_cputemp(struct sys_cputemp *out)
  107. {
  108. unsigned int i;
  109. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  110. "sys_cputemp.cpu_temp_c is too small");
  111. if (!out)
  112. return -1;
  113. out->temp_c = thermal_max_c();
  114. out->tjmax_c = thermal_tjmax();
  115. out->delta_c = -1;
  116. out->status = 0;
  117. if (thermal_status_this_cpu(&out->status) == 0)
  118. out->delta_c = (int)((out->status >> 16) & 0x7F);
  119. out->ncpus = cpu_count;
  120. for (i = 0; i < SYS_CPU_MAX; i++)
  121. out->cpu_temp_c[i] = cpus[i].temp_c;
  122. return 0;
  123. }
  124. int sys_sensors(struct sys_sensors *out)
  125. {
  126. struct it87_info info;
  127. unsigned int i;
  128. if (!out)
  129. return -1;
  130. it87_read(&info);
  131. out->present = info.present ? 1u : 0u;
  132. out->chip_id = info.chip_id;
  133. out->base = info.base;
  134. out->temp_enable = info.temp_enable;
  135. out->temp_extra = info.temp_extra;
  136. for (i = 0; i < 3; i++) {
  137. out->temp_c[i] = info.temp_c[i];
  138. out->fan_rpm[i] = info.fan_rpm[i];
  139. }
  140. return 0;
  141. }
  142. int sys_tpec_sensors(struct sys_tpec_sensors *out)
  143. {
  144. struct tpec_info info;
  145. unsigned int i;
  146. if (!out)
  147. return -1;
  148. tpec_read(&info);
  149. out->present = info.present ? 1u : 0u;
  150. copy_str(out->vendor, info.vendor, sizeof(out->vendor));
  151. copy_str(out->product, info.product, sizeof(out->product));
  152. copy_str(out->version, info.version, sizeof(out->version));
  153. for (i = 0; i < SYS_TPEC_TEMP_MAX; i++)
  154. out->temp_c[i] = info.temp_c[i];
  155. out->fan_rpm = info.fan_rpm;
  156. out->fan_level = info.fan_level;
  157. return 0;
  158. }
  159. int sys_powerinfo(struct sys_powerinfo *out, unsigned int flags)
  160. {
  161. struct tpec_power p;
  162. unsigned int i;
  163. if (!out)
  164. return -1;
  165. tpec_power_read(&p, (flags & SYS_POWER_F_DETAIL) != 0);
  166. for (i = 0; i < sizeof(*out); i++)
  167. ((unsigned char *)out)[i] = 0;
  168. out->present = p.present ? 1u : 0u;
  169. out->ac_online = p.ac_online;
  170. out->ec_rev = p.ec_rev;
  171. copy_str(out->vendor, p.vendor, sizeof(out->vendor));
  172. copy_str(out->product, p.product, sizeof(out->product));
  173. copy_str(out->version, p.version, sizeof(out->version));
  174. out->battery_count = p.battery_count;
  175. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  176. struct sys_battery *d = &out->battery[i];
  177. const struct tpec_battery *s = &p.battery[i];
  178. d->present = s->present ? 1u : 0u;
  179. d->error = s->error ? 1u : 0u;
  180. d->status = s->status;
  181. d->charging = s->charging ? 1u : 0u;
  182. d->discharging = s->discharging ? 1u : 0u;
  183. d->percent = s->percent > 0 ? (unsigned int)s->percent : 0u;
  184. d->percent_valid = s->percent_valid ? 1u : 0u;
  185. d->remaining = s->remaining;
  186. d->full = s->full;
  187. d->design = s->design;
  188. d->voltage_mv = s->voltage_mv;
  189. d->rate = s->rate;
  190. d->unit_known = s->unit_known ? 1u : 0u;
  191. d->unit_mwh = s->unit_mwh ? 1u : 0u;
  192. copy_str(d->manufacturer, s->manufacturer,
  193. sizeof(d->manufacturer));
  194. copy_str(d->model, s->model, sizeof(d->model));
  195. }
  196. return 0;
  197. }