paging.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * KDUMA - Kernel Mode Red-Zone memory allocator.
  3. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  4. * Copyright (C) 2006 Eric Rachner <eric@rachner.us>
  5. * Copyright (C) 2002-2005 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  6. * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  7. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. /* $Id$ */
  25. #ifndef DUMA_PAGING_H
  26. #define DUMA_PAGING_H
  27. #include "log.h"
  28. /*
  29. * Lots of systems are missing the definition of PROT_NONE.
  30. */
  31. #ifndef PROT_NONE
  32. #define PROT_NONE 0
  33. #endif
  34. #ifndef size_t
  35. #define size_t unsigned long
  36. #endif
  37. static caddr_t startAddr = (caddr_t) 0;
  38. /* Function: mprotectFailed
  39. *
  40. * Report that VirtualProtect or mprotect failed and abort
  41. * program execution.
  42. */
  43. static void mprotectFailed(void)
  44. {
  45. DUMA_Abort("mprotect() failed: %s", stringErrorReport());
  46. }
  47. /* Function: Page_Create
  48. *
  49. * Create memory. Allocates actual memory. Uses
  50. * VirtualAlloc on windows and mmap on unix.
  51. *
  52. * flags - Passed along from kmalloc (GFP_ATOMIC and the like)
  53. *
  54. * See Also:
  55. * <Page_Delete>
  56. */
  57. static void *
  58. Page_Create(size_t size, int exitonfail, int printerror, int flags)
  59. {
  60. caddr_t allocation;
  61. unsigned long numPages = size/DUMA_PAGE_SIZE;
  62. if( size % DUMA_AGE_SIZE )
  63. numPages++;
  64. unsigned long order = ilog2(numPages);
  65. allocation = __get_free_pages(flags, order);
  66. if ( allocation == 0 )
  67. {
  68. if ( exitonfail )
  69. DUMA_Abort("__get_fre_pages(%d, %d) failed: %s", flags, order, stringErrorReport());
  70. else if ( printerror )
  71. DUMA_Print("\nDUMA warning: __get_fre_pages(%d, %d) failed: %s", flags, order, stringErrorReport());
  72. }
  73. return (void *)allocation;
  74. }
  75. /* Function: Page_AllowAccess
  76. *
  77. * Allow memory access to allocated memory.
  78. *
  79. * See Also:
  80. * <Page_DenyAccess>
  81. */
  82. void Page_AllowAccess(void * address, size_t size)
  83. {
  84. if ( mprotect((caddr_t)address, size, PROT_READ|PROT_WRITE) < 0 )
  85. mprotectFailed();
  86. #endif
  87. }
  88. /* Function: Page_DenyAccess
  89. *
  90. * Deny access to allocated memory region.
  91. *
  92. * See Also:
  93. * <Page_AllowAccess>
  94. */
  95. static void Page_DenyAccess(void * address, size_t size)
  96. {
  97. if ( mprotect((caddr_t)address, size, PROT_NONE) < 0 )
  98. mprotectFailed();
  99. }
  100. extern struct _DUMA_Slot;
  101. /* Function: Page_Delete
  102. *
  103. * Free's DUMA allocated memory. This is the real deal, make sure
  104. * the page is no longer in our slot list first!
  105. *
  106. * See Also:
  107. * <Page_Create>
  108. */
  109. static void Page_Delete(void * address, size_t size)
  110. {
  111. unsigned long numPages = size/DUMA_PAGE_SIZE;
  112. if( size % DUMA_AGE_SIZE )
  113. numPages++;
  114. unsigned long order = ilog2(numPages);
  115. __free_pages(address, size);
  116. }
  117. /* Function: Page_Size
  118. *
  119. * Retrieve page size.
  120. */
  121. static size_t
  122. Page_Size(void)
  123. {
  124. return getpagesize();
  125. }
  126. #endif /* DUMA_PAGING_H */