se_memory.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _SE_MEMORY_H_
  32. #define _SE_MEMORY_H_
  33. #define _FILE_OFFSET_BITS 64
  34. #define _LARGEFILE64_SOURCE 1
  35. #include <unistd.h>
  36. #include <sys/mman.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #ifndef MEM_COMMIT
  40. #define MEM_COMMIT 0x1000
  41. #endif
  42. #ifndef MEM_RESERVE
  43. #define MEM_RESERVE 0x2000
  44. #endif
  45. #ifdef MEM_RELEASE
  46. #warning "MEM_RELEASE define conflict"
  47. #else
  48. #define MEM_RELEASE 0x8000
  49. #endif
  50. #ifdef MEM_DECOMMIT
  51. #warning "MEM_DECOMMIT define conflict"
  52. #else
  53. #define MEM_DECOMMIT 0x4000
  54. #endif
  55. #include "se_types.h"
  56. #include "arch.h"
  57. #include <stdlib.h>
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /*
  62. Reserves or commits a region of pages in the virtual address space of the calling process.
  63. Memory allocated by this function is automatically initialized to zero, unless MEM_RESET is specified.
  64. @address: the starting address of the region to allocate.
  65. @size: size of region in bytes.
  66. @type: Only MEM_COMMIT accepted.
  67. MEM_COMMIT - Allocates memory charges for the specified reserved memory pages.
  68. Actual physical pages are not allocated until the virtual addresses are actually accessed.
  69. The function initializes the memory to zero.
  70. @return value: If the function succeeds, the return value is the base address of the allocated region of pages.
  71. If the function fails, the return value is NULL.
  72. */
  73. void* se_virtual_alloc(void* address, size_t size, uint32_t type);
  74. /*
  75. Releases, decommits, or releases and decommits a region of pages within the virtual address space of the calling process.
  76. @address:A pointer to the base address of the region of pages to be freed. If the dwFreeType parameter is MEM_RELEASE,
  77. this parameter must be the base address returned by the se_virtual_alloc function when the region of pages is reserved.
  78. @size: The size of the region of memory to be freed, in bytes.
  79. @type: Only MEM_RELEASE accepted
  80. MEM_RELEASE - releases the specified region of pages. After this operation, the pages are in the free state.
  81. @return value:If the function succeeds, the return value is nonzero.If the function fails, the return value is zero.
  82. */
  83. int se_virtual_free(void* address, size_t size, uint32_t type);
  84. /*
  85. Locks the specified region of the process's virtual address space into physical memory, ensuring that subsequent access to the region will not incur a page fault.
  86. @address: A pointer to the base address of the region of pages to be locked.
  87. The region of affected pages includes all pages that contain one or more bytes in the range from the address parameter to (address+size).
  88. @size: The size of the region to be locked, in bytes.
  89. @return value: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
  90. */
  91. int se_virtual_lock(void* address, size_t size);
  92. /*
  93. Changes the protection on a region of committed pages in the virtual address space of the calling process.
  94. @address: A pointer an address that describes the starting page of the region of pages whose access protection attributes are to be changed.
  95. @size: The size of the region whose access protection attributes are to be changed, in bytes.
  96. @prot: The memory protection option. The option can be SI_FLAG_R, SI_FLAG_W, SI_FLAG_X.
  97. @return value: If the function succeeds, the return value is nonzero.If the function fails, the return value is zero.
  98. */
  99. #define SGX_PROT_NONE PROT_NONE
  100. int se_virtual_protect(void* address, size_t size, uint32_t prot);
  101. #include <sys/types.h>
  102. #include <sys/stat.h>
  103. #include <fcntl.h>
  104. #include <stdio.h>
  105. typedef pid_t se_proc_t;
  106. /*
  107. @return value: on success, return TRUE else return FALSE
  108. */
  109. se_proc_t get_self_proc(void);
  110. /*
  111. ** If the function succeeds, the return value is nonzero.
  112. ** If the function fails, the return value is zero.
  113. */
  114. int put_self_proc(se_proc_t proc);
  115. int se_read_process_mem(se_proc_t proc, void* base_addr, void* buffer, size_t size, size_t* read_nr);
  116. int se_write_process_mem(se_proc_t proc, void* base_addr, void* buffer, size_t size, size_t* write_ndr);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif