Subversion Repositories Kolibri OS

Rev

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