1 /*
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 be
4  * found in the LICENSE file.
5  */
6 
7 /*
8  * NaCl Simple/secure ELF loader (NaCl SEL) misc utilities.  Inlined
9  * functions.  Internal; do not include.
10  */
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_
12 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_ 1
13 
14 #include "native_client/src/include/build_config.h"
15 
16 /*
17  * NaClRoundPage is a bit of a misnomer -- it always rounds up to a
18  * page size, not the nearest.
19  */
NaClRoundPage(size_t nbytes)20 static INLINE size_t  NaClRoundPage(size_t    nbytes) {
21   return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
22 }
23 
NaClRoundPage32(uint32_t nbytes)24 static INLINE uint32_t  NaClRoundPage32(uint32_t    nbytes) {
25   return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
26 }
27 
NaClRoundAllocPage(size_t nbytes)28 static INLINE size_t  NaClRoundAllocPage(size_t    nbytes) {
29   return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((size_t) NACL_MAP_PAGESIZE - 1);
30 }
31 
NaClRoundAllocPage32(uint32_t nbytes)32 static INLINE uint32_t  NaClRoundAllocPage32(uint32_t    nbytes) {
33   return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((uint32_t)NACL_MAP_PAGESIZE - 1);
34 }
35 
NaClTruncPage(size_t nbytes)36 static INLINE size_t NaClTruncPage(size_t  nbytes) {
37   return nbytes & ~((size_t) NACL_PAGESIZE - 1);
38 }
39 
NaClTruncAllocPage(size_t nbytes)40 static INLINE size_t NaClTruncAllocPage(size_t  nbytes) {
41   return nbytes & ~((size_t) NACL_MAP_PAGESIZE - 1);
42 }
43 
NaClBytesToPages(size_t nbytes)44 static INLINE size_t  NaClBytesToPages(size_t nbytes) {
45   return (nbytes + NACL_PAGESIZE - 1) >> NACL_PAGESHIFT;
46 }
47 
NaClIsPageMultiple(uintptr_t addr_or_size)48 static INLINE int /* bool */ NaClIsPageMultiple(uintptr_t addr_or_size) {
49   return 0 == ((NACL_PAGESIZE - 1) & addr_or_size);
50 }
51 
NaClIsAllocPageMultiple(uintptr_t addr_or_size)52 static INLINE int /* bool */ NaClIsAllocPageMultiple(uintptr_t addr_or_size) {
53   return 0 == ((NACL_MAP_PAGESIZE - 1) & addr_or_size);
54 }
55 
56 /*
57  * True host-OS allocation unit.
58  */
NaClRoundHostAllocPage(size_t nbytes)59 static INLINE size_t NaClRoundHostAllocPage(size_t  nbytes) {
60 #if NACL_WINDOWS
61   return NaClRoundAllocPage(nbytes);
62 #else   /* NACL_WINDOWS */
63   return NaClRoundPage(nbytes);
64 #endif  /* !NACL_WINDOWS */
65 }
66 
NaClRoundPageNumUpToMapMultiple(size_t npages)67 static INLINE size_t NaClRoundPageNumUpToMapMultiple(size_t npages) {
68   return (npages + NACL_PAGES_PER_MAP - 1) & ~((size_t) NACL_PAGES_PER_MAP - 1);
69 }
70 
NaClTruncPageNumDownToMapMultiple(size_t npages)71 static INLINE size_t NaClTruncPageNumDownToMapMultiple(size_t npages) {
72   return npages & ~((size_t) NACL_PAGES_PER_MAP - 1);
73 }
74 
75 #endif  /* NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_ */
76