sys_mem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Per-process user heap: see sys_mem.h. */
  2. #include "sys_mem.h"
  3. #include "mm.h"
  4. #include "paging.h"
  5. #include "process.h"
  6. static void invlpg_va(unsigned int va)
  7. {
  8. __asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
  9. }
  10. static void zero_page(unsigned int phys)
  11. {
  12. unsigned int *p = (unsigned int *)phys;
  13. unsigned int i;
  14. for (i = 0; i < PAGE_SIZE / 4; i++)
  15. p[i] = 0;
  16. }
  17. /* Map new pages [from, to) of the arena. On failure release only the pages
  18. * mapped by this call; the caller's existing arena is untouched. */
  19. static int map_pages(struct process *p, unsigned int from, unsigned int to,
  20. int zero)
  21. {
  22. unsigned int i;
  23. for (i = from; i < to; i++) {
  24. unsigned int phys = mm_alloc_page();
  25. unsigned int va = USER_HEAP_BASE + i * PAGE_SIZE;
  26. if (!phys)
  27. break;
  28. if (zero)
  29. zero_page(phys);
  30. if (!paging_map_page(p->cr3, va, phys,
  31. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  32. mm_free_page(phys);
  33. break;
  34. }
  35. invlpg_va(va);
  36. }
  37. if (i < to) {
  38. if (i > from)
  39. paging_release_user_range(p->cr3,
  40. USER_HEAP_BASE + from * PAGE_SIZE,
  41. (i - from) * PAGE_SIZE, 1);
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. int sys_mem_alloc(unsigned int bytes)
  47. {
  48. struct process *p = current_proc();
  49. unsigned int pages;
  50. if (!p || !p->cr3 || bytes == 0 || p->heap_pages != 0)
  51. return -1;
  52. if (bytes > USER_HEAP_MAX)
  53. return -1;
  54. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  55. if (pages == 0 || pages > USER_HEAP_MAX / PAGE_SIZE)
  56. return -1;
  57. if (map_pages(p, 0, pages, 1))
  58. return -1;
  59. p->heap_pages = pages;
  60. return (int)USER_HEAP_BASE;
  61. }
  62. int sys_mem_grow(unsigned int bytes)
  63. {
  64. struct process *p = current_proc();
  65. unsigned int pages;
  66. if (!p || !p->cr3 || bytes == 0 || bytes > USER_HEAP_MAX)
  67. return -1;
  68. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  69. if (pages <= p->heap_pages)
  70. return 0; /* shrinking is deliberately unsupported */
  71. if (pages > USER_HEAP_MAX / PAGE_SIZE)
  72. return -1;
  73. /* Growth is normally used for a decode arena; the decoder fills every
  74. * byte it consumes. Skip the zero pass, which dominates the grow
  75. * time on a large image. The initial allocation above is zeroed. */
  76. if (map_pages(p, p->heap_pages, pages, 0))
  77. return -1;
  78. p->heap_pages = pages;
  79. return 0;
  80. }
  81. int sys_mem_free(void)
  82. {
  83. struct process *p = current_proc();
  84. if (!p || !p->cr3 || !p->heap_pages)
  85. return -1;
  86. paging_release_user_range(p->cr3, USER_HEAP_BASE,
  87. p->heap_pages * PAGE_SIZE, 1);
  88. p->heap_pages = 0;
  89. return 0;
  90. }
  91. void sys_mem_release(struct process *p)
  92. {
  93. if (!p || !p->cr3 || !p->heap_pages)
  94. return;
  95. paging_release_user_range(p->cr3, USER_HEAP_BASE,
  96. p->heap_pages * PAGE_SIZE, 1);
  97. p->heap_pages = 0;
  98. }