Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2540 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
431 serge 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
2540 hidnplayr 8
$Revision $
593 mikedld 9
 
2540 hidnplayr 10
 
11
GREEDY_KERNEL  equ 0
12
 
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
21
 
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
32
 
33
 
34
struct  APP_PARAMS
35
        app_cmdline     dd ?    ;0x00
36
        app_path        dd ?    ;0x04
37
        app_eip         dd ?    ;0x08
38
        app_esp         dd ?    ;0x0C
39
        app_mem         dd ?    ;0x10
40
ends
41
 
42
macro _clear_ op
43
{  mov ecx, op/4
2382 hidnplayr 44
        xor     eax, eax
45
        cld
46
        rep stosd
237 serge 47
}
48
 
521 diamond 49
fs_execute_from_sysdir:
2382 hidnplayr 50
        xor     ebx, ebx
51
fs_execute_from_sysdir_param:
52
        xor     edx, edx
53
        mov     esi, sysdir_path
363 serge 54
 
164 serge 55
align 4
363 serge 56
proc fs_execute
57
 
58
;fn_read:dword, file_size:dword, cluster:dword
59
 
60
; ebx - cmdline
61
; edx - flags
62
; ebp - full filename
63
; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
64
 
1074 Galkov 65
       locals
66
         cmdline       rd 64    ;256/4
67
         filename      rd 256   ;1024/4
68
         flags     dd ?
363 serge 69
 
1074 Galkov 70
         save_cr3      dd ?
71
         slot      dd ?
72
         slot_base     dd ?
73
         file_base     dd ?
74
         file_size     dd ?
2540 hidnplayr 75
         handle        dd ? ;temp. for default cursor handle for curr. thread
1074 Galkov 76
                      ;app header data
77
         hdr_cmdline   dd ? ;0x00
78
         hdr_path      dd ? ;0x04
79
         hdr_eip       dd ? ;0x08
80
         hdr_esp       dd ? ;0x0C
81
         hdr_mem       dd ? ;0x10
82
         hdr_i_end     dd ? ;0x14
83
       endl
164 serge 84
 
2382 hidnplayr 85
        pushad
363 serge 86
 
2540 hidnplayr 87
        pushad
88
        stdcall set_cursor, [def_cursor_clock]
89
        mov     [handle], eax
90
        mov     [redrawmouse_unconditional], 1
91
        call    __sys_draw_pointer
92
        popad
93
 
2382 hidnplayr 94
        mov     [flags], edx
363 serge 95
 
96
; [ebp]  pointer to filename
97
 
2382 hidnplayr 98
        lea     edi, [filename]
99
        lea     ecx, [edi+1024]
100
        mov     al, '/'
101
        stosb
521 diamond 102
@@:
2382 hidnplayr 103
        cmp     edi, ecx
104
        jae     .bigfilename
105
        lodsb
106
        stosb
107
        test    al, al
108
        jnz     @b
109
        mov     esi, [ebp]
110
        test    esi, esi
111
        jz      .namecopied
112
        mov     byte [edi-1], '/'
521 diamond 113
@@:
2382 hidnplayr 114
        cmp     edi, ecx
115
        jae     .bigfilename
116
        lodsb
117
        stosb
118
        test    al, al
119
        jnz     @b
120
        jmp     .namecopied
521 diamond 121
.bigfilename:
2382 hidnplayr 122
        popad
123
        mov     eax, -ERROR_FILE_NOT_FOUND
1220 serge 124
 
2540 hidnplayr 125
        jmp     .final
126
 
521 diamond 127
.namecopied:
363 serge 128
 
2382 hidnplayr 129
        mov     [cmdline], ebx
130
        test    ebx, ebx
131
        jz      @F
519 serge 132
 
2382 hidnplayr 133
        lea     eax, [cmdline]
134
        mov     dword [eax+252], 0
135
        stdcall strncpy, eax, ebx, 255
519 serge 136
@@:
2382 hidnplayr 137
        lea     eax, [filename]
138
        stdcall load_file, eax
2540 hidnplayr 139
 
2382 hidnplayr 140
        mov     esi, -ERROR_FILE_NOT_FOUND
141
        test    eax, eax
142
        jz      .err_file
237 serge 143
 
2382 hidnplayr 144
        mov     [file_base], eax
145
        mov     [file_size], ebx
237 serge 146
 
2382 hidnplayr 147
        lea     ebx, [hdr_cmdline]
148
        call    test_app_header
149
        mov     esi, -0x1F
150
        test    eax, eax
151
        jz      .err_hdr
237 serge 152
 
153
.wait_lock:
2382 hidnplayr 154
        cmp     [application_table_status], 0
155
        je      .get_lock
156
        call    change_task
157
        jmp     .wait_lock
237 serge 158
 
159
.get_lock:
2382 hidnplayr 160
        mov     eax, 1
161
        xchg    eax, [application_table_status]
162
        test    eax, eax
163
        jnz     .wait_lock
237 serge 164
 
2382 hidnplayr 165
        call    set_application_table_status
237 serge 166
 
2382 hidnplayr 167
        call    get_new_process_place
168
        test    eax, eax
169
        mov     esi, -0x20 ; too many processes
170
        jz      .err
237 serge 171
 
2382 hidnplayr 172
        mov     [slot], eax
173
        shl     eax, 8
174
        add     eax, SLOT_BASE
175
        mov     [slot_base], eax
176
        mov     edi, eax
1074 Galkov 177
       _clear_ 256     ;clean extended information about process
237 serge 178
 
179
; write application name
2382 hidnplayr 180
        lea     eax, [filename]
181
        stdcall strrchr, eax, '/'  ; now eax points to name without path
237 serge 182
 
2382 hidnplayr 183
        lea     esi, [eax+1]
184
        test    eax, eax
