Subversion Repositories Kolibri OS

Rev

Rev 887 | Rev 889 | 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: 888 $
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
211
               cld
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
269 serge 419
 
886 serge 420
           mov ecx, [app_tabs]
421
           shl ecx, 10
422
           xor eax, eax
423
           rep stosd
164 serge 424
 
886 serge 425
           mov ecx, [app_pages]
888 serge 426
           xor ebx, ebx
164 serge 427
.alloc:
888 serge 428
           xor ecx, ecx
429
           call @core_alloc@4
886 serge 430
           test eax, eax
431
           jz .fail
164 serge 432
 
888 serge 433
           stdcall map_page,ebx,eax,dword PG_UW
434
           add ebx, 0x1000
886 serge 435
           dec [app_pages]
436
           jnz .alloc
164 serge 437
 
888 serge 438
           mov ecx, [img_size]                 ; FIXME remap md
439
           mov esi, [img_base]
440
           xor edi, edi
441
 
442
           rep movsb
443
 
269 serge 444
.done:
839 serge 445
           dec [pg_data.pg_mutex]
886 serge 446
           mov eax, [dir_addr]
447
           ret
164 serge 448
.fail:
886 serge 449
           dec [pg_data.pg_mutex]
450
           cmp [dir_addr], 0
451
           je @f
452
           stdcall destroy_app_space, [dir_addr]
164 serge 453
@@:
886 serge 454
           xor eax, eax
455
           ret
164 serge 456
endp
457
 
458
align 4
459
set_cr3:
465 serge 460
 
886 serge 461
           mov ebx, [current_slot]
462
           mov [ebx+APPDATA.dir_table], eax
463
           mov cr3, eax
464
           ret
164 serge 465
 
466
align 4
467
proc destroy_page_table stdcall, pg_tab:dword
468
 
888 serge 469
           push ebx
886 serge 470
           push esi
164 serge 471
 
886 serge 472
           mov esi, [pg_tab]
888 serge 473
           mov ebx, 1024
164 serge 474
.free:
888 serge 475
           mov ecx, [esi]
476
           test ecx, 1
886 serge 477
           jz .next
478
 
888 serge 479
           test ecx, 1 shl 9
819 serge 480
           jnz .next                      ;skip shared pages
886 serge 481
 
888 serge 482
           call @core_free@4
164 serge 483
.next:
886 serge 484
           add esi, 4
888 serge 485
           dec ebx
886 serge 486
           jnz .free
487
           pop esi
888 serge 488
           pop ebx
886 serge 489
           ret
164 serge 490
endp
491
 
492
align 4
493
proc destroy_app_space stdcall, pg_dir:dword
494
 
886 serge 495
           mov ebx, pg_data.pg_mutex
496
           call wait_mutex   ;ebx
164 serge 497
 
886 serge 498
           xor   edx,edx
499
           mov   eax,0x2
500
           mov ebx, [pg_dir]
164 serge 501
.loop:
502
;eax = current slot of process
886 serge 503
           mov   ecx,eax
504
           shl   ecx,5
505
           cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
506
           jz    @f                             ;skip empty slots
507
 
508
           shl   ecx,3
509
           cmp   [SLOT_BASE+ecx+0xB8],ebx       ;compare page directory addresses
510
           jnz   @f
511
 
512
           inc   edx                            ;thread found
164 serge 513
@@:
886 serge 514
           inc   eax
515
           cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
516
           jle   .loop
164 serge 517
 
518
;edx = number of threads
519
;our process is zombi so it isn't counted
886 serge 520
           cmp   edx,1
521
           jg    .exit
164 serge 522
;if there isn't threads then clear memory.
523
 
886 serge 524
           mov eax, [pg_dir]
839 serge 525
           and eax, -4096
526
           add eax, OS_BASE
527
           mov [tmp_task_pdir], eax
528
           mov esi, eax
529
           mov edi, (HEAP_BASE shr 20)/4
164 serge 530
.destroy:
886 serge 531
           mov eax, [esi]
532
           test eax, 1
533
           jz .next
