remove trailing whitespaces
[mplayer/glamo.git] / loader / ldt_keeper.c
blobe0d4447e604dbd29169125d66d9f7872358b0401
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 /* prototype it here, so we won't depend on kernel headers */
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 /// declare modify_ldt with the _syscall3 macro for older glibcs
51 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0))
52 _syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
53 #else
54 int modify_ldt(int func, void *ptr, unsigned long bytecount);
55 #endif
56 #ifdef __cplusplus
58 #endif
59 #else
60 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
61 #include <machine/segments.h>
62 #include <machine/sysarch.h>
63 #endif
65 #if defined(__APPLE__)
66 #include <architecture/i386/table.h>
67 #include <i386/user_ldt.h>
68 #endif
70 #ifdef __svr4__
71 #include <sys/segment.h>
72 #include <sys/sysi86.h>
74 /* solaris x86: add missing prototype for sysi86(), but only when sysi86(int, void*) is known to be valid */
75 #ifdef HAVE_SYSI86_iv
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 int sysi86(int, void*);
80 #ifdef __cplusplus
82 #endif
83 #endif
85 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */
86 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */
87 #endif
89 #define TEB_SEL_IDX NUMSYSLDTS
90 #endif
92 #define LDT_ENTRIES 8192
93 #define LDT_ENTRY_SIZE 8
94 #pragma pack(4)
95 struct modify_ldt_ldt_s {
96 unsigned int entry_number;
97 unsigned long base_addr;
98 unsigned int limit;
99 unsigned int seg_32bit:1;
100 unsigned int contents:2;
101 unsigned int read_exec_only:1;
102 unsigned int limit_in_pages:1;
103 unsigned int seg_not_present:1;
104 unsigned int useable:1;
107 #define MODIFY_LDT_CONTENTS_DATA 0
108 #define MODIFY_LDT_CONTENTS_STACK 1
109 #define MODIFY_LDT_CONTENTS_CODE 2
110 #endif
113 /* user level (privilege level: 3) ldt (1<<2) segment selector */
114 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
116 /* i got this value from wine sources, it's the first free LDT entry */
117 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(LDT_AUTO_ALLOC)
118 #define TEB_SEL_IDX LDT_AUTO_ALLOC
119 #define USE_LDT_AA
120 #endif
122 #ifndef TEB_SEL_IDX
123 #define TEB_SEL_IDX 17
124 #endif
126 static unsigned int fs_ldt = TEB_SEL_IDX;
130 * here is a small logical problem with Restore for multithreaded programs -
131 * in C++ we use static class for this...
134 #ifdef __cplusplus
135 extern "C"
136 #endif
137 void Setup_FS_Segment(void)
139 unsigned int ldt_desc = LDT_SEL(fs_ldt);
141 __asm__ volatile(
142 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc)
143 :"eax"
147 /* we don't need this - use modify_ldt instead */
148 #if 0
149 #ifdef __linux__
150 /* XXX: why is this routine from libc redefined here? */
151 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
152 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
153 unsigned long count )
155 int res;
156 #ifdef __PIC__
157 __asm__ volatile( "pushl %%ebx\n\t"
158 "movl %2,%%ebx\n\t"
159 "int $0x80\n\t"
160 "popl %%ebx"
161 : "=a" (res)
162 : "0" (__NR_modify_ldt),
163 "r" (func),
164 "c" (ptr),
165 "d"(16)//sizeof(*ptr) from kernel point of view
166 :"esi" );
167 #else
168 __asm__ volatile("int $0x80"
169 : "=a" (res)
170 : "0" (__NR_modify_ldt),
171 "b" (func),
172 "c" (ptr),
173 "d"(16)
174 :"esi");
175 #endif /* __PIC__ */
176 if (res >= 0) return res;
177 errno = -res;
178 return -1;
180 #endif
181 #endif
183 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
184 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
186 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
187 (content->limit & 0x0ffff);
188 *buffer = (content->base_addr & 0xff000000) |
189 ((content->base_addr & 0x00ff0000)>>16) |
190 (content->limit & 0xf0000) |
191 (content->contents << 10) |
192 ((content->read_exec_only == 0) << 9) |
193 ((content->seg_32bit != 0) << 22) |
194 ((content->limit_in_pages != 0) << 23) |
195 0xf000;
197 #endif
199 void* fs_seg=0;
201 ldt_fs_t* Setup_LDT_Keeper(void)
203 struct modify_ldt_ldt_s array;
204 int ret;
205 ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t));
207 if (!ldt_fs)
208 return NULL;
210 #ifdef __APPLE__
211 if (getenv("DYLD_BIND_AT_LAUNCH") == NULL)
212 mp_msg(MSGT_LOADER, MSGL_WARN, MSGTR_LOADER_DYLD_Warning);
213 #endif /* __APPLE__ */
215 fs_seg=
216 ldt_fs->fs_seg = mmap_anon(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, 0);
217 if (ldt_fs->fs_seg == (void*)-1)
219 perror("ERROR: Couldn't allocate memory for fs segment");
220 free(ldt_fs);
221 return NULL;
223 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg;
224 memset(&array, 0, sizeof(array));
225 array.base_addr=(int)ldt_fs->fs_seg;
226 array.entry_number=TEB_SEL_IDX;
227 array.limit=array.base_addr+getpagesize()-1;
228 array.seg_32bit=1;
229 array.read_exec_only=0;
230 array.seg_not_present=0;
231 array.contents=MODIFY_LDT_CONTENTS_DATA;
232 array.limit_in_pages=0;
233 #ifdef __linux__
234 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
235 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s));
236 if(ret<0)
238 perror("install_fs");
239 printf("Couldn't install fs segment, expect segfault\n");
241 #endif /*linux*/
243 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
245 unsigned long d[2];
247 LDT_EntryToBytes( d, &array );
248 #ifdef USE_LDT_AA
249 ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1);
250 array.entry_number = ret;
251 fs_ldt = ret;
252 #else
253 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
254 #endif
255 if (ret < 0)
257 perror("install_fs");
258 printf("Couldn't install fs segment, expect segfault\n");
259 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
260 #ifdef __OpenBSD__
261 printf("On newer OpenBSD systems did you set machdep.userldt to 1?\n");
262 #endif
265 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ || __APPLE__ */
267 #if defined(__svr4__)
269 struct ssd ssd;
270 ssd.sel = LDT_SEL(TEB_SEL_IDX);
271 ssd.bo = array.base_addr;
272 ssd.ls = array.limit - array.base_addr;
273 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
274 (array.contents << 2) |
275 0xf0; /* P(resent) | DPL3 | S */
276 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
277 if (sysi86(SI86DSCR, &ssd) < 0) {
278 perror("sysi86(SI86DSCR)");
279 printf("Couldn't install fs segment, expect segfault\n");
282 #endif
284 Setup_FS_Segment();
286 ldt_fs->prev_struct = malloc(8);
287 *(void**)array.base_addr = ldt_fs->prev_struct;
289 return ldt_fs;
292 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs)
294 if (ldt_fs == NULL || ldt_fs->fs_seg == 0)
295 return;
296 if (ldt_fs->prev_struct)
297 free(ldt_fs->prev_struct);
298 munmap((char*)ldt_fs->fs_seg, getpagesize());
299 ldt_fs->fs_seg = 0;
300 free(ldt_fs);