Subversion Repositories Kolibri OS

Rev

Rev 4459 | 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
align 4
393
alloc_thread_slot:
394
;input:
395
;  none
396
;result:
397
;  eax=[new_thread_slot]<>0 - ok
398
;      0 - failed.
399
;This function find least empty slot.
400
;It doesn't increase [TASK_COUNT]!
401
 
402
 
403
        mov     edx, thr_slot_map
404
        pushfd
405
        cli
406
.l1:
407
        bsf     eax, [edx]
408
        jnz     .found
409
        add     edx, 4
410
        cmp     edx, thr_slot_map+32
411
        jb      .l1
412
 
413
        popfd
414
        xor     eax, eax
415
        ret
416
.found:
417
        btr     [edx], eax
418
        sub     edx, thr_slot_map
419
        lea     eax, [eax+edx*8]
420
        popfd
421
        ret
422
 
423
 
424
align 4
425
proc create_process stdcall, app_size:dword,img_base:dword,img_size:dword
426
       locals
427
         app_pages   dd ?
428
         img_pages   dd ?
429
         process     dd ?
430
         app_tabs    dd ?
431
       endl
432
 
4923 Serge 433
        push    ebx
434
        push    esi
435
        push    edi
436
 
4429 Serge 437
        mov     ecx, pg_data.mutex
438
        call    mutex_lock
439
 
440
        xor     eax, eax
441
        mov     [process], eax
442
 
443
        mov     eax, [app_size]
444
        add     eax, 4095
445
        and     eax, NOT(4095)
446
        mov     [app_size], eax
447
        mov     ebx, eax
448
        shr     eax, 12
449
        mov     [app_pages], eax
450
 
451
        add     ebx, 0x3FFFFF
452
        and     ebx, NOT(0x3FFFFF)
453
        shr     ebx, 22
454
        mov     [app_tabs], ebx
455
 
456
        mov     ecx, [img_size]
457
        add     ecx, 4095
458
        and     ecx, NOT(4095)
459
 
460
        mov     [img_size], ecx
461
        shr     ecx, 12
462
        mov     [img_pages], ecx
463
 
464
        lea     eax, [eax+ebx+2];all requested memory
465
 
466
        cmp     eax, [pg_data.pages_free]
467
        ja      .fail
468
 
469
        stdcall    kernel_alloc, 0x2000
470
        test    eax, eax
471
        jz      .fail
472
        mov     [process], eax
473
 
474
        lea     edi, [eax+PROC.heap_lock]
475
        mov     ecx, (4096-PROC.heap_lock)/4
476
 
477
        list_init eax
478
        add     eax, PROC.thr_list
479
        list_init eax
480
 
481
        xor     eax, eax
482
        cld
483
        rep stosd
484
 
485
        mov     eax, edi
486
        call    get_pg_addr
487
        mov     [edi-4096+PROC.pdt_0_phys], eax
488
 
489
        mov     ecx, (OS_BASE shr 20)/4
490
        xor     eax, eax
491
        rep stosd
492
 
493
        mov     ecx, (OS_BASE shr 20)/4
494
        mov     esi, sys_proc+PROC.pdt_0+(OS_BASE shr 20)
495
        rep movsd
496
 
497
        mov     eax, [edi-8192+PROC.pdt_0_phys]
498
        or      eax, PG_SW
499
        mov     [edi-4096+(page_tabs shr 20)], eax
500
 
501
        lea     eax, [edi-8192]
502
        call    set_cr3
503
 
504
        mov     edx, [app_tabs]
505
        xor     edi, edi
506
@@:
507
        call    alloc_page
508
        test    eax, eax
509
        jz      .fail
510
 
511
        stdcall map_page_table, edi, eax
512
        add     edi, 0x00400000
513
        dec     edx
514
        jnz     @B
515
 
516
        mov     edi, page_tabs
517
 
518
        mov     ecx, [app_tabs]
519
        shl     ecx, 10
520
        xor     eax, eax
521
        rep stosd
522
 
523
        mov     ecx, [img_pages]
524
        mov     ebx, PG_UW
