Subversion Repositories Kolibri OS

Rev

Rev 419 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
357 serge 1
GREEDY_KERNEL  equ 0
164 serge 2
 
170 serge 3
 
164 serge 4
struc APP_HEADER_00
5
{ .banner      dq ?
6
  .version     dd ?    ;+8
7
  .start       dd ?    ;+12
8
  .i_end       dd ?    ;+16
9
  .mem_size    dd ?    ;+20
10
  .i_param     dd ?    ;+24
11
}
12
 
13
struc APP_HEADER_01
14
{ .banner      dq ?
15
  .version     dd ?    ;+8
16
  .start       dd ?    ;+12
17
  .i_end       dd ?    ;+16
18
  .mem_size    dd ?    ;+20
19
  .stack_top   dd ?    ;+24
20
  .i_param     dd ?    ;+28
21
  .i_icon      dd ?    ;+32
22
}
23
 
237 serge 24
struc TSS
25
{
26
  ._back   rw 2
27
  ._esp0   rd 1
28
  ._ss0    rw 2
29
  ._esp1   rd 1
30
  ._ss1    rw 2
31
  ._esp2   rd 1
32
  ._ss2    rw 2
33
  ._cr3    rd 1
34
  ._eip    rd 1
35
  ._eflags rd 1
36
  ._eax    rd 1
37
  ._ecx    rd 1
38
  ._edx    rd 1
39
  ._ebx    rd 1
40
  ._esp    rd 1
41
  ._ebp    rd 1
42
  ._esi    rd 1
43
  ._edi    rd 1
44
  ._es     rw 2
45
  ._cs     rw 2
46
  ._ss     rw 2
47
  ._ds     rw 2
48
  ._fs     rw 2
49
  ._gs     rw 2
50
  ._ldt    rw 2
51
  ._trap   rw 1
52
  ._io     rw 1
53
}
54
 
55
virtual at 0
56
  TSS  TSS
57
end virtual
58
 
59
struc APP_PARAMS
60
{ .app_cmdline   ;0x00
61
  .app_path      ;0x04
62
  .app_eip       ;0x08
63
  .app_esp       ;0x0C
64
  .app_mem       ;0x10
65
}
66
 
67
macro _clear_ op
68
{  mov ecx, op/4
69
   xor eax, eax
70
   cld
71
   rep stosd
72
}
73
 
363 serge 74
 
164 serge 75
align 4
363 serge 76
proc fs_execute
77
 
78
;fn_read:dword, file_size:dword, cluster:dword
79
 
80
; ebx - cmdline
81
; edx - flags
82
; ebp - full filename
83
; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
84
 
237 serge 85
           locals
363 serge 86
             cmdline       rd 64        ;256/4
87
             filename      rd 256       ;1024/4
88
             flags         dd ?
89
 
237 serge 90
             save_cr3      dd ?
91
             slot          dd ?
92
             slot_base     dd ?
93
             file_base     dd ?
94
             file_size     dd ?
363 serge 95
                                          ;app header data
96
             hdr_cmdline   dd ? ;0x00
97
             hdr_path      dd ? ;0x04
98
             hdr_eip       dd ? ;0x08
99
             hdr_esp       dd ? ;0x0C
100
             hdr_mem       dd ? ;0x10
101
             hdr_i_end     dd ? ;0x14
237 serge 102
           endl
164 serge 103
 
363 serge 104
           pushad
105
 
106
           mov [cmdline], ebx
107
           mov [flags], edx
108
 
109
; [ebp]  pointer to filename
110
 
111
           lea eax, [filename]
112
           mov dword [eax+1020],0              ;force terminate
113
                                               ;string
114
           stdcall k_strncpy, eax, [ebp], 1023
115
 
116
           lea eax, [cmdline]
117
           mov dword [eax+252], 0
118
           stdcall k_strncpy, eax, [cmdline], 255
119
 
120
           lea eax, [filename]
121
           stdcall load_file, eax
237 serge 122
           mov  ecx, -ERROR_FILE_NOT_FOUND
123
           test eax, eax
247 serge 124
           jz .err_file
237 serge 125
 
126
           mov [file_base], eax
127
           mov [file_size], ebx
128
 
363 serge 129
           lea ebx, [hdr_cmdline]
237 serge 130
           call test_app_header
131
           mov ecx, -0x1F
132
           test eax, eax
247 serge 133
           jz .err_hdr
237 serge 134
 
135
           mov esi, new_process_loading
136
           call sys_msg_board_str       ; write message to message board
137
 
138
.wait_lock:
139
           cmp [application_table_status],0
140
           je .get_lock
141
           call   change_task
142
           jmp .wait_lock
143
 
144
.get_lock:
145
           mov eax, 1
146
           xchg eax, [application_table_status]
147
           cmp eax, 0
148
           jne .wait_lock
149
 
150
           call set_application_table_status
151
 
152
           call get_new_process_place
153
           test eax, eax
154
           mov ecx, -0x20      ; too many processes
269 serge 155
           jz .err
237 serge 156
 
157
           mov [slot], eax
158
           shl eax, 8
