Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1 ha 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                               ;;
3
;;  MenuetOS process management, protected ring3                 ;;
4
;;                                                               ;;
5
;;  Distributed under GPL. See file COPYING for details.         ;;
6
;;  Copyright 2003 Ville Turjanmaa                               ;;
7
;;                                                               ;;
8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9
 
10
align 32
11
 
12
; GDT TABLE
13
 
14
gdts:
15
 
16
        dw     gdte-$-1
17
        dd     gdts
18
        dw     0
19
 
20
int_code_l:
21
os_code_l:
22
 
23
        dw     0xffff
24
        dw     0x0000
25
        db     0x00
26
        dw     11011111b *256 +10011010b
27
        db     0x00
28
 
29
int_data_l:
30
os_data_l:
31
 
32
        dw     0xffff
33
        dw     0x0000
34
        db     0x00
35
        dw     11011111b *256 +10010010b
36
        db     0x00
37
 
38
graph_data_l:
39
 
40
        dw     0x3ff
41
        dw     0x0000
42
        db     0x00
43
        dw     11010000b *256 +11110010b
44
        db     0x00
45
 
46
tss0_l:
47
      times (max_processes+10) dd 0,0
48
 
49
tss0i_l:
50
      times 0x41 dq 0 ;(256+10) dd 0,0
51
 
52
app_code_l:
4 poddubny 53
      dw ((0x80000000-std_application_base_address) shr 12) and 0xffff
54
      dw 0
55
      db 0
56
      dw 11010000b*256+11111010b+256*((0x80000000-std_application_base_address) shr 28)
57
      db std_application_base_address shr 24
1 ha 58
 
59
app_data_l:
4 poddubny 60
      dw (0x80000000-std_application_base_address) shr 12 and 0xffff
61
      dw 0
62
      db 0
63
      dw 11010000b*256+11110010b+256*((0x80000000-std_application_base_address) shr 28)
64
      db std_application_base_address shr 24
1 ha 65
 
66
 
67
gdte:
68
 
69
 
70
 
71
; 
72
idtreg:
73
     dw   8*0x41-1
74
     dd   idts+8
75
 
76
label idts at 0xB100-8
77
;idte = idts + 8 + 0x60
78
 
79
;  ; old code below:
80
 
81
 
82
;align 32
83
 
84
;idts:
85
;      dw  idte-$-1
86
;      dd  idts+8
87
;      dw  0
88
 
89
;      times 0x62 dd 0,0
90
 
91
;idte:
92
 
93
build_process_gdt_tss_pointer:
94
 
95
        mov    ecx,tss_data
96
        mov    edi,0
97
      setgdtl2:
98
        mov    [edi+gdts+ tss0 +0], word tss_step
99
        mov    [edi+gdts+ tss0 +2], cx
100
        mov    eax,ecx
101
        shr    eax,16
102
        mov    [edi+gdts+ tss0 +4], al
103
        mov    [edi+gdts+ tss0 +7], ah
104
        mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
105
        add    ecx,tss_step
106
        add    edi,8
107
        cmp    edi,8*(max_processes+5)
108
        jbe    setgdtl2
109
 
110
        ret
111
 
112
 
113
build_interrupt_table:
114
 
115
        mov  [l.eflags],dword 0x11002
116
        mov  [l.ss0], int_data
117
        ;mov  [l.ss1], ring1_data
118
        ;mov  [l.ss2], ring2_data
119
        mov  [l.esp0], 0x52000
120
        mov  [l.esp1], 0x53000
121
        mov  [l.esp2], 0x54000
122
 
123
        mov  eax,cr3
124
        mov  [l.cr3],eax
125
        mov  [l.cs],int_code
126
        mov  [l.ss],int_data
127
        mov  [l.ds],int_data
128
        mov  [l.es],int_data
129
        mov  [l.fs],int_data
130
        mov  [l.gs],int_data
131
 
132
        mov  eax,sys_int
133
        mov  [l.esp],0x720000
134
        mov  edi,0x290000
135
 
136
     newint:
137
        push edi
138
        mov  ebx,[eax]
139
        mov  [l.eip],ebx
140
        mov  esi,tss_sceleton
141
        mov  ecx,120/4
142
        cld
143
        rep  movsd
144
        pop  edi
145
 
146
        add  edi,128
147
        add  [l.esp],1024
148
        add  eax,4
149
 
150
        cmp  eax,sys_int+4*0x40 ;0x60
151
        jbe  newint  ;jb
152
 
153
;;
154
 
155
        mov    esi,boot_sched_3_2
156
        call   boot_log
157
 
158
        mov    ecx,0x290000
159
        mov    edi,0
160
      setgdtl2i:
161
        mov    [edi+gdts+ tss0i +0], word 128
162
        mov    [edi+gdts+ tss0i +2], cx
163
        mov    eax,ecx
164
        shr    eax,16
165
        mov    [edi+gdts+ tss0i +4], al
166
        mov    [edi+gdts+ tss0i +7], ah
167
        mov    [edi+gdts+ tss0i +5], word 01010000b *256 +11101001b
168
        add    ecx,128
169
        add    edi,8
170
        cmp    edi,8*0x40 ;0x60
171
        jbe    setgdtl2i
172
 
173
;;
174
 
175
        mov    esi,boot_sched_3_3
176
        call   boot_log
177
 
178
        mov    edi,0
179
        mov    edx,tss0i
180
      setidtl2:
181
        mov    [edi+idts+ 8 +0], word 0
182
        mov    [edi+idts+ 8 +2], dx
183
        mov    [edi+idts+ 8 +4], word 10000101b*256 ; task gate DPL=0
184
;        cmp    edi,0x40*8
185
;        jne    no_sw_int
186
;        mov    [edi+idts+ 8 +4], word 11100101b*256 ; task gate DPL=3
187
;      no_sw_int:
188
        mov    [edi+idts+ 8 +6], word 0
189
        add    edx,8
190
        add    edi,8
191
 
192
        cmp    edi,8*0x40 ;0x60
193
        jbe    setidtl2 ;jb
194
 
195
; 
196
; THIS CODE WON'T WORK ;-(
197
; because each process's 0-level stack points to the same area
198
; and if task switch occurs and another process is being interrupted
199
; a stack overflow happens
200
; The only way to solve that problem is to disable interrupts
201
; while 0x40-handler is working
202
; Then we have to make all entries in the IDT INTERRUPT gates, not TASK
203
;        mov    edi, idts+8
204
;        mov    esi, sys_int
205
;        mov    ecx, 32
206
;     @@:
207
;        mov    eax, [esi]
208
;        mov    [edi],   ax           ; lower part of offset
209
;        mov    [edi+2], word os_code ; segment selector
210
;        shr    eax, 16
211
;        mov    [edi+4], word 10001110b shl 8 ; interrupt descriptor
212
;        mov    [edi+6], ax
213
;        add    esi, 4
214
;        add    edi, 8
215
;        dec    ecx
216
;        jnz    @b
217
; 
218
 
219
        ret
220
 
221
build_syscall_interrupt_table:
222
 
223
        ;mov    dx,tss0sys
224
        mov    edi,8*0x40+idts+8
3 halyavin 225
        mov    [edi + 0], word (i40 and ((1 shl 16)-1))
226
        mov    [edi + 2], word os_code
227
        mov    [edi + 4], word 11101110b*256
228
        mov    [edi + 6], word (i40 shr 16)
1 ha 229
 
230
        ret
231
 
232
 
233
align 4
234
i38:
235
        ; load data selectors
236
        pushfd
237
        push    ds es
238
        push    eax
239
        mov     ax, os_data
240
        mov     ds, ax
241
        mov     es, ax
242
        pop     eax
243
 
244
        pushad
245
        push    edi
246
        mov     edi, eax
247
        mov     eax, ebx
248
        mov     ebx, ecx
249
        mov     ecx, edx
250
        mov     esi, [esp]
251
        and     edi, 0xFF
252
        call    dword [servetable+edi*4]
253
        add     esp, 4
254
        popad
255
        pop     es ds
256
        popfd
257
iret
258
 
259
 
260
iglobal
261
  sys_int:
262
    dd   s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,sa,sb,sc,sd,se,sf
263
 
264
    dd   s10        ,s11      ;  ,i_unknown12,i_unknown13
265
;    dd   i_unknown14,i_unknown15,i_unknown16,i_unknown17
266
;    dd   i_unknown18,i_unknown19,i_unknown1a,i_unknown1b
267
;    dd   i_unknown1c,i_unknown1d,i_unknown1e,i_unknown1f
268
    times 14 dd unknown_interrupt
269
 
270
    dd   irq0  ,irq1  ,p_irq2 ,p_irq3 ,p_irq4 ,p_irq5,p_irq6 ,p_irq7
271
    dd   p_irq8,p_irq9,p_irq10,p_irq11,p_irq12,irqD  ,p_irq14,p_irq15
272
 
273
    ;dd   i_unknown30,i_unknown31,i_unknown32,i_unknown33
274
    ;dd   i_unknown34,i_unknown35,i_unknown36,i_unknown37
275
    ;dd   i_unknown38,i_unknown39,i_unknown3a,i_unknown3b
276
    ;dd   i_unknown3c,i_unknown3d,i_unknown3e,i_unknown3f
277
    times 16 dd unknown_interrupt
278
 
279
    dd   i40
280
endg
281
 
282
uglobal
283
 tss_sceleton:
284
  l.back   dw 0,0
285
  l.esp0   dd 0
286
  l.ss0    dw 0,0
287
  l.esp1   dd 0
288
  l.ss1    dw 0,0
289
  l.esp2   dd 0
290
  l.ss2    dw 0,0
291
  l.cr3    dd 0
292
  l.eip    dd 0
293
  l.eflags dd 0
294
  l.eax    dd 0
295
  l.ecx    dd 0
296
  l.edx    dd 0
297
  l.ebx    dd 0
298
  l.esp    dd 0
299
  l.ebp    dd 0
300
  l.esi    dd 0
301
  l.edi    dd 0
302
  l.es     dw 0,0
303
  l.cs     dw 0,0
304
  l.ss     dw 0,0
305
  l.ds     dw 0,0
306
  l.fs     dw 0,0
307
  l.gs     dw 0,0
308
  l.ldt    dw 0,0
309
  l.trap   dw 0
310
  l.io     dw 0
311
endg
312
 
313
s0:
314
        cli
315
 
316
        mov   [error_interrupt],0x0
317
        mov   [error_interrupt_entry],dword s0
318
 
319
        call  show_error_parameters
320
 
321
        mov   edx,[0x3010]
322
        mov   [edx+0xa],byte 4
323
 
324
        jmp   change_task
325
 
326
 
