Subversion Repositories Kolibri OS

Rev

Rev 960 | Rev 979 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
709 diamond 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2007-2008. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
750 victor 8
$Revision: 971 $
9
 
709 diamond 10
; Virtual-8086 mode manager
11
; diamond, 2007, 2008
12
 
13
DEBUG_SHOW_IO = 0
14
 
15
struc V86_machine
16
{
17
; page directory
18
        .pagedir dd     ?
19
; translation table: V86 address -> flat linear address
20
        .pages  dd      ?
21
; mutex to protect all data from writing by multiple threads at one time
22
        .mutex  dd      ?
23
; i/o permission map
24
        .iopm   dd      ?
25
.size = $
26
}
27
virtual at 0
28
V86_machine V86_machine
29
end virtual
30
 
31
; Create V86 machine
32
; in: nothing
33
; out: eax = handle (pointer to struc V86_machine)
34
;      eax = NULL => failure
35
; destroys: ebx, ecx, edx (due to malloc)
36
v86_create:
37
; allocate V86_machine structure
38
        mov     eax, V86_machine.size
39
        call    malloc
40
        test    eax, eax
41
        jz      .fail
42
; initialize mutex
43
        and     dword [eax+V86_machine.mutex], 0
44
; allocate tables
45
        mov     ebx, eax
46
; We allocate 4 pages.
47
; First is main page directory for V86 mode.
48
; Second page:
49
; first half (0x800 bytes) is page table for addresses 0 - 0x100000,
50
; second half is for V86-to-linear translation.
51
; Third and fourth are for I/O permission map.
724 diamond 52
        push    8000h           ; blocks less than 8 pages are discontinuous
709 diamond 53
        call    kernel_alloc
54
        test    eax, eax
55
        jz      .fail2
56
        mov     [ebx+V86_machine.pagedir], eax
57
        push    edi eax
58
        mov     edi, eax
59
        add     eax, 1800h
60
        mov     [ebx+V86_machine.pages], eax
61
; initialize tables
62
        mov     ecx, 2000h/4
63
        xor     eax, eax
64
        rep     stosd
65
        mov     [ebx+V86_machine.iopm], edi
66
        dec     eax
67
        mov     ecx, 2000h/4
68
        rep     stosd
69
        pop     eax
70
; page directory: first entry is page table...
71
        mov     edi, eax
72
        add     eax, 1000h
73
        push    eax
74
        call    get_pg_addr
75
        or      al, PG_UW
76
        stosd
77
; ...and also copy system page tables
78
; thx to Serge, system is located at high addresses
79
        add     edi, (OS_BASE shr 20) - 4
80
        push    esi
81
        mov     esi, (OS_BASE shr 20) + sys_pgdir
82
        mov     ecx, 0x80000000 shr 22
83
        rep     movsd
712 serge 84
 
85
        mov     eax, [ebx+V86_machine.pagedir]   ;root dir also is
86
        call    get_pg_addr                      ;used as page table
87
        or      al, PG_SW
88
        mov [edi-4096+(page_tabs shr 20)], eax
89
 
709 diamond 90
        pop     esi
91
; now V86 specific: initialize known addresses in first Mb
820 diamond 92
        pop     eax
709 diamond 93
; first page - BIOS data (shared between all machines!)
94
; physical address = 0x2f0000
95
; linear address = BOOT_VAR = OS_BASE + 0x2f0000
96
        mov     dword [eax], (BOOT_VAR - OS_BASE) or 111b
97
        mov     dword [eax+800h], BOOT_VAR
98
; page before 0xA0000 - Extended BIOS Data Area (shared between all machines!)
820 diamond 99
; physical address = 0x9C000
100
; linear address = 0x8009C000
101
; (I have seen one computer with EBDA segment = 0x9D80,
102
; all other computers use less memory)
103
        mov     ecx, 4
104
        mov     edx, 0x9C000
105
        push    eax
106
        lea     edi, [eax+0x9C*4]
107
@@:
108
        lea     eax, [edx + OS_BASE]
109
        mov     [edi+800h], eax
110
        lea     eax, [edx + 111b]
111
        stosd
891 diamond 112
        add     edx, 0x1000
820 diamond 113
        loop    @b
114
        pop     eax
115
        pop     edi
709 diamond 116
; addresses 0xC0000 - 0xFFFFF - BIOS code (shared between all machines!)
117
; physical address = 0xC0000
118
; linear address = 0x800C0000
119
        mov     ecx, 0xC0
120
@@:
121
        mov     edx, ecx