380 serge 159
           add eax, SLOT_BASE
237 serge 160
           mov [slot_base], eax
161
           mov edi, eax
162
           _clear_ 256     ;clean extended information about process
163
 
164
; write application name
363 serge 165
           lea edi, [filename]
237 serge 166
           mov al, '/'
167
           call k_strrchr  ; now eax points to name without path
168
 
169
           lea esi, [eax+1]
170
           test eax, eax
171
           jnz @F
363 serge 172
           lea esi, [filename]
237 serge 173
@@:
174
           mov ecx, 8  ; 8 chars for name
175
           mov edi, [slot_base]
176
.copy_process_name_loop:
177
           lodsb
178
           cmp al, '.'
179
           jz .copy_process_name_done
180
           test al, al
181
           jz .copy_process_name_done
182
           stosb
183
           loop .copy_process_name_loop
184
.copy_process_name_done:
185
 
186
           mov ebx, cr3
187
           mov [save_cr3], ebx
269 serge 188
 
363 serge 189
           stdcall create_app_space,[hdr_mem],[file_base],[file_size]
364 diamond 190
           mov ecx, -30  ; no memory
237 serge 191
           test eax, eax
192
           jz .failed
193
 
194
           mov   ebx,[slot_base]
195
           mov   [ebx+APPDATA.dir_table],eax
363 serge 196
           mov   eax,[hdr_mem]
237 serge 197
           mov   [ebx+APPDATA.mem_size],eax
198
 
357 serge 199
if GREEDY_KERNEL
200
else
363 serge 201
           mov ecx, [hdr_mem]
269 serge 202
           mov edi, [file_size]
203
           add edi, 4095
204
           and edi, not 4095
205
           sub ecx, edi
206
           jna @F
207
 
208
           xor eax, eax
419 serge 209
      ;     add edi, new_app_base
237 serge 210
           cld
269 serge 211
           rep stosb
247 serge 212
@@:
269 serge 213
end if
214
 
215
; release only virtual space, not phisical memory
216
 
217
           stdcall free_kernel_space, [file_base]
363 serge 218
           lea eax, [hdr_cmdline]
219
           lea ebx, [cmdline]
220
           lea ecx, [filename]
221
           stdcall set_app_params ,[slot],eax,ebx,ecx,[flags]
237 serge 222
 
223
           mov eax, [save_cr3]
224
           call set_cr3
225
 
247 serge 226
           xor ebx, ebx
227
           mov [application_table_status],ebx ;unlock application_table_status mutex
228
           mov eax,[process_number]  ;set result
164 serge 229
           ret
237 serge 230
.failed:
231
           mov eax, [save_cr3]
232
           call set_cr3
269 serge 233
.err:
247 serge 234
.err_hdr:
235
           stdcall kernel_free,[file_base]
236
.err_file:
237 serge 237
           xor eax, eax
238
           mov [application_table_status],eax
321 diamond 239
           mov eax, ecx
237 serge 240
           ret
241
endp
164 serge 242
 
237 serge 243
align 4
244
test_app_header:
245
           virtual at eax
246
             APP_HEADER_00 APP_HEADER_00
247
           end virtual
248
           virtual at eax
164 serge 249
             APP_HEADER_01 APP_HEADER_01
250
           end virtual
251
 
237 serge 252
           cmp dword [eax], 'MENU'
253
           jne .fail
254
           cmp word [eax+4],'ET'
255
           jne .fail
164 serge 256
 
237 serge 257
           cmp [eax+6], word '00'
258
           jne  .check_01_header
164 serge 259
 
237 serge 260
           mov  ecx,[APP_HEADER_00.start]
261
           mov  [ebx+0x08], ecx                ;app_eip
262
           mov  edx,[APP_HEADER_00.mem_size]
263
           mov  [ebx+0x10], edx                ;app_mem
264
           shr  edx,1
265
           sub  edx,0x10
266
           mov  [ebx+0x0C], edx                ;app_esp
267
           mov  ecx,[APP_HEADER_00.i_param]
268
           mov  [ebx], ecx                     ;app_cmdline
269
           mov  [ebx+4], dword 0               ;app_path
269 serge 270
           mov  edx, [APP_HEADER_00.i_end]
271
           mov  [ebx+0x14], edx
164 serge 272
           ret
273
 
237 serge 274
 .check_01_header:
164 serge 275
 
237 serge 276
           cmp  [eax+6],word '01'
277
           jne  .fail
278
 
279
           mov  ecx,[APP_HEADER_01.start]
280
           mov  [ebx+0x08], ecx                ;app_eip
281
           mov  edx,[APP_HEADER_01.mem_size]
381 serge 282
 
283
; \begin{diamond}[20.08.2006]
284
; sanity check (functions 19,58 load app_i_end bytes and that must
285
; fit in allocated memory to prevent kernel faults)
286
           cmp  edx,[APP_HEADER_01.i_end]
287
           jb   .fail
288
; \end{diamond}[20.08.2006]
289
 
237 serge 290
           mov  [ebx+0x10], edx                ;app_mem
