Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
164 serge 1
 
2
{ .banner      dq ?
3
  .version     dd ?    ;+8
4
  .start       dd ?    ;+12
5
  .i_end       dd ?    ;+16
6
  .mem_size    dd ?    ;+20
7
  .i_param     dd ?    ;+24
8
}
9
10
 
11
{ .banner      dq ?
12
  .version     dd ?    ;+8
13
  .start       dd ?    ;+12
14
  .i_end       dd ?    ;+16
15
  .mem_size    dd ?    ;+20
16
  .stack_top   dd ?    ;+24
17
  .i_param     dd ?    ;+28
18
  .i_icon      dd ?    ;+32
19
}
20
21
 
22
proc test_app_header stdcall, header:dword
23
           virtual at ebx
24
             APP_HEADER_00 APP_HEADER_00
25
           end virtual
26
27
 
28
           cmp [ebx+6], word '00'
29
           jne  .check_01_header
30
31
 
32
           mov  [app_start],eax
33
           mov  eax,[APP_HEADER_00.i_end]
34
           mov  [app_i_end],eax
35
           mov  eax,[APP_HEADER_00.mem_size]
36
           mov  [app_mem],eax
37
           shr  eax,1
38
           sub  eax,0x10
39
           mov  [app_esp],eax
40
           mov  eax,[APP_HEADER_00.i_param]
41
           mov  [app_i_param],eax
42
           mov  [app_i_icon],dword 0
43
44
 
45
           ret
46
47
 
48
           virtual at ebx
49
             APP_HEADER_01 APP_HEADER_01
50
           end virtual
51
52
 
53
           jne  .no_01_header
54
55
 
56
           mov  [app_start],eax
57
           mov  eax,[APP_HEADER_01.i_end]
58
           mov  [app_i_end],eax
59
           mov  eax,[APP_HEADER_01.mem_size]
60
           mov  [app_mem],eax
61
           mov  eax,[APP_HEADER_01.stack_top]
62
           mov  [app_esp],eax
63
           mov  eax,[APP_HEADER_01.i_param]
64
           mov  [app_i_param],eax
65
           mov  eax,[APP_HEADER_01.i_icon]
66
           mov  [app_i_icon],eax
67
68
 
69
           ret
70
71
 
72
73
 
74
           ret
75
endp
76
77
 
78
proc get_new_process_place
79
;input:
80
;  none
81
;result:
82
;  eax=[new_process_place]<>0 - ok
83
;      0 - failed.
84
;This function find least empty slot.
85
;It doesn't increase [TASK_COUNT]!
86
           mov    eax,CURRENT_TASK
87
           mov    ebx,[TASK_COUNT]
88
           inc    ebx
89
           shl    ebx,5
90
           add    ebx,eax               ;ebx - address of process information for (last+1) slot
91
.newprocessplace:
92
;eax = address of process information for current slot
93
           cmp    eax,ebx
94
           jz     .endnewprocessplace   ;empty slot after high boundary
95
           add    eax,0x20
96
           cmp    word [eax+0xa],9      ;check process state, 9 means that process slot is empty
97
           jnz    .newprocessplace
98
.endnewprocessplace:
99
           mov    ebx,eax
100
           sub    eax,CURRENT_TASK
101
           shr    eax,5                 ;calculate slot index
102
           cmp    eax,256
103
           jge    .failed               ;it should be <256
104
           mov    word [ebx+0xa],9      ;set process state to 9 (for slot after hight boundary)
105
        ;   mov [new_process_place], eax
106
           ret
107
108
 
109
           xor    eax,eax
110
           ret
111
endp
112
113
 
114
proc create_app_space stdcall, app_size:dword,img_size:dword
115
           locals
116
             app_pages   dd ?
117
             img_pages   dd ?
118
             dir_addr    dd ?
119
             master_addr dd ?
120
             app_tabs    dd ?
121
           endl
122
123
 
124
125
 
126
           mov [dir_addr], eax
127
           mov [master_addr], eax
128
129
 
130
           add eax, 4095+4096
