kmem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2011-2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <kern/cpumap.h>
  21. #include <kern/init.h>
  22. #include <kern/macros.h>
  23. #include <machine/page.h>
  24. #include <machine/pmap.h>
  25. #include <machine/types.h>
  26. #include <vm/defs.h>
  27. #include <vm/kmem.h>
  28. #include <vm/map.h>
  29. #include <vm/object.h>
  30. #include <vm/page.h>
  31. static int __init
  32. vm_kmem_setup (void)
  33. {
  34. return (0);
  35. }
  36. INIT_OP_DEFINE (vm_kmem_setup,
  37. INIT_OP_DEP (pmap_bootstrap, true),
  38. INIT_OP_DEP (vm_map_bootstrap, true),
  39. INIT_OP_DEP (vm_object_bootstrap, true),
  40. INIT_OP_DEP (vm_page_setup, true));
  41. static bool
  42. vm_kmem_alloc_check (size_t size)
  43. {
  44. return (vm_page_aligned (size) && size != 0);
  45. }
  46. static bool
  47. vm_kmem_free_check (uintptr_t va, size_t size)
  48. {
  49. return (vm_page_aligned (va) && vm_kmem_alloc_check (size));
  50. }
  51. void*
  52. vm_kmem_alloc_va (size_t size)
  53. {
  54. assert (vm_kmem_alloc_check (size));
  55. uintptr_t va = 0;
  56. int flags = VM_MAP_FLAGS (VM_PROT_RDWR, VM_PROT_RDWR, VM_INHERIT_NONE,
  57. VM_ADV_DEFAULT, 0),
  58. error = vm_map_enter (vm_map_get_kernel_map (), &va, size,
  59. flags, NULL, 0);
  60. return (error ? NULL : (void *)va);
  61. }
  62. void
  63. vm_kmem_free_va (void *addr, size_t size)
  64. {
  65. uintptr_t va = (uintptr_t) addr;
  66. assert (vm_kmem_free_check (va, size));
  67. vm_map_remove (vm_map_get_kernel_map (), va, va + vm_page_round (size));
  68. }
  69. void*
  70. vm_kmem_alloc (size_t size)
  71. {
  72. size = vm_page_round (size);
  73. uint32_t order = vm_page_order (size);
  74. _Auto pages = vm_page_alloc (order, VM_PAGE_SEL_HIGHMEM,
  75. VM_PAGE_KERNEL, 0);
  76. if (! pages)
  77. return (NULL);
  78. uintptr_t va = 0;
  79. int flags = VM_MAP_FLAGS (VM_PROT_RDWR, VM_PROT_RDWR, VM_INHERIT_NONE,
  80. VM_ADV_DEFAULT, VM_MAP_PHYS),
  81. error = vm_map_enter (vm_map_get_kernel_map (), &va, size,
  82. flags, NULL, vm_page_to_pa (pages));
  83. if (error)
  84. {
  85. vm_page_free (pages, order, 0);
  86. return (NULL);
  87. }
  88. for (uint32_t i = 0; i < (1u << order); ++i)
  89. vm_page_init_refcount (pages + i);
  90. _Auto kernel_pmap = pmap_get_kernel_pmap ();
  91. for (uintptr_t start = va, end = va + size; start < end; start += PAGE_SIZE)
  92. {
  93. uint32_t idx = (uint32_t)((start - va) / PAGE_SIZE);
  94. error = pmap_enter (kernel_pmap, start, vm_page_to_pa (pages + idx),
  95. VM_PROT_RDWR, PMAP_PEF_GLOBAL | PMAP_SKIP_RSET);
  96. if (error)
  97. goto cleanup;
  98. }
  99. if (pmap_update (kernel_pmap) == 0)
  100. return ((void *)va);
  101. cleanup:
  102. for (uint32_t i = 0; i < (1u << order); ++i)
  103. vm_page_unref (pages + i);
  104. vm_map_remove (vm_map_get_kernel_map (), va, va + size);
  105. return (NULL);
  106. }
  107. void
  108. vm_kmem_free (void *addr, size_t size)
  109. {
  110. uintptr_t va = (uintptr_t)addr;
  111. _Auto kernel_map = vm_map_get_kernel_map ();
  112. _Auto entry = vm_map_find (kernel_map, va);
  113. if (! entry)
  114. return;
  115. _Auto pages = entry->pages;
  116. vm_map_remove (kernel_map, va, va + size);
  117. for (uint32_t i = 0; i < (1u << vm_page_order (size)); ++i)
  118. vm_page_unref (pages + i);
  119. }
  120. void*
  121. vm_kmem_map_pa (phys_addr_t pa, size_t size,
  122. uintptr_t *map_vap, size_t *map_sizep)
  123. {
  124. _Auto kernel_pmap = pmap_get_kernel_pmap ();
  125. phys_addr_t start = vm_page_trunc (pa);
  126. size_t map_size = vm_page_round (pa + size) - start;
  127. uintptr_t map_va = (uintptr_t) vm_kmem_alloc_va (map_size);
  128. if (! map_va)
  129. return (NULL);
  130. for (uintptr_t offset = 0; offset < map_size; offset += PAGE_SIZE)
  131. if (pmap_enter (kernel_pmap, map_va + offset, start + offset,
  132. VM_PROT_RDWR, PMAP_PEF_GLOBAL | PMAP_SKIP_RSET) != 0)
  133. goto error;
  134. if (pmap_update (kernel_pmap) != 0)
  135. goto error;
  136. if (map_vap != NULL)
  137. *map_vap = map_va;
  138. if (map_sizep != NULL)
  139. *map_sizep = map_size;
  140. return ((void *)(map_va + (uintptr_t)(pa & PAGE_MASK)));
  141. error:
  142. vm_kmem_unmap_pa (map_va, map_size);
  143. return (NULL);
  144. }
  145. void
  146. vm_kmem_unmap_pa (uintptr_t map_va, size_t map_size)
  147. {
  148. vm_kmem_free_va ((void *)map_va, map_size);
  149. }