VirtualBox

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

Last change on this file since 16477 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

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