327
s1:
328
        cli
329
 
330
        mov   [error_interrupt],0x1
331
        mov   [error_interrupt_entry],dword s1
332
 
333
        call  show_error_parameters
334
 
335
        mov   edx,[0x3010]
336
        mov   [edx+0xa],byte 4
337
 
338
        jmp   change_task
339
 
340
s2:
341
        cli
342
 
343
        mov   [error_interrupt],0x2
344
        mov   [error_interrupt_entry],dword s2
345
 
346
        call  show_error_parameters
347
 
348
        mov   edx,[0x3010]
349
        mov   [edx+0xa],byte 4
350
 
351
        jmp   change_task
352
 
353
s3:
354
        cli
355
 
356
        mov   [error_interrupt],0x3
357
        mov   [error_interrupt_entry],dword s3
358
 
359
        call  show_error_parameters
360
 
361
        mov   edx,[0x3010]
362
        mov   [edx+0xa],byte 4
363
 
364
        jmp   change_task
365
 
366
s4:
367
        cli
368
 
369
        mov   [error_interrupt],0x4
370
        mov   [error_interrupt_entry],dword s4
371
 
372
        call  show_error_parameters
373
 
374
        mov   edx,[0x3010]
375
        mov   [edx+0xa],byte 4
376
 
377
        jmp   change_task
378
 
379
s5:
380
        cli
381
 
382
        mov   [error_interrupt],0x5
383
        mov   [error_interrupt_entry],dword s5
384
 
385
        call  show_error_parameters
386
 
387
        mov   edx,[0x3010]
388
        mov   [edx+0xa],byte 4
389
 
390
        jmp   change_task
391
 
392
s6:
393
        cli
394
 
395
        mov   [error_interrupt],0x6
396
        mov   [error_interrupt_entry],dword s6
397
 
398
        call  show_error_parameters
399
 
400
        mov   edx,[0x3010]
401
        mov   [edx+0xa],byte 4
402
 
403
        jmp   change_task
404
 
405
 
406
;;;;;;;;;;;;;;;;;;;;;;;
407
;; FPU ERROR HANDLER ;;
408
;;;;;;;;;;;;;;;;;;;;;;;
409
 
410
align 4
411
s7:
412
        ; 
413
        cli
414
        ; 
415
        mov    edi, 7*8
416
        mov    [edi+gdts+ tss0i +5], word 01010000b *256 +11101001b
417
 
418
        mov    edi,[0x3000]
419
        shl    edi, 3
420
        mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
421
 
422
 
423
        ; save a copy of current task's TSS to fpu_tss
424
        mov    esi,[0x3000]
425
        imul   esi,tss_step
426
        add    esi,tss_data
427
        mov    edi,fpu_tss
428
        mov    ecx,120/4
429
        cld
430
        rep    movsd
431
 
432
        ; get base address of our TSS and...
433
        mov    esi,[0x3000]
434
        imul   esi,tss_step
435
        add    esi,tss_data
436
 
437
        ; ...init segments, stack, eip, flags
438
        mov    word [esi+l.cs-tss_sceleton],int_code
439
        mov    word [esi+l.ss-tss_sceleton],int_data
440
        mov    word [esi+l.ds-tss_sceleton],int_data
441
        mov    word [esi+l.es-tss_sceleton],int_data
442
        mov    word [esi+l.fs-tss_sceleton],int_data
443
        mov    word [esi+l.gs-tss_sceleton],int_data
444
        mov    dword [esi+l.esp-tss_sceleton],fpu_stack+4*8
445
        mov    dword [esi+l.eip-tss_sceleton],fpu_handler
446
        mov    dword [esi+l.eflags-tss_sceleton],0x11002
447
 
448
        ; then execute this task
449
        mov    ebx, [0x3000]
450
        shl    ebx,3
451
        add    ebx, tss0 ;t
452
        mov    [0xB004], bx
453
 
454
        jmp    pword [0xB000]
455
 
456
        jmp    s7
457
 
458
s8:
459
        cli
460
 
461
        mov   [error_interrupt],0x8
462
        mov   [error_interrupt_entry],dword s8
463
 
464
        call  show_error_parameters
465
 
466
        mov   edx,[0x3010]
467
        mov   [edx+0xa],byte 4
468
 
469
        jmp   change_task
470
 
471
s9:
472
        cli
473
 
474
        mov   [error_interrupt],0x9
475
        mov   [error_interrupt_entry],dword s9
476
 
477
        call  show_error_parameters
478
 
479
        mov   edx,[0x3010]
480
        mov   [edx+0xa],byte 4
481
 
482
        jmp   change_task
483
 
484
sa:
485
        cli
486
 
487
        mov   [error_interrupt],0xa
488
        mov   [error_interrupt_entry],dword sa
489
 
490
        call  show_error_parameters
491
 
492
        mov   edx,[0x3010]
493
        mov   [edx+0xa],byte 4
494
 
495
        jmp   change_task
496
 
497
sb:
498
        cli
499
 
500
        mov   [error_interrupt],0xb
501
        mov   [error_interrupt_entry],dword sb
502
 
503
        call  show_error_parameters
504
 
505
        mov   edx,[0x3010]
506
        mov   [edx+0xa],byte 4
507
 
508
        jmp   change_task
509
 
510
sc:
511
        cli
512
 
513
        mov   [error_interrupt],0xc
514
        mov   [error_interrupt_entry],dword sc
