Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
214 serge 1
GREEDY_KERNEL  equ 0
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
 
214 serge 607
           mov edi, [slot]
608
           mov esi, [fpu_data]
211 serge 609
           bt [cpu_caps], CAPS_FXSR
164 serge 610
           jnc .no_SSE
214 serge 611
 
612
           shl edi, 8
613
           mov eax, edi
614
           lea edi, [esi+edi*2]
615
           mov [eax+PROC_BASE+APPDATA.fpu_state], edi
616
           mov [eax+PROC_BASE+APPDATA.fpu_handler], 0
617
           mov [eax+PROC_BASE+APPDATA.sse_handler], 0
618
           mov ecx, 512/4
619
           jmp @F
620
.no_SSE:
621
           mov eax, edi
164 serge 622
           shl eax, 8
214 serge 623
           mov ebx, edi
624
           shl edi, 7
164 serge 625
           shl ebx, 4
214 serge 626
           sub edi, ebx       ;edi*=112
627
           add edi, esi
628
           mov [eax+PROC_BASE+APPDATA.fpu_state], edi
629
           mov [eax+PROC_BASE+APPDATA.fpu_handler], 0
630
           mov [eax+PROC_BASE+APPDATA.sse_handler], 0
631
           mov ecx, 112/4
632
@@:
633
           rep movsd
634
 
164 serge 635
           mov    ebx,[slot]
636
           cmp    ebx,[TASK_COUNT]
637
           jle    .noinc
638
           inc    dword [TASK_COUNT]       ;update number of processes
639
.noinc:
640
           shl    ebx,8
641
           mov    eax,[app_mem]
642
           mov    [PROC_BASE+0x8c+ebx],eax
643
 
644
           shr    ebx,3
645
           mov eax, new_app_base
646
           mov    dword [CURRENT_TASK+ebx+0x10],eax
647
 
648
.add_command_line:
649
           mov    edx,[app_i_param]
650
           test   edx,edx
651
           jz     .no_command_line      ;application don't need parameters
652
           mov    eax,[cmd_line]
653
           test   eax,eax
654
           jz     .no_command_line      ;no parameters specified
655
;calculate parameter length
656
           xor    ecx,ecx
657
.command_line_len:
658
           cmp    byte [eax],0
659
           jz     .command_line_len_end
660
           inc    eax
661
           inc    ecx
662
           cmp    ecx,255
663
           jl     .command_line_len
664
 
665
.command_line_len_end:
666
;ecx - parameter length
667
;edx - address of parameters in new process address space
668
           inc ecx
669
           mov edi, [img_base]
670
           add edi, edx
671
           mov esi, [cmd_line]
672
           rep movsb
673
 
674
.no_command_line:
675
 
676
            mov    edx,[app_i_icon]
677
            test   edx,edx
678
            jz     .no_command_line_1      ;application don't need path of file
679
            mov    esi,[app_path]
680
            test esi, esi
681
            jz     .no_command_line_1      ;application don't need path of file
682
            mov    ecx, 64
683
            mov    edi, [img_base]
684
            add edi, edx
685
            rep movsb
686
 
687
.no_command_line_1:
688
           mov    ebx,[slot]
689
           mov    eax,ebx
690
           shl    ebx,5
202 diamond 691
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
692
        mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
693
        mov     [ebx+window_data+WDATA.fl_redraw], 1
164 serge 694
           add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
202 diamond 695
           mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
164 serge 696
 
202 diamond 697
           mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
164 serge 698
 
699
           inc    dword [process_number]
700
           mov    eax,[process_number]
701
           mov    [ebx+4],eax           ;set PID
702
 
703
           mov    ecx,ebx
704
           add    ecx,(draw_data-CURRENT_TASK)  ;ecx - pointer to draw data
705
;set draw data to full screen
706
 
707
           mov    [ecx+0],dword 0
708
           mov    [ecx+4],dword 0
709
           mov    eax,[SCR_X_SIZE]
710
           mov    [ecx+8],eax
711
           mov    eax,[SCR_Y_SIZE]
712
           mov    [ecx+12],eax
713
;set cr3 register in TSS of application
714
 
715
           mov    ecx,[slot]
716
           shl    ecx,8
717
           mov    eax,[PROC_BASE+0xB8+ecx]
718
           ;or     eax,  PG_NOCACHE
719
           mov    [l.cr3],eax
720
 
721
           mov    eax,[app_start]
722
           mov    [l.eip],eax           ;set eip in TSS
723
           mov    eax,[app_esp]
724
           mov    [l.esp],eax           ;set stack in TSS
725
 
726
;gdt
727
           mov    ax,app_code           ;ax - selector of code segment
728
           mov    [l.cs],ax
