server: Don't crash when trying to set a file lock on a device.
[wine.git] / libs / wine / ldt.c
blobceb8db423f846e35164f1e3cca1eb45b7c29cce9
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 #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 "m" (*ptr) );
85 if (res >= 0) return res;
86 errno = -res;
87 return -1;
90 static inline int set_thread_area( struct modify_ldt_s *ptr )
92 int res;
93 __asm__ __volatile__( "pushl %%ebx\n\t"
94 "movl %3,%%ebx\n\t"
95 "int $0x80\n\t"
96 "popl %%ebx"
97 : "=a" (res), "=m" (*ptr)
98 : "0" (243) /* SYS_set_thread_area */, "q" (ptr), "m" (*ptr) );
99 if (res >= 0) return res;
100 errno = -res;
101 return -1;
104 #endif /* linux */
106 #if defined(__svr4__) || defined(_SCO_DS)
107 #include <sys/sysi86.h>
108 #ifndef __sun__
109 #include <sys/seg.h>
110 #endif
111 #endif
113 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || 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 #ifdef __APPLE__
121 #include <i386/user_ldt.h>
122 #endif
124 #endif /* __i386__ */
126 /* local copy of the LDT */
127 #ifdef __APPLE__
128 struct __wine_ldt_copy wine_ldt_copy = { { 0, 0, 0 } };
129 #else
130 struct __wine_ldt_copy wine_ldt_copy;
131 #endif
133 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
135 #define LDT_FIRST_ENTRY 512
136 #define LDT_SIZE 8192
138 /* empty function for default locks */
139 static void nop(void) { }
141 static void (*lock_ldt)(void) = nop;
142 static void (*unlock_ldt)(void) = nop;
145 static inline int is_gdt_sel( unsigned short sel ) { return !(sel & 4); }
147 /***********************************************************************
148 * wine_ldt_init_locking
150 * Set the LDT locking/unlocking functions.
152 void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) )
154 lock_ldt = lock_func;
155 unlock_ldt = unlock_func;
159 /***********************************************************************
160 * wine_ldt_get_entry
162 * Retrieve an LDT entry. Return a null entry if selector is not allocated.
164 void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry )
166 int index = sel >> 3;
168 if (is_gdt_sel(sel))
170 *entry = null_entry;
171 return;
173 lock_ldt();
174 if (wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)
176 wine_ldt_set_base( entry, wine_ldt_copy.base[index] );
177 wine_ldt_set_limit( entry, wine_ldt_copy.limit[index] );
178 wine_ldt_set_flags( entry, wine_ldt_copy.flags[index] );
180 else *entry = null_entry;
181 unlock_ldt();
185 /***********************************************************************
186 * internal_set_entry
188 * Set an LDT entry, without locking. For internal use only.
190 static int internal_set_entry( unsigned short sel, const LDT_ENTRY *entry )
192 int ret = 0, index = sel >> 3;
194 if (index < LDT_FIRST_ENTRY) return 0; /* cannot modify reserved entries */
196 #ifdef __i386__
198 #ifdef linux
200 struct modify_ldt_s ldt_info;
202 ldt_info.entry_number = index;
203 fill_modify_ldt_struct( &ldt_info, entry );
204 if ((ret = modify_ldt(0x11, &ldt_info, sizeof(ldt_info))) < 0)
205 perror( "modify_ldt" );
207 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
209 LDT_ENTRY entry_copy = *entry;
210 /* The kernel will only let us set LDTs with user priority level */
211 if (entry_copy.HighWord.Bits.Pres
212 && entry_copy.HighWord.Bits.Dpl != 3)
213 entry_copy.HighWord.Bits.Dpl = 3;
214 ret = i386_set_ldt(index, (union descriptor *)&entry_copy, 1);
215 if (ret < 0)
217 perror("i386_set_ldt");
218 fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
219 exit(1);
222 #elif defined(__svr4__) || defined(_SCO_DS)
224 struct ssd ldt_mod;
225 ldt_mod.sel = sel;
226 ldt_mod.bo = (unsigned long)wine_ldt_get_base(entry);
227 ldt_mod.ls = entry->LimitLow | (entry->HighWord.Bits.LimitHi << 16);
228 ldt_mod.acc1 = entry->HighWord.Bytes.Flags1;
229 ldt_mod.acc2 = entry->HighWord.Bytes.Flags2 >> 4;
230 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
232 #elif defined(__APPLE__)
233 if ((ret = i386_set_ldt(index, (union ldt_entry *)entry, 1)) < 0)
234 perror("i386_set_ldt");
235 #else
236 fprintf( stderr, "No LDT support on this platform\n" );
237 exit(1);
238 #endif
240 #endif /* __i386__ */
242 if (ret >= 0)
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));
250 return ret;
254 /***********************************************************************
255 * wine_ldt_set_entry
257 * Set an LDT entry.
259 int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry )
261 int ret;
263 lock_ldt();
264 ret = internal_set_entry( sel, entry );
265 unlock_ldt();
266 return ret;
270 /***********************************************************************
271 * wine_ldt_is_system
273 * Check if the selector is a system selector (i.e. not managed by Wine).
275 int wine_ldt_is_system( unsigned short sel )
277 return is_gdt_sel(sel) || ((sel >> 3) < LDT_FIRST_ENTRY);
281 /***********************************************************************
282 * wine_ldt_get_ptr
284 * Convert a segment:offset pair to a linear pointer.
285 * Note: we don't lock the LDT since this has to be fast.
287 void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset )
289 int index;
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;
300 /***********************************************************************
301 * wine_ldt_alloc_entries
303 * Allocate a number of consecutive ldt entries, without setting the LDT contents.
304 * Return a selector for the first entry.
306 unsigned short wine_ldt_alloc_entries( int count )
308 int i, index, size = 0;
310 if (count <= 0) return 0;
311 lock_ldt();
312 for (i = LDT_FIRST_ENTRY; i < LDT_SIZE; i++)
314 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
315 else if (++size >= count) /* found a large enough block */
317 index = i - size + 1;
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;
325 unlock_ldt();
326 return 0;
330 /***********************************************************************
331 * wine_ldt_realloc_entries
333 * Reallocate a number of consecutive ldt entries, without changing the LDT contents.
334 * Return a selector for the first entry.
336 unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount )
338 int i;
340 if (oldcount < newcount) /* we need to add selectors */
342 int index = sel >> 3;
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;
351 if (i < newcount) /* they are not free */
353 wine_ldt_free_entries( sel, oldcount );
354 sel = wine_ldt_alloc_entries( newcount );
356 else /* mark the selectors as allocated */
358 for (i = oldcount; i < newcount; i++)
359 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
361 unlock_ldt();
363 else if (oldcount > newcount) /* we need to remove selectors */
365 wine_ldt_free_entries( sel + (newcount << 3), newcount - oldcount );
367 return sel;
371 /***********************************************************************
372 * wine_ldt_free_entries
374 * Free a number of consecutive ldt entries and clear their contents.
376 void wine_ldt_free_entries( unsigned short sel, int count )
378 int index;
380 lock_ldt();
381 for (index = sel >> 3; count > 0; count--, index++)
383 internal_set_entry( sel, &null_entry );
384 wine_ldt_copy.flags[index] = 0;
386 unlock_ldt();
390 #ifdef __i386__
392 static int global_fs_sel = -1; /* global selector for %fs shared among all threads */
394 /***********************************************************************
395 * wine_ldt_alloc_fs
397 * Allocate an LDT entry for a %fs selector, reusing a global
398 * GDT selector if possible. Return the selector value.
400 unsigned short wine_ldt_alloc_fs(void)
402 if (global_fs_sel == -1)
404 #ifdef __linux__
405 struct modify_ldt_s ldt_info;
406 int ret;
408 /* the preloader may have allocated it already */
409 global_fs_sel = wine_get_fs();
410 if (global_fs_sel && is_gdt_sel(global_fs_sel)) return global_fs_sel;
412 ldt_info.entry_number = -1;
413 fill_modify_ldt_struct( &ldt_info, &null_entry );
414 if ((ret = set_thread_area( &ldt_info ) < 0))
416 global_fs_sel = 0; /* don't try it again */
417 if (errno != ENOSYS) perror( "set_thread_area" );
419 else global_fs_sel = (ldt_info.entry_number << 3) | 3;
420 #endif
422 if (global_fs_sel > 0) return global_fs_sel;
423 return wine_ldt_alloc_entries( 1 );
427 /***********************************************************************
428 * wine_ldt_init_fs
430 * Initialize the entry for the %fs selector of the current thread, and
431 * set the thread %fs register.
433 * Note: this runs in the context of the new thread, so cannot acquire locks.
435 void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry )
437 if ((sel & ~3) == (global_fs_sel & ~3))
439 #ifdef __linux__
440 struct modify_ldt_s ldt_info;
441 int ret;
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 #endif
448 else /* LDT selector */
450 internal_set_entry( sel, entry );
452 wine_set_fs( sel );
456 /***********************************************************************
457 * wine_ldt_free_fs
459 * Free a %fs selector returned by wine_ldt_alloc_fs.
461 void wine_ldt_free_fs( unsigned short sel )
463 if (is_gdt_sel(sel)) return; /* nothing to do */
464 if (!((wine_get_fs() ^ sel) & ~3))
466 /* FIXME: if freeing current %fs we cannot acquire locks */
467 wine_set_fs( 0 );
468 internal_set_entry( sel, &null_entry );
469 wine_ldt_copy.flags[sel >> 3] = 0;
471 else wine_ldt_free_entries( sel, 1 );
475 /***********************************************************************
476 * selector access functions
478 # ifndef _MSC_VER
479 /* Nothing needs to be done for MS C, it will do with inline versions from the winnt.h */
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 # endif /* defined(_MSC_VER) */
490 #endif /* __i386__ */