2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
14 DEFAULT_DEBUG_CHANNEL(ldt
)
19 #include <sys/syscall.h>
23 unsigned int entry_number
;
24 unsigned long base_addr
;
26 unsigned int seg_32bit
: 1;
27 unsigned int contents
: 2;
28 unsigned int read_exec_only
: 1;
29 unsigned int limit_in_pages
: 1;
30 unsigned int seg_not_present
: 1;
33 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
,
38 __asm__
__volatile__( "pushl %%ebx\n\t"
43 : "0" (SYS_modify_ldt
),
48 __asm__
__volatile__("int $0x80"
50 : "0" (SYS_modify_ldt
),
55 if (res
>= 0) return res
;
62 #if defined(__svr4__) || defined(_SCO_DS)
63 #include <sys/sysi86.h>
69 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
70 #include <machine/segments.h>
72 extern int i386_get_ldt(int, union descriptor
*, int);
73 extern int i386_set_ldt(int, union descriptor
*, int);
74 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
79 ldt_copy_entry ldt_copy
[LDT_SIZE
];
80 unsigned char ldt_flags_copy
[LDT_SIZE
];
83 /***********************************************************************
86 * Convert the raw bytes of the descriptor to an ldt_entry structure.
88 void LDT_BytesToEntry( const unsigned long *buffer
, ldt_entry
*content
)
90 content
->base
= (*buffer
>> 16) & 0x0000ffff;
91 content
->limit
= *buffer
& 0x0000ffff;
93 content
->base
|= (*buffer
& 0xff000000) | ((*buffer
<< 16) & 0x00ff0000);
94 content
->limit
|= (*buffer
& 0x000f0000);
95 content
->type
= (*buffer
>> 10) & 3;
96 content
->seg_32bit
= (*buffer
& 0x00400000) != 0;
97 content
->read_only
= (*buffer
& 0x00000200) == 0;
98 content
->limit_in_pages
= (*buffer
& 0x00800000) != 0;
102 /***********************************************************************
105 * Convert an ldt_entry structure to the raw bytes of the descriptor.
107 void LDT_EntryToBytes( unsigned long *buffer
, const ldt_entry
*content
)
109 *buffer
++ = ((content
->base
& 0x0000ffff) << 16) |
110 (content
->limit
& 0x0ffff);
111 *buffer
= (content
->base
& 0xff000000) |
112 ((content
->base
& 0x00ff0000)>>16) |
113 (content
->limit
& 0xf0000) |
114 (content
->type
<< 10) |
115 ((content
->read_only
== 0) << 9) |
116 ((content
->seg_32bit
!= 0) << 22) |
117 ((content
->limit_in_pages
!= 0) << 23) |
122 /***********************************************************************
125 * Retrieve an LDT entry.
127 int LDT_GetEntry( int entry
, ldt_entry
*content
)
131 content
->base
= ldt_copy
[entry
].base
;
132 content
->limit
= ldt_copy
[entry
].limit
;
133 content
->type
= (ldt_flags_copy
[entry
] & LDT_FLAGS_TYPE
);
134 content
->seg_32bit
= (ldt_flags_copy
[entry
] & LDT_FLAGS_32BIT
) != 0;
135 content
->read_only
= (ldt_flags_copy
[entry
] & LDT_FLAGS_READONLY
) !=0;
136 content
->limit_in_pages
= (ldt_flags_copy
[entry
] & LDT_FLAGS_BIG
) !=0;
137 if (content
->limit_in_pages
) content
->limit
>>= 12;
142 /***********************************************************************
147 int LDT_SetEntry( int entry
, const ldt_entry
*content
)
151 TRACE(ldt
, "entry=%04x base=%08lx limit=%05lx %s %d-bit "
152 "flags=%c%c%c\n", entry
, content
->base
, content
->limit
,
153 content
->limit_in_pages
? "pages" : "bytes",
154 content
->seg_32bit
? 32 : 16,
155 content
->read_only
&& (content
->type
& SEGMENT_CODE
) ? '-' : 'r',
156 content
->read_only
|| (content
->type
& SEGMENT_CODE
) ? '-' : 'w',
157 (content
->type
& SEGMENT_CODE
) ? 'x' : '-' );
159 /* Entry 0 must not be modified; its base and limit are always 0 */
160 if (!entry
) return 0;
166 struct modify_ldt_s ldt_info
;
168 ldt_info
.entry_number
= entry
;
169 ldt_info
.base_addr
= content
->base
;
170 ldt_info
.limit
= content
->limit
;
171 ldt_info
.seg_32bit
= content
->seg_32bit
!= 0;
172 ldt_info
.contents
= content
->type
;
173 ldt_info
.read_exec_only
= content
->read_only
!= 0;
174 ldt_info
.limit_in_pages
= content
->limit_in_pages
!= 0;
175 ldt_info
.seg_not_present
= 0;
176 /* Make sure the info will be accepted by the kernel */
177 /* This is ugly, but what can I do? */
178 if (content
->type
== SEGMENT_STACK
)
184 if (ldt_info
.base_addr
>= 0xc0000000)
186 WARN(ldt
, "Invalid base addr %08lx\n",
187 ldt_info
.base_addr
);
190 if (content
->limit_in_pages
)
192 if ((ldt_info
.limit
<< 12) + 0xfff >
193 0xc0000000 - ldt_info
.base_addr
)
194 ldt_info
.limit
= (0xc0000000 - 0xfff - ldt_info
.base_addr
) >> 12;
198 if (ldt_info
.limit
> 0xc0000000 - ldt_info
.base_addr
)
199 ldt_info
.limit
= 0xc0000000 - ldt_info
.base_addr
;
202 if ((ret
= modify_ldt(1, &ldt_info
, sizeof(ldt_info
))) < 0)
203 perror( "modify_ldt" );
207 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
211 LDT_EntryToBytes( d
, content
);
212 ret
= i386_set_ldt(entry
, (union descriptor
*)d
, 1);
215 perror("i386_set_ldt");
216 MSG("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
220 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
222 #if defined(__svr4__) || defined(_SCO_DS)
226 ldt_mod
.sel
= ENTRY_TO_SELECTOR(entry
) | 4;
227 ldt_mod
.bo
= content
->base
;
228 ldt_mod
.ls
= content
->limit
;
229 i
= ((content
->limit
& 0xf0000) |
230 (content
->type
<< 10) |
231 (((content
->read_only
!= 0) ^ 1) << 9) |
232 ((content
->seg_32bit
!= 0) << 22) |
233 ((content
->limit_in_pages
!= 0)<< 23) |
237 ldt_mod
.acc1
= (i
& 0xff00) >> 8;
238 ldt_mod
.acc2
= (i
& 0xf00000) >> 20;
240 if (content
->base
== 0)
245 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
249 #endif /* __i386__ */
251 if (ret
< 0) return ret
;
252 ldt_copy
[entry
].base
= content
->base
;
253 if (!content
->limit_in_pages
) ldt_copy
[entry
].limit
= content
->limit
;
254 else ldt_copy
[entry
].limit
= (content
->limit
<< 12) | 0x0fff;
255 ldt_flags_copy
[entry
] = (content
->type
& LDT_FLAGS_TYPE
) |
256 (content
->read_only
? LDT_FLAGS_READONLY
: 0) |
257 (content
->seg_32bit
? LDT_FLAGS_32BIT
: 0) |
258 (content
->limit_in_pages
? LDT_FLAGS_BIG
: 0) |
259 (ldt_flags_copy
[entry
] & LDT_FLAGS_ALLOCATED
);
264 /***********************************************************************
267 * Print the content of the LDT on stdout.
269 void LDT_Print( int start
, int length
)
274 if (length
== -1) length
= LDT_SIZE
- start
;
275 for (i
= start
; i
< start
+ length
; i
++)
277 if (!ldt_copy
[i
].base
&& !ldt_copy
[i
].limit
) continue; /* Free entry */
278 if ((ldt_flags_copy
[i
] & LDT_FLAGS_TYPE
) == SEGMENT_CODE
)
280 flags
[0] = (ldt_flags_copy
[i
] & LDT_FLAGS_EXECONLY
) ? '-' : 'r';
287 flags
[1] = (ldt_flags_copy
[i
] & LDT_FLAGS_READONLY
) ? '-' : 'w';
290 MSG("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
291 i
, ENTRY_TO_SELECTOR(i
), ldt_copy
[i
].base
, ldt_copy
[i
].limit
,
292 ldt_flags_copy
[i
] & LDT_FLAGS_32BIT
? 32 : 16,
293 flags
[0], flags
[1], flags
[2] );