ldt.c 17 KB

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