Mercurial > mplayer.hg
annotate loader/ldt_keeper.c @ 30486:51db2b34b91c
Fix linking of mp3lib test programs when internal mp3lib is disabled.
Dependencies were only set correctly if internal mp3lib was enabled.
author | diego |
---|---|
date | Mon, 08 Feb 2010 23:23:04 +0000 |
parents | 74d33572f1ff |
children | 26f673ba0675 |
rev | line source |
---|---|
2067 | 1 /** |
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! | |
7386 | 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 | |
2067 | 11 */ |
12 | |
7386 | 13 /* applied some modification to make make our xine friend more happy */ |
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
14537
diff
changeset
|
14 |
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
14537
diff
changeset
|
15 /* |
18783 | 16 * Modified for use with MPlayer, detailed changelog at |
17 * http://svn.mplayerhq.hu/mplayer/trunk/ | |
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
14537
diff
changeset
|
18 */ |
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
14537
diff
changeset
|
19 |
26105 | 20 #include "config.h" |
2139 | 21 #include "ldt_keeper.h" |
22 | |
2067 | 23 #include <string.h> |
24 #include <stdlib.h> | |
25 #include <errno.h> | |
26 #include <fcntl.h> | |
26105 | 27 #ifdef HAVE_SYS_MMAN_H |
2067 | 28 #include <sys/mman.h> |
26111
eb82d1524b6d
#include osdep/mman.h if sys/mman.h is not available.
diego
parents:
26105
diff
changeset
|
29 #else |
eb82d1524b6d
#include osdep/mman.h if sys/mman.h is not available.
diego
parents:
26105
diff
changeset
|
30 #include "osdep/mmap.h" |
26105 | 31 #endif |
2067 | 32 #include <sys/types.h> |
33 #include <stdio.h> | |
34 #include <unistd.h> | |
21187
d9cedf7b8069
added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
18878
diff
changeset
|
35 #include "osdep/mmap_anon.h" |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
36 #include "mp_msg.h" |
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
37 #include "help_mp.h" |
2067 | 38 #ifdef __linux__ |
39 #include <asm/unistd.h> | |
40 #include <asm/ldt.h> | |
8213 | 41 // 2.5.xx+ calls this user_desc: |
42 #include <linux/version.h> | |
43 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47) | |
44 #define modify_ldt_ldt_s user_desc | |
45 #endif | |
7386 | 46 /* prototype it here, so we won't depend on kernel headers */ |
47 #ifdef __cplusplus | |
48 extern "C" { | |
49 #endif | |
13351
698fe63084d3
declare modify_ldt with syscall3 macro for older glibcs patch by Mikulas Patocka <mikulas at artax.karlin.mff.cuni.cz>
faust3
parents:
10821
diff
changeset
|
50 /// declare modify_ldt with the _syscall3 macro for older glibcs |
13361 | 51 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)) |
13351
698fe63084d3
declare modify_ldt with syscall3 macro for older glibcs patch by Mikulas Patocka <mikulas at artax.karlin.mff.cuni.cz>
faust3
parents:
10821
diff
changeset
|
52 _syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount ); |
698fe63084d3
declare modify_ldt with syscall3 macro for older glibcs patch by Mikulas Patocka <mikulas at artax.karlin.mff.cuni.cz>
faust3
parents:
10821
diff
changeset
|
53 #else |
7386 | 54 int modify_ldt(int func, void *ptr, unsigned long bytecount); |
13351
698fe63084d3
declare modify_ldt with syscall3 macro for older glibcs patch by Mikulas Patocka <mikulas at artax.karlin.mff.cuni.cz>
faust3
parents:
10821
diff
changeset
|
55 #endif |
7386 | 56 #ifdef __cplusplus |
57 } | |
58 #endif | |
2067 | 59 #else |
15566 | 60 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
5872 | 61 #include <machine/segments.h> |
2067 | 62 #include <machine/sysarch.h> |
30483
74d33572f1ff
Merge some preprocessor conditionals where appropriate.
diego
parents:
29263
diff
changeset
|
63 #elif defined(__APPLE__) |
26054
d9b11d83367f
Add missing #includes for Mac OS X, fixes the warning
diego
parents:
25849
diff
changeset
|
64 #include <architecture/i386/table.h> |
d9b11d83367f
Add missing #includes for Mac OS X, fixes the warning
diego
parents:
25849
diff
changeset
|
65 #include <i386/user_ldt.h> |
30483
74d33572f1ff
Merge some preprocessor conditionals where appropriate.
diego
parents:
29263
diff
changeset
|
66 #elif defined(__svr4__) |
2067 | 67 #include <sys/segment.h> |
68 #include <sys/sysi86.h> | |
69 | |
22715
363c84953547
check that definition of prototype of sysi86(int, void*) doesn't conflict; fixed compilation in opensolaris
nicodvb
parents:
21290
diff
changeset
|
70 /* solaris x86: add missing prototype for sysi86(), but only when sysi86(int, void*) is known to be valid */ |
363c84953547
check that definition of prototype of sysi86(int, void*) doesn't conflict; fixed compilation in opensolaris
nicodvb
parents:
21290
diff
changeset
|
71 #ifdef HAVE_SYSI86_iv |
2067 | 72 #ifdef __cplusplus |
73 extern "C" { | |
74 #endif | |
7386 | 75 int sysi86(int, void*); |
2067 | 76 #ifdef __cplusplus |
77 } | |
78 #endif | |
18362
29b2221982b3
Add a check for sysi86() on Solaris systems, solving a build failure on Solaris
diego
parents:
17605
diff
changeset
|
79 #endif |
2067 | 80 |
2139 | 81 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */ |
82 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */ | |
2067 | 83 #endif |
84 | |
85 #define TEB_SEL_IDX NUMSYSLDTS | |
86 #endif | |
87 | |
88 #define LDT_ENTRIES 8192 | |
89 #define LDT_ENTRY_SIZE 8 | |
90 #pragma pack(4) | |
91 struct modify_ldt_ldt_s { | |
92 unsigned int entry_number; | |
93 unsigned long base_addr; | |
94 unsigned int limit; | |
95 unsigned int seg_32bit:1; | |
96 unsigned int contents:2; | |
97 unsigned int read_exec_only:1; | |
98 unsigned int limit_in_pages:1; | |
99 unsigned int seg_not_present:1; | |
100 unsigned int useable:1; | |
101 }; | |
102 | |
103 #define MODIFY_LDT_CONTENTS_DATA 0 | |
104 #define MODIFY_LDT_CONTENTS_STACK 1 | |
105 #define MODIFY_LDT_CONTENTS_CODE 2 | |
106 #endif | |
107 | |
108 | |
109 /* user level (privilege level: 3) ldt (1<<2) segment selector */ | |
110 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) | |
111 | |
7386 | 112 /* i got this value from wine sources, it's the first free LDT entry */ |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
113 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(LDT_AUTO_ALLOC) |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
114 #define TEB_SEL_IDX LDT_AUTO_ALLOC |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
115 #define USE_LDT_AA |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
116 #endif |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
117 |
2067 | 118 #ifndef TEB_SEL_IDX |
7386 | 119 #define TEB_SEL_IDX 17 |
2067 | 120 #endif |
7386 | 121 |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
122 static unsigned int fs_ldt = TEB_SEL_IDX; |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
123 |
2067 | 124 |
125 /** | |
126 * here is a small logical problem with Restore for multithreaded programs - | |
127 * in C++ we use static class for this... | |
128 */ | |
129 | |
130 #ifdef __cplusplus | |
131 extern "C" | |
132 #endif | |
133 void Setup_FS_Segment(void) | |
134 { | |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
135 unsigned int ldt_desc = LDT_SEL(fs_ldt); |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
136 |
27757
b5a46071062a
Replace all occurrences of '__volatile__' and '__volatile' by plain 'volatile'.
diego
parents:
26111
diff
changeset
|
137 __asm__ volatile( |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
138 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc) |
14212
540903a59fc0
add missing registers in clobber list, fixes bug #169
reimar
parents:
13361
diff
changeset
|
139 :"eax" |
2067 | 140 ); |
141 } | |
142 | |
7386 | 143 /* we don't need this - use modify_ldt instead */ |
144 #if 0 | |
2067 | 145 #ifdef __linux__ |
146 /* XXX: why is this routine from libc redefined here? */ | |
147 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */ | |
148 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr, | |
149 unsigned long count ) | |
150 { | |
151 int res; | |
152 #ifdef __PIC__ | |
27757
b5a46071062a
Replace all occurrences of '__volatile__' and '__volatile' by plain 'volatile'.
diego
parents:
26111
diff
changeset
|
153 __asm__ volatile( "pushl %%ebx\n\t" |
2067 | 154 "movl %2,%%ebx\n\t" |
155 "int $0x80\n\t" | |
156 "popl %%ebx" | |
157 : "=a" (res) | |
158 : "0" (__NR_modify_ldt), | |
159 "r" (func), | |
160 "c" (ptr), | |
161 "d"(16)//sizeof(*ptr) from kernel point of view | |
162 :"esi" ); | |
163 #else | |
27757
b5a46071062a
Replace all occurrences of '__volatile__' and '__volatile' by plain 'volatile'.
diego
parents:
26111
diff
changeset
|
164 __asm__ volatile("int $0x80" |
2067 | 165 : "=a" (res) |
166 : "0" (__NR_modify_ldt), | |
167 "b" (func), | |
168 "c" (ptr), | |
169 "d"(16) | |
170 :"esi"); | |
171 #endif /* __PIC__ */ | |
172 if (res >= 0) return res; | |
173 errno = -res; | |
174 return -1; | |
175 } | |
176 #endif | |
7386 | 177 #endif |
2067 | 178 |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
179 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
2067 | 180 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) |
181 { | |
182 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | | |
183 (content->limit & 0x0ffff); | |
184 *buffer = (content->base_addr & 0xff000000) | | |
185 ((content->base_addr & 0x00ff0000)>>16) | | |
186 (content->limit & 0xf0000) | | |
187 (content->contents << 10) | | |
188 ((content->read_exec_only == 0) << 9) | | |
189 ((content->seg_32bit != 0) << 22) | | |
190 ((content->limit_in_pages != 0) << 23) | | |
191 0xf000; | |
192 } | |
193 #endif | |
194 | |
8223 | 195 void* fs_seg=0; |
7386 | 196 |
197 ldt_fs_t* Setup_LDT_Keeper(void) | |
2067 | 198 { |
199 struct modify_ldt_ldt_s array; | |
200 int ret; | |
7386 | 201 ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t)); |
2067 | 202 |
7386 | 203 if (!ldt_fs) |
204 return NULL; | |
2067 | 205 |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
206 #ifdef __APPLE__ |
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
207 if (getenv("DYLD_BIND_AT_LAUNCH") == NULL) |
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
208 mp_msg(MSGT_LOADER, MSGL_WARN, MSGTR_LOADER_DYLD_Warning); |
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
209 #endif /* __APPLE__ */ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27757
diff
changeset
|
210 |
8223 | 211 fs_seg= |
21248 | 212 ldt_fs->fs_seg = mmap_anon(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, 0); |
7386 | 213 if (ldt_fs->fs_seg == (void*)-1) |
2067 | 214 { |
215 perror("ERROR: Couldn't allocate memory for fs segment"); | |
7386 | 216 free(ldt_fs); |
217 return NULL; | |
2067 | 218 } |
7386 | 219 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg; |
14537 | 220 memset(&array, 0, sizeof(array)); |
7386 | 221 array.base_addr=(int)ldt_fs->fs_seg; |
2067 | 222 array.entry_number=TEB_SEL_IDX; |
223 array.limit=array.base_addr+getpagesize()-1; | |
224 array.seg_32bit=1; | |
225 array.read_exec_only=0; | |
226 array.seg_not_present=0; | |
227 array.contents=MODIFY_LDT_CONTENTS_DATA; | |
228 array.limit_in_pages=0; | |
229 #ifdef __linux__ | |
7386 | 230 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s)); |
231 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s)); | |
2067 | 232 if(ret<0) |
233 { | |
234 perror("install_fs"); | |
235 printf("Couldn't install fs segment, expect segfault\n"); | |
236 } | |
30483
74d33572f1ff
Merge some preprocessor conditionals where appropriate.
diego
parents:
29263
diff
changeset
|
237 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
2067 | 238 { |
239 unsigned long d[2]; | |
240 | |
241 LDT_EntryToBytes( d, &array ); | |
21290
efc774a1e5a4
fix compilation for win32 dll codec support for intel osx
nplourde
parents:
21248
diff
changeset
|
242 #ifdef USE_LDT_AA |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
243 ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1); |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
244 array.entry_number = ret; |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
245 fs_ldt = ret; |
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
246 #else |
2067 | 247 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
248 #endif |
2067 | 249 if (ret < 0) |
250 { | |
251 perror("install_fs"); | |
252 printf("Couldn't install fs segment, expect segfault\n"); | |
253 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); | |
17605 | 254 #ifdef __OpenBSD__ |
255 printf("On newer OpenBSD systems did you set machdep.userldt to 1?\n"); | |
256 #endif | |
2067 | 257 } |
258 } | |
30483
74d33572f1ff
Merge some preprocessor conditionals where appropriate.
diego
parents:
29263
diff
changeset
|
259 #elif defined(__svr4__) |
2070
c1edbb8bfc0c
(solaris x86) C++ style variable declaration not at the start of a block does
jkeil
parents:
2069
diff
changeset
|
260 { |
2139 | 261 struct ssd ssd; |
10821
e9e5dca4af9e
FreeBSD 5.0 (libkse/libthr) support by Dan Eischen <eischen@vigrid.com>
alex
parents:
8223
diff
changeset
|
262 ssd.sel = LDT_SEL(TEB_SEL_IDX); |
2139 | 263 ssd.bo = array.base_addr; |
264 ssd.ls = array.limit - array.base_addr; | |
265 ssd.acc1 = ((array.read_exec_only == 0) << 1) | | |
266 (array.contents << 2) | | |
267 0xf0; /* P(resent) | DPL3 | S */ | |
268 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */ | |
269 if (sysi86(SI86DSCR, &ssd) < 0) { | |
270 perror("sysi86(SI86DSCR)"); | |
271 printf("Couldn't install fs segment, expect segfault\n"); | |
272 } | |
2070
c1edbb8bfc0c
(solaris x86) C++ style variable declaration not at the start of a block does
jkeil
parents:
2069
diff
changeset
|
273 } |
2067 | 274 #endif |
275 | |
276 Setup_FS_Segment(); | |
277 | |
18878 | 278 ldt_fs->prev_struct = malloc(8); |
7386 | 279 *(void**)array.base_addr = ldt_fs->prev_struct; |
280 | |
281 return ldt_fs; | |
2067 | 282 } |
283 | |
7386 | 284 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs) |
2067 | 285 { |
7386 | 286 if (ldt_fs == NULL || ldt_fs->fs_seg == 0) |
2067 | 287 return; |
7386 | 288 if (ldt_fs->prev_struct) |
289 free(ldt_fs->prev_struct); | |
290 munmap((char*)ldt_fs->fs_seg, getpagesize()); | |
291 ldt_fs->fs_seg = 0; | |
292 free(ldt_fs); | |
2067 | 293 } |