291
           mov  ecx,[APP_HEADER_01.stack_top]
292
           mov  [ebx+0x0C], ecx                ;app_esp
293
           mov  edx,[APP_HEADER_01.i_param]
294
           mov  [ebx], edx                     ;app_cmdline
295
           mov  ecx,[APP_HEADER_01.i_icon]
296
           mov  [ebx+4], ecx                   ;app_path
269 serge 297
           mov  edx, [APP_HEADER_01.i_end]
298
           mov  [ebx+0x14], edx
237 serge 299
           ret
300
.fail:
164 serge 301
           xor eax, eax
302
           ret
303
 
304
align 4
305
proc get_new_process_place
306
;input:
307
;  none
308
;result:
309
;  eax=[new_process_place]<>0 - ok
310
;      0 - failed.
311
;This function find least empty slot.
312
;It doesn't increase [TASK_COUNT]!
313
           mov    eax,CURRENT_TASK
314
           mov    ebx,[TASK_COUNT]
315
           inc    ebx
316
           shl    ebx,5
317
           add    ebx,eax               ;ebx - address of process information for (last+1) slot
318
.newprocessplace:
319
;eax = address of process information for current slot
320
           cmp    eax,ebx
321
           jz     .endnewprocessplace   ;empty slot after high boundary
322
           add    eax,0x20
323
           cmp    word [eax+0xa],9      ;check process state, 9 means that process slot is empty
324
           jnz    .newprocessplace
325
.endnewprocessplace:
326
           mov    ebx,eax
327
           sub    eax,CURRENT_TASK
328
           shr    eax,5                 ;calculate slot index
329
           cmp    eax,256
330
           jge    .failed               ;it should be <256
331
           mov    word [ebx+0xa],9      ;set process state to 9 (for slot after hight boundary)
332
           ret
333
.failed:
334
           xor    eax,eax
335
           ret
336
endp
337
 
338
align 4
269 serge 339
proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
164 serge 340
           locals
341
             app_pages   dd ?
342
             img_pages   dd ?
343
             dir_addr    dd ?
344
             app_tabs    dd ?
345
           endl
346
 
276 serge 347
           mov ebx, pg_data.pg_mutex
348
           call wait_mutex   ;ebx
164 serge 349
 
350
           xor eax, eax
351
           mov [dir_addr], eax
352
 
353
           mov eax, [app_size]
172 serge 354
           add eax, 4095
164 serge 355
           and eax, NOT(4095)
356
           mov [app_size], eax
357
           mov ebx, eax
358
           shr eax, 12
359
           mov [app_pages], eax
170 serge 360
 
164 serge 361
           add ebx, 0x3FFFFF
362
           and ebx, NOT(0x3FFFFF)
363
           shr ebx, 22
364
           mov [app_tabs], ebx
365
 
170 serge 366
           mov ecx, [img_size]
367
           add ecx, 4095
368
           and ecx, NOT(4095)
164 serge 369
 
170 serge 370
           mov [img_size], ecx
371
           shr ecx, 12
372
           mov [img_pages], ecx
164 serge 373
 
170 serge 374
     if GREEDY_KERNEL
375
           lea eax, [ecx+ebx+2]    ;only image size
376
     else
377
           lea eax, [eax+ebx+2]    ;all requested memory
378
     end if
379
           cmp eax, [pg_data.pages_free]
380
           ja .fail
381
 
164 serge 382
           call alloc_page
383
           test eax, eax
384
           jz .fail
385
           mov [dir_addr], eax
386
           stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
387
 
388
           mov edi, [tmp_task_pdir]
418 serge 389
           mov ecx, (OS_BASE shr 20)/4
394 serge 390
           xor eax, eax
418 serge 391
           cld
394 serge 392
           rep stosd
393
 
418 serge 394
           mov ecx, (OS_BASE shr 20)/4
400 serge 395
           mov esi, sys_pgdir+(OS_BASE shr 20)
164 serge 396
           rep movsd
397
 
378 serge 398
           mov eax, [dir_addr]
418 serge 399
           or eax, PG_SW
400
           mov [edi-4096+(page_tabs shr 20)], eax
401
 
402
           and eax, -4096
164 serge 403
           call set_cr3
404
 
405
           mov edx, [app_tabs]
406
           mov edi, new_app_base
407
@@:
408
           call alloc_page
409
           test eax, eax
410
           jz .fail
411
 
188 serge 412
           stdcall map_page_table, edi, eax
164 serge 413
           add edi, 0x00400000
414
           dec edx
415
           jnz @B
416
 
417
           mov edi, new_app_base
418
           shr edi, 10
365 serge 419
           add edi, page_tabs
269 serge 420
 
164 serge 421
           mov ecx, [app_tabs]
422
           shl ecx, 10
423
           xor eax, eax
424
           rep stosd
425
 
269 serge 426
           mov ecx, [img_pages]
427
           mov ebx, PG_UW
164 serge 428
           mov edx, new_app_base
269 serge 429
           mov esi, [img_base]
430
           mov edi, new_app_base
431
           shr esi, 10
432
           shr edi, 10
