/js/src/nanojit/VMPI.cpp

http://github.com/zpao/v8monkey · C++ · 153 lines · 98 code · 19 blank · 36 comment · 19 complexity · 1a407525aa87ef0ce1a09bec24dd7639 MD5 · raw file

  1. /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2. /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version 1.1 (the
  7. * "License"); you may not use this file except in compliance with the License. You may obtain
  8. * a copy of the License at http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
  11. * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
  12. * language governing rights and limitations under the License.
  13. *
  14. * The Original Code is [Open Source Virtual Machine.]
  15. *
  16. * The Initial Developer of the Original Code is Adobe System Incorporated. Portions created
  17. * by the Initial Developer are Copyright (C)[ 2004-2006 ] Adobe Systems Incorporated. All Rights
  18. * Reserved.
  19. *
  20. * Contributor(s): Adobe AS3 Team
  21. * Andreas Gal <gal@mozilla.com>
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of either the GNU
  24. * General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public
  25. * License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the
  26. * LGPL are applicable instead of those above. If you wish to allow use of your version of this
  27. * file only under the terms of either the GPL or the LGPL, and not to allow others to use your
  28. * version of this file under the terms of the MPL, indicate your decision by deleting provisions
  29. * above and replace them with the notice and other provisions required by the GPL or the
  30. * LGPL. If you do not delete the provisions above, a recipient may use your version of this file
  31. * under the terms of any one of the MPL, the GPL or the LGPL.
  32. *
  33. ***** END LICENSE BLOCK ***** */
  34. #include "nanojit.h"
  35. #ifdef SOLARIS
  36. typedef caddr_t maddr_ptr;
  37. #else
  38. typedef void *maddr_ptr;
  39. #endif
  40. using namespace avmplus;
  41. size_t
  42. VMPI_getVMPageSize()
  43. {
  44. return 4096;
  45. }
  46. #ifdef WIN32
  47. void
  48. VMPI_setPageProtection(void *address,
  49. size_t size,
  50. bool executableFlag,
  51. bool writeableFlag)
  52. {
  53. DWORD oldProtectFlags = 0;
  54. DWORD newProtectFlags = 0;
  55. if ( executableFlag && writeableFlag ) {
  56. newProtectFlags = PAGE_EXECUTE_READWRITE;
  57. } else if ( executableFlag ) {
  58. newProtectFlags = PAGE_EXECUTE_READ;
  59. } else if ( writeableFlag ) {
  60. newProtectFlags = PAGE_READWRITE;
  61. } else {
  62. newProtectFlags = PAGE_READONLY;
  63. }
  64. BOOL retval;
  65. MEMORY_BASIC_INFORMATION mbi;
  66. do {
  67. VirtualQuery(address, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
  68. size_t markSize = size > mbi.RegionSize ? mbi.RegionSize : size;
  69. retval = VirtualProtect(address, markSize, newProtectFlags, &oldProtectFlags);
  70. NanoAssert(retval);
  71. address = (char*) address + markSize;
  72. size -= markSize;
  73. } while(size > 0 && retval);
  74. // We should not be clobbering PAGE_GUARD protections
  75. NanoAssert((oldProtectFlags & PAGE_GUARD) == 0);
  76. }
  77. #elif defined(AVMPLUS_OS2)
  78. void
  79. VMPI_setPageProtection(void *address,
  80. size_t size,
  81. bool executableFlag,
  82. bool writeableFlag)
  83. {
  84. ULONG flags = PAG_READ;
  85. if (executableFlag) {
  86. flags |= PAG_EXECUTE;
  87. }
  88. if (writeableFlag) {
  89. flags |= PAG_WRITE;
  90. }
  91. address = (void*)((size_t)address & ~(0xfff));
  92. size = (size + 0xfff) & ~(0xfff);
  93. ULONG attribFlags = PAG_FREE;
  94. while (size) {
  95. ULONG attrib;
  96. ULONG range = size;
  97. ULONG retval = DosQueryMem(address, &range, &attrib);
  98. NanoAssert(retval == 0);
  99. // exit if this is the start of the next memory object
  100. if (attrib & attribFlags) {
  101. break;
  102. }
  103. attribFlags |= PAG_BASE;
  104. range = size > range ? range : size;
  105. retval = DosSetMem(address, range, flags);
  106. NanoAssert(retval == 0);
  107. address = (char*)address + range;
  108. size -= range;
  109. }
  110. }
  111. #else // !WIN32 && !AVMPLUS_OS2
  112. void VMPI_setPageProtection(void *address,
  113. size_t size,
  114. bool executableFlag,
  115. bool writeableFlag)
  116. {
  117. int bitmask = sysconf(_SC_PAGESIZE) - 1;
  118. // mprotect requires that the addresses be aligned on page boundaries
  119. void *endAddress = (void*) ((char*)address + size);
  120. void *beginPage = (void*) ((size_t)address & ~bitmask);
  121. void *endPage = (void*) (((size_t)endAddress + bitmask) & ~bitmask);
  122. size_t sizePaged = (size_t)endPage - (size_t)beginPage;
  123. int flags = PROT_READ;
  124. if (executableFlag) {
  125. flags |= PROT_EXEC;
  126. }
  127. if (writeableFlag) {
  128. flags |= PROT_WRITE;
  129. }
  130. int retval = mprotect((maddr_ptr)beginPage, (unsigned int)sizePaged, flags);
  131. NanoAssert(retval == 0);
  132. (void)retval;
  133. }
  134. #endif // WIN32