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