Subversion Repositories Kolibri OS

Rev

Rev 709 | Rev 753 | 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: 750 $
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
35
{ .app_cmdline   ;0x00
36
  .app_path      ;0x04
37
  .app_eip       ;0x08
38
  .app_esp       ;0x0C
39
  .app_mem       ;0x10
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:
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
 
237 serge 64
           locals
363 serge 65
             cmdline       rd 64        ;256/4
66
             filename      rd 256       ;1024/4
67
             flags         dd ?
68
 
237 serge 69
             save_cr3      dd ?
70
             slot          dd ?
71
             slot_base     dd ?
72
             file_base     dd ?
73
             file_size     dd ?
363 serge 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
237 serge 81
           endl
164 serge 82
 
363 serge 83
           pushad
84
 
85
           mov [flags], edx
86
 
87
; [ebp]  pointer to filename
88
 
521 diamond 89
           lea edi, [filename]
90
           lea ecx, [edi+1024]
91
           mov al, '/'
92
           stosb
93
@@:
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], '/'
104
@@:
105
           cmp edi, ecx
106
           jae .bigfilename
107
           lodsb
108
           stosb
109
           test al, al
110
           jnz @b
111
           jmp .namecopied
112
.bigfilename:
113
           popad
114
           mov eax, -ERROR_FILE_NOT_FOUND
115
           ret
116
.namecopied:
363 serge 117
 
519 serge 118
           mov [cmdline], ebx
119
           test ebx, ebx
120
           jz @F
121
 
363 serge 122
           lea eax, [cmdline]
123
           mov dword [eax+252], 0
519 serge 124
           stdcall strncpy, eax, ebx, 255
125
@@:
363 serge 126
           lea eax, [filename]
127
           stdcall load_file, eax
237 serge 128
           mov  ecx, -ERROR_FILE_NOT_FOUND
129
           test eax, eax
247 serge 130
           jz .err_file
237 serge 131
 
132
           mov [file_base], eax
133
           mov [file_size], ebx
134
 
363 serge 135
           lea ebx, [hdr_cmdline]
237 serge 136
           call test_app_header
137
           mov ecx, -0x1F
138
           test eax, eax
247 serge 139
           jz .err_hdr
237 serge 140
 
506 mikedld 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:
146
           cmp [application_table_status],0
147
           je .get_lock
148
           call   change_task
149
           jmp .wait_lock
150
 
151
.get_lock:
152
           mov eax, 1
153
           xchg eax, [application_table_status]
154
           cmp eax, 0
155
           jne .wait_lock
156
 
157
           call set_application_table_status
158
 
159
           call get_new_process_place
160
           test eax, eax
161
           mov ecx, -0x20      ; too many processes
269 serge 162
           jz .err
237 serge 163
 
164
           mov [slot], eax
165
           shl eax, 8
380 serge 166
           add eax, SLOT_BASE
237 serge 167
           mov [slot_base], eax
168
           mov edi, eax
169
           _clear_ 256     ;clean extended information about process
170
 
171
; write application name
519 serge 172
           lea eax, [filename]
173
           stdcall strrchr,  eax, '/'  ; now eax points to name without path
237 serge 174
 
175
           lea esi, [eax+1]
176
           test eax, eax
177
           jnz @F
363 serge 178
           lea esi, [filename]
237 serge 179
@@:
180
           mov ecx, 8  ; 8 chars for name
181
           mov edi, [slot_base]
182
.copy_process_name_loop:
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
190
.copy_process_name_done:
191
 
192
           mov ebx, cr3
193
           mov [save_cr3], ebx
269 serge 194
 
363 serge 195
           stdcall create_app_space,[hdr_mem],[file_base],[file_size]
364 diamond 196
           mov ecx, -30  ; no memory
237 serge 197
           test eax, eax
198
           jz .failed
199
 
200
           mov   ebx,[slot_base]
201
           mov   [ebx+APPDATA.dir_table],eax
363 serge 202
           mov   eax,[hdr_mem]
237 serge 203
           mov   [ebx+APPDATA.mem_size],eax
204
 
357 serge 205
if GREEDY_KERNEL
206
else
363 serge 207
           mov ecx, [hdr_mem]
269 serge 208
           mov edi, [file_size]
209
           add edi, 4095
210
           and edi, not 4095
211
           sub ecx, edi
212
           jna @F