131
           and eax, NOT(4095)
132
           mov [app_size], eax
133
           mov ebx, eax
134
           shr eax, 12
135
           mov [app_pages], eax
136
           add ebx, 0x3FFFFF
137
           and ebx, NOT(0x3FFFFF)
138
           shr ebx, 22
139
           mov [app_tabs], ebx
140
141
 
142
           add eax, 4095
143
           and eax, NOT(4095)
144
145
 
146
           shr eax, 12
147
           mov [img_pages], eax
148
149
 
150
           test eax, eax
151
           jz .fail
152
           mov [dir_addr], eax
153
           stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
154
155
 
156
           mov edi, [tmp_task_pdir]
157
           mov ecx, 384
158
           cld
159
           rep movsd
160
161
 
162
           xor eax, eax
163
           cld
164
           rep stosd
165
166
 
167
           mov esi, sys_pgdir+0xc00
168
           rep movsd
169
170
 
171
           test eax, eax
172
           jz .fail
173
           mov [master_addr], eax
174
           stdcall map_page,[tmp_task_ptab],eax,dword PG_SW
175
176
 
177
           mov edi, [tmp_task_ptab]
178
           mov esi, master_tab
179
           cld
180
           rep movsd
181
182
 
183
           xor eax, eax
184
           rep stosd
185
186
 
187
           mov esi, master_tab+0xc00
188
           rep movsd
189
190
 
191
           or eax, PG_SW
192
           mov ebx, [tmp_task_pdir]
193
           mov [ebx+0x600], eax
194
           mov ecx, [tmp_task_ptab]
195
           mov [ecx+0x600],eax
196
197
 
198
           call set_cr3
199
200
 
201
           mov edi, new_app_base
202
@@:
203
           call alloc_page
204
           test eax, eax
205
           jz .fail
206
207
 
208
           add edi, 0x00400000
209
           dec edx
210
           jnz @B
211
212
 
213
           shr edi, 10
214
           add edi, pages_tab
215
           mov ecx, [app_tabs]
216
           shl ecx, 10
217
           xor eax, eax
218
           rep stosd
219
220
 
221
222
 
223
           call alloc_page
224
           test eax, eax
225
           jz .fail
226
227
 
228
           add edx, 0x1000
229
           sub [app_pages], 1
230
           sub [img_pages], 1
231
           jnz .alloc
232
233
 
234
           and ecx, ecx
235
           jz .next
236
237
 
238
           shr edx, 12
239
.reserve:
240
           mov dword [pages_tab+edx*4], 0x02
241
           invlpg [ebx]
242
           inc edx
243
           dec ecx
244
           jnz .reserve
245
.next:
246
           mov edi, new_app_base
247
           mov ecx, [img_size]
248
           shr ecx, 2
249
           xor eax, eax
250
           cld
251
           rep stosd
252
253
 
254
           stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
255
256
 
257
           mov eax, [dir_addr]
258
           ret
259
.fail:
260
           dec [pg_data.pg_mutex]
261
           cmp [dir_addr], 0
262
           jz @f
263
           stdcall destroy_app_space, [dir_addr]
264
@@:
265
           xor eax, eax
266
           ret
267
endp
268
269
 
270
set_cr3:
271
           mov esi, [CURRENT_TASK]
272
           mov ebx, esi
273
           shl esi,8
274
           mov [PROC_BASE+esi+0xB8],eax
275
           imul   ebx,tss_step
276
           add    ebx,tss_data
277
           mov [ebx+28], eax
278
           mov cr3, eax
279
           ret
280
281
 
282
proc destroy_page_table stdcall, pg_tab:dword
283
284
 
285
286
 
287
           mov ecx, 1024
288
.free:
289
           mov eax, [esi]
290
           test eax, 1
291
           jz .next
292
           call free_page
293
.next:
294
           add esi, 4
295
           dec ecx
296
           jnz .free
297
           pop esi
298
           ret
299
endp
300
301
 
302
proc destroy_app_space stdcall, pg_dir:dword
303
304
 
305
306
 
307
           mov   eax,0x2
