Mercurial > mplayer.hg
annotate loader/setup_FS.c @ 1677:1bf2c3dbc36e
Resolved endianess issues.
author | zybi |
---|---|
date | Fri, 24 Aug 2001 10:46:31 +0000 |
parents | d8c1b0b38edc |
children | 73c8f54305b1 |
rev | line source |
---|---|
236 | 1 #include <string.h> |
2 #include <stdlib.h> | |
3 #include <errno.h> | |
4 #include <fcntl.h> | |
5 #include <sys/mman.h> | |
6 #include <sys/types.h> | |
7 #include <stdio.h> | |
8 #include <unistd.h> | |
9 | |
10 #ifdef __linux__ | |
11 #include <asm/unistd.h> | |
12 #include <asm/ldt.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
13 #else /* !__linux__ */ |
236 | 14 |
15 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
16 #include <machine/sysarch.h> | |
17 #endif | |
18 | |
19 #ifdef __svr4__ | |
20 #include <sys/segment.h> | |
21 #include <sys/sysi86.h> | |
22 /* solaris x86: add missing prototype for sysi86() */ | |
23 extern int sysi86(int, void*); | |
24 #define TEB_SEL_IDX NUMSYSLDTS | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
25 #endif /* __svr4__ */ |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
26 |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
27 |
236 | 28 |
29 #define LDT_ENTRIES 8192 | |
30 #define LDT_ENTRY_SIZE 8 | |
31 #pragma pack(4) | |
32 struct modify_ldt_ldt_s { | |
33 unsigned int entry_number; | |
34 unsigned long base_addr; | |
35 unsigned int limit; | |
36 unsigned int seg_32bit:1; | |
37 unsigned int contents:2; | |
38 unsigned int read_exec_only:1; | |
39 unsigned int limit_in_pages:1; | |
40 unsigned int seg_not_present:1; | |
41 unsigned int useable:1; | |
42 }; | |
43 | |
44 #define MODIFY_LDT_CONTENTS_DATA 0 | |
45 #define MODIFY_LDT_CONTENTS_STACK 1 | |
46 #define MODIFY_LDT_CONTENTS_CODE 2 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
47 #endif /* !__linux__ */ |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
48 |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
49 #include "setup_FS.h" |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
50 |
236 | 51 |
52 | |
53 /* user level (privilege level: 3) ldt (1<<2) segment selector */ | |
54 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) | |
55 | |
56 #ifndef TEB_SEL_IDX | |
57 #define TEB_SEL_IDX 1 | |
58 #endif | |
59 | |
60 #define TEB_SEL LDT_SEL(TEB_SEL_IDX) | |
61 | |
62 void setup_FS_Segment() | |
63 { | |
340 | 64 #ifdef DETAILED_OUT |
236 | 65 printf("Setup FS...");fflush(stdout); |
340 | 66 #endif |
236 | 67 __asm__ __volatile__( |
68 "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL) | |
69 ); | |
340 | 70 #ifdef DETAILED_OUT |
236 | 71 printf("OK!\n"); |
340 | 72 #endif |
236 | 73 } |
74 | |
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 | |
83 static void* fs_seg=NULL; | |
84 | |
85 #ifdef __linux__ | |
86 /* XXX: why is this routine from libc redefined here? */ | |
87 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */ | |
88 static int modify_ldt( int func, struct modify_ldt_ldt_s *ptr, | |
89 unsigned long count ) | |
90 { | |
91 int res; | |
92 #ifdef __PIC__ | |
93 __asm__ __volatile__( "pushl %%ebx\n\t" | |
94 "movl %2,%%ebx\n\t" | |
95 "int $0x80\n\t" | |
96 "popl %%ebx" | |
97 : "=a" (res) | |
98 : "0" (__NR_modify_ldt), | |
99 "r" (func), | |
100 "c" (ptr), | |
101 "d"(16)//sizeof(*ptr) from kernel point of view | |
102 :"esi" ); | |
103 #else | |
104 __asm__ __volatile__("int $0x80" | |
105 : "=a" (res) | |
106 : "0" (__NR_modify_ldt), | |
107 "b" (func), | |
108 "c" (ptr), | |
109 "d"(16) | |
110 :"esi"); | |
111 #endif /* __PIC__ */ | |
112 if (res >= 0) return res; | |
113 errno = -res; | |
114 return -1; | |
115 } | |
116 #endif | |
117 | |
118 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
119 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) | |
120 { | |
121 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | | |
122 (content->limit & 0x0ffff); | |
123 *buffer = (content->base_addr & 0xff000000) | | |
124 ((content->base_addr & 0x00ff0000)>>16) | | |
125 (content->limit & 0xf0000) | | |
126 (content->contents << 10) | | |
127 ((content->read_exec_only == 0) << 9) | | |
128 ((content->seg_32bit != 0) << 22) | | |
129 ((content->limit_in_pages != 0) << 23) | | |
130 0xf000; | |
131 } | |
132 #endif | |
133 | |
134 void Setup_LDT_Keeper(){ | |
135 struct modify_ldt_ldt_s array; | |
136 int fd; | |
137 int ret; | |
138 void* prev_struct; | |
139 | |
140 if(fs_seg) return; // already set! | |
141 | |
142 fd=open("/dev/zero", O_RDWR); | |
143 fs_seg=mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, | |
144 fd, 0); | |
145 if(fs_seg==(void*)-1) | |
146 { | |
147 perror("ERROR: Couldn't allocate memory for fs segment"); | |
148 return; | |
149 } | |
150 array.base_addr=(int)fs_seg; | |
151 array.entry_number=TEB_SEL_IDX; | |
152 array.limit=array.base_addr+getpagesize()-1; | |
153 array.seg_32bit=1; | |
154 array.read_exec_only=0; | |
155 array.seg_not_present=0; | |
156 array.contents=MODIFY_LDT_CONTENTS_DATA; | |
157 array.limit_in_pages=0; | |
158 #ifdef __linux__ | |
159 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s)); | |
160 if(ret<0) | |
161 { | |
162 perror("install_fs"); | |
163 printf("Couldn't install fs segment, expect segfault\n"); | |
164 } | |
165 #endif /*linux*/ | |
166 | |
167 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
168 { | |
169 unsigned long d[2]; | |
170 | |
171 LDT_EntryToBytes( d, &array ); | |
172 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); | |
173 if (ret < 0) | |
174 { | |
175 perror("install_fs"); | |
176 printf("Couldn't install fs segment, expect segfault\n"); | |
177 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); | |
178 } | |
179 printf("Set_LDT\n"); | |
180 } | |
181 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */ | |
182 | |
183 #if defined(__svr4__) | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
340
diff
changeset
|
184 { |
236 | 185 struct ssd ssd; |
186 ssd.sel = TEB_SEL; | |
187 ssd.bo = array.base_addr; | |
188 ssd.ls = array.limit - array.base_addr; | |
189 ssd.acc1 = ((array.read_exec_only == 0) << 1) | | |
190 (array.contents << 2) | | |
191 0xf0; /* P(resent) | DPL3 | S */ | |
192 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */ | |
193 if (sysi86(SI86DSCR, &ssd) < 0) { | |
194 perror("sysi86(SI86DSCR)"); | |
195 printf("Couldn't install fs segment, expect segfault\n"); | |
196 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
340
diff
changeset
|
197 } |
236 | 198 #endif |
199 | |
200 setup_FS_Segment(); | |
201 | |
202 prev_struct=malloc(8); | |
203 *(void**)array.base_addr=prev_struct; | |
204 close(fd); | |
205 } | |
206 | |
207 void Restore_LDT_Keeper() | |
208 { | |
209 if(fs_seg==0) return; | |
210 munmap((char*)fs_seg, getpagesize()); | |
211 } | |
212 |