Subversion Repositories Kolibri OS

Rev

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