sys_mem.c 2.7 KB

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