Subversion Repositories Kolibri OS

Rev

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