308
           mov ebx, [pg_dir]
309
310
 
311
;eax = current slot of process
312
           mov   ecx,eax
313
           shl   ecx,5
314
           cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
315
           jz    @f                              ;skip empty slots
316
           shl   ecx,3
317
           cmp   [PROC_BASE+ecx+0xB8],ebx       ;compare page directory addresses
318
           jnz   @f
319
           inc   edx                            ;thread found
320
@@:
321
           inc   eax
322
           cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
323
           jle   .loop
324
325
 
326
;our process is zombi so it isn't counted
327
           cmp   edx,1
328
           jg    .exit
329
;if there isn't threads then clear memory.
330
331
 
332
           and eax, not 0xFFF
333
           stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
334
           mov esi, [tmp_task_pdir]
335
           add esi, 0x600
336
           mov eax, [esi]
337
           call free_page   ;destroy master table
338
           add esi, 4
339
           mov edi, 383
340
.destroy:
341
           mov eax, [esi]
342
           test eax, 1
343
           jz .next
344
           and eax, not 0xFFF
345
           stdcall map_page,[tmp_task_ptab],eax,dword PG_SW
346
           stdcall destroy_page_table, [tmp_task_ptab]
347
           mov eax, [esi]
348
           call free_page
349
.next:
350
           add esi, 4
351
           dec edi
352
           jnz .destroy
353
354
 
355
           call free_page
356
.exit:
357
           stdcall map_page,[tmp_task_ptab],dword 0,dword PG_UNMAP
358
           stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
359
           dec [pg_data.pg_mutex]
360
           ret
361
endp
362
363
 
364
proc fs_execute
365
366
 
367
368
 
369
; edx - flags
370
; ebp - full filename
371
; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
372
373
 
374
             cmdline    dd ?
375
             flags      dd ?
376
             filename   dd ?
377
             retval     dd ?
378
           endl
379
380
 
381
382
 
383
           mov [flags], edx
384
           mov eax, [ebp]
385
           mov [filename], eax
386
387
 
388
389
 
390
           mov ecx, (2048+256)/4
391
           xor eax, eax
392
           rep stosd
393
394
 
395
           mov edi, [tmp_task_data]
396
           add edi, TMP_FILE_NAME
397
           mov ecx, 1024
398
           rep movsb
399
400
 
401
           mov edi, [tmp_task_data]
402
           add edi, TMP_ICON_OFFS
403
           mov ecx, 1024
404
           rep movsb
405
406
 
407
           test esi, esi
408
           jz @f
409
           mov edi, [tmp_task_data]
410
           add edi, TMP_CMD_LINE
411
           mov ecx, 256
412
           rep movsb
413
@@:
414
           mov eax, TMP_FILE_NAME
415
           add eax, [tmp_task_data]
416
           mov ebx, [tmp_task_data]    ;cmd line
417
           add ebx, TMP_CMD_LINE
418
419
 
420
                            [ebp+12], [ebp+16],[ebp+20]
421
           mov [retval], eax
422
           popad
423
           mov [pg_data.tmp_task_mutex], 0
424
           mov eax, [retval]
425
           ret
426
427
 
428
429
 
430
proc fs_exec stdcall file_name:dword, cmd_line:dword, flags:dword,\
431
                       fn_read:dword, file_size:dword,\
432
                       cluster:dword, some_data:dword
433
434
 
435
             slot          dd ?
436
             app_path_size dd ?
437
             save_cr3      dd ?
438
             img_size      dd ?
439
           endl
440
441
 
442
443
 
444
           mov ecx, 1024
445
           xor eax, eax
446
           repnz scasb
447
           jz @f
448
           mov     eax, -ERROR_FILE_NOT_FOUND
449
           ret
450
@@:
451
           sub edi, [file_name]
452
           mov [app_path_size], edi
453
454
 
455
           call sys_msg_board_str       ; write message to message board
456
457
 
458
           cli
459
460
 
461
           cmp [application_table_status],0
462
           je .get_lock
463
           call   change_task
464
           jmp .wait_lock
