sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / loader / ldt_keeper.c
blob92a7df88a52768380a5e2e0a9edb99f763892ff6
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 #include "help_mp.h"
38 #ifdef __linux__
39 #include <asm/unistd.h>
40 #include <asm/ldt.h>
41 // 2.5.xx+ calls this user_desc:
42 #include <linux/version.h>
43 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47)
44 #define modify_ldt_ldt_s user_desc
45 #endif
46 /// declare modify_ldt with the _syscall3 macro for older glibcs
47 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0))
48 _syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
49 #else
50 int modify_ldt(int func, void *ptr, unsigned long bytecount);
51 #endif
52 #else
53 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
54 #include <machine/segments.h>
55 #include <machine/sysarch.h>
56 #elif defined(__APPLE__)
57 #include <architecture/i386/table.h>
58 #include <i386/user_ldt.h>
59 #elif defined(__svr4__)
60 #include <sys/segment.h>
61 #include <sys/sysi86.h>
63 /* solaris x86: add missing prototype for sysi86(), but only when sysi86(int, void*) is known to be valid */
64 #ifdef HAVE_SYSI86_iv
65 int sysi86(int, void*);
66 #endif
68 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */
69 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */
70 #endif
72 #define TEB_SEL_IDX NUMSYSLDTS
73 #endif
75 #define LDT_ENTRIES 8192
76 #define LDT_ENTRY_SIZE 8
77 #pragma pack(4)
78 struct modify_ldt_ldt_s {
79 unsigned int entry_number;
80 unsigned long base_addr;
81 unsigned int limit;
82 unsigned int seg_32bit:1;
83 unsigned int contents:2;
84 unsigned int read_exec_only:1;
85 unsigned int limit_in_pages:1;
86 unsigned int seg_not_present:1;
87 unsigned int useable:1;
90 #define MODIFY_LDT_CONTENTS_DATA 0
91 #define MODIFY_LDT_CONTENTS_STACK 1
92 #define MODIFY_LDT_CONTENTS_CODE 2
93 #endif
96 /* user level (privilege level: 3) ldt (1<<2) segment selector */
97 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
99 /* i got this value from wine sources, it's the first free LDT entry */
100 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(LDT_AUTO_ALLOC)
101 #define TEB_SEL_IDX LDT_AUTO_ALLOC
102 #define USE_LDT_AA
103 #endif
105 #ifndef TEB_SEL_IDX
106 #define TEB_SEL_IDX 17
107 #endif
109 static unsigned int fs_ldt = TEB_SEL_IDX;
113 * here is a small logical problem with Restore for multithreaded programs -
114 * in C++ we use static class for this...
117 void Setup_FS_Segment(void)
119 unsigned int ldt_desc = LDT_SEL(fs_ldt);
121 __asm__ volatile(
122 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc)
123 :"eax"
127 /* we don't need this - use modify_ldt instead */
128 #if 0
129 #ifdef __linux__
130 /* XXX: why is this routine from libc redefined here? */
131 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
132 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
133 unsigned long count )
135 int res;
136 #ifdef __PIC__
137 __asm__ volatile( "pushl %%ebx\n\t"
138 "movl %2,%%ebx\n\t"
139 "int $0x80\n\t"
140 "popl %%ebx"
141 : "=a" (res)
142 : "0" (__NR_modify_ldt),
143 "r" (func),
144 "c" (ptr),
145 "d"(16)//sizeof(*ptr) from kernel point of view
146 :"esi" );
147 #else
148 __asm__ volatile("int $0x80"
149 : "=a" (res)
150 : "0" (__NR_modify_ldt),
151 "b" (func),
152 "c" (ptr),
153 "d"(16)
154 :"esi");
155 #endif /* __PIC__ */
156 if (res >= 0) return res;
157 errno = -res;
158 return -1;
160 #endif
161 #endif
163 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
164 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
166 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
167 (content->limit & 0x0ffff);
168 *buffer = (content->base_addr & 0xff000000) |
169 ((content->base_addr & 0x00ff0000)>>16) |
170 (content->limit & 0xf0000) |
171 (content->contents << 10) |
172 ((content->read_exec_only == 0) << 9) |
173 ((content->seg_32bit != 0) << 22) |
174 ((content->limit_in_pages != 0) << 23) |
175 0xf000;
177 #endif
179 void* fs_seg=0;
181 ldt_fs_t* Setup_LDT_Keeper(void)
183 struct modify_ldt_ldt_s array;
184 int ret;
185 ldt_fs_t* ldt_fs = malloc(sizeof(ldt_fs_t));
187 if (!ldt_fs)
188 return NULL;
190 #ifdef __APPLE__
191 if (getenv("DYLD_BIND_AT_LAUNCH") == NULL)
192 mp_msg(MSGT_LOADER, MSGL_WARN, MSGTR_LOADER_DYLD_Warning);
193 #endif /* __APPLE__ */
195 fs_seg=
196 ldt_fs->fs_seg = mmap_anon(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, 0);
197 if (ldt_fs->fs_seg == (void*)-1)
199 perror("ERROR: Couldn't allocate memory for fs segment");
200 free(ldt_fs);
201 return NULL;
203 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg;
204 memset(&array, 0, sizeof(array));
205 array.base_addr=(int)ldt_fs->fs_seg;
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__
214 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
215 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s));
216 if(ret<0)
218 perror("install_fs");
219 printf("Couldn't install fs segment, expect segfault\n");
221 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
223 unsigned long d[2];
225 LDT_EntryToBytes( d, &array );
226 #ifdef USE_LDT_AA
227 ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1);
228 array.entry_number = ret;
229 fs_ldt = ret;
230 #else
231 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
232 #endif
233 if (ret < 0)
235 perror("install_fs");
236 printf("Couldn't install fs segment, expect segfault\n");
237 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
238 #ifdef __OpenBSD__
239 printf("On newer OpenBSD systems did you set machdep.userldt to 1?\n");
240 #endif
243 #elif defined(__svr4__)
245 struct ssd ssd;
246 ssd.sel = LDT_SEL(TEB_SEL_IDX);
247 ssd.bo = array.base_addr;
248 ssd.ls = array.limit - array.base_addr;
249 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
250 (array.contents << 2) |
251 0xf0; /* P(resent) | DPL3 | S */
252 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
253 if (sysi86(SI86DSCR, &ssd) < 0) {
254 perror("sysi86(SI86DSCR)");
255 printf("Couldn't install fs segment, expect segfault\n");
258 #elif defined(__OS2__)
259 /* convert flat addr to sel idx for LDT_SEL() */
260 fs_ldt = (uintptr_t)fs_seg >> 16;
261 #endif
263 Setup_FS_Segment();
265 ldt_fs->prev_struct = malloc(8);
266 *(void**)array.base_addr = ldt_fs->prev_struct;
268 return ldt_fs;
271 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs)
273 if (ldt_fs == NULL || ldt_fs->fs_seg == 0)
274 return;
275 if (ldt_fs->prev_struct)
276 free(ldt_fs->prev_struct);
277 munmap((char*)ldt_fs->fs_seg, getpagesize());
278 ldt_fs->fs_seg = 0;
279 free(ldt_fs);