Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * NaCl Service Runtime memory allocation code | 8 * NaCl Service Runtime memory allocation code |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <sys/mman.h> | 11 #include <sys/mman.h> |
| 12 #include <sys/stat.h> | 12 #include <sys/stat.h> |
| 13 #include <sys/types.h> | 13 #include <sys/types.h> |
| 14 | 14 |
| 15 #include <dlfcn.h> | |
| 15 #include <errno.h> | 16 #include <errno.h> |
| 16 #include <fcntl.h> | 17 #include <fcntl.h> |
| 17 #include <stdint.h> | 18 #include <stdint.h> |
| 18 #include <stdio.h> | 19 #include <stdio.h> |
| 19 #include <string.h> | 20 #include <string.h> |
| 20 #include <unistd.h> | 21 #include <unistd.h> |
| 21 | 22 |
| 22 #include "native_client/src/include/nacl_platform.h" | 23 #include "native_client/src/include/nacl_platform.h" |
| 23 #include "native_client/src/include/portability.h" | 24 #include "native_client/src/include/portability.h" |
| 24 #include "native_client/src/shared/platform/nacl_exit.h" | 25 #include "native_client/src/shared/platform/nacl_exit.h" |
| 25 #include "native_client/src/shared/platform/nacl_global_secure_random.h" | 26 #include "native_client/src/shared/platform/nacl_global_secure_random.h" |
| 26 #include "native_client/src/shared/platform/nacl_log.h" | 27 #include "native_client/src/shared/platform/nacl_log.h" |
| 27 #include "native_client/src/trusted/service_runtime/sel_memory.h" | 28 #include "native_client/src/trusted/service_runtime/sel_memory.h" |
| 28 #include "native_client/src/trusted/service_runtime/nacl_config.h" | 29 #include "native_client/src/trusted/service_runtime/nacl_config.h" |
| 29 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" | 30 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" |
| 30 | 31 |
| 31 | 32 |
| 33 /* | |
| 34 * Find sandbox memory pre-reserved by the nacl_helper in chrome. The | |
| 35 * nacl_helper, if present, reserves the bottom 1G of the address space | |
| 36 * for use by Native Client. | |
| 37 * | |
| 38 * NOTE: num_bytes is currently ignored. It should be 1GB on Linux and | |
| 39 * 1GB plus a few pages on ARM. TODO(bradchen): deal with num_bytes. | |
| 40 * | |
| 41 * Out parameter p should be either: | |
| 42 * 0: reserved memory was not found | |
| 43 * less than 128K: indicates the bottom 1G was reserved. | |
| 44 */ | |
| 45 int NaCl_find_prereserved_sandbox_memory(void **p, | |
| 46 size_t num_bytes) { | |
| 47 typedef uintptr_t (base_addr_func)(); | |
| 48 void* nacl_helper_so = dlopen(NULL, RTLD_LAZY | RTLD_NOLOAD); | |
|
Mark Seaborn
2011/08/18 23:09:45
" *" style
Brad Chen
2011/08/19 00:24:35
Done.
| |
| 49 base_addr_func* nacl_helper_get_base_addr; | |
|
Mark Seaborn
2011/08/18 23:09:45
" *" style
Brad Chen
2011/08/19 00:24:35
Done.
| |
| 50 uintptr_t tmpint; | |
| 51 uintptr_t base_addr; | |
| 52 | |
| 53 NaClLog(2, "NaCl_find_preserved_sandbox_memory(p, 0x%08"NACL_PRIxPTR")\n", | |
| 54 num_bytes); | |
| 55 *p = 0; | |
| 56 if (!nacl_helper_so) return 0; | |
|
Mark Seaborn
2011/08/18 23:09:45
Prefer:
if (x) {
blah
}
Brad Chen
2011/08/19 00:24:35
Done.
| |
| 57 tmpint = (uintptr_t)dlsym(nacl_helper_so, "nacl_helper_get_1G_address"); | |
| 58 nacl_helper_get_base_addr = (base_addr_func*)tmpint; | |
| 59 | |
| 60 if (!nacl_helper_get_base_addr) return 0; | |
| 61 base_addr = nacl_helper_get_base_addr(); | |
| 62 if (base_addr == 0) return 0; | |
| 63 NaClLog(2, "NaCl_find_preserved_sandbox_memory() at 0x%08"NACL_PRIxPTR"\n", | |
| 64 base_addr); | |
| 65 *p = (void *)base_addr; | |
| 66 return 1; | |
| 67 } | |
| 68 | |
| 32 void NaCl_page_free(void *p, | 69 void NaCl_page_free(void *p, |
| 33 size_t size) { | 70 size_t size) { |
| 34 if (p == 0 || size == 0) | 71 if (p == 0 || size == 0) |
| 35 return; | 72 return; |
| 36 if (munmap(p, size) != 0) { | 73 if (munmap(p, size) != 0) { |
| 37 NaClLog(LOG_FATAL, "NaCl_page_free: munmap() failed"); | 74 NaClLog(LOG_FATAL, "NaCl_page_free: munmap() failed"); |
| 38 } | 75 } |
| 39 } | 76 } |
| 40 | 77 |
| 41 /* | 78 /* |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 int NaCl_madvise(void *start, | 224 int NaCl_madvise(void *start, |
| 188 size_t length, | 225 size_t length, |
| 189 int advice) { | 226 int advice) { |
| 190 int ret = madvise(start, length, advice); | 227 int ret = madvise(start, length, advice); |
| 191 | 228 |
| 192 /* | 229 /* |
| 193 * MADV_DONTNEED and MADV_NORMAL are needed | 230 * MADV_DONTNEED and MADV_NORMAL are needed |
| 194 */ | 231 */ |
| 195 return ret == -1 ? -errno : ret; | 232 return ret == -1 ? -errno : ret; |
| 196 } | 233 } |
| OLD | NEW |