465
466
 
467
           mov eax, 1
468
           xchg eax, [application_table_status]
469
           cmp eax, 0
470
           jne .wait_lock
471
472
 
473
474
 
475
           test eax, eax
476
           mov ecx, -0x20      ; too many processes
477
           jz .err
478
           mov [slot], eax
479
480
 
481
           shl    edi,8
482
           add    edi,PROC_BASE
483
           mov    ecx,256/4
484
           xor    eax,eax
485
           cld
486
           rep    stosd              ;clean extended information about process
487
488
 
489
490
 
491
           mov ecx, [app_path_size]
492
           add edi, ecx
493
           dec edi
494
           std
495
           mov al, '/'
496
           repnz scasb
497
           cld
498
           jnz @f
499
           inc edi
500
@@:
501
           inc edi
502
; now edi points to name without path
503
504
 
505
           mov ecx, 8  ; 8 chars for name
506
           mov edi, [slot]
507
           shl edi, cl
508
           add edi, PROC_BASE
509
.copy_process_name_loop:
510
           lodsb
511
           cmp al, '.'
512
           jz .copy_process_name_done
513
           test al, al
514
           jz .copy_process_name_done
515
           stosb
516
           loop .copy_process_name_loop
517
.copy_process_name_done:
518
           mov al, ' '
519
           rep stosb
520
           pop eax
521
           mov cl, 3   ; 3 chars for extension
522
           dec esi
523
@@:
524
           dec eax
525
           cmp eax, esi
526
           jbe .copy_process_ext_done
527
           cmp byte [eax], '.'
528
           jnz @b
529
           lea esi, [eax+1]
530
.copy_process_ext_loop:
531
           lodsb
532
           test al, al
533
           jz  .copy_process_ext_done
534
           stosb
535
           loop .copy_process_ext_loop
536
.copy_process_ext_done:
537
           mov al, ' '
538
           rep stosb
539
540
 
541
542
 
543
           mov  edi, TMP_BUFF
544
           call [fn_read]
545
           test eax, eax
546
           jnz .err
547
548
 
549
550
 
551
;check MENUET signature
552
           cmp    [TMP_BUFF],dword 'MENU'
553
           jnz    .err
554
           cmp    [TMP_BUFF+4],word 'ET'
555
           jnz    .err
556
557
 
558
           test eax, eax
559
           jz .err
560
561
 
562
           mov [save_cr3], eax
563
           stdcall create_app_space,[app_mem], [app_mem];[file_size]
564
           test eax, eax
565
           jz .failed
566
567
 
568
           shl   ebx,8
569
           mov   [PROC_BASE+ebx+0xB8],eax
570
571
 
572
           mov edi, new_app_base
573
           mov ecx, 512/4
574
           cld
575
           rep movsd
576
577
 
578
@@:
579
           lea  eax, [file_size]
580
           cmp dword [eax], 0
581
           jz .done
582
           push edi
583
           call [fn_read]
584
           pop edi
585
           add edi, 512
586
           test eax, eax
587
           jz  @b
588
           cmp ebx, 6
589
           jne .failed
590
.done:
591
           stdcall add_app_parameters, [slot], new_app_base,\
592
                                       [cmd_line],[file_name],[flags]
593
594
 
595
           call set_cr3
596
597
 
598
           mov    [application_table_status],eax ;unlock application_table_status mutex
599
           popfd
600
           mov    eax,[process_number]  ;set result
601
           ret
602
603
 
604
           mov eax, [save_cr3]
605
           call set_cr3
606
.err:
607
608
 
609
           xor eax, eax
610
           mov [application_table_status],eax
611
           ret
612
endp
613
614
 
615
proc add_app_parameters stdcall,slot:dword,img_base:dword,\
616
                        cmd_line:dword, app_path:dword, flags:dword
617
618
 
619
           bt [cpu_caps], CAPS_SSE
620
           jnc .no_SSE
621
           shl eax, 8
622
           mov ebx, eax
623
           add eax, eax
624
           add eax, [fpu_data]
