Subversion Repositories Kolibri OS

Rev

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

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