515
 
516
        call  show_error_parameters
517
 
518
        mov   edx,[0x3010]
519
        mov   [edx+0xa],byte 4
520
 
521
        jmp   change_task
522
 
523
sd:
524
        cli
525
 
526
        mov   [error_interrupt],0xd
527
        mov   [error_interrupt_entry],dword sd
528
 
529
        call  show_error_parameters
530
 
531
        mov   edx,[0x3010]
532
        mov   [edx+0xa],byte 4
533
 
534
        jmp   change_task
535
 
536
se:
537
        cli
538
 
539
        mov   [error_interrupt],0xe
540
        mov   [error_interrupt_entry],dword se
541
 
542
        call  show_error_parameters
543
 
544
        mov   edx,[0x3010]
545
        mov   [edx+0xa],byte 4
546
 
547
        jmp   change_task
548
 
549
sf:
550
        cli
551
 
552
        mov   [error_interrupt],0xf
553
        mov   [error_interrupt_entry],dword sf
554
 
555
        call  show_error_parameters
556
 
557
        mov   edx,[0x3010]
558
        mov   [edx+0xa],byte 4
559
 
560
        jmp   change_task
561
 
562
s10:
563
        cli
564
 
565
        mov   [error_interrupt],0x10
566
        mov   [error_interrupt_entry],dword s10
567
 
568
        call  show_error_parameters
569
 
570
        mov   edx,[0x3010]
571
        mov   [edx+0xa],byte 4
572
 
573
        jmp   change_task
574
 
575
s11:
576
        cli
577
 
578
        mov   [error_interrupt],0x11
579
        mov   [error_interrupt_entry],dword s11
580
 
581
        call  show_error_parameters
582
 
583
        mov   edx,[0x3010]
584
        mov   [edx+0xa],byte 4
585
 
586
        jmp   change_task
587
 
588
writehex:
589
 
590
      pusha
591
 
592
      mov  ecx,eax
593
      mov  ax,word [printerrorat]
594
      shl  eax,16
595
      mov  ax,[esp+32+4]
596
      sub  ax,60
597
      mov  edx,1
598
      mov  esi,8
599
      mov  ebx,0xffffff
600
    whl1:
601
      push ecx
602
      and  ecx,0xf
603
      add  ecx,hexletters
604
      mov  edi,1
605
 
606
      mov  cl,[ecx]
607
      mov  edi,[write_error_to]
608
      mov  [edi],cl
609
      dec  [write_error_to]
610
 
611
      pop  ecx
612
      shr  ecx,4
613
      sub  eax,6*65536
614
      dec  esi
615
      jnz  whl1
616
 
617
      popa
618
      ret
619
 
620
iglobal
621
  hexletters  db '0123456789ABCDEF'
622
 
623
  error_interrupt         dd  -1
624
  error_interrupt_entry   dd  -1
625
 
626
  printerrorat            dd 300
627
 
628
  process_error  db 'K : Process - forced terminate INT: 00000000',13,10,0
629
  process_pid    db 'K : Process - forced terminate PID: 00000000',13,10,0
630
  process_eip    db 'K : Process - forced terminate EIP: 00000000',13,10,0
631
  system_error   db 'K : Kernel error',13,10,0
632
endg
633
 
634
uglobal
635
  write_error_to  dd  0x0
636
endg
637
 
638
show_error_parameters:
639
 
640
        mov    [write_error_to],process_pid+43
641
        mov    eax,[0x3000]
642
        shl    eax, 5
643
        mov    eax,[0x3000+4+eax]
644
        call   writehex
645
 
646
        mov    [write_error_to],process_error+43
647
        mov    eax,[error_interrupt]
648
        call   writehex
649
 
650
        mov    eax,[0x3000]
651
        shl    eax,8
652
        cmp    [0x80000+eax+0xB0],dword 0
653
        jnz    .system_error
654
 
655
        mov    eax,[0x3000]
656
        imul   eax,tss_step
657
        mov    eax,[eax+tss_data+l.eip-tss_sceleton]
658
.out_eip:
659
        mov    [write_error_to],process_eip+43
660
        call   writehex
661
 
662
        mov    esi,process_error
663
        call   sys_msg_board_str
664
 
665
        mov    esi,process_pid
666
        call   sys_msg_board_str
667
 
668
        mov    esi,process_eip
669
        call   sys_msg_board_str
670
 
671
        ret
672
 
673
.system_error:
674
        mov    esi,system_error
675
        call   sys_msg_board_str
676
        mov    eax,[0x3000]
677
        shl    eax,7
678
        mov    eax,[eax+0x298000+l.eip-tss_sceleton]
679
        jmp    .out_eip
680
 
681
 
682
irq5:
683
 
684
     call   restore_caller
685
 
686
     mov    dx,word [sb16]
687
     add    dx,0xe
688
     in     al,dx
689
 
690
;     mov    byte [SB16_Status],0
691
 
692
     mov    [check_idle_semaphore],5
693
 
694
     mov    al,0x20
695
     out    0x20,al
696
 
697
     call  return_to_caller
698
 
699
     jmp   irq5
700
 
701
irqD:
702
 
703
     call   restore_caller
704
 
705
     mov   dx,0xf0
706
     mov   al,0
707
     out   dx,al
708
 
709
     mov   dx,0xa0
710
     mov   al,0x20
711
     out   dx,al
712
     mov   dx,0x20
713
     out   dx,al
714
 
