Subversion Repositories Kolibri OS

Rev

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