Subversion Repositories Kolibri OS

Rev

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

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