Subversion Repositories Kolibri OS

Rev

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