kernel32: Use wide-char string literals.
[wine.git] / libs / wine / ldt.c
blob18b0b9be9bfc9c4786c837dd9e436df9f4b98681
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/asm.h"
36 #ifdef __i386__
38 #ifdef __ASM_OBSOLETE
40 /* the local copy of the LDT */
41 struct __wine_ldt_copy
43 void *base[8192]; /* base address or 0 if entry is free */
44 unsigned long limit[8192]; /* limit in bytes or 0 if entry is free */
45 unsigned char flags[8192]; /* flags (defined below) */
46 } wine_ldt_copy_obsolete = { { 0, 0, 0 } };
48 #define WINE_LDT_FLAGS_32BIT 0x40 /* Segment is 32-bit (code or stack) */
49 #define WINE_LDT_FLAGS_ALLOCATED 0x80 /* Segment is allocated (no longer free) */
51 static inline void *wine_ldt_get_base( const LDT_ENTRY *ent )
53 return (void *)(ent->BaseLow |
54 (ULONG_PTR)ent->HighWord.Bits.BaseMid << 16 |
55 (ULONG_PTR)ent->HighWord.Bits.BaseHi << 24);
57 static inline unsigned int wine_ldt_get_limit( const LDT_ENTRY *ent )
59 unsigned int limit = ent->LimitLow | (ent->HighWord.Bits.LimitHi << 16);
60 if (ent->HighWord.Bits.Granularity) limit = (limit << 12) | 0xfff;
61 return limit;
64 #ifdef __linux__
66 #ifdef HAVE_SYS_SYSCALL_H
67 # include <sys/syscall.h>
68 #endif
70 struct modify_ldt_s
72 unsigned int entry_number;
73 unsigned long base_addr;
74 unsigned int limit;
75 unsigned int seg_32bit : 1;
76 unsigned int contents : 2;
77 unsigned int read_exec_only : 1;
78 unsigned int limit_in_pages : 1;
79 unsigned int seg_not_present : 1;
80 unsigned int usable : 1;
81 unsigned int garbage : 25;
84 static inline void fill_modify_ldt_struct( struct modify_ldt_s *ptr, const LDT_ENTRY *entry )
86 ptr->base_addr = (unsigned long)wine_ldt_get_base(entry);
87 ptr->limit = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
88 ptr->seg_32bit = entry->HighWord.Bits.Default_Big;
89 ptr->contents = (entry->HighWord.Bits.Type >> 2) & 3;
90 ptr->read_exec_only = !(entry->HighWord.Bits.Type & 2);
91 ptr->limit_in_pages = entry->HighWord.Bits.Granularity;
92 ptr->seg_not_present = !entry->HighWord.Bits.Pres;
93 ptr->usable = entry->HighWord.Bits.Sys;
94 ptr->garbage = 0;
97 static inline int modify_ldt( int func, struct modify_ldt_s *ptr, unsigned long count )
99 return syscall( 123 /* SYS_modify_ldt */, func, ptr, count );
102 static inline int set_thread_area( struct modify_ldt_s *ptr )
104 return syscall( 243 /* SYS_set_thread_area */, ptr );
107 #endif /* linux */
109 #if defined(__svr4__) || defined(_SCO_DS)
110 #include <sys/sysi86.h>
111 #ifndef __sun__
112 #include <sys/seg.h>
113 #endif
114 #endif
116 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
117 #include <machine/segments.h>
118 #include <machine/sysarch.h>
119 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
121 #ifdef __GNU__
122 #include <mach/i386/mach_i386.h>
123 #include <mach/mach_traps.h>
124 #endif
126 #ifdef __APPLE__
127 #include <i386/user_ldt.h>
128 #endif
130 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
132 #define LDT_FIRST_ENTRY 512
133 #define LDT_SIZE 8192
135 /* empty function for default locks */
136 static void nop(void) { }
138 static void (*lock_ldt)(void) = nop;
139 static void (*unlock_ldt)(void) = nop;
142 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
144 /***********************************************************************
145 * wine_ldt_init_locking
147 * Set the LDT locking/unlocking functions.
149 void wine_ldt_init_locking_obsolete( void (*lock_func)(void), void (*unlock_func)(void) )
151 lock_ldt = lock_func;
152 unlock_ldt = unlock_func;
156 /***********************************************************************
157 * wine_ldt_get_entry
159 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
161 void wine_ldt_get_entry_obsolete( unsigned short sel, LDT_ENTRY *entry )
163 int index = sel >> 3;
165 if (is_gdt_sel(sel))
167 *entry = null_entry;
168 return;
170 lock_ldt();
171 if (wine_ldt_copy_obsolete.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
173 ULONG_PTR base = (ULONG_PTR)wine_ldt_copy_obsolete.base[index];
174 ULONG limit = wine_ldt_copy_obsolete.limit[index];
176 entry->BaseLow = (WORD)base;
177 entry->HighWord.Bits.BaseMid = (BYTE)(base >> 16);
178 entry->HighWord.Bits.BaseHi = (BYTE)(base >> 24);
179 if ((entry->HighWord.Bits.Granularity = (limit >= 0x100000))) limit >>= 12;
180 entry->LimitLow = (WORD)limit;
181 entry->HighWord.Bits.LimitHi = (limit >> 16);
182 entry->HighWord.Bits.Dpl = 3;
183 entry->HighWord.Bits.Pres = 1;
184 entry->HighWord.Bits.Type = wine_ldt_copy_obsolete.flags[index];
185 entry->HighWord.Bits.Sys = 0;
186 entry->HighWord.Bits.Reserved_0 = 0;
187 entry->HighWord.Bits.Default_Big = !!(wine_ldt_copy_obsolete.flags[index] & WINE_LDT_FLAGS_32BIT);
189 else *entry = null_entry;
190 unlock_ldt();
194 /***********************************************************************
195 * internal_set_entry
197 * Set an LDT entry, without locking. For internal use only.
199 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
201 int ret = 0, index = sel >> 3;
203 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
205 #ifdef linux
207 struct modify_ldt_s ldt_info;
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" );
214 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
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)
224 perror("i386_set_ldt");
225 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
226 exit(1);
229 #elif defined(__svr4__) || defined(_SCO_DS)
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");
239 #elif defined(__APPLE__)
240 if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
241 perror("i386_set_ldt");
242 #elif defined(__GNU__)
243 if ((ret = i386_set_ldt(mach_thread_self(), sel, (descriptor_list_t)entry, 1)) != KERN_SUCCESS)
244 perror("i386_set_ldt");
245 #else
246 fprintf( stderr, "No LDT support on this platform\n" );
247 exit(1);
248 #endif
250 if (ret >= 0)
252 wine_ldt_copy_obsolete.base[index] = wine_ldt_get_base(entry);
253 wine_ldt_copy_obsolete.limit[index] = wine_ldt_get_limit(entry);
254 wine_ldt_copy_obsolete.flags[index] = (entry->HighWord.Bits.Type |
255 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
256 (wine_ldt_copy_obsolete.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
258 return ret;
262 /***********************************************************************
263 * wine_ldt_set_entry
265 * Set an LDT entry.
267 int wine_ldt_set_entry_obsolete( unsigned short sel, const LDT_ENTRY *entry )
269 int ret;
271 lock_ldt();
272 ret = internal_set_entry( sel, entry );
273 unlock_ldt();
274 return ret;
278 /***********************************************************************
279 * wine_ldt_is_system
281 * Check if the selector is a system selector (i.e. not managed by Wine).
283 int wine_ldt_is_system_obsolete( unsigned short sel )
285 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
289 /***********************************************************************
290 * wine_ldt_get_ptr
292 * Convert a segment:offset pair to a linear pointer.
293 * Note: we don't lock the LDT since this has to be fast.
295 void *wine_ldt_get_ptr_obsolete( unsigned short sel, unsigned long offset )
297 int index;
299 if (is_gdt_sel(sel)) /* GDT selector */
300 return (void *)offset;
301 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
302 return (void *)offset;
303 if (!(wine_ldt_copy_obsolete.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
304 return (char *)wine_ldt_copy_obsolete.base[index] + offset;
308 /***********************************************************************
309 * wine_ldt_alloc_entries
311 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
312 * Return a selector for the first entry.
314 unsigned short wine_ldt_alloc_entries_obsolete( int count )
316 int i, index, size = 0;
318 if (count <= 0) return 0;
319 lock_ldt();
320 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
322 if (wine_ldt_copy_obsolete.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
323 else if (++size >= count) /* found a large enough block */
325 index = i - size + 1;
327 /* mark selectors as allocated */
328 for (i = 0; i < count; i++) wine_ldt_copy_obsolete.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
329 unlock_ldt();
330 return (index << 3) | 7;
333 unlock_ldt();
334 return 0;
338 void wine_ldt_free_entries_obsolete( unsigned short sel, int count );
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_obsolete( 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_obsolete.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
361 if (i < newcount) /* they are not free */
363 wine_ldt_free_entries_obsolete( sel, oldcount );
364 sel = wine_ldt_alloc_entries_obsolete( newcount );
366 else /* mark the selectors as allocated */
368 for (i = oldcount; i < newcount; i++)
369 wine_ldt_copy_obsolete.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_obsolete( 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_obsolete( 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_obsolete.flags[index] = 0;
396 unlock_ldt();
400 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
402 /***********************************************************************
403 * wine_ldt_alloc_fs
405 * Allocate an LDT entry for a %fs selector, reusing a global
406 * GDT selector if possible. Return the selector value.
408 unsigned short wine_ldt_alloc_fs_obsolete(void)
410 if (global_fs_sel == -1)
412 #ifdef __linux__
413 struct modify_ldt_s ldt_info;
414 int ret;
416 /* the preloader may have allocated it already */
417 __asm__( "mov %%fs,%0" : "=r" (global_fs_sel) );
418 if (global_fs_sel && is_gdt_sel(global_fs_sel)) return global_fs_sel;
420 memset( &ldt_info, 0, sizeof(ldt_info) );
421 ldt_info.entry_number = -1;
422 ldt_info.seg_32bit = 1;
423 ldt_info.usable = 1;
424 if ((ret = set_thread_area( &ldt_info ) < 0))
426 global_fs_sel = 0; /* don't try it again */
427 if (errno != ENOSYS) perror( "set_thread_area" );
429 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
430 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
431 global_fs_sel = GSEL( GUFS_SEL, SEL_UPL );
432 #endif
434 if (global_fs_sel > 0) return global_fs_sel;
435 return wine_ldt_alloc_entries_obsolete( 1 );
439 /***********************************************************************
440 * wine_ldt_init_fs
442 * Initialize the entry for the %fs selector of the current thread, and
443 * set the thread %fs register.
445 * Note: this runs in the context of the new thread, so cannot acquire locks.
447 void wine_ldt_init_fs_obsolete( unsigned short sel, const LDT_ENTRY *entry )
449 if ((sel & ~3) == (global_fs_sel & ~3))
451 #ifdef __linux__
452 struct modify_ldt_s ldt_info;
453 int ret;
455 ldt_info.entry_number = sel >> 3;
456 fill_modify_ldt_struct( &ldt_info, entry );
457 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
458 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
459 i386_set_fsbase( wine_ldt_get_base( entry ));
460 #endif
462 else /* LDT selector */
464 internal_set_entry( sel, entry );
466 __asm__( "mov %0,%%fs" :: "r" (sel) );
470 /***********************************************************************
471 * wine_ldt_free_fs
473 * Free a %fs selector returned by wine_ldt_alloc_fs.
475 void wine_ldt_free_fs_obsolete( unsigned short sel )
477 WORD fs;
479 if (is_gdt_sel(sel)) return; /* nothing to do */
480 __asm__( "mov %%fs,%0" : "=r" (fs) );
481 if (!((fs ^ sel) & ~3))
483 /* FIXME: if freeing current %fs we cannot acquire locks */
484 __asm__( "mov %0,%%fs" :: "r" (0) );
485 internal_set_entry( sel, &null_entry );
486 wine_ldt_copy_obsolete.flags[sel >> 3] = 0;
488 else wine_ldt_free_entries_obsolete( sel, 1 );
492 /***********************************************************************
493 * selector access functions
495 __ASM_GLOBAL_FUNC( wine_get_cs_obsolete, "movw %cs,%ax\n\tret" )
496 __ASM_GLOBAL_FUNC( wine_get_ds_obsolete, "movw %ds,%ax\n\tret" )
497 __ASM_GLOBAL_FUNC( wine_get_es_obsolete, "movw %es,%ax\n\tret" )
498 __ASM_GLOBAL_FUNC( wine_get_fs_obsolete, "movw %fs,%ax\n\tret" )
499 __ASM_GLOBAL_FUNC( wine_get_gs_obsolete, "movw %gs,%ax\n\tret" )
500 __ASM_GLOBAL_FUNC( wine_get_ss_obsolete, "movw %ss,%ax\n\tret" )
501 __ASM_GLOBAL_FUNC( wine_set_fs_obsolete, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
502 __ASM_GLOBAL_FUNC( wine_set_gs_obsolete, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
505 __ASM_OBSOLETE(wine_ldt_alloc_entries);
506 __ASM_OBSOLETE(wine_ldt_alloc_fs);
507 __ASM_OBSOLETE(wine_ldt_copy);
508 __ASM_OBSOLETE(wine_ldt_free_entries);
509 __ASM_OBSOLETE(wine_ldt_free_fs);
510 __ASM_OBSOLETE(wine_ldt_get_entry);
511 __ASM_OBSOLETE(wine_ldt_get_ptr);
512 __ASM_OBSOLETE(wine_ldt_init_fs);
513 __ASM_OBSOLETE(wine_ldt_init_locking);
514 __ASM_OBSOLETE(wine_ldt_is_system);
515 __ASM_OBSOLETE(wine_ldt_realloc_entries);
516 __ASM_OBSOLETE(wine_ldt_set_entry);
517 __ASM_OBSOLETE(wine_get_cs);
518 __ASM_OBSOLETE(wine_get_ds);
519 __ASM_OBSOLETE(wine_get_es);
520 __ASM_OBSOLETE(wine_get_fs);
521 __ASM_OBSOLETE(wine_get_gs);
522 __ASM_OBSOLETE(wine_get_ss);
523 __ASM_OBSOLETE(wine_set_fs);
524 __ASM_OBSOLETE(wine_set_gs);
526 #endif /* __ASM_OBSOLETE */
528 #endif /* __i386__ */