Subversion Repositories Kolibri OS

Rev

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