Subversion Repositories Kolibri OS

Rev

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