vm_page.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1993-1988 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * File: vm/vm_page.h
  28. * Author: Avadis Tevanian, Jr., Michael Wayne Young
  29. * Date: 1985
  30. *
  31. * Resident memory system definitions.
  32. */
  33. #ifndef _VM_VM_PAGE_H_
  34. #define _VM_VM_PAGE_H_
  35. #include <mach/boolean.h>
  36. #include <mach/vm_prot.h>
  37. #include <machine/vm_param.h>
  38. #include <vm/vm_object.h>
  39. #include <vm/vm_types.h>
  40. #include <kern/queue.h>
  41. #include <kern/list.h>
  42. #include <kern/lock.h>
  43. #include <kern/log2.h>
  44. #include <kern/macros.h>
  45. #include <kern/sched_prim.h> /* definitions of wait/wakeup */
  46. #if MACH_VM_DEBUG
  47. #include <mach_debug/hash_info.h>
  48. #endif
  49. /*
  50. * Management of resident (logical) pages.
  51. *
  52. * A small structure is kept for each resident
  53. * page, indexed by page number. Each structure
  54. * is an element of several lists:
  55. *
  56. * A hash table bucket used to quickly
  57. * perform object/offset lookups
  58. *
  59. * A list of all pages for a given object,
  60. * so they can be quickly deactivated at
  61. * time of deallocation.
  62. *
  63. * An ordered list of pages due for pageout.
  64. *
  65. * In addition, the structure contains the object
  66. * and offset to which this page belongs (for pageout),
  67. * and sundry status bits.
  68. *
  69. * Fields in this structure are locked either by the lock on the
  70. * object that the page belongs to (O) or by the lock on the page
  71. * queues (P). [Some fields require that both locks be held to
  72. * change that field; holding either lock is sufficient to read.]
  73. */
  74. struct vm_page {
  75. struct list node; /* page queues or free list (P) */
  76. unsigned short type;
  77. unsigned short seg_index;
  78. unsigned short order;
  79. void *priv;
  80. /*
  81. * This member is used throughout the code and may only change for
  82. * fictitious pages.
  83. */
  84. phys_addr_t phys_addr;
  85. queue_chain_t listq; /* all pages in same object (O) */
  86. struct vm_page *next; /* VP bucket link (O) */
  87. /* We use an empty struct as the delimiter. */
  88. struct {} vm_page_header;
  89. #define VM_PAGE_HEADER_SIZE offsetof(struct vm_page, vm_page_header)
  90. vm_object_t object; /* which object am I in (O,P) */
  91. vm_offset_t offset; /* offset into that object (O,P) */
  92. unsigned int wire_count:15, /* how many wired down maps use me?
  93. (O&P) */
  94. /* boolean_t */ inactive:1, /* page is in inactive list (P) */
  95. active:1, /* page is in active list (P) */
  96. laundry:1, /* page is being cleaned now (P)*/
  97. external_laundry:1, /* same as laundry for external pagers (P)*/
  98. free:1, /* page is on free list (P) */
  99. reference:1, /* page has been used (P) */
  100. external:1, /* page in external object (P) */
  101. busy:1, /* page is in transit (O) */
  102. wanted:1, /* someone is waiting for page (O) */
  103. tabled:1, /* page is in VP table (O) */
  104. fictitious:1, /* Physical page doesn't exist (O) */
  105. private:1, /* Page should not be returned to
  106. * the free list (O) */
  107. absent:1, /* Data has been requested, but is
  108. * not yet available (O) */
  109. error:1, /* Data manager was unable to provide
  110. * data due to error (O) */
  111. dirty:1, /* Page must be cleaned (O) */
  112. precious:1, /* Page is precious; data must be
  113. * returned even if clean (O) */
  114. overwriting:1; /* Request to unlock has been made
  115. * without having data. (O)
  116. * [See vm_object_overwrite] */
  117. vm_prot_t page_lock; /* Uses prohibited by data manager (O) */
  118. vm_prot_t unlock_request; /* Outstanding unlock request (O) */
  119. };
  120. /*
  121. * For debugging, this macro can be defined to perform
  122. * some useful check on a page structure.
  123. */
  124. #define VM_PAGE_CHECK(mem) vm_page_check(mem)
  125. void vm_page_check(const struct vm_page *page);
  126. /*
  127. * Each pageable resident page falls into one of three lists:
  128. *
  129. * free
  130. * Available for allocation now.
  131. * inactive
  132. * Not referenced in any map, but still has an
  133. * object/offset-page mapping, and may be dirty.
  134. * This is the list of pages that should be
  135. * paged out next.
  136. * active
  137. * A list of pages which have been placed in
  138. * at least one physical map. This list is
  139. * ordered, in LRU-like fashion.
  140. */
  141. extern
  142. int vm_page_fictitious_count;/* How many fictitious pages are free? */
  143. extern
  144. int vm_page_active_count; /* How many pages are active? */
  145. extern
  146. int vm_page_inactive_count; /* How many pages are inactive? */
  147. extern
  148. int vm_page_wire_count; /* How many pages are wired? */
  149. extern
  150. int vm_page_laundry_count; /* How many pages being laundered? */
  151. extern
  152. int vm_page_external_laundry_count; /* How many external pages being paged out? */
  153. decl_simple_lock_data(extern,vm_page_queue_lock)/* lock on active and inactive
  154. page queues */
  155. decl_simple_lock_data(extern,vm_page_queue_free_lock)
  156. /* lock on free page queue */
  157. extern phys_addr_t vm_page_fictitious_addr;
  158. /* (fake) phys_addr of fictitious pages */
  159. extern void vm_page_bootstrap(
  160. vm_offset_t *startp,
  161. vm_offset_t *endp);
  162. extern void vm_page_module_init(void);
  163. extern vm_page_t vm_page_lookup(
  164. vm_object_t object,
  165. vm_offset_t offset);
  166. extern vm_page_t vm_page_grab_fictitious(void);
  167. extern boolean_t vm_page_convert(vm_page_t *);
  168. extern void vm_page_more_fictitious(void);
  169. extern vm_page_t vm_page_grab(void);
  170. extern void vm_page_release(vm_page_t, boolean_t, boolean_t);
  171. extern phys_addr_t vm_page_grab_phys_addr(void);
  172. extern vm_page_t vm_page_grab_contig(vm_size_t, unsigned int);
  173. extern void vm_page_free_contig(vm_page_t, vm_size_t);
  174. extern void vm_page_wait(void (*)(void));
  175. extern vm_page_t vm_page_alloc(
  176. vm_object_t object,
  177. vm_offset_t offset);
  178. extern void vm_page_init(
  179. vm_page_t mem);
  180. extern void vm_page_free(vm_page_t);
  181. extern void vm_page_activate(vm_page_t);
  182. extern void vm_page_deactivate(vm_page_t);
  183. extern void vm_page_rename(
  184. vm_page_t mem,
  185. vm_object_t new_object,
  186. vm_offset_t new_offset);
  187. extern void vm_page_insert(
  188. vm_page_t mem,
  189. vm_object_t object,
  190. vm_offset_t offset);
  191. extern void vm_page_remove(
  192. vm_page_t mem);
  193. extern void vm_page_zero_fill(vm_page_t);
  194. extern void vm_page_copy(vm_page_t src_m, vm_page_t dest_m);
  195. extern void vm_page_wire(vm_page_t);
  196. extern void vm_page_unwire(vm_page_t);
  197. #if MACH_VM_DEBUG
  198. extern unsigned int vm_page_info(
  199. hash_info_bucket_t *info,
  200. unsigned int count);
  201. #endif
  202. /*
  203. * Functions implemented as macros
  204. */
  205. #define PAGE_ASSERT_WAIT(m, interruptible) \
  206. MACRO_BEGIN \
  207. (m)->wanted = TRUE; \
  208. assert_wait((event_t) (m), (interruptible)); \
  209. MACRO_END
  210. #define PAGE_WAKEUP_DONE(m) \
  211. MACRO_BEGIN \
  212. (m)->busy = FALSE; \
  213. if ((m)->wanted) { \
  214. (m)->wanted = FALSE; \
  215. thread_wakeup(((event_t) m)); \
  216. } \
  217. MACRO_END
  218. #define PAGE_WAKEUP(m) \
  219. MACRO_BEGIN \
  220. if ((m)->wanted) { \
  221. (m)->wanted = FALSE; \
  222. thread_wakeup((event_t) (m)); \
  223. } \
  224. MACRO_END
  225. #define VM_PAGE_FREE(p) \
  226. MACRO_BEGIN \
  227. vm_page_lock_queues(); \
  228. vm_page_free(p); \
  229. vm_page_unlock_queues(); \
  230. MACRO_END
  231. /*
  232. * Macro to be used in place of pmap_enter()
  233. */
  234. #define PMAP_ENTER(pmap, virtual_address, page, protection, wired) \
  235. MACRO_BEGIN \
  236. pmap_enter( \
  237. (pmap), \
  238. (virtual_address), \
  239. (page)->phys_addr, \
  240. (protection) & ~(page)->page_lock, \
  241. (wired) \
  242. ); \
  243. MACRO_END
  244. #define VM_PAGE_WAIT(continuation) vm_page_wait(continuation)
  245. #define vm_page_lock_queues() simple_lock(&vm_page_queue_lock)
  246. #define vm_page_unlock_queues() simple_unlock(&vm_page_queue_lock)
  247. #define VM_PAGE_QUEUES_REMOVE(mem) vm_page_queues_remove(mem)
  248. /*
  249. * Copyright (c) 2010-2014 Richard Braun.
  250. *
  251. * This program is free software: you can redistribute it and/or modify
  252. * it under the terms of the GNU General Public License as published by
  253. * the Free Software Foundation, either version 2 of the License, or
  254. * (at your option) any later version.
  255. *
  256. * This program is distributed in the hope that it will be useful,
  257. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  258. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  259. * GNU General Public License for more details.
  260. *
  261. * You should have received a copy of the GNU General Public License
  262. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  263. *
  264. *
  265. * Physical page management.
  266. */
  267. /*
  268. * Address/page conversion and rounding macros (not inline functions to
  269. * be easily usable on both virtual and physical addresses, which may not
  270. * have the same type size).
  271. */
  272. #define vm_page_atop(addr) ((addr) >> PAGE_SHIFT)
  273. #define vm_page_ptoa(page) ((page) << PAGE_SHIFT)
  274. #define vm_page_trunc(addr) P2ALIGN(addr, PAGE_SIZE)
  275. #define vm_page_round(addr) P2ROUND(addr, PAGE_SIZE)
  276. #define vm_page_aligned(addr) P2ALIGNED(addr, PAGE_SIZE)
  277. /*
  278. * Segment selectors.
  279. *
  280. * Selector-to-segment-list translation table :
  281. * DMA DMA
  282. * DMA32 DMA32 DMA
  283. * DIRECTMAP DIRECTMAP DMA32 DMA
  284. * HIGHMEM HIGHMEM DIRECTMAP DMA32 DMA
  285. */
  286. #define VM_PAGE_SEL_DMA 0
  287. #define VM_PAGE_SEL_DMA32 1
  288. #define VM_PAGE_SEL_DIRECTMAP 2
  289. #define VM_PAGE_SEL_HIGHMEM 3
  290. /*
  291. * Page usage types.
  292. */
  293. #define VM_PT_FREE 0 /* Page unused */
  294. #define VM_PT_RESERVED 1 /* Page reserved at boot time */
  295. #define VM_PT_TABLE 2 /* Page is part of the page table */
  296. #define VM_PT_KERNEL 3 /* Type for generic kernel allocations */
  297. static inline unsigned short
  298. vm_page_type(const struct vm_page *page)
  299. {
  300. return page->type;
  301. }
  302. void vm_page_set_type(struct vm_page *page, unsigned int order,
  303. unsigned short type);
  304. #if 0
  305. static inline unsigned int
  306. vm_page_order(size_t size)
  307. {
  308. return gnumach_iorder2(vm_page_atop(vm_page_round(size)));
  309. }
  310. #endif
  311. static inline phys_addr_t
  312. vm_page_to_pa(const struct vm_page *page)
  313. {
  314. return page->phys_addr;
  315. }
  316. /*
  317. * Associate private data with a page.
  318. */
  319. static inline void
  320. vm_page_set_priv(struct vm_page *page, void *priv)
  321. {
  322. page->priv = priv;
  323. }
  324. static inline void *
  325. vm_page_get_priv(const struct vm_page *page)
  326. {
  327. return page->priv;
  328. }
  329. /*
  330. * Load physical memory into the vm_page module at boot time.
  331. *
  332. * All addresses must be page-aligned. Segments can be loaded in any order.
  333. */
  334. void vm_page_load(unsigned int seg_index, phys_addr_t start, phys_addr_t end);
  335. /*
  336. * Load available physical memory into the vm_page module at boot time.
  337. *
  338. * The segment referred to must have been loaded with vm_page_load
  339. * before loading its heap.
  340. */
  341. void vm_page_load_heap(unsigned int seg_index, phys_addr_t start,
  342. phys_addr_t end);
  343. /*
  344. * Return true if the vm_page module is completely initialized, false
  345. * otherwise, in which case only vm_page_bootalloc() can be used for
  346. * allocations.
  347. */
  348. int vm_page_ready(void);
  349. /*
  350. * Early allocation function.
  351. *
  352. * This function is used by the vm_resident module to implement
  353. * pmap_steal_memory. It can be used after physical segments have been loaded
  354. * and before the vm_page module is initialized.
  355. */
  356. unsigned long vm_page_bootalloc(size_t size);
  357. /*
  358. * Set up the vm_page module.
  359. *
  360. * Architecture-specific code must have loaded segments before calling this
  361. * function. Segments must comply with the selector-to-segment-list table,
  362. * e.g. HIGHMEM is loaded if and only if DIRECTMAP, DMA32 and DMA are loaded,
  363. * notwithstanding segment aliasing.
  364. *
  365. * Once this function returns, the vm_page module is ready, and normal
  366. * allocation functions can be used.
  367. */
  368. void vm_page_setup(void);
  369. /*
  370. * Make the given page managed by the vm_page module.
  371. *
  372. * If additional memory can be made usable after the VM system is initialized,
  373. * it should be reported through this function.
  374. */
  375. void vm_page_manage(struct vm_page *page);
  376. /*
  377. * Return the page descriptor for the given physical address.
  378. */
  379. struct vm_page * vm_page_lookup_pa(phys_addr_t pa);
  380. /*
  381. * Allocate a block of 2^order physical pages.
  382. *
  383. * The selector is used to determine the segments from which allocation can
  384. * be attempted.
  385. *
  386. * This function should only be used by the vm_resident module.
  387. */
  388. struct vm_page * vm_page_alloc_pa(unsigned int order, unsigned int selector,
  389. unsigned short type);
  390. /*
  391. * Release a block of 2^order physical pages.
  392. *
  393. * This function should only be used by the vm_resident module.
  394. */
  395. void vm_page_free_pa(struct vm_page *page, unsigned int order);
  396. /*
  397. * Return the name of the given segment.
  398. */
  399. const char * vm_page_seg_name(unsigned int seg_index);
  400. /*
  401. * Display internal information about the module.
  402. */
  403. void vm_page_info_all(void);
  404. /*
  405. * Return the maximum physical address for a given segment selector.
  406. */
  407. phys_addr_t vm_page_seg_end(unsigned int selector);
  408. /*
  409. * Return the total number of physical pages.
  410. */
  411. unsigned long vm_page_table_size(void);
  412. /*
  413. * Return the index of a page in the page table.
  414. */
  415. unsigned long vm_page_table_index(phys_addr_t pa);
  416. /*
  417. * Return the total amount of physical memory.
  418. */
  419. phys_addr_t vm_page_mem_size(void);
  420. /*
  421. * Return the amount of free (unused) pages.
  422. *
  423. * XXX This currently relies on the kernel being non preemptible and
  424. * uniprocessor.
  425. */
  426. unsigned long vm_page_mem_free(void);
  427. /*
  428. * Remove the given page from any page queue it might be in.
  429. */
  430. void vm_page_queues_remove(struct vm_page *page);
  431. /*
  432. * Balance physical pages among segments.
  433. *
  434. * This function should be called first by the pageout daemon
  435. * on memory pressure, since it may be unnecessary to perform any
  436. * other operation, let alone shrink caches, if balancing is
  437. * enough to make enough free pages.
  438. *
  439. * Return TRUE if balancing made enough free pages for unprivileged
  440. * allocations to succeed, in which case pending allocations are resumed.
  441. *
  442. * This function acquires vm_page_queue_free_lock, which is held on return.
  443. */
  444. boolean_t vm_page_balance(void);
  445. /*
  446. * Evict physical pages.
  447. *
  448. * This function should be called by the pageout daemon after balancing
  449. * the segments and shrinking kernel caches.
  450. *
  451. * Return TRUE if eviction made enough free pages for unprivileged
  452. * allocations to succeed, in which case pending allocations are resumed.
  453. *
  454. * Otherwise, report whether the pageout daemon should wait (some pages
  455. * have been paged out) or not (only clean pages have been released).
  456. *
  457. * This function acquires vm_page_queue_free_lock, which is held on return.
  458. */
  459. boolean_t vm_page_evict(boolean_t *should_wait);
  460. /*
  461. * Turn active pages into inactive ones for second-chance LRU
  462. * approximation.
  463. *
  464. * This function should be called by the pageout daemon on memory pressure,
  465. * i.e. right before evicting pages.
  466. *
  467. * XXX This is probably not the best strategy, compared to keeping the
  468. * active/inactive ratio in check at all times, but this means less
  469. * frequent refills.
  470. */
  471. void vm_page_refill_inactive(void);
  472. #endif /* _VM_VM_PAGE_H_ */