Subversion Repositories Kolibri OS

Rev

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