729
           mov    ax,app_data
730
           mov    [l.ss],ax
731
           mov    [l.ds],ax
732
           mov    [l.es],ax
733
           mov    [l.fs],ax
734
           mov    ax,graph_data         ;ax - selector of graphic segment
735
           mov    [l.gs],ax
736
           mov    [l.io],word 128
172 serge 737
           mov    [l.eflags],dword 0x1202
164 serge 738
 
739
           mov    [l.ss0],os_data
740
           mov    ebx,[slot]
741
           shl    ebx,12
742
           add    ebx,sysint_stack_data+4096
743
           mov    [l.esp0],ebx
744
 
745
;copy tss to it place
746
           mov    eax,tss_sceleton
747
           mov    ebx,[slot]
748
           imul   ebx,tss_step
749
           add    ebx,tss_data          ;ebx - address of application TSS
750
           mov    ecx,120
751
           call   memmove
752
 
753
;Add IO access table - bit array of permitted ports
754
           or     eax,-1
755
           mov    edi,[slot]
756
           imul   edi,tss_step
757
           add    edi,tss_data+128
758
           mov    ecx,2048
759
           cld
760
           rep    stosd                 ;full access to 2048*8=16384 ports
761
 
762
           mov    ecx,ebx               ;ecx - address of application TSS
763
           mov    edi,[slot]
764
           shl    edi,3
765
;set TSS descriptor
766
           mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
767
           mov    [edi+gdts+tss0+2],cx  ;part of offset
768
           mov    eax,ecx
769
           shr    eax,16
770
           mov    [edi+gdts+tss0+4],al  ;part of offset
771
           mov    [edi+gdts+tss0+7],ah  ;part of offset
772
           mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
773
 
774
;flush keyboard and buttons queue
775
           mov    [KEY_COUNT],byte 0
776
           mov    [BTN_COUNT],byte 0
777
 
778
           mov    edi,[slot]
779
           shl    edi,5
780
           add    edi,window_data
781
           mov    ebx,[slot]
782
           movzx  esi,word [WIN_STACK+ebx*2]
783
           lea    esi,[WIN_POS+esi*2]
784
           call   windowactivate        ;gui initialization
785
 
786
           mov    ebx,[slot]
787
           shl    ebx,5
788
           mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
789
; set if debuggee
790
           mov eax, [flags]
791
           test   byte [flags], 1
792
           jz     .no_debug
