Subversion Repositories Kolibri OS

Rev

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

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