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