534
           and eax, not 0xFFF
839 serge 535
           add eax, OS_BASE
536
 
537
           stdcall destroy_page_table, eax
538
 
888 serge 539
           mov ecx, [esi]
540
           call @core_free@4
164 serge 541
.next:
886 serge 542
           add esi, 4
543
           dec edi
544
           jnz .destroy
164 serge 545
 
888 serge 546
           mov ecx, [pg_dir]
547
           call @core_free@4
164 serge 548
.exit:
886 serge 549
           dec [pg_data.pg_mutex]
550
           ret
164 serge 551
endp
552
 
520 serge 553
align 4
554
get_pid:
886 serge 555
           mov eax, [TASK_BASE]
819 serge 556
           mov eax, [eax+TASKDATA.pid]
886 serge 557
           ret
520 serge 558
 
164 serge 559
pid_to_slot:
560
;Input:
561
;  eax - pid of process
562
;Output:
563
;  eax - slot of process or 0 if process don't exists
564
;Search process by PID.
565
    push   ebx
566
    push   ecx
567
    mov    ebx,[TASK_COUNT]
568
    shl    ebx,5
569
    mov    ecx,2*32
570
 
571
.loop:
572
;ecx=offset of current process info entry
573
;ebx=maximum permitted offset
574
    cmp    byte [CURRENT_TASK+ecx+0xa],9
774 Rus 575
    jz	   .endloop		 ;skip empty slots
164 serge 576
    cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
774 Rus 577
    jz	   .pid_found
164 serge 578
.endloop:
579
    add    ecx,32
580
    cmp    ecx,ebx
581
    jle    .loop
582
 
583
    pop    ecx
584
    pop    ebx
585
    xor    eax,eax
586
    ret
587
 
588
.pid_found:
589
    shr    ecx,5
774 Rus 590
    mov    eax,ecx		 ;convert offset to index of slot
164 serge 591
    pop    ecx
592
    pop    ebx
593
    ret
594
 
595
check_region:
596
;input:
597
;  ebx - start of buffer
598
;  ecx - size of buffer
599
;result:
600
;  eax = 1 region lays in app memory
601
;  eax = 0 region don't lays in app memory
602
     mov  eax,[CURRENT_TASK]
603
     jmp  check_process_region
604
;-----------------------------------------------------------------------------
605
check_process_region:
606
;input:
607
;  eax - slot
608
;  ebx - start of buffer
609
;  ecx - size of buffer
610
;result:
611
;  eax = 1 region lays in app memory
612
;  eax = 0 region don't lays in app memory
613
 
614
     test ecx,ecx
615
     jle  .ok
616
     shl  eax,5
617
     cmp  word [CURRENT_TASK+eax+0xa],0
618
     jnz  .failed
619
     shl  eax,3
380 serge 620
     mov  eax,[SLOT_BASE+eax+0xb8]
164 serge 621
     test eax,eax
622
     jz   .failed
623
 
624
     mov  eax,1
625
     ret
626
 
627
 
628
;    call MEM_Get_Linear_Address
629
;    push ebx
630
;    push ecx
631
;    push edx
632
;    mov  edx,ebx
633
;    and  edx,not (4096-1)
634
;    sub  ebx,edx
635
;    add  ecx,ebx
636
;    mov  ebx,edx
637
;    add  ecx,(4096-1)
638
;    and  ecx,not (4096-1)
639
;.loop:
640
;;eax - linear address of page directory
641
;;ebx - current page
642
;;ecx - current size
643
;    mov  edx,ebx
644
;    shr  edx,22
645
;    mov  edx,[eax+4*edx]
646
;    and  edx,not (4096-1)
647
;    test edx,edx
648
;    jz   .failed1
649
;    push eax
650
;    mov  eax,edx
651
;    call MEM_Get_Linear_Address
652
;    mov  edx,ebx
653
;    shr  edx,12
654
;    and  edx,(1024-1)
655
;    mov  eax,[eax+4*edx]
656
;    and  eax,not (4096-1)
657
;    test eax,eax
658
;    pop  eax
659
;    jz   .failed1
660
;    add  ebx,4096
661
;    sub  ecx,4096
662
;    jg   .loop
663
;    pop  edx
664
;    pop  ecx
665
;    pop  ebx
666
.ok:
667
    mov  eax,1
