2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
17 #include <sys/syscall.h>
21 unsigned int entry_number
;
22 unsigned long base_addr
;
24 unsigned int seg_32bit
: 1;
25 unsigned int contents
: 2;
26 unsigned int read_exec_only
: 1;
27 unsigned int limit_in_pages
: 1;
28 unsigned int seg_not_present
: 1;
31 static __inline__
int modify_ldt( int func
, struct modify_ldt_s
*ptr
,
36 __asm__
__volatile__( "pushl %%ebx\n\t"
41 : "0" (SYS_modify_ldt
),
46 __asm__
__volatile__("int $0x80"
48 : "0" (SYS_modify_ldt
),
53 if (res
>= 0) return res
;
60 #if defined(__svr4__) || defined(_SCO_DS)
61 #include <sys/sysi86.h>
67 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
68 #include <machine/segments.h>
70 extern int i386_get_ldt(int, union descriptor
*, int);
71 extern int i386_set_ldt(int, union descriptor
*, int);
72 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
77 ldt_copy_entry ldt_copy
[LDT_SIZE
];
78 unsigned char ldt_flags_copy
[LDT_SIZE
];
81 /***********************************************************************
84 * Convert the raw bytes of the descriptor to an ldt_entry structure.
86 void LDT_BytesToEntry( const unsigned long *buffer
, ldt_entry
*content
)
88 content
->base
= (*buffer
>> 16) & 0x0000ffff;
89 content
->limit
= *buffer
& 0x0000ffff;
91 content
->base
|= (*buffer
& 0xff000000) | ((*buffer
<< 16) & 0x00ff0000);
92 content
->limit
|= (*buffer
& 0x000f0000);
93 content
->type
= (*buffer
>> 10) & 3;
94 content
->seg_32bit
= (*buffer
& 0x00400000) != 0;
95 content
->read_only
= (*buffer
& 0x00000200) == 0;
96 content
->limit_in_pages
= (*buffer
& 0x00800000) != 0;
100 /***********************************************************************
103 * Convert an ldt_entry structure to the raw bytes of the descriptor.
105 void LDT_EntryToBytes( unsigned long *buffer
, const ldt_entry
*content
)
107 *buffer
++ = ((content
->base
& 0x0000ffff) << 16) |
108 (content
->limit
& 0x0ffff);
109 *buffer
= (content
->base
& 0xff000000) |
110 ((content
->base
& 0x00ff0000)>>16) |
111 (content
->limit
& 0xf0000) |
112 (content
->type
<< 10) |
113 ((content
->read_only
== 0) << 9) |
114 ((content
->seg_32bit
!= 0) << 22) |
115 ((content
->limit_in_pages
!= 0) << 23) |
120 /***********************************************************************
123 * Retrieve an LDT entry.
125 int LDT_GetEntry( int entry
, ldt_entry
*content
)
129 content
->base
= ldt_copy
[entry
].base
;
130 content
->limit
= ldt_copy
[entry
].limit
;
131 content
->type
= (ldt_flags_copy
[entry
] & LDT_FLAGS_TYPE
);
132 content
->seg_32bit
= (ldt_flags_copy
[entry
] & LDT_FLAGS_32BIT
) != 0;
133 content
->read_only
= (ldt_flags_copy
[entry
] & LDT_FLAGS_READONLY
) !=0;
134 content
->limit_in_pages
= (ldt_flags_copy
[entry
] & LDT_FLAGS_BIG
) !=0;
135 if (content
->limit_in_pages
) content
->limit
>>= 12;
140 /***********************************************************************
145 int LDT_SetEntry( int entry
, const ldt_entry
*content
)
149 TRACE(ldt
, "entry=%04x base=%08lx limit=%05lx %s %d-bit "
150 "flags=%c%c%c\n", entry
, content
->base
, content
->limit
,
151 content
->limit_in_pages
? "pages" : "bytes",
152 content
->seg_32bit
? 32 : 16,
153 content
->read_only
&& (content
->type
& SEGMENT_CODE
) ? '-' : 'r',
154 content
->read_only
|| (content
->type
& SEGMENT_CODE
) ? '-' : 'w',
155 (content
->type
& SEGMENT_CODE
) ? 'x' : '-' );
157 /* Entry 0 must not be modified; its base and limit are always 0 */
158 if (!entry
) return 0;
164 struct modify_ldt_s ldt_info
;
166 ldt_info
.entry_number
= entry
;
167 ldt_info
.base_addr
= content
->base
;
168 ldt_info
.limit
= content
->limit
;
169 ldt_info
.seg_32bit
= content
->seg_32bit
!= 0;
170 ldt_info
.contents
= content
->type
;
171 ldt_info
.read_exec_only
= content
->read_only
!= 0;
172 ldt_info
.limit_in_pages
= content
->limit_in_pages
!= 0;
173 ldt_info
.seg_not_present
= 0;
174 /* Make sure the info will be accepted by the kernel */
175 /* This is ugly, but what can I do? */
176 if (content
->type
== SEGMENT_STACK
)
182 if (ldt_info
.base_addr
>= 0xc0000000)
184 WARN(ldt
, "Invalid base addr %08lx\n",
185 ldt_info
.base_addr
);
188 if (content
->limit_in_pages
)
190 if ((ldt_info
.limit
<< 12) + 0xfff >
191 0xc0000000 - ldt_info
.base_addr
)
192 ldt_info
.limit
= (0xc0000000 - 0xfff - ldt_info
.base_addr
) >> 12;
196 if (ldt_info
.limit
> 0xc0000000 - ldt_info
.base_addr
)
197 ldt_info
.limit
= 0xc0000000 - ldt_info
.base_addr
;
200 if ((ret
= modify_ldt(1, &ldt_info
, sizeof(ldt_info
))) < 0)
201 perror( "modify_ldt" );
205 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209 LDT_EntryToBytes( d
, content
);
210 ret
= i386_set_ldt(entry
, (union descriptor
*)d
, 1);
213 perror("i386_set_ldt");
214 MSG("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
218 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
220 #if defined(__svr4__) || defined(_SCO_DS)
224 ldt_mod
.sel
= ENTRY_TO_SELECTOR(entry
) | 4;
225 ldt_mod
.bo
= content
->base
;
226 ldt_mod
.ls
= content
->limit
;
227 i
= ((content
->limit
& 0xf0000) |
228 (content
->type
<< 10) |
229 (((content
->read_only
!= 0) ^ 1) << 9) |
230 ((content
->seg_32bit
!= 0) << 22) |
231 ((content
->limit_in_pages
!= 0)<< 23) |
235 ldt_mod
.acc1
= (i
& 0xff00) >> 8;
236 ldt_mod
.acc2
= (i
& 0xf00000) >> 20;
238 if (content
->base
== 0)
243 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
247 #endif /* __i386__ */
249 if (ret
< 0) return ret
;
250 ldt_copy
[entry
].base
= content
->base
;
251 if (!content
->limit_in_pages
) ldt_copy
[entry
].limit
= content
->limit
;
252 else ldt_copy
[entry
].limit
= (content
->limit
<< 12) | 0x0fff;
253 ldt_flags_copy
[entry
] = (content
->type
& LDT_FLAGS_TYPE
) |
254 (content
->read_only
? LDT_FLAGS_READONLY
: 0) |
255 (content
->seg_32bit
? LDT_FLAGS_32BIT
: 0) |
256 (content
->limit_in_pages
? LDT_FLAGS_BIG
: 0) |
257 (ldt_flags_copy
[entry
] & LDT_FLAGS_ALLOCATED
);
262 /***********************************************************************
265 * Print the content of the LDT on stdout.
267 void LDT_Print( int start
, int length
)
272 if (length
== -1) length
= LDT_SIZE
- start
;
273 for (i
= start
; i
< start
+ length
; i
++)
275 if (!ldt_copy
[i
].base
&& !ldt_copy
[i
].limit
) continue; /* Free entry */
276 if ((ldt_flags_copy
[i
] & LDT_FLAGS_TYPE
) == SEGMENT_CODE
)
278 flags
[0] = (ldt_flags_copy
[i
] & LDT_FLAGS_EXECONLY
) ? '-' : 'r';
285 flags
[1] = (ldt_flags_copy
[i
] & LDT_FLAGS_READONLY
) ? '-' : 'w';
288 MSG("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
289 i
, ENTRY_TO_SELECTOR(i
), ldt_copy
[i
].base
, ldt_copy
[i
].limit
,
290 ldt_flags_copy
[i
] & LDT_FLAGS_32BIT
? 32 : 16,
291 flags
[0], flags
[1], flags
[2] );