map.h 5.5 KB

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