793
           mov    [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
794
           mov    eax,[CURRENT_TASK]
795
           mov    [PROC_BASE+ebx*8+0xac],eax ;set debugger PID - current
796
.no_debug:
797
 
798
           mov    esi,new_process_running
799
           call   sys_msg_board_str     ;output information about succefull startup
800
 
801
           ret
802
endp
803
 
804
pid_to_slot:
805
;Input:
806
;  eax - pid of process
807
;Output:
808
;  eax - slot of process or 0 if process don't exists
809
;Search process by PID.
810
    push   ebx
811
    push   ecx
812
    mov    ebx,[TASK_COUNT]
813
    shl    ebx,5
814
    mov    ecx,2*32
815
 
816
.loop:
817
;ecx=offset of current process info entry
818
;ebx=maximum permitted offset
819
    cmp    byte [CURRENT_TASK+ecx+0xa],9
820
    jz     .endloop              ;skip empty slots
821
    cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
822
    jz     .pid_found
823
.endloop:
824
    add    ecx,32
825
    cmp    ecx,ebx
826
    jle    .loop
827
 
828
    pop    ecx
829
    pop    ebx
830
    xor    eax,eax
831
    ret
832
 
833
.pid_found:
834
    shr    ecx,5
835
    mov    eax,ecx               ;convert offset to index of slot
836
    pop    ecx
837
    pop    ebx
838
    ret
839
 
840
check_region:
841
;input:
842
;  ebx - start of buffer
843
;  ecx - size of buffer
844
;result:
845
;  eax = 1 region lays in app memory
846
;  eax = 0 region don't lays in app memory
847
     mov  eax,[CURRENT_TASK]
848
     jmp  check_process_region
849
;-----------------------------------------------------------------------------
850
check_process_region:
851
;input:
852
;  eax - slot
853
;  ebx - start of buffer
854
;  ecx - size of buffer
855
;result:
856
;  eax = 1 region lays in app memory
857
;  eax = 0 region don't lays in app memory
858
 
859
     test ecx,ecx
860
     jle  .ok
861
     shl  eax,5
862
     cmp  word [CURRENT_TASK+eax+0xa],0
863
     jnz  .failed
864
     shl  eax,3
865
     mov  eax,[PROC_BASE+eax+0xb8]
866
     test eax,eax
867
     jz   .failed
868
 
869
     mov  eax,1
870
     ret
871
 
872
 
873
;    call MEM_Get_Linear_Address
874
;    push ebx
875
;    push ecx
876
;    push edx
877
;    mov  edx,ebx
878
;    and  edx,not (4096-1)
879
;    sub  ebx,edx
880
;    add  ecx,ebx
881
;    mov  ebx,edx
882
;    add  ecx,(4096-1)
883
;    and  ecx,not (4096-1)
884
;.loop:
885
;;eax - linear address of page directory
886
;;ebx - current page
887
;;ecx - current size
888
;    mov  edx,ebx
889
;    shr  edx,22
890
;    mov  edx,[eax+4*edx]
891
;    and  edx,not (4096-1)
892
;    test edx,edx
893
;    jz   .failed1
894
;    push eax
895
;    mov  eax,edx
896
;    call MEM_Get_Linear_Address
897
;    mov  edx,ebx
898
;    shr  edx,12
899
;    and  edx,(1024-1)
900
;    mov  eax,[eax+4*edx]
901
;    and  eax,not (4096-1)
902
;    test eax,eax
903
;    pop  eax
904
;    jz   .failed1
905
;    add  ebx,4096
906
;    sub  ecx,4096
907
;    jg   .loop
908
;    pop  edx
909
;    pop  ecx
910
;    pop  ebx
911
.ok:
912
    mov  eax,1
913
    ret
914
;
915
;.failed1:
916
;    pop  edx
917
;    pop  ecx
918
;    pop  ebx
919
.failed:
920
    xor  eax,eax
921
    ret
922
 
923
align 4
924
proc read_process_memory
925
;Input:
926
;  eax - process slot
927
;  ebx - buffer address
928
;  ecx - buffer size
929
;  edx - start address in other process
930
;Output:
931
;  eax - number of bytes read.
932
           locals
933
             slot       dd ?
934
             buff       dd ?
935
             r_count    dd ?
936
             offset     dd ?
937
             tmp_r_cnt  dd ?
938
           endl
939
 
940
           mov [slot], eax
941
           mov [buff], ebx
942
           mov [r_count], ecx
943
           mov [tmp_r_cnt], ecx
944
           mov [offset], edx
945
 
946
           pushad
947
.read_mem:
948
           mov edx, [offset]
949
           mov ebx, [tmp_r_cnt]
950
 
951
           mov ecx, 0x400000
952
           and edx, 0x3FFFFF
953
           sub ecx, edx
954
           cmp ecx, ebx
955
           jbe @f
956
           mov ecx, ebx
957
@@:
958
           cmp ecx, 0x8000
959
           jna @F
960
           mov ecx, 0x8000
961
@@:
962
           mov eax, [slot]
963
           shl  eax,8
964
           mov ebx, [offset]
965
           add ebx, new_app_base
966
           push ecx
967
           stdcall map_memEx, [proc_mem_map],\
968
                              [PROC_BASE+eax+0xB8],\
969
                              ebx, ecx
970
           pop ecx
971
 
972
           mov esi, [offset]
973
           and esi, 0xfff
974
           add esi, [proc_mem_map]
975
           mov edi, [buff]
976
           mov edx, ecx
977
           rep movsb
978
 
979
           add [offset], edx
980
           sub [tmp_r_cnt], edx
981
           jnz .read_mem
982
 
983
           popad
984
           mov eax, [r_count]
985
           ret
986
 
987
endp
988
 
989
align 4
990
proc write_process_memory
991
;Input:
992
;  eax - process slot
993
;  ebx - buffer address
994
;  ecx - buffer size
995
;  edx - start address in other process
996
;Output:
997
;  eax - number of bytes written
998
 
999
           locals
1000
             slot       dd ?
1001
             buff       dd ?
1002
             w_count    dd ?
1003
             offset     dd ?
1004
             tmp_w_cnt  dd ?
1005
           endl
1006
 
1007
           mov [slot], eax
1008
           mov [buff], ebx
1009
           mov [w_count], ecx
1010
           mov [tmp_w_cnt], ecx
1011
           mov [offset], edx
1012
 
1013
           pushad
1014
.read_mem:
1015
           mov edx, [offset]
1016
           mov ebx, [tmp_w_cnt]
1017
 
1018
           mov ecx, 0x400000
1019
           and edx, 0x3FFFFF
1020
           sub ecx, edx
1021
           cmp ecx, ebx
1022
           jbe @f
1023
           mov ecx, ebx
1024
@@:
1025
           cmp ecx, 0x8000
1026
           jna @F
1027
           mov ecx, 0x8000
1028
@@:
1029
           mov eax, [slot]
1030
           shl  eax,8
1031
           mov ebx, [offset]
1032
           add ebx, new_app_base
1033
           push ecx
1034
           stdcall map_memEx, [proc_mem_map],\
1035
                              [PROC_BASE+eax+0xB8],\
1036
                              ebx, ecx
1037
           pop ecx
1038
 
1039
           mov edi, [offset]
1040
           and edi, 0xfff
1041
           add edi, [proc_mem_map]
1042
           mov esi, [buff]
1043
           mov edx, ecx
1044
           rep movsb
1045
 
1046
           add [offset], edx
1047
           sub [tmp_w_cnt], edx
1048
           jnz .read_mem
1049
 
1050
           popad
1051
           mov eax, [w_count]
1052
           ret
1053
endp
1054
 
1055
 
1056
align 4
1057
proc new_sys_threads
1058
           locals
1059
             thread_start  dd ?
1060
             thread_stack  dd ?
1061
             params        dd ?
1062
             slot          dd ?
1063
           endl
1064
 
1065
           mov [thread_start], ebx
1066
           mov [thread_stack], ecx
1067
           mov [params], 0
1068
 
1069
           xor    edx,edx      ; flags=0
1070
 
1071
           cmp    eax,1
1072
           jnz    .failed                  ;other subfunctions
1073
           mov    esi,new_process_loading
1074
           call   sys_msg_board_str
1075
 
1076
.wait_lock:
1077
           cmp [application_table_status],0
1078
           je .get_lock
1079
           call   change_task
1080
           jmp .wait_lock
1081
 
1082
.get_lock:
1083
           mov eax, 1
1084
           xchg eax, [application_table_status]
1085
           cmp eax, 0
1086
           jne .wait_lock
1087
 
1088
           call   set_application_table_status
1089
 
1090
           call get_new_process_place
1091
           test eax, eax
1092
           jz .failed
1093
 
1094
           mov [slot], eax
1095
 
1096
           xor    eax,eax
1097
           mov    [app_i_param],eax
1098
           mov    [app_i_icon],eax
1099
 
1100
           mov ebx, [thread_start]
1101
           mov ecx, [thread_stack]
1102
 
1103
           mov    [app_start],ebx
1104
           mov    [app_esp],ecx
1105
 
1106
           mov    esi,[CURRENT_TASK]
1107
           shl    esi,8
1108
           add    esi,PROC_BASE
1109
           mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
1110
 
1111
           mov    edi,[slot]
1112
           shl    edi,8
1113
           add    edi,PROC_BASE
1114
           mov    edx,edi               ;edx=edi - pointer to extended infomation about new thread
1115
           mov    ecx,256/4
1116
           rep    stosd                 ;clean extended information about new thread
1117
           mov    edi,edx
1118
           mov    ecx,11
1119
           rep    movsb                 ;copy process name
198 serge 1120
           mov    eax,[ebx+APPDATA.heap_base]
1121
           mov    [edx+APPDATA.heap_base], eax
1122
           mov    ecx,[ebx+APPDATA.heap_top]
1123
           mov    [edx+APPDATA.heap_top], ecx
1124
           mov    eax,[ebx+APPDATA.mem_size]
164 serge 1125
           mov    [app_mem],eax         ;set memory size
1126
           mov    eax,[ebx+0xb8]
1127
           mov    [edx+0xb8],eax        ;copy page directory
1128
 
1129
           stdcall add_app_parameters, [slot], new_app_base,\
1130
                                        [params], dword 0,dword 0
1131
 
1132
           mov    esi,new_process_running
1133
           call   sys_msg_board_str     ;output information about succefull startup
1134
 
1135
           mov    [application_table_status],0 ;unlock application_table_status mutex
1136
           mov    eax,[process_number]  ;set result
1137
           ret
1138
.failed:
1139
           mov    [application_table_status],0
1140
           mov    eax,-1
1141
           ret
1142
endp
1143
 
1144
align 4
1145
proc wait_mutex stdcall, mutex:dword
1146
           mov ebx, [mutex]
1147
.wait_lock:
1148
           cmp dword [ebx],0
1149
           je .get_lock
1150
           push ebx
1151
           call change_task
1152
           pop ebx
1153
           jmp .wait_lock
1154
 
1155
.get_lock:
1156
           mov eax, 1
1157
           xchg eax, [ebx]
1158
           test eax, eax
1159
           jnz .wait_lock
1160
           ret
1161
endp
1162
 
1163
 
1164
include "debug.inc"
1165
 
1166
iglobal
1167
    new_process_loading db 'K : New Process - loading',13,10,0
1168
    new_process_running db 'K : New Process - done',13,10,0
1169
    start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
1170
endg
1171