365 serge 433
           add esi, page_tabs
434
           add edi, page_tabs
269 serge 435
.remap:
436
           lodsd
437
           or eax, ebx      ; force user level r/w access
438
           stosd
439
           add edx, 0x1000
440
           dec [app_pages]
441
           dec ecx
442
           jnz .remap
443
 
444
           mov ecx, [app_pages]
445
           test ecx, ecx
446
           jz .done
447
 
448
if GREEDY_KERNEL
449
           mov eax, 0x02
450
.reserve:
451
           stosd
452
           invlpg [edx]
453
           add edx, 4096
454
           dec ecx
455
           jnz .reserve
456
else
457
 
164 serge 458
.alloc:
459
           call alloc_page
460
           test eax, eax
461
           jz .fail
462
 
463
           stdcall map_page,edx,eax,dword PG_UW
464
           add edx, 0x1000
269 serge 465
           dec [app_pages]
164 serge 466
           jnz .alloc
269 serge 467
end if
164 serge 468
 
269 serge 469
.done:
164 serge 470
           stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
471
 
472
           dec [pg_data.pg_mutex]
473
           mov eax, [dir_addr]
474
           ret
475
.fail:
476
           dec [pg_data.pg_mutex]
477
           cmp [dir_addr], 0
357 serge 478
           je @f
164 serge 479
           stdcall destroy_app_space, [dir_addr]
480
@@:
481
           xor eax, eax
482
           ret
483
endp
484
 
485
align 4
486
set_cr3:
487
           mov esi, [CURRENT_TASK]
488
           mov ebx, esi
489
           shl esi,8
380 serge 490
           mov [SLOT_BASE+esi+0xB8],eax
164 serge 491
           imul   ebx,tss_step
492
           add    ebx,tss_data
493
           mov [ebx+28], eax
494
           mov cr3, eax
495
           ret
496
 
497
align 4
498
proc destroy_page_table stdcall, pg_tab:dword
499
 
500
           push esi
501
 
502
           mov esi, [pg_tab]
503
           mov ecx, 1024
504
.free:
505
           mov eax, [esi]
506
           test eax, 1
507
           jz .next
508
           call free_page
509
.next:
510
           add esi, 4
511
           dec ecx
512
           jnz .free
513
           pop esi
514
           ret
515
endp
516
 
517
align 4
518
proc destroy_app_space stdcall, pg_dir:dword
519
 
276 serge 520
           mov ebx, pg_data.pg_mutex
521
           call wait_mutex   ;ebx
164 serge 522
 
523
           xor   edx,edx
524
           mov   eax,0x2
525
           mov ebx, [pg_dir]
526
.loop:
527
;eax = current slot of process
528
           mov   ecx,eax
529
           shl   ecx,5
530
           cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
531
           jz    @f                              ;skip empty slots
532
           shl   ecx,3
380 serge 533
           cmp   [SLOT_BASE+ecx+0xB8],ebx       ;compare page directory addresses
164 serge 534
           jnz   @f
535
           inc   edx                            ;thread found
536
@@:
537
           inc   eax
538
           cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
539
           jle   .loop
540
 
541
;edx = number of threads
542
;our process is zombi so it isn't counted
543
           cmp   edx,1
544
           jg    .exit
545
;if there isn't threads then clear memory.
546
 
547
           mov eax, [pg_dir]
548
           and eax, not 0xFFF
549
           stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
550
           mov esi, [tmp_task_pdir]
418 serge 551
           mov edi, (OS_BASE shr 20)/4
164 serge 552
.destroy:
553
           mov eax, [esi]
554
           test eax, 1
555
           jz .next
556
           and eax, not 0xFFF
557
           stdcall map_page,[tmp_task_ptab],eax,dword PG_SW
558
           stdcall destroy_page_table, [tmp_task_ptab]
559
           mov eax, [esi]
560
           call free_page
561
.next:
562
           add esi, 4
563
           dec edi
564
           jnz .destroy
565
 
566
           mov eax, [pg_dir]
567
           call free_page
568
.exit:
569
           stdcall map_page,[tmp_task_ptab],dword 0,dword PG_UNMAP
570
           stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
571
           dec [pg_data.pg_mutex]
572
           ret
573
endp
574
 
575
pid_to_slot:
576
;Input:
577
;  eax - pid of process
578
;Output:
579
;  eax - slot of process or 0 if process don't exists
580
;Search process by PID.
581
    push   ebx
582
    push   ecx
583
    mov    ebx,[TASK_COUNT]
584
    shl    ebx,5
585
    mov    ecx,2*32
586
 
587
.loop:
588
;ecx=offset of current process info entry
589
;ebx=maximum permitted offset
590
    cmp    byte [CURRENT_TASK+ecx+0xa],9
591
    jz     .endloop              ;skip empty slots
592
    cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
593
    jz     .pid_found
594
.endloop:
595
    add    ecx,32
596
    cmp    ecx,ebx
597
    jle    .loop
598
 
599
    pop    ecx
600
    pop    ebx
601
    xor    eax,eax
602
    ret
603
 
