manpage: restore -lavfdopts doc accidentally removed in d76ad5f2
[mplayer.git] / loader / ldt_keeper.c
blob3f7933426ab2a3722ee7071cd2be783da1bec488
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!
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
13 /* applied some modification to make make our xine friend more happy */
16 * Modified for use with MPlayer, detailed changelog at
17 * http://svn.mplayerhq.hu/mplayer/trunk/
20 #include "config.h"
21 #include "ldt_keeper.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #else
30 #include "osdep/mmap.h"
31 #endif
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include "osdep/mmap_anon.h"
36 #include "mp_msg.h"
37 #ifdef __linux__
38 #include <asm/unistd.h>
39 #include <asm/ldt.h>
40 // 2.5.xx+ calls this user_desc:
41 #include <linux/version.h>
42 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47)
43 #define modify_ldt_ldt_s user_desc
44 #endif
45 /// declare modify_ldt with the _syscall3 macro for older glibcs
46 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0))
47 _syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
48 #else
49 int modify_ldt(int func, void *ptr, unsigned long bytecount);
50 #endif
51 #else
52 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
53 #include <machine/segments.h>
54 #include <machine/sysarch.h>
55 #elif defined(__APPLE__)
56 #include <architecture/i386/table.h>
57 #include <i386/user_ldt.h>
58 #elif defined(__svr4__)
59 #include <sys/segment.h>
60 #include <sys/sysi86.h>
62 /* solaris x86: add missing prototype for sysi86(), but only when sysi86(int, void*) is known to be valid */
63 #ifdef HAVE_SYSI86_iv
64 int sysi86(int, void*);
65 #endif
67 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */
68 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */
69 #endif
71 #define TEB_SEL_IDX NUMSYSLDTS
72 #endif
74 #define LDT_ENTRIES 8192
75 #define LDT_ENTRY_SIZE 8
76 #pragma pack(4)
77 struct modify_ldt_ldt_s {
78 unsigned int entry_number;
79 unsigned long base_addr;
80 unsigned int limit;
81 unsigned int seg_32bit:1;
82 unsigned int contents:2;
83 unsigned int read_exec_only:1;
84 unsigned int limit_in_pages:1;
85 unsigned int seg_not_present:1;
86 unsigned int useable:1;
89 #define MODIFY_LDT_CONTENTS_DATA 0
90 #define MODIFY_LDT_CONTENTS_STACK 1
91 #define MODIFY_LDT_CONTENTS_CODE 2
92 #endif
95 /* user level (privilege level: 3) ldt (1<<2) segment selector */
96 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
98 /* i got this value from wine sources, it's the first free LDT entry */
99 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(LDT_AUTO_ALLOC)
100 #define TEB_SEL_IDX LDT_AUTO_ALLOC
101 #define USE_LDT_AA
102 #endif
104 #ifndef TEB_SEL_IDX
105 #define TEB_SEL_IDX 17
106 #endif
108 static unsigned int fs_ldt = TEB_SEL_IDX;
112 * here is a small logical problem with Restore for multithreaded programs -
113 * in C++ we use static class for this...
116 void Setup_FS_Segment(void)
118 unsigned int ldt_desc = LDT_SEL(fs_ldt);
120 __asm__ volatile(
121 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc)
122 :"eax"
126 /* we don't need this - use modify_ldt instead */
127 #if 0
128 #ifdef __linux__
129 /* XXX: why is this routine from libc redefined here? */
130 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
131 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
132 unsigned long count )
134 int res;
135 #ifdef __PIC__
136 __asm__ volatile( "pushl %%ebx\n\t"
137 "movl %2,%%ebx\n\t"
138 "int $0x80\n\t"
139 "popl %%ebx"
140 : "=a" (res)
141 : "0" (__NR_modify_ldt),
142 "r" (func),
143 "c" (ptr),
144 "d"(16)//sizeof(*ptr) from kernel point of view
145 :"esi" );
146 #else
147 __asm__ volatile("int $0x80"
148 : "=a" (res)
149 : "0" (__NR_modify_ldt),
150 "b" (func),
151 "c" (ptr),
152 "d"(16)
153 :"esi");
154 #endif /* __PIC__ */
155 if (res >= 0) return res;
156 errno = -res;
157 return -1;
159 #endif
160 #endif
162 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
163 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
165 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
166 (content->limit & 0x0ffff);
167 *buffer = (content->base_addr & 0xff000000) |
168 ((content->base_addr & 0x00ff0000)>>16) |
169 (content->limit & 0xf0000) |
170 (content->contents << 10) |
171 ((content->read_exec_only == 0) << 9) |
172 ((content->seg_32bit != 0) << 22) |
173 ((content->limit_in_pages != 0) << 23) |
174 0xf000;
176 #endif
178 void* fs_seg=0;
180 ldt_fs_t* Setup_LDT_Keeper(void)
182 struct modify_ldt_ldt_s array;
183 int ret;
184 ldt_fs_t* ldt_fs = malloc(sizeof(ldt_fs_t));
186 if (!ldt_fs)
187 return NULL;
189 #ifdef __APPLE__
190 if (getenv("DYLD_BIND_AT_LAUNCH") == NULL)
191 mp_tmsg(MSGT_LOADER, MSGL_WARN, "WARNING: Attempting to use DLL codecs but environment variable\n DYLD_BIND_AT_LAUNCH not set. This will likely crash.\n");
192 #endif /* __APPLE__ */
194 fs_seg=
195 ldt_fs->fs_seg = mmap_anon(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, 0);
196 if (ldt_fs->fs_seg == (void*)-1)
198 perror("ERROR: Couldn't allocate memory for fs segment");
199 free(ldt_fs);
200 return NULL;
202 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg;
203 memset(&array, 0, sizeof(array));
204 array.base_addr=(int)ldt_fs->fs_seg;
205 array.entry_number=TEB_SEL_IDX;
206 array.limit=array.base_addr+getpagesize()-1;
207 array.seg_32bit=1;
208 array.read_exec_only=0;
209 array.seg_not_present=0;
210 array.contents=MODIFY_LDT_CONTENTS_DATA;
211 array.limit_in_pages=0;
212 #ifdef __linux__
213 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
214 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s));
215 if(ret<0)
217 perror("install_fs");
218 printf("Couldn't install fs segment, expect segfault\n");
220 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
222 unsigned long d[2];
224 LDT_EntryToBytes( d, &array );
225 #ifdef USE_LDT_AA
226 ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1);
227 array.entry_number = ret;
228 fs_ldt = ret;
229 #else
230 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
231 #endif
232 if (ret < 0)
234 perror("install_fs");
235 printf("Couldn't install fs segment, expect segfault\n");
236 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
237 #ifdef __OpenBSD__
238 printf("On newer OpenBSD systems did you set machdep.userldt to 1?\n");
239 #endif
242 #elif defined(__svr4__)
244 struct ssd ssd;
245 ssd.sel = LDT_SEL(TEB_SEL_IDX);
246 ssd.bo = array.base_addr;
247 ssd.ls = array.limit - array.base_addr;
248 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
249 (array.contents << 2) |
250 0xf0; /* P(resent) | DPL3 | S */
251 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
252 if (sysi86(SI86DSCR, &ssd) < 0) {
253 perror("sysi86(SI86DSCR)");
254 printf("Couldn't install fs segment, expect segfault\n");
257 #elif defined(__OS2__)
258 /* convert flat addr to sel idx for LDT_SEL() */
259 fs_ldt = (uintptr_t)fs_seg >> 16;
260 #endif
262 Setup_FS_Segment();
264 ldt_fs->prev_struct = malloc(8);
265 *(void**)array.base_addr = ldt_fs->prev_struct;
267 return ldt_fs;
270 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs)
272 if (ldt_fs == NULL || ldt_fs->fs_seg == 0)
273 return;
274 free(ldt_fs->prev_struct);
275 munmap((char*)ldt_fs->fs_seg, getpagesize());
276 ldt_fs->fs_seg = 0;
277 free(ldt_fs);