185
        jnz     @F
186
        lea     esi, [filename]
237 serge 187
@@:
2382 hidnplayr 188
        mov     ecx, 8; 8 chars for name
189
        mov     edi, [slot_base]
237 serge 190
.copy_process_name_loop:
2382 hidnplayr 191
        lodsb
192
        cmp     al, '.'
193
        jz      .copy_process_name_done
194
        test    al, al
195
        jz      .copy_process_name_done
196
        stosb
197
        loop    .copy_process_name_loop
237 serge 198
.copy_process_name_done:
199
 
2382 hidnplayr 200
        mov     ebx, cr3
201
        mov     [save_cr3], ebx
269 serge 202
 
2382 hidnplayr 203
        stdcall create_app_space, [hdr_mem], [file_base], [file_size]
204
        mov     esi, -30; no memory
205
        test    eax, eax
206
        jz      .failed
237 serge 207
 
2382 hidnplayr 208
        mov     ebx, [slot_base]
209
        mov     [ebx+APPDATA.dir_table], eax
210
        mov     eax, [hdr_mem]
211
        mov     [ebx+APPDATA.mem_size], eax
237 serge 212
 
2382 hidnplayr 213
        xor     edx, edx
214
        cmp     word [6], '02'
215
        jne     @f
1220 serge 216
 
2382 hidnplayr 217
        not     edx
1220 serge 218
@@:
2382 hidnplayr 219
        mov     [ebx+APPDATA.tls_base], edx
1220 serge 220
 
357 serge 221
if GREEDY_KERNEL
222
else
2382 hidnplayr 223
        mov     ecx, [hdr_mem]
224
        mov     edi, [file_size]
225
        add     edi, 4095
226
        and     edi, not 4095
227
        sub     ecx, edi
228
        jna     @F
269 serge 229
 
2382 hidnplayr 230
        xor     eax, eax
231
        cld
232
        rep stosb
247 serge 233
@@:
269 serge 234
end if
235
 
236
; release only virtual space, not phisical memory
237
 
2382 hidnplayr 238
        stdcall free_kernel_space, [file_base]
239
        lea     eax, [hdr_cmdline]
240
        lea     ebx, [cmdline]
241
        lea     ecx, [filename]
242
        stdcall set_app_params , [slot], eax, ebx, ecx, [flags]
237 serge 243
 
2382 hidnplayr 244
        mov     eax, [save_cr3]
245
        call    set_cr3
237 serge 246
 
2382 hidnplayr 247
        xor     ebx, ebx
248
        mov     [application_table_status], ebx;unlock application_table_status mutex
249
        mov     eax, [process_number];set result
2540 hidnplayr 250
 
251
        jmp     .final
252
 
237 serge 253
.failed:
2382 hidnplayr 254
        mov     eax, [save_cr3]
255
        call    set_cr3
269 serge 256
.err:
247 serge 257
.err_hdr:
2382 hidnplayr 258
        stdcall kernel_free, [file_base]
247 serge 259
.err_file:
2382 hidnplayr 260
        xor     eax, eax
261
        mov     [application_table_status], eax
262
        mov     eax, esi
2540 hidnplayr 263
.final:
264
        pushad
265
        stdcall set_cursor, [handle]
266
        mov     [redrawmouse_unconditional], 1
267
        call    __sys_draw_pointer
268
        popad
2382 hidnplayr 269
        ret
237 serge 270
endp
164 serge 271
 
2540 hidnplayr 272
align 4
273
test_app_header:
274
       virtual at eax
275
         APP_HEADER_00 APP_HEADER_00_
276
       end virtual
277
       virtual at eax
278
         APP_HEADER_01 APP_HEADER_01_
279
       end virtual
280
 
281
        cmp     dword [eax], 'MENU'
2382 hidnplayr 282
        jne     .fail
283
        cmp     word [eax+4], 'ET'
284
        jne     .fail
164 serge 285
 
2382 hidnplayr 286
        cmp     [eax+6], word '00'
287
        jne     .check_01_header
164 serge 288
 
2382 hidnplayr 289
        mov     ecx, [APP_HEADER_00.start]
290
        mov     [ebx+0x08], ecx             ;app_eip
291
        mov     edx, [APP_HEADER_00.mem_size]
292
        mov     [ebx+0x10], edx             ;app_mem
293
        shr     edx, 1
294
        sub     edx, 0x10
295
        mov     [ebx+0x0C], edx             ;app_esp
296
        mov     ecx, [APP_HEADER_00.i_param]
297
        mov     [ebx], ecx                  ;app_cmdline
298
        mov     [ebx+4], dword 0            ;app_path
299
        mov     edx, [APP_HEADER_00.i_end]
300
        mov     [ebx+0x14], edx
301
        ret
164 serge 302
 
237 serge 303
 .check_01_header:
164 serge 304
 
2382 hidnplayr 305
        cmp     [eax+6], word '01'
306
        je      @f
307
        cmp     [eax+6], word '02'
308
        jne     .fail
1220 serge 309
@@:
2382 hidnplayr 310
        mov     ecx, [APP_HEADER_01.start]
311
        mov     [ebx+0x08], ecx             ;app_eip
312
        mov     edx, [APP_HEADER_01.mem_size]
381 serge 313
 
314
; \begin{diamond}[20.08.2006]
315
; sanity check (functions 19,58 load app_i_end bytes and that must
316
; fit in allocated memory to prevent kernel faults)
2382 hidnplayr 317
        cmp     edx, [APP_HEADER_01.i_end]
318
        jb      .fail
381 serge 319
; \end{diamond}[20.08.2006]
320
 
2382 hidnplayr 321
        mov     [ebx+0x10], edx             ;app_mem
322
        mov     ecx, [APP_HEADER_01.stack_top]
