Point size in DLG_TEMPLATE may be negative.
[wine.git] / libs / wine / ldt.c
blobf505489fb44077b2cd41177c969f40f950b23ba8
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 */, "r" (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 extern int sysi86(int,void*);
108 #ifndef __sun__
109 #include <sys/seg.h>
110 #endif
111 #endif
113 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
114 #include <machine/segments.h>
116 extern int i386_get_ldt(int, union descriptor *, int);
117 extern int i386_set_ldt(int, union descriptor *, int);
118 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
120 #endif /* __i386__ */
122 /* local copy of the LDT */
123 struct __wine_ldt_copy wine_ldt_copy;
125 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
127 #define LDT_FIRST_ENTRY 512
128 #define LDT_SIZE 8192
130 /* empty function for default locks */
131 static void nop(void) { }
133 static void (*lock_ldt)(void) = nop;
134 static void (*unlock_ldt)(void) = nop;
137 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
139 /***********************************************************************
140 * wine_ldt_init_locking
142 * Set the LDT locking/unlocking functions.
144 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
146 lock_ldt = lock_func;
147 unlock_ldt = unlock_func;
151 /***********************************************************************
152 * wine_ldt_get_entry
154 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
156 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
158 int index = sel >> 3;
160 if (is_gdt_sel(sel))
162 *entry = null_entry;
163 return;
165 lock_ldt();
166 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
168 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
169 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
170 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
172 else *entry = null_entry;
173 unlock_ldt();
177 /***********************************************************************
178 * internal_set_entry
180 * Set an LDT entry, without locking. For internal use only.
182 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
184 int ret = 0, index = sel >> 3;
186 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
188 #ifdef __i386__
190 #ifdef linux
192 struct modify_ldt_s ldt_info;
194 ldt_info.entry_number = index;
195 fill_modify_ldt_struct( &ldt_info, entry );
196 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
197 perror( "modify_ldt" );
199 #endif /* linux */
201 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
203 LDT_ENTRY entry_copy = *entry;
204 /* The kernel will only let us set LDTs with user priority level */
205 if (entry_copy.HighWord.Bits.Pres
206 && entry_copy.HighWord.Bits.Dpl != 3)
207 entry_copy.HighWord.Bits.Dpl = 3;
208 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
209 if (ret < 0)
211 perror("i386_set_ldt");
212 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
213 exit(1);
216 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
218 #if defined(__svr4__) || defined(_SCO_DS)
220 struct ssd ldt_mod;
221 ldt_mod.sel = sel;
222 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
223 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
224 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
225 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
226 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
228 #endif
230 #endif /* __i386__ */
232 if (ret >= 0)
234 wine_ldt_copy.base[index] = wine_ldt_get_base(entry);
235 wine_ldt_copy.limit[index] = wine_ldt_get_limit(entry);
236 wine_ldt_copy.flags[index] = (entry->HighWord.Bits.Type |
237 (entry->HighWord.Bits.Default_Big ? WINE_LDT_FLAGS_32BIT : 0) |
238 (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED));
240 return ret;
244 /***********************************************************************
245 * wine_ldt_set_entry
247 * Set an LDT entry.
249 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
251 int ret;
253 lock_ldt();
254 ret = internal_set_entry( sel, entry );
255 unlock_ldt();
256 return ret;
260 /***********************************************************************
261 * wine_ldt_is_system
263 * Check if the selector is a system selector (i.e. not managed by Wine).
265 int wine_ldt_is_system( unsigned short sel )
267 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
271 /***********************************************************************
272 * wine_ldt_get_ptr
274 * Convert a segment:offset pair to a linear pointer.
275 * Note: we don't lock the LDT since this has to be fast.
277 void *wine_ldt_get_ptr( unsigned short sel, unsigned int offset )
279 int index;
281 if (is_gdt_sel(sel)) /* GDT selector */
282 return (void *)offset;
283 if ((index = (sel >> 3)) < LDT_FIRST_ENTRY) /* system selector */
284 return (void *)offset;
285 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_32BIT)) offset &= 0xffff;
286 return (char *)wine_ldt_copy.base[index] + offset;
290 /***********************************************************************
291 * wine_ldt_alloc_entries
293 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
294 * Return a selector for the first entry.
296 unsigned short wine_ldt_alloc_entries( int count )
298 int i, index, size = 0;
300 if (count <= 0) return 0;
301 lock_ldt();
302 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
304 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
305 else if (++size >= count) /* found a large enough block */
307 index = i - size + 1;
309 /* mark selectors as allocated */
310 for (i = 0; i < count; i++) wine_ldt_copy.flags[index + i] |= WINE_LDT_FLAGS_ALLOCATED;
311 unlock_ldt();
312 return (index << 3) | 7;
315 unlock_ldt();
316 return 0;
320 /***********************************************************************
321 * wine_ldt_realloc_entries
323 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
324 * Return a selector for the first entry.
326 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
328 int i;
330 if (oldcount < newcount) /* we need to add selectors */
332 int index = sel >> 3;
334 lock_ldt();
335 /* check if the next selectors are free */
336 if (index + newcount > LDT_SIZE) i = oldcount;
337 else
338 for (i = oldcount; i < newcount; i++)
339 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
341 if (i < newcount) /* they are not free */
343 wine_ldt_free_entries( sel, oldcount );
344 sel = wine_ldt_alloc_entries( newcount );
346 else /* mark the selectors as allocated */
348 for (i = oldcount; i < newcount; i++)
349 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
351 unlock_ldt();
353 else if (oldcount > newcount) /* we need to remove selectors */
355 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
357 return sel;
361 /***********************************************************************
362 * wine_ldt_free_entries
364 * Free a number of consecutive ldt entries and clear their contents.
366 void wine_ldt_free_entries( unsigned short sel, int count )
368 int index;
370 lock_ldt();
371 for (index = sel >> 3; count > 0; count--, index++)
373 internal_set_entry( sel, &null_entry );
374 wine_ldt_copy.flags[index] = 0;
376 unlock_ldt();
380 #ifdef __i386__
382 static int fs_gdt_index = -1; /* GDT index for %fs, or 0 if GDT not supported on this kernel */
384 /***********************************************************************
385 * wine_ldt_alloc_fs
387 * Allocate an LDT entry for a %fs selector, reusing a global
388 * GDT selector if possible. Return the selector value.
390 unsigned short wine_ldt_alloc_fs(void)
392 if (fs_gdt_index == -1)
394 #ifdef __linux__
395 struct modify_ldt_s ldt_info;
396 int ret;
398 ldt_info.entry_number = -1;
399 fill_modify_ldt_struct( &ldt_info, &null_entry );
400 if ((ret = set_thread_area( &ldt_info ) < 0))
402 fs_gdt_index = 0; /* don't try it again */
403 if (errno != ENOSYS) perror( "set_thread_area" );
405 else fs_gdt_index = ldt_info.entry_number;
406 #endif /* __linux__ */
408 if (fs_gdt_index > 0) return (fs_gdt_index << 3) | 3;
409 return wine_ldt_alloc_entries( 1 );
413 /***********************************************************************
414 * wine_ldt_init_fs
416 * Initialize the entry for the %fs selector of the current thread, and
417 * set the thread %fs register.
419 * Note: this runs in the context of the new thread, so cannot acquire locks.
421 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
423 if (is_gdt_sel(sel))
425 #ifdef __linux__
426 struct modify_ldt_s ldt_info;
427 int ret;
429 ldt_info.entry_number = sel >> 3;
430 assert( ldt_info.entry_number == fs_gdt_index );
431 fill_modify_ldt_struct( &ldt_info, entry );
432 if ((ret = set_thread_area( &ldt_info ) < 0)) perror( "set_thread_area" );
433 #endif /* __linux__ */
435 else /* LDT selector */
437 internal_set_entry( sel, entry );
439 wine_set_fs( sel );
443 /***********************************************************************
444 * wine_ldt_free_fs
446 * Free a %fs selector returned by wine_ldt_alloc_fs.
448 void wine_ldt_free_fs( unsigned short sel )
450 if (is_gdt_sel(sel)) return; /* nothing to do */
451 if (!((wine_get_fs() ^ sel) & ~3))
453 /* FIXME: if freeing current %fs we cannot acquire locks */
454 wine_set_fs( 0 );
455 internal_set_entry( sel, &null_entry );
456 wine_ldt_copy.flags[sel >> 3] = 0;
458 else wine_ldt_free_entries( sel, 1 );
462 /***********************************************************************
463 * selector access functions
465 # ifndef _MSC_VER
466 /* Nothing needs to be done for MS C, it will do with inline versions from the winnt.h */
467 __ASM_GLOBAL_FUNC( wine_get_cs, "movw %cs,%ax\n\tret" )
468 __ASM_GLOBAL_FUNC( wine_get_ds, "movw %ds,%ax\n\tret" )
469 __ASM_GLOBAL_FUNC( wine_get_es, "movw %es,%ax\n\tret" )
470 __ASM_GLOBAL_FUNC( wine_get_fs, "movw %fs,%ax\n\tret" )
471 __ASM_GLOBAL_FUNC( wine_get_gs, "movw %gs,%ax\n\tret" )
472 __ASM_GLOBAL_FUNC( wine_get_ss, "movw %ss,%ax\n\tret" )
473 __ASM_GLOBAL_FUNC( wine_set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
474 __ASM_GLOBAL_FUNC( wine_set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
475 # endif /* defined(_MSC_VER) */
477 #endif /* __i386__ */