sys_mem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. {
  21. unsigned int i;
  22. for (i = from; i < to; i++) {
  23. unsigned int phys = mm_alloc_page();
  24. unsigned int va = USER_HEAP_BASE + i * PAGE_SIZE;
  25. if (!phys)
  26. break;
  27. zero_page(phys);
  28. if (!paging_map_page(p->cr3, va, phys,
  29. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  30. mm_free_page(phys);
  31. break;
  32. }
  33. invlpg_va(va);
  34. }
  35. if (i < to) {
  36. if (i > from)
  37. paging_release_user_range(p->cr3,
  38. USER_HEAP_BASE + from * PAGE_SIZE,
  39. (i - from) * PAGE_SIZE, 1);
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. int sys_mem_alloc(unsigned int bytes)
  45. {
  46. struct process *p = current_proc();
  47. unsigned int pages;
  48. if (!p || !p->cr3 || bytes == 0 || p->heap_pages != 0)
  49. return -1;
  50. if (bytes > USER_HEAP_MAX)
  51. return -1;
  52. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  53. if (pages == 0 || pages > USER_HEAP_MAX / PAGE_SIZE)
  54. return -1;
  55. if (map_pages(p, 0, pages))
  56. return -1;
  57. p->heap_pages = pages;
  58. return (int)USER_HEAP_BASE;
  59. }
  60. int sys_mem_grow(unsigned int bytes)
  61. {
  62. struct process *p = current_proc();
  63. unsigned int pages;
  64. if (!p || !p->cr3 || bytes == 0 || bytes > USER_HEAP_MAX)
  65. return -1;
  66. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  67. if (pages <= p->heap_pages)
  68. return 0; /* shrinking is deliberately unsupported */
  69. if (pages > USER_HEAP_MAX / PAGE_SIZE)
  70. return -1;
  71. if (map_pages(p, p->heap_pages, pages))
  72. return -1;
  73. p->heap_pages = pages;
  74. return 0;
  75. }
  76. int sys_mem_free(void)
  77. {
  78. struct process *p = current_proc();
  79. if (!p || !p->cr3 || !p->heap_pages)
  80. return -1;
  81. paging_release_user_range(p->cr3, USER_HEAP_BASE,
  82. p->heap_pages * PAGE_SIZE, 1);
  83. p->heap_pages = 0;
  84. return 0;
  85. }
  86. void sys_mem_release(struct process *p)
  87. {
  88. if (!p || !p->cr3 || !p->heap_pages)
  89. return;
  90. paging_release_user_range(p->cr3, USER_HEAP_BASE,
  91. p->heap_pages * PAGE_SIZE, 1);
  92. p->heap_pages = 0;
  93. }