]> git.sesse.net Git - vlc/blob - libs/loader/ldt_keeper.c
33d304aac4a0ba6751c632db4313546803a8b5f2
[vlc] / libs / loader / ldt_keeper.c
1 /*
2  * $Id$
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  *
7  * Originally distributed under LPGL 2.1 (or later) by the Wine project.
8  *
9  * Modified for use with MPlayer, detailed CVS changelog at
10  * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
11  *
12  * File now distributed as part of VLC media player with no modifications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  */
28
29 /**
30  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31  * This file MUST be in main library because LDT must
32  * be modified before program creates first thread
33  * - avifile includes this file from C++ code
34  * and initializes it at the start of player!
35  * it might sound like a hack and it really is - but
36  * as aviplay is deconding video with more than just one
37  * thread currently it's necessary to do it this way
38  * this might change in the future
39  */
40
41 /* applied some modification to make make our xine friend more happy */
42
43 #include "ldt_keeper.h"
44
45 #include <string.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <sys/mman.h>
50 #include <sys/types.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #ifdef __linux__
54 #include <asm/unistd.h>
55 #include <asm/ldt.h>
56 // 2.5.xx+ calls this user_desc:
57 #include <linux/version.h>
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47)
59 #define modify_ldt_ldt_s user_desc
60 #endif
61 /* prototype it here, so we won't depend on kernel headers */
62 #ifdef  __cplusplus
63 extern "C" {
64 #endif
65 /// declare modify_ldt with the _syscall3 macro for older glibcs
66 #if defined(__GLIBC__) &&  (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0))
67 _syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
68 #else
69 int modify_ldt(int func, void *ptr, unsigned long bytecount);
70 #endif
71 #ifdef  __cplusplus
72 }
73 #endif
74 #else
75 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
76 #include <machine/segments.h>
77 #include <machine/sysarch.h>
78 #endif
79
80 #ifdef __svr4__
81 #include <sys/segment.h>
82 #include <sys/sysi86.h>
83
84 /* solaris x86: add missing prototype for sysi86() */
85 #ifdef  __cplusplus
86 extern "C" {
87 #endif
88 int sysi86(int, void*);
89 #ifdef  __cplusplus
90 }
91 #endif
92
93 #ifndef NUMSYSLDTS             /* SunOS 2.5.1 does not define NUMSYSLDTS */
94 #define NUMSYSLDTS     6       /* Let's hope the SunOS 5.8 value is OK */
95 #endif
96
97 #define       TEB_SEL_IDX     NUMSYSLDTS
98 #endif
99
100 #define LDT_ENTRIES     8192
101 #define LDT_ENTRY_SIZE  8
102 #pragma pack(4)
103 struct modify_ldt_ldt_s {
104         unsigned int  entry_number;
105         unsigned long base_addr;
106         unsigned int  limit;
107         unsigned int  seg_32bit:1;
108         unsigned int  contents:2;
109         unsigned int  read_exec_only:1;
110         unsigned int  limit_in_pages:1;
111         unsigned int  seg_not_present:1;
112         unsigned int  useable:1;
113 };
114
115 #define MODIFY_LDT_CONTENTS_DATA        0
116 #define MODIFY_LDT_CONTENTS_STACK       1
117 #define MODIFY_LDT_CONTENTS_CODE        2
118 #endif
119
120
121 /* user level (privilege level: 3) ldt (1<<2) segment selector */
122 #define       LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
123
124 /* i got this value from wine sources, it's the first free LDT entry */
125 #if defined(__FreeBSD__) && defined(LDT_AUTO_ALLOC)
126 #define       TEB_SEL_IDX     LDT_AUTO_ALLOC
127 #endif
128
129 #ifndef       TEB_SEL_IDX
130 #define       TEB_SEL_IDX     17
131 #endif
132
133 static unsigned int fs_ldt = TEB_SEL_IDX;
134
135
136 /**
137  * here is a small logical problem with Restore for multithreaded programs -
138  * in C++ we use static class for this...
139  */
140
141 #ifdef __cplusplus
142 extern "C"
143 #endif
144 void Setup_FS_Segment(void)
145 {
146     unsigned int ldt_desc = LDT_SEL(fs_ldt);
147
148     __asm__ __volatile__(
149         "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc)
150         :"eax"
151     );
152 }
153
154 /* we don't need this - use modify_ldt instead */
155 #if 0
156 #ifdef __linux__
157 /* XXX: why is this routine from libc redefined here? */
158 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
159 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
160                        unsigned long count )
161 {
162     int res;
163 #ifdef __PIC__
164     __asm__ __volatile__( "pushl %%ebx\n\t"
165                           "movl %2,%%ebx\n\t"
166                           "int $0x80\n\t"
167                           "popl %%ebx"
168                           : "=a" (res)
169                           : "0" (__NR_modify_ldt),
170                           "r" (func),
171                           "c" (ptr),
172                           "d"(16)//sizeof(*ptr) from kernel point of view
173                           :"esi"     );
174 #else
175     __asm__ __volatile__("int $0x80"
176                          : "=a" (res)
177                          : "0" (__NR_modify_ldt),
178                          "b" (func),
179                          "c" (ptr),
180                          "d"(16)
181                          :"esi");
182 #endif  /* __PIC__ */
183     if (res >= 0) return res;
184     errno = -res;
185     return -1;
186 }
187 #endif
188 #endif
189
190 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
191 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
192 {
193     *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
194         (content->limit & 0x0ffff);
195     *buffer = (content->base_addr & 0xff000000) |
196         ((content->base_addr & 0x00ff0000)>>16) |
197         (content->limit & 0xf0000) |
198         (content->contents << 10) |
199         ((content->read_exec_only == 0) << 9) |
200         ((content->seg_32bit != 0) << 22) |
201         ((content->limit_in_pages != 0) << 23) |
202         0xf000;
203 }
204 #endif
205
206 void* fs_seg=0;
207
208 ldt_fs_t* Setup_LDT_Keeper(void)
209 {
210     struct modify_ldt_ldt_s array;
211     int ret;
212     ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t));
213
214     if (!ldt_fs)
215         return NULL;
216
217     ldt_fs->fd = open("/dev/zero", O_RDWR);
218     if(ldt_fs->fd<0){
219         perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
220         return NULL;
221     }
222     fs_seg=
223     ldt_fs->fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE,
224                           ldt_fs->fd, 0);
225     if (ldt_fs->fs_seg == (void*)-1)
226     {
227         perror("ERROR: Couldn't allocate memory for fs segment");
228         close(ldt_fs->fd);
229         free(ldt_fs);
230         return NULL;
231     }
232     *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg;
233     memset(&array, 0, sizeof(array));
234     array.base_addr=(int)ldt_fs->fs_seg;
235     array.entry_number=TEB_SEL_IDX;
236     array.limit=array.base_addr+getpagesize()-1;
237     array.seg_32bit=1;
238     array.read_exec_only=0;
239     array.seg_not_present=0;
240     array.contents=MODIFY_LDT_CONTENTS_DATA;
241     array.limit_in_pages=0;
242 #ifdef __linux__
243     //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
244     ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s));
245     if(ret<0)
246     {
247         perror("install_fs");
248         printf("Couldn't install fs segment, expect segfault\n");
249     }
250 #endif /*linux*/
251
252 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
253     {
254         unsigned long d[2];
255
256         LDT_EntryToBytes( d, &array );
257 #if defined(__FreeBSD__) && defined(LDT_AUTO_ALLOC)
258         ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1);
259         array.entry_number = ret;
260         fs_ldt = ret;
261 #else
262         ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
263 #endif
264         if (ret < 0)
265         {
266             perror("install_fs");
267             printf("Couldn't install fs segment, expect segfault\n");
268             printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
269         }
270     }
271 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ */
272
273 #if defined(__svr4__)
274     {
275         struct ssd ssd;
276         ssd.sel = LDT_SEL(TEB_SEL_IDX);
277         ssd.bo = array.base_addr;
278         ssd.ls = array.limit - array.base_addr;
279         ssd.acc1 = ((array.read_exec_only == 0) << 1) |
280             (array.contents << 2) |
281             0xf0;   /* P(resent) | DPL3 | S */
282         ssd.acc2 = 0x4;   /* byte limit, 32-bit segment */
283         if (sysi86(SI86DSCR, &ssd) < 0) {
284             perror("sysi86(SI86DSCR)");
285             printf("Couldn't install fs segment, expect segfault\n");
286         }
287     }
288 #endif
289
290     Setup_FS_Segment();
291
292     ldt_fs->prev_struct = (char*)malloc(sizeof(char) * 8);
293     *(void**)array.base_addr = ldt_fs->prev_struct;
294
295     return ldt_fs;
296 }
297
298 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs)
299 {
300     if (ldt_fs == NULL || ldt_fs->fs_seg == 0)
301         return;
302     free(ldt_fs->prev_struct);
303     munmap((char*)ldt_fs->fs_seg, getpagesize());
304     ldt_fs->fs_seg = 0;
305     close(ldt_fs->fd);
306     free(ldt_fs);
307 }