Subversion Repositories Kolibri OS

Rev

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