668
    ret
669
;
670
;.failed1:
671
;    pop  edx
672
;    pop  ecx
673
;    pop  ebx
674
.failed:
675
    xor  eax,eax
676
    ret
677
 
678
align 4
679
proc read_process_memory
680
;Input:
681
;  eax - process slot
682
;  ebx - buffer address
683
;  ecx - buffer size
684
;  edx - start address in other process
685
;Output:
686
;  eax - number of bytes read.
774 Rus 687
	   locals
688
	     slot	dd ?
689
	     buff	dd ?
690
	     r_count	dd ?
691
	     offset	dd ?
692
	     tmp_r_cnt	dd ?
693
	   endl
164 serge 694
 
774 Rus 695
	   mov [slot], eax
696
	   mov [buff], ebx
697
	   and [r_count], 0
698
	   mov [tmp_r_cnt], ecx
699
	   mov [offset], edx
164 serge 700
 
774 Rus 701
	   pushad
164 serge 702
.read_mem:
774 Rus 703
	   mov edx, [offset]
704
	   mov ebx, [tmp_r_cnt]
164 serge 705
 
774 Rus 706
	   mov ecx, 0x400000
707
	   and edx, 0x3FFFFF
708
	   sub ecx, edx
709
	   cmp ecx, ebx
710
	   jbe @f
711
	   mov ecx, ebx
164 serge 712
@@:
774 Rus 713
	   cmp ecx, 0x8000
714
	   jna @F
715
	   mov ecx, 0x8000
164 serge 716
@@:
774 Rus 717
	   mov eax, [slot]
718
	   shl	eax,8
719
	   mov ebx, [offset]
720
	   push ecx
721
	   stdcall map_memEx, [proc_mem_map],\
722
			      [SLOT_BASE+eax+0xB8],\
723
			      ebx, ecx
724
	   pop ecx
164 serge 725
 
774 Rus 726
	   mov esi, [offset]
727
	   and esi, 0xfff
728
	   add esi, [proc_mem_map]
729
	   mov edi, [buff]
730
	   mov edx, ecx
731
	   rep movsb
732
	   add [r_count], edx
164 serge 733
 
774 Rus 734
	   add [offset], edx
735
	   sub [tmp_r_cnt], edx
736
	   jnz .read_mem
164 serge 737
 
774 Rus 738
	   popad
739
	   mov eax, [r_count]
740
	   ret
164 serge 741
endp
742
 
743
align 4
744
proc write_process_memory
745
;Input:
746
;  eax - process slot
747
;  ebx - buffer address
748
;  ecx - buffer size
749
;  edx - start address in other process
750
;Output:
751
;  eax - number of bytes written
752
 
774 Rus 753
	   locals
754
	     slot	dd ?
755
	     buff	dd ?
756
	     w_count	dd ?
757
	     offset	dd ?
758
	     tmp_w_cnt	dd ?
759
	   endl
164 serge 760
 
774 Rus 761
	   mov [slot], eax
762
	   mov [buff], ebx
763
	   and [w_count], 0
764
	   mov [tmp_w_cnt], ecx
765
	   mov [offset], edx
164 serge 766
 
774 Rus 767
	   pushad
164 serge 768
.read_mem:
774 Rus 769
	   mov edx, [offset]
770
	   mov ebx, [tmp_w_cnt]
164 serge 771
 
774 Rus 772
	   mov ecx, 0x400000
773
	   and edx, 0x3FFFFF
774
	   sub ecx, edx
775
	   cmp ecx, ebx
776
	   jbe @f
777
	   mov ecx, ebx
164 serge 778
@@:
774 Rus 779
	   cmp ecx, 0x8000
780
	   jna @F
781
	   mov ecx, 0x8000
164 serge 782
@@:
774 Rus 783
	   mov eax, [slot]
