Subversion Repositories Kolibri OS

Rev

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

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