Subversion Repositories Kolibri OS

Rev

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