122
        shl     edx, 12
123
        push    edx
124
        or      edx, 101b
125
        mov     [eax+ecx*4], edx
126
        pop     edx
127
        add     edx, OS_BASE
128
        mov     [eax+ecx*4+0x800], edx
129
        inc     cl
130
        jnz     @b
131
        mov     eax, ebx
132
        ret
133
.fail2:
134
        mov     eax, ebx
135
        call    free
136
.fail:
137
        xor     eax, eax
138
        ret
139
 
140
; Destroy V86 machine
141
; in: eax = handle
142
; out: nothing
143
; destroys: eax, ebx, ecx, edx (due to free)
144
v86_destroy:
145
        push    eax
146
        stdcall kernel_free, [eax+V86_machine.pagedir]
147
        pop     eax
148
        jmp     free
149
 
150
; Translate V86-address to linear address
151
; in: eax=V86 address
152
;     esi=handle
153
; out: eax=linear address
154
; destroys: nothing
155
v86_get_lin_addr:
156
        push    ecx edx
157
        mov     ecx, eax
158
        mov     edx, [esi+V86_machine.pages]
159
        shr     ecx, 12
160
        and     eax, 0xFFF
161
        add     eax, [edx+ecx*4]        ; atomic operation, no mutex needed
162
        pop     edx ecx
163
        ret
164
 
165
; Sets linear address for V86-page
166
; in: eax=linear address (must be page-aligned)
167
;     ecx=V86 page (NOT address!)
168
;     esi=handle
169
; out: nothing
170
; destroys: nothing
171
v86_set_page:
172
        push    eax ebx
173
        mov     ebx, [esi+V86_machine.pagedir]
174
        mov     [ebx+ecx*4+0x1800], eax
175
        call    get_pg_addr
176
        or      al, 111b
177
        mov     [ebx+ecx*4+0x1000], eax
178
        pop     ebx eax
179
        ret
180
 
181
; Allocate memory in V86 machine
182
; in: eax=size (in bytes)
183
;     esi=handle
184
; out: eax=V86 address, para-aligned (0x10 multiple)
185
; destroys: nothing
724 diamond 186
; недописана!!!
709 diamond 187
;v86_alloc:
188
;        push    ebx ecx edx edi
189
;        lea     ebx, [esi+V86_machine.mutex]
190
;        call    wait_mutex
191
;        add     eax, 0x1F
192
;        shr     eax, 4
193
;        mov     ebx, 0x1000  ; start with address 0x1000 (second page)
194
;        mov     edi, [esi+V86_machine.tables]
195
;.l:
196
;        mov     ecx, ebx
197
;        shr     ecx, 12
198
;        mov     edx, [edi+0x1000+ecx*4] ; get linear address
199
;        test    edx, edx                ; page allocated?
200
;        jz      .unalloc
201
;        mov     ecx, ebx
202
;        and     ecx, 0xFFF
203
;        add     edx, ecx
204
;        cmp     dword [edx], 0          ; free block?
205
;        jnz     .n
724 diamond 206
;        cmp     dword [edx+4],
709 diamond 207
;        and     [esi+V86_machine.mutex], 0
208
;        pop     edi edx ecx ebx
209
;        ret
210
 
211
uglobal
212
sys_v86_machine dd      ?
213
endg
214
 
215
; Called from kernel.asm at first stages of loading
216
; Initialize system V86 machine (used to simulate BIOS int 13h)
217
init_sys_v86:
218
        call    v86_create
219
        mov     [sys_v86_machine], eax
220
        test    eax, eax
221
        jz      .ret
222
        mov     byte [BOOT_VAR + 0x500], 0xCD
223
        mov     byte [BOOT_VAR + 0x501], 0x13
224
        mov     byte [BOOT_VAR + 0x502], 0xF4
225
        mov     byte [BOOT_VAR + 0x503], 0xCD
226
        mov     byte [BOOT_VAR + 0x504], 0x10
227
        mov     byte [BOOT_VAR + 0x505], 0xF4
228
        mov     esi, eax
229
        mov     ebx, [eax+V86_machine.pagedir]
820 diamond 230
; one page for stack, two pages for results (0x2000 bytes = 16 sectors)
231
        mov     dword [ebx+0x99*4+0x1000], 0x99000 or 111b
232
        mov     dword [ebx+0x99*4+0x1800], OS_BASE + 0x99000
233
        mov     dword [ebx+0x9A*4+0x1000], 0x9A000 or 111b
234
        mov     dword [ebx+0x9A*4+0x1800], OS_BASE + 0x9A000
