Subversion Repositories Kolibri OS

Rev

Rev 8593 | Rev 8709 | 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: 8671 $
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.
284
;It doesn't increase [TASK_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
486
        mov     ebx, [TASK_COUNT]
487
        shl     ebx, 5
488
        mov     ecx, 2*32
489
 
490
.loop:
491
;ecx=offset of current process info entry
492
;ebx=maximum permitted offset
493
        cmp     byte [CURRENT_TASK+ecx+0xa], 9
494
        jz      .endloop ;skip empty slots
495
        cmp     [CURRENT_TASK+ecx+0x4], eax;check PID
496
        jz      .pid_found
497
.endloop:
498
        add     ecx, 32
499
        cmp     ecx, ebx
500
        jle     .loop
501
 
502
        pop     ecx
503
        pop     ebx
504
        xor     eax, eax
505
        ret
506
 
507
.pid_found:
508
        shr     ecx, 5
509
        mov     eax, ecx ;convert offset to index of slot
510
        pop     ecx
511
        pop     ebx
512
        ret
513
 
514
check_region:
515
;input:
516
;  esi - start of buffer
517
;  edx - size of buffer
518
;result:
519
;  eax = 1 region lays in app memory
520
;  eax = 0 region don't lays in app memory
5130 serge 521
 
522
        mov     eax, 1
523
        ret
524
if 0
2288 clevermous 525
        mov     eax, [CURRENT_TASK]
526
;     jmp  check_process_region
527
;-----------------------------------------------------------------------------
528
;check_process_region:
529
;input:
530
;  eax - slot
531
;  esi - start of buffer
532
;  edx - size of buffer
533
;result:
534
;  eax = 1 region lays in app memory
535
;  eax = 0 region don't lays in app memory
536
 
537
        test    edx, edx
538
        jle     .ok
539
        shl     eax, 5
540
        cmp     word [CURRENT_TASK+eax+0xa], 0
541
        jnz     .failed
542
        shl     eax, 3
543
        mov     eax, [SLOT_BASE+eax+0xb8]
544
        test    eax, eax
545
        jz      .failed
546
 
547
        mov     eax, 1
548
        ret
549
.ok:
550
        mov     eax, 1
551
        ret
552
.failed:
553
        xor     eax, eax
554
        ret
5130 serge 555
end if
2288 clevermous 556
 
557
align 4
558
proc read_process_memory
559
;Input:
560
;  eax - process slot
561
;  ecx - buffer address
562
;  edx - buffer size
563
;  esi - start address in other process
564
;Output:
565
;  eax - number of bytes read.
566
       locals
567
         slot   dd ?
568
         buff   dd ?
569
         r_count    dd ?
570
         offset dd ?
571
         tmp_r_cnt  dd ?
572
       endl
573
 
574
        mov     [slot], eax
575
        mov     [buff], ecx
576
        and     [r_count], 0
577
        mov     [tmp_r_cnt], edx
578
        mov     [offset], esi
579
 
580
        pushad
581
.read_mem:
582
        mov     edx, [offset]
583
        mov     ebx, [tmp_r_cnt]
584
 
585
        mov     ecx, 0x400000
586
        and     edx, 0x3FFFFF
587
        sub     ecx, edx
588
        cmp     ecx, ebx
589
        jbe     @f
590
        mov     ecx, ebx
591
@@:
592
        cmp     ecx, 0x8000
593
        jna     @F
594
        mov     ecx, 0x8000
595
@@:
596
        mov     ebx, [offset]
597
 
598
        push    ecx
599
        stdcall map_memEx, [proc_mem_map], \
5356 serge 600
                [slot], ebx, ecx, PG_READ
2288 clevermous 601
        pop     ecx
602
 
603
        mov     esi, [offset]
604
        and     esi, 0xfff
605
        sub     eax, esi
606
        jbe     .ret
607
        cmp     ecx, eax
608
        jbe     @f
609
        mov     ecx, eax
610
        mov     [tmp_r_cnt], eax
