ldt.c 17 KB

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