vm_external.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989 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. * This module maintains information about the presence of
  28. * pages not in memory. Since an external memory object
  29. * must maintain a complete knowledge of its contents, this
  30. * information takes the form of hints.
  31. */
  32. #include <mach/boolean.h>
  33. #include <kern/slab.h>
  34. #include <vm/vm_external.h>
  35. #include <mach/vm_param.h>
  36. #include <kern/assert.h>
  37. #include <string.h>
  38. boolean_t vm_external_unsafe = FALSE;
  39. struct kmem_cache vm_external_cache;
  40. /*
  41. * The implementation uses bit arrays to record whether
  42. * a page has been written to external storage. For
  43. * convenience, these bit arrays come in two sizes
  44. * (measured in bytes).
  45. */
  46. #define SMALL_SIZE (VM_EXTERNAL_SMALL_SIZE/8)
  47. #define LARGE_SIZE (VM_EXTERNAL_LARGE_SIZE/8)
  48. struct kmem_cache vm_object_small_existence_map_cache;
  49. struct kmem_cache vm_object_large_existence_map_cache;
  50. vm_external_t vm_external_create(vm_offset_t size)
  51. {
  52. vm_external_t result;
  53. vm_size_t bytes;
  54. result = (vm_external_t) kmem_cache_alloc(&vm_external_cache);
  55. result->existence_map = (char *) 0;
  56. bytes = (atop(size) + 07) >> 3;
  57. if (bytes <= SMALL_SIZE) {
  58. result->existence_map =
  59. (char *) kmem_cache_alloc(&vm_object_small_existence_map_cache);
  60. result->existence_size = SMALL_SIZE;
  61. } else {
  62. result->existence_map =
  63. (char *) kmem_cache_alloc(&vm_object_large_existence_map_cache);
  64. result->existence_size = LARGE_SIZE;
  65. }
  66. memset (result->existence_map, 0, result->existence_size);
  67. return(result);
  68. }
  69. void vm_external_destroy(vm_external_t e)
  70. {
  71. if (e == VM_EXTERNAL_NULL)
  72. return;
  73. if (e->existence_map != (char *) 0) {
  74. if (e->existence_size <= SMALL_SIZE) {
  75. kmem_cache_free(&vm_object_small_existence_map_cache,
  76. (vm_offset_t) e->existence_map);
  77. } else {
  78. kmem_cache_free(&vm_object_large_existence_map_cache,
  79. (vm_offset_t) e->existence_map);
  80. }
  81. }
  82. kmem_cache_free(&vm_external_cache, (vm_offset_t) e);
  83. }
  84. vm_external_state_t _vm_external_state_get(e, offset)
  85. const vm_external_t e;
  86. vm_offset_t offset;
  87. {
  88. unsigned
  89. int bit, byte;
  90. if (vm_external_unsafe ||
  91. (e == VM_EXTERNAL_NULL) ||
  92. (e->existence_map == (char *) 0))
  93. return(VM_EXTERNAL_STATE_UNKNOWN);
  94. bit = atop(offset);
  95. byte = bit >> 3;
  96. if (byte >= e->existence_size) return (VM_EXTERNAL_STATE_UNKNOWN);
  97. return( (e->existence_map[byte] & (1 << (bit & 07))) ?
  98. VM_EXTERNAL_STATE_EXISTS : VM_EXTERNAL_STATE_ABSENT );
  99. }
  100. void vm_external_state_set(
  101. vm_external_t e,
  102. vm_offset_t offset,
  103. vm_external_state_t state)
  104. {
  105. unsigned
  106. int bit, byte;
  107. if ((e == VM_EXTERNAL_NULL) || (e->existence_map == (char *) 0))
  108. return;
  109. if (state != VM_EXTERNAL_STATE_EXISTS)
  110. return;
  111. bit = atop(offset);
  112. byte = bit >> 3;
  113. if (byte >= e->existence_size) return;
  114. e->existence_map[byte] |= (1 << (bit & 07));
  115. }
  116. void vm_external_module_initialize(void)
  117. {
  118. vm_size_t size = (vm_size_t) sizeof(struct vm_external);
  119. kmem_cache_init(&vm_external_cache, "vm_external", size, 0,
  120. NULL, 0);
  121. kmem_cache_init(&vm_object_small_existence_map_cache,
  122. "small_existence_map", SMALL_SIZE, 0,
  123. NULL, 0);
  124. kmem_cache_init(&vm_object_large_existence_map_cache,
  125. "large_existence_map", LARGE_SIZE, 0,
  126. NULL, 0);
  127. }