770 diamond 235
        mov     dword [ebx+0x9B*4+0x1000], 0x9B000 or 111b
236
        mov     dword [ebx+0x9B*4+0x1800], OS_BASE + 0x9B000
709 diamond 237
if ~DEBUG_SHOW_IO
238
; allow access to all ports
239
        mov     ecx, [esi+V86_machine.iopm]
240
        xor     eax, eax
241
        mov     edi, ecx
242
        mov     ecx, 10000h/8/4
243
        rep     stosd
244
end if
245
.ret:
246
        ret
247
 
248
struc v86_regs
249
{
250
; don't change the order, it is important
251
        .edi    dd      ?
252
        .esi    dd      ?
253
        .ebp    dd      ?
254
                dd      ?       ; ignored
255
        .ebx    dd      ?
256
        .edx    dd      ?
257
        .ecx    dd      ?
258
        .eax    dd      ?
259
        .eip    dd      ?
260
        .cs     dd      ?
261
        .eflags dd      ?       ; VM flag must be set!
262
        .esp    dd      ?
263
        .ss     dd      ?
264
        .es     dd      ?
265
        .ds     dd      ?
266
        .fs     dd      ?
267
        .gs     dd      ?
268
.size = $
269
}
270
virtual at 0
271
v86_regs v86_regs
272
end virtual
273
 
274
; Run V86 machine
275
; in: ebx -> registers for V86 (two structures: in and out)
276
;     esi = handle
277
;     ecx = expected end address (CS:IP)
278
;     edx = IRQ to hook or -1 if not required
279
; out: structure pointed to by ebx is filled with new values
280
;     eax = 1 - exception has occured, cl contains code
281
;     eax = 2 - access to disabled i/o port, ecx contains port address
282
;     eax = 3 - IRQ is already hooked by another VM
283
; destroys: nothing
284
v86_start:
285
        pushad
286
 
287
        cli
288
 
289
        mov     ecx, [CURRENT_TASK]
290
        shl     ecx, 8
291
        add     ecx, SLOT_BASE
292
 
293
        mov     eax, [esi+V86_machine.iopm]
294
        call    get_pg_addr
295
        inc     eax
296
        push    dword [ecx+APPDATA.io_map]
297
        push    dword [ecx+APPDATA.io_map+4]
298
        mov     dword [ecx+APPDATA.io_map], eax
299
        mov     dword [page_tabs + (tss._io_map_0 shr 10)], eax
300
        add     eax, 0x1000
301
        mov     dword [ecx+APPDATA.io_map+4], eax
302
        mov     dword [page_tabs + (tss._io_map_1 shr 10)], eax
303
 
304
        push    [ecx+APPDATA.dir_table]
305
        push    [ecx+APPDATA.saved_esp0]
306
        mov     [ecx+APPDATA.saved_esp0], esp
307
        mov     [tss._esp0], esp
308
 
309
        mov     eax, [esi+V86_machine.pagedir]
310
        call    get_pg_addr
311
        mov     [ecx+APPDATA.dir_table], eax
312
        mov     cr3, eax
313
 
314
;        mov     [irq_tab+5*4], my05
315
 
316
; We do not enable interrupts, because V86 IRQ redirector assumes that
317
; machine is running
318
; They will be enabled by IRET.
319
;        sti
320
 
321
        mov     eax, esi
322
        sub     esp, v86_regs.size
323
        mov     esi, ebx
324
        mov     edi, esp
325
        mov     ecx, v86_regs.size/4
326
        rep     movsd
327
 
328
        cmp     edx, -1
329
        jz      .noirqhook
330
uglobal
331
v86_irqhooks    rd      16*2
332
endg
333
        cmp     [v86_irqhooks+edx*8], 0
334
        jz      @f
335
        cmp     [v86_irqhooks+edx*8], eax
336
        jz      @f
337
        mov     esi, v86_irqerr
338
        call    sys_msg_board_str
339
        inc     [v86_irqhooks+edx*8+4]
340
        mov     eax, 3
341
        jmp     v86_exc_c.exit
342
@@:
343
        mov     [v86_irqhooks+edx*8], eax
344
        inc     [v86_irqhooks+edx*8+4]
345
.noirqhook:
346
 
347
        popad
348
        iretd
349
 
350
; It is only possible to leave virtual-8086 mode by faulting to
351
; a protected-mode interrupt handler (typically the general-protection
352
; exception handler, which in turn calls the virtual 8086-mode monitor).
353
 
