Mercurial > mplayer.hg
annotate loader/ldt_keeper.c @ 7618:555ef64dfe1c
sync, shorter messages in help screen
author | rtognimp |
---|---|
date | Sun, 06 Oct 2002 13:19:21 +0000 |
parents | 174e2a58b4cd |
children | 9d3e1476ecd0 |
rev | line source |
---|---|
2067 | 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! | |
7386 | 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 | |
2067 | 11 */ |
12 | |
7386 | 13 /* applied some modification to make make our xine friend more happy */ |
2139 | 14 #include "ldt_keeper.h" |
15 | |
2067 | 16 #include <string.h> |
17 #include <stdlib.h> | |
18 #include <errno.h> | |
19 #include <fcntl.h> | |
20 #include <sys/mman.h> | |
21 #include <sys/types.h> | |
22 #include <stdio.h> | |
23 #include <unistd.h> | |
24 #ifdef __linux__ | |
25 #include <asm/unistd.h> | |
26 #include <asm/ldt.h> | |
7386 | 27 /* prototype it here, so we won't depend on kernel headers */ |
28 #ifdef __cplusplus | |
29 extern "C" { | |
30 #endif | |
31 int modify_ldt(int func, void *ptr, unsigned long bytecount); | |
32 #ifdef __cplusplus | |
33 } | |
34 #endif | |
2067 | 35 #else |
36 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
5872 | 37 #include <machine/segments.h> |
2067 | 38 #include <machine/sysarch.h> |
39 #endif | |
40 | |
41 #ifdef __svr4__ | |
42 #include <sys/segment.h> | |
43 #include <sys/sysi86.h> | |
44 | |
45 /* solaris x86: add missing prototype for sysi86() */ | |
46 #ifdef __cplusplus | |
47 extern "C" { | |
48 #endif | |
7386 | 49 int sysi86(int, void*); |
2067 | 50 #ifdef __cplusplus |
51 } | |
52 #endif | |
53 | |
2139 | 54 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */ |
55 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */ | |
2067 | 56 #endif |
57 | |
58 #define TEB_SEL_IDX NUMSYSLDTS | |
59 #endif | |
60 | |
61 #define LDT_ENTRIES 8192 | |
62 #define LDT_ENTRY_SIZE 8 | |
63 #pragma pack(4) | |
64 struct modify_ldt_ldt_s { | |
65 unsigned int entry_number; | |
66 unsigned long base_addr; | |
67 unsigned int limit; | |
68 unsigned int seg_32bit:1; | |
69 unsigned int contents:2; | |
70 unsigned int read_exec_only:1; | |
71 unsigned int limit_in_pages:1; | |
72 unsigned int seg_not_present:1; | |
73 unsigned int useable:1; | |
74 }; | |
75 | |
76 #define MODIFY_LDT_CONTENTS_DATA 0 | |
77 #define MODIFY_LDT_CONTENTS_STACK 1 | |
78 #define MODIFY_LDT_CONTENTS_CODE 2 | |
79 #endif | |
80 | |
81 | |
82 /* user level (privilege level: 3) ldt (1<<2) segment selector */ | |
83 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) | |
84 | |
7386 | 85 /* i got this value from wine sources, it's the first free LDT entry */ |
2067 | 86 #ifndef TEB_SEL_IDX |
7386 | 87 #define TEB_SEL_IDX 17 |
2067 | 88 #endif |
7386 | 89 |
2067 | 90 #define TEB_SEL LDT_SEL(TEB_SEL_IDX) |
91 | |
92 /** | |
93 * here is a small logical problem with Restore for multithreaded programs - | |
94 * in C++ we use static class for this... | |
95 */ | |
96 | |
97 #ifdef __cplusplus | |
98 extern "C" | |
99 #endif | |
100 void Setup_FS_Segment(void) | |
101 { | |
102 __asm__ __volatile__( | |
103 "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL) | |
104 ); | |
105 } | |
106 | |
7386 | 107 /* we don't need this - use modify_ldt instead */ |
108 #if 0 | |
2067 | 109 #ifdef __linux__ |
110 /* XXX: why is this routine from libc redefined here? */ | |
111 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */ | |
112 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr, | |
113 unsigned long count ) | |
114 { | |
115 int res; | |
116 #ifdef __PIC__ | |
117 __asm__ __volatile__( "pushl %%ebx\n\t" | |
118 "movl %2,%%ebx\n\t" | |
119 "int $0x80\n\t" | |
120 "popl %%ebx" | |
121 : "=a" (res) | |
122 : "0" (__NR_modify_ldt), | |
123 "r" (func), | |
124 "c" (ptr), | |
125 "d"(16)//sizeof(*ptr) from kernel point of view | |
126 :"esi" ); | |
127 #else | |
128 __asm__ __volatile__("int $0x80" | |
129 : "=a" (res) | |
130 : "0" (__NR_modify_ldt), | |
131 "b" (func), | |
132 "c" (ptr), | |
133 "d"(16) | |
134 :"esi"); | |
135 #endif /* __PIC__ */ | |
136 if (res >= 0) return res; | |
137 errno = -res; | |
138 return -1; | |
139 } | |
140 #endif | |
7386 | 141 #endif |
2067 | 142 |
143 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
144 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) | |
145 { | |
146 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | | |
147 (content->limit & 0x0ffff); | |
148 *buffer = (content->base_addr & 0xff000000) | | |
149 ((content->base_addr & 0x00ff0000)>>16) | | |
150 (content->limit & 0xf0000) | | |
151 (content->contents << 10) | | |
152 ((content->read_exec_only == 0) << 9) | | |
153 ((content->seg_32bit != 0) << 22) | | |
154 ((content->limit_in_pages != 0) << 23) | | |
155 0xf000; | |
156 } | |
157 #endif | |
158 | |
7386 | 159 //void* fs_seg=0; |
160 | |
161 ldt_fs_t* Setup_LDT_Keeper(void) | |
2067 | 162 { |
163 struct modify_ldt_ldt_s array; | |
164 int ret; | |
7386 | 165 ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t)); |
2067 | 166 |
7386 | 167 if (!ldt_fs) |
168 return NULL; | |
2067 | 169 |
7386 | 170 ldt_fs->fd = open("/dev/zero", O_RDWR); |
171 if(ldt_fs->fd<0){ | |
172 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: "); | |
173 return NULL; | |
3775 | 174 } |
7386 | 175 // fs_seg= |
176 ldt_fs->fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, | |
177 ldt_fs->fd, 0); | |
178 if (ldt_fs->fs_seg == (void*)-1) | |
2067 | 179 { |
180 perror("ERROR: Couldn't allocate memory for fs segment"); | |
7386 | 181 close(ldt_fs->fd); |
182 free(ldt_fs); | |
183 return NULL; | |
2067 | 184 } |
7386 | 185 *(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg; |
186 array.base_addr=(int)ldt_fs->fs_seg; | |
2067 | 187 array.entry_number=TEB_SEL_IDX; |
188 array.limit=array.base_addr+getpagesize()-1; | |
189 array.seg_32bit=1; | |
190 array.read_exec_only=0; | |
191 array.seg_not_present=0; | |
192 array.contents=MODIFY_LDT_CONTENTS_DATA; | |
193 array.limit_in_pages=0; | |
194 #ifdef __linux__ | |
7386 | 195 //ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s)); |
196 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s)); | |
2067 | 197 if(ret<0) |
198 { | |
199 perror("install_fs"); | |
200 printf("Couldn't install fs segment, expect segfault\n"); | |
201 } | |
202 #endif /*linux*/ | |
203 | |
204 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
205 { | |
206 unsigned long d[2]; | |
207 | |
208 LDT_EntryToBytes( d, &array ); | |
209 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); | |
210 if (ret < 0) | |
211 { | |
212 perror("install_fs"); | |
213 printf("Couldn't install fs segment, expect segfault\n"); | |
214 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); | |
215 } | |
216 } | |
217 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */ | |
218 | |
219 #if defined(__svr4__) | |
2070
c1edbb8bfc0c
(solaris x86) C++ style variable declaration not at the start of a block does
jkeil
parents:
2069
diff
changeset
|
220 { |
2139 | 221 struct ssd ssd; |
222 ssd.sel = TEB_SEL; | |
223 ssd.bo = array.base_addr; | |
224 ssd.ls = array.limit - array.base_addr; | |
225 ssd.acc1 = ((array.read_exec_only == 0) << 1) | | |
226 (array.contents << 2) | | |
227 0xf0; /* P(resent) | DPL3 | S */ | |
228 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */ | |
229 if (sysi86(SI86DSCR, &ssd) < 0) { | |
230 perror("sysi86(SI86DSCR)"); | |
231 printf("Couldn't install fs segment, expect segfault\n"); | |
232 } | |
2070
c1edbb8bfc0c
(solaris x86) C++ style variable declaration not at the start of a block does
jkeil
parents:
2069
diff
changeset
|
233 } |
2067 | 234 #endif |
235 | |
236 Setup_FS_Segment(); | |
237 | |
7386 | 238 ldt_fs->prev_struct = (char*)malloc(sizeof(char) * 8); |
239 *(void**)array.base_addr = ldt_fs->prev_struct; | |
240 | |
241 return ldt_fs; | |
2067 | 242 } |
243 | |
7386 | 244 void Restore_LDT_Keeper(ldt_fs_t* ldt_fs) |
2067 | 245 { |
7386 | 246 if (ldt_fs == NULL || ldt_fs->fs_seg == 0) |
2067 | 247 return; |
7386 | 248 if (ldt_fs->prev_struct) |
249 free(ldt_fs->prev_struct); | |
250 munmap((char*)ldt_fs->fs_seg, getpagesize()); | |
251 ldt_fs->fs_seg = 0; | |
252 close(ldt_fs->fd); | |
253 free(ldt_fs); | |
2067 | 254 } |