2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(ldt
)
22 #ifdef HAVE_SYS_SYSCALL_H
23 # include <sys/syscall.h>
28 unsigned int entry_number
;
29 unsigned long base_addr
;
31 unsigned int seg_32bit
: 1;
32 unsigned int contents
: 2;
33 unsigned int read_exec_only
: 1;
34 unsigned int limit_in_pages
: 1;
35 unsigned int seg_not_present
: 1;
38 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
,
43 __asm__
__volatile__( "pushl %%ebx\n\t"
48 : "0" (SYS_modify_ldt
),
53 __asm__
__volatile__("int $0x80"
55 : "0" (SYS_modify_ldt
),
60 if (res
>= 0) return res
;
67 #if defined(__svr4__) || defined(_SCO_DS)
68 #include <sys/sysi86.h>
69 extern int sysi86(int,void*);
75 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
76 #include <machine/segments.h>
78 extern int i386_get_ldt(int, union descriptor
*, int);
79 extern int i386_set_ldt(int, union descriptor
*, int);
80 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
85 ldt_copy_entry ldt_copy
[LDT_SIZE
];
86 unsigned char ldt_flags_copy
[LDT_SIZE
];
89 /***********************************************************************
92 * Convert the raw bytes of the descriptor to an ldt_entry structure.
94 void LDT_BytesToEntry( const unsigned long *buffer
, ldt_entry
*content
)
96 content
->base
= (*buffer
>> 16) & 0x0000ffff;
97 content
->limit
= *buffer
& 0x0000ffff;
99 content
->base
|= (*buffer
& 0xff000000) | ((*buffer
<< 16) & 0x00ff0000);
100 content
->limit
|= (*buffer
& 0x000f0000);
101 content
->type
= (*buffer
>> 10) & 3;
102 content
->seg_32bit
= (*buffer
& 0x00400000) != 0;
103 content
->read_only
= (*buffer
& 0x00000200) == 0;
104 content
->limit_in_pages
= (*buffer
& 0x00800000) != 0;
108 /***********************************************************************
111 * Convert an ldt_entry structure to the raw bytes of the descriptor.
113 void LDT_EntryToBytes( unsigned long *buffer
, const ldt_entry
*content
)
115 *buffer
++ = ((content
->base
& 0x0000ffff) << 16) |
116 (content
->limit
& 0x0ffff);
117 *buffer
= (content
->base
& 0xff000000) |
118 ((content
->base
& 0x00ff0000)>>16) |
119 (content
->limit
& 0xf0000) |
120 (content
->type
<< 10) |
121 ((content
->read_only
== 0) << 9) |
122 ((content
->seg_32bit
!= 0) << 22) |
123 ((content
->limit_in_pages
!= 0) << 23) |
128 /***********************************************************************
131 * Retrieve an LDT entry.
133 int LDT_GetEntry( int entry
, ldt_entry
*content
)
137 content
->base
= ldt_copy
[entry
].base
;
138 content
->limit
= ldt_copy
[entry
].limit
;
139 content
->type
= (ldt_flags_copy
[entry
] & LDT_FLAGS_TYPE
);
140 content
->seg_32bit
= (ldt_flags_copy
[entry
] & LDT_FLAGS_32BIT
) != 0;
141 content
->read_only
= (ldt_flags_copy
[entry
] & LDT_FLAGS_READONLY
) !=0;
142 content
->limit_in_pages
= (ldt_flags_copy
[entry
] & LDT_FLAGS_BIG
) !=0;
143 if (content
->limit_in_pages
) content
->limit
>>= 12;
148 /***********************************************************************
153 int LDT_SetEntry( int entry
, const ldt_entry
*content
)
157 TRACE("entry=%04x base=%08lx limit=%05lx %s %d-bit "
158 "flags=%c%c%c\n", entry
, content
->base
, content
->limit
,
159 content
->limit_in_pages
? "pages" : "bytes",
160 content
->seg_32bit
? 32 : 16,
161 content
->read_only
&& (content
->type
& SEGMENT_CODE
) ? '-' : 'r',
162 content
->read_only
|| (content
->type
& SEGMENT_CODE
) ? '-' : 'w',
163 (content
->type
& SEGMENT_CODE
) ? 'x' : '-' );
165 /* Entry 0 must not be modified; its base and limit are always 0 */
166 if (!entry
) return 0;
172 struct modify_ldt_s ldt_info
;
174 ldt_info
.entry_number
= entry
;
175 ldt_info
.base_addr
= content
->base
;
176 ldt_info
.limit
= content
->limit
;
177 ldt_info
.seg_32bit
= content
->seg_32bit
!= 0;
178 ldt_info
.contents
= content
->type
;
179 ldt_info
.read_exec_only
= content
->read_only
!= 0;
180 ldt_info
.limit_in_pages
= content
->limit_in_pages
!= 0;
181 ldt_info
.seg_not_present
= 0;
182 /* Make sure the info will be accepted by the kernel */
183 /* This is ugly, but what can I do? */
184 if (content
->type
== SEGMENT_STACK
)
190 if (ldt_info
.base_addr
>= 0xc0000000)
192 WARN("Invalid base addr %08lx\n",
193 ldt_info
.base_addr
);
196 if (content
->limit_in_pages
)
198 if ((ldt_info
.limit
<< 12) + 0xfff >
199 0xc0000000 - ldt_info
.base_addr
)
200 ldt_info
.limit
= (0xc0000000 - 0xfff - ldt_info
.base_addr
) >> 12;
204 if (ldt_info
.limit
> 0xc0000000 - ldt_info
.base_addr
)
205 ldt_info
.limit
= 0xc0000000 - ldt_info
.base_addr
;
208 if ((ret
= modify_ldt(1, &ldt_info
, sizeof(ldt_info
))) < 0)
209 perror( "modify_ldt" );
213 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
217 LDT_EntryToBytes( d
, content
);
218 ret
= i386_set_ldt(entry
, (union descriptor
*)d
, 1);
221 perror("i386_set_ldt");
222 MESSAGE("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
226 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
228 #if defined(__svr4__) || defined(_SCO_DS)
232 ldt_mod
.sel
= ENTRY_TO_SELECTOR(entry
) | 4;
233 ldt_mod
.bo
= content
->base
;
234 ldt_mod
.ls
= content
->limit
;
235 i
= ((content
->limit
& 0xf0000) |
236 (content
->type
<< 10) |
237 (((content
->read_only
!= 0) ^ 1) << 9) |
238 ((content
->seg_32bit
!= 0) << 22) |
239 ((content
->limit_in_pages
!= 0)<< 23) |
243 ldt_mod
.acc1
= (i
& 0xff00) >> 8;
244 ldt_mod
.acc2
= (i
& 0xf00000) >> 20;
246 if (content
->base
== 0)
251 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
255 #endif /* __i386__ */
257 if (ret
< 0) return ret
;
258 ldt_copy
[entry
].base
= content
->base
;
259 if (!content
->limit_in_pages
) ldt_copy
[entry
].limit
= content
->limit
;
260 else ldt_copy
[entry
].limit
= (content
->limit
<< 12) | 0x0fff;
261 ldt_flags_copy
[entry
] = (content
->type
& LDT_FLAGS_TYPE
) |
262 (content
->read_only
? LDT_FLAGS_READONLY
: 0) |
263 (content
->seg_32bit
? LDT_FLAGS_32BIT
: 0) |
264 (content
->limit_in_pages
? LDT_FLAGS_BIG
: 0) |
265 (ldt_flags_copy
[entry
] & LDT_FLAGS_ALLOCATED
);
270 /***********************************************************************
273 * Print the content of the LDT on stdout.
275 void LDT_Print( int start
, int length
)
280 if (length
== -1) length
= LDT_SIZE
- start
;
281 for (i
= start
; i
< start
+ length
; i
++)
283 if (!ldt_copy
[i
].base
&& !ldt_copy
[i
].limit
) continue; /* Free entry */
284 if ((ldt_flags_copy
[i
] & LDT_FLAGS_TYPE
) == SEGMENT_CODE
)
286 flags
[0] = (ldt_flags_copy
[i
] & LDT_FLAGS_EXECONLY
) ? '-' : 'r';
293 flags
[1] = (ldt_flags_copy
[i
] & LDT_FLAGS_READONLY
) ? '-' : 'w';
296 MESSAGE("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
297 i
, ENTRY_TO_SELECTOR(i
), ldt_copy
[i
].base
, ldt_copy
[i
].limit
,
298 ldt_flags_copy
[i
] & LDT_FLAGS_32BIT
? 32 : 16,
299 flags
[0], flags
[1], flags
[2] );