213
 
214
           xor eax, eax
465 serge 215
      ;     add edi, new_app_base
237 serge 216
           cld
269 serge 217
           rep stosb
247 serge 218
@@:
269 serge 219
end if
220
 
221
; release only virtual space, not phisical memory
222
 
223
           stdcall free_kernel_space, [file_base]
363 serge 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
 
229
           mov eax, [save_cr3]
230
           call set_cr3
231
 
247 serge 232
           xor ebx, ebx
233
           mov [application_table_status],ebx ;unlock application_table_status mutex
234
           mov eax,[process_number]  ;set result
164 serge 235
           ret
237 serge 236
.failed:
237
           mov eax, [save_cr3]
238
           call set_cr3
269 serge 239
.err:
247 serge 240
.err_hdr:
241
           stdcall kernel_free,[file_base]
242
.err_file:
237 serge 243
           xor eax, eax
244
           mov [application_table_status],eax
321 diamond 245
           mov eax, ecx
237 serge 246
           ret
247
endp
164 serge 248
 
237 serge 249
align 4
250
test_app_header:
251
           virtual at eax
252
             APP_HEADER_00 APP_HEADER_00
253
           end virtual
254
           virtual at eax
164 serge 255
             APP_HEADER_01 APP_HEADER_01
256
           end virtual
257
 
237 serge 258
           cmp dword [eax], 'MENU'
259
           jne .fail
260
           cmp word [eax+4],'ET'
261
           jne .fail
164 serge 262
 
237 serge 263
           cmp [eax+6], word '00'
264
           jne  .check_01_header
164 serge 265
 
237 serge 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
269 serge 276
           mov  edx, [APP_HEADER_00.i_end]
277
           mov  [ebx+0x14], edx
164 serge 278
           ret
279
 
237 serge 280
 .check_01_header:
164 serge 281
 
237 serge 282
           cmp  [eax+6],word '01'
283
           jne  .fail
284
 
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)
292
           cmp  edx,[APP_HEADER_01.i_end]
293
           jb   .fail
294
; \end{diamond}[20.08.2006]
295
 
237 serge 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
269 serge 303
           mov  edx, [APP_HEADER_01.i_end]
304
           mov  [ebx+0x14], edx
237 serge 305
           ret
306
.fail:
164 serge 307
           xor eax, eax
308
           ret
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]!
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
324
.newprocessplace:
325
;eax = address of process information for current slot
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
331
.endnewprocessplace:
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
339
.failed:
340
           xor    eax,eax
341
           ret
342
endp
343
 
344
align 4
269 serge 345
proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
164 serge 346
           locals
347
             app_pages   dd ?
348
             img_pages   dd ?
349
             dir_addr    dd ?
350
             app_tabs    dd ?
351
           endl
352
 
276 serge 353
           mov ebx, pg_data.pg_mutex
354
           call wait_mutex   ;ebx
164 serge 355
 
356
           xor eax, eax
357
           mov [dir_addr], eax
358
 
359
           mov eax, [app_size]
172 serge 360
           add eax, 4095
164 serge 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
 
164 serge 367
           add ebx, 0x3FFFFF
368
           and ebx, NOT(0x3FFFFF)
369
           shr ebx, 22
370
           mov [app_tabs], ebx
371
 
170 serge 372
           mov ecx, [img_size]
373
           add ecx, 4095
374
           and ecx, NOT(4095)
164 serge 375
 
170 serge 376
           mov [img_size], ecx
377
           shr ecx, 12
378
           mov [img_pages], ecx
164 serge 379
 
170 serge 380
     if GREEDY_KERNEL
381
           lea eax, [ecx+ebx+2]    ;only image size
382
     else
383
           lea eax, [eax+ebx+2]    ;all requested memory
384
     end if
385
           cmp eax, [pg_data.pages_free]
386
           ja .fail
387
 
164 serge 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
393
 
394
           mov edi, [tmp_task_pdir]
465 serge 395
           mov ecx, (OS_BASE shr 20)/4
396
           xor eax, eax
164 serge 397
           cld
465 serge 398
           rep stosd
399
 
400
           mov ecx, (OS_BASE shr 20)/4
401
           mov esi, sys_pgdir+(OS_BASE shr 20)
164 serge 402
           rep movsd
403
 
378 serge 404
           mov eax, [dir_addr]
