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
29 #include <sys/types.h>
41 /* the local copy of the LDT */
42 struct __wine_ldt_copy
44 void *base
[8192]; /* base address or 0 if entry is free */
45 unsigned long limit
[8192]; /* limit in bytes or 0 if entry is free */
46 unsigned char flags
[8192]; /* flags (defined below) */
47 } wine_ldt_copy_obsolete
= { { 0, 0, 0 } };
49 #define WINE_LDT_FLAGS_32BIT 0x40 /* Segment is 32-bit (code or stack) */
50 #define WINE_LDT_FLAGS_ALLOCATED 0x80 /* Segment is allocated (no longer free) */
52 static inline void *wine_ldt_get_base( const LDT_ENTRY
*ent
)
54 return (void *)(ent
->BaseLow
|
55 (ULONG_PTR
)ent
->HighWord
.Bits
.BaseMid
<< 16 |
56 (ULONG_PTR
)ent
->HighWord
.Bits
.BaseHi
<< 24);
58 static inline unsigned int wine_ldt_get_limit( const LDT_ENTRY
*ent
)
60 unsigned int limit
= ent
->LimitLow
| (ent
->HighWord
.Bits
.LimitHi
<< 16);
61 if (ent
->HighWord
.Bits
.Granularity
) limit
= (limit
<< 12) | 0xfff;
67 #ifdef HAVE_SYS_SYSCALL_H
68 # include <sys/syscall.h>
73 unsigned int entry_number
;
74 unsigned long base_addr
;
76 unsigned int seg_32bit
: 1;
77 unsigned int contents
: 2;
78 unsigned int read_exec_only
: 1;
79 unsigned int limit_in_pages
: 1;
80 unsigned int seg_not_present
: 1;
81 unsigned int usable
: 1;
82 unsigned int garbage
: 25;
85 static inline void fill_modify_ldt_struct( struct modify_ldt_s
*ptr
, const LDT_ENTRY
*entry
)
87 ptr
->base_addr
= (unsigned long)wine_ldt_get_base(entry
);
88 ptr
->limit
= entry
->LimitLow
| (entry
->HighWord
.Bits
.LimitHi
<< 16);
89 ptr
->seg_32bit
= entry
->HighWord
.Bits
.Default_Big
;
90 ptr
->contents
= (entry
->HighWord
.Bits
.Type
>> 2) & 3;
91 ptr
->read_exec_only
= !(entry
->HighWord
.Bits
.Type
& 2);
92 ptr
->limit_in_pages
= entry
->HighWord
.Bits
.Granularity
;
93 ptr
->seg_not_present
= !entry
->HighWord
.Bits
.Pres
;
94 ptr
->usable
= entry
->HighWord
.Bits
.Sys
;
98 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
, unsigned long count
)
100 return syscall( 123 /* SYS_modify_ldt */, func
, ptr
, count
);
103 static inline int set_thread_area( struct modify_ldt_s
*ptr
)
105 return syscall( 243 /* SYS_set_thread_area */, ptr
);
110 #if defined(__svr4__) || defined(_SCO_DS)
111 #include <sys/sysi86.h>
117 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
118 #include <machine/segments.h>
119 #include <machine/sysarch.h>
120 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
123 #include <mach/i386/mach_i386.h>
124 #include <mach/mach_traps.h>
128 #include <i386/user_ldt.h>
131 static const LDT_ENTRY null_entry
; /* all-zeros, used to clear LDT entries */
133 #define LDT_FIRST_ENTRY 512
134 #define LDT_SIZE 8192
136 /* empty function for default locks */
137 static void nop(void) { }
139 static void (*lock_ldt
)(void) = nop
;
140 static void (*unlock_ldt
)(void) = nop
;
143 static inline int is_gdt_sel( unsigned short sel
) { return !(sel
& 4); }
145 /***********************************************************************
146 * wine_ldt_init_locking
148 * Set the LDT locking/unlocking functions.
150 void wine_ldt_init_locking_obsolete( void (*lock_func
)(void), void (*unlock_func
)(void) )
152 lock_ldt
= lock_func
;
153 unlock_ldt
= unlock_func
;
157 /***********************************************************************
160 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
162 void wine_ldt_get_entry_obsolete( unsigned short sel
, LDT_ENTRY
*entry
)
164 int index
= sel
>> 3;
172 if (wine_ldt_copy_obsolete
.flags
[index
] & WINE_LDT_FLAGS_ALLOCATED
)
174 ULONG_PTR base
= (ULONG_PTR
)wine_ldt_copy_obsolete
.base
[index
];
175 ULONG limit
= wine_ldt_copy_obsolete
.limit
[index
];
177 entry
->BaseLow
= (WORD
)base
;
178 entry
->HighWord
.Bits
.BaseMid
= (BYTE
)(base
>> 16);
179 entry
->HighWord
.Bits
.BaseHi
= (BYTE
)(base
>> 24);
180 if ((entry
->HighWord
.Bits
.Granularity
= (limit
>= 0x100000))) limit
>>= 12;
181 entry
->LimitLow
= (WORD
)limit
;
182 entry
->HighWord
.Bits
.LimitHi
= (limit
>> 16);
183 entry
->HighWord
.Bits
.Dpl
= 3;
184 entry
->HighWord
.Bits
.Pres
= 1;
185 entry
->HighWord
.Bits
.Type
= wine_ldt_copy_obsolete
.flags
[index
];
186 entry
->HighWord
.Bits
.Sys
= 0;
187 entry
->HighWord
.Bits
.Reserved_0
= 0;
188 entry
->HighWord
.Bits
.Default_Big
= !!(wine_ldt_copy_obsolete
.flags
[index
] & WINE_LDT_FLAGS_32BIT
);
190 else *entry
= null_entry
;
195 /***********************************************************************
198 * Set an LDT entry, without locking. For internal use only.
200 static int internal_set_entry( unsigned short sel
, const LDT_ENTRY
*entry
)
202 int ret
= 0, index
= sel
>> 3;
204 if (index
< LDT_FIRST_ENTRY
) return 0; /* cannot modify reserved entries */
208 struct modify_ldt_s ldt_info
;
210 ldt_info
.entry_number
= index
;
211 fill_modify_ldt_struct( &ldt_info
, entry
);
212 if ((ret
= modify_ldt(0x11, &ldt_info
, sizeof(ldt_info
))) < 0)
213 perror( "modify_ldt" );
215 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
217 LDT_ENTRY entry_copy
= *entry
;
218 /* The kernel will only let us set LDTs with user priority level */
219 if (entry_copy
.HighWord
.Bits
.Pres
220 && entry_copy
.HighWord
.Bits
.Dpl
!= 3)
221 entry_copy
.HighWord
.Bits
.Dpl
= 3;
222 ret
= i386_set_ldt(index
, (union descriptor
*)&entry_copy
, 1);
225 perror("i386_set_ldt");
226 fprintf( stderr
, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
230 #elif defined(__svr4__) || defined(_SCO_DS)
234 ldt_mod
.bo
= (unsigned long)wine_ldt_get_base(entry
);
235 ldt_mod
.ls
= entry
->LimitLow
| (entry
->HighWord
.Bits
.LimitHi
<< 16);
236 ldt_mod
.acc1
= entry
->HighWord
.Bytes
.Flags1
;
237 ldt_mod
.acc2
= entry
->HighWord
.Bytes
.Flags2
>> 4;
238 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
240 #elif defined(__APPLE__)
241 if ((ret
= i386_set_ldt(index
, (union ldt_entry
*)entry
, 1)) < 0)
242 perror("i386_set_ldt");
243 #elif defined(__GNU__)
244 if ((ret
= i386_set_ldt(mach_thread_self(), sel
, (descriptor_list_t
)entry
, 1)) != KERN_SUCCESS
)
245 perror("i386_set_ldt");
247 fprintf( stderr
, "No LDT support on this platform\n" );
253 wine_ldt_copy_obsolete
.base
[index
] = wine_ldt_get_base(entry
);
254 wine_ldt_copy_obsolete
.limit
[index
] = wine_ldt_get_limit(entry
);
255 wine_ldt_copy_obsolete
.flags
[index
] = (entry
->HighWord
.Bits
.Type
|
256 (entry
->HighWord
.Bits
.Default_Big
? WINE_LDT_FLAGS_32BIT
: 0) |
257 (wine_ldt_copy_obsolete
.flags
[index
] & WINE_LDT_FLAGS_ALLOCATED
));
263 /***********************************************************************
268 int wine_ldt_set_entry_obsolete( unsigned short sel
, const LDT_ENTRY
*entry
)
273 ret
= internal_set_entry( sel
, entry
);
279 /***********************************************************************
282 * Check if the selector is a system selector (i.e. not managed by Wine).
284 int wine_ldt_is_system_obsolete( unsigned short sel
)
286 return is_gdt_sel(sel
) || ((sel
>> 3) < LDT_FIRST_ENTRY
);
290 /***********************************************************************
293 * Convert a segment:offset pair to a linear pointer.
294 * Note: we don't lock the LDT since this has to be fast.
296 void *wine_ldt_get_ptr_obsolete( unsigned short sel
, unsigned long offset
)
300 if (is_gdt_sel(sel
)) /* GDT selector */
301 return (void *)offset
;
302 if ((index
= (sel
>> 3)) < LDT_FIRST_ENTRY
) /* system selector */
303 return (void *)offset
;
304 if (!(wine_ldt_copy_obsolete
.flags
[index
] & WINE_LDT_FLAGS_32BIT
)) offset
&= 0xffff;
305 return (char *)wine_ldt_copy_obsolete
.base
[index
] + offset
;
309 /***********************************************************************
310 * wine_ldt_alloc_entries
312 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
313 * Return a selector for the first entry.
315 unsigned short wine_ldt_alloc_entries_obsolete( int count
)
317 int i
, index
, size
= 0;
319 if (count
<= 0) return 0;
321 for (i
= LDT_FIRST_ENTRY
; i
< LDT_SIZE
; i
++)
323 if (wine_ldt_copy_obsolete
.flags
[i
] & WINE_LDT_FLAGS_ALLOCATED
) size
= 0;
324 else if (++size
>= count
) /* found a large enough block */
326 index
= i
- size
+ 1;
328 /* mark selectors as allocated */
329 for (i
= 0; i
< count
; i
++) wine_ldt_copy_obsolete
.flags
[index
+ i
] |= WINE_LDT_FLAGS_ALLOCATED
;
331 return (index
<< 3) | 7;
339 void wine_ldt_free_entries_obsolete( unsigned short sel
, int count
);
341 /***********************************************************************
342 * wine_ldt_realloc_entries
344 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
345 * Return a selector for the first entry.
347 unsigned short wine_ldt_realloc_entries_obsolete( unsigned short sel
, int oldcount
, int newcount
)
351 if (oldcount
< newcount
) /* we need to add selectors */
353 int index
= sel
>> 3;
356 /* check if the next selectors are free */
357 if (index
+ newcount
> LDT_SIZE
) i
= oldcount
;
359 for (i
= oldcount
; i
< newcount
; i
++)
360 if (wine_ldt_copy_obsolete
.flags
[index
+i
] & WINE_LDT_FLAGS_ALLOCATED
) break;
362 if (i
< newcount
) /* they are not free */
364 wine_ldt_free_entries_obsolete( sel
, oldcount
);
365 sel
= wine_ldt_alloc_entries_obsolete( newcount
);
367 else /* mark the selectors as allocated */
369 for (i
= oldcount
; i
< newcount
; i
++)
370 wine_ldt_copy_obsolete
.flags
[index
+i
] |= WINE_LDT_FLAGS_ALLOCATED
;
374 else if (oldcount
> newcount
) /* we need to remove selectors */
376 wine_ldt_free_entries_obsolete( sel
+ (newcount
<< 3), newcount
- oldcount
);
382 /***********************************************************************
383 * wine_ldt_free_entries
385 * Free a number of consecutive ldt entries and clear their contents.
387 void wine_ldt_free_entries_obsolete( unsigned short sel
, int count
)
392 for (index
= sel
>> 3; count
> 0; count
--, index
++)
394 internal_set_entry( sel
, &null_entry
);
395 wine_ldt_copy_obsolete
.flags
[index
] = 0;
401 static int global_fs_sel
= -1; /* global selector for %fs shared among all threads */
403 /***********************************************************************
406 * Allocate an LDT entry for a %fs selector, reusing a global
407 * GDT selector if possible. Return the selector value.
409 unsigned short wine_ldt_alloc_fs_obsolete(void)
411 if (global_fs_sel
== -1)
414 struct modify_ldt_s ldt_info
;
417 /* the preloader may have allocated it already */
418 __asm__( "mov %%fs,%0" : "=r" (global_fs_sel
) );
419 if (global_fs_sel
&& is_gdt_sel(global_fs_sel
)) return global_fs_sel
;
421 memset( &ldt_info
, 0, sizeof(ldt_info
) );
422 ldt_info
.entry_number
= -1;
423 ldt_info
.seg_32bit
= 1;
425 if ((ret
= set_thread_area( &ldt_info
) < 0))
427 global_fs_sel
= 0; /* don't try it again */
428 if (errno
!= ENOSYS
) perror( "set_thread_area" );
430 else global_fs_sel
= (ldt_info
.entry_number
<< 3) | 3;
431 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
432 global_fs_sel
= GSEL( GUFS_SEL
, SEL_UPL
);
435 if (global_fs_sel
> 0) return global_fs_sel
;
436 return wine_ldt_alloc_entries_obsolete( 1 );
440 /***********************************************************************
443 * Initialize the entry for the %fs selector of the current thread, and
444 * set the thread %fs register.
446 * Note: this runs in the context of the new thread, so cannot acquire locks.
448 void wine_ldt_init_fs_obsolete( unsigned short sel
, const LDT_ENTRY
*entry
)
450 if ((sel
& ~3) == (global_fs_sel
& ~3))
453 struct modify_ldt_s ldt_info
;
456 ldt_info
.entry_number
= sel
>> 3;
457 fill_modify_ldt_struct( &ldt_info
, entry
);
458 if ((ret
= set_thread_area( &ldt_info
) < 0)) perror( "set_thread_area" );
459 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
460 i386_set_fsbase( wine_ldt_get_base( entry
));
463 else /* LDT selector */
465 internal_set_entry( sel
, entry
);
467 __asm__( "mov %0,%%fs" :: "r" (sel
) );
471 /***********************************************************************
474 * Free a %fs selector returned by wine_ldt_alloc_fs.
476 void wine_ldt_free_fs_obsolete( unsigned short sel
)
480 if (is_gdt_sel(sel
)) return; /* nothing to do */
481 __asm__( "mov %%fs,%0" : "=r" (fs
) );
482 if (!((fs
^ sel
) & ~3))
484 /* FIXME: if freeing current %fs we cannot acquire locks */
485 __asm__( "mov %0,%%fs" :: "r" (0) );
486 internal_set_entry( sel
, &null_entry
);
487 wine_ldt_copy_obsolete
.flags
[sel
>> 3] = 0;
489 else wine_ldt_free_entries_obsolete( sel
, 1 );
493 /***********************************************************************
494 * selector access functions
496 __ASM_GLOBAL_FUNC( wine_get_cs_obsolete
, "movw %cs,%ax\n\tret" )
497 __ASM_GLOBAL_FUNC( wine_get_ds_obsolete
, "movw %ds,%ax\n\tret" )
498 __ASM_GLOBAL_FUNC( wine_get_es_obsolete
, "movw %es,%ax\n\tret" )
499 __ASM_GLOBAL_FUNC( wine_get_fs_obsolete
, "movw %fs,%ax\n\tret" )
500 __ASM_GLOBAL_FUNC( wine_get_gs_obsolete
, "movw %gs,%ax\n\tret" )
501 __ASM_GLOBAL_FUNC( wine_get_ss_obsolete
, "movw %ss,%ax\n\tret" )
502 __ASM_GLOBAL_FUNC( wine_set_fs_obsolete
, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
503 __ASM_GLOBAL_FUNC( wine_set_gs_obsolete
, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
506 __ASM_OBSOLETE(wine_ldt_alloc_entries
);
507 __ASM_OBSOLETE(wine_ldt_alloc_fs
);
508 __ASM_OBSOLETE(wine_ldt_copy
);
509 __ASM_OBSOLETE(wine_ldt_free_entries
);
510 __ASM_OBSOLETE(wine_ldt_free_fs
);
511 __ASM_OBSOLETE(wine_ldt_get_entry
);
512 __ASM_OBSOLETE(wine_ldt_get_ptr
);
513 __ASM_OBSOLETE(wine_ldt_init_fs
);
514 __ASM_OBSOLETE(wine_ldt_init_locking
);
515 __ASM_OBSOLETE(wine_ldt_is_system
);
516 __ASM_OBSOLETE(wine_ldt_realloc_entries
);
517 __ASM_OBSOLETE(wine_ldt_set_entry
);
518 __ASM_OBSOLETE(wine_get_cs
);
519 __ASM_OBSOLETE(wine_get_ds
);
520 __ASM_OBSOLETE(wine_get_es
);
521 __ASM_OBSOLETE(wine_get_fs
);
522 __ASM_OBSOLETE(wine_get_gs
);
523 __ASM_OBSOLETE(wine_get_ss
);
524 __ASM_OBSOLETE(wine_set_fs
);
525 __ASM_OBSOLETE(wine_set_gs
);
527 #endif /* __ASM_OBSOLETE */
529 #endif /* __i386__ */