625
           mov [ebx+PROC_BASE+APPDATA.fpu_state], eax
626
           mov [ebx+PROC_BASE+APPDATA.fpu_handler], 0
627
           mov [ebx+PROC_BASE+APPDATA.sse_handler], 0
628
           jmp .m1
629
.no_SSE:
630
           mov ecx, eax
631
           mov ebx, eax
632
           shl eax, 7
633
           shl ebx, 4
634
           sub eax, ebx       ;eax*=112
635
           add eax, [fpu_data]
636
           shl ecx, 8
637
           mov [ecx+PROC_BASE+APPDATA.fpu_state], eax
638
           mov [ecx+PROC_BASE+APPDATA.fpu_handler], 0
639
           mov [ecx+PROC_BASE+APPDATA.sse_handler], 0
640
.m1:
641
           mov    ebx,[slot]
642
           cmp    ebx,[TASK_COUNT]
643
           jle    .noinc
644
           inc    dword [TASK_COUNT]       ;update number of processes
645
.noinc:
646
           shl    ebx,8
647
           mov    eax,[app_mem]
648
           mov    [PROC_BASE+0x8c+ebx],eax
649
650
 
651
           mov eax, new_app_base
652
           mov    dword [CURRENT_TASK+ebx+0x10],eax
653
654
 
655
           mov    edx,[app_i_param]
656
           test   edx,edx
657
           jz     .no_command_line      ;application don't need parameters
658
           mov    eax,[cmd_line]
659
           test   eax,eax
660
           jz     .no_command_line      ;no parameters specified
661
;calculate parameter length
662
           xor    ecx,ecx
663
.command_line_len:
664
           cmp    byte [eax],0
665
           jz     .command_line_len_end
666
           inc    eax
667
           inc    ecx
668
           cmp    ecx,255
669
           jl     .command_line_len
670
671
 
672
;ecx - parameter length
673
;edx - address of parameters in new process address space
674
           inc ecx
675
           mov edi, [img_base]
676
           add edi, edx
677
           mov esi, [cmd_line]
678
           rep movsb
679
680
 
681
682
 
683
            test   edx,edx
684
            jz     .no_command_line_1      ;application don't need path of file
685
            mov    esi,[app_path]
686
            test esi, esi
687
            jz     .no_command_line_1      ;application don't need path of file
688
            mov    ecx, 64
689
            mov    edi, [img_base]
690
            add edi, edx
691
            rep movsb
692
693
 
694
           mov    ebx,[slot]
695
           mov    eax,ebx
696
           shl    ebx,5
697
           add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
698
           mov    [ebx+0xe],al          ;set window number on screen = process slot
699
700
 
701
702
 
703
           mov    eax,[process_number]
704
           mov    [ebx+4],eax           ;set PID
705
706
 
707
           add    ecx,(draw_data-CURRENT_TASK)  ;ecx - pointer to draw data
708
;set draw data to full screen
709
710
 
711
           mov    [ecx+4],dword 0
712
           mov    eax,[SCR_X_SIZE]
713
           mov    [ecx+8],eax
714
           mov    eax,[SCR_Y_SIZE]
715
           mov    [ecx+12],eax
716
;set window state to 'normal' (non-minimized/maximized/rolled-up) state
717
           mov    [ecx+WDATA.fl_wstate],WSTATE_NORMAL
718
;set cr3 register in TSS of application
719
720
 
721
           shl    ecx,8
722
           mov    eax,[PROC_BASE+0xB8+ecx]
723
           ;or     eax,  PG_NOCACHE
724
           mov    [l.cr3],eax
725
726
 
727
           mov    [l.eip],eax           ;set eip in TSS
728
           mov    eax,[app_esp]
729
           mov    [l.esp],eax           ;set stack in TSS
730
731
 
732
           mov    ax,app_code           ;ax - selector of code segment
733
           mov    [l.cs],ax
734
           mov    ax,app_data
735
           mov    [l.ss],ax
736
           mov    [l.ds],ax
737
           mov    [l.es],ax
738
           mov    [l.fs],ax
