Subversion Repositories Kolibri OS

Rev

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