323
        mov     [ebx+0x0C], ecx             ;app_esp
324
        mov     edx, [APP_HEADER_01.i_param]
325
        mov     [ebx], edx                  ;app_cmdline
326
        mov     ecx, [APP_HEADER_01.i_icon]
327
        mov     [ebx+4], ecx                ;app_path
328
        mov     edx, [APP_HEADER_01.i_end]
329
        mov     [ebx+0x14], edx
330
        ret
237 serge 331
.fail:
2382 hidnplayr 332
        xor     eax, eax
333
        ret
164 serge 334
 
335
align 4
336
proc get_new_process_place
337
;input:
338
;  none
339
;result:
340
;  eax=[new_process_place]<>0 - ok
341
;      0 - failed.
342
;This function find least empty slot.
343
;It doesn't increase [TASK_COUNT]!
2382 hidnplayr 344
        mov     eax, CURRENT_TASK
345
        mov     ebx, [TASK_COUNT]
346
        inc     ebx
347
        shl     ebx, 5
348
        add     ebx, eax    ;ebx - address of process information for (last+1) slot
164 serge 349
.newprocessplace:
350
;eax = address of process information for current slot
2382 hidnplayr 351
        cmp     eax, ebx
352
        jz      .endnewprocessplace ;empty slot after high boundary
353
        add     eax, 0x20
354
        cmp     word [eax+0xa], 9;check process state, 9 means that process slot is empty
355
        jnz     .newprocessplace
164 serge 356
.endnewprocessplace:
2382 hidnplayr 357
        mov     ebx, eax
358
        sub     eax, CURRENT_TASK
359
        shr     eax, 5      ;calculate slot index
360
        cmp     eax, 256
361
        jge     .failed     ;it should be <256
362
        mov     word [ebx+0xa], 9;set process state to 9 (for slot after hight boundary)
363
        ret
164 serge 364
.failed:
2382 hidnplayr 365
        xor     eax, eax
366
        ret
164 serge 367
endp
368
 
369
align 4
269 serge 370
proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
1074 Galkov 371
       locals
372
         app_pages   dd ?
373
         img_pages   dd ?
374
         dir_addr    dd ?
375
         app_tabs    dd ?
376
       endl
164 serge 377
 
2382 hidnplayr 378
        mov     ecx, pg_data.mutex
379
        call    mutex_lock
164 serge 380
 
2382 hidnplayr 381
        xor     eax, eax
382
        mov     [dir_addr], eax
164 serge 383
 
2382 hidnplayr 384
        mov     eax, [app_size]
385
        add     eax, 4095
386
        and     eax, NOT(4095)
387
        mov     [app_size], eax
388
        mov     ebx, eax
389
        shr     eax, 12
390
        mov     [app_pages], eax
170 serge 391
 
2382 hidnplayr 392
        add     ebx, 0x3FFFFF
393
        and     ebx, NOT(0x3FFFFF)
394
        shr     ebx, 22
395
        mov     [app_tabs], ebx
164 serge 396
 
2382 hidnplayr 397
        mov     ecx, [img_size]
398
        add     ecx, 4095
399
        and     ecx, NOT(4095)
164 serge 400
 
2382 hidnplayr 401
        mov     [img_size], ecx
402
        shr     ecx, 12
403
        mov     [img_pages], ecx
164 serge 404
 
2382 hidnplayr 405
if GREEDY_KERNEL
406
        lea     eax, [ecx+ebx+2];only image size
407
else
408
        lea     eax, [eax+ebx+2];all requested memory
409
end if
410
        cmp     eax, [pg_data.pages_free]
411
        ja      .fail
170 serge 412
 
2382 hidnplayr 413
        call    alloc_page
414
        test    eax, eax
415
        jz      .fail
416
        mov     [dir_addr], eax
417
        stdcall map_page, [tmp_task_pdir], eax, dword PG_SW
164 serge 418
 
2382 hidnplayr 419
        mov     edi, [tmp_task_pdir]
420
        mov     ecx, (OS_BASE shr 20)/4
421
        xor     eax, eax
422
        cld
423
        rep stosd
465 serge 424
 
2382 hidnplayr 425
        mov     ecx, (OS_BASE shr 20)/4
426
        mov     esi, sys_pgdir+(OS_BASE shr 20)
427
        rep movsd
164 serge 428
 
2382 hidnplayr 429
        mov     eax, [dir_addr]
430
        or      eax, PG_SW
431
        mov     [edi-4096+(page_tabs shr 20)], eax
378 serge 432
 
2382 hidnplayr 433
        and     eax, -4096
434
        call    set_cr3
164 serge 435
 
2382 hidnplayr 436
        mov     edx, [app_tabs]
437
        mov     edi, new_app_base
164 serge 438
@@:
2382 hidnplayr 439
        call    alloc_page
440
        test    eax, eax
441
        jz      .fail
164 serge 442
 
2382 hidnplayr 443
        stdcall map_page_table, edi, eax
444
        add     edi, 0x00400000
445
        dec     edx
446
        jnz     @B
164 serge 447
 
2382 hidnplayr 448
        mov     edi, new_app_base
449
        shr     edi, 10
450
        add     edi, page_tabs
269 serge 451
 
2382 hidnplayr 452
        mov     ecx, [app_tabs]
453
        shl     ecx, 10
454
        xor     eax, eax
455
        rep stosd
164 serge 456
 
2382 hidnplayr 457
        mov     ecx, [img_pages]
458
        mov     ebx, PG_UW
459
        mov     edx, new_app_base
460
        mov     esi, [img_base]
461
        mov     edi, new_app_base
462
        shr     esi, 10
463
        shr     edi, 10
464
        add     esi, page_tabs
465
        add     edi, page_tabs