604
.pid_found:
605
    shr    ecx,5
606
    mov    eax,ecx               ;convert offset to index of slot
607
    pop    ecx
608
    pop    ebx
609
    ret
610
 
611
check_region:
612
;input:
613
;  ebx - start of buffer
614
;  ecx - size of buffer
615
;result:
616
;  eax = 1 region lays in app memory
617
;  eax = 0 region don't lays in app memory
618
     mov  eax,[CURRENT_TASK]
619
     jmp  check_process_region
620
;-----------------------------------------------------------------------------
621
check_process_region:
622
;input:
623
;  eax - slot
624
;  ebx - start of buffer
625
;  ecx - size of buffer
626
;result:
627
;  eax = 1 region lays in app memory
628
;  eax = 0 region don't lays in app memory
629
 
630
     test ecx,ecx
631
     jle  .ok
632
     shl  eax,5
633
     cmp  word [CURRENT_TASK+eax+0xa],0
634
     jnz  .failed
635
     shl  eax,3
380 serge 636
     mov  eax,[SLOT_BASE+eax+0xb8]
164 serge 637
     test eax,eax
638
     jz   .failed
639
 
640
     mov  eax,1
641
     ret
642
 
643
 
644
;    call MEM_Get_Linear_Address
645
;    push ebx
646
;    push ecx
647
;    push edx
648
;    mov  edx,ebx
649
;    and  edx,not (4096-1)
650
;    sub  ebx,edx
651
;    add  ecx,ebx
652
;    mov  ebx,edx
653
;    add  ecx,(4096-1)
654
;    and  ecx,not (4096-1)
655
;.loop:
656
;;eax - linear address of page directory
657
;;ebx - current page
658
;;ecx - current size
659
;    mov  edx,ebx
660
;    shr  edx,22
661
;    mov  edx,[eax+4*edx]
662
;    and  edx,not (4096-1)
663
;    test edx,edx
664
;    jz   .failed1
665
;    push eax
666
;    mov  eax,edx
667
;    call MEM_Get_Linear_Address
668
;    mov  edx,ebx
669
;    shr  edx,12
670
;    and  edx,(1024-1)
671
;    mov  eax,[eax+4*edx]
672
;    and  eax,not (4096-1)
673
;    test eax,eax
674
;    pop  eax
675
;    jz   .failed1
676
;    add  ebx,4096
677
;    sub  ecx,4096
678
;    jg   .loop
679
;    pop  edx
680
;    pop  ecx
681
;    pop  ebx
682
.ok:
683
    mov  eax,1
684
    ret
685
;
686
;.failed1:
687
;    pop  edx
688
;    pop  ecx
689
;    pop  ebx
690
.failed:
691
    xor  eax,eax
692
    ret
693
 
694
align 4
695
proc read_process_memory
696
;Input:
697
;  eax - process slot
698
;  ebx - buffer address
699
;  ecx - buffer size
700
;  edx - start address in other process
701
;Output:
702
;  eax - number of bytes read.
703
           locals
704
             slot       dd ?
705
             buff       dd ?
706
             r_count    dd ?
707
             offset     dd ?
708
             tmp_r_cnt  dd ?
709
           endl
710
 
711
           mov [slot], eax
712
           mov [buff], ebx
713
           mov [r_count], ecx
714
           mov [tmp_r_cnt], ecx
715
           mov [offset], edx
716
 
717
           pushad
718
.read_mem:
719
           mov edx, [offset]
720
           mov ebx, [tmp_r_cnt]
721
 
722
           mov ecx, 0x400000
723
           and edx, 0x3FFFFF
724
           sub ecx, edx
725
           cmp ecx, ebx
726
           jbe @f
727
           mov ecx, ebx
728
@@:
729
           cmp ecx, 0x8000
730
           jna @F
731
           mov ecx, 0x8000
732
@@:
733
           mov eax, [slot]
734
           shl  eax,8
735
           mov ebx, [offset]
419 serge 736
     ;      add ebx, new_app_base
164 serge 737
           push ecx
738
           stdcall map_memEx, [proc_mem_map],\
380 serge 739
                              [SLOT_BASE+eax+0xB8],\
164 serge 740
                              ebx, ecx
741
           pop ecx
742
 
743
           mov esi, [offset]
744
           and esi, 0xfff
745
           add esi, [proc_mem_map]
746
           mov edi, [buff]
747
           mov edx, ecx
748
           rep movsb
749
 
750
           add [offset], edx
751
           sub [tmp_r_cnt], edx
752
           jnz .read_mem
753
 
754
           popad
755
           mov eax, [r_count]
756
           ret
757
endp
758
 
759
align 4
760
proc write_process_memory
761
;Input:
762
;  eax - process slot
763
;  ebx - buffer address
764
;  ecx - buffer size
765
;  edx - start address in other process
766
;Output:
767
;  eax - number of bytes written
768
 
769
           locals
770
             slot       dd ?
771
             buff       dd ?
772
             w_count    dd ?
773
             offset     dd ?
774
             tmp_w_cnt  dd ?
775
           endl
776
 
777
           mov [slot], eax