739
           mov    ax,graph_data         ;ax - selector of graphic segment
740
           mov    [l.gs],ax
741
           mov    [l.io],word 128
742
           mov    [l.eflags],dword 0x3202
743
744
 
745
           mov    ebx,[slot]
746
           shl    ebx,12
747
           add    ebx,sysint_stack_data+4096
748
           mov    [l.esp0],ebx
749
750
 
751
           mov    eax,tss_sceleton
752
           mov    ebx,[slot]
753
           imul   ebx,tss_step
754
           add    ebx,tss_data          ;ebx - address of application TSS
755
           mov    ecx,120
756
           call   memmove
757
758
 
759
           or     eax,-1
760
           mov    edi,[slot]
761
           imul   edi,tss_step
762
           add    edi,tss_data+128
763
           mov    ecx,2048
764
           cld
765
           rep    stosd                 ;full access to 2048*8=16384 ports
766
767
 
768
           mov    edi,[slot]
769
           shl    edi,3
770
;set TSS descriptor
771
           mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
772
           mov    [edi+gdts+tss0+2],cx  ;part of offset
773
           mov    eax,ecx
774
           shr    eax,16
775
           mov    [edi+gdts+tss0+4],al  ;part of offset
776
           mov    [edi+gdts+tss0+7],ah  ;part of offset
777
           mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
778
779
 
780
           mov    [KEY_COUNT],byte 0
781
           mov    [BTN_COUNT],byte 0
782
783
 
784
           shl    edi,5
785
           add    edi,window_data
786
           mov    ebx,[slot]
787
           movzx  esi,word [WIN_STACK+ebx*2]
788
           lea    esi,[WIN_POS+esi*2]
789
           call   windowactivate        ;gui initialization
790
791
 
792
           shl    ebx,5
793
           mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
794
; set if debuggee
795
           mov eax, [flags]
796
           test   byte [flags], 1
797
           jz     .no_debug