269 serge 466
.remap:
2382 hidnplayr 467
        lodsd
468
        or      eax, ebx; force user level r/w access
469
        stosd
470
        add     edx, 0x1000
471
        dec     [app_pages]
472
        dec     ecx
473
        jnz     .remap
269 serge 474
 
2382 hidnplayr 475
        mov     ecx, [app_pages]
476
        test    ecx, ecx
477
        jz      .done
269 serge 478
 
479
if GREEDY_KERNEL
2382 hidnplayr 480
        mov     eax, 0x02
481
        rep stosd
269 serge 482
else
483
 
164 serge 484
.alloc:
2382 hidnplayr 485
        call    alloc_page
486
        test    eax, eax
487
        jz      .fail
164 serge 488
 
2382 hidnplayr 489
        stdcall map_page, edx, eax, dword PG_UW
490
        add     edx, 0x1000
491
        dec     [app_pages]
492
        jnz     .alloc
269 serge 493
end if
164 serge 494
 
269 serge 495
.done:
2382 hidnplayr 496
        stdcall map_page, [tmp_task_pdir], dword 0, dword PG_UNMAP
164 serge 497
 
2382 hidnplayr 498
        mov     ecx, pg_data.mutex
499
        call    mutex_unlock
500
        mov     eax, [dir_addr]
501
        ret
164 serge 502
.fail:
2382 hidnplayr 503
        mov     ecx, pg_data.mutex
504
        call    mutex_unlock
505
        cmp     [dir_addr], 0
506
        je      @f
507
        stdcall destroy_app_space, [dir_addr], 0
164 serge 508
@@:
2382 hidnplayr 509
        xor     eax, eax
510
        ret
164 serge 511
endp
512
 
513
align 4
514
set_cr3:
465 serge 515
 
2382 hidnplayr 516
        mov     ebx, [current_slot]
517
        mov     [ebx+APPDATA.dir_table], eax
518
        mov     cr3, eax
519
        ret
164 serge 520
 
521
align 4
522
proc destroy_page_table stdcall, pg_tab:dword
523
 
2382 hidnplayr 524
        push    esi
164 serge 525
 
2382 hidnplayr 526
        mov     esi, [pg_tab]
527
        mov     ecx, 1024
164 serge 528
.free:
2382 hidnplayr 529
        mov     eax, [esi]
530
        test    eax, 1
531
        jz      .next
532
        test    eax, 1 shl 9
533
        jnz     .next                     ;skip shared pages
534
        call    free_page
164 serge 535
.next:
2382 hidnplayr 536
        add     esi, 4
537
        dec     ecx
538
        jnz     .free
539
        pop     esi
540
        ret
164 serge 541
endp
542
 
543
align 4
1311 diamond 544
proc destroy_app_space stdcall, pg_dir:dword, dlls_list:dword
164 serge 545
 
2382 hidnplayr 546
        xor     edx, edx
547
        push    edx
548
        mov     eax, 0x2
549
        mov     ebx, [pg_dir]
164 serge 550
.loop:
551
;eax = current slot of process
2382 hidnplayr 552
        mov     ecx, eax
553
        shl     ecx, 5
554
        cmp     byte [CURRENT_TASK+ecx+0xa], 9;if process running?
555
        jz      @f           ;skip empty slots
556
        shl     ecx, 3
557
        add     ecx, SLOT_BASE
558
        cmp     [ecx+APPDATA.dir_table], ebx;compare page directory addresses
559
        jnz     @f
560
        mov     [ebp-4], ecx
561
        inc     edx             ;thread found
164 serge 562
@@:
2382 hidnplayr 563
        inc     eax
564
        cmp     eax, [TASK_COUNT]   ;exit loop if we look through all processes
565
        jle     .loop
164 serge 566
 
567
;edx = number of threads
568
;our process is zombi so it isn't counted
2382 hidnplayr 569
        pop     ecx
570
        cmp     edx, 1
571
        jg      .ret
164 serge 572
;if there isn't threads then clear memory.
2382 hidnplayr 573
        mov     esi, [dlls_list]
574
        call    destroy_all_hdlls;ecx=APPDATA
164 serge 575
 
2382 hidnplayr 576
        mov     ecx, pg_data.mutex
577
        call    mutex_lock
1311 diamond 578
 
2382 hidnplayr 579
        mov     eax, [pg_dir]
580
        and     eax, not 0xFFF
581
        stdcall map_page, [tmp_task_pdir], eax, PG_SW
582
        mov     esi, [tmp_task_pdir]
583
        mov     edi, (OS_BASE shr 20)/4
164 serge 584
.destroy:
2382 hidnplayr 585
        mov     eax, [esi]
586
        test    eax, 1
587
        jz      .next
588
        and     eax, not 0xFFF
589
        stdcall map_page, [tmp_task_ptab], eax, PG_SW
590
        stdcall destroy_page_table, [tmp_task_ptab]
591
        mov     eax, [esi]
592
        call    free_page
164 serge 593
.next:
2382 hidnplayr 594
        add     esi, 4
595
        dec     edi
596
        jnz     .destroy
164 serge 597
 
2382 hidnplayr 598
        mov     eax, [pg_dir]
599
        call    free_page
164 serge 600
.exit:
2382 hidnplayr 601
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
602
        stdcall map_page, [tmp_task_pdir], 0, PG_UNMAP
603
        mov     ecx, pg_data.mutex
604
        call    mutex_unlock
1311 diamond 605
.ret:
2382 hidnplayr 606
        ret
164 serge 607
endp
608
 
520 serge 609
align 4
610
get_pid:
2382 hidnplayr 611
        mov     eax, [TASK_BASE]
612
        mov     eax, [eax+TASKDATA.pid]
613
        ret
520 serge 614
 