611
@@:
612
        add     esi, [proc_mem_map]
613
        mov     edi, [buff]
614
        mov     edx, ecx
615
        rep movsb
616
        add     [r_count], edx
617
 
618
        add     [offset], edx
619
        sub     [tmp_r_cnt], edx
620
        jnz     .read_mem
621
.ret:
622
        popad
623
        mov     eax, [r_count]
624
        ret
625
endp
626
 
627
align 4
628
proc write_process_memory
629
;Input:
630
;  eax - process slot
631
;  ecx - buffer address
632
;  edx - buffer size
633
;  esi - start address in other process
634
;Output:
635
;  eax - number of bytes written
636
 
637
       locals
638
         slot   dd ?
639
         buff   dd ?
640
         w_count    dd ?
641
         offset dd ?
642
         tmp_w_cnt  dd ?
643
       endl
644
 
645
        mov     [slot], eax
646
        mov     [buff], ecx
647
        and     [w_count], 0
648
        mov     [tmp_w_cnt], edx
649
        mov     [offset], esi
650
 
651
        pushad
652
.read_mem:
653
        mov     edx, [offset]
654
        mov     ebx, [tmp_w_cnt]
655
 
656
        mov     ecx, 0x400000
657
        and     edx, 0x3FFFFF
658
        sub     ecx, edx
659
        cmp     ecx, ebx
660
        jbe     @f
661
        mov     ecx, ebx
662
@@:
663
        cmp     ecx, 0x8000
664
        jna     @F
665
        mov     ecx, 0x8000
666
@@:
667
        mov     ebx, [offset]
668
        push    ecx
669
        stdcall map_memEx, [proc_mem_map], \
5356 serge 670
                [slot], ebx, ecx, PG_SWR
2288 clevermous 671
        pop     ecx
672
 
673
        mov     edi, [offset]
674
        and     edi, 0xfff
675
        sub     eax, edi
676
        jbe     .ret
677
        cmp     ecx, eax
678
        jbe     @f
679
        mov     ecx, eax
680
        mov     [tmp_w_cnt], eax
681
@@:
682
        add     edi, [proc_mem_map]
683
        mov     esi, [buff]
684
        mov     edx, ecx
685
        rep movsb
686
 
687
        add     [w_count], edx
688
        add     [offset], edx
689
        sub     [tmp_w_cnt], edx
690
        jnz     .read_mem
691
.ret:
692
        popad
693
        mov     eax, [w_count]
694
        ret
695
endp
696
 
4105 Serge 697
;ebx = 1 - kernel thread
698
;ecx=thread entry point
699
;edx=thread stack pointer
700
;creation flags  0x01 - debugged
701
;                0x02 - kernel
702
 
2288 clevermous 703
align 4
704
proc new_sys_threads
705
       locals
4105 Serge 706
         slot          dd ?
707
         flags         dd ?
2288 clevermous 708
         app_cmdline   dd ? ;0x00
709
         app_path      dd ? ;0x04
710
         app_eip       dd ? ;0x08
711
         app_esp       dd ? ;0x0C
712
         app_mem       dd ? ;0x10
713
       endl
714
 
4105 Serge 715
        shl     ebx, 1
716
        mov     [flags], ebx
2288 clevermous 717
 
718
        xor     eax, eax
719
        mov     [app_eip], ecx
720
        mov     [app_cmdline], eax
721
        mov     [app_esp], edx
722
        mov     [app_path], eax
4105 Serge 723
 
3534 clevermous 724
        call    lock_application_table
2288 clevermous 725
 
5130 serge 726
        call    alloc_thread_slot
2288 clevermous 727
        test    eax, eax
728
        jz      .failed
729
 
730
        mov     [slot], eax
731
 
732
        mov     esi, [current_slot]
733
        mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
734
 
735
        mov     edi, eax
736
        shl     edi, 8
737
        add     edi, SLOT_BASE
738
        mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
8093 dunkaist 739
        mov     ecx, sizeof.APPDATA/4
