Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2009 The Native Client Authors. All rights reserved. | 2 * Copyright 2009 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "native_client/src/include/nacl_platform.h" | 7 #include "native_client/src/include/nacl_platform.h" |
| 8 #include "native_client/src/shared/platform/nacl_check.h" | 8 #include "native_client/src/shared/platform/nacl_check.h" |
| 9 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" | 9 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" |
| 10 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | 10 #include "native_client/src/trusted/service_runtime/sel_ldr.h" |
| 11 #include "native_client/src/trusted/service_runtime/sel_memory.h" | 11 #include "native_client/src/trusted/service_runtime/sel_memory.h" |
| 12 | 12 |
| 13 | 13 |
| 14 #define POST_ADDR_SPACE_GUARD_SIZE (2 * NACL_PAGESIZE) | |
| 15 | |
| 16 /* | 14 /* |
| 17 * On ARM, we cheat slightly: we add two pages to the requested allocation! | 15 * On ARM, we cheat slightly: we add two pages to the requested allocation! |
| 18 * This accomodates the guard region we require at the top end of untrusted | 16 * This accomodates the guard region we require at the top end of untrusted |
| 19 * memory. | 17 * memory. |
| 20 */ | 18 */ |
| 19 #define POST_ADDR_SPACE_GUARD_SIZE (2 * NACL_PAGESIZE) | |
| 20 | |
| 21 /* NOTE: This routine is almost identical to the x86_32 version. | |
| 22 */ | |
| 21 NaClErrorCode NaClAllocateSpace(void **mem, size_t addrsp_size) { | 23 NaClErrorCode NaClAllocateSpace(void **mem, size_t addrsp_size) { |
| 22 CHECK(mem); | 24 #if NACL_LINUX |
| 25 const void *ONE_MEGABYTE = (void *)(1024*1024); | |
| 26 #endif | |
| 27 int result; | |
| 28 | |
| 29 CHECK(NULL != mem); | |
| 23 | 30 |
| 24 addrsp_size += POST_ADDR_SPACE_GUARD_SIZE; | 31 addrsp_size += POST_ADDR_SPACE_GUARD_SIZE; |
| 32 #if NACL_LINUX | |
| 33 /* | |
| 34 * When creating a zero-based sandbox, we do not allocate the first 64K of | |
| 35 * pages beneath the trampolines, because -- on Linux at least -- we cannot. | |
| 36 * Instead, we allocate starting at the trampolines, and then coerce the | |
| 37 * "mem" out parameter. | |
| 38 */ | |
| 25 addrsp_size -= NACL_TRAMPOLINE_START; | 39 addrsp_size -= NACL_TRAMPOLINE_START; |
| 26 | 40 /* |
| 27 *mem = (void *) NACL_TRAMPOLINE_START; | 41 * On 32 bit Linux, a 1 gigabyte block of address space may be reserved at |
| 28 if (NaCl_page_alloc_at_addr(mem, addrsp_size) != 0) { | 42 * the zero-end of the address space during process creation, to address |
| 43 * sandbox layout requirements on ARM and performance issues on Intel ATOM. | |
| 44 * Look for this pre-reserved block and if found, pass its address to the | |
| 45 * page allocation function. | |
| 46 */ | |
| 47 if (NaCl_find_prereserved_sandbox_memory(mem, addrsp_size)) { | |
| 48 /* Sanity check zero sandbox base address. | |
| 49 * It should be within a few pages above the 64KB boundary. See | |
| 50 * chrome/nacl/nacl_helper_bootstrap.c in the Chromium repository | |
| 51 * for more details. | |
| 52 */ | |
| 53 if (0 == *mem || ONE_MEGABYTE < *mem) { | |
| 54 NaClLog(LOG_ERROR, "NaClAllocateSpace:" | |
| 55 "Can't handle sandbox at high address" | |
|
bsy
2011/08/23 21:34:20
" Can't handle"
" 0x%08"...
so words in format st
Brad Chen
2011/08/23 22:29:52
Done.
| |
| 56 "0x%08"NACL_PRIxPTR"\n", | |
| 57 (uintptr_t)*mem); | |
| 58 return LOAD_NO_MEMORY; | |
| 59 } | |
| 60 } else { | |
| 61 /* Zero-based sandbox not pre-reserved. Try anyways. | |
| 62 * TODO(bradchen): delete once new code is working. | |
| 63 */ | |
| 64 *mem = (void *) NACL_TRAMPOLINE_START; | |
| 65 } | |
| 66 result = NaCl_page_alloc_at_addr(mem, addrsp_size); | |
| 67 *mem = 0; | |
| 68 #else | |
| 69 # error "I only know how to allocate memory for ARM on Linux." | |
| 70 #endif | |
| 71 if (0 != result) { | |
| 29 NaClLog(2, | 72 NaClLog(2, |
| 30 "NaClAllocateSpace: NaCl_page_alloc_at_addr 0x%08"NACL_PRIxPTR | 73 "NaClAllocateSpace: NaCl_page_alloc_at_addr 0x%08"NACL_PRIxPTR |
| 31 " failed\n", | 74 " failed\n", |
| 32 (uintptr_t) *mem); | 75 (uintptr_t) *mem); |
| 33 return LOAD_NO_MEMORY; | 76 return LOAD_NO_MEMORY; |
| 34 } | 77 } |
| 35 NaClLog(4, "NaClAllocateSpace: %"NACL_PRIxPTR", %"NACL_PRIxS"\n", | 78 NaClLog(4, "NaClAllocateSpace: %"NACL_PRIxPTR", %"NACL_PRIxS"\n", |
| 36 (uintptr_t) *mem, | 79 (uintptr_t) *mem, |
| 37 addrsp_size); | 80 addrsp_size); |
| 38 | 81 |
| 39 /* | |
| 40 * makes sel_ldr think that the module's address space is at 0x0, this where | |
| 41 * it should be | |
| 42 */ | |
| 43 *mem = 0x0; | |
| 44 | |
| 45 return LOAD_OK; | 82 return LOAD_OK; |
| 46 } | 83 } |
| 47 | 84 |
| 48 /* | 85 /* |
| 49 * In the ARM sandboxing scheme we put the NaCl module at low virtual | 86 * In the ARM sandboxing scheme we put the NaCl module at low virtual |
| 50 * address -- and thus there can only ever be one running NaCl app per | 87 * address -- and thus there can only ever be one running NaCl app per |
| 51 * sel_ldr process -- to simplify the address masking etc needed for | 88 * sel_ldr process -- to simplify the address masking etc needed for |
| 52 * the sandboxing. All memory below ((uintptr_t) 1) << nap->addr_bits | 89 * the sandboxing. All memory below ((uintptr_t) 1) << nap->addr_bits |
| 53 * is accessible to the NaCl app, and modulo page protection, | 90 * is accessible to the NaCl app, and modulo page protection, |
| 54 * potentially writable. Page protection is, of course, used to | 91 * potentially writable. Page protection is, of course, used to |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 66 * address 0x0, so we mmap it at the start of a trampoline region. | 103 * address 0x0, so we mmap it at the start of a trampoline region. |
| 67 * Therefore, there is not need to mprotect at the start_addr. | 104 * Therefore, there is not need to mprotect at the start_addr. |
| 68 * | 105 * |
| 69 * However, we do create a vmmap entry to describe it. | 106 * However, we do create a vmmap entry to describe it. |
| 70 */ | 107 */ |
| 71 NaClLog(3, | 108 NaClLog(3, |
| 72 ("NULL detection region start 0x%08"NACL_PRIxPTR", " | 109 ("NULL detection region start 0x%08"NACL_PRIxPTR", " |
| 73 "size 0x%08x, end 0x%08"NACL_PRIxPTR"\n"), | 110 "size 0x%08x, end 0x%08"NACL_PRIxPTR"\n"), |
| 74 0, NACL_SYSCALL_START_ADDR, | 111 0, NACL_SYSCALL_START_ADDR, |
| 75 NACL_SYSCALL_START_ADDR); | 112 NACL_SYSCALL_START_ADDR); |
| 113 | |
| 76 if (!NaClVmmapAdd(&nap->mem_map, | 114 if (!NaClVmmapAdd(&nap->mem_map, |
| 77 nap->mem_start >> NACL_PAGESHIFT, | 115 nap->mem_start >> NACL_PAGESHIFT, |
| 78 NACL_SYSCALL_START_ADDR >> NACL_PAGESHIFT, | 116 NACL_SYSCALL_START_ADDR >> NACL_PAGESHIFT, |
| 79 PROT_NONE, | 117 PROT_NONE, |
| 80 (struct NaClMemObj *) NULL)) { | 118 (struct NaClMemObj *) NULL)) { |
| 81 NaClLog(LOG_ERROR, ("NaClMemoryProtection: NaClVmmapAdd failed" | 119 NaClLog(LOG_ERROR, ("NaClMemoryProtection: NaClVmmapAdd failed" |
| 82 " (NULL pointer guard page)\n")); | 120 " (NULL pointer guard page)\n")); |
| 83 return LOAD_MPROTECT_FAIL; | 121 return LOAD_MPROTECT_FAIL; |
| 84 } | 122 } |
| 85 | 123 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 103 * NB: the pages just mapped are OUTSIDE of the address space of the | 141 * NB: the pages just mapped are OUTSIDE of the address space of the |
| 104 * NaCl module. We should not track them in the Vmmap structure, | 142 * NaCl module. We should not track them in the Vmmap structure, |
| 105 * since that's to track addressable memory for mapping and unmapping. | 143 * since that's to track addressable memory for mapping and unmapping. |
| 106 * | 144 * |
| 107 * This means that because these pages are implicit and not tracked, | 145 * This means that because these pages are implicit and not tracked, |
| 108 * we should have a hook to tear down these pages as part of the | 146 * we should have a hook to tear down these pages as part of the |
| 109 * NaClApp dtor. | 147 * NaClApp dtor. |
| 110 */ | 148 */ |
| 111 return LOAD_OK; | 149 return LOAD_OK; |
| 112 } | 150 } |
| OLD | NEW |