Subversion Repositories Kolibri OS

Rev

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