Subversion Repositories Kolibri OS

Rev

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

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