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