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