715
     mov   ds,cx
716
     mov   es,cx
717
     mov   fs,cx
718
 
719
     call  return_to_caller
720
 
721
     jmp   irqD
722
 
723
p_irq2:
724
 
725
     call  restore_caller
726
 
727
     mov   edi,2               ; 1
728
     call  irqhandler          ; 2/5
729
 
730
     call  return_to_caller
731
 
732
     jmp   p_irq2
733
 
734
p_irq3:
735
 
736
     call  restore_caller
737
 
738
     mov   edi,3
739
     call  irqhandler
740
 
741
     call  return_to_caller
742
 
743
     jmp   p_irq3
744
 
745
p_irq4:
746
 
747
     call  restore_caller
748
 
749
     mov   edi,4
750
     call  irqhandler
751
 
752
     call  return_to_caller
753
 
754
     jmp   p_irq4
755
 
756
p_irq5:
757
 
758
     call  restore_caller
759
 
760
     mov   edi,5
761
     call  irqhandler
762
 
763
     call  return_to_caller
764
 
765
     jmp   p_irq5
766
 
767
p_irq6:
768
 
769
     call  restore_caller
770
 
771
     call  fdc_irq
772
 
773
     mov   edi,6
774
     call  irqhandler
775
 
776
     call  return_to_caller
777
 
778
     jmp   p_irq6
779
 
780
p_irq7:
781
 
782
     call  restore_caller
783
 
784
     mov   edi,7
785
     call  irqhandler
786
 
787
     call  return_to_caller
788
 
789
     jmp   p_irq7
790
 
791
p_irq8:
792
 
793
     call  restore_caller
794
 
795
     mov   edi,8
796
     call  irqhandler
797
 
798
     call  return_to_caller
799
 
800
     jmp   p_irq8
801
 
802
p_irq9:
803
 
804
     call  restore_caller
805
 
806
     mov   edi,9
807
     call  irqhandler
808
 
809
     call  return_to_caller
810
 
811
     jmp   p_irq9
812
 
813
p_irq10:
814
 
815
     call  restore_caller
816
 
817
     mov   edi,10
818
     call  irqhandler
819
 
820
     call  return_to_caller
821
 
822
     jmp   p_irq10
823
 
824
p_irq11:
825
 
826
     call  restore_caller
827
 
828
     mov   edi,11
829
     call  irqhandler
830
 
831
     call  return_to_caller
832
 
833
     jmp   p_irq11
834
 
835
p_irq12:
836
 
837
      call  restore_caller
838
 
839
      mov   edi,12
840
      call  irqhandler
841
 
842
      call  return_to_caller
843
 
844
      jmp   p_irq12
845
 
846
p_irq13:
847
 
848
     call  restore_caller
849
 
850
     mov   edi,13
851
     call  irqhandler
852
 
853
     call  return_to_caller
854
 
855
     jmp   p_irq13
856
 
857
p_irq14:
858
 
859
     call  restore_caller
860
 
861
     mov   edi,14
862
     call  irqhandler
863
 
864
     call  return_to_caller
865
 
866
     jmp   p_irq14
867
 
868
p_irq15:
869
 
870
     call  restore_caller
871
 
872
     mov   edi,15
873
     call  irqhandler
874
 
875
     call  return_to_caller
876
 
877
     jmp   p_irq15
878
 
879
 
880
 
881
align 4
882
restore_caller:
883
 
884
      mov    edi,[0x3000]
885
      shl    edi, 3
886
      mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
887
 
888
      ret
889
 
890
align 4
891
return_to_caller:
892
 
893
      mov   ebx,[0x3000]
894
      shl   bx,3
895
      add   bx,tss0;t
896
      mov   [tss_irq12],bx
897
 
898
           db    0xea
899
           dd    0
900
tss_irq12  dw    tss0;t
901
 
902
           ret
903
 
904
uglobal
905
  irqh dd 0x0
906
endg
907
 
908
irqhandler:
909
 
910
     push   edi
911
 
912
     mov    esi,edi          ; 1
913
     shl    esi,6            ; 1
914
     add    esi,irq00read    ; 1
915
     shl    edi,12           ; 1
916
     add    edi,0x2E0000
917
 
918
     mov    [check_idle_semaphore],5
919
 
920
   irqnewread:
921
 
922
     mov    dx,[esi]         ; 2+
923
 
924
     cmp    dx,0             ; 1
925
     jz     irqover
926
     cmp    [esi+3],byte 1   ; 2     ; byte read
927
     jne    noirqbyte        ; 4-11
928
 
929
     in     al,dx
930
 
931
     mov    edx,[edi]
932
     cmp    edx,4000
933
     je     irqfull
934
     mov    ebx,edi
935
     add    ebx,0x10
936
     add    ebx,edx
937
     mov    [ebx],al
938
     inc    edx
939
     mov    [edi],edx
940
 
941
     add    esi,4
942
     jmp    irqnewread
943
 
944
   noirqbyte:
945
 
946
 
947
     cmp    [esi+3],byte 2     ; word read
948
     jne    noirqword
949
 
950
     in     ax,dx
951
 
952
     mov    edx,[edi]
953
     cmp    edx,4000
954
     je     irqfull
955
     mov    ebx,edi
956
     add    ebx,0x10
957
     add    ebx,edx
958
     mov    [ebx],ax
959
     add    edx,2
960
     mov    [edi],edx
961
     add    esi,4
