VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/libWine/ldt.c@ 30720

Last change on this file since 30720 was 25949, checked in by vboxsync, 15 years ago

crOpenGL: update to wine 1.1.36 and disable unnecessary fbo state poll

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
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/*
23 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
24 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
25 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
26 * a choice of LGPL license versions is made available with the language indicating
27 * that LGPLv2 or any later version may be used, or where a choice of which version
28 * of the LGPL is applied is otherwise unspecified.
29 */
30
31#include "config.h"
32#include "wine/port.h"
33
34#include <assert.h>
35#include <stdlib.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39#include <errno.h>
40
41#include "windef.h"
42#include "winbase.h"
43#include "wine/library.h"
44
45#if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
46
47#ifdef __linux__
48
49#ifdef HAVE_SYS_SYSCALL_H
50# include <sys/syscall.h>
51#endif
52
53struct modify_ldt_s
54{
55 unsigned int entry_number;
56 unsigned long base_addr;
57 unsigned int limit;
58 unsigned int seg_32bit : 1;
59 unsigned int contents : 2;
60 unsigned int read_exec_only : 1;
61 unsigned int limit_in_pages : 1;
62 unsigned int seg_not_present : 1;
63 unsigned int usable : 1;
64 unsigned int garbage : 25;
65};
66
67static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
68{
69 ptr->base_addr = (unsigned long)wine_ldt_get_base(entry);
70 ptr->limit = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
71 ptr->seg_32bit = entry->HighWord.Bits.Default_Big;
72 ptr->contents = (entry->HighWord.Bits.Type >> 2) & 3;
73 ptr->read_exec_only = !(entry->HighWord.Bits.Type & 2);
74 ptr->limit_in_pages = entry->HighWord.Bits.Granularity;
75 ptr->seg_not_present = !entry->HighWord.Bits.Pres;
76 ptr->usable = entry->HighWord.Bits.Sys;
77 ptr->garbage = 0;
78}
79
80static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
81{
82 int res;
83 __asm__ __volatile__( "pushl %%ebx\n\t"
84 "movl %2,%%ebx\n\t"
85 "int $0x80\n\t"
86 "popl %%ebx"
87 : "=a" (res)
88 : "0" (SYS_modify_ldt),
89 "r" (func),
90 "c" (ptr),
91 "d" (count),
92 "m" (*ptr) );
93 if (res >= 0) return res;
94 errno = -res;
95 return -1;
96}
97
98static inline int set_thread_area( struct modify_ldt_s *ptr )
99{
100 int res;
101 __asm__ __volatile__( "pushl %%ebx\n\t"
102 "movl %3,%%ebx\n\t"
103 "int $0x80\n\t"
104 "popl %%ebx"
105 : "=a" (res), "=m" (*ptr)
106 : "0" (243) /* SYS_set_thread_area */, "q" (ptr), "m" (*ptr) );
107 if (res >= 0) return res;
108 errno = -res;
109 return -1;
110}
111
112#endif /* linux */
113
114#if defined(__svr4__) || defined(_SCO_DS)
115#include <sys/sysi86.h>
116#ifndef __sun__
117#include <sys/seg.h>
118#endif
119#endif
120
121#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
122#include <machine/segments.h>
123#include <machine/sysarch.h>
124#endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
125
126#ifdef __APPLE__
127#include <i386/user_ldt.h>
128#endif
129
130/* local copy of the LDT */
131#ifdef __APPLE__
132struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
133#else
134struct __wine_ldt_copy wine_ldt_copy;
135#endif
136
137static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
138
139#define LDT_FIRST_ENTRY 512
140#define LDT_SIZE 8192
141
142/* empty function for default locks */
143static void nop(void) { }
144
145static void (*lock_ldt)(void) = nop;
146static void (*unlock_ldt)(void) = nop;
147
148
149static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
150
151/***********************************************************************
152 * wine_ldt_init_locking
153 *
154 * Set the LDT locking/unlocking functions.
155 */
156void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
157{
158 lock_ldt = lock_func;
159 unlock_ldt = unlock_func;
160}
161
162
163/***********************************************************************
164 * wine_ldt_get_entry
165 *
166 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
167 */
168void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
169{
170 int index = sel >> 3;
171
172 if (is_gdt_sel(sel))
173 {
174 *entry = null_entry;
175 return;
176 }
177 lock_ldt();
178 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
179 {
180 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
181 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
182 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
183 }
184 else *entry = null_entry;
185 unlock_ldt();
186}
187
188
189/***********************************************************************
190 * internal_set_entry
191 *
192 * Set an LDT entry, without locking. For internal use only.
193 */
194static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
195{
196 int ret = 0, index = sel >> 3;
197
198 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
199
200#ifdef linux
201 {
202 struct modify_ldt_s ldt_info;
203
204 ldt_info.entry_number = index;
205 fill_modify_ldt_struct( &ldt_info, entry );
206 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
207 perror( "modify_ldt" );
208 }
209#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
210 {
211 LDT_ENTRY entry_copy = *entry;
212 /* The kernel will only let us set LDTs with user priority level */
213 if (entry_copy.HighWord.Bits.Pres
214 && entry_copy.HighWord.Bits.Dpl != 3)
215 entry_copy.HighWord.Bits.Dpl = 3;
216 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
217 if (ret < 0)
218 {
219 perror("i386_set_ldt");
220 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
221 exit(1);
222 }
223 }
224#elif defined(__svr4__) || defined(_SCO_DS)
225 {
226 struct ssd ldt_mod;
227 ldt_mod.sel = sel;
228 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
229 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
230 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
231 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
232 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
233 }
234#elif defined(__APPLE__)
235 if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
236 perror("i386_set_ldt");
237#else
238 fprintf( stderr, "No LDT support on this platform\n" );
239 exit(1);
240#endif
241
242 if (ret >= 0)
243 {
244 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
245 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
246 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
247 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
248 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
249 }
250 return ret;
251}
252
253
254/***********************************************************************
255 * wine_ldt_set_entry
256 *
257 * Set an LDT entry.
258 */
259int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
260{
261 int ret;
262
263 lock_ldt();
264 ret = internal_set_entry( sel, entry );
265 unlock_ldt();
266 return ret;
267}
268
269
270/***********************************************************************
271 * wine_ldt_is_system
272 *
273 * Check if the selector is a system selector (i.e. not managed by Wine).
274 */
275int wine_ldt_is_system( unsigned short sel )
276{
277 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
278}
279
280
281/***********************************************************************
282 * wine_ldt_get_ptr
283 *
284 * Convert a segment:offset pair to a linear pointer.
285 * Note: we don't lock the LDT since this has to be fast.
286 */
287void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
288{
289 int index;
290
291 if (is_gdt_sel(sel)) /* GDT selector */
292 return (void *)offset;
293 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
294 return (void *)offset;
295 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
296 return (char *)wine_ldt_copy.base[index] + offset;
297}
298
299
300/***********************************************************************
301 * wine_ldt_alloc_entries
302 *
303 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
304 * Return a selector for the first entry.
305 */
306unsigned short wine_ldt_alloc_entries( int count )
307{
308 int i, index, size = 0;
309
310 if (count <= 0) return 0;
311 lock_ldt();
312 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
313 {
314 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
315 else if (++size >= count) /* found a large enough block */
316 {
317 index = i - size + 1;
318
319 /* mark selectors as allocated */
320 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
321 unlock_ldt();
322 return (index << 3) | 7;
323 }
324 }
325 unlock_ldt();
326 return 0;
327}
328
329
330/***********************************************************************
331 * wine_ldt_realloc_entries
332 *
333 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
334 * Return a selector for the first entry.
335 */
336unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
337{
338 int i;
339
340 if (oldcount < newcount) /* we need to add selectors */
341 {
342 int index = sel >> 3;
343
344 lock_ldt();
345 /* check if the next selectors are free */
346 if (index + newcount > LDT_SIZE) i = oldcount;
347 else
348 for (i = oldcount; i < newcount; i++)
349 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
350
351 if (i < newcount) /* they are not free */
352 {
353 wine_ldt_free_entries( sel, oldcount );
354 sel = wine_ldt_alloc_entries( newcount );
355 }
356 else /* mark the selectors as allocated */
357 {
358 for (i = oldcount; i < newcount; i++)
359 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
360 }
361 unlock_ldt();
362 }
363 else if (oldcount > newcount) /* we need to remove selectors */
364 {
365 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
366 }
367 return sel;
368}
369
370
371/***********************************************************************
372 * wine_ldt_free_entries
373 *
374 * Free a number of consecutive ldt entries and clear their contents.
375 */
376void wine_ldt_free_entries( unsigned short sel, int count )
377{
378 int index;
379
380 lock_ldt();
381 for (index = sel >> 3; count > 0; count--, index++)
382 {
383 internal_set_entry( sel, &null_entry );
384 wine_ldt_copy.flags[index] = 0;
385 }
386 unlock_ldt();
387}
388
389
390static 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 */
398unsigned 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 */
435void 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 */
463void 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__ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
481__ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
482__ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
483__ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
484__ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
485__ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
486__ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
487__ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
488
489#endif /* __i386__ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette