Subversion Repositories Kolibri OS

Rev

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