962
     jmp    irqnewread
963
 
964
   noirqword:
965
   irqfull:
966
   irqover:
967
 
968
     mov    al,0x20            ; ready for next irq
969
     out    0x20,al
970
 
971
     pop    ebx
972
     cmp    ebx,7
973
     jbe    noa0
974
     out    0xa0,al
975
   noa0:
976
 
977
     ret
978
 
979
 
980
; this code should never get control!
981
; applications can use only 0x40 interrupt
982
unknown_interrupt:
983
 @@: call   change_task
984
     jmp    @b
985
 
986
 
987
 
988
 
989
set_application_table_status:
990
        push eax
991
 
992
        mov  eax,[0x3000]
993
        ;imul eax,32
994
        shl  eax, 5
995
        add  eax,0x3000+4
996
        mov  eax,[eax]
997
 
998
        mov  [application_table_status],eax
999
 
1000
        pop  eax
1001
 
1002
        ret
1003
 
1004
 
1005
clear_application_table_status:
1006
        push eax
1007
 
1008
        mov  eax,[0x3000]
1009
        ;imul eax,32
1010
        shl  eax, 5
1011
        add  eax,0x3000+4
1012
        mov  eax,[eax]
1013
 
1014
        cmp  eax,[application_table_status]
1015
        jne  apptsl1
1016
        mov  [application_table_status],0
1017
      apptsl1:
1018
 
1019
        pop  eax
1020
 
1021
        ret
1022
 
1023
uglobal
1024
  old_code_0 dd 0x0
1025
  old_code_1 dd 0x0
1026
 
1027
  ;
1028
 
1029
  new_code_0 dd 0x0
1030
  new_code_1 dd 0x0
1031
 
1032
  new_data_0 dd 0x0
1033
  new_data_1 dd 0x0
1034
 
1035
  new_pos    dd 0x0
1036
  new_amount dd 0x0
1037
endg
1038
 
1039
 
1040
sys_resize_app_memory:
1041
        ; eax = 1 - resize
1042
        ;     ebx = new amount of memory
1043
 
1044
        cmp    eax,1
4 poddubny 1045
        jne    .no_application_mem_resize
1 ha 1046
 
4 poddubny 1047
        jmp    new_mem_resize ;resize for new type of processes
1 ha 1048
 
1049
 
4 poddubny 1050
     .no_application_mem_resize:
1 ha 1051
 
1052
        ret
1053
 
1054
 
1055
 
1056
get_app_params:
1057
 
1058
    push eax
1059
 
1060
    cmp  [0x90000+6],word '00'
1061
    jne  no_00_header
1062
 
1063
    mov  eax,[0x90000+12]
1064
    mov  [app_start],eax
1065
    mov  eax,[0x90000+16]
1066
    mov  [app_i_end],eax
1067
    mov  eax,[0x90000+20]
1068
    mov  [app_mem],eax
1069
    shr  eax,1
1070
    sub  eax,0x10
1071
    mov  [app_esp],eax
1072
    mov  eax,[0x90000+24]
1073
    mov  [app_i_param],eax
1074
    mov  [app_i_icon],dword 0
1075
 
1076
    pop  eax
1077
    mov  esi,1
1078
    ret
1079
 
1080
  no_00_header:
1081
 
1082
 
1083
    cmp  [0x90000+6],word '01'
1084
    jne  no_01_header
1085
 
1086
    mov  eax,[0x90000+12]
1087
    mov  [app_start],eax
1088
    mov  eax,[0x90000+16]
1089
    mov  [app_i_end],eax
1090
    mov  eax,[0x90000+20]
1091
    mov  [app_mem],eax
1092
    mov  eax,[0x90000+24]
1093
    mov  [app_esp],eax
1094
    mov  eax,[0x90000+28]
1095
    mov  [app_i_param],eax
1096
    mov  eax,[0x90000+32]
1097
    mov  [app_i_icon],eax
1098
 
1099
    pop  eax
1100
    mov  esi,1
1101
    ret
1102
 
1103
   no_01_header:
1104
 
1105
    pop  eax
1106
    mov  esi,0
1107
    ret
1108
 
1109
 
1110
start_application_fl:
1111
    jmp new_start_application_fl
1112
 
1113
;************************************************************************
1114
 
1115
start_application_floppy:
1116
    jmp  new_start_application_floppy
1117
 
1118
;********************************************************************
1119
 
1120
start_application_hd:
1121
    jmp   new_start_application_hd
1122
 
1123
uglobal
4 poddubny 1124
  ;threadstring       dd  0x0
1 ha 1125
  new_process_place  dd  0x0
4 poddubny 1126
  ;check_processes    dd  0x0
1127
  ;dec3004            db  0x0
1 ha 1128
  app_start    dd  0x0
1129
  app_i_end    dd  0x0
1130
  app_mem      dd  0x0
1131
  app_esp      dd  0x0
1132
  app_i_param  dd  0x0
1133
  app_i_icon   dd  0x0
1134
  app_mem_pos  dd  0x0
4 poddubny 1135
  ;thread_create    dd 0x0
1136
  ;gdt_place        dd 0x0
5 halyavin 1137
  appl_path        dd 0x0
1138
  appl_path_size   dd 0x0
1 ha 1139
endg
1140
 
1141
iglobal
1142
  hd_app_string      db  'HDAPP       '
1143
  process_loading    db 'K : Process - loading ',13,10,0