164 serge 615
pid_to_slot:
616
;Input:
617
;  eax - pid of process
618
;Output:
619
;  eax - slot of process or 0 if process don't exists
620
;Search process by PID.
2382 hidnplayr 621
        push    ebx
622
        push    ecx
623
        mov     ebx, [TASK_COUNT]
624
        shl     ebx, 5
625
        mov     ecx, 2*32
164 serge 626
 
627
.loop:
628
;ecx=offset of current process info entry
629
;ebx=maximum permitted offset
2382 hidnplayr 630
        cmp     byte [CURRENT_TASK+ecx+0xa], 9
631
        jz      .endloop ;skip empty slots
632
        cmp     [CURRENT_TASK+ecx+0x4], eax;check PID
633
        jz      .pid_found
164 serge 634
.endloop:
2382 hidnplayr 635
        add     ecx, 32
636
        cmp     ecx, ebx
637
        jle     .loop
164 serge 638
 
2382 hidnplayr 639
        pop     ecx
640
        pop     ebx
641
        xor     eax, eax
642
        ret
164 serge 643
 
644
.pid_found:
2382 hidnplayr 645
        shr     ecx, 5
646
        mov     eax, ecx ;convert offset to index of slot
647
        pop     ecx
648
        pop     ebx
649
        ret
164 serge 650
 
651
check_region:
652
;input:
1329 Lrz 653
;  esi - start of buffer
654
;  edx - size of buffer
164 serge 655
;result:
656
;  eax = 1 region lays in app memory
657
;  eax = 0 region don't lays in app memory
2382 hidnplayr 658
        mov     eax, [CURRENT_TASK]
1329 Lrz 659
;     jmp  check_process_region
164 serge 660
;-----------------------------------------------------------------------------
1329 Lrz 661
;check_process_region:
164 serge 662
;input:
663
;  eax - slot
1329 Lrz 664
;  esi - start of buffer
665
;  edx - size of buffer
164 serge 666
;result:
667
;  eax = 1 region lays in app memory
668
;  eax = 0 region don't lays in app memory
669
 
2382 hidnplayr 670
        test    edx, edx
671
        jle     .ok
672
        shl     eax, 5
673
        cmp     word [CURRENT_TASK+eax+0xa], 0
674
        jnz     .failed
675
        shl     eax, 3
676
        mov     eax, [SLOT_BASE+eax+0xb8]
677
        test    eax, eax
678
        jz      .failed
164 serge 679
 
2382 hidnplayr 680
        mov     eax, 1
681
        ret
164 serge 682
 
683
 
684
;    call MEM_Get_Linear_Address
685
;    push ebx
686
;    push ecx
687
;    push edx
688
;    mov  edx,ebx
689
;    and  edx,not (4096-1)
690
;    sub  ebx,edx
691
;    add  ecx,ebx
692
;    mov  ebx,edx
693
;    add  ecx,(4096-1)
694
;    and  ecx,not (4096-1)
695
;.loop:
696
;;eax - linear address of page directory
697
;;ebx - current page
698
;;ecx - current size
699
;    mov  edx,ebx
700
;    shr  edx,22
701
;    mov  edx,[eax+4*edx]
702
;    and  edx,not (4096-1)
703
;    test edx,edx
704
;    jz   .failed1
705
;    push eax
706
;    mov  eax,edx
707
;    call MEM_Get_Linear_Address
708
;    mov  edx,ebx
709
;    shr  edx,12
710
;    and  edx,(1024-1)
711
;    mov  eax,[eax+4*edx]
712
;    and  eax,not (4096-1)
713
;    test eax,eax
714
;    pop  eax
715
;    jz   .failed1
716
;    add  ebx,4096
717
;    sub  ecx,4096
718
;    jg   .loop
719
;    pop  edx
720
;    pop  ecx
721
;    pop  ebx
722
.ok:
2382 hidnplayr 723
        mov     eax, 1
724
        ret
164 serge 725
;
726
;.failed1:
727
;    pop  edx
728
;    pop  ecx
729
;    pop  ebx
730
.failed:
2382 hidnplayr 731
        xor     eax, eax
732
        ret
164 serge 733
 
734
align 4
735
proc read_process_memory
736
;Input:
737
;  eax - process slot
1329 Lrz 738
;  ecx - buffer address
739
;  edx - buffer size
740
;  esi - start address in other process
164 serge 741
;Output:
742
;  eax - number of bytes read.
1074 Galkov 743
       locals
744
         slot   dd ?
745
         buff   dd ?
746
         r_count    dd ?
747
         offset dd ?
748
         tmp_r_cnt  dd ?
749
       endl
164 serge 750
 
2382 hidnplayr 751
        mov     [slot], eax
752
        mov     [buff], ecx
753
        and     [r_count], 0
754
        mov     [tmp_r_cnt], edx
755
        mov     [offset], esi
164 serge 756
 
2382 hidnplayr 757
        pushad
164 serge 758
.read_mem:
2382 hidnplayr 759
        mov     edx, [offset]
760
        mov     ebx, [tmp_r_cnt]
164 serge 761
 
2382 hidnplayr 762
        mov     ecx, 0x400000
763
        and     edx, 0x3FFFFF
764
        sub     ecx, edx
765
        cmp     ecx, ebx
766
        jbe     @f
767
        mov     ecx, ebx
164 serge 768
@@:
2382 hidnplayr 769
        cmp     ecx, 0x8000
770
        jna     @F
771
        mov     ecx, 0x8000
164 serge 772
@@:
2382 hidnplayr 773
        mov     ebx, [offset]
1220 serge 774
 
2382 hidnplayr 775
        push    ecx
776
        stdcall map_memEx, [proc_mem_map], \
777
                [slot], ebx, ecx, PG_MAP
778
        pop     ecx
164 serge 779
 
