Subversion Repositories Kolibri OS

Rev

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

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