1144
  process_running    db 'K : Process - done',13,10,0
1145
  first_gdt_search   dd 0x2
1146
endg
1147
 
1148
 
1149
sys_threads:
1150
 
1151
; eax=1 create thread
1152
;
1153
;   ebx=thread start
1154
;   ecx=thread stack value
1155
;
1156
; on return : eax = pid
1157
jmp new_sys_threads
1158
 
1159
iglobal
1160
  process_terminating   db 'K : Process - terminating',13,10,0
1161
  process_terminated    db 'K : Process - done',13,10,0
1162
endg
1163
 
1164
 
1165
terminate: ; terminate application
1166
    push   esi
1167
    mov    esi,process_terminating
1168
    call   sys_msg_board_str
1169
    pop    esi
1170
;start memory manager code
1171
;    mov    eax,esi
1172
;    call   MEM_Heap_Clean
1173
;end memory manager code
1174
 
1175
 
1176
    cli
1177
    cmp   [application_table_status],0
1178
    je    term9
1179
    sti
1180
    call  change_task
1181
    jmp   terminate
1182
  term9:
1183
 
1184
    call  set_application_table_status
1185
 
1186
    mov    eax,esi
1187
    call   dispose_app_cr3_table
1188
 
1189
    mov   [first_gdt_search],0x2   ; start gdt search from beginning
1190
 
1191
    cmp   [prev_user_of_fpu],esi   ; if user fpu last -> fpu user = 1
1192
    jne   fpu_ok_1
1193
    mov   [prev_user_of_fpu],1
1194
  fpu_ok_1:
1195
 
1196
    mov   [0xf400],byte 0           ; empty keyboard buffer
1197
    mov   [0xf500],byte 0           ; empty button buffer
1198
 
4 poddubny 1199
;    mov   ecx,esi                               ; clear memory reserv.
1200
;    shl   ecx,3
1201
;    mov   [ecx+gdts+ app_code-3 +0],dword 0
1202
;    mov   [ecx+gdts+ app_code-3 +4],dword 0
1203
;    mov   [ecx+gdts+ app_data-3 +0],dword 0
1204
;    mov   [ecx+gdts+ app_data-3 +4],dword 0
1 ha 1205
 
1206
    mov   edi, esi
1207
 
1208
 
1209
    mov   [usedi40+eax],byte 0
1210
 
1211
 
1212
    mov   ecx,esi                 ; remove buttons
1213
  bnewba2:
1214
    mov   edi,[0xfe88]
1215
    mov   eax,edi
1216
    cld
1217
    movzx ebx,word [edi]
1218
    inc   bx
1219
  bnewba:
1220
    dec   bx
1221
    jz    bnmba
1222
    add   eax,0x10
1223
    cmp   cx,[eax]
1224
    jnz   bnewba
1225
    pusha
1226
    mov   ecx,ebx
1227
    inc   ecx
1228
    shl   ecx,4
1229
    mov   ebx,eax
1230
    add   eax,0x10
1231
    call  memmove
1232
    dec   dword [edi]
1233
    popa
1234
    jmp   bnewba2
1235
  bnmba:
1236
 
1237
    pusha     ; save window coordinates for window restoring
1238
    cld
1239
    shl   esi,5
1240
    add   esi,window_data
1241
    mov   ax,[esi+0]
1242
    mov   word [dlx],ax
1243
    mov   bx,[esi+8]
1244
    add   ax,bx
1245
    mov   word [dlxe],ax
1246
    mov   ax,[esi+4]
1247
    mov   word [dly],ax
1248
    mov   bx,[esi+12]
1249
    add   ax,bx
1250
    mov   word [dlye],ax
1251
    mov   [esi+0],word 0
1252
    mov   [esi+8],word 5
1253
    mov   ax,[0xFE04]
1254
    mov   [esi+4],ax
1255
    mov   [esi+12],word 5
1256
    xor   eax, eax
1257
    mov   [esi+16],eax;dword 0
1258
    mov   [esi+20],eax;dword 0
1259
    mov   [esi+24],eax;dword 0
1260
    mov   [esi+28],eax;dword 0
1261
    popa
1262
 
1263
    pusha
1264
    mov   edi,esi
1265
    shl   edi,5
1266
    add   edi,window_data
1267
    mov   ecx,32/4
1268
    xor   eax, eax
1269
  ;  cld
1270
    rep   stosd
1271
 
1272
    mov   eax,[0xFE04]      ; set window to start from maxy+1
1273
    add   eax,2
1274
 
1275
    mov   edi,esi
1276
    shl   edi,5
1277
    add   edi,window_data
1278
    mov   [edi+4],eax
1279
 
1280
    popa
1281
 
1282
    pusha
1283
    mov   edi,esi
1284
    shl   edi,5
1285
    add   edi,draw_data
1286
    mov   ecx,32/4
1287
    xor   eax, eax
1288
  ;  cld
1289
    rep   stosd
1290
    popa
1291
 
1292
    pusha         ; at 0x80000+
1293
    mov   edi,esi
1294
    shl   edi,8
1295
    add   edi,0x80000
1296
    mov   ecx,256/4
1297
    xor   eax, eax
1298
  ;  cld
1299
    rep   stosd
1300
    popa
1301
 
1302
    pusha          ; name to spaces
1303
    mov   edi,esi
1304
    shl   edi,8
1305
    add   edi,0x80000
1306
    mov   ecx,11
1307
    mov   eax,' '
