Subversion Repositories Kolibri OS

Rev

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

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