Subversion Repositories Kolibri OS

Rev

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