4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * Originally distributed under LPGL 2.1 (or later) by the Wine project.
9 * Modified for use with MPlayer, detailed CVS changelog at
10 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
12 * File now distributed as part of VLC media player with no modifications.
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.
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.
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.
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
41 /* applied some modification to make make our xine friend more happy */
43 #include "ldt_keeper.h"
50 #include <sys/types.h>
54 #include <asm/unistd.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
61 /* prototype it here, so we won't depend on kernel headers */
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 );
69 int modify_ldt(int func, void *ptr, unsigned long bytecount);
75 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
76 #include <machine/segments.h>
77 #include <machine/sysarch.h>
81 #include <sys/segment.h>
82 #include <sys/sysi86.h>
84 /* solaris x86: add missing prototype for sysi86() */
88 int sysi86(int, void*);
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 */
97 #define TEB_SEL_IDX NUMSYSLDTS
100 #define LDT_ENTRIES 8192
101 #define LDT_ENTRY_SIZE 8
103 struct modify_ldt_ldt_s {
104 unsigned int entry_number;
105 unsigned long base_addr;
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;
115 #define MODIFY_LDT_CONTENTS_DATA 0
116 #define MODIFY_LDT_CONTENTS_STACK 1
117 #define MODIFY_LDT_CONTENTS_CODE 2
121 /* user level (privilege level: 3) ldt (1<<2) segment selector */
122 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
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
130 #define TEB_SEL_IDX 17
133 static unsigned int fs_ldt = TEB_SEL_IDX;
137 * here is a small logical problem with Restore for multithreaded programs -
138 * in C++ we use static class for this...
144 void Setup_FS_Segment(void)
146 unsigned int ldt_desc = LDT_SEL(fs_ldt);
148 __asm__ __volatile__(
149 "movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc)
154 /* we don't need this - use modify_ldt instead */
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 )
164 __asm__ __volatile__( "pushl %%ebx\n\t"
169 : "0" (__NR_modify_ldt),
172 "d"(16)//sizeof(*ptr) from kernel point of view
175 __asm__ __volatile__("int $0x80"
177 : "0" (__NR_modify_ldt),
183 if (res >= 0) return res;
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 )
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) |
208 ldt_fs_t* Setup_LDT_Keeper(void)
210 struct modify_ldt_ldt_s array;
212 ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t));
217 ldt_fs->fd = open("/dev/zero", O_RDWR);
219 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
224 ldt_fs->fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE,
226 if (ldt_fs->fs_seg == (void*)-1)
228 perror("ERROR: Couldn't allocate memory for fs segment");
233 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg;
234 memset(&array, 0, sizeof(array));
235 array.base_addr=(int)ldt_fs->fs_seg;
236 array.entry_number=TEB_SEL_IDX;
237 array.limit=array.base_addr+getpagesize()-1;
239 array.read_exec_only=0;
240 array.seg_not_present=0;
241 array.contents=MODIFY_LDT_CONTENTS_DATA;
242 array.limit_in_pages=0;
244 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
245 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s));
248 perror("install_fs");
249 printf("Couldn't install fs segment, expect segfault\n");
253 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
257 LDT_EntryToBytes( d, &array );
258 #if defined(__FreeBSD__) && defined(LDT_AUTO_ALLOC)
259 ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1);
260 array.entry_number = ret;
263 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
267 perror("install_fs");
268 printf("Couldn't install fs segment, expect segfault\n");
269 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
272 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ */
274 #if defined(__svr4__)
277 ssd.sel = LDT_SEL(TEB_SEL_IDX);
278 ssd.bo = array.base_addr;
279 ssd.ls = array.limit - array.base_addr;
280 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
281 (array.contents << 2) |
282 0xf0; /* P(resent) | DPL3 | S */
283 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
284 if (sysi86(SI86DSCR, &ssd) < 0) {
285 perror("sysi86(SI86DSCR)");
286 printf("Couldn't install fs segment, expect segfault\n");
293 ldt_fs->prev_struct = (char*)malloc(sizeof(char) * 8);
294 *(void**)array.base_addr = ldt_fs->prev_struct;
299 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs)
301 if (ldt_fs == NULL || ldt_fs->fs_seg == NULL)
303 free(ldt_fs->prev_struct);
304 munmap((char*)ldt_fs->fs_seg, getpagesize());