784
	   shl	eax,8
785
	   mov ebx, [offset]
465 serge 786
      ;     add ebx, new_app_base
774 Rus 787
	   push ecx
788
	   stdcall map_memEx, [proc_mem_map],\
789
			      [SLOT_BASE+eax+0xB8],\
790
			      ebx, ecx
791
	   pop ecx
164 serge 792
 
774 Rus 793
	   mov edi, [offset]
794
	   and edi, 0xfff
795
	   add edi, [proc_mem_map]
796
	   mov esi, [buff]
797
	   mov edx, ecx
798
	   rep movsb
164 serge 799
 
774 Rus 800
	   add [w_count], edx
801
	   add [offset], edx
802
	   sub [tmp_w_cnt], edx
803
	   jnz .read_mem
164 serge 804
 
774 Rus 805
	   popad
806
	   mov eax, [w_count]
807
	   ret
164 serge 808
endp
809
 
810
align 4
811
proc new_sys_threads
886 serge 812
           locals
813
             slot      dd ?
814
             app_cmdline   dd ? ;0x00
815
             app_path      dd ? ;0x04
816
             app_eip       dd ? ;0x08
817
             app_esp       dd ? ;0x0C
818
             app_mem       dd ? ;0x10
819
           endl
164 serge 820
 
886 serge 821
           cmp eax,1
822
           jne .failed          ;other subfunctions
164 serge 823
 
886 serge 824
           xor  eax,eax
825
           mov [app_cmdline], eax
826
           mov [app_path], eax
827
           mov [app_eip], ebx
828
           mov [app_esp], ecx
164 serge 829
 
886 serge 830
           ;mov    esi,new_process_loading
831
           ;call   sys_msg_board_str
832
           DEBUGF 1,"%s",new_process_loading
164 serge 833
.wait_lock:
886 serge 834
           cmp [application_table_status],0
835
           je .get_lock
164 serge 836
 
886 serge 837
           call   change_task
838
           jmp .wait_lock
839
 
164 serge 840
.get_lock:
886 serge 841
           mov eax, 1
842
           xchg eax, [application_table_status]
843
           cmp eax, 0
844
           jne .wait_lock
164 serge 845
 
886 serge 846
           call   set_application_table_status
164 serge 847
 
886 serge 848
           call get_new_process_place
849
           test eax, eax
850
           jz .failed
164 serge 851
 
886 serge 852
           mov [slot], eax
164 serge 853
 
886 serge 854
           mov    esi,[current_slot]
855
           mov    ebx,esi         ;ebx=esi - pointer to extended information about current thread
164 serge 856
 
886 serge 857
           mov    edi, eax
858
           shl    edi,8
859
           add    edi,SLOT_BASE
860
           mov    edx,edi         ;edx=edi - pointer to extended infomation about new thread
861
           mov    ecx,256/4
862
           xor eax, eax
863
           cld
864
           rep    stosd           ;clean extended information about new thread
865
           mov    esi,ebx
866
           mov    edi,edx
867
           mov    ecx,11
868
           rep    movsb           ;copy process name
164 serge 869
 
886 serge 870
           mov eax,[ebx+APPDATA.heap_base]
871
           mov [edx+APPDATA.heap_base], eax
164 serge 872
 
886 serge 873
           mov ecx,[ebx+APPDATA.heap_top]
874
           mov [edx+APPDATA.heap_top], ecx
237 serge 875
 
886 serge 876
           mov eax,[ebx+APPDATA.mem_size]
877
           mov [edx+APPDATA.mem_size], eax
237 serge 878
 
886 serge 879
           mov ecx,[ebx+APPDATA.dir_table]
880
           mov [edx+APPDATA.dir_table],ecx  ;copy page directory
237 serge 881
 
886 serge 882
           lea eax, [app_cmdline]
883
           stdcall set_app_params ,[slot],eax,dword 0,\
884
                         dword 0,dword 0
237 serge 885
 
886 serge 886
           ;mov    esi,new_process_running
887
           ;call   sys_msg_board_str     ;output information about succefull startup
