libwine: Set the default bindir and dlldir from argv0 if dladdr is not available.
[wine] / libs / wine / ldt.c
1 /*
2  * LDT manipulation functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #define WINE_EXPORT_LDT_COPY
35 #include "wine/library.h"
36
37 #ifdef __i386__
38
39 #ifdef linux
40
41 #ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
43 #endif
44
45 struct modify_ldt_s
46 {
47     unsigned int  entry_number;
48     unsigned long base_addr;
49     unsigned int  limit;
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;
57 };
58
59 static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
60 {
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;
69     ptr->garbage         = 0;
70 }
71
72 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
73 {
74     int res;
75     __asm__ __volatile__( "pushl %%ebx\n\t"
76                           "movl %2,%%ebx\n\t"
77                           "int $0x80\n\t"
78                           "popl %%ebx"
79                           : "=a" (res)
80                           : "0" (SYS_modify_ldt),
81                             "r" (func),
82                             "c" (ptr),
83                             "d" (count) );
84     if (res >= 0) return res;
85     errno = -res;
86     return -1;
87 }
88
89 static inline int set_thread_area( struct modify_ldt_s *ptr )
90 {
91     int res;
92     __asm__ __volatile__( "pushl %%ebx\n\t"
93                           "movl %2,%%ebx\n\t"
94                           "int $0x80\n\t"
95                           "popl %%ebx"
96                           : "=a" (res)
97                           : "0" (243) /* SYS_set_thread_area */, "q" (ptr) );
98     if (res >= 0) return res;
99     errno = -res;
100     return -1;
101 }
102
103 #endif  /* linux */
104
105 #if defined(__svr4__) || defined(_SCO_DS)
106 #include <sys/sysi86.h>
107 #ifndef __sun__
108 #include <sys/seg.h>
109 #endif
110 #endif
111
112 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
113 #include <machine/segments.h>
114
115 extern int i386_get_ldt(int, union descriptor *, int);
116 extern int i386_set_ldt(int, union descriptor *, int);
117 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
118
119 #ifdef __APPLE__
120 #include <i386/user_ldt.h>
121 #endif
122
123 #endif  /* __i386__ */
124
125 /* local copy of the LDT */
126 #ifdef __APPLE__
127 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
128 #else
129 struct __wine_ldt_copy wine_ldt_copy;
130 #endif
131
132 static const LDT_ENTRY null_entry;  /* all-zeros, used to clear LDT entries */
133
134 #define LDT_FIRST_ENTRY 512
135 #define LDT_SIZE 8192
136
137 /* empty function for default locks */
138 static void nop(void) { }
139
140 static void (*lock_ldt)(void) = nop;
141 static void (*unlock_ldt)(void) = nop;
142
143
144 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
145
146 /***********************************************************************
147  *           wine_ldt_init_locking
148  *
149  * Set the LDT locking/unlocking functions.
150  */
151 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
152 {
153     lock_ldt = lock_func;
154     unlock_ldt = unlock_func;
155 }
156
157
158 /***********************************************************************
159  *           wine_ldt_get_entry
160  *
161  * Retrieve an LDT entry. Return a null entry if selector is not allocated.
162  */
163 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
164 {
165     int index = sel >> 3;
166
167     if (is_gdt_sel(sel))
168     {
169         *entry = null_entry;
170         return;
171     }
172     lock_ldt();
173     if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
174     {
175         wine_ldt_set_base(  entry, wine_ldt_copy.base[index] );
176         wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
177         wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
178     }
179     else *entry = null_entry;
180     unlock_ldt();
181 }
182
183
184 /***********************************************************************
185  *           internal_set_entry
186  *
187  * Set an LDT entry, without locking. For internal use only.
188  */
189 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
190 {
191     int ret = 0, index = sel >> 3;
192
193     if (index < LDT_FIRST_ENTRY) return 0;  /* cannot modify reserved entries */
194
195 #ifdef __i386__
196
197 #ifdef linux
198     {
199         struct modify_ldt_s ldt_info;
200
201         ldt_info.entry_number = index;
202         fill_modify_ldt_struct( &ldt_info, entry );
203         if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
204             perror( "modify_ldt" );
205     }
206 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
207     {
208         LDT_ENTRY entry_copy = *entry;
209         /* The kernel will only let us set LDTs with user priority level */
210         if (entry_copy.HighWord.Bits.Pres
211             && entry_copy.HighWord.Bits.Dpl != 3)
212                 entry_copy.HighWord.Bits.Dpl = 3;
213         ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
214         if (ret < 0)
215         {
216             perror("i386_set_ldt");
217             fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
218             exit(1);
219         }
220     }
221 #elif defined(__svr4__) || defined(_SCO_DS)
222     {
223         struct ssd ldt_mod;
224         ldt_mod.sel  = sel;
225         ldt_mod.bo   = (unsigned long)wine_ldt_get_base(entry);
226         ldt_mod.ls   = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
227         ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
228         ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
229         if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
230     }
231 #elif defined(__APPLE__)
232     if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
233         perror("i386_set_ldt");
234 #else
235     fprintf( stderr, "No LDT support on this platform\n" );
236     exit(1);
237 #endif
238
239 #endif  /* __i386__ */
240
241     if (ret >= 0)
242     {
243         wine_ldt_copy.base[index]  = wine_ldt_get_base(entry);
244         wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
245         wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
246                                  (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
247                                  (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
248     }
249     return ret;
250 }
251
252
253 /***********************************************************************
254  *           wine_ldt_set_entry
255  *
256  * Set an LDT entry.
257  */
258 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
259 {
260     int ret;
261
262     lock_ldt();
263     ret = internal_set_entry( sel, entry );
264     unlock_ldt();
265     return ret;
266 }
267
268
269 /***********************************************************************
270  *           wine_ldt_is_system
271  *
272  * Check if the selector is a system selector (i.e. not managed by Wine).
273  */
274 int wine_ldt_is_system( unsigned short sel )
275 {
276     return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
277 }
278
279
280 /***********************************************************************
281  *           wine_ldt_get_ptr
282  *
283  * Convert a segment:offset pair to a linear pointer.
284  * Note: we don't lock the LDT since this has to be fast.
285  */
286 void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
287 {
288     int index;
289
290     if (is_gdt_sel(sel))  /* GDT selector */
291         return (void *)offset;
292     if ((index = (sel >> 3)) < LDT_FIRST_ENTRY)  /* system selector */
293         return (void *)offset;
294     if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
295     return (char *)wine_ldt_copy.base[index] + offset;
296 }
297
298
299 /***********************************************************************
300  *           wine_ldt_alloc_entries
301  *
302  * Allocate a number of consecutive ldt entries, without setting the LDT contents.
303  * Return a selector for the first entry.
304  */
305 unsigned short wine_ldt_alloc_entries( int count )
306 {
307     int i, index, size = 0;
308
309     if (count <= 0) return 0;
310     lock_ldt();
311     for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
312     {
313         if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
314         else if (++size >= count)  /* found a large enough block */
315         {
316             index = i - size + 1;
317
318             /* mark selectors as allocated */
319             for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
320             unlock_ldt();
321             return (index << 3) | 7;
322         }
323     }
324     unlock_ldt();
325     return 0;
326 }
327
328
329 /***********************************************************************
330  *           wine_ldt_realloc_entries
331  *
332  * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
333  * Return a selector for the first entry.
334  */
335 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
336 {
337     int i;
338
339     if (oldcount < newcount)  /* we need to add selectors */
340     {
341         int index = sel >> 3;
342
343         lock_ldt();
344         /* check if the next selectors are free */
345         if (index + newcount > LDT_SIZE) i = oldcount;
346         else
347             for (i = oldcount; i < newcount; i++)
348                 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
349
350         if (i < newcount)  /* they are not free */
351         {
352             wine_ldt_free_entries( sel, oldcount );
353             sel = wine_ldt_alloc_entries( newcount );
354         }
355         else  /* mark the selectors as allocated */
356         {
357             for (i = oldcount; i < newcount; i++)
358                 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
359         }
360         unlock_ldt();
361     }
362     else if (oldcount > newcount) /* we need to remove selectors */
363     {
364         wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
365     }
366     return sel;
367 }
368
369
370 /***********************************************************************
371  *           wine_ldt_free_entries
372  *
373  * Free a number of consecutive ldt entries and clear their contents.
374  */
375 void wine_ldt_free_entries( unsigned short sel, int count )
376 {
377     int index;
378
379     lock_ldt();
380     for (index = sel >> 3; count > 0; count--, index++)
381     {
382         internal_set_entry( sel, &null_entry );
383         wine_ldt_copy.flags[index] = 0;
384     }
385     unlock_ldt();
386 }
387
388
389 #ifdef __i386__
390
391 static int global_fs_sel = -1;  /* global selector for %fs shared among all threads */
392
393 /***********************************************************************
394  *           wine_ldt_alloc_fs
395  *
396  * Allocate an LDT entry for a %fs selector, reusing a global
397  * GDT selector if possible. Return the selector value.
398  */
399 unsigned short wine_ldt_alloc_fs(void)
400 {
401     if (global_fs_sel == -1)
402     {
403 #ifdef __linux__
404         struct modify_ldt_s ldt_info;
405         int ret;
406
407         ldt_info.entry_number = -1;
408         fill_modify_ldt_struct( &ldt_info, &null_entry );
409         if ((ret = set_thread_area( &ldt_info ) < 0))
410         {
411             global_fs_sel = 0;  /* don't try it again */
412             if (errno != ENOSYS) perror( "set_thread_area" );
413         }
414         else global_fs_sel = (ldt_info.entry_number << 3) | 3;
415 #endif
416     }
417     if (global_fs_sel > 0) return global_fs_sel;
418     return wine_ldt_alloc_entries( 1 );
419 }
420
421
422 /***********************************************************************
423  *           wine_ldt_init_fs
424  *
425  * Initialize the entry for the %fs selector of the current thread, and
426  * set the thread %fs register.
427  *
428  * Note: this runs in the context of the new thread, so cannot acquire locks.
429  */
430 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
431 {
432     if ((sel & ~3) == (global_fs_sel & ~3))
433     {
434 #ifdef __linux__
435         struct modify_ldt_s ldt_info;
436         int ret;
437
438         ldt_info.entry_number = sel >> 3;
439         fill_modify_ldt_struct( &ldt_info, entry );
440         if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
441 #endif
442     }
443     else  /* LDT selector */
444     {
445         internal_set_entry( sel, entry );
446     }
447     wine_set_fs( sel );
448 }
449
450
451 /***********************************************************************
452  *           wine_ldt_free_fs
453  *
454  * Free a %fs selector returned by wine_ldt_alloc_fs.
455  */
456 void wine_ldt_free_fs( unsigned short sel )
457 {
458     if (is_gdt_sel(sel)) return;  /* nothing to do */
459     if (!((wine_get_fs() ^ sel) & ~3))
460     {
461         /* FIXME: if freeing current %fs we cannot acquire locks */
462         wine_set_fs( 0 );
463         internal_set_entry( sel, &null_entry );
464         wine_ldt_copy.flags[sel >> 3] = 0;
465     }
466     else wine_ldt_free_entries( sel, 1 );
467 }
468
469
470 /***********************************************************************
471  *           selector access functions
472  */
473 # ifndef _MSC_VER
474 /* Nothing needs to be done for MS C, it will do with inline versions from the winnt.h */
475 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
476 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
477 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
478 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
479 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
480 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
481 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
482 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
483 # endif /* defined(_MSC_VER) */
484
485 #endif /* __i386__ */