2 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3 * This file MUST be in main library because LDT must
4 * be modified before program creates first thread
5 * - avifile includes this file from C++ code
6 * and initializes it at the start of player!
7 * it might sound like a hack and it really is - but
8 * as aviplay is deconding video with more than just one
9 * thread currently it's necessary to do it this way
10 * this might change in the future
13 /* applied some modification to make make our xine friend more happy */
16 * Modified for use with MPlayer, detailed changelog at
17 * http://svn.mplayerhq.hu/mplayer/trunk/
21 #include "ldt_keeper.h"
28 #include <sys/types.h>
31 #include "osdep/mmap_anon.h"
35 #include <asm/unistd.h>
37 // 2.5.xx+ calls this user_desc:
38 #include <linux/version.h>
39 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47)
40 #define modify_ldt_ldt_s user_desc
42 /* prototype it here, so we won't depend on kernel headers */
46 /// declare modify_ldt with the _syscall3 macro for older glibcs
47 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0))
48 _syscall3( int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
);
50 int modify_ldt(int func
, void *ptr
, unsigned long bytecount
);
56 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
57 #include <machine/segments.h>
58 #include <machine/sysarch.h>
62 #include <sys/segment.h>
63 #include <sys/sysi86.h>
65 /* solaris x86: add missing prototype for sysi86() */
70 int sysi86(int, void*);
76 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */
77 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */
80 #define TEB_SEL_IDX NUMSYSLDTS
83 #define LDT_ENTRIES 8192
84 #define LDT_ENTRY_SIZE 8
86 struct modify_ldt_ldt_s
{
87 unsigned int entry_number
;
88 unsigned long base_addr
;
90 unsigned int seg_32bit
:1;
91 unsigned int contents
:2;
92 unsigned int read_exec_only
:1;
93 unsigned int limit_in_pages
:1;
94 unsigned int seg_not_present
:1;
95 unsigned int useable
:1;
98 #define MODIFY_LDT_CONTENTS_DATA 0
99 #define MODIFY_LDT_CONTENTS_STACK 1
100 #define MODIFY_LDT_CONTENTS_CODE 2
104 /* user level (privilege level: 3) ldt (1<<2) segment selector */
105 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
107 /* i got this value from wine sources, it's the first free LDT entry */
108 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(LDT_AUTO_ALLOC)
109 #define TEB_SEL_IDX LDT_AUTO_ALLOC
114 #define TEB_SEL_IDX 17
117 static unsigned int fs_ldt
= TEB_SEL_IDX
;
121 * here is a small logical problem with Restore for multithreaded programs -
122 * in C++ we use static class for this...
128 void Setup_FS_Segment(void)
130 unsigned int ldt_desc
= LDT_SEL(fs_ldt
);
132 __asm__
__volatile__(
133 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc
)
138 /* we don't need this - use modify_ldt instead */
141 /* XXX: why is this routine from libc redefined here? */
142 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
143 static int LDT_Modify( int func
, struct modify_ldt_ldt_s
*ptr
,
144 unsigned long count
)
148 __asm__
__volatile__( "pushl %%ebx\n\t"
153 : "0" (__NR_modify_ldt
),
156 "d"(16)//sizeof(*ptr) from kernel point of view
159 __asm__
__volatile__("int $0x80"
161 : "0" (__NR_modify_ldt
),
167 if (res
>= 0) return res
;
174 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
175 static void LDT_EntryToBytes( unsigned long *buffer
, const struct modify_ldt_ldt_s
*content
)
177 *buffer
++ = ((content
->base_addr
& 0x0000ffff) << 16) |
178 (content
->limit
& 0x0ffff);
179 *buffer
= (content
->base_addr
& 0xff000000) |
180 ((content
->base_addr
& 0x00ff0000)>>16) |
181 (content
->limit
& 0xf0000) |
182 (content
->contents
<< 10) |
183 ((content
->read_exec_only
== 0) << 9) |
184 ((content
->seg_32bit
!= 0) << 22) |
185 ((content
->limit_in_pages
!= 0) << 23) |
192 ldt_fs_t
* Setup_LDT_Keeper(void)
194 struct modify_ldt_ldt_s array
;
196 ldt_fs_t
* ldt_fs
= (ldt_fs_t
*) malloc(sizeof(ldt_fs_t
));
202 if (getenv("DYLD_BIND_AT_LAUNCH") == NULL
)
203 mp_msg(MSGT_LOADER
, MSGL_WARN
, MSGTR_LOADER_DYLD_Warning
);
204 #endif /* __APPLE__ */
207 ldt_fs
->fs_seg
= mmap_anon(NULL
, getpagesize(), PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, 0);
208 if (ldt_fs
->fs_seg
== (void*)-1)
210 perror("ERROR: Couldn't allocate memory for fs segment");
214 *(void**)((char*)ldt_fs
->fs_seg
+0x18) = ldt_fs
->fs_seg
;
215 memset(&array
, 0, sizeof(array
));
216 array
.base_addr
=(int)ldt_fs
->fs_seg
;
217 array
.entry_number
=TEB_SEL_IDX
;
218 array
.limit
=array
.base_addr
+getpagesize()-1;
220 array
.read_exec_only
=0;
221 array
.seg_not_present
=0;
222 array
.contents
=MODIFY_LDT_CONTENTS_DATA
;
223 array
.limit_in_pages
=0;
225 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
226 ret
=modify_ldt(0x1, &array
, sizeof(struct modify_ldt_ldt_s
));
229 perror("install_fs");
230 printf("Couldn't install fs segment, expect segfault\n");
234 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
238 LDT_EntryToBytes( d
, &array
);
240 ret
= i386_set_ldt(LDT_AUTO_ALLOC
, (union descriptor
*)d
, 1);
241 array
.entry_number
= ret
;
244 ret
= i386_set_ldt(array
.entry_number
, (union descriptor
*)d
, 1);
248 perror("install_fs");
249 printf("Couldn't install fs segment, expect segfault\n");
250 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
252 printf("On newer OpenBSD systems did you set machdep.userldt to 1?\n");
256 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ || __APPLE__ */
258 #if defined(__svr4__)
261 ssd
.sel
= LDT_SEL(TEB_SEL_IDX
);
262 ssd
.bo
= array
.base_addr
;
263 ssd
.ls
= array
.limit
- array
.base_addr
;
264 ssd
.acc1
= ((array
.read_exec_only
== 0) << 1) |
265 (array
.contents
<< 2) |
266 0xf0; /* P(resent) | DPL3 | S */
267 ssd
.acc2
= 0x4; /* byte limit, 32-bit segment */
268 if (sysi86(SI86DSCR
, &ssd
) < 0) {
269 perror("sysi86(SI86DSCR)");
270 printf("Couldn't install fs segment, expect segfault\n");
277 ldt_fs
->prev_struct
= malloc(8);
278 *(void**)array
.base_addr
= ldt_fs
->prev_struct
;
283 void Restore_LDT_Keeper(ldt_fs_t
* ldt_fs
)
285 if (ldt_fs
== NULL
|| ldt_fs
->fs_seg
== 0)
287 if (ldt_fs
->prev_struct
)
288 free(ldt_fs
->prev_struct
);
289 munmap((char*)ldt_fs
->fs_seg
, getpagesize());