Subversion Repositories Kolibri OS

Rev

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

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