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