Subversion Repositories Kolibri OS

Rev

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