2382 hidnplayr 780
        mov     esi, [offset]
781
        and     esi, 0xfff
782
        sub     eax, esi
783
        jbe     .ret
784
        cmp     ecx, eax
785
        jbe     @f
786
        mov     ecx, eax
787
        mov     [tmp_r_cnt], eax
1314 diamond 788
@@:
2382 hidnplayr 789
        add     esi, [proc_mem_map]
790
        mov     edi, [buff]
791
        mov     edx, ecx
792
        rep movsb
793
        add     [r_count], edx
164 serge 794
 
2382 hidnplayr 795
        add     [offset], edx
796
        sub     [tmp_r_cnt], edx
797
        jnz     .read_mem
1314 diamond 798
.ret:
2382 hidnplayr 799
        popad
800
        mov     eax, [r_count]
801
        ret
164 serge 802
endp
803
 
804
align 4
805
proc write_process_memory
806
;Input:
807
;  eax - process slot
1329 Lrz 808
;  ecx - buffer address
809
;  edx - buffer size
810
;  esi - start address in other process
164 serge 811
;Output:
812
;  eax - number of bytes written
813
 
1074 Galkov 814
       locals
815
         slot   dd ?
816
         buff   dd ?
817
         w_count    dd ?
818
         offset dd ?
819
         tmp_w_cnt  dd ?
820
       endl
164 serge 821
 
2382 hidnplayr 822
        mov     [slot], eax
823
        mov     [buff], ecx
824
        and     [w_count], 0
825
        mov     [tmp_w_cnt], edx
826
        mov     [offset], esi
164 serge 827
 
2382 hidnplayr 828
        pushad
164 serge 829
.read_mem:
2382 hidnplayr 830
        mov     edx, [offset]
831
        mov     ebx, [tmp_w_cnt]
164 serge 832
 
2382 hidnplayr 833
        mov     ecx, 0x400000
834
        and     edx, 0x3FFFFF
835
        sub     ecx, edx
836
        cmp     ecx, ebx
837
        jbe     @f
838
        mov     ecx, ebx
164 serge 839
@@:
2382 hidnplayr 840
        cmp     ecx, 0x8000
841
        jna     @F
842
        mov     ecx, 0x8000
164 serge 843
@@:
2382 hidnplayr 844
        mov     ebx, [offset]
465 serge 845
      ;     add ebx, new_app_base
2382 hidnplayr 846
        push    ecx
847
        stdcall map_memEx, [proc_mem_map], \
848
                [slot], ebx, ecx, PG_SW
849
        pop     ecx
164 serge 850
 
2382 hidnplayr 851
        mov     edi, [offset]
852
        and     edi, 0xfff
853
        sub     eax, edi
854
        jbe     .ret
855
        cmp     ecx, eax
856
        jbe     @f
857
        mov     ecx, eax
858
        mov     [tmp_w_cnt], eax
1314 diamond 859
@@:
2382 hidnplayr 860
        add     edi, [proc_mem_map]
861
        mov     esi, [buff]
862
        mov     edx, ecx
863
        rep movsb
164 serge 864
 
2382 hidnplayr 865
        add     [w_count], edx
866
        add     [offset], edx
867
        sub     [tmp_w_cnt], edx
868
        jnz     .read_mem
1314 diamond 869
.ret:
2382 hidnplayr 870
        popad
871
        mov     eax, [w_count]
872
        ret
164 serge 873
endp
874
 
875
align 4
876
proc new_sys_threads
1074 Galkov 877
       locals
878
         slot      dd ?
879
         app_cmdline   dd ? ;0x00
880
         app_path      dd ? ;0x04
881
         app_eip       dd ? ;0x08
882
         app_esp       dd ? ;0x0C
883
         app_mem       dd ? ;0x10
884
       endl
164 serge 885
 
2382 hidnplayr 886
        cmp     ebx, 1
887
        jne     .failed     ;other subfunctions
164 serge 888
 
2382 hidnplayr 889
        xor     eax, eax
890
        mov     [app_eip], ecx
891
        mov     [app_cmdline], eax
892
        mov     [app_esp], edx
893
        mov     [app_path], eax
1074 Galkov 894
       ;mov    esi,new_process_loading
895
       ;call   sys_msg_board_str
164 serge 896
.wait_lock:
2382 hidnplayr 897
        cmp     [application_table_status], 0
898
        je      .get_lock
899
        call    change_task
900
        jmp     .wait_lock
164 serge 901
 
902
.get_lock:
2382 hidnplayr 903
        mov     eax, 1
904
        xchg    eax, [application_table_status]
905
        test    eax, eax
906
        jnz     .wait_lock
164 serge 907
 
2382 hidnplayr 908
        call    set_application_table_status
164 serge 909
 
2382 hidnplayr 910
        call    get_new_process_place
911
        test    eax, eax
912
        jz      .failed
164 serge 913
 
2382 hidnplayr 914
        mov     [slot], eax
164 serge 915
 
2382 hidnplayr 916
        mov     esi, [current_slot]
917
        mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
164 serge 918
 
2382 hidnplayr 919
        mov     edi, eax
920
        shl     edi, 8
921
        add     edi, SLOT_BASE
922
        mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
923
        mov     ecx, 256/4
924
        xor     eax, eax
925
        cld
926
        rep stosd             ;clean extended information about new thread
927
        mov     esi, ebx
928
        mov     edi, edx
929
        mov     ecx, 11
930
        rep movsb             ;copy process name
164 serge 931
 
2382 hidnplayr 932
        mov     eax, [ebx+APPDATA.heap_base]
933
        mov     [edx+APPDATA.heap_base], eax
164 serge 934
 
2382 hidnplayr 935
        mov     ecx, [ebx+APPDATA.heap_top]
936
        mov     [edx+APPDATA.heap_top], ecx