525
        xor     edx, edx
526
        mov     esi, [img_base]
527
        shr     esi, 10
528
        add     esi, page_tabs
529
        mov     edi, page_tabs
530
.remap:
531
        lodsd
532
        and     eax, 0xFFFFF000
533
        or      eax, ebx; force user level r/w access
534
        stosd
535
        add     edx, 0x1000
536
        dec     [app_pages]
537
        dec     ecx
538
        jnz     .remap
539
 
540
        mov     ecx, [app_pages]
541
        test    ecx, ecx
542
        jz      .done
543
 
544
.alloc:
545
        call    alloc_page
546
        test    eax, eax
547
        jz      .fail
548
 
549
        stdcall map_page, edx, eax, dword PG_UW
550
        add     edx, 0x1000
551
        dec     [app_pages]
552
        jnz     .alloc
553
 
554
.done:
555
        mov     ecx, pg_data.mutex
556
        call    mutex_unlock
557
        mov     eax, [process]
4923 Serge 558
 
559
        pop     edi
560
        pop     esi
561
        pop     ebx
4429 Serge 562
        ret
563
.fail:
564
        mov     ecx, pg_data.mutex
565
        call    mutex_unlock
566
        cmp     [process], 0
567
        je      @f
568
;;        stdcall destroy_app_space, [dir_addr], 0
569
@@:
570
        xor     eax, eax
4923 Serge 571
        pop     edi
572
        pop     esi
573
        pop     ebx
4429 Serge 574
        ret
575
endp
576
 
577
align 4
578
set_cr3:
4432 Serge 579
        pushfd
580
        cli
4429 Serge 581
        mov     ebx, [current_slot]
4432 Serge 582
        mov     [current_process], eax
4429 Serge 583
        mov     [ebx+APPDATA.process], eax
584
        mov     eax, [eax+PROC.pdt_0_phys]
585
        mov     cr3, eax
4432 Serge 586
        popfd
4429 Serge 587
        ret
588
 
589
align 4
590
proc destroy_page_table stdcall, pg_tab:dword
591
 
592
        push    esi
593
 
594
        mov     esi, [pg_tab]
595
        mov     ecx, 1024
596
.free:
597
        mov     eax, [esi]
598
        test    eax, 1
599
        jz      .next
4459 Serge 600
        test    eax, 2
601
        jz      .next
4429 Serge 602
        test    eax, 1 shl 9
603
        jnz     .next                     ;skip shared pages
604
        call    free_page
605
.next:
606
        add     esi, 4
607
        dec     ecx
608
        jnz     .free
609
        pop     esi
610
        ret
611
endp
612
 
613
align 4
4459 Serge 614
destroy_process: ;fastcall ecx= ptr to process
4429 Serge 615
 
4459 Serge 616
        lea     eax, [ecx+PROC.thr_list]
617
        cmp     eax, [eax+LHEAD.next]
618
        jne     .exit
4429 Serge 619
 
4459 Serge 620
align 4
621
.internal:
622
        push    ecx
4429 Serge 623
 
4459 Serge 624
        mov     esi, [ecx+PROC.dlls_list_ptr]
625
        call    destroy_all_hdlls
4429 Serge 626
 
4459 Serge 627
;        mov     ecx, pg_data.mutex
628
;        call    mutex_lock
629
 
630
        mov     esi, [esp]
631
        add     esi, PROC.pdt_0
632
        mov     edi, (0x80000000 shr 20)/4
4429 Serge 633
.destroy:
634
        mov     eax, [esi]
635
        test    eax, 1
636
        jz      .next
637
        and     eax, not 0xFFF
638
        stdcall map_page, [tmp_task_ptab], eax, PG_SW
639
        stdcall destroy_page_table, [tmp_task_ptab]
640
        mov     eax, [esi]
641
        call    free_page
642
.next:
643
        add     esi, 4
644
        dec     edi
645
        jnz     .destroy
646
 
4459 Serge 647
        call kernel_free  ;ecx still in stack
648
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
649
 ;       mov     ecx, pg_data.mutex
