Subversion Repositories Kolibri OS

Rev

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