Subversion Repositories Kolibri OS

Rev

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