This source file includes following definitions.
- save_v86_state
- mark_screen_rdonly
- sys_vm86
- return_to_32bit
- set_IF
- clear_IF
- clear_TF
- set_vflags_long
- set_vflags_short
- get_vflags
- is_revectored
- do_int
- handle_vm86_debug
- handle_vm86_fault
1
2
3
4
5
6 #include <linux/errno.h>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/signal.h>
10 #include <linux/string.h>
11 #include <linux/ptrace.h>
12 #include <linux/mm.h>
13
14 #include <asm/segment.h>
15 #include <asm/pgtable.h>
16 #include <asm/io.h>
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #define AL(regs) (((unsigned char *)&((regs)->eax))[0])
37 #define AH(regs) (((unsigned char *)&((regs)->eax))[1])
38 #define IP(regs) (*(unsigned short *)&((regs)->eip))
39 #define SP(regs) (*(unsigned short *)&((regs)->esp))
40
41
42
43
44 #define VFLAGS (*(unsigned short *)&(current->tss.v86flags))
45 #define VEFLAGS (current->tss.v86flags)
46
47 #define set_flags(X,new,mask) \
48 ((X) = ((X) & ~(mask)) | ((new) & (mask)))
49
50 #define SAFE_MASK (0xDD5)
51 #define RETURN_MASK (0xDFF)
52
53 asmlinkage struct pt_regs * save_v86_state(struct vm86_regs * regs)
54 {
55 unsigned long tmp;
56
57 if (!current->tss.vm86_info) {
58 printk("no vm86_info: BAD\n");
59 do_exit(SIGSEGV);
60 }
61 set_flags(regs->eflags, VEFLAGS, VIF_MASK | current->tss.v86mask);
62 memcpy_tofs(¤t->tss.vm86_info->regs,regs,sizeof(*regs));
63 put_fs_long(current->tss.screen_bitmap,¤t->tss.vm86_info->screen_bitmap);
64 tmp = current->tss.esp0;
65 current->tss.esp0 = current->saved_kernel_stack;
66 current->saved_kernel_stack = 0;
67 return (struct pt_regs *) tmp;
68 }
69
70 static void mark_screen_rdonly(struct task_struct * tsk)
71 {
72 pgd_t *pgd;
73 pmd_t *pmd;
74 pte_t *pte;
75 int i;
76
77 pgd = pgd_offset(tsk, 0xA0000);
78 if (pgd_none(*pgd))
79 return;
80 if (pgd_bad(*pgd)) {
81 printk("vm86: bad pgd entry [%p]:%08lx\n", pgd, pgd_val(*pgd));
82 pgd_clear(pgd);
83 return;
84 }
85 pmd = pmd_offset(pgd, 0xA0000);
86 if (pmd_none(*pmd))
87 return;
88 if (pmd_bad(*pmd)) {
89 printk("vm86: bad pmd entry [%p]:%08lx\n", pmd, pmd_val(*pmd));
90 pmd_clear(pmd);
91 return;
92 }
93 pte = pte_offset(pmd, 0xA0000);
94 for (i = 0; i < 32; i++) {
95 if (pte_present(*pte))
96 *pte = pte_wrprotect(*pte);
97 pte++;
98 }
99 invalidate();
100 }
101
102 asmlinkage int sys_vm86(struct vm86_struct * v86)
103 {
104 struct vm86_struct info;
105 struct pt_regs * pt_regs = (struct pt_regs *) &v86;
106 int error;
107
108 if (current->saved_kernel_stack)
109 return -EPERM;
110
111 error = verify_area(VERIFY_WRITE,v86,sizeof(*v86));
112 if (error)
113 return error;
114 memcpy_fromfs(&info,v86,sizeof(info));
115
116
117
118 info.regs.__null_ds = 0;
119 info.regs.__null_es = 0;
120 info.regs.__null_fs = 0;
121 info.regs.__null_gs = 0;
122
123
124
125
126
127 VEFLAGS = info.regs.eflags;
128 info.regs.eflags &= SAFE_MASK;
129 info.regs.eflags |= pt_regs->eflags & ~SAFE_MASK;
130 info.regs.eflags |= VM_MASK;
131
132 switch (info.cpu_type) {
133 case CPU_286:
134 current->tss.v86mask = 0;
135 break;
136 case CPU_386:
137 current->tss.v86mask = NT_MASK | IOPL_MASK;
138 break;
139 case CPU_486:
140 current->tss.v86mask = AC_MASK | NT_MASK | IOPL_MASK;
141 break;
142 default:
143 current->tss.v86mask = ID_MASK | AC_MASK | NT_MASK | IOPL_MASK;
144 break;
145 }
146
147
148
149
150 pt_regs->eax = 0;
151 current->saved_kernel_stack = current->tss.esp0;
152 current->tss.esp0 = (unsigned long) pt_regs;
153 current->tss.vm86_info = v86;
154
155 current->tss.screen_bitmap = info.screen_bitmap;
156 if (info.flags & VM86_SCREEN_BITMAP)
157 mark_screen_rdonly(current);
158 __asm__ __volatile__("movl %0,%%esp\n\t"
159 "jmp ret_from_sys_call"
160 :
161 :"r" (&info.regs));
162 return 0;
163 }
164
165 static inline void return_to_32bit(struct vm86_regs * regs16, int retval)
166 {
167 struct pt_regs * regs32;
168
169 regs32 = save_v86_state(regs16);
170 regs32->eax = retval;
171 __asm__ __volatile__("movl %0,%%esp\n\t"
172 "jmp ret_from_sys_call"
173 : : "r" (regs32));
174 }
175
176 static inline void set_IF(struct vm86_regs * regs)
177 {
178 VEFLAGS |= VIF_MASK;
179 if (VEFLAGS & VIP_MASK)
180 return_to_32bit(regs, VM86_STI);
181 }
182
183 static inline void clear_IF(struct vm86_regs * regs)
184 {
185 VEFLAGS &= ~VIF_MASK;
186 }
187
188 static inline void clear_TF(struct vm86_regs * regs)
189 {
190 regs->eflags &= ~TF_MASK;
191 }
192
193 static inline void set_vflags_long(unsigned long eflags, struct vm86_regs * regs)
194 {
195 set_flags(VEFLAGS, eflags, current->tss.v86mask);
196 set_flags(regs->eflags, eflags, SAFE_MASK);
197 if (eflags & IF_MASK)
198 set_IF(regs);
199 }
200
201 static inline void set_vflags_short(unsigned short flags, struct vm86_regs * regs)
202 {
203 set_flags(VFLAGS, flags, current->tss.v86mask);
204 set_flags(regs->eflags, flags, SAFE_MASK);
205 if (flags & IF_MASK)
206 set_IF(regs);
207 }
208
209 static inline unsigned long get_vflags(struct vm86_regs * regs)
210 {
211 unsigned long flags = regs->eflags & RETURN_MASK;
212
213 if (VEFLAGS & VIF_MASK)
214 flags |= IF_MASK;
215 return flags | (VEFLAGS & current->tss.v86mask);
216 }
217
218 static inline int is_revectored(int nr, struct revectored_struct * bitmap)
219 {
220 __asm__ __volatile__("btl %2,%%fs:%1\n\tsbbl %0,%0"
221 :"=r" (nr)
222 :"m" (*bitmap),"r" (nr));
223 return nr;
224 }
225
226
227
228
229
230
231 #define pushb(base, ptr, val) \
232 __asm__ __volatile__( \
233 "decw %w0\n\t" \
234 "movb %2,%%fs:0(%1,%0)" \
235 : "=r" (ptr) \
236 : "r" (base), "q" (val), "0" (ptr))
237
238 #define pushw(base, ptr, val) \
239 __asm__ __volatile__( \
240 "decw %w0\n\t" \
241 "movb %h2,%%fs:0(%1,%0)\n\t" \
242 "decw %w0\n\t" \
243 "movb %b2,%%fs:0(%1,%0)" \
244 : "=r" (ptr) \
245 : "r" (base), "q" (val), "0" (ptr))
246
247 #define pushl(base, ptr, val) \
248 __asm__ __volatile__( \
249 "decw %w0\n\t" \
250 "rorl $16,%2\n\t" \
251 "movb %h2,%%fs:0(%1,%0)\n\t" \
252 "decw %w0\n\t" \
253 "movb %b2,%%fs:0(%1,%0)\n\t" \
254 "decw %w0\n\t" \
255 "rorl $16,%2\n\t" \
256 "movb %h2,%%fs:0(%1,%0)\n\t" \
257 "decw %w0\n\t" \
258 "movb %b2,%%fs:0(%1,%0)" \
259 : "=r" (ptr) \
260 : "r" (base), "q" (val), "0" (ptr))
261
262 #define popb(base, ptr) \
263 ({ unsigned long __res; \
264 __asm__ __volatile__( \
265 "movb %%fs:0(%1,%0),%b2\n\t" \
266 "incw %w0" \
267 : "=r" (ptr), "=r" (base), "=q" (__res) \
268 : "0" (ptr), "1" (base), "2" (0)); \
269 __res; })
270
271 #define popw(base, ptr) \
272 ({ unsigned long __res; \
273 __asm__ __volatile__( \
274 "movb %%fs:0(%1,%0),%b2\n\t" \
275 "incw %w0\n\t" \
276 "movb %%fs:0(%1,%0),%h2\n\t" \
277 "incw %w0" \
278 : "=r" (ptr), "=r" (base), "=q" (__res) \
279 : "0" (ptr), "1" (base), "2" (0)); \
280 __res; })
281
282 #define popl(base, ptr) \
283 ({ unsigned long __res; \
284 __asm__ __volatile__( \
285 "movb %%fs:0(%1,%0),%b2\n\t" \
286 "incw %w0\n\t" \
287 "movb %%fs:0(%1,%0),%h2\n\t" \
288 "incw %w0\n\t" \
289 "rorl $16,%2\n\t" \
290 "movb %%fs:0(%1,%0),%b2\n\t" \
291 "incw %w0\n\t" \
292 "movb %%fs:0(%1,%0),%h2\n\t" \
293 "incw %w0\n\t" \
294 "rorl $16,%2" \
295 : "=r" (ptr), "=r" (base), "=q" (__res) \
296 : "0" (ptr), "1" (base)); \
297 __res; })
298
299 static void do_int(struct vm86_regs *regs, int i, unsigned char * ssp, unsigned long sp)
300 {
301 unsigned short seg = get_fs_word((void *) ((i<<2)+2));
302
303 if (seg == BIOSSEG || regs->cs == BIOSSEG ||
304 is_revectored(i, ¤t->tss.vm86_info->int_revectored))
305 return_to_32bit(regs, VM86_INTx + (i << 8));
306 if (i==0x21 && is_revectored(AH(regs),¤t->tss.vm86_info->int21_revectored))
307 return_to_32bit(regs, VM86_INTx + (i << 8));
308 pushw(ssp, sp, get_vflags(regs));
309 pushw(ssp, sp, regs->cs);
310 pushw(ssp, sp, IP(regs));
311 regs->cs = seg;
312 SP(regs) -= 6;
313 IP(regs) = get_fs_word((void *) (i<<2));
314 clear_TF(regs);
315 clear_IF(regs);
316 return;
317 }
318
319 void handle_vm86_debug(struct vm86_regs * regs, long error_code)
320 {
321 #if 0
322 do_int(regs, 1, (unsigned char *) (regs->ss << 4), SP(regs));
323 #else
324 if (current->flags & PF_PTRACED)
325 current->blocked &= ~(1 << (SIGTRAP-1));
326 send_sig(SIGTRAP, current, 1);
327 current->tss.trap_no = 1;
328 current->tss.error_code = error_code;
329 #endif
330 }
331
332 void handle_vm86_fault(struct vm86_regs * regs, long error_code)
333 {
334 unsigned char *csp, *ssp;
335 unsigned long ip, sp;
336
337 csp = (unsigned char *) (regs->cs << 4);
338 ssp = (unsigned char *) (regs->ss << 4);
339 sp = SP(regs);
340 ip = IP(regs);
341
342 switch (popb(csp, ip)) {
343
344
345 case 0x66:
346 switch (popb(csp, ip)) {
347
348
349 case 0x9c:
350 SP(regs) -= 4;
351 IP(regs) += 2;
352 pushl(ssp, sp, get_vflags(regs));
353 return;
354
355
356 case 0x9d:
357 SP(regs) += 4;
358 IP(regs) += 2;
359 set_vflags_long(popl(ssp, sp), regs);
360 return;
361 }
362
363
364 case 0x9c:
365 SP(regs) -= 2;
366 IP(regs)++;
367 pushw(ssp, sp, get_vflags(regs));
368 return;
369
370
371 case 0x9d:
372 SP(regs) += 2;
373 IP(regs)++;
374 set_vflags_short(popw(ssp, sp), regs);
375 return;
376
377
378 case 0xcd:
379 IP(regs) += 2;
380 do_int(regs, popb(csp, ip), ssp, sp);
381 return;
382
383
384 case 0xcf:
385 SP(regs) += 6;
386 IP(regs) = popw(ssp, sp);
387 regs->cs = popw(ssp, sp);
388 set_vflags_short(popw(ssp, sp), regs);
389 return;
390
391
392 case 0xfa:
393 IP(regs)++;
394 clear_IF(regs);
395 return;
396
397
398
399
400
401
402
403
404 case 0xfb:
405 IP(regs)++;
406 set_IF(regs);
407 return;
408
409 default:
410 return_to_32bit(regs, VM86_UNKNOWN);
411 }
412 }