Subversion Repositories Kolibri OS

Rev

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

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