405
           or eax, PG_SW
465 serge 406
           mov [edi-4096+(page_tabs shr 20)], eax
378 serge 407
 
465 serge 408
           and eax, -4096
164 serge 409
           call set_cr3
410
 
411
           mov edx, [app_tabs]
412
           mov edi, new_app_base
413
@@:
414
           call alloc_page
415
           test eax, eax
416
           jz .fail
417
 
188 serge 418
           stdcall map_page_table, edi, eax
164 serge 419
           add edi, 0x00400000
420
           dec edx
421
           jnz @B
422
 
423
           mov edi, new_app_base
424
           shr edi, 10
365 serge 425
           add edi, page_tabs
269 serge 426
 
164 serge 427
           mov ecx, [app_tabs]
428
           shl ecx, 10
429
           xor eax, eax
430
           rep stosd
431
 
269 serge 432
           mov ecx, [img_pages]
433
           mov ebx, PG_UW
164 serge 434
           mov edx, new_app_base
269 serge 435
           mov esi, [img_base]
436
           mov edi, new_app_base
437
           shr esi, 10
438
           shr edi, 10
365 serge 439
           add esi, page_tabs
440
           add edi, page_tabs
269 serge 441
.remap:
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
449
 
450
           mov ecx, [app_pages]
451
           test ecx, ecx
452
           jz .done
453
 
454
if GREEDY_KERNEL
455
           mov eax, 0x02
456
.reserve:
457
           stosd
458
           invlpg [edx]
459
           add edx, 4096
460
           dec ecx
461
           jnz .reserve
462
else
463
 
164 serge 464
.alloc:
465
           call alloc_page
466
           test eax, eax
467
           jz .fail
468
 
469
           stdcall map_page,edx,eax,dword PG_UW
470
           add edx, 0x1000
269 serge 471
           dec [app_pages]
164 serge 472
           jnz .alloc
269 serge 473
end if
164 serge 474
 
269 serge 475
.done:
164 serge 476
           stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
477
 
478
           dec [pg_data.pg_mutex]
479
           mov eax, [dir_addr]
480
           ret
481
.fail:
482
           dec [pg_data.pg_mutex]
483
           cmp [dir_addr], 0
357 serge 484
           je @f
164 serge 485
           stdcall destroy_app_space, [dir_addr]
486
@@:
487
           xor eax, eax
488
           ret
489
endp
490
 
491
align 4
492
set_cr3:
465 serge 493
 
494
           mov ebx, [current_slot]
495
           mov [ebx+APPDATA.dir_table], eax
164 serge 496
           mov cr3, eax
497
           ret
498
 
499
align 4
500
proc destroy_page_table stdcall, pg_tab:dword
501
 
502
           push esi
503
 
504
           mov esi, [pg_tab]
505
           mov ecx, 1024
506
.free:
507
           mov eax, [esi]
508
           test eax, 1
509
           jz .next
510
           call free_page
511
.next:
512
           add esi, 4
513
           dec ecx
514
           jnz .free
515
           pop esi
516
           ret
517
endp
518
 
519
align 4
520
proc destroy_app_space stdcall, pg_dir:dword
521
 
276 serge 522
           mov ebx, pg_data.pg_mutex
523
           call wait_mutex   ;ebx
164 serge 524
 
525
           xor   edx,edx
526
           mov   eax,0x2
527
           mov ebx, [pg_dir]
528
.loop:
529
;eax = current slot of process
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
380 serge 535
           cmp   [SLOT_BASE+ecx+0xB8],ebx       ;compare page directory addresses
164 serge 536
           jnz   @f
537
           inc   edx                            ;thread found
538
@@:
539
           inc   eax
540
           cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
541
           jle   .loop
542
 
543
;edx = number of threads
544
;our process is zombi so it isn't counted
545
           cmp   edx,1
546
           jg    .exit
547
;if there isn't threads then clear memory.
548
 
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]
465 serge 553
           mov edi, (OS_BASE shr 20)/4
164 serge 554
.destroy:
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
563
.next:
564
           add esi, 4
565
           dec edi
566
           jnz .destroy
567
 
568
           mov eax, [pg_dir]
569
           call free_page
570
.exit:
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
575
endp
576
 
520 serge 577
align 4
578
get_pid:
579
           mov eax, [TASK_DATA]
580
           mov eax, [eax+4]