888
           DEBUGF 1,"%s",new_process_running
164 serge 889
 
886 serge 890
           mov    [application_table_status],0 ;unlock application_table_status mutex
891
           mov    eax,[process_number]  ;set result
892
           ret
164 serge 893
.failed:
886 serge 894
           mov    [application_table_status],0
895
           mov    eax,-1
896
           ret
164 serge 897
endp
898
 
276 serge 899
; param
900
;  ebx=mutex
901
 
164 serge 902
align 4
276 serge 903
wait_mutex:
886 serge 904
           push eax
905
           push ebx
321 diamond 906
.do_wait:
886 serge 907
           cmp dword [ebx],0
908
           je .get_lock
909
 
910
           call change_task
911
           jmp .do_wait
164 serge 912
.get_lock:
886 serge 913
           mov eax, 1
914
           xchg eax, [ebx]
915
           test eax, eax
916
           jnz .do_wait
164 serge 917
 
886 serge 918
           pop ebx
919
           pop eax
920
           ret
921
 
774 Rus 922
EFL_IF	    equ 0x0200
465 serge 923
EFL_IOPL1   equ 0x1000
924
EFL_IOPL2   equ 0x2000
925
EFL_IOPL3   equ 0x3000
926
 
927
 
237 serge 928
align 4
929
proc set_app_params stdcall,slot:dword, params:dword,\
774 Rus 930
			cmd_line:dword, app_path:dword, flags:dword
164 serge 931
 
886 serge 932
           locals
933
             pl0_stack dd ?
934
           endl
237 serge 935
 
864 serge 936
           mov ecx, 1            ;(RING0_STACK_SIZE+512) shr 12
937
           call @core_alloc@4
839 serge 938
           add eax, OS_BASE
886 serge 939
           mov [pl0_stack], eax
357 serge 940
 
886 serge 941
           lea edi, [eax+RING0_STACK_SIZE]
357 serge 942
 
886 serge 943
           mov eax, [slot]
944
           mov ebx, eax
357 serge 945
 
886 serge 946
           shl eax, 8
947
           mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
948
           mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
949
           mov [eax+SLOT_BASE+APPDATA.sse_handler], 0
357 serge 950
 
465 serge 951
;set default io permission map
886 serge 952
           mov [eax+SLOT_BASE+APPDATA.io_map],\
953
               (tss._io_map_0-OS_BASE+PG_MAP)
954
           mov [eax+SLOT_BASE+APPDATA.io_map+4],\
955
               (tss._io_map_1-OS_BASE+PG_MAP)
465 serge 956
 
886 serge 957
           mov esi, fpu_data
958
           mov ecx, 512/4
959
           rep movsd
237 serge 960
 
886 serge 961
           cmp    ebx,[TASK_COUNT]
962
           jle    .noinc
963
           inc    dword [TASK_COUNT]       ;update number of processes
237 serge 964
.noinc:
886 serge 965
           shl ebx,8
966
           lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
967
           mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
968
           mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
354 serge 969
 
886 serge 970
           add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
971
           mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
972
           mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
237 serge 973
 
886 serge 974
           mov ecx, [def_cursor]
975
           mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
976
           mov eax, [pl0_stack]
977
           mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
978
           add eax, RING0_STACK_SIZE
979
           mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
281 serge 980
 
854 serge 981
           call _alloc_page
839 serge 982
           add eax, OS_BASE
886 serge 983
           mov esi,[current_slot]
984
           mov esi,[esi+APPDATA.cur_dir]
985
           mov ecx,0x1000/4
986
           mov edi,eax
987
           mov [ebx+SLOT_BASE+APPDATA.cur_dir],eax
988
           rep movsd
521 diamond 989
 
886 serge 990
           shr ebx,3
840 serge 991
           mov dword [CURRENT_TASK+ebx+0x10], 0
237 serge 992
 
993
.add_command_line:
886 serge 994
           mov edx,[params]
995
           mov edx,[edx]       ;app_cmdline
996
           test edx,edx