354
v86_debug_exc:
355
        pushad
356
        xor     eax, eax
357
        mov     dr6, eax
358
        mov     bl, 1
359
        jmp     v86_exc_c
360
 
361
v86_page_fault:
362
        add     esp, 4
363
        pushad
364
        mov     bl, 14
365
        jmp     v86_exc_c
366
 
367
v86_except_16:
368
        pushad
369
        mov     bl, 16
370
        jmp     v86_exc_c
371
v86_except_19:
372
        pushad
373
        mov     bl, 19
374
 
375
iglobal
376
v86_exc_str1    db      'V86 : unexpected exception ',0
377
v86_exc_str2    db      ' at ',0
378
v86_exc_str3    db      ':',0
379
v86_exc_str4    db      13,10,'V86 : faulted code:',0
380
v86_exc_str5    db      ' (unavailable)',0
381
v86_newline     db      13,10,0
382
v86_io_str1     db      'V86 : access to disabled i/o port ',0
383
v86_io_byte     db      ' (byte)',13,10,0
384
v86_io_word     db      ' (word)',13,10,0
385
v86_io_dword    db      ' (dword)',13,10,0
386
v86_irqerr      db      'V86 : IRQ already hooked',13,10,0
387
endg
388
 
389
v86_exc_c:
390
        mov     ax, app_data
391
        mov     ds, ax
392
        mov     es, ax
393
; Did we all that we have wanted to do?
394
        mov     eax, [esp+v86_regs.size+10h+18h]
395
        cmp     word [esp+v86_regs.eip], ax
396
        jnz     @f
397
        shr     eax, 16
398
        cmp     word [esp+v86_regs.cs], ax
399
        jz      .done
400
@@:
401
; Various system events, which must be handled, result in #GP
402
        cmp     bl, 13
403
        jnz     .nogp
404
; If faulted EIP exceeds 0xFFFF, we have #GP and it is an error
405
        cmp     word [esp+v86_regs.eip+2], 0
406
        jnz     .nogp