778
           mov [buff], ebx
779
           mov [w_count], ecx
780
           mov [tmp_w_cnt], ecx
781
           mov [offset], edx
782
 
783
           pushad
784
.read_mem:
785
           mov edx, [offset]
786
           mov ebx, [tmp_w_cnt]
787
 
788
           mov ecx, 0x400000
789
           and edx, 0x3FFFFF
790
           sub ecx, edx
791
           cmp ecx, ebx
792
           jbe @f
793
           mov ecx, ebx
794
@@:
795
           cmp ecx, 0x8000
796
           jna @F
797
           mov ecx, 0x8000
798
@@:
799
           mov eax, [slot]
800
           shl  eax,8
801
           mov ebx, [offset]
419 serge 802
      ;     add ebx, new_app_base
164 serge 803
           push ecx
804
           stdcall map_memEx, [proc_mem_map],\
380 serge 805
                              [SLOT_BASE+eax+0xB8],\
164 serge 806
                              ebx, ecx
807
           pop ecx
808
 
809
           mov edi, [offset]
810
           and edi, 0xfff
811
           add edi, [proc_mem_map]
812
           mov esi, [buff]
813
           mov edx, ecx
814
           rep movsb
815
 
816
           add [offset], edx
817
           sub [tmp_w_cnt], edx
818
           jnz .read_mem
819
 
820
           popad
821
           mov eax, [w_count]
822
           ret
823
endp
824
 
825
align 4
826
proc new_sys_threads
827
           locals
828
             slot          dd ?
237 serge 829
             app_cmdline   dd ? ;0x00
830
             app_path      dd ? ;0x04
831
             app_eip       dd ? ;0x08
832
             app_esp       dd ? ;0x0C
833
             app_mem       dd ? ;0x10
164 serge 834
           endl
835
 
237 serge 836
           cmp eax,1
837
           jne .failed                  ;other subfunctions
164 serge 838
 
237 serge 839
           xor  eax,eax
840
           mov [app_cmdline], eax
841
           mov [app_path], eax
842
           mov [app_eip], ebx
843
           mov [app_esp], ecx
164 serge 844
 
845
           mov    esi,new_process_loading
846
           call   sys_msg_board_str
847
.wait_lock:
848
           cmp [application_table_status],0
849
           je .get_lock
850
           call   change_task
851
           jmp .wait_lock
852
 
853
.get_lock:
854
           mov eax, 1
855
           xchg eax, [application_table_status]
856
           cmp eax, 0
857
           jne .wait_lock
858
 
859
           call   set_application_table_status
860
 
861
           call get_new_process_place
862
           test eax, eax
863
           jz .failed
864
 
865
           mov [slot], eax
866
 
867
           mov    esi,[CURRENT_TASK]
868
           shl    esi,8
380 serge 869
           add    esi,SLOT_BASE
237 serge 870
           mov    ebx,esi             ;ebx=esi - pointer to extended information about current thread
164 serge 871
 
237 serge 872
           mov    edi, eax
164 serge 873
           shl    edi,8
380 serge 874
           add    edi,SLOT_BASE
237 serge 875
           mov    edx,edi             ;edx=edi - pointer to extended infomation about new thread
164 serge 876
           mov    ecx,256/4
237 serge 877
           xor eax, eax
878
           cld
879
           rep    stosd               ;clean extended information about new thread
880
           mov    esi,ebx
164 serge 881
           mov    edi,edx
419 serge 882
           mov byte [edi], '@'
883
           inc    edi
164 serge 884
           mov    ecx,11
237 serge 885
           rep    movsb               ;copy process name
164 serge 886
 
237 serge 887
           mov eax,[ebx+APPDATA.heap_base]
888
           mov [edx+APPDATA.heap_base], eax
164 serge 889
 
237 serge 890
           mov ecx,[ebx+APPDATA.heap_top]
891
           mov [edx+APPDATA.heap_top], ecx
892
 
893
           mov eax,[ebx+APPDATA.mem_size]
894
           mov [edx+APPDATA.mem_size], eax
895
 
896
           mov ecx,[ebx+APPDATA.dir_table]
897
           mov [edx+APPDATA.dir_table],ecx      ;copy page directory
898
 
899
           lea eax, [app_cmdline]
900
           stdcall set_app_params ,[slot],eax,dword 0,\
901
                                         dword 0,dword 0
902
 
164 serge 903
           mov    esi,new_process_running
904
           call   sys_msg_board_str     ;output information about succefull startup
905
 
906
           mov    [application_table_status],0 ;unlock application_table_status mutex
907
           mov    eax,[process_number]  ;set result
908
           ret
909
.failed:
910
           mov    [application_table_status],0
911
           mov    eax,-1
912
           ret
913
endp
914
 
276 serge 915
; param
916
;  ebx=mutex
917
 
164 serge 918
align 4
276 serge 919
wait_mutex:
920
           push eax
921
           push ebx
321 diamond 922
.do_wait:
164 serge 923
           cmp dword [ebx],0
924
           je .get_lock
925
           call change_task
321 diamond 926
           jmp .do_wait