581
           ret
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
599
    jz     .endloop              ;skip empty slots
600
    cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
601
    jz     .pid_found
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
614
    mov    eax,ecx               ;convert offset to index of slot
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.
711
           locals
712
             slot       dd ?
713
             buff       dd ?
714
             r_count    dd ?
715
             offset     dd ?
716
             tmp_r_cnt  dd ?
717
           endl
718
 
719
           mov [slot], eax
720
           mov [buff], ebx
521 diamond 721
           and [r_count], 0
164 serge 722
           mov [tmp_r_cnt], ecx
723
           mov [offset], edx
724
 
725
           pushad
726
.read_mem:
727
           mov edx, [offset]
728
           mov ebx, [tmp_r_cnt]
729
 
730
           mov ecx, 0x400000
731
           and edx, 0x3FFFFF
732
           sub ecx, edx
733
           cmp ecx, ebx
734
           jbe @f
735
           mov ecx, ebx
736
@@:
737
           cmp ecx, 0x8000
738
           jna @F
739
           mov ecx, 0x8000
740
@@:
741
           mov eax, [slot]
742
           shl  eax,8
743
           mov ebx, [offset]
465 serge 744
     ;      add ebx, new_app_base
164 serge 745
           push ecx
746
           stdcall map_memEx, [proc_mem_map],\
380 serge 747
                              [SLOT_BASE+eax+0xB8],\
164 serge 748
                              ebx, ecx
749
           pop ecx
750
 
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
521 diamond 757
           add [r_count], edx
164 serge 758
 
759
           add [offset], edx
760
           sub [tmp_r_cnt], edx
761
           jnz .read_mem
762
 
763
           popad
764
           mov eax, [r_count]
765
           ret
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
 
778
           locals
779
             slot       dd ?
780
             buff       dd ?
781
             w_count    dd ?
782
             offset     dd ?
783
             tmp_w_cnt  dd ?
784
           endl
785
 
786
           mov [slot], eax
787
           mov [buff], ebx
521 diamond 788
           and [w_count], 0
164 serge 789
           mov [tmp_w_cnt], ecx
790
           mov [offset], edx
791
 
792
           pushad
793
.read_mem:
794
           mov edx, [offset]
795
           mov ebx, [tmp_w_cnt]
796
 
797
           mov ecx, 0x400000
798
           and edx, 0x3FFFFF
799
           sub ecx, edx
800
           cmp ecx, ebx
801
           jbe @f
802
           mov ecx, ebx
803
@@:
804
           cmp ecx, 0x8000
805
           jna @F
806
           mov ecx, 0x8000
807
@@:
808
           mov eax, [slot]
809
           shl  eax,8
810
           mov ebx, [offset]
465 serge 811
      ;     add ebx, new_app_base
164 serge 812
           push ecx
813
           stdcall map_memEx, [proc_mem_map],\
380 serge 814
                              [SLOT_BASE+eax+0xB8],\
164 serge 815
                              ebx, ecx
816
           pop ecx
817
 
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
824
 
521 diamond 825
           add [w_count], edx
164 serge 826
           add [offset], edx
827
           sub [tmp_w_cnt], edx
828
           jnz .read_mem
829
 
830
           popad
831
           mov eax, [w_count]
832
           ret
833
endp
834
 
835
align 4
836
proc new_sys_threads
837
           locals
838
             slot          dd ?
237 serge 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
164 serge 844
           endl
845
 
237 serge 846
           cmp eax,1
847
           jne .failed                  ;other subfunctions
164 serge 848
 
237 serge 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
 
506 mikedld 855
           ;mov    esi,new_process_loading
856
           ;call   sys_msg_board_str
857
           DEBUGF 1,"%s",new_process_loading
164 serge 858
.wait_lock:
859
           cmp [application_table_status],0
860
           je .get_lock
861
           call   change_task
862
           jmp .wait_lock
863
 
864
.get_lock:
865
           mov eax, 1
866
           xchg eax, [application_table_status]
867
           cmp eax, 0
868
           jne .wait_lock
869
 
870
           call   set_application_table_status
871
 
872
           call get_new_process_place
873
           test eax, eax
874
           jz .failed
875
 
876
           mov [slot], eax
877
 
465 serge 878
           mov    esi,[current_slot]
237 serge 879
           mov    ebx,esi             ;ebx=esi - pointer to extended information about current thread
