ldt.c 17 KB

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