407
; Otherwise we can safely access byte at CS:IP
408
; (because it is #GP, not #PF handler)
724 diamond 409
; Если бы мы могли схлопотать исключение только из-за чтения байтов кода,
410
; мы бы его уже схлопотали и это было бы не #GP
709 diamond 411
        movzx   esi, word [esp+v86_regs.cs]
412
        shl     esi, 4
413
        add     esi, [esp+v86_regs.eip]
414
        lodsb
415
        cmp     al, 0xCD        ; int xx command = CD xx
416
        jz      .handle_int
417
        cmp     al, 0xCF
418
        jz      .handle_iret
419
        cmp     al, 0xF3
420
        jz      .handle_rep
421
        cmp     al, 0xEC
422
        jz      .handle_in
423
        cmp     al, 0xED
424
        jz      .handle_in_word
425
        cmp     al, 0xEE
426
        jz      .handle_out
427
        cmp     al, 0xEF
428
        jz      .handle_out_word
429
        cmp     al, 0xE4
430
        jz      .handle_in_imm
431
        cmp     al, 0xE6
432
        jz      .handle_out_imm
433
        cmp     al, 0x9C
434
        jz      .handle_pushf
435
        cmp     al, 0x9D
436
        jz      .handle_popf
437
        cmp     al, 0xFA
438
        jz      .handle_cli
439
        cmp     al, 0xFB
440
        jz      .handle_sti
441
        cmp     al, 0x66
442
        jz      .handle_66
443
        jmp     .nogp
444
.handle_int:
445
        cmp     word [esp+v86_regs.eip], 0xFFFF
446
        jae     .nogp
447
        xor     eax, eax
448
        lodsb
449
;        call    sys_msg_board_byte
450
; simulate INT command
451
; N.B. It is possible that some checks need to be corrected,
452
;      but at least in case of normal execution the code works.
453
.simulate_int:
454
        cmp     word [esp+v86_regs.esp], 6
455
        jae     @f
456
        mov     bl, 12          ; #SS exception
457
        jmp     .nogp
458
@@:
459
        movzx   edx, word [esp+v86_regs.ss]
460
        shl     edx, 4
461
        push    eax
462
        movzx   eax, word [esp+4+v86_regs.esp]
463
        sub     eax, 6
464
        add     edx, eax
465
        mov     eax, edx
466
        mov     esi, [esp+4+v86_regs.size+10h+4]
467
        call    v86_get_lin_addr
468
        cmp     eax, 0x1000
469
        jae     @f
470
        mov     bl, 14          ; #PF exception
471
        jmp     .nogp
472
@@:
473
        lea     eax, [edx+5]
474
        call    v86_get_lin_addr
475
        cmp     eax, 0x1000
476
        jae     @f
477
        mov     bl, 14          ; #PF exception
478
        jmp     .nogp
479
@@:
480
        sub     word [esp+4+v86_regs.esp], 6
481
        mov     eax, [esp+4+v86_regs.eip]
971 diamond 482
        cmp     byte [esp+1], 0
483
        jnz     @f
709 diamond 484
        inc     eax
485
        inc     eax
971 diamond 486
@@:
709 diamond 487
        mov     word [edx], ax
488
        mov     eax, [esp+4+v86_regs.cs]
489
        mov     word [edx+2], ax
490
        mov     eax, [esp+4+v86_regs.eflags]
491
        mov     word [edx+4], ax
492
        pop     eax
971 diamond 493
        mov     ah, 0
709 diamond 494
        mov     cx, [eax*4]
495
        mov     word [esp+v86_regs.eip], cx
496
        mov     cx, [eax*4+2]
497
        mov     word [esp+v86_regs.cs], cx
498
; note that interrupts will be disabled globally at IRET
499
        and     byte [esp+v86_regs.eflags+1], not 3 ; clear IF and TF flags
500
; continue V86 execution
501
        popad
502
        iretd
503
.handle_iret:
504
        cmp     word [esp+v86_regs.esp], 0x10000 - 6
505
        jbe     @f
506
        mov     bl, 12
507
        jmp     .nogp
508
@@:
509
        movzx   edx, word [esp+v86_regs.ss]
510
        shl     edx, 4
511
        movzx   eax, word [esp+v86_regs.esp]
512
        add     edx, eax
513
        mov     eax, edx
514
        mov     esi, [esp+v86_regs.size+10h+4]
515
        call    v86_get_lin_addr
516
        cmp     eax, 0x1000
517
        jae     @f
518
        mov     bl, 14
519
        jmp     .nogp
520
@@:
521
        lea     eax, [edx+5]
522
        call    v86_get_lin_addr
523
        cmp     eax, 0x1000
524
        jae     @f
525
        mov     bl, 14
526
        jmp     .nogp
527
@@:
528
        mov     ax, [edx]
529
        mov     word [esp+v86_regs.eip], ax
530
        mov     ax, [edx+2]
531
        mov     word [esp+v86_regs.cs], ax
532
        mov     ax, [edx+4]
533
        mov     word [esp+v86_regs.eflags], ax
534
        add     word [esp+v86_regs.esp], 6
535
        popad
536
        iretd
537
.handle_pushf:
538
        cmp     word [esp+v86_regs.esp], 1
539
        jnz     @f
540
        mov     bl, 12
541
        jmp     .nogp
542
@@:
543
        movzx   edx, word [esp+v86_regs.ss]
544
        shl     edx, 4
545
        mov     eax, [esp+v86_regs.esp]
546
        sub     eax, 2
547
        movzx   eax, ax
548
        add     edx, eax
549
        mov     eax, edx
550
        mov     esi, [esp+v86_regs.size+10h+4]
551
        call    v86_get_lin_addr
552
        cmp     eax, 0x1000
553
        jae     @f
554
        mov     bl, 14          ; #PF exception
555
        jmp     .nogp
556
@@:
557
        lea     eax, [edx+1]
558
        call    v86_get_lin_addr
559
        cmp     eax, 0x1000
560
        jae     @f
561
        mov     bl, 14
562
        jmp     .nogp
563
@@:
564
        sub     word [esp+v86_regs.esp], 2
565
        mov     eax, [esp+v86_regs.eflags]
566
        mov     [edx], ax
567
        inc     word [esp+v86_regs.eip]
568
        popad
569
        iretd
770 diamond 570
.handle_pushfd:
571
        cmp     word [esp+v86_regs.esp], 4
572
        jae     @f
573
        mov     bl, 12          ; #SS exception
574
        jmp     .nogp
575
@@:
576
        movzx   edx, word [esp+v86_regs.ss]
577
        shl     edx, 4
578
        movzx   eax, word [esp+v86_regs.esp]
579
        sub     eax, 4
580
        add     edx, eax
581
        mov     eax, edx
582
        mov     esi, [esp+v86_regs.size+10h+4]
583
        call    v86_get_lin_addr
584
        cmp     eax, 0x1000
585
        jae     @f
586
        mov     bl, 14          ; #PF exception
587
        jmp     .nogp
588
@@:
589
        lea     eax, [edx+3]
590
        call    v86_get_lin_addr
591
        cmp     eax, 0x1000
592
        jae     @f
593
        mov     bl, 14          ; #PF exception
594
        jmp     .nogp
595
@@:
596
        sub     word [esp+v86_regs.esp], 4
597
        movzx   eax, word [esp+v86_regs.eflags]
598
        mov     [edx], eax
599
        add     word [esp+v86_regs.eip], 2
600
        popad
601
        iretd
709 diamond 602
.handle_popf:
603
        cmp     word [esp+v86_regs.esp], 0xFFFF
604
        jnz     @f
605
        mov     bl, 12
606
        jmp     .nogp
607
@@:
608
        movzx   edx, word [esp+v86_regs.ss]
609
        shl     edx, 4
610
        movzx   eax, word [esp+v86_regs.esp]
611
        add     edx, eax
612
        mov     eax, edx
613
        mov     esi, [esp+v86_regs.size+10h+4]
614
        call    v86_get_lin_addr
615
        cmp     eax, 0x1000
616
        jae     @f
617
        mov     bl, 14          ; #PF exception
618
        jmp     .nogp
619
@@:
620
        lea     eax, [edx+1]
621
        call    v86_get_lin_addr
622
        cmp     eax, 0x1000
623
        jae     @f
624
        mov     bl, 14
625
        jmp     .nogp
626
@@:
627
        mov     ax, [edx]
628
        mov     word [esp+v86_regs.eflags], ax
629
        add     word [esp+v86_regs.esp], 2
630
        inc     word [esp+v86_regs.eip]
631
        popad
632
        iretd
770 diamond 633
.handle_popfd:
634
        cmp     word [esp+v86_regs.esp], 0x10000 - 4
635
        jbe     @f
636
        mov     bl, 12
637
        jmp     .nogp
638
@@:
639
        movzx   edx, word [esp+v86_regs.ss]
640
        shl     edx, 4
641
        movzx   eax, word [esp+v86_regs.esp]
642
        add     edx, eax
643
        mov     eax, edx
644
        mov     esi, [esp+v86_regs.size+10h+4]
645
        call    v86_get_lin_addr
646
        cmp     eax, 0x1000
647
        jae     @f
648
        mov     bl, 14
649
        jmp     .nogp
650
@@:
651
        lea     eax, [edx+3]
652
        call    v86_get_lin_addr
653
        cmp     eax, 0x1000
654
        jae     @f
655
        mov     bl, 14
656
        jmp     .nogp
657
@@:
658
        mov     eax, [edx]
659
        mov     word [esp+v86_regs.eflags], ax
660
        add     word [esp+v86_regs.esp], 4
661
        add     word [esp+v86_regs.eip], 2
662
        popad
663
        iretd
709 diamond 664
.handle_cli:
665
        and     byte [esp+v86_regs.eflags+1], not 2
666
        inc     word [esp+v86_regs.eip]
667
        popad
668
        iretd
669
.handle_sti:
670
        or      byte [esp+v86_regs.eflags+1], 2
671
        inc     word [esp+v86_regs.eip]
672
        popad
673
        iretd
674
.handle_rep:
675
        cmp     word [esp+v86_regs.eip], 0xFFFF
676
        jae     .nogp
677
        lodsb
678
        cmp     al, 6Eh
679
        jz      .handle_rep_outsb
680
        jmp     .nogp
681
.handle_rep_outsb:
682
.handle_in:
683
.handle_out:
684
.invalid_io_byte:
685
        movzx   ebx, word [esp+v86_regs.edx]
686
        mov     ecx, 1
687
        jmp     .invalid_io
688
.handle_in_imm:
689
.handle_out_imm:
690
        cmp     word [esp+v86_regs.eip], 0xFFFF
691
        jae     .nogp
692
        lodsb
693
        movzx   ebx, al
694
        mov     ecx, 1
695
        jmp     .invalid_io
696
.handle_66:
697
        cmp     word [esp+v86_regs.eip], 0xFFFF
698
        jae     .nogp
699
        lodsb
770 diamond 700
        cmp     al, 0x9C
701
        jz      .handle_pushfd
702
        cmp     al, 0x9D
703
        jz      .handle_popfd
709 diamond 704
        cmp     al, 0xEF
705
        jz      .handle_out_dword
706
        cmp     al, 0xED
707
        jz      .handle_in_dword
708
        jmp     .nogp
709
.handle_in_word:
710
.handle_out_word:
711
        movzx   ebx, word [esp+v86_regs.edx]
712
        mov     ecx, 2
713
        jmp     .invalid_io
714
.handle_in_dword:
715
.handle_out_dword:
716
.invalid_io_dword:
717
        movzx   ebx, word [esp+v86_regs.edx]
718
        mov     ecx, 4
719
.invalid_io:
720
        mov     esi, v86_io_str1
721
        call    sys_msg_board_str
722
        mov     eax, ebx
723
        call    sys_msg_board_dword
724
        mov     esi, v86_io_byte
725
        cmp     ecx, 1
726
        jz      @f
727
        mov     esi, v86_io_word
728
        cmp     ecx, 2
729
        jz      @f
730
        mov     esi, v86_io_dword
731
@@:
732
        call    sys_msg_board_str
733
if DEBUG_SHOW_IO
734
        mov     edx, ebx
735
        mov     ebx, 200
736
        call    delay_hs
737
        mov     esi, [esp+v86_regs.size+10h+4]
738
        mov     eax, [esi+V86_machine.iopm]
739
@@:
740
        btr     [eax], edx
741
        inc     edx
742
        loop    @b
743
        popad
744
        iretd
745
else
746
        mov     eax, 2
747
        jmp     .exit
748
end if
749
.nogp:
750
 
751
        mov     esi, v86_exc_str1
752
        call    sys_msg_board_str
753
        mov     al, bl
754
        call    sys_msg_board_byte
755
        mov     esi, v86_exc_str2
756
        call    sys_msg_board_str
757
        mov     ax, [esp+32+4]
758
        call    sys_msg_board_word
759
        mov     esi, v86_exc_str3
760
        call    sys_msg_board_str
761
        mov     ax, [esp+32]
762
        call    sys_msg_board_word
763
        mov     esi, v86_exc_str4
764
        call    sys_msg_board_str
765
        mov     ecx, 8
766
        movzx   edx, word [esp+32+4]
767
        shl     edx, 4
768
        add     edx, [esp+32]
769
@@:
770
        mov     esi, [esp+v86_regs.size+10h+4]
771
        mov     eax, edx
772
        call    v86_get_lin_addr
773
        cmp     eax, 0x1000
774
        jb      .nopage
775
        mov     esi, v86_exc_str3-2
776
        call    sys_msg_board_str
777
        mov     al, [edx]
778
        call    sys_msg_board_byte
779
        inc     edx
780
        loop    @b
781
        jmp     @f
782
.nopage:
783
        mov     esi, v86_exc_str5
784
        call    sys_msg_board_str
785
@@:
786
        mov     esi, v86_newline
787
        call    sys_msg_board_str
788
        mov     eax, 1
789
        jmp     .exit
790
 
791
.done:
792
        xor     eax, eax
793
 
794
.exit:
795
        mov     [esp+v86_regs.size+10h+1Ch], eax
796
        mov     [esp+v86_regs.size+10h+18h], ebx
797
 
798
        mov     edx, [esp+v86_regs.size+10h+14h]
799
        cmp     edx, -1
800
        jz      @f
801
        dec     [v86_irqhooks+edx*8+4]
802
        jnz     @f
803
        and     [v86_irqhooks+edx*8], 0
804
@@:
805
 
806
        mov     esi, esp
807
        mov     edi, [esi+v86_regs.size+10h+10h]
808
        add     edi, v86_regs.size
809
        mov     ecx, v86_regs.size/4
810
        rep     movsd
811
        mov     esp, esi
812
 
813
        cli
814
        mov     ecx, [CURRENT_TASK]
815
        shl     ecx, 8
816
        pop     eax
817
        mov     [SLOT_BASE+ecx+APPDATA.saved_esp0], eax
818
        mov     [tss._esp0], eax
819
        pop     eax
820
        mov     [SLOT_BASE+ecx+APPDATA.dir_table], eax
821
        pop     ebx
822
        mov     dword [SLOT_BASE+ecx+APPDATA.io_map+4], ebx
823
        mov     dword [page_tabs + (tss._io_map_1 shr 10)], ebx
824
        pop     ebx
825
        mov     dword [SLOT_BASE+ecx+APPDATA.io_map], ebx
826
        mov     dword [page_tabs + (tss._io_map_0 shr 10)], ebx
827
        mov     cr3, eax
828
;        mov     [irq_tab+5*4], 0
829
        sti
830
 
831
        popad
832
        ret
833
 
834
;my05:
835
;        mov     dx, 30C2h
836
;        mov     cx, 4
837
;.0:
838
;        in      al, dx
839
;        cmp     al, 0FFh
840
;        jz      @f
841
;        test    al, 4
842
;        jnz     .1
843
;@@:
844
;        add     dx, 8
845
;        in      al, dx
846
;        cmp     al, 0FFh
847
;        jz      @f
848
;        test    al, 4
849
;        jnz     .1
850
;@@:
851
;        loop    .0
852
;        ret
853
;.1:
854
;        or      al, 84h
855
;        out     dx, al
856
;.2:
857
;        mov     dx, 30F7h
858
;        in      al, dx
859
;        mov     byte [BOOT_VAR + 48Eh], 0FFh
860
;        ret
861
 
862
v86_irq:
785 diamond 863
; push irq/pushad/jmp v86_irq
709 diamond 864
; eax = irq
789 diamond 865
        lea     esi, [esp+1Ch]
709 diamond 866
        lea     edi, [esi+4]
785 diamond 867
        mov     ecx, 8
709 diamond 868
        std
869
        rep     movsd
870
        cld
788 diamond 871
        mov     edi, eax
709 diamond 872
        pop     eax
960 diamond 873
v86_irq2:
709 diamond 874
        mov     esi, [v86_irqhooks+edi*8]       ; get VM handle
875
        mov     eax, [esi+V86_machine.pagedir]
876
        call    get_pg_addr
877
        mov     ecx, [CURRENT_TASK]
878
        shl     ecx, 8
879
        cmp     [SLOT_BASE+ecx+APPDATA.dir_table], eax
880
        jnz     .notcurrent
881
        lea     eax, [edi+8]
882
        cmp     al, 10h
971 diamond 883
        mov     ah, 1
709 diamond 884
        jb      @f
724 diamond 885
        add     al, 60h
709 diamond 886
@@:
887
        jmp     v86_exc_c.simulate_int
888
.notcurrent:
889
        mov     ebx, SLOT_BASE + 0x100
890
        mov     ecx, [TASK_COUNT]
891
.scan:
892
        cmp     [ebx+APPDATA.dir_table], eax
893
        jnz     .cont
894
        push    ecx
895
        mov     ecx, [ebx+APPDATA.saved_esp0]
896
        cmp     word [ecx-v86_regs.size+v86_regs.esp], 6
897
        jb      .cont2
898
        movzx   edx, word [ecx-v86_regs.size+v86_regs.ss]
899
        shl     edx, 4
900
        push    eax
901
        movzx   eax, word [ecx-v86_regs.size+v86_regs.esp]
902
        sub     eax, 6
903
        add     edx, eax
904
        mov     eax, edx
905
        call    v86_get_lin_addr
906
        cmp     eax, 0x1000
907
        jb      .cont3
908
        lea     eax, [edx+5]
909
        call    v86_get_lin_addr
910
        cmp     eax, 0x1000
911
        jb      .cont3
912
        pop     eax
913
        pop     ecx
914
        jmp     .found
915
.cont3:
916
        pop     eax
917
.cont2:
918
        pop     ecx
919
.cont:
920
        loop    .scan
921
        mov     al, 20h
922
        out     20h, al
923
        cmp     edi, 8
924
        jb      @f
925
        out     0A0h, al
926
@@:
927
        popad
928
        iretd
929
.found:
930
        mov     cr3, eax
931
        sub     word [esi-v86_regs.size+v86_regs.esp], 6
932
        mov     ecx, [esi-v86_regs.size+v86_regs.eip]
933
        mov     word [edx], cx
934
        mov     ecx, [esi-v86_regs.size+v86_regs.cs]
935
        mov     word [edx+2], cx
936
        mov     ecx, [esi-v86_regs.size+v86_regs.eflags]
937
        mov     word [edx+4], cx
938
        lea     eax, [edi+8]
939
        cmp     al, 10h
940
        jb      @f
724 diamond 941
        add     al, 60h
709 diamond 942
@@:
943
        mov     cx, [eax*4]
944
        mov     word [esi-v86_regs.size+v86_regs.eip], cx
945
        mov     cx, [eax*4+2]
946
        mov     word [esi-v86_regs.size+v86_regs.cs], cx
947
        and     byte [esi-v86_regs.size+v86_regs.eflags+1], not 3
948
        push    ebx
949
        call    update_counters
950
        pop     ebx
951
        sub     ebx, SLOT_BASE
952
        shr     ebx, 8
953
        mov     esi, [CURRENT_TASK]
954
        call    do_change_task
955
        popad
956
        iretd