Subversion Repositories Kolibri OS

Rev

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