164 serge 927
.get_lock:
928
           mov eax, 1
929
           xchg eax, [ebx]
930
           test eax, eax
321 diamond 931
           jnz .do_wait
276 serge 932
           pop ebx
933
           pop eax
164 serge 934
           ret
935
 
237 serge 936
align 4
937
proc set_app_params stdcall,slot:dword, params:dword,\
938
                        cmd_line:dword, app_path:dword, flags:dword
164 serge 939
 
357 serge 940
           locals
941
             pl0_stack dd ?
942
           endl
237 serge 943
 
418 serge 944
           stdcall kernel_alloc, RING0_STACK_SIZE+512
357 serge 945
           mov [pl0_stack], eax
946
 
418 serge 947
           lea edi, [eax+RING0_STACK_SIZE]
357 serge 948
 
949
           mov eax, [slot]
950
           mov ebx, eax
951
 
952
           shl eax, 8
380 serge 953
           mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
954
           mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
955
           mov [eax+SLOT_BASE+APPDATA.sse_handler], 0
357 serge 956
 
957
           mov esi, fpu_data
237 serge 958
           mov ecx, 512/4
959
           rep movsd
960
 
357 serge 961
           cmp    ebx,[TASK_COUNT]
962
           jle    .noinc
963
           inc    dword [TASK_COUNT]       ;update number of processes
237 serge 964
.noinc:
281 serge 965
           shl ebx,8
380 serge 966
           lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
967
           mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
968
           mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
354 serge 969
 
970
           add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
380 serge 971
           mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
972
           mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
237 serge 973
 
281 serge 974
           mov ecx, [def_cursor]
380 serge 975
           mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
357 serge 976
           mov eax, [pl0_stack]
380 serge 977
           mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
281 serge 978
 
979
           shr ebx,3
237 serge 980
           mov eax, new_app_base
981
           mov dword [CURRENT_TASK+ebx+0x10],eax
982
 
983
.add_command_line:
984
           mov edx,[params]
985
           mov edx,[edx]           ;app_cmdline
986
           test edx,edx
987
           jz @F                   ;application don't need parameters
281 serge 988
 
989
           mov     eax, edx
990
           add     eax, 256
991
           jc      @f
992
 
380 serge 993
           cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
281 serge 994
           ja      @f
995
 
419 serge 996
     ;      add edx, new_app_base
262 diamond 997
           stdcall k_strncpy, edx, [cmd_line], 256
237 serge 998
@@:
999
           mov edx,[params]
1000
           mov edx, [edx+4]        ;app_path
1001
           test edx,edx
1002
           jz @F                   ;application don't need path of file
262 diamond 1003
        mov     eax, edx
1004
        add     eax, 1024
1005
        jc      @f
380 serge 1006
        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
262 diamond 1007
        ja      @f
419 serge 1008
    ;       add edx, new_app_base
262 diamond 1009
           stdcall k_strncpy, edx, [app_path], 1024
237 serge 1010
@@:
1011
           mov    ebx,[slot]
1012
           mov    eax,ebx
1013
           shl    ebx,5
420 serge 1014
           lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
1015
 
237 serge 1016
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1017
           mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
1018
           mov     [ebx+window_data+WDATA.fl_redraw], 1
1019
           add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
1020
           mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
1021
 
1022
           mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
1023
 
1024
           inc    dword [process_number]
1025
           mov    eax,[process_number]
1026
           mov    [ebx+4],eax           ;set PID
1027
 
1028
;set draw data to full screen
1029
 
1030
           mov    [ecx+0],dword 0
1031
           mov    [ecx+4],dword 0
381 serge 1032
           mov    eax,[ScreenWidth]
237 serge 1033
           mov    [ecx+8],eax
381 serge 1034
           mov    eax,[ScreenHeight]
237 serge 1035
           mov    [ecx+12],eax
1036
 
1037
           mov edi,[slot]
1038
           imul edi,tss_step
1039
           add edi,tss_data
1040
           mov ecx,128/4
1041
           xor eax, eax
1042
           cld
1043
           rep stosd
1044
;Add IO access table - bit array of permitted ports
1045
           not eax
1046
           mov ecx,2048
1047
           rep stosd                 ; access to 4096*8=65536 ports
1048
           sub edi, tss_step
1049
 
1050
;set cr3 register in TSS of application
1051
           mov ecx, [slot]
1052
           shl ecx, 8
380 serge 1053
           mov eax,[SLOT_BASE+ecx+APPDATA.dir_table]
237 serge 1054
           mov [edi+TSS._cr3],eax
1055
 
1056
           mov esi,[params]
1057
           mov eax, [esi+0x08]       ;app_eip
1058
           mov [edi+TSS._eip],eax    ;set eip in TSS
1059
           mov eax, [esi+0x0C]       ;app_esp
1060
           mov [edi+TSS._esp],eax    ;set stack in TSS
420 serge 1061
           mov [edi+TSS._eflags],dword 0x1202
237 serge 1062
 
1063
           mov [edi+TSS._cs],app_code  ;selector of code segment