798
           mov    [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
799
           mov    eax,[CURRENT_TASK]
800
           mov    [PROC_BASE+ebx*8+0xac],eax ;set debugger PID - current
801
.no_debug:
802
803
 
804
           call   sys_msg_board_str     ;output information about succefull startup
805
806
 
807
endp
808
809
 
810
;Input:
811
;  eax - pid of process
812
;Output:
813
;  eax - slot of process or 0 if process don't exists
814
;Search process by PID.
815
    push   ebx
816
    push   ecx
817
    mov    ebx,[TASK_COUNT]
818
    shl    ebx,5
819
    mov    ecx,2*32
820
821
 
822
;ecx=offset of current process info entry
823
;ebx=maximum permitted offset
824
    cmp    byte [CURRENT_TASK+ecx+0xa],9
825
    jz     .endloop              ;skip empty slots
826
    cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
827
    jz     .pid_found
828
.endloop:
829
    add    ecx,32
830
    cmp    ecx,ebx
831
    jle    .loop
832
833
 
834
    pop    ebx
835
    xor    eax,eax
836
    ret
837
838
 
839
    shr    ecx,5
840
    mov    eax,ecx               ;convert offset to index of slot
841
    pop    ecx
842
    pop    ebx
843
    ret
844
845
 
846
;input:
847
;  ebx - start of buffer
848
;  ecx - size of buffer
849
;result:
850
;  eax = 1 region lays in app memory
851
;  eax = 0 region don't lays in app memory
852
     mov  eax,[CURRENT_TASK]
853
     jmp  check_process_region
854
;-----------------------------------------------------------------------------
855
check_process_region:
856
;input:
857
;  eax - slot
858
;  ebx - start of buffer
859
;  ecx - size of buffer
860
;result:
861
;  eax = 1 region lays in app memory
862
;  eax = 0 region don't lays in app memory
863
864
 
865
     jle  .ok
866
     shl  eax,5
867
     cmp  word [CURRENT_TASK+eax+0xa],0
868
     jnz  .failed
869
     shl  eax,3
870
     mov  eax,[PROC_BASE+eax+0xb8]
871
     test eax,eax
872
     jz   .failed
873
874
 
875
     ret
876
877
 
878
 
879
;    push ebx
880
;    push ecx
881
;    push edx
882
;    mov  edx,ebx
883
;    and  edx,not (4096-1)
884
;    sub  ebx,edx
885
;    add  ecx,ebx
886
;    mov  ebx,edx
887
;    add  ecx,(4096-1)
888
;    and  ecx,not (4096-1)
889
;.loop:
890
;;eax - linear address of page directory
891
;;ebx - current page
892
;;ecx - current size
893
;    mov  edx,ebx
894
;    shr  edx,22
895
;    mov  edx,[eax+4*edx]
896
;    and  edx,not (4096-1)
897
;    test edx,edx
898
;    jz   .failed1
899
;    push eax
900
;    mov  eax,edx
901
;    call MEM_Get_Linear_Address
902
;    mov  edx,ebx
903
;    shr  edx,12
904
;    and  edx,(1024-1)
905
;    mov  eax,[eax+4*edx]
906
;    and  eax,not (4096-1)
907
;    test eax,eax
908
;    pop  eax
909
;    jz   .failed1
910
;    add  ebx,4096
911
;    sub  ecx,4096
912
;    jg   .loop
913
;    pop  edx
914
;    pop  ecx
915
;    pop  ebx
916
.ok:
917
    mov  eax,1
918
    ret
919
;
920
;.failed1:
921
;    pop  edx
922
;    pop  ecx
923
;    pop  ebx
924
.failed:
925
    xor  eax,eax
926
    ret
927
928
 
929
proc read_process_memory
930
;Input:
931
;  eax - process slot
932
;  ebx - buffer address
933
;  ecx - buffer size
934
;  edx - start address in other process
935
;Output:
936
;  eax - number of bytes read.
937
           locals
938
             slot       dd ?
939
             buff       dd ?
940
             r_count    dd ?
941
             offset     dd ?
942
             tmp_r_cnt  dd ?
943
           endl
944
945
 
946
           mov [buff], ebx
947
           mov [r_count], ecx
948
           mov [tmp_r_cnt], ecx
949
           mov [offset], edx
950
951
 
952
.read_mem:
953
           mov edx, [offset]
954
           mov ebx, [tmp_r_cnt]
955
956
 
957
           and edx, 0x3FFFFF
958
           sub ecx, edx
959
           cmp ecx, ebx
960
           jbe @f
961
           mov ecx, ebx
962
@@:
963
           cmp ecx, 0x8000
964
           jna @F
965
           mov ecx, 0x8000
966
@@:
967
           mov eax, [slot]
968
           shl  eax,8
969
           mov ebx, [offset]
970
           add ebx, new_app_base
971
           push ecx
972
           stdcall map_memEx, [proc_mem_map],\
973
                              [PROC_BASE+eax+0xB8],\
974
                              ebx, ecx
975
           pop ecx
976
977
 
978
           and esi, 0xfff
979
           add esi, [proc_mem_map]
980
           mov edi, [buff]
981
           mov edx, ecx
982
           rep movsb
983
984
 
985
           sub [tmp_r_cnt], edx
986
           jnz .read_mem
987
988
 
989
           mov eax, [r_count]
990
           ret
991
992
 
993
994
 
995
proc write_process_memory
996
;Input:
997
;  eax - process slot
998
;  ebx - buffer address
999
;  ecx - buffer size
1000
;  edx - start address in other process
1001
;Output:
1002
;  eax - number of bytes written
1003
1004
 
1005
             slot       dd ?
1006
             buff       dd ?
1007
             w_count    dd ?
1008
             offset     dd ?
1009
             tmp_w_cnt  dd ?
1010
           endl
1011
1012
 
1013
           mov [buff], ebx
1014
           mov [w_count], ecx
1015
           mov [tmp_w_cnt], ecx
1016
           mov [offset], edx
1017
1018
 
1019
.read_mem:
1020
           mov edx, [offset]
1021
           mov ebx, [tmp_w_cnt]
1022
1023
 
1024
           and edx, 0x3FFFFF
1025
           sub ecx, edx
1026
           cmp ecx, ebx
1027
           jbe @f
1028
           mov ecx, ebx
1029
@@:
1030
           cmp ecx, 0x8000
1031
           jna @F
1032
           mov ecx, 0x8000
1033
@@:
1034
           mov eax, [slot]
1035
           shl  eax,8
1036
           mov ebx, [offset]
1037
           add ebx, new_app_base
1038
           push ecx
1039
           stdcall map_memEx, [proc_mem_map],\
1040
                              [PROC_BASE+eax+0xB8],\
1041
                              ebx, ecx
1042
           pop ecx
1043
1044
 
1045
           and edi, 0xfff
1046
           add edi, [proc_mem_map]
1047
           mov esi, [buff]
1048
           mov edx, ecx
1049
           rep movsb
1050
1051
 
1052
           sub [tmp_w_cnt], edx
1053
           jnz .read_mem
1054
1055
 
1056
           mov eax, [w_count]
1057
           ret
1058
endp
1059
1060
 
1061
 
1062
proc new_sys_threads
1063
           locals
1064
             thread_start  dd ?
1065
             thread_stack  dd ?
1066
             params        dd ?
1067
             slot          dd ?
1068
           endl
1069
1070
 
1071
           mov [thread_stack], ecx
1072
           mov [params], 0
1073
1074
 
1075
1076
 
1077
           jnz    .failed                  ;other subfunctions
1078
           mov    esi,new_process_loading
1079
           call   sys_msg_board_str
1080
1081
 
1082
           cmp [application_table_status],0
1083
           je .get_lock
1084
           call   change_task
1085
           jmp .wait_lock
1086
1087
 
1088
           mov eax, 1
1089
           xchg eax, [application_table_status]
1090
           cmp eax, 0
1091
           jne .wait_lock
1092
1093
 
1094
1095
 
1096
           test eax, eax
1097
           jz .failed
1098
1099
 
1100
1101
 
1102
           mov    [app_i_param],eax
1103
           mov    [app_i_icon],eax
1104
1105
 
1106
           mov ecx, [thread_stack]
1107
1108
 
1109
           mov    [app_esp],ecx
1110
1111
 
1112
           shl    esi,8
1113
           add    esi,PROC_BASE
1114
           mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
1115
1116
 
1117
           shl    edi,8
1118
           add    edi,PROC_BASE
1119
           mov    edx,edi               ;edx=edi - pointer to extended infomation about new thread
1120
           mov    ecx,256/4
1121
           rep    stosd                 ;clean extended information about new thread
1122
           mov    edi,edx
1123
           mov    ecx,11
1124
           rep    movsb                 ;copy process name
1125
           mov    eax,[ebx+0x8c]
1126
           mov    [app_mem],eax         ;set memory size
1127
           mov    eax,[ebx+0xb8]
1128
           mov    [edx+0xb8],eax        ;copy page directory
1129
1130
 
1131
                                        [params], dword 0,dword 0
1132
1133
 
1134
           call   sys_msg_board_str     ;output information about succefull startup
1135
1136
 
1137
           mov    eax,[process_number]  ;set result
1138
           ret
1139
.failed:
1140
           mov    [application_table_status],0
1141
           mov    eax,-1
1142
           ret
1143
endp
1144
1145
 
1146
proc wait_mutex stdcall, mutex:dword
1147
           mov ebx, [mutex]
1148
.wait_lock:
1149
           cmp dword [ebx],0
1150
           je .get_lock
1151
           push ebx
1152
           call change_task
1153
           pop ebx
1154
           jmp .wait_lock
1155
1156
 
1157
           mov eax, 1
1158
           xchg eax, [ebx]
1159
           test eax, eax
1160
           jnz .wait_lock
1161
           ret
1162
endp
1163
1164
 
1165
 
1166
1167
 
1168
    new_process_loading db 'K : New Process - loading',13,10,0
1169
    new_process_running db 'K : New Process - done',13,10,0
1170
    start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
1171
endg
1172
>
1173