650
 ;       call    mutex_unlock
651
 
4429 Serge 652
.exit:
653
        ret
654
 
655
align 4
656
get_pid:
657
        mov     eax, [TASK_BASE]
658
        mov     eax, [eax+TASKDATA.pid]
659
        ret
660
 
661
pid_to_slot:
662
;Input:
663
;  eax - pid of process
664
;Output:
665
;  eax - slot of process or 0 if process don't exists
666
;Search process by PID.
667
        push    ebx
668
        push    ecx
669
        mov     ebx, [TASK_COUNT]
670
        shl     ebx, 5
671
        mov     ecx, 2*32
672
 
673
.loop:
674
;ecx=offset of current process info entry
675
;ebx=maximum permitted offset
676
        cmp     byte [CURRENT_TASK+ecx+0xa], 9
677
        jz      .endloop ;skip empty slots
678
        cmp     [CURRENT_TASK+ecx+0x4], eax;check PID
679
        jz      .pid_found
680
.endloop:
681
        add     ecx, 32
682
        cmp     ecx, ebx
683
        jle     .loop
684
 
685
        pop     ecx
686
        pop     ebx
687
        xor     eax, eax
688
        ret
689
 
690
.pid_found:
691
        shr     ecx, 5
692
        mov     eax, ecx ;convert offset to index of slot
693
        pop     ecx
694
        pop     ebx
695
        ret
696
 
697
check_region:
698
;input:
699
;  esi - start of buffer
700
;  edx - size of buffer
701
;result:
702
;  eax = 1 region lays in app memory
703
;  eax = 0 region don't lays in app memory
704
        mov     eax, [CURRENT_TASK]
705
;     jmp  check_process_region
706
;-----------------------------------------------------------------------------
707
;check_process_region:
708
;input:
709
;  eax - slot
710
;  esi - start of buffer
711
;  edx - size of buffer
712
;result:
713
;  eax = 1 region lays in app memory
714
;  eax = 0 region don't lays in app memory
715
 
716
        test    edx, edx
717
        jle     .ok
718
        shl     eax, 5
719
        cmp     word [CURRENT_TASK+eax+0xa], 0
720
        jnz     .failed
721
        shl     eax, 3
722
        mov     eax, [SLOT_BASE+eax+0xb8]
723
        test    eax, eax
724
        jz      .failed
725
 
726
        mov     eax, 1
727
        ret
728
 
729
 
730
;    call MEM_Get_Linear_Address
731
;    push ebx
732
;    push ecx
733
;    push edx
734
;    mov  edx,ebx
735
;    and  edx,not (4096-1)
736
;    sub  ebx,edx
737
;    add  ecx,ebx
738
;    mov  ebx,edx
739
;    add  ecx,(4096-1)
740
;    and  ecx,not (4096-1)
741
;.loop:
742
;;eax - linear address of page directory
743
;;ebx - current page
744
;;ecx - current size
745
;    mov  edx,ebx
746
;    shr  edx,22
747
;    mov  edx,[eax+4*edx]
748
;    and  edx,not (4096-1)
749
;    test edx,edx
750
;    jz   .failed1
751
;    push eax
752
;    mov  eax,edx
753
;    call MEM_Get_Linear_Address
754
;    mov  edx,ebx
755
;    shr  edx,12
756
;    and  edx,(1024-1)
757
;    mov  eax,[eax+4*edx]
758
;    and  eax,not (4096-1)
759
;    test eax,eax
760
;    pop  eax
761
;    jz   .failed1
762
;    add  ebx,4096
763
;    sub  ecx,4096
764
;    jg   .loop
765
;    pop  edx
766
;    pop  ecx
767
;    pop  ebx
768
.ok:
769
        mov     eax, 1
770
        ret
771
;
772
;.failed1:
773
;    pop  edx
774
;    pop  ecx
775
;    pop  ebx
776
.failed:
777
        xor     eax, eax
778
        ret
779
 
780
align 4
781
proc read_process_memory
782
;Input:
783
;  eax - process slot
784
;  ecx - buffer address
785
;  edx - buffer size
786
;  esi - start address in other process
787
;Output:
788
;  eax - number of bytes read.
789
       locals