2288 clevermous 740
        xor     eax, eax
741
        cld
742
        rep stosd             ;clean extended information about new thread
743
        mov     esi, ebx
744
        mov     edi, edx
745
        mov     ecx, 11
746
        rep movsb             ;copy process name
747
 
748
 
749
        mov     eax, [ebx+APPDATA.tls_base]
750
        test    eax, eax
751
        jz      @F
752
 
753
        push    edx
754
        stdcall user_alloc, 4096
755
        pop     edx
756
        test    eax, eax
757
        jz      .failed1;eax=0
758
@@:
759
        mov     [edx+APPDATA.tls_base], eax
760
 
6090 serge 761
        mov     eax, [ebx+APPDATA.process]
762
        mov     [edx+APPDATA.process], eax
763
 
764
        lea     ebx, [edx+APPDATA.list]
765
        lea     ecx, [eax+PROC.thr_list]
766
        list_add_tail ebx, ecx               ;add thread to process child's list
767
 
2288 clevermous 768
        lea     eax, [app_cmdline]
6333 serge 769
        stdcall set_app_params , [slot], eax, [flags]
2288 clevermous 770
 
771
        mov     eax, [process_number]           ;set result
3534 clevermous 772
        call    unlock_application_table
2288 clevermous 773
        ret
774
.failed:
775
        xor     eax, eax
776
.failed1:
3534 clevermous 777
        call    unlock_application_table
2288 clevermous 778
        dec     eax     ;-1
779
        ret
780
endp
781
 
6333 serge 782
proc map_process_image stdcall, img_size:dword, file_base:dword, file_size:dword
783
 
784
        mov     edx, [img_size]
785
        mov     esi, [file_base]
786
        mov     ecx, [file_size]
787
        add     edx, 4095
788
        add     ecx, 4095
789
        shr     edx, 12        ; total pages
790
        shr     ecx, 12        ; image pages
791
 
792
        mov     edi, page_tabs
793
        shr     esi, 10
794
        add     esi, edi
795
 
796
.map_image:
797
        lodsd
798
        and     eax, -4096
799
        or      eax, PG_UWR
800
        stosd
801
        dec     edx
802
        loop    .map_image
803
 
804
        test    edx, edx
805
        jz      .done
806
.map_bss:
807
        call    alloc_page
808
        test    eax, eax
809
        jz      .fail
810
 
811
        or      eax, PG_UWR
812
        stosd
813
        dec     edx
814
        jnz     .map_bss
815
 
816
        mov     edi, [file_size]
817
        mov     ecx, [img_size]
818
        add     edi, 4095
819
        and     edi, -4096
820
        add     ecx, 4095
821
        and     ecx, -4096
822
        sub     ecx, edi
823
        shr     ecx, 2
824
        xor     eax, eax
825
        rep stosd
826
.done:
827
.fail:
828
        ret
829
endp
830
 
2288 clevermous 831
align 4
6333 serge 832
common_app_entry:
833
        mov     ebp, [current_slot]
834
        mov     ebp, [ebp+APPDATA.exec_params]
835
        test    ebp, ebp
836
        jz      .exit
837
        stdcall map_process_image, [ebp+APP_HDR._emem],\
838
                [ebp+APP_HDR.img_base], [ebp+APP_HDR.img_size]
6502 pathoswith 839
        mov     esi, [ebp+APP_HDR.path_string]
6333 serge 840
        mov     edi, [ebp+APP_HDR.path]
6758 pathoswith 841
        mov     ecx, [ebp+APP_HDR.filename_size]
842
        cmp     ecx, 1023
843
        jc      @f
844
        mov     ecx, 1022
845
@@:
6502 pathoswith 846
        push    esi
847
        test    edi, edi
848
        jz      @f
8593 rgimad 849
        stdcall is_region_userspace, edi, [ebp+APP_HDR.filename_size]
850
        jz      @f
6758 pathoswith 851
        mov     al, '/'
852
        stosb
6502 pathoswith 853
        rep movsb
854
        mov     byte [edi], 0
