Subversion Repositories Kolibri OS

Rev

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