map.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2011-2019 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. *
  18. * Virtual memory map management.
  19. */
  20. #ifndef VM_VM_MAP_H
  21. #define VM_VM_MAP_H
  22. #include <stdint.h>
  23. #include <kern/init.h>
  24. #include <kern/list.h>
  25. #include <kern/rbtree.h>
  26. #include <kern/stream.h>
  27. #include <kern/sxlock.h>
  28. #include <machine/pmap.h>
  29. #include <vm/defs.h>
  30. #include <vm/object.h>
  31. /*
  32. * Mapping flags.
  33. *
  34. * Unless otherwise mentioned, these can also be used as map entry flags.
  35. */
  36. #define VM_MAP_NOMERGE 0x010000
  37. #define VM_MAP_FIXED 0x020000 // Not an entry flag.
  38. #define VM_MAP_ANON 0x040000
  39. #define VM_MAP_PHYS 0x080000
  40. #define VM_MAP_CLEAN 0x100000
  41. // Macro used to forge "packed" flags.
  42. #define VM_MAP_FLAGS(prot, maxprot, inherit, advice, mapflags) \
  43. ((prot) | ((maxprot) << 4) | ((inherit) << 8) | \
  44. ((advice) << 12) | (mapflags))
  45. /*
  46. * Flags usable as map entry flags.
  47. *
  48. * Map entry flags also use the packed format.
  49. */
  50. #define VM_MAP_ENTRY_MASK \
  51. (VM_MAP_NOMERGE | VM_MAP_ANON | VM_MAP_PHYS | VM_MAP_CLEAN | 0xffff)
  52. // Macros used to extract specific properties out of packed flags.
  53. #define VM_MAP_PROT(flags) ((flags) & 0xf)
  54. #define VM_MAP_MAXPROT(flags) (((flags) & 0xf0) >> 4)
  55. #define VM_MAP_INHERIT(flags) (((flags) & 0xf00) >> 8)
  56. #define VM_MAP_ADVICE(flags) (((flags) & 0xf000) >> 12)
  57. // Macros used to set specific properties in packed flags.
  58. #define VM_MAP_SET_PROP(flagp, val, mask, shift) \
  59. do \
  60. { \
  61. _Auto flagp_ = (flagp); \
  62. *flagp_ = (*flagp_ & ~(mask)) | ((val) << (shift)); \
  63. } \
  64. while (0)
  65. #define VM_MAP_SET_PROT(flagp, prot) VM_MAP_SET_PROP (flagp, prot, 0xf, 0)
  66. #define VM_MAP_SET_MAXPROT(flagp, prot) \
  67. VM_MAP_SET_PROP (flagp, prot, 0xf0, 4)
  68. #define VM_MAP_SET_INHERIT(flagp, x) VM_MAP_SET_PROP (flagp, x, 0xf00, 8)
  69. #define VM_MAP_SET_ADVICE(flagp, x) VM_MAP_SET_PROP (flagp, x, 0xf000, 12)
  70. // Maximum number of frames to allocate when faulting in pages.
  71. #define VM_MAP_MAX_FRAMES_ORDER 3
  72. #define VM_MAP_MAX_FRAMES (1 << VM_MAP_MAX_FRAMES_ORDER)
  73. // Memory range descriptor.
  74. struct vm_map_entry
  75. {
  76. struct list list_node;
  77. struct rbtree_node tree_node;
  78. uintptr_t start;
  79. uintptr_t end;
  80. struct vm_object *object;
  81. union
  82. {
  83. uint64_t offset;
  84. struct vm_page *pages;
  85. };
  86. int flags;
  87. };
  88. // Memory map.
  89. struct vm_map
  90. {
  91. struct sxlock lock;
  92. struct list entry_list;
  93. struct rbtree entry_tree;
  94. uint32_t nr_entries;
  95. uintptr_t start;
  96. uintptr_t end;
  97. size_t size;
  98. struct vm_map_entry *lookup_cache;
  99. struct vm_object *priv_cache;
  100. struct pmap *pmap;
  101. uint32_t soft_faults;
  102. uint32_t hard_faults;
  103. };
  104. struct ipc_vme_iter;
  105. static inline struct vm_map*
  106. vm_map_get_kernel_map (void)
  107. {
  108. extern struct vm_map vm_map_kernel_map;
  109. return (&vm_map_kernel_map);
  110. }
  111. // Get the current task's VM map.
  112. #define vm_map_self() ((struct vm_map *)thread_self()->xtask->map)
  113. // Create a virtual mapping.
  114. int vm_map_enter (struct vm_map *map, uintptr_t *startp, size_t size,
  115. int flags, struct vm_object *object, uint64_t offset);
  116. // Remove mappings from start to end.
  117. int vm_map_remove (struct vm_map *map, uintptr_t start, uintptr_t end);
  118. // Create a VM map.
  119. int vm_map_create (struct vm_map **mapp);
  120. // Create a fork of a VM map.
  121. int vm_map_fork (struct vm_map **mapp, struct vm_map *src);
  122. /*
  123. * Lookup an entry in a VM map.
  124. *
  125. * Note that the returned entry may not contain the address, and instead
  126. * compare strictly less than it. The VM object of the entry is referenced
  127. * prior to returning to guarantee its existence, so normally 'vm_map_entry_put'
  128. * must be called on the entry.
  129. */
  130. int vm_map_lookup (struct vm_map *map, uintptr_t addr,
  131. struct vm_map_entry *entry);
  132. // Put back a previously returned entry.
  133. static inline void
  134. vm_map_entry_put (struct vm_map_entry *entry)
  135. {
  136. struct vm_object *obj = entry->object;
  137. if (obj)
  138. vm_object_unref (obj);
  139. }
  140. // Same as 'vm_map_lookup', only more ergonomic.
  141. #define vm_map_find(map, addr) \
  142. ({ \
  143. struct vm_map_entry *e_ = __builtin_alloca (sizeof (*e_)); \
  144. vm_map_lookup ((map), (addr), e_) == 0 ? e_ : NULL; \
  145. })
  146. // Handle a page fault. Interrupts must be disabled when calling this function.
  147. int vm_map_fault (struct vm_map *map, uintptr_t addr, int prot);
  148. // Destroy a VM map.
  149. void vm_map_destroy (struct vm_map *map);
  150. // Allocate anonymous memory in a VM map.
  151. int vm_map_anon_alloc (void **outp, struct vm_map *map, size_t size);
  152. // Change the protection of a memory range.
  153. int vm_map_protect (struct vm_map *map, uintptr_t start,
  154. uintptr_t end, int prot);
  155. // Transfer VMEs between a remote and the local VM map.
  156. int vm_map_iter_copy (struct vm_map *r_map, struct ipc_vme_iter *r_it,
  157. struct ipc_vme_iter *l_it, uint32_t flags);
  158. // Reply to a page request message.
  159. int vm_map_reply_pagereq (const uintptr_t *src, uint32_t cnt,
  160. struct vm_page **pages);
  161. // Display information about a memory map.
  162. void vm_map_info (struct vm_map *map, struct stream *stream);
  163. /*
  164. * This init operation provides :
  165. * - kernel mapping operations
  166. */
  167. INIT_OP_DECLARE (vm_map_bootstrap);
  168. /*
  169. * This init operation provides :
  170. * - VM map creation
  171. * - module fully initialized
  172. */
  173. INIT_OP_DECLARE (vm_map_setup);
  174. #endif