6338 serge 855
@@:
6502 pathoswith 856
        call    kernel_free
6333 serge 857
        mov     edi, [ebp+APP_HDR.cmdline]
858
        test    edi, edi
859
        jz      .check_tls_header
6502 pathoswith 860
        lea     esi, [ebp+sizeof.APP_HDR]
861
        mov     ecx, [ebp+APP_HDR.cmdline_size]
6333 serge 862
        cmp     ecx, 256
863
        jb      .copy_cmdline
864
        mov     edi, [ebp+APP_HDR._emem]
865
        add     edi, 4095
866
        and     edi, -4096
867
        sub     edi, ecx
868
        dec     edi
869
        cmp     word [6], '00'
6502 pathoswith 870
        jne     @f
6333 serge 871
        mov     [APP_HEADER_00_.i_param], edi
872
        jmp     .copy_cmdline
873
@@:
874
        mov     [APP_HEADER_01_.i_param], edi
875
.copy_cmdline:
8593 rgimad 876
        inc     ecx  ; keep in mind about 0 in the end
877
        stdcall is_region_userspace, edi, ecx
878
        jz      .check_tls_header
879
        dec     ecx
6333 serge 880
        rep movsb
6502 pathoswith 881
        mov     byte [edi], 0
6333 serge 882
.check_tls_header:
883
        cmp     word [6], '02'
8671 Coldy 884
        jne     .try_load_dll ;.cleanup
2288 clevermous 885
        call    init_heap
886
        stdcall user_alloc, 4096
887
        mov     edx, [current_slot]
888
        mov     [edx+APPDATA.tls_base], eax
889
        mov     [tls_data_l+2], ax
890
        shr     eax, 16
891
        mov     [tls_data_l+4], al
892
        mov     [tls_data_l+7], ah
893
        mov     dx, app_tls
8671 Coldy 894
        mov     fs, dx
895
; { Patch by Coldy, For DLL autoload
896
;   if APP_HEADER.version = 2 => lib/load dll.obj & change eip to APP_START_THUNK)
897
.try_load_dll:
898
; TODO: It;s app, not thread?
899
 
900
; Test app header version (
901
        mov     ecx, dword[ebp+APP_HDR.img_base]
902
        ;DEBUGF 1, "K : ecx = %d\n", [ecx+8]
903
        cmp     dword[ecx+8], 2
904
        jne     .cleanup
905
        DEBUGF 1, 'K : App header version 2\n'
906
        ;DEBUGF 1, "K : DLL.OBJ path: %s\n", dll_lib_path
907
        stdcall load_library, dll_lib_path, 0
908
        ;DEBUGF 1, "K : DLL.OBJ exp ptr: %x\n", eax
909
 
910
        ;test eax, eax
911
        ;jnz @f
912
        cmp     eax, 0
913
        jne     @f
914
; Something went wrong
915
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
916
        stdcall kernel_free, ebp
917
        DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!'
918
        mov     ebx, dll_error_msg
919
        mov     ebp, notifyapp
920
        call    fs_execute_from_sysdir_param
921
; Terminate process
922
        call    sys_end
923
 
924
@@:
925
        ; Find base of DLL.OBJ
926
        ;mov     ebx, eax
927
        ;cdq
928
        ;mov     ecx, 0x00000400
929
        ;div     ecx
930
        ;sub     ebx, edx
931
        sub     eax, 4
932
        mov     eax, [eax]
933
        ;DEBUGF 1, "K : DLL.OBJ base ptr: %x\n", eax
934
 
935
; load_library don't map coff header,
936
; so we may change entry point for app APP_START_THUNK
937
; to base of DLL.OBJ
938
 
939
;.change_eip:
940
        mov     ecx, [current_slot]
941
        mov     ecx, [ecx+APPDATA.pl0_stack]
942
        ;DEBUGF 1, "K : EIP = %x\n", ebx
943
        mov     [ecx+REG_EIP], eax
944
 
945
; } End patch by Coldy, For DLL autoload
6333 serge 946
.cleanup:
947
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
948
        stdcall kernel_free, ebp
