Subversion Repositories Kolibri OS

Rev

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