790
         slot   dd ?
791
         buff   dd ?
792
         r_count    dd ?
793
         offset dd ?
794
         tmp_r_cnt  dd ?
795
       endl
796
 
797
        mov     [slot], eax
798
        mov     [buff], ecx
799
        and     [r_count], 0
800
        mov     [tmp_r_cnt], edx
801
        mov     [offset], esi
802
 
803
        pushad
804
.read_mem:
805
        mov     edx, [offset]
806
        mov     ebx, [tmp_r_cnt]
807
 
808
        mov     ecx, 0x400000
809
        and     edx, 0x3FFFFF
810
        sub     ecx, edx
811
        cmp     ecx, ebx
812
        jbe     @f
813
        mov     ecx, ebx
814
@@:
815
        cmp     ecx, 0x8000
816
        jna     @F
817
        mov     ecx, 0x8000
818
@@:
819
        mov     ebx, [offset]
820
 
821
        push    ecx
822
        stdcall map_memEx, [proc_mem_map], \
823
                [slot], ebx, ecx, PG_MAP
824
        pop     ecx
825
 
826
        mov     esi, [offset]
827
        and     esi, 0xfff
828
        sub     eax, esi
829
        jbe     .ret
830
        cmp     ecx, eax
831
        jbe     @f
832
        mov     ecx, eax
833
        mov     [tmp_r_cnt], eax
834
@@:
835
        add     esi, [proc_mem_map]
836
        mov     edi, [buff]
837
        mov     edx, ecx
838
        rep movsb
839
        add     [r_count], edx
840
 
841
        add     [offset], edx
842
        sub     [tmp_r_cnt], edx
843
        jnz     .read_mem
844
.ret:
845
        popad
846
        mov     eax, [r_count]
847
        ret
848
endp
849
 
850
align 4
851
proc write_process_memory
852
;Input:
853
;  eax - process slot
854
;  ecx - buffer address
855
;  edx - buffer size
856
;  esi - start address in other process
857
;Output:
858
;  eax - number of bytes written
859
 
860
       locals
861
         slot   dd ?
862
         buff   dd ?
863
         w_count    dd ?
864
         offset dd ?
865
         tmp_w_cnt  dd ?
866
       endl
867
 
868
        mov     [slot], eax
869
        mov     [buff], ecx
870
        and     [w_count], 0
871
        mov     [tmp_w_cnt], edx
872
        mov     [offset], esi
873
 
874
        pushad
875
.read_mem:
876
        mov     edx, [offset]
877
        mov     ebx, [tmp_w_cnt]
878
 
879
        mov     ecx, 0x400000
880
        and     edx, 0x3FFFFF
881
        sub     ecx, edx
882
        cmp     ecx, ebx
883
        jbe     @f
884
        mov     ecx, ebx
885
@@:
886
        cmp     ecx, 0x8000
887
        jna     @F
888
        mov     ecx, 0x8000
889
@@:
890
        mov     ebx, [offset]
891
      ;     add ebx, new_app_base
892
        push    ecx
893
        stdcall map_memEx, [proc_mem_map], \
894
                [slot], ebx, ecx, PG_SW
895
        pop     ecx
896
 
897
        mov     edi, [offset]
898
        and     edi, 0xfff
899
        sub     eax, edi
900
        jbe     .ret
901
        cmp     ecx, eax
902
        jbe     @f
903
        mov     ecx, eax
904
        mov     [tmp_w_cnt], eax
905
@@:
906
        add     edi, [proc_mem_map]
907
        mov     esi, [buff]
908
        mov     edx, ecx
909
        rep movsb
910
 
911
        add     [w_count], edx
912
        add     [offset], edx
913
        sub     [tmp_w_cnt], edx
914
        jnz     .read_mem
915
.ret:
916
        popad
917
        mov     eax, [w_count]
918
        ret
919
endp
920
 
921
;ebx = 1 - kernel thread
922
;ecx=thread entry point
923
;edx=thread stack pointer
924
;creation flags  0x01 - debugged
925
;                0x02 - kernel
926
 