164 serge 880
 
237 serge 881
           mov    edi, eax
164 serge 882
           shl    edi,8
380 serge 883
           add    edi,SLOT_BASE
237 serge 884
           mov    edx,edi             ;edx=edi - pointer to extended infomation about new thread
164 serge 885
           mov    ecx,256/4
237 serge 886
           xor eax, eax
887
           cld
888
           rep    stosd               ;clean extended information about new thread
889
           mov    esi,ebx
164 serge 890
           mov    edi,edx
891
           mov    ecx,11
237 serge 892
           rep    movsb               ;copy process name
164 serge 893
 
237 serge 894
           mov eax,[ebx+APPDATA.heap_base]
895
           mov [edx+APPDATA.heap_base], eax
164 serge 896
 
237 serge 897
           mov ecx,[ebx+APPDATA.heap_top]
898
           mov [edx+APPDATA.heap_top], ecx
899
 
900
           mov eax,[ebx+APPDATA.mem_size]
901
           mov [edx+APPDATA.mem_size], eax
902
 
903
           mov ecx,[ebx+APPDATA.dir_table]
904
           mov [edx+APPDATA.dir_table],ecx      ;copy page directory
905
 
906
           lea eax, [app_cmdline]
907
           stdcall set_app_params ,[slot],eax,dword 0,\
908
                                         dword 0,dword 0
909
 
506 mikedld 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
 
914
           mov    [application_table_status],0 ;unlock application_table_status mutex
915
           mov    eax,[process_number]  ;set result
916
           ret
917
.failed:
918
           mov    [application_table_status],0
919
           mov    eax,-1
920
           ret
921
endp
922
 
276 serge 923
; param
924
;  ebx=mutex
925
 
164 serge 926
align 4
276 serge 927
wait_mutex:
928
           push eax
929
           push ebx
321 diamond 930
.do_wait:
164 serge 931
           cmp dword [ebx],0
932
           je .get_lock
933
           call change_task
321 diamond 934
           jmp .do_wait
164 serge 935
.get_lock:
936
           mov eax, 1
937
           xchg eax, [ebx]
938
           test eax, eax
321 diamond 939
           jnz .do_wait
276 serge 940
           pop ebx
941
           pop eax
164 serge 942
           ret
943
 
465 serge 944
EFL_IF      equ 0x0200
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,\
952
                        cmd_line:dword, app_path:dword, flags:dword
164 serge 953
 
357 serge 954
           locals
955
             pl0_stack dd ?
956
           endl
237 serge 957
 
392 serge 958
           stdcall kernel_alloc, RING0_STACK_SIZE+512
357 serge 959
           mov [pl0_stack], eax
960
 
392 serge 961
           lea edi, [eax+RING0_STACK_SIZE]
357 serge 962
 
963
           mov eax, [slot]
964
           mov ebx, eax
965
 
966
           shl eax, 8
380 serge 967
           mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
968
           mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
969
           mov [eax+SLOT_BASE+APPDATA.sse_handler], 0
357 serge 970
 
465 serge 971
;set default io permission map
972
           mov [eax+SLOT_BASE+APPDATA.io_map],\
973
                  (tss._io_map_0-OS_BASE+PG_MAP)
974
           mov [eax+SLOT_BASE+APPDATA.io_map+4],\
975
                  (tss._io_map_1-OS_BASE+PG_MAP)
976
 
357 serge 977
           mov esi, fpu_data
237 serge 978
           mov ecx, 512/4
979
           rep movsd
980
 
357 serge 981
           cmp    ebx,[TASK_COUNT]
982
           jle    .noinc
983
           inc    dword [TASK_COUNT]       ;update number of processes
237 serge 984
.noinc:
281 serge 985
           shl ebx,8
380 serge 986
           lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
987
           mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
988
           mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
354 serge 989
 
990
           add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
380 serge 991
           mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
992
           mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
237 serge 993
 
281 serge 994
           mov ecx, [def_cursor]
380 serge 995
           mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
357 serge 996
           mov eax, [pl0_stack]
380 serge 997
           mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
709 diamond 998
           add eax, RING0_STACK_SIZE
999
           mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
281 serge 1000
 
521 diamond 1001
           push ebx
1002
           stdcall kernel_alloc, 0x1000
1003
           pop ebx
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
1010
 
