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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
34 #define WINE_EXPORT_LDT_COPY
35 #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 useable : 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->useable = entry->HighWord.Bits.Sys;
72 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
75 __asm__ __volatile__( "pushl %%ebx\n\t"
80 : "0" (SYS_modify_ldt),
84 if (res >= 0) return res;
89 static inline int set_thread_area( struct modify_ldt_s *ptr )
92 __asm__ __volatile__( "pushl %%ebx\n\t"
97 : "0" (243) /* SYS_set_thread_area */, "q" (ptr) );
98 if (res >= 0) return res;
105 #if defined(__svr4__) || defined(_SCO_DS)
106 #include <sys/sysi86.h>
107 extern int sysi86(int,void*);
113 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
114 #include <machine/segments.h>
116 extern int i386_get_ldt(int, union descriptor *, int);
117 extern int i386_set_ldt(int, union descriptor *, int);
118 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
122 static inline int thread_set_user_ldt( const void *addr, unsigned int size, unsigned int flags )
125 __asm__ __volatile__ ("pushl %4\n\t"
130 "leal 16(%%esp),%%esp"
132 : "0" (4 /*thread_set_user_ldt*/), "r" (addr), "r" (size), "r" (flags) );
136 #endif /* __APPLE__ */
138 #endif /* __i386__ */
140 /* local copy of the LDT */
142 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
144 struct __wine_ldt_copy wine_ldt_copy;
147 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
149 #define LDT_FIRST_ENTRY 512
150 #define LDT_SIZE 8192
152 /* empty function for default locks */
153 static void nop(void) { }
155 static void (*lock_ldt)(void) = nop;
156 static void (*unlock_ldt)(void) = nop;
159 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
161 /***********************************************************************
162 * wine_ldt_init_locking
164 * Set the LDT locking/unlocking functions.
166 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
168 lock_ldt = lock_func;
169 unlock_ldt = unlock_func;
173 /***********************************************************************
176 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
178 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
180 int index = sel >> 3;
188 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
190 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
191 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
192 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
194 else *entry = null_entry;
199 /***********************************************************************
202 * Set an LDT entry, without locking. For internal use only.
204 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
206 int ret = 0, index = sel >> 3;
208 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
214 struct modify_ldt_s ldt_info;
216 ldt_info.entry_number = index;
217 fill_modify_ldt_struct( &ldt_info, entry );
218 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
219 perror( "modify_ldt" );
221 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
223 LDT_ENTRY entry_copy = *entry;
224 /* The kernel will only let us set LDTs with user priority level */
225 if (entry_copy.HighWord.Bits.Pres
226 && entry_copy.HighWord.Bits.Dpl != 3)
227 entry_copy.HighWord.Bits.Dpl = 3;
228 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
231 perror("i386_set_ldt");
232 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
236 #elif defined(__svr4__) || defined(_SCO_DS)
240 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
241 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
242 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
243 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
244 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
247 fprintf( stderr, "No LDT support on this platform\n" );
251 #endif /* __i386__ */
255 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
256 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
257 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
258 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
259 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
265 /***********************************************************************
270 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
275 ret = internal_set_entry( sel, entry );
281 /***********************************************************************
284 * Check if the selector is a system selector (i.e. not managed by Wine).
286 int wine_ldt_is_system( unsigned short sel )
288 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
292 /***********************************************************************
295 * Convert a segment:offset pair to a linear pointer.
296 * Note: we don't lock the LDT since this has to be fast.
298 void *wine_ldt_get_ptr( unsigned short sel, unsigned int offset )
302 if (is_gdt_sel(sel)) /* GDT selector */
303 return (void *)offset;
304 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
305 return (void *)offset;
306 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
307 return (char *)wine_ldt_copy.base[index] + offset;
311 /***********************************************************************
312 * wine_ldt_alloc_entries
314 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
315 * Return a selector for the first entry.
317 unsigned short wine_ldt_alloc_entries( int count )
319 int i, index, size = 0;
321 if (count <= 0) return 0;
323 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
325 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
326 else if (++size >= count) /* found a large enough block */
328 index = i - size + 1;
330 /* mark selectors as allocated */
331 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
333 return (index << 3) | 7;
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( 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.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
362 if (i < newcount) /* they are not free */
364 wine_ldt_free_entries( sel, oldcount );
365 sel = wine_ldt_alloc_entries( newcount );
367 else /* mark the selectors as allocated */
369 for (i = oldcount; i < newcount; i++)
370 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
374 else if (oldcount > newcount) /* we need to remove selectors */
376 wine_ldt_free_entries( 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( 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.flags[index] = 0;
403 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
405 /***********************************************************************
408 * Allocate an LDT entry for a %fs selector, reusing a global
409 * GDT selector if possible. Return the selector value.
411 unsigned short wine_ldt_alloc_fs(void)
413 if (global_fs_sel == -1)
416 struct modify_ldt_s ldt_info;
419 ldt_info.entry_number = -1;
420 fill_modify_ldt_struct( &ldt_info, &null_entry );
421 if ((ret = set_thread_area( &ldt_info ) < 0))
423 global_fs_sel = 0; /* don't try it again */
424 if (errno != ENOSYS) perror( "set_thread_area" );
426 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
427 #elif defined(__APPLE__)
428 int ret = thread_set_user_ldt( NULL, 0, 0 );
429 if (ret != -1) global_fs_sel = ret;
430 else global_fs_sel = 0;
431 #endif /* __APPLE__ */
433 if (global_fs_sel > 0) return global_fs_sel;
434 return wine_ldt_alloc_entries( 1 );
438 /***********************************************************************
441 * Initialize the entry for the %fs selector of the current thread, and
442 * set the thread %fs register.
444 * Note: this runs in the context of the new thread, so cannot acquire locks.
446 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
448 if ((sel & ~3) == (global_fs_sel & ~3))
451 struct modify_ldt_s ldt_info;
454 ldt_info.entry_number = sel >> 3;
455 fill_modify_ldt_struct( &ldt_info, entry );
456 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
457 #elif defined(__APPLE__)
458 int ret = thread_set_user_ldt( wine_ldt_get_base(entry), wine_ldt_get_limit(entry), 0 );
459 if (ret == -1) perror( "thread_set_user_ldt" );
460 else assert( ret == global_fs_sel );
461 #endif /* __APPLE__ */
463 else /* LDT selector */
465 internal_set_entry( sel, entry );
471 /***********************************************************************
474 * Free a %fs selector returned by wine_ldt_alloc_fs.
476 void wine_ldt_free_fs( unsigned short sel )
478 if (is_gdt_sel(sel)) return; /* nothing to do */
479 if (!((wine_get_fs() ^ sel) & ~3))
481 /* FIXME: if freeing current %fs we cannot acquire locks */
483 internal_set_entry( sel, &null_entry );
484 wine_ldt_copy.flags[sel >> 3] = 0;
486 else wine_ldt_free_entries( sel, 1 );
490 /***********************************************************************
491 * selector access functions
494 /* Nothing needs to be done for MS C, it will do with inline versions from the winnt.h */
495 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
496 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
497 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
498 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
499 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
500 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
501 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
502 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
503 # endif /* defined(_MSC_VER) */
505 #endif /* __i386__ */