6345 serge 949
        mov     ebx, [current_slot]
950
        cmp     [ebx+APPDATA.debugger_slot], 0
951
        je      .exit
952
        mov     eax, [TASK_BASE]
953
        mov     [eax+TASKDATA.state], 1
954
        call    change_task
6333 serge 955
.exit:
2288 clevermous 956
        popad
957
        iretd
958
 
7136 dunkaist 959
EFL_IF      = 0x0200
960
EFL_IOPL1   = 0x1000
961
EFL_IOPL2   = 0x2000
962
EFL_IOPL3   = 0x3000
2288 clevermous 963
 
964
align 4
6333 serge 965
proc set_app_params stdcall,slot:dword, params:dword, flags:dword
2288 clevermous 966
 
967
       locals
968
         pl0_stack dd ?
969
       endl
970
 
7124 dunkaist 971
        mov     eax, [xsave_area_size]
972
        add     eax, RING0_STACK_SIZE
973
        stdcall kernel_alloc, eax
2288 clevermous 974
        mov     [pl0_stack], eax
975
 
976
        lea     edi, [eax+RING0_STACK_SIZE]
977
 
978
        mov     eax, [slot]
979
        mov     ebx, eax
980
 
981
        shl     eax, 8
982
        mov     [eax+SLOT_BASE+APPDATA.fpu_state], edi
983
        mov     [eax+SLOT_BASE+APPDATA.exc_handler], 0
984
        mov     [eax+SLOT_BASE+APPDATA.except_mask], 0
3296 clevermous 985
        mov     [eax+SLOT_BASE+APPDATA.terminate_protection], 80000001h
2288 clevermous 986
 
987
;set default io permission map
8093 dunkaist 988
        mov     ecx, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map]
2288 clevermous 989
        mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
8093 dunkaist 990
        mov     ecx, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map+4]
2288 clevermous 991
        mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
992
 
993
        mov     esi, fpu_data
7165 clevermous 994
        mov     ecx, [xsave_area_size]
995
        add     ecx, 3
996
        shr     ecx, 2
2288 clevermous 997
        rep movsd
998
 
7276 dunkaist 999
        cmp     [TASK_COUNT], ebx
1000
        adc     dword [TASK_COUNT], 0   ; update number of processes
2288 clevermous 1001
        shl     ebx, 8
1002
        lea     edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
1003
        mov     [SLOT_BASE+APPDATA.fd_ev+ebx], edx
1004
        mov     [SLOT_BASE+APPDATA.bk_ev+ebx], edx
1005
 
1006
        add     edx, APP_OBJ_OFFSET-APP_EV_OFFSET
1007
        mov     [SLOT_BASE+APPDATA.fd_obj+ebx], edx
1008
        mov     [SLOT_BASE+APPDATA.bk_obj+ebx], edx
1009
 
1010
        mov     ecx, [def_cursor]
1011
        mov     [SLOT_BASE+APPDATA.cursor+ebx], ecx
1012
        mov     eax, [pl0_stack]
1013
        mov     [SLOT_BASE+APPDATA.pl0_stack+ebx], eax
1014
        add     eax, RING0_STACK_SIZE