997
           jz @f           ;application doesn't need parameters
281 serge 998
 
886 serge 999
           mov     eax, edx
1000
           add     eax, 256
1001
           jc      @f
281 serge 1002
 
886 serge 1003
           cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1004
           ja      @f
281 serge 1005
 
886 serge 1006
           mov     byte [edx], 0   ;force empty string if no cmdline given
1007
           mov     eax, [cmd_line]
1008
           test    eax, eax
1009
           jz      @f
1010
           stdcall strncpy, edx, eax, 256
237 serge 1011
@@:
886 serge 1012
           mov edx,[params]
1013
           mov edx, [edx+4]    ;app_path
1014
           test edx,edx
1015
           jz @F           ;application don't need path of file
1016
           mov     eax, edx
1017
           add     eax, 1024
1018
           jc      @f
1019
           cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1020
           ja      @f
1021
           stdcall strncpy, edx, [app_path], 1024
237 serge 1022
@@:
886 serge 1023
           mov    ebx,[slot]
1024
           mov    eax,ebx
1025
           shl    ebx,5
1026
           lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
465 serge 1027
 
237 serge 1028
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
886 serge 1029
           mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1030
           mov     [ebx+window_data+WDATA.fl_redraw], 1
1031
           add    ebx,CURRENT_TASK        ;ebx - pointer to information about process
1032
           mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
237 serge 1033
 
886 serge 1034
           mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
237 serge 1035
 
886 serge 1036
           inc    dword [process_number]
1037
           mov    eax,[process_number]
1038
           mov    [ebx+4],eax       ;set PID
237 serge 1039
 
1040
;set draw data to full screen
1041
 
886 serge 1042
           mov    [ecx+0],dword 0
1043
           mov    [ecx+4],dword 0
1044
           mov    eax,[Screen_Max_X]
1045
           mov    [ecx+8],eax
1046
           mov    eax,[Screen_Max_Y]
1047
           mov    [ecx+12],eax
237 serge 1048
 
886 serge 1049
           mov ebx, [pl0_stack]
1050
           mov esi,[params]
1051
           lea ecx, [ebx+REG_EIP]
1052
           xor eax, eax
237 serge 1053
 
886 serge 1054
           mov [ebx+REG_RET], dword irq0.return
1055
           mov [ebx+REG_EDI], eax
1056
           mov [ebx+REG_ESI], eax
1057
           mov [ebx+REG_EBP], eax
1058
           mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
1059
           mov [ebx+REG_EBX], eax
1060
           mov [ebx+REG_EDX], eax
1061
           mov [ebx+REG_ECX], eax
1062
           mov [ebx+REG_EAX], eax
237 serge 1063
 
886 serge 1064
           mov eax, [esi+0x08]       ;app_eip
1065
           mov [ebx+REG_EIP],  eax   ;app_entry
1066
           mov [ebx+REG_CS], dword app_code
1067
               mov [ebx+REG_EFLAGS], dword EFL_IOPL3+EFL_IF
465 serge 1068
 
886 serge 1069
           mov eax, [esi+0x0C]       ;app_esp
1070
           mov [ebx+REG_APP_ESP], eax    ;app_stack
1071
           mov [ebx+REG_SS], dword app_data
237 serge 1072
 
886 serge 1073
           lea ecx, [ebx+REG_RET]
1074
           mov ebx, [slot]
1075
           shl ebx, 5
1076
           mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx
237 serge 1077
 
886 serge 1078
           xor  ecx, ecx    ; process state - running
237 serge 1079
; set if debuggee
886 serge 1080
           test byte [flags], 1
1081
           jz   .no_debug
1082
 
1083
           inc  ecx     ; process state - suspended
1084
           mov  eax,[CURRENT_TASK]
1085
           mov  [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax
237 serge 1086
.no_debug:
886 serge 1087
           mov  [CURRENT_TASK+ebx+TASKDATA.state], cl
1088
           DEBUGF 1,"%s",new_process_running
1089
           ret
237 serge 1090
endp
1091
 
164 serge 1092
include "debug.inc"