kernel: Make sure the file contains data for the ReadFile test.
[wine/multimedia.git] / libs / wine / ldt.c
blob9f70f111139b3bfb7066330cf752fcfa8dc1cb20
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #define WINE_EXPORT_LDT_COPY
35 #include "wine/library.h"
37 #ifdef __i386__
39 #ifdef linux
41 #ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
43 #endif
45 struct modify_ldt_s
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;
59 static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
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;
72 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
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;
89 static inline int set_thread_area( struct modify_ldt_s *ptr )
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;
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(__OpenBSD__)
113 #include <machine/segments.h>
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__ */
119 #ifdef __APPLE__
121 static inline int thread_set_user_ldt( const void *addr, unsigned int size, unsigned int flags )
123 int ret;
124 __asm__ __volatile__ ("pushl %4\n\t"
125 "pushl %3\n\t"
126 "pushl %2\n\t"
127 "pushl $0\n\t"
128 "lcall $0x3b,$0\n\t"
129 "leal 16(%%esp),%%esp"
130 : "=a" (ret)
131 : "0" (4 /*thread_set_user_ldt*/), "r" (addr), "r" (size), "r" (flags) );
132 return ret;
135 #endif /* __APPLE__ */
137 #endif /* __i386__ */
139 /* local copy of the LDT */
140 #ifdef __APPLE__
141 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
142 #else
143 struct __wine_ldt_copy wine_ldt_copy;
144 #endif
146 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
148 #define LDT_FIRST_ENTRY 512
149 #define LDT_SIZE 8192
151 /* empty function for default locks */
152 static void nop(void) { }
154 static void (*lock_ldt)(void) = nop;
155 static void (*unlock_ldt)(void) = nop;
158 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
160 /***********************************************************************
161 * wine_ldt_init_locking
163 * Set the LDT locking/unlocking functions.
165 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
167 lock_ldt = lock_func;
168 unlock_ldt = unlock_func;
172 /***********************************************************************
173 * wine_ldt_get_entry
175 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
177 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
179 int index = sel >> 3;
181 if (is_gdt_sel(sel))
183 *entry = null_entry;
184 return;
186 lock_ldt();
187 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
189 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
190 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
191 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
193 else *entry = null_entry;
194 unlock_ldt();
198 /***********************************************************************
199 * internal_set_entry
201 * Set an LDT entry, without locking. For internal use only.
203 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
205 int ret = 0, index = sel >> 3;
207 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
209 #ifdef __i386__
211 #ifdef linux
213 struct modify_ldt_s ldt_info;
215 ldt_info.entry_number = index;
216 fill_modify_ldt_struct( &ldt_info, entry );
217 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
218 perror( "modify_ldt" );
220 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
222 LDT_ENTRY entry_copy = *entry;
223 /* The kernel will only let us set LDTs with user priority level */
224 if (entry_copy.HighWord.Bits.Pres
225 && entry_copy.HighWord.Bits.Dpl != 3)
226 entry_copy.HighWord.Bits.Dpl = 3;
227 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
228 if (ret < 0)
230 perror("i386_set_ldt");
231 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
232 exit(1);
235 #elif defined(__svr4__) || defined(_SCO_DS)
237 struct ssd ldt_mod;
238 ldt_mod.sel = sel;
239 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
240 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
241 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
242 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
243 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
245 #else
246 fprintf( stderr, "No LDT support on this platform\n" );
247 exit(1);
248 #endif
250 #endif /* __i386__ */
252 if (ret >= 0)
254 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
255 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
256 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
257 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
258 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
260 return ret;
264 /***********************************************************************
265 * wine_ldt_set_entry
267 * Set an LDT entry.
269 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
271 int ret;
273 lock_ldt();
274 ret = internal_set_entry( sel, entry );
275 unlock_ldt();
276 return ret;
280 /***********************************************************************
281 * wine_ldt_is_system
283 * Check if the selector is a system selector (i.e. not managed by Wine).
285 int wine_ldt_is_system( unsigned short sel )
287 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
291 /***********************************************************************
292 * wine_ldt_get_ptr
294 * Convert a segment:offset pair to a linear pointer.
295 * Note: we don't lock the LDT since this has to be fast.
297 void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
299 int index;
301 if (is_gdt_sel(sel)) /* GDT selector */
302 return (void *)offset;
303 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
304 return (void *)offset;
305 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
306 return (char *)wine_ldt_copy.base[index] + offset;
310 /***********************************************************************
311 * wine_ldt_alloc_entries
313 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
314 * Return a selector for the first entry.
316 unsigned short wine_ldt_alloc_entries( int count )
318 int i, index, size = 0;
320 if (count <= 0) return 0;
321 lock_ldt();
322 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
324 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
325 else if (++size >= count) /* found a large enough block */
327 index = i - size + 1;
329 /* mark selectors as allocated */
330 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
331 unlock_ldt();
332 return (index << 3) | 7;
335 unlock_ldt();
336 return 0;
340 /***********************************************************************
341 * wine_ldt_realloc_entries
343 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
344 * Return a selector for the first entry.
346 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
348 int i;
350 if (oldcount < newcount) /* we need to add selectors */
352 int index = sel >> 3;
354 lock_ldt();
355 /* check if the next selectors are free */
356 if (index + newcount > LDT_SIZE) i = oldcount;
357 else
358 for (i = oldcount; i < newcount; i++)
359 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
361 if (i < newcount) /* they are not free */
363 wine_ldt_free_entries( sel, oldcount );
364 sel = wine_ldt_alloc_entries( newcount );
366 else /* mark the selectors as allocated */
368 for (i = oldcount; i < newcount; i++)
369 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
371 unlock_ldt();
373 else if (oldcount > newcount) /* we need to remove selectors */
375 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
377 return sel;
381 /***********************************************************************
382 * wine_ldt_free_entries
384 * Free a number of consecutive ldt entries and clear their contents.
386 void wine_ldt_free_entries( unsigned short sel, int count )
388 int index;
390 lock_ldt();
391 for (index = sel >> 3; count > 0; count--, index++)
393 internal_set_entry( sel, &null_entry );
394 wine_ldt_copy.flags[index] = 0;
396 unlock_ldt();
400 #ifdef __i386__
402 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
404 /***********************************************************************
405 * wine_ldt_alloc_fs
407 * Allocate an LDT entry for a %fs selector, reusing a global
408 * GDT selector if possible. Return the selector value.
410 unsigned short wine_ldt_alloc_fs(void)
412 if (global_fs_sel == -1)
414 #ifdef __linux__
415 struct modify_ldt_s ldt_info;
416 int ret;
418 ldt_info.entry_number = -1;
419 fill_modify_ldt_struct( &ldt_info, &null_entry );
420 if ((ret = set_thread_area( &ldt_info ) < 0))
422 global_fs_sel = 0; /* don't try it again */
423 if (errno != ENOSYS) perror( "set_thread_area" );
425 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
426 #elif defined(__APPLE__)
427 int ret = thread_set_user_ldt( NULL, 0, 0 );
428 if (ret != -1) global_fs_sel = ret;
429 else global_fs_sel = 0;
430 #endif /* __APPLE__ */
432 if (global_fs_sel > 0) return global_fs_sel;
433 return wine_ldt_alloc_entries( 1 );
437 /***********************************************************************
438 * wine_ldt_init_fs
440 * Initialize the entry for the %fs selector of the current thread, and
441 * set the thread %fs register.
443 * Note: this runs in the context of the new thread, so cannot acquire locks.
445 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
447 if ((sel & ~3) == (global_fs_sel & ~3))
449 #ifdef __linux__
450 struct modify_ldt_s ldt_info;
451 int ret;
453 ldt_info.entry_number = sel >> 3;
454 fill_modify_ldt_struct( &ldt_info, entry );
455 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
456 #elif defined(__APPLE__)
457 int ret = thread_set_user_ldt( wine_ldt_get_base(entry), wine_ldt_get_limit(entry), 0 );
458 if (ret == -1) perror( "thread_set_user_ldt" );
459 else assert( ret == global_fs_sel );
460 #endif /* __APPLE__ */
462 else /* LDT selector */
464 internal_set_entry( sel, entry );
466 wine_set_fs( sel );
470 /***********************************************************************
471 * wine_ldt_free_fs
473 * Free a %fs selector returned by wine_ldt_alloc_fs.
475 void wine_ldt_free_fs( unsigned short sel )
477 if (is_gdt_sel(sel)) return; /* nothing to do */
478 if (!((wine_get_fs() ^ sel) & ~3))
480 /* FIXME: if freeing current %fs we cannot acquire locks */
481 wine_set_fs( 0 );
482 internal_set_entry( sel, &null_entry );
483 wine_ldt_copy.flags[sel >> 3] = 0;
485 else wine_ldt_free_entries( sel, 1 );
489 /***********************************************************************
490 * selector access functions
492 # ifndef _MSC_VER
493 /* Nothing needs to be done for MS C, it will do with inline versions from the winnt.h */
494 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
495 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
496 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
497 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
498 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
499 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
500 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
501 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
502 # endif /* defined(_MSC_VER) */
504 #endif /* __i386__ */