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