1308
  ;  cld
1309
    rep   stosb
1310
    popa
1311
 
1312
    pusha                ; C000 --> C400
1313
    mov   eax, 0xc000
1314
    mov   esi, 0
1315
  nlc40:
1316
    add   eax, 2
1317
    inc   esi
1318
    cmp   esi, [0x3004]
1319
    jae   nlc41
1320
    movzx ecx, word [eax]
1321
    mov   [0xC400 + ecx*2], si
1322
    jmp   nlc40
1323
  nlc41:
1324
    popa
1325
 
1326
    pusha ; remove hd1 reservation
1327
    mov   edx,esi
1328
    shl   edx, 5 ;imul  edx,0x20
1329
    add   edx,0x3000
1330
    mov   edx,[edx+4]
1331
    cmp   [hd1_status],edx
1332
    jne   no_hd1_s_remove
1333
    mov   [hd1_status],0
1334
  no_hd1_s_remove:
1335
    popa
1336
 
1337
    pusha ; remove all irq reservations
1338
    mov   edx,esi
1339
    shl   edx, 5 ;imul  edx,0x20
1340
    add   edx,0x3000
1341
    mov   edx,[edx+4]
1342
    mov   edi,irq_owner
1343
    mov   ecx,16
1344
  newirqfree:
1345
    cmp   [edi],edx
1346
    jne   nofreeirq
1347
    mov   [edi],dword 0
1348
  nofreeirq:
1349
    add    edi,4
1350
    loop   newirqfree
1351
    popa
1352
 
1353
 
1354
    pusha                     ; remove all port reservations
1355
 
1356
    mov   edx,esi
1357
    shl   edx, 5 ;imul  edx,0x20
1358
    add   edx,0x3000
1359
    mov   edx,[edx+4]
1360
 
1361
  rmpr0:
1362
 
1363
    mov   esi,[0x2d0000]
1364
 
1365
    cmp   esi,0
1366
    je    rmpr9
1367
 
1368
  rmpr3:
1369
 
1370
    mov   edi,esi
1371
    shl   edi,4
1372
    add   edi,0x2d0000
1373
 
1374
    cmp   edx,[edi]
1375
    je    rmpr4
1376
 
1377
    dec   esi
1378
    jnz   rmpr3
1379
 
1380
    jmp   rmpr9
1381
 
1382
  rmpr4:
1383
 
1384
    mov   ecx,256
1385
    sub   ecx,esi
1386
    shl   ecx,4
1387
 
1388
    mov   esi,edi
1389
    add   esi,16
1390
    cld
1391
    rep   movsb
1392
 
1393
    dec   dword [0x2d0000]
1394
 
1395
    jmp   rmpr0
1396
 
1397
  rmpr9:
1398
 
1399
    popa
1400
    mov  edi,esi         ; do not run this process slot
1401
    shl  edi, 5 ;imul edi,0x20
1402
;    add  edi,0x3000
1403
    mov  [edi+0x300A],byte 9
1404
;    call  systest
1405
    sti  ; .. and life goes on
1406
 
1407
;    movzx eax,word [dlx]
1408
;    movzx ebx,word [dly]
1409
;    movzx ecx,word [dlxe]
1410
;    movzx edx,word [dlye]
1411
    call  calculatescreen
1412
 
1413
    xor   eax, eax
1414
    xor   esi, esi
1415
    call  redrawscreen
1416
 
1417
    mov   [0xfff4],byte 0  ; no mouse background
1418
    mov   [0xfff5],byte 0  ; draw mouse
1419
 
1420
    mov   [application_table_status],0
1421
 
1422
    mov   esi,process_terminated
1423
    call  sys_msg_board_str
1424
 
1425
;* start code - fix error redraw for terminate (2) - Mario79
1426
;     cmp  [draw_present],1
1427
;     je  no_draw_background_temp
1428
;     mov   [0xfff0],byte 1
1429
;no_draw_background_temp:
1430
;    mov   [draw_present],0
1431
    ret
1432
;draw_present db 0
1433
;* end code - fix error redraw for terminate (2) - Mario79
1434
 
1435
iglobal
1436
  boot_sched_1    db   'Building gdt tss pointer',0
1437
  ;boot_sched_2    db   'Building gdt gate pointer',0
1438
  boot_sched_3    db   'Building interrupt table - TSS',0
1439
  boot_sched_3_2  db   'Building interrupt table - GDT',0
1440
  boot_sched_3_3  db   'Building interrupt table - IDT',0
1441
  boot_sched_4    db   'Building syscall interrupt table',0
1442
endg
1443
 
1444
 
1445
build_scheduler:
1446
      ; { Ivan 06.03.2005
1447
        mov    edi, usedi40
1448
        mov    ecx, 256/4
1449
        xor    eax, eax
1450
        rep    stosd
1451
      ; } Ivan 06.03.2005
1452
 
1453
        mov    esi,boot_sched_1
1454
        call   boot_log
1455
        call   build_process_gdt_tss_pointer
1456
 
1457
;        mov    esi,boot_sched_2
1458
;        call   boot_log
1459
;        call   build_process_gdt_gate_pointer
1460
 
1461
        mov    esi,boot_sched_3
1462
        call   boot_log
1463
        call   build_interrupt_table
1464
 
1465
        mov    esi,boot_sched_4
1466
        call   boot_log
1467
        call   build_syscall_interrupt_table
1468
 
1469
        ret
1470