927
align 4
928
proc new_sys_threads
929
       locals
930
         slot          dd ?
931
         flags         dd ?
932
         app_cmdline   dd ? ;0x00
933
         app_path      dd ? ;0x04
934
         app_eip       dd ? ;0x08
935
         app_esp       dd ? ;0x0C
936
         app_mem       dd ? ;0x10
937
       endl
938
 
939
        shl     ebx, 1
940
        mov     [flags], ebx
941
 
942
        xor     eax, eax
943
        mov     [app_eip], ecx
944
        mov     [app_cmdline], eax
945
        mov     [app_esp], edx
946
        mov     [app_path], eax
947
 
948
        call    lock_application_table
949
 
950
        call    alloc_thread_slot
951
        test    eax, eax
952
        jz      .failed
953
 
954
        mov     [slot], eax
955
 
956
        mov     esi, [current_slot]
957
        mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
958
 
959
        mov     edi, eax
960
        shl     edi, 8
961
        add     edi, SLOT_BASE
962
        mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
963
        mov     ecx, 256/4
964
        xor     eax, eax
965
        cld
966
        rep stosd             ;clean extended information about new thread
967
        mov     esi, ebx
968
        mov     edi, edx
969
        mov     ecx, 11
970
        rep movsb             ;copy process name
971
 
4430 Serge 972
        mov     eax, [ebx+APPDATA.process]
973
        mov     [edx+APPDATA.process], eax
4429 Serge 974
 
4457 Serge 975
        lea     ebx, [edx+APPDATA.list]
976
        lea     ecx, [eax+PROC.thr_list]
4459 Serge 977
        list_add_tail ebx, ecx               ;add thread to process child's list
4457 Serge 978
 
4429 Serge 979
        mov     eax, [ebx+APPDATA.tls_base]
980
        test    eax, eax
981
        jz      @F
982
 
983
        push    edx
984
        stdcall user_alloc, 4096
985
        pop     edx
986
        test    eax, eax
987
        jz      .failed1;eax=0
988
@@:
989
        mov     [edx+APPDATA.tls_base], eax
990
 
991
        lea     eax, [app_cmdline]
992
        stdcall set_app_params , [slot], eax, dword 0, \
993
                dword 0, [flags]
994
 
995
        mov     eax, [process_number]           ;set result
996
        call    unlock_application_table
997
        ret
998
.failed:
999
        xor     eax, eax
1000
.failed1:
1001
        call    unlock_application_table
1002
        dec     eax     ;-1
1003
        ret
1004
endp
1005
 
1006
align 4
1007
tls_app_entry:
1008
 
1009
        call    init_heap
1010
        stdcall user_alloc, 4096
1011
 
1012
        mov     edx, [current_slot]
1013
        mov     [edx+APPDATA.tls_base], eax
1014
        mov     [tls_data_l+2], ax
1015
        shr     eax, 16
1016
        mov     [tls_data_l+4], al
1017
        mov     [tls_data_l+7], ah
1018
        mov     dx, app_tls
1019
        mov     fs, dx
1020
        popad
1021
        iretd
1022
 
1023
 
1024
EFL_IF      equ 0x0200
1025
EFL_IOPL1   equ 0x1000
1026
EFL_IOPL2   equ 0x2000
1027
EFL_IOPL3   equ 0x3000
1028
 
1029
 
1030
align 4
1031
proc set_app_params stdcall,slot:dword, params:dword,\
1032
            cmd_line:dword, app_path:dword, flags:dword
1033
 
1034
       locals
1035
         pl0_stack dd ?
1036
       endl
1037
 
1038
        stdcall kernel_alloc, RING0_STACK_SIZE+512
1039
        mov     [pl0_stack], eax
1040
 
1041
        lea     edi, [eax+RING0_STACK_SIZE]
1042
 
1043
        mov     eax, [slot]
1044
        mov     ebx, eax
1045
 
1046
        shl     eax, 8
1047
        mov     [eax+SLOT_BASE+APPDATA.fpu_state], edi
