mem.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* meminfo command: report total/free physical memory and the E820 map. */
  2. #include "io.h"
  3. #include "mm.h"
  4. static void put_dec(unsigned int n)
  5. {
  6. char buf[12];
  7. unsigned int i = sizeof(buf) - 1;
  8. buf[i] = '\0';
  9. if (n == 0) {
  10. io_putchar('0');
  11. return;
  12. }
  13. while (n && i > 0) {
  14. buf[--i] = (char)('0' + (n % 10));
  15. n /= 10;
  16. }
  17. io_putstr(buf + i);
  18. }
  19. void program_mem_main(void)
  20. {
  21. unsigned int total_kb = mm_total_ram_kb();
  22. unsigned int free_pages = mm_free_pages();
  23. unsigned int free_kb = free_pages * (PAGE_SIZE / 1024);
  24. unsigned int n = mm_e820_count();
  25. unsigned int i;
  26. io_putstr("memory: ");
  27. put_dec(total_kb);
  28. io_putstr(" KB total, ");
  29. put_dec(free_kb);
  30. io_putstr(" KB free (");
  31. put_dec(free_pages);
  32. io_putstr(" pages)\n");
  33. if (n == 0) {
  34. io_putstr("E820 map unavailable\n");
  35. return;
  36. }
  37. io_putstr("E820 map (");
  38. put_dec(n);
  39. io_putstr(" entries):\n");
  40. for (i = 0; i < n; i++) {
  41. const struct e820_entry *e = mm_e820_entry(i);
  42. io_putstr(" [0x");
  43. io_puthex((unsigned int)e->base);
  44. io_putstr(" +0x");
  45. io_puthex((unsigned int)e->length);
  46. io_putstr("] type ");
  47. put_dec(e->type);
  48. io_putchar('\n');
  49. }
  50. }