2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl local descriptor table manipulation support.
40 #include "native_client/service_runtime/nacl_ldt.h"
41 #include "native_client/service_runtime/nacl_sync.h"
44 * The modify_ldt system call is used to get and set the local descriptor
47 extern int modify_ldt(int func
, void* ptr
, unsigned long bytecount
);
50 * A static helper for finding unused LDT entry.
52 static int NaClFindUnusedEntryNumber();
54 /* struct LdtEntry is a structure that is laid out exactly as the segment
55 * descriptors described in the Intel reference manuals. This is needed
56 * because Mac and Windows use this representation in the methods used to
57 * set and get entries in the local descriptor table (LDT), but use different
58 * names for the members. Linux uses a different representation to set, but
59 * also reads entries back in this format. It needs to be laid out packed,
60 * bitwise, little endian.
63 uint16_t limit_00to15
;
65 // NOTE: the bitfields are using a gcc extension switching
66 // to "unsigned:<n>" is NOT straight forward
69 uint8_t descriptor_privilege
: 2;
71 uint8_t limit_16to19
: 4;
72 uint8_t available
: 1;
73 uint8_t code_64_bit
: 1;
74 uint8_t op_size_32
: 1;
75 uint8_t granularity
: 1;
80 * The module initializer and finalizer set up a mutex used to guard LDT
83 static struct NaClMutex nacl_ldt_mutex
;
85 int NaClLdtInitPlatformSpecific() {
86 return NaClMutexCtor(&nacl_ldt_mutex
);
89 void NaClLdtFiniPlatformSpecific() {
90 NaClMutexDtor(&nacl_ldt_mutex
);
94 * Find and allocate an available selector, inserting an LDT entry with the
95 * appropriate permissions.
97 uint16_t NaClLdtAllocateSelector(int32_t entry_number
,
99 NaClLdtDescriptorType type
,
102 uint32_t size_minus_one
) {
106 NaClMutexLock(&nacl_ldt_mutex
);
108 if (-1 == entry_number
) {
109 /* -1 means caller did not specify -- allocate */
110 entry_number
= NaClFindUnusedEntryNumber();
112 if (-1 == entry_number
) {
114 * No free entries were available.
119 ud
.entry_number
= entry_number
;
122 case NACL_LDT_DESCRIPTOR_DATA
:
123 ud
.contents
= MODIFY_LDT_CONTENTS_DATA
;
125 case NACL_LDT_DESCRIPTOR_CODE
:
126 ud
.contents
= MODIFY_LDT_CONTENTS_CODE
;
131 ud
.read_exec_only
= read_exec_only
;
133 ud
.seg_not_present
= 0;
136 if (size_is_in_pages
&& ((unsigned long) base_addr
& 0xfff)) {
138 * The base address needs to be page aligned.
142 ud
.base_addr
= (unsigned long) base_addr
;
144 if (size_minus_one
> 0xfffff) {
146 * If size is in pages, no more than 2**20 pages can be protected.
147 * If size is in bytes, no more than 2**20 bytes can be protected.
151 ud
.limit
= size_minus_one
;
152 ud
.limit_in_pages
= size_is_in_pages
;
155 * Install the LDT entry.
157 retval
= modify_ldt(1, &ud
, sizeof ud
);
163 * Return an LDT selector with a requested privilege level of 3.
165 NaClMutexUnlock(&nacl_ldt_mutex
);
166 return (ud
.entry_number
<< 3) | 0x7;
169 * All error returns go through this epilog.
172 NaClMutexUnlock(&nacl_ldt_mutex
);
177 * Allocates a selector whose size is specified in pages.
178 * Page granular protection requires that the start address be page-aligned.
180 uint16_t NaClLdtAllocatePageSelector(NaClLdtDescriptorType type
,
183 uint32_t size_in_pages
) {
184 return NaClLdtAllocateSelector(-1, 1, type
, read_exec_only
, base_addr
,
189 * Allocates a selector whose size is specified in bytes.
191 uint16_t NaClLdtAllocateByteSelector(NaClLdtDescriptorType type
,
194 uint32_t size_in_bytes
) {
195 return NaClLdtAllocateSelector(-1, 0, type
, read_exec_only
, base_addr
,
200 * Change a selector whose size is specified in pages.
201 * Page granular protection requires that the start address be page-aligned.
203 uint16_t NaClLdtChangePageSelector(int32_t entry_number
,
204 NaClLdtDescriptorType type
,
207 uint32_t size_in_pages
) {
208 if ((uint32_t) entry_number
>= LDT_ENTRIES
) {
211 return NaClLdtAllocateSelector(entry_number
, 1, type
, read_exec_only
,
212 base_addr
, size_in_pages
- 1);
216 * Change a selector whose size is specified in bytes.
218 uint16_t NaClLdtChangeByteSelector(int32_t entry_number
,
219 NaClLdtDescriptorType type
,
222 uint32_t size_in_bytes
) {
223 if ((uint32_t) entry_number
>= LDT_ENTRIES
) {
226 return NaClLdtAllocateSelector(entry_number
, 0, type
, read_exec_only
,
227 base_addr
, size_in_bytes
- 1);
231 void NaClLdtPrintSelector(uint16_t selector
) {
232 /* type_name converts the segment type into print name */
233 static const char* type_name
[] = {
235 "data read_only accessed",
237 "data read write accessed",
239 "data read expand accessed",
240 "data read write expand",
241 "data read write expand accessed",
243 "code execute accessed",
245 "code execute read accessed",
246 "code execute conforming",
247 "code execute conforming accessed",
248 "code execute read conforming",
249 "code execute read conforming accessed"
252 struct LdtEntry entries
[LDT_ENTRIES
];
253 struct LdtEntry entry
;
254 int retval
= modify_ldt(0, entries
, sizeof(entries
));
258 entry
= entries
[selector
>> 3];
259 printf("DESCRIPTOR for selector %04x\n", selector
);
260 // create functions to do base, limit, and type.
261 printf(" base %08x\n", (entry
.base_24to31
<< 24)
262 | (entry
.base_16to23
<< 16) | entry
.base_00to15
);
263 printf(" limit %08x%s\n",
264 (((entry
.limit_16to19
<< 16) | entry
.limit_00to15
)
265 << (entry
.granularity
? 12 : 0)),
266 (entry
.granularity
? " (page granularity)" : ""));
267 printf(" type %s, %s\n", ((entry
.type
& 0x10) ? "user" : "system"),
268 type_name
[entry
.type
& 0xf]);
269 printf(" privilege %d\n", entry
.descriptor_privilege
);
270 printf(" present %s\n", (entry
.present
? "yes" : "no"));
271 printf(" available %s\n", (entry
.available
? "yes" : "no"));
272 printf(" 64-bit code %s\n", (entry
.code_64_bit
? "yes" : "no"));
273 printf(" op size %s\n", (entry
.op_size_32
? "32" : "16"));
277 * Mark a selector as available for future reuse.
279 void NaClLdtDeleteSelector(uint16_t selector
) {
281 ud
.entry_number
= selector
>> 3;
282 ud
.seg_not_present
= 1;
285 ud
.limit_in_pages
= 0;
286 ud
.read_exec_only
= 0;
289 ud
.contents
= MODIFY_LDT_CONTENTS_DATA
;
290 NaClMutexLock(&nacl_ldt_mutex
);
291 modify_ldt(1, &ud
, sizeof ud
);
292 NaClMutexUnlock(&nacl_ldt_mutex
);
296 * Find a free selector. Always invoked while holding nacl_ldt_mutex.
298 static int NaClFindUnusedEntryNumber() {
299 int size
= sizeof(struct LdtEntry
) * LDT_ENTRIES
;
300 struct LdtEntry
*entries
= malloc(size
);
303 int retval
= modify_ldt(0, entries
, size
);
306 retval
= -1; /* In case we don't find any free entry */
307 for (i
= 0; i
< LDT_ENTRIES
; ++i
) {
308 if (!entries
[i
].present
) {