sys_info.c 5.5 KB

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