ddraw: Disable lighting when a 0 material handle is set.
[wine.git] / libs / wine / ldt.c
blob0946407625e90bad85d3a0ceaaa027f922f4838a
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include "wine/library.h"
36 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
38 #ifdef __linux__
40 #ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
42 #endif
44 struct modify_ldt_s
46 unsigned int entry_number;
47 unsigned long base_addr;
48 unsigned int limit;
49 unsigned int seg_32bit : 1;
50 unsigned int contents : 2;
51 unsigned int read_exec_only : 1;
52 unsigned int limit_in_pages : 1;
53 unsigned int seg_not_present : 1;
54 unsigned int usable : 1;
55 unsigned int garbage : 25;
58 static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
60 ptr->base_addr = (unsigned long)wine_ldt_get_base(entry);
61 ptr->limit = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
62 ptr->seg_32bit = entry->HighWord.Bits.Default_Big;
63 ptr->contents = (entry->HighWord.Bits.Type >> 2) & 3;
64 ptr->read_exec_only = !(entry->HighWord.Bits.Type & 2);
65 ptr->limit_in_pages = entry->HighWord.Bits.Granularity;
66 ptr->seg_not_present = !entry->HighWord.Bits.Pres;
67 ptr->usable = entry->HighWord.Bits.Sys;
68 ptr->garbage = 0;
71 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
73 return syscall( 123 /* SYS_modify_ldt */, func, ptr, count );
76 static inline int set_thread_area( struct modify_ldt_s *ptr )
78 return syscall( 243 /* SYS_set_thread_area */, ptr );
81 #endif /* linux */
83 #if defined(__svr4__) || defined(_SCO_DS)
84 #include <sys/sysi86.h>
85 #ifndef __sun__
86 #include <sys/seg.h>
87 #endif
88 #endif
90 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
91 #include <machine/segments.h>
92 #include <machine/sysarch.h>
93 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
95 #ifdef __GNU__
96 #include <mach/i386/mach_i386.h>
97 #include <mach/mach_traps.h>
98 #endif
100 #ifdef __APPLE__
101 #include <i386/user_ldt.h>
102 #endif
104 /* local copy of the LDT */
105 #ifdef __APPLE__
106 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
107 #else
108 struct __wine_ldt_copy wine_ldt_copy;
109 #endif
111 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
113 #define LDT_FIRST_ENTRY 512
114 #define LDT_SIZE 8192
116 /* empty function for default locks */
117 static void nop(void) { }
119 static void (*lock_ldt)(void) = nop;
120 static void (*unlock_ldt)(void) = nop;
123 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
125 /***********************************************************************
126 * wine_ldt_init_locking
128 * Set the LDT locking/unlocking functions.
130 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
132 lock_ldt = lock_func;
133 unlock_ldt = unlock_func;
137 /***********************************************************************
138 * wine_ldt_get_entry
140 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
142 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
144 int index = sel >> 3;
146 if (is_gdt_sel(sel))
148 *entry = null_entry;
149 return;
151 lock_ldt();
152 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
154 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
155 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
156 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
158 else *entry = null_entry;
159 unlock_ldt();
163 /***********************************************************************
164 * internal_set_entry
166 * Set an LDT entry, without locking. For internal use only.
168 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
170 int ret = 0, index = sel >> 3;
172 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
174 #ifdef linux
176 struct modify_ldt_s ldt_info;
178 ldt_info.entry_number = index;
179 fill_modify_ldt_struct( &ldt_info, entry );
180 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
181 perror( "modify_ldt" );
183 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
185 LDT_ENTRY entry_copy = *entry;
186 /* The kernel will only let us set LDTs with user priority level */
187 if (entry_copy.HighWord.Bits.Pres
188 && entry_copy.HighWord.Bits.Dpl != 3)
189 entry_copy.HighWord.Bits.Dpl = 3;
190 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
191 if (ret < 0)
193 perror("i386_set_ldt");
194 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
195 exit(1);
198 #elif defined(__svr4__) || defined(_SCO_DS)
200 struct ssd ldt_mod;
201 ldt_mod.sel = sel;
202 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
203 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
204 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
205 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
206 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
208 #elif defined(__APPLE__)
209 if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
210 perror("i386_set_ldt");
211 #elif defined(__GNU__)
212 if ((ret = i386_set_ldt(mach_thread_self(), sel, (descriptor_list_t)entry, 1)) != KERN_SUCCESS)
213 perror("i386_set_ldt");
214 #else
215 fprintf( stderr, "No LDT support on this platform\n" );
216 exit(1);
217 #endif
219 if (ret >= 0)
221 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
222 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
223 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
224 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
225 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
227 return ret;
231 /***********************************************************************
232 * wine_ldt_set_entry
234 * Set an LDT entry.
236 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
238 int ret;
240 lock_ldt();
241 ret = internal_set_entry( sel, entry );
242 unlock_ldt();
243 return ret;
247 /***********************************************************************
248 * wine_ldt_is_system
250 * Check if the selector is a system selector (i.e. not managed by Wine).
252 int wine_ldt_is_system( unsigned short sel )
254 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
258 /***********************************************************************
259 * wine_ldt_get_ptr
261 * Convert a segment:offset pair to a linear pointer.
262 * Note: we don't lock the LDT since this has to be fast.
264 void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
266 int index;
268 if (is_gdt_sel(sel)) /* GDT selector */
269 return (void *)offset;
270 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
271 return (void *)offset;
272 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
273 return (char *)wine_ldt_copy.base[index] + offset;
277 /***********************************************************************
278 * wine_ldt_alloc_entries
280 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
281 * Return a selector for the first entry.
283 unsigned short wine_ldt_alloc_entries( int count )
285 int i, index, size = 0;
287 if (count <= 0) return 0;
288 lock_ldt();
289 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
291 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
292 else if (++size >= count) /* found a large enough block */
294 index = i - size + 1;
296 /* mark selectors as allocated */
297 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
298 unlock_ldt();
299 return (index << 3) | 7;
302 unlock_ldt();
303 return 0;
307 /***********************************************************************
308 * wine_ldt_realloc_entries
310 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
311 * Return a selector for the first entry.
313 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
315 int i;
317 if (oldcount < newcount) /* we need to add selectors */
319 int index = sel >> 3;
321 lock_ldt();
322 /* check if the next selectors are free */
323 if (index + newcount > LDT_SIZE) i = oldcount;
324 else
325 for (i = oldcount; i < newcount; i++)
326 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
328 if (i < newcount) /* they are not free */
330 wine_ldt_free_entries( sel, oldcount );
331 sel = wine_ldt_alloc_entries( newcount );
333 else /* mark the selectors as allocated */
335 for (i = oldcount; i < newcount; i++)
336 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
338 unlock_ldt();
340 else if (oldcount > newcount) /* we need to remove selectors */
342 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
344 return sel;
348 /***********************************************************************
349 * wine_ldt_free_entries
351 * Free a number of consecutive ldt entries and clear their contents.
353 void wine_ldt_free_entries( unsigned short sel, int count )
355 int index;
357 lock_ldt();
358 for (index = sel >> 3; count > 0; count--, index++)
360 internal_set_entry( sel, &null_entry );
361 wine_ldt_copy.flags[index] = 0;
363 unlock_ldt();
367 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
369 /***********************************************************************
370 * wine_ldt_alloc_fs
372 * Allocate an LDT entry for a %fs selector, reusing a global
373 * GDT selector if possible. Return the selector value.
375 unsigned short wine_ldt_alloc_fs(void)
377 if (global_fs_sel == -1)
379 #ifdef __linux__
380 struct modify_ldt_s ldt_info;
381 int ret;
383 /* the preloader may have allocated it already */
384 global_fs_sel = wine_get_fs();
385 if (global_fs_sel && is_gdt_sel(global_fs_sel)) return global_fs_sel;
387 ldt_info.entry_number = -1;
388 fill_modify_ldt_struct( &ldt_info, &null_entry );
389 if ((ret = set_thread_area( &ldt_info ) < 0))
391 global_fs_sel = 0; /* don't try it again */
392 if (errno != ENOSYS) perror( "set_thread_area" );
394 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
395 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
396 global_fs_sel = GSEL( GUFS_SEL, SEL_UPL );
397 #endif
399 if (global_fs_sel > 0) return global_fs_sel;
400 return wine_ldt_alloc_entries( 1 );
404 /***********************************************************************
405 * wine_ldt_init_fs
407 * Initialize the entry for the %fs selector of the current thread, and
408 * set the thread %fs register.
410 * Note: this runs in the context of the new thread, so cannot acquire locks.
412 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
414 if ((sel & ~3) == (global_fs_sel & ~3))
416 #ifdef __linux__
417 struct modify_ldt_s ldt_info;
418 int ret;
420 ldt_info.entry_number = sel >> 3;
421 fill_modify_ldt_struct( &ldt_info, entry );
422 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
423 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
424 i386_set_fsbase( wine_ldt_get_base( entry ));
425 #endif
427 else /* LDT selector */
429 internal_set_entry( sel, entry );
431 wine_set_fs( sel );
435 /***********************************************************************
436 * wine_ldt_free_fs
438 * Free a %fs selector returned by wine_ldt_alloc_fs.
440 void wine_ldt_free_fs( unsigned short sel )
442 if (is_gdt_sel(sel)) return; /* nothing to do */
443 if (!((wine_get_fs() ^ sel) & ~3))
445 /* FIXME: if freeing current %fs we cannot acquire locks */
446 wine_set_fs( 0 );
447 internal_set_entry( sel, &null_entry );
448 wine_ldt_copy.flags[sel >> 3] = 0;
450 else wine_ldt_free_entries( sel, 1 );
454 /***********************************************************************
455 * selector access functions
457 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
458 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
459 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
460 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
461 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
462 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
463 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
464 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
466 #endif /* __i386__ */