237 serge 937
 
2382 hidnplayr 938
        mov     eax, [ebx+APPDATA.mem_size]
939
        mov     [edx+APPDATA.mem_size], eax
237 serge 940
 
2382 hidnplayr 941
        mov     ecx, [ebx+APPDATA.dir_table]
942
        mov     [edx+APPDATA.dir_table], ecx;copy page directory
237 serge 943
 
2382 hidnplayr 944
        mov     eax, [ebx+APPDATA.dlls_list_ptr]
945
        mov     [edx+APPDATA.dlls_list_ptr], eax
1311 diamond 946
 
2382 hidnplayr 947
        mov     eax, [ebx+APPDATA.tls_base]
948
        test    eax, eax
949
        jz      @F
1220 serge 950
 
2382 hidnplayr 951
        push    edx
952
        stdcall user_alloc, 4096
953
        pop     edx
954
        test    eax, eax
955
        jz      .failed1;eax=0
1220 serge 956
@@:
2382 hidnplayr 957
        mov     [edx+APPDATA.tls_base], eax
1220 serge 958
 
2382 hidnplayr 959
        lea     eax, [app_cmdline]
960
        stdcall set_app_params , [slot], eax, dword 0, \
961
                dword 0,dword 0
237 serge 962
 
1074 Galkov 963
       ;mov    esi,new_process_running
2382 hidnplayr 964
       ;call   sys_msg_board_str                ;output information about succefull startup
965
        xor     eax, eax
966
        mov     [application_table_status], eax ;unlock application_table_status mutex
967
        mov     eax, [process_number]           ;set result
968
        ret
164 serge 969
.failed:
2382 hidnplayr 970
        xor     eax, eax
1322 Lrz 971
.failed1:
2382 hidnplayr 972
        mov     [application_table_status], eax
973
        dec     eax     ;-1
974
        ret
164 serge 975
endp
976
 
977
align 4
1220 serge 978
tls_app_entry:
979
 
2382 hidnplayr 980
        call    init_heap
1220 serge 981
        stdcall user_alloc, 4096
982
 
2382 hidnplayr 983
        mov     edx, [current_slot]
984
        mov     [edx+APPDATA.tls_base], eax
985
        mov     [tls_data_l+2], ax
986
        shr     eax, 16
987
        mov     [tls_data_l+4], al
988
        mov     [tls_data_l+7], ah
989
        mov     dx, app_tls
990
        mov     fs, dx
1220 serge 991
        popad
992
        iretd
993
 
994
 
1074 Galkov 995
EFL_IF      equ 0x0200
465 serge 996
EFL_IOPL1   equ 0x1000
997
EFL_IOPL2   equ 0x2000
998
EFL_IOPL3   equ 0x3000
999
 
1000
 
237 serge 1001
align 4
1002
proc set_app_params stdcall,slot:dword, params:dword,\
1074 Galkov 1003
            cmd_line:dword, app_path:dword, flags:dword
164 serge 1004
 
1074 Galkov 1005
       locals
1006
         pl0_stack dd ?
1007
       endl
237 serge 1008
 
2382 hidnplayr 1009
        stdcall kernel_alloc, RING0_STACK_SIZE+512
1010
        mov     [pl0_stack], eax
357 serge 1011
 
2382 hidnplayr 1012
        lea     edi, [eax+RING0_STACK_SIZE]
357 serge 1013
 
2382 hidnplayr 1014
        mov     eax, [slot]
1015
        mov     ebx, eax
357 serge 1016
 
2382 hidnplayr 1017
        shl     eax, 8
1018
        mov     [eax+SLOT_BASE+APPDATA.fpu_state], edi
1019
        mov     [eax+SLOT_BASE+APPDATA.exc_handler], 0
1020
        mov     [eax+SLOT_BASE+APPDATA.except_mask], 0
357 serge 1021
 
465 serge 1022
;set default io permission map
2382 hidnplayr 1023
        mov     ecx, [SLOT_BASE+256+APPDATA.io_map]
1024
        mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
1025
        mov     ecx, [SLOT_BASE+256+APPDATA.io_map+4]
1026
        mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
465 serge 1027
 
2382 hidnplayr 1028
        mov     esi, fpu_data
1029
        mov     ecx, 512/4
1030
        rep movsd
237 serge 1031
 
2382 hidnplayr 1032
        cmp     ebx, [TASK_COUNT]
1033
        jle     .noinc
1034
        inc     dword [TASK_COUNT]     ;update number of processes
237 serge 1035
.noinc:
2382 hidnplayr 1036
        shl     ebx, 8
1037
        lea     edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
1038
        mov     [SLOT_BASE+APPDATA.fd_ev+ebx], edx
1039
        mov     [SLOT_BASE+APPDATA.bk_ev+ebx], edx
354 serge 1040
 
2382 hidnplayr 1041
        add     edx, APP_OBJ_OFFSET-APP_EV_OFFSET
1042
        mov     [SLOT_BASE+APPDATA.fd_obj+ebx], edx
1043
        mov     [SLOT_BASE+APPDATA.bk_obj+ebx], edx
237 serge 1044
 
2382 hidnplayr 1045
        mov     ecx, [def_cursor]
1046
        mov     [SLOT_BASE+APPDATA.cursor+ebx], ecx
1047
        mov     eax, [pl0_stack]
1048
        mov     [SLOT_BASE+APPDATA.pl0_stack+ebx], eax
1049
        add     eax, RING0_STACK_SIZE