1064
           mov [edi+TSS._ss],app_data
1065
           mov [edi+TSS._ds],app_data
1066
           mov [edi+TSS._es],app_data
1067
           mov [edi+TSS._fs],app_data
1068
           mov [edi+TSS._gs],graph_data ;selector of graphic segment
1069
           mov [edi+TSS._io],word 128
420 serge 1070
           mov [edi+TSS._ss0], os_stack
357 serge 1071
           mov ebx, [pl0_stack]
418 serge 1072
           add ebx, RING0_STACK_SIZE
237 serge 1073
           mov [edi+TSS._esp0],ebx
1074
 
420 serge 1075
           xor eax, eax
1076
           mov ebx, [pl0_stack]
1077
           lea ecx, [ebx+REG_EIP]
1078
 
1079
           mov [ebx+REG_RET], dword irq0.return
1080
           mov [ebx+REG_EDI], eax
1081
           mov [ebx+REG_ESI], eax
1082
           mov [ebx+REG_EBP], eax
1083
           mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
1084
           mov [ebx+REG_EBX], eax
1085
           mov [ebx+REG_EDX], eax
1086
           mov [ebx+REG_ECX], eax
1087
           mov [ebx+REG_EAX], eax
1088
 
1089
           mov [ebx+REG_EFL_2], dword 0x1002
1090
 
1091
           mov eax, [esi+0x08]       ;app_eip
1092
           mov [ebx+REG_EIP],  eax   ;app_entry
1093
           mov [ebx+REG_CS], dword app_code
1094
           mov [ebx+REG_EFLAGS], dword 0x1202
1095
 
1096
           mov eax, [esi+0x0C]       ;app_esp
1097
           mov [ebx+REG_APP_ESP], eax    ;app_stack
1098
           mov [ebx+REG_SS], dword app_data
1099
 
1100
           lea ecx, [ebx+REG_RET]
1101
           mov ebx, [slot]
1102
           shl ebx, 8
1103
           mov [ebx+SLOT_BASE+APPDATA.saved_esp], ecx
1104
 
237 serge 1105
           mov ecx, edi    ;ecx - address of application TSS
1106
           mov ebx,[slot]
1107
           shl ebx,3
1108
;set TSS descriptor
1109
           mov [ebx+gdts+tss0+0],word tss_step ;limit (size)
1110
           mov [ebx+gdts+tss0+2],cx  ;part of offset
1111
           shr ecx,16
1112
           mov [ebx+gdts+tss0+4],cl  ;part of offset
1113
           mov [ebx+gdts+tss0+7],ch  ;part of offset
1114
           mov [ebx+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
1115
 
1116
;flush keyboard and buttons queue
1117
           mov    [KEY_COUNT],byte 0
1118
           mov    [BTN_COUNT],byte 0
1119
 
1120
           mov    edi,[slot]
1121
           shl    edi,5
1122
           add    edi,window_data
1123
           mov    ebx,[slot]
1124
           movzx  esi,word [WIN_STACK+ebx*2]
1125
           lea    esi,[WIN_POS+esi*2]
1126
           call   windowactivate        ;gui initialization
1127
 
1128
           mov    ebx,[slot]
1129
           shl    ebx,5
1130
           mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
1131
; set if debuggee
1132
           mov eax, [flags]
1133
           test byte [flags], 1
1134
           jz   .no_debug
1135
           mov  [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
1136
           mov  eax,[CURRENT_TASK]
380 serge 1137
           mov  [SLOT_BASE+ebx*8+0xac],eax ;set debugger PID - current
237 serge 1138
.no_debug:
1139
           mov    esi,new_process_running
1140
           call   sys_msg_board_str     ;output information about succefull startup
1141
           ret
420 serge 1142
 
1143
if 0
1144
; set stack frame
1145
 
1146
           xor eax, eax
1147
           lea ecx, [ebx+REG_EIP]
1148
           mov ebx, [pl0_stack]
1149
           mov esi,[params]
1150
 
1151
           mov [ebx+REG_RET], dword irq0.return
1152
           mov [ebx+REG_EDI], eax
1153
           mov [ebx+REG_ESI], eax
1154
           mov [ebx+REG_EBP], eax
1155
           mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
1156
           mov [ebx+REG_EBX], eax
1157
           mov [ebx+REG_EDX], eax
1158
           mov [ebx+REG_ECX], eax
1159
           mov [ebx+REG_EAX], eax
1160
 
1161
           mov [ebx+REG_EFL_2], dword 0x1002
1162
 
1163
           mov eax, [esi+0x08]       ;app_eip
1164
           mov [ebx+REG_EIP],  eax   ;app_entry
1165
           mov [ebx+REG_CS], dword app_code
1166
           mov [ebx+REG_EFLAGS], dword 0x1202
1167
 
1168
           mov eax, [esi+0x0C]       ;app_esp
1169
           mov [ebx+REG_ESP], eax    ;app_stack
1170
           mov [ebx+REG_SS], dword app_data
1171
end if
1172
 
237 serge 1173
endp
1174
 
164 serge 1175
include "debug.inc"
1176