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