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