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