1048
        mov     [eax+SLOT_BASE+APPDATA.exc_handler], 0
1049
        mov     [eax+SLOT_BASE+APPDATA.except_mask], 0
1050
        mov     [eax+SLOT_BASE+APPDATA.terminate_protection], 80000001h
1051
 
1052
;set default io permission map
1053
        mov     ecx, [SLOT_BASE+256+APPDATA.io_map]
1054
        mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
1055
        mov     ecx, [SLOT_BASE+256+APPDATA.io_map+4]
1056
        mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
1057
 
1058
        mov     esi, fpu_data
1059
        mov     ecx, 512/4
1060
        rep movsd
1061
 
1062
        cmp     ebx, [TASK_COUNT]
1063
        jle     .noinc
1064
        inc     dword [TASK_COUNT]     ;update number of processes
1065
.noinc:
1066
        shl     ebx, 8
1067
        lea     edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
1068
        mov     [SLOT_BASE+APPDATA.fd_ev+ebx], edx
1069
        mov     [SLOT_BASE+APPDATA.bk_ev+ebx], edx
1070
 
1071
        add     edx, APP_OBJ_OFFSET-APP_EV_OFFSET
1072
        mov     [SLOT_BASE+APPDATA.fd_obj+ebx], edx
1073
        mov     [SLOT_BASE+APPDATA.bk_obj+ebx], edx
1074
 
1075
        mov     ecx, [def_cursor]
1076
        mov     [SLOT_BASE+APPDATA.cursor+ebx], ecx
1077
        mov     eax, [pl0_stack]
1078
        mov     [SLOT_BASE+APPDATA.pl0_stack+ebx], eax
1079
        add     eax, RING0_STACK_SIZE
