2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
34 #include "wine/library.h"
41 #ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
47 unsigned int entry_number
;
48 unsigned long base_addr
;
50 unsigned int seg_32bit
: 1;
51 unsigned int contents
: 2;
52 unsigned int read_exec_only
: 1;
53 unsigned int limit_in_pages
: 1;
54 unsigned int seg_not_present
: 1;
55 unsigned int usable
: 1;
56 unsigned int garbage
: 25;
59 static inline void fill_modify_ldt_struct( struct modify_ldt_s
*ptr
, const LDT_ENTRY
*entry
)
61 ptr
->base_addr
= (unsigned long)wine_ldt_get_base(entry
);
62 ptr
->limit
= entry
->LimitLow
| (entry
->HighWord
.Bits
.LimitHi
<< 16);
63 ptr
->seg_32bit
= entry
->HighWord
.Bits
.Default_Big
;
64 ptr
->contents
= (entry
->HighWord
.Bits
.Type
>> 2) & 3;
65 ptr
->read_exec_only
= !(entry
->HighWord
.Bits
.Type
& 2);
66 ptr
->limit_in_pages
= entry
->HighWord
.Bits
.Granularity
;
67 ptr
->seg_not_present
= !entry
->HighWord
.Bits
.Pres
;
68 ptr
->usable
= entry
->HighWord
.Bits
.Sys
;
72 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
, unsigned long count
)
74 return syscall( 123 /* SYS_modify_ldt */, func
, ptr
, count
);
77 static inline int set_thread_area( struct modify_ldt_s
*ptr
)
79 return syscall( 243 /* SYS_set_thread_area */, ptr
);
84 #if defined(__svr4__) || defined(_SCO_DS)
85 #include <sys/sysi86.h>
91 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
92 #include <machine/segments.h>
93 #include <machine/sysarch.h>
94 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
97 #include <mach/i386/mach_i386.h>
98 #include <mach/mach_traps.h>
102 #include <i386/user_ldt.h>
105 /* local copy of the LDT */
107 struct __wine_ldt_copy wine_ldt_copy
= { { 0, 0, 0 } };
109 struct __wine_ldt_copy wine_ldt_copy
;
112 static const LDT_ENTRY null_entry
; /* all-zeros, used to clear LDT entries */
114 #define LDT_FIRST_ENTRY 512
115 #define LDT_SIZE 8192
117 /* empty function for default locks */
118 static void nop(void) { }
120 static void (*lock_ldt
)(void) = nop
;
121 static void (*unlock_ldt
)(void) = nop
;
124 static inline int is_gdt_sel( unsigned short sel
) { return !(sel
& 4); }
126 /***********************************************************************
127 * wine_ldt_init_locking
129 * Set the LDT locking/unlocking functions.
131 void wine_ldt_init_locking( void (*lock_func
)(void), void (*unlock_func
)(void) )
133 lock_ldt
= lock_func
;
134 unlock_ldt
= unlock_func
;
138 /***********************************************************************
141 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
143 void wine_ldt_get_entry( unsigned short sel
, LDT_ENTRY
*entry
)
145 int index
= sel
>> 3;
153 if (wine_ldt_copy
.flags
[index
] & WINE_LDT_FLAGS_ALLOCATED
)
155 wine_ldt_set_base( entry
, wine_ldt_copy
.base
[index
] );
156 wine_ldt_set_limit( entry
, wine_ldt_copy
.limit
[index
] );
157 wine_ldt_set_flags( entry
, wine_ldt_copy
.flags
[index
] );
159 else *entry
= null_entry
;
164 /***********************************************************************
167 * Set an LDT entry, without locking. For internal use only.
169 static int internal_set_entry( unsigned short sel
, const LDT_ENTRY
*entry
)
171 int ret
= 0, index
= sel
>> 3;
173 if (index
< LDT_FIRST_ENTRY
) return 0; /* cannot modify reserved entries */
177 struct modify_ldt_s ldt_info
;
179 ldt_info
.entry_number
= index
;
180 fill_modify_ldt_struct( &ldt_info
, entry
);
181 if ((ret
= modify_ldt(0x11, &ldt_info
, sizeof(ldt_info
))) < 0)
182 perror( "modify_ldt" );
184 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
186 LDT_ENTRY entry_copy
= *entry
;
187 /* The kernel will only let us set LDTs with user priority level */
188 if (entry_copy
.HighWord
.Bits
.Pres
189 && entry_copy
.HighWord
.Bits
.Dpl
!= 3)
190 entry_copy
.HighWord
.Bits
.Dpl
= 3;
191 ret
= i386_set_ldt(index
, (union descriptor
*)&entry_copy
, 1);
194 perror("i386_set_ldt");
195 fprintf( stderr
, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
199 #elif defined(__svr4__) || defined(_SCO_DS)
203 ldt_mod
.bo
= (unsigned long)wine_ldt_get_base(entry
);
204 ldt_mod
.ls
= entry
->LimitLow
| (entry
->HighWord
.Bits
.LimitHi
<< 16);
205 ldt_mod
.acc1
= entry
->HighWord
.Bytes
.Flags1
;
206 ldt_mod
.acc2
= entry
->HighWord
.Bytes
.Flags2
>> 4;
207 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
209 #elif defined(__APPLE__)
210 if ((ret
= i386_set_ldt(index
, (union ldt_entry
*)entry
, 1)) < 0)
211 perror("i386_set_ldt");
212 #elif defined(__GNU__)
213 if ((ret
= i386_set_ldt(mach_thread_self(), sel
, (descriptor_list_t
)entry
, 1)) != KERN_SUCCESS
)
214 perror("i386_set_ldt");
216 fprintf( stderr
, "No LDT support on this platform\n" );
222 wine_ldt_copy
.base
[index
] = wine_ldt_get_base(entry
);
223 wine_ldt_copy
.limit
[index
] = wine_ldt_get_limit(entry
);
224 wine_ldt_copy
.flags
[index
] = (entry
->HighWord
.Bits
.Type
|
225 (entry
->HighWord
.Bits
.Default_Big
? WINE_LDT_FLAGS_32BIT
: 0) |
226 (wine_ldt_copy
.flags
[index
] & WINE_LDT_FLAGS_ALLOCATED
));
232 /***********************************************************************
237 int wine_ldt_set_entry( unsigned short sel
, const LDT_ENTRY
*entry
)
242 ret
= internal_set_entry( sel
, entry
);
248 /***********************************************************************
251 * Check if the selector is a system selector (i.e. not managed by Wine).
253 int wine_ldt_is_system( unsigned short sel
)
255 return is_gdt_sel(sel
) || ((sel
>> 3) < LDT_FIRST_ENTRY
);
259 /***********************************************************************
262 * Convert a segment:offset pair to a linear pointer.
263 * Note: we don't lock the LDT since this has to be fast.
265 void *wine_ldt_get_ptr( unsigned short sel
, unsigned long offset
)
269 if (is_gdt_sel(sel
)) /* GDT selector */
270 return (void *)offset
;
271 if ((index
= (sel
>> 3)) < LDT_FIRST_ENTRY
) /* system selector */
272 return (void *)offset
;
273 if (!(wine_ldt_copy
.flags
[index
] & WINE_LDT_FLAGS_32BIT
)) offset
&= 0xffff;
274 return (char *)wine_ldt_copy
.base
[index
] + offset
;
278 /***********************************************************************
279 * wine_ldt_alloc_entries
281 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
282 * Return a selector for the first entry.
284 unsigned short wine_ldt_alloc_entries( int count
)
286 int i
, index
, size
= 0;
288 if (count
<= 0) return 0;
290 for (i
= LDT_FIRST_ENTRY
; i
< LDT_SIZE
; i
++)
292 if (wine_ldt_copy
.flags
[i
] & WINE_LDT_FLAGS_ALLOCATED
) size
= 0;
293 else if (++size
>= count
) /* found a large enough block */
295 index
= i
- size
+ 1;
297 /* mark selectors as allocated */
298 for (i
= 0; i
< count
; i
++) wine_ldt_copy
.flags
[index
+ i
] |= WINE_LDT_FLAGS_ALLOCATED
;
300 return (index
<< 3) | 7;
308 /***********************************************************************
309 * wine_ldt_realloc_entries
311 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
312 * Return a selector for the first entry.
314 unsigned short wine_ldt_realloc_entries( unsigned short sel
, int oldcount
, int newcount
)
318 if (oldcount
< newcount
) /* we need to add selectors */
320 int index
= sel
>> 3;
323 /* check if the next selectors are free */
324 if (index
+ newcount
> LDT_SIZE
) i
= oldcount
;
326 for (i
= oldcount
; i
< newcount
; i
++)
327 if (wine_ldt_copy
.flags
[index
+i
] & WINE_LDT_FLAGS_ALLOCATED
) break;
329 if (i
< newcount
) /* they are not free */
331 wine_ldt_free_entries( sel
, oldcount
);
332 sel
= wine_ldt_alloc_entries( newcount
);
334 else /* mark the selectors as allocated */
336 for (i
= oldcount
; i
< newcount
; i
++)
337 wine_ldt_copy
.flags
[index
+i
] |= WINE_LDT_FLAGS_ALLOCATED
;
341 else if (oldcount
> newcount
) /* we need to remove selectors */
343 wine_ldt_free_entries( sel
+ (newcount
<< 3), newcount
- oldcount
);
349 /***********************************************************************
350 * wine_ldt_free_entries
352 * Free a number of consecutive ldt entries and clear their contents.
354 void wine_ldt_free_entries( unsigned short sel
, int count
)
359 for (index
= sel
>> 3; count
> 0; count
--, index
++)
361 internal_set_entry( sel
, &null_entry
);
362 wine_ldt_copy
.flags
[index
] = 0;
368 static int global_fs_sel
= -1; /* global selector for %fs shared among all threads */
370 /***********************************************************************
373 * Allocate an LDT entry for a %fs selector, reusing a global
374 * GDT selector if possible. Return the selector value.
376 unsigned short wine_ldt_alloc_fs(void)
378 if (global_fs_sel
== -1)
381 struct modify_ldt_s ldt_info
;
384 /* the preloader may have allocated it already */
385 global_fs_sel
= wine_get_fs();
386 if (global_fs_sel
&& is_gdt_sel(global_fs_sel
)) return global_fs_sel
;
388 memset( &ldt_info
, 0, sizeof(ldt_info
) );
389 ldt_info
.entry_number
= -1;
390 ldt_info
.seg_32bit
= 1;
392 if ((ret
= set_thread_area( &ldt_info
) < 0))
394 global_fs_sel
= 0; /* don't try it again */
395 if (errno
!= ENOSYS
) perror( "set_thread_area" );
397 else global_fs_sel
= (ldt_info
.entry_number
<< 3) | 3;
398 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
399 global_fs_sel
= GSEL( GUFS_SEL
, SEL_UPL
);
402 if (global_fs_sel
> 0) return global_fs_sel
;
403 return wine_ldt_alloc_entries( 1 );
407 /***********************************************************************
410 * Initialize the entry for the %fs selector of the current thread, and
411 * set the thread %fs register.
413 * Note: this runs in the context of the new thread, so cannot acquire locks.
415 void wine_ldt_init_fs( unsigned short sel
, const LDT_ENTRY
*entry
)
417 if ((sel
& ~3) == (global_fs_sel
& ~3))
420 struct modify_ldt_s ldt_info
;
423 ldt_info
.entry_number
= sel
>> 3;
424 fill_modify_ldt_struct( &ldt_info
, entry
);
425 if ((ret
= set_thread_area( &ldt_info
) < 0)) perror( "set_thread_area" );
426 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
427 i386_set_fsbase( wine_ldt_get_base( entry
));
430 else /* LDT selector */
432 internal_set_entry( sel
, entry
);
438 /***********************************************************************
441 * Free a %fs selector returned by wine_ldt_alloc_fs.
443 void wine_ldt_free_fs( unsigned short sel
)
445 if (is_gdt_sel(sel
)) return; /* nothing to do */
446 if (!((wine_get_fs() ^ sel
) & ~3))
448 /* FIXME: if freeing current %fs we cannot acquire locks */
450 internal_set_entry( sel
, &null_entry
);
451 wine_ldt_copy
.flags
[sel
>> 3] = 0;
453 else wine_ldt_free_entries( sel
, 1 );
457 /***********************************************************************
458 * selector access functions
460 __ASM_GLOBAL_FUNC( wine_get_cs
, "movw %cs,%ax\n\tret" )
461 __ASM_GLOBAL_FUNC( wine_get_ds
, "movw %ds,%ax\n\tret" )
462 __ASM_GLOBAL_FUNC( wine_get_es
, "movw %es,%ax\n\tret" )
463 __ASM_GLOBAL_FUNC( wine_get_fs
, "movw %fs,%ax\n\tret" )
464 __ASM_GLOBAL_FUNC( wine_get_gs
, "movw %gs,%ax\n\tret" )
465 __ASM_GLOBAL_FUNC( wine_get_ss
, "movw %ss,%ax\n\tret" )
466 __ASM_GLOBAL_FUNC( wine_set_fs
, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
467 __ASM_GLOBAL_FUNC( wine_set_gs
, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
469 #endif /* __i386__ */