1015
        mov     [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
1016
 
1017
        push    ebx
6502 pathoswith 1018
        stdcall kernel_alloc, maxPathLength
2288 clevermous 1019
        pop     ebx
1020
        mov     esi, [current_slot]
1021
        mov     esi, [esi+APPDATA.cur_dir]
6502 pathoswith 1022
        mov     ecx, maxPathLength/4
2288 clevermous 1023
        mov     edi, eax
1024
        mov     [ebx+SLOT_BASE+APPDATA.cur_dir], eax
1025
        rep movsd
1026
 
1027
        shr     ebx, 3
6333 serge 1028
        mov     dword [CURRENT_TASK+ebx+0x10], 0
2288 clevermous 1029
 
1030
        mov     ebx, [slot]
1031
        mov     eax, ebx
1032
        shl     ebx, 5
1033
        lea     ecx, [draw_data+ebx];ecx - pointer to draw data
1034
 
1035
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1036
        mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1037
        mov     [ebx+window_data+WDATA.fl_redraw], 1
1038
        add     ebx, CURRENT_TASK     ;ebx - pointer to information about process
1039
        mov     [ebx+TASKDATA.wnd_number], al;set window number on screen = process slot
1040
 
1041
        mov     [ebx+TASKDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
1042
 
1043
        inc     dword [process_number]
1044
        mov     eax, [process_number]
1045
        mov     [ebx+4], eax    ;set PID
1046
 
1047
;set draw data to full screen
1048
        xor     eax, eax
1049
        mov     [ecx+0], dword eax
1050
        mov     [ecx+4], dword eax
5350 serge 1051
        mov     eax, [screen_workarea.right]
2288 clevermous 1052
        mov     [ecx+8], eax
5350 serge 1053
        mov     eax, [screen_workarea.bottom]
2288 clevermous 1054
        mov     [ecx+12], eax
1055
 
1056
        mov     ebx, [pl0_stack]
1057
        mov     esi, [params]
1058
        lea     ecx, [ebx+REG_EIP]
1059
        xor     eax, eax
1060
 
6333 serge 1061
        mov     [ebx+REG_RET], dword common_app_entry
2288 clevermous 1062
        mov     [ebx+REG_EDI], eax
1063
        mov     [ebx+REG_ESI], eax
1064
        mov     [ebx+REG_EBP], eax
1065
        mov     [ebx+REG_ESP], ecx;ebx+REG_EIP
1066
        mov     [ebx+REG_EBX], eax
1067
        mov     [ebx+REG_EDX], eax
1068
        mov     [ebx+REG_ECX], eax
1069
        mov     [ebx+REG_EAX], eax
1070
 
6333 serge 1071
        mov     eax, [esi+APP_HDR.eip]
1072
        mov     [ebx+REG_EIP], eax
2288 clevermous 1073
        mov     [ebx+REG_CS], dword app_code
3534 clevermous 1074
        mov     ecx, USER_PRIORITY
4105 Serge 1075
 
1076
        test    byte [flags], 2
1077
        jz      @F
1078
 
3325 clevermous 1079
        mov     [ebx+REG_CS], dword os_code ; kernel thread
3534 clevermous 1080
        mov     ecx, MAX_PRIORITY
3325 clevermous 1081
@@:
2288 clevermous 1082
        mov     [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
1083
 
6333 serge 1084
        mov     eax, [esi+APP_HDR.esp]
1085
        mov     [ebx+REG_APP_ESP], eax
2288 clevermous 1086
        mov     [ebx+REG_SS], dword app_data
1087
 
3534 clevermous 1088
        lea     edx, [ebx+REG_RET]
2288 clevermous 1089
        mov     ebx, [slot]
1090
        shl     ebx, 5
3534 clevermous 1091
        mov     [ebx*8+SLOT_BASE+APPDATA.saved_esp], edx
2288 clevermous 1092
 
3534 clevermous 1093
        xor     edx, edx; process state - running
2288 clevermous 1094
; set if debuggee
1095
        test    byte [flags], 1
1096
        jz      .no_debug
1097
        mov     eax, [CURRENT_TASK]
1098
        mov     [SLOT_BASE+ebx*8+APPDATA.debugger_slot], eax
1099
.no_debug:
3534 clevermous 1100
        mov     [CURRENT_TASK+ebx+TASKDATA.state], dl
1101
        lea     edx, [SLOT_BASE+ebx*8]
1102
        call    scheduler_add_thread
2288 clevermous 1103
        ret
1104
endp
1105
 
1106
align 4
1107
get_stack_base:
1108
        mov     eax, [current_slot]
1109
        mov     eax, [eax+APPDATA.pl0_stack]
1110
        ret
1111
 
1112
 
1113
include "debug.inc"