Subversion Repositories Kolibri OS

Rev

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

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