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