1080
        mov     [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
1081
 
1082
        push    ebx
1083
        stdcall kernel_alloc, 0x1000
1084
        pop     ebx
1085
        mov     esi, [current_slot]
1086
        mov     esi, [esi+APPDATA.cur_dir]
1087
        mov     ecx, 0x1000/4
1088
        mov     edi, eax
1089
        mov     [ebx+SLOT_BASE+APPDATA.cur_dir], eax
1090
        rep movsd
1091
 
1092
        shr     ebx, 3
1093
        mov     eax, new_app_base
1094
        mov     dword [CURRENT_TASK+ebx+0x10], eax
1095
 
1096
.add_command_line:
1097
        mov     edx, [params]
1098
        mov     edx, [edx] ;app_cmdline
1099
        test    edx, edx
1100
        jz      @f     ;application doesn't need parameters
1101
 
1102
        mov     eax, edx
1103
        add     eax, 256
1104
        jc      @f
1105
 
4430 Serge 1106
;        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1107
;        ja      @f
4429 Serge 1108
 
1109
        mov     eax, [cmd_line]
1110
 
1111
        cmp     [edx], dword 0xffffffff ; extended destination tag
1112
        jne     .no_ext_dest
1113
 
1114
        mov     edx, [edx+4] ; extended destination for cmdline
1115
        jmp     .continue
1116
 
1117
.no_ext_dest:
1118
        mov     [eax-12], dword 255
1119
.continue:
1120
        mov     byte [edx], 0  ;force empty string if no cmdline given
1121
 
1122
        test    eax, eax
1123
        jz      @f
1124
;--------------------------------------
1125
        cmp     [eax-4], dword 0xffffffff ; cmdline_flag
1126
        jne     .old_copy
1127
 
1128
        push    eax
1129
        stdcall strncpy, edx, [eax-8], [eax-12]
1130
        pop     eax
1131
 
1132
        stdcall kernel_free, [eax-8]
1133
        jmp     @f
1134
 
1135
.old_copy:
1136
;--------------------------------------
1137
        stdcall strncpy, edx, eax, 256
1138
@@:
1139
        mov     edx, [params]
1140
        mov     edx, [edx+4];app_path
1141
        test    edx, edx
1142
        jz      @F     ;application don't need path of file
1143
        mov     eax, edx
1144
        add     eax, 1024
1145
        jc      @f
4430 Serge 1146
;        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1147
;        ja      @f
4429 Serge 1148
        stdcall strncpy, edx, [app_path], 1024
1149
@@:
1150
        mov     ebx, [slot]
1151
        mov     eax, ebx
1152
        shl     ebx, 5
1153
        lea     ecx, [draw_data+ebx];ecx - pointer to draw data
1154
 
1155
        mov     edx, irq0.return
1156
        cmp     [ebx*8+SLOT_BASE+APPDATA.tls_base], -1
1157
        jne     @F
1158
        mov     edx, tls_app_entry
1159
@@:
1160
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1161
        mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1162
        mov     [ebx+window_data+WDATA.fl_redraw], 1
1163
        add     ebx, CURRENT_TASK     ;ebx - pointer to information about process
1164
        mov     [ebx+TASKDATA.wnd_number], al;set window number on screen = process slot
1165
 
1166
        mov     [ebx+TASKDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
1167
 
1168
        inc     dword [process_number]
1169
        mov     eax, [process_number]
1170
        mov     [ebx+4], eax    ;set PID
1171
 
1172
;set draw data to full screen
1173
        xor     eax, eax
1174
        mov     [ecx+0], dword eax
1175
        mov     [ecx+4], dword eax
1176
        mov     eax, [Screen_Max_X]
1177
        mov     [ecx+8], eax
1178
        mov     eax, [Screen_Max_Y]
1179
        mov     [ecx+12], eax
1180
 
1181
        mov     ebx, [pl0_stack]
1182
        mov     esi, [params]
1183
        lea     ecx, [ebx+REG_EIP]
1184
        xor     eax, eax
1185
 
1186
        mov     [ebx+REG_RET], edx
1187
        mov     [ebx+REG_EDI], eax
1188
        mov     [ebx+REG_ESI], eax
1189
        mov     [ebx+REG_EBP], eax
1190
        mov     [ebx+REG_ESP], ecx;ebx+REG_EIP
1191
        mov     [ebx+REG_EBX], eax
1192
        mov     [ebx+REG_EDX], eax
1193
        mov     [ebx+REG_ECX], eax
1194
        mov     [ebx+REG_EAX], eax
1195
 
1196
        mov     eax, [esi+0x08]             ;app_eip
1197
        mov     [ebx+REG_EIP], eax          ;app_entry
1198
        mov     [ebx+REG_CS], dword app_code
1199
        mov     ecx, USER_PRIORITY
1200
 
1201
        test    byte [flags], 2
1202
        jz      @F
1203
 
1204
        mov     [ebx+REG_CS], dword os_code ; kernel thread
1205
        mov     ecx, MAX_PRIORITY
1206
 
1207
@@:
1208
        mov     [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
1209
 
1210
        mov     eax, [esi+0x0C]  ;app_esp
1211
        mov     [ebx+REG_APP_ESP], eax;app_stack
1212
        mov     [ebx+REG_SS], dword app_data
1213
 
1214
        lea     edx, [ebx+REG_RET]
1215
        mov     ebx, [slot]
1216
        shl     ebx, 5
1217
        mov     [ebx*8+SLOT_BASE+APPDATA.saved_esp], edx
1218
 
1219
        xor     edx, edx; process state - running
1220
; set if debuggee
1221
        test    byte [flags], 1
1222
        jz      .no_debug
1223
        inc     edx ; process state - suspended
1224
        mov     eax, [CURRENT_TASK]
1225
        mov     [SLOT_BASE+ebx*8+APPDATA.debugger_slot], eax
1226
.no_debug:
1227
        mov     [CURRENT_TASK+ebx+TASKDATA.state], dl
1228
        lea     edx, [SLOT_BASE+ebx*8]
1229
        call    scheduler_add_thread
1230
        ret
1231
endp
1232
 
1233
 
1234
align 4
1235
 
1236
get_stack_base:
1237
        mov     eax, [current_slot]
1238
        mov     eax, [eax+APPDATA.pl0_stack]
1239
        ret
1240
 
1241
 
1242
include "debug.inc"