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