push 52d6b63ba2f2d4f9b02b6b922d27bff05a60596f
[wine/hacks.git] / libs / wine / ldt.c
blob685ed6a2a125917a76d308e4a69b75d89d209eca
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wine/library.h"
36 #ifdef __i386__
38 #ifdef __linux__
40 #ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
42 #endif
44 struct modify_ldt_s
46 unsigned int entry_number;
47 unsigned long base_addr;
48 unsigned int limit;
49 unsigned int seg_32bit : 1;
50 unsigned int contents : 2;
51 unsigned int read_exec_only : 1;
52 unsigned int limit_in_pages : 1;
53 unsigned int seg_not_present : 1;
54 unsigned int usable : 1;
55 unsigned int garbage : 25;
58 static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
60 ptr->base_addr = (unsigned long)wine_ldt_get_base(entry);
61 ptr->limit = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
62 ptr->seg_32bit = entry->HighWord.Bits.Default_Big;
63 ptr->contents = (entry->HighWord.Bits.Type >> 2) & 3;
64 ptr->read_exec_only = !(entry->HighWord.Bits.Type & 2);
65 ptr->limit_in_pages = entry->HighWord.Bits.Granularity;
66 ptr->seg_not_present = !entry->HighWord.Bits.Pres;
67 ptr->usable = entry->HighWord.Bits.Sys;
68 ptr->garbage = 0;
71 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
73 int res;
74 __asm__ __volatile__( "pushl %%ebx\n\t"
75 "movl %2,%%ebx\n\t"
76 "int $0x80\n\t"
77 "popl %%ebx"
78 : "=a" (res)
79 : "0" (SYS_modify_ldt),
80 "r" (func),
81 "c" (ptr),
82 "d" (count),
83 "m" (*ptr) );
84 if (res >= 0) return res;
85 errno = -res;
86 return -1;
89 static inline int set_thread_area( struct modify_ldt_s *ptr )
91 int res;
92 __asm__ __volatile__( "pushl %%ebx\n\t"
93 "movl %3,%%ebx\n\t"
94 "int $0x80\n\t"
95 "popl %%ebx"
96 : "=a" (res), "=m" (*ptr)
97 : "0" (243) /* SYS_set_thread_area */, "q" (ptr), "m" (*ptr) );
98 if (res >= 0) return res;
99 errno = -res;
100 return -1;
103 #endif /* linux */
105 #if defined(__svr4__) || defined(_SCO_DS)
106 #include <sys/sysi86.h>
107 #ifndef __sun__
108 #include <sys/seg.h>
109 #endif
110 #endif
112 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
113 #include <machine/segments.h>
114 #include <machine/sysarch.h>
115 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
117 #ifdef __APPLE__
118 #include <i386/user_ldt.h>
119 #endif
121 #endif /* __i386__ */
123 /* local copy of the LDT */
124 #ifdef __APPLE__
125 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
126 #else
127 struct __wine_ldt_copy wine_ldt_copy;
128 #endif
130 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
132 #define LDT_FIRST_ENTRY 512
133 #define LDT_SIZE 8192
135 /* empty function for default locks */
136 static void nop(void) { }
138 static void (*lock_ldt)(void) = nop;
139 static void (*unlock_ldt)(void) = nop;
142 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
144 /***********************************************************************
145 * wine_ldt_init_locking
147 * Set the LDT locking/unlocking functions.
149 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
151 lock_ldt = lock_func;
152 unlock_ldt = unlock_func;
156 /***********************************************************************
157 * wine_ldt_get_entry
159 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
161 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
163 int index = sel >> 3;
165 if (is_gdt_sel(sel))
167 *entry = null_entry;
168 return;
170 lock_ldt();
171 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
173 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
174 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
175 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
177 else *entry = null_entry;
178 unlock_ldt();
182 /***********************************************************************
183 * internal_set_entry
185 * Set an LDT entry, without locking. For internal use only.
187 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
189 int ret = 0, index = sel >> 3;
191 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
193 #ifdef __i386__
195 #ifdef linux
197 struct modify_ldt_s ldt_info;
199 ldt_info.entry_number = index;
200 fill_modify_ldt_struct( &ldt_info, entry );
201 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
202 perror( "modify_ldt" );
204 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
206 LDT_ENTRY entry_copy = *entry;
207 /* The kernel will only let us set LDTs with user priority level */
208 if (entry_copy.HighWord.Bits.Pres
209 && entry_copy.HighWord.Bits.Dpl != 3)
210 entry_copy.HighWord.Bits.Dpl = 3;
211 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
212 if (ret < 0)
214 perror("i386_set_ldt");
215 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
216 exit(1);
219 #elif defined(__svr4__) || defined(_SCO_DS)
221 struct ssd ldt_mod;
222 ldt_mod.sel = sel;
223 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
224 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
225 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
226 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
227 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
229 #elif defined(__APPLE__)
230 if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
231 perror("i386_set_ldt");
232 #else
233 fprintf( stderr, "No LDT support on this platform\n" );
234 exit(1);
235 #endif
237 #endif /* __i386__ */
239 if (ret >= 0)
241 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
242 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
243 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
244 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
245 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
247 return ret;
251 /***********************************************************************
252 * wine_ldt_set_entry
254 * Set an LDT entry.
256 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
258 int ret;
260 lock_ldt();
261 ret = internal_set_entry( sel, entry );
262 unlock_ldt();
263 return ret;
267 /***********************************************************************
268 * wine_ldt_is_system
270 * Check if the selector is a system selector (i.e. not managed by Wine).
272 int wine_ldt_is_system( unsigned short sel )
274 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
278 /***********************************************************************
279 * wine_ldt_get_ptr
281 * Convert a segment:offset pair to a linear pointer.
282 * Note: we don't lock the LDT since this has to be fast.
284 void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
286 int index;
288 if (is_gdt_sel(sel)) /* GDT selector */
289 return (void *)offset;
290 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
291 return (void *)offset;
292 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
293 return (char *)wine_ldt_copy.base[index] + offset;
297 /***********************************************************************
298 * wine_ldt_alloc_entries
300 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
301 * Return a selector for the first entry.
303 unsigned short wine_ldt_alloc_entries( int count )
305 int i, index, size = 0;
307 if (count <= 0) return 0;
308 lock_ldt();
309 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
311 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
312 else if (++size >= count) /* found a large enough block */
314 index = i - size + 1;
316 /* mark selectors as allocated */
317 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
318 unlock_ldt();
319 return (index << 3) | 7;
322 unlock_ldt();
323 return 0;
327 /***********************************************************************
328 * wine_ldt_realloc_entries
330 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
331 * Return a selector for the first entry.
333 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
335 int i;
337 if (oldcount < newcount) /* we need to add selectors */
339 int index = sel >> 3;
341 lock_ldt();
342 /* check if the next selectors are free */
343 if (index + newcount > LDT_SIZE) i = oldcount;
344 else
345 for (i = oldcount; i < newcount; i++)
346 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
348 if (i < newcount) /* they are not free */
350 wine_ldt_free_entries( sel, oldcount );
351 sel = wine_ldt_alloc_entries( newcount );
353 else /* mark the selectors as allocated */
355 for (i = oldcount; i < newcount; i++)
356 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
358 unlock_ldt();
360 else if (oldcount > newcount) /* we need to remove selectors */
362 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
364 return sel;
368 /***********************************************************************
369 * wine_ldt_free_entries
371 * Free a number of consecutive ldt entries and clear their contents.
373 void wine_ldt_free_entries( unsigned short sel, int count )
375 int index;
377 lock_ldt();
378 for (index = sel >> 3; count > 0; count--, index++)
380 internal_set_entry( sel, &null_entry );
381 wine_ldt_copy.flags[index] = 0;
383 unlock_ldt();
387 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
389 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
391 /***********************************************************************
392 * wine_ldt_alloc_fs
394 * Allocate an LDT entry for a %fs selector, reusing a global
395 * GDT selector if possible. Return the selector value.
397 unsigned short wine_ldt_alloc_fs(void)
399 if (global_fs_sel == -1)
401 #ifdef __linux__
402 struct modify_ldt_s ldt_info;
403 int ret;
405 /* the preloader may have allocated it already */
406 global_fs_sel = wine_get_fs();
407 if (global_fs_sel && is_gdt_sel(global_fs_sel)) return global_fs_sel;
409 ldt_info.entry_number = -1;
410 fill_modify_ldt_struct( &ldt_info, &null_entry );
411 if ((ret = set_thread_area( &ldt_info ) < 0))
413 global_fs_sel = 0; /* don't try it again */
414 if (errno != ENOSYS) perror( "set_thread_area" );
416 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
417 #elif defined(__FreeBSD__)
418 global_fs_sel = GSEL( GUFS_SEL, SEL_UPL );
419 #endif
421 if (global_fs_sel > 0) return global_fs_sel;
422 return wine_ldt_alloc_entries( 1 );
426 /***********************************************************************
427 * wine_ldt_init_fs
429 * Initialize the entry for the %fs selector of the current thread, and
430 * set the thread %fs register.
432 * Note: this runs in the context of the new thread, so cannot acquire locks.
434 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
436 if ((sel & ~3) == (global_fs_sel & ~3))
438 #ifdef __linux__
439 struct modify_ldt_s ldt_info;
440 int ret;
442 ldt_info.entry_number = sel >> 3;
443 fill_modify_ldt_struct( &ldt_info, entry );
444 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
445 #elif defined(__FreeBSD__)
446 i386_set_fsbase( wine_ldt_get_base( entry ));
447 #endif
449 else /* LDT selector */
451 internal_set_entry( sel, entry );
453 wine_set_fs( sel );
457 /***********************************************************************
458 * wine_ldt_free_fs
460 * Free a %fs selector returned by wine_ldt_alloc_fs.
462 void wine_ldt_free_fs( unsigned short sel )
464 if (is_gdt_sel(sel)) return; /* nothing to do */
465 if (!((wine_get_fs() ^ sel) & ~3))
467 /* FIXME: if freeing current %fs we cannot acquire locks */
468 wine_set_fs( 0 );
469 internal_set_entry( sel, &null_entry );
470 wine_ldt_copy.flags[sel >> 3] = 0;
472 else wine_ldt_free_entries( sel, 1 );
476 /***********************************************************************
477 * selector access functions
479 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
480 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
481 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
482 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
483 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
484 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
485 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
486 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
488 #endif /* __i386__ */