281 serge 1011
           shr ebx,3
237 serge 1012
           mov eax, new_app_base
1013
           mov dword [CURRENT_TASK+ebx+0x10],eax
1014
 
1015
.add_command_line:
1016
           mov edx,[params]
1017
           mov edx,[edx]           ;app_cmdline
525 diamond 1018
           test edx,edx
1019
           jz @f                   ;application doesn't need parameters
281 serge 1020
 
1021
           mov     eax, edx
1022
           add     eax, 256
1023
           jc      @f
1024
 
380 serge 1025
           cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
281 serge 1026
           ja      @f
1027
 
525 diamond 1028
           mov     byte [edx], 0   ;force empty string if no cmdline given
1029
           mov     eax, [cmd_line]
1030
           test    eax, eax
1031
           jz      @f
1032
           stdcall strncpy, edx, eax, 256
237 serge 1033
@@:
1034
           mov edx,[params]
1035
           mov edx, [edx+4]        ;app_path
1036
           test edx,edx
1037
           jz @F                   ;application don't need path of file
465 serge 1038
           mov     eax, edx
1039
           add     eax, 1024
1040
           jc      @f
1041
           cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
1042
           ja      @f
519 serge 1043
           stdcall strncpy, edx, [app_path], 1024
237 serge 1044
@@:
1045
           mov    ebx,[slot]
1046
           mov    eax,ebx
1047
           shl    ebx,5
465 serge 1048
           lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
1049
 
237 serge 1050
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1051
           mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1052
           mov     [ebx+window_data+WDATA.fl_redraw], 1
1053
           add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
1054
           mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
1055
 
1056
           mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
1057
 
1058
           inc    dword [process_number]
1059
           mov    eax,[process_number]
1060
           mov    [ebx+4],eax           ;set PID
1061
 
1062
;set draw data to full screen
1063
 
1064
           mov    [ecx+0],dword 0
1065
           mov    [ecx+4],dword 0
381 serge 1066
           mov    eax,[ScreenWidth]
237 serge 1067
           mov    [ecx+8],eax
381 serge 1068
           mov    eax,[ScreenHeight]
237 serge 1069
           mov    [ecx+12],eax
1070
 
465 serge 1071
           mov ebx, [pl0_stack]
1072
           mov esi,[params]
1073
           lea ecx, [ebx+REG_EIP]
237 serge 1074
           xor eax, eax
1075
 
465 serge 1076
           mov [ebx+REG_RET], dword irq0.return
1077
           mov [ebx+REG_EDI], eax
1078
           mov [ebx+REG_ESI], eax
1079
           mov [ebx+REG_EBP], eax
1080
           mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
1081
           mov [ebx+REG_EBX], eax
1082
           mov [ebx+REG_EDX], eax
1083
           mov [ebx+REG_ECX], eax
1084
           mov [ebx+REG_EAX], eax
237 serge 1085
 
1086
           mov eax, [esi+0x08]       ;app_eip
465 serge 1087
           mov [ebx+REG_EIP],  eax   ;app_entry
1088
           mov [ebx+REG_CS], dword app_code
1089
           mov [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
1090
 
237 serge 1091
           mov eax, [esi+0x0C]       ;app_esp
465 serge 1092
           mov [ebx+REG_APP_ESP], eax    ;app_stack
1093
           mov [ebx+REG_SS], dword app_data
237 serge 1094
 
465 serge 1095
           lea ecx, [ebx+REG_RET]
1096
           mov ebx, [slot]
569 diamond 1097
           shl ebx, 5
1098
           mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx
237 serge 1099
 
569 diamond 1100
           xor  ecx, ecx        ; process state - running
237 serge 1101
; set if debuggee
1102
           test byte [flags], 1
1103
           jz   .no_debug
569 diamond 1104
           inc  ecx             ; process state - suspended
237 serge 1105
           mov  eax,[CURRENT_TASK]
569 diamond 1106
           mov  [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax
237 serge 1107
.no_debug:
569 diamond 1108
           mov  [CURRENT_TASK+ebx+TASKDATA.state], cl
506 mikedld 1109
           ;mov    esi,new_process_running
1110
           ;call   sys_msg_board_str     ;output information about succefull startup
1111
           DEBUGF 1,"%s",new_process_running
237 serge 1112
           ret
1113
endp
1114
 
164 serge 1115
include "debug.inc"