1050
        mov     [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
281 serge 1051
 
2382 hidnplayr 1052
        push    ebx
1053
        stdcall kernel_alloc, 0x1000
1054
        pop     ebx
1055
        mov     esi, [current_slot]
1056
        mov     esi, [esi+APPDATA.cur_dir]
1057
        mov     ecx, 0x1000/4
1058
        mov     edi, eax
1059
        mov     [ebx+SLOT_BASE+APPDATA.cur_dir], eax
1060
        rep movsd
521 diamond 1061
 
2382 hidnplayr 1062
        shr     ebx, 3
1063
        mov     eax, new_app_base
1064
        mov     dword [CURRENT_TASK+ebx+0x10], eax
237 serge 1065
 
1066
.add_command_line:
2382 hidnplayr 1067
        mov     edx, [params]
1068
        mov     edx, [edx] ;app_cmdline
1069
        test    edx, edx
1070
        jz      @f     ;application doesn't need parameters
281 serge 1071
 
2382 hidnplayr 1072
        mov     eax, edx
1073
        add     eax, 256
1074
        jc      @f
281 serge 1075
 
2382 hidnplayr 1076
        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1077
        ja      @f
281 serge 1078
 
2382 hidnplayr 1079
        mov     byte [edx], 0  ;force empty string if no cmdline given
1080
        mov     eax, [cmd_line]
1081
        test    eax, eax
1082
        jz      @f
1083
        stdcall strncpy, edx, eax, 256
237 serge 1084
@@:
2382 hidnplayr 1085
        mov     edx, [params]
1086
        mov     edx, [edx+4];app_path
1087
        test    edx, edx
1088
        jz      @F     ;application don't need path of file
1089
        mov     eax, edx
1090
        add     eax, 1024
1091
        jc      @f
1092
        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1093
        ja      @f
1094
        stdcall strncpy, edx, [app_path], 1024
237 serge 1095
@@:
2382 hidnplayr 1096
        mov     ebx, [slot]
1097
        mov     eax, ebx
1098
        shl     ebx, 5
1099
        lea     ecx, [draw_data+ebx];ecx - pointer to draw data
465 serge 1100
 
2382 hidnplayr 1101
        mov     edx, irq0.return
1102
        cmp     [ebx*8+SLOT_BASE+APPDATA.tls_base], -1
1103
        jne     @F
1104
        mov     edx, tls_app_entry
1220 serge 1105
@@:
237 serge 1106
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
2382 hidnplayr 1107
        mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1108
        mov     [ebx+window_data+WDATA.fl_redraw], 1
1109
        add     ebx, CURRENT_TASK     ;ebx - pointer to information about process
1110
        mov     [ebx+TASKDATA.wnd_number], al;set window number on screen = process slot
237 serge 1111
 
2382 hidnplayr 1112
        mov     [ebx+TASKDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
237 serge 1113
 
2382 hidnplayr 1114
        inc     dword [process_number]
1115
        mov     eax, [process_number]
1116
        mov     [ebx+4], eax    ;set PID
237 serge 1117
 
1118
;set draw data to full screen
2382 hidnplayr 1119
        xor     eax, eax
1120
        mov     [ecx+0], dword eax
1121
        mov     [ecx+4], dword eax
1122
        mov     eax, [Screen_Max_X]
1123
        mov     [ecx+8], eax
1124
        mov     eax, [Screen_Max_Y]
1125
        mov     [ecx+12], eax
237 serge 1126
 
2382 hidnplayr 1127
        mov     ebx, [pl0_stack]
1128
        mov     esi, [params]
1129
        lea     ecx, [ebx+REG_EIP]
1130
        xor     eax, eax
237 serge 1131
 
2382 hidnplayr 1132
        mov     [ebx+REG_RET], edx
1133
        mov     [ebx+REG_EDI], eax
1134
        mov     [ebx+REG_ESI], eax
1135
        mov     [ebx+REG_EBP], eax
1136
        mov     [ebx+REG_ESP], ecx;ebx+REG_EIP
1137
        mov     [ebx+REG_EBX], eax
1138
        mov     [ebx+REG_EDX], eax
1139
        mov     [ebx+REG_ECX], eax
1140
        mov     [ebx+REG_EAX], eax
237 serge 1141
 
2382 hidnplayr 1142
        mov     eax, [esi+0x08]  ;app_eip
1143
        mov     [ebx+REG_EIP], eax;app_entry
1144
        mov     [ebx+REG_CS], dword app_code
1145
        mov     [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
465 serge 1146
 
2382 hidnplayr 1147
        mov     eax, [esi+0x0C]  ;app_esp
1148
        mov     [ebx+REG_APP_ESP], eax;app_stack
1149
        mov     [ebx+REG_SS], dword app_data
237 serge 1150
 
2382 hidnplayr 1151
        lea     ecx, [ebx+REG_RET]
1152
        mov     ebx, [slot]
1153
        shl     ebx, 5
1154
        mov     [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx
237 serge 1155
 
2382 hidnplayr 1156
        xor     ecx, ecx; process state - running
237 serge 1157
; set if debuggee
2382 hidnplayr 1158
        test    byte [flags], 1
1159
        jz      .no_debug
1160
        inc     ecx ; process state - suspended
1161
        mov     eax, [CURRENT_TASK]
1162
        mov     [SLOT_BASE+ebx*8+APPDATA.debugger_slot], eax
237 serge 1163
.no_debug:
2382 hidnplayr 1164
        mov     [CURRENT_TASK+ebx+TASKDATA.state], cl
1074 Galkov 1165
       ;mov    esi,new_process_running
1166
       ;call   sys_msg_board_str     ;output information about succefull startup
2382 hidnplayr 1167
        ret
237 serge 1168
endp
1169
 
2382 hidnplayr 1170
 
1171
align 4
1172
 
1173
get_stack_base:
1174
        mov     eax, [current_slot]
1175
        mov     eax, [eax+APPDATA.pl0_stack]
1176
        ret
1177
 
1178
 
164 serge 1179
include "debug.inc"