Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1 ha 1
if ~defined newprocess_inc
2
newprocess_inc_fix:
3
newprocess_inc fix newprocess_inc_fix
4
include "mem.inc"
5
include "memmanag.inc"
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
;;Working with new types of processes.
8
;;Author: Khalyavin Andrey halyavin@land.ru
9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
iglobal
11
    new_process_loading db 'K : New Process - loading',13,10,0
12
    new_process_running db 'K : New Process - done',13,10,0
13
    start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
14
endg
15
;-----------------------------------------------------------------------------
16
 
17
find_new_process_place:
18
;input:
19
;  none
20
;result:
21
;  eax=[new_process_place]<>0 - ok
22
;      0 - failed.
23
;This function find least empty slot.
24
;It doesn't increase [0x3004]!
25
    mov    eax,0x3000+second_base_address
26
    push   ebx
27
    mov    ebx,[0x3004]
28
    inc    ebx
29
    shl    ebx,5
30
    add    ebx,eax               ;ebx - address of process information for (last+1) slot
31
.newprocessplace:
32
;eax = address of process information for current slot
33
    cmp    eax,ebx
34
    jz     .endnewprocessplace   ;empty slot after high boundary
35
    add    eax,0x20
36
    cmp    word [eax+0xa],9      ;check process state, 9 means that process slot is empty
37
    jnz    .newprocessplace
38
.endnewprocessplace:
39
    mov    ebx,eax
40
    sub    eax,0x3000+second_base_address
41
    shr    eax,5                 ;calculate slot index
42
    cmp    eax,256
43
    jge    .failed               ;it should be <256
44
    mov    word [ebx+0xa],9      ;set process state to 9 (for slot after hight boundary)
45
    mov    [new_process_place],eax ;save process slot
46
    pop    ebx
47
    ret
48
.failed:
49
    xor    eax,eax
50
    pop    ebx
51
    ret
52
;-----------------------------------------------------------------------------
4 poddubny 53
safe_sti:
54
    cmp    byte [0xe000], 1
55
    jne    @f
56
    sti
57
 @@:ret
58
 
1 ha 59
new_start_application_floppy:
60
;input:
61
;  eax - pointer to filename
62
;  ebx - parameters to pass
40 halyavin 63
;  edx - flags
1 ha 64
;result:
65
;  eax - pid of new process
66
;        or 0 if call fails.
5 halyavin 67
    mov    [appl_path],edi
1 ha 68
    pushad
69
    mov    esi,new_process_loading
70
    call   sys_msg_board_str     ;write to debug board
71
 
72
;wait application_table_status mutex
73
.table_status:
74
    cli
75
    cmp    [application_table_status],0
76
    jz     .stf
77
    sti
78
    call   change_task
79
    jmp    .table_status
80
.stf:
81
    call   set_application_table_status
82
;we can change system tables now
83
    push   edi
84
    push   ebx
85
    push   eax
86
    call   find_new_process_place ;find empty process slot
87
    sti
88
    test   eax,eax
89
    jz     .failed
90
 
91
    mov    edi,eax
92
    shl    edi,8
93
    add    edi,0x80000
94
    mov    ecx,256/4
95
    xor    eax,eax
96
    cld
97
    rep    stosd                 ;clean extended information about process
98
 
99
;set new process name
5 halyavin 100
    xor    eax,eax
101
    mov    [appl_path_size],eax
102
    mov    eax,[esp]  ;+8]
1 ha 103
.find_last_byte:
104
    cmp    byte [eax],0
105
    jz     .find_last_byte_end
106
    inc    eax
5 halyavin 107
    inc    [appl_path_size]
1 ha 108
    jmp    .find_last_byte
5 halyavin 109
.find_last_byte_end:
110
    add    [appl_path_size],24
1 ha 111
    sub    eax,11                ;last 11 bytes = application name
112
;    mov    eax,[esp]             ;eax - pointer to file name
113
    mov    ebx,[new_process_place]
114
    shl    ebx,8
115
    add    ebx,0x80000
116
    mov    ecx,11
117
    call   memmove
118
 
119
;read header of file
120
    mov    eax,[esp]
121
    mov    ebx,1                 ;index of first block
122
    mov    ecx,2                 ;number of blocks
123
    mov    edx,0x90000           ;temp area
124
    mov    esi,12                ;file name length
125
    mov    edi,[esp+8]
126
;    cli
127
    call   floppy_fileread       ;read file from FD
128
;    sti
129
    cmp    eax,0
130
    jne    .cleanfailed
131
;check MENUET signature
132
    cmp    [0x90000],dword 'MENU'
133
    jnz    .cleanfailed
134
    cmp    [0x90004],word 'ET'
135
    jnz    .cleanfailed
136
 
137
    call   get_app_params        ;parse header fields
138
    cmp    esi,0
139
    jz     .cleanfailed
140
 
141
    mov    eax,[new_process_place]
142
    call   create_app_cr3_table   ;create page directory for new process
143
    test   eax,eax
144
    jz     .cleanfailed_mem
145
 
146
    call   MEM_Get_Linear_Address ;calculate linear address of it
147
 
148
    mov    ebx,std_application_base_address
149
    mov    ecx,[app_mem]
150
    add    ecx,4095
151
    shr    ecx,12
152
    mov    edx,eax
153
    call   mem_alloc_specified_region ;allocate memory for application
154
    test   eax,eax
9 halyavin 155
    jz     .cleanfailed_mem1
1 ha 156
 
157
    mov    eax,[edx+(std_application_base_address shr 20)]
158
    and    eax,not (4096-1)      ;eax - physical address of first (for application memory) page table
159
    call   MEM_Get_Linear_Address
160
    mov    edx,eax
161
 
162
;read file
163
    mov    ebx,1
164
    mov    esi,12                ;length of file name
165
.loop1:
166
;edx = linear address of current page table entry
167
;ebx = index of current block in file
168
    push   edx
169
    mov    eax,[edx]
170
    and    eax,not (4096-1)
171
    call   MEM_Get_Linear_Address
172
    mov    edx,eax               ;read file block to current page
173
    mov    eax,[esp+4]           ;restore pointer to file name
174
    mov    ecx,8                 ;number of blocks read
175
    push   ebx
176
    mov    edi,[esp+16]
177
;    cli
178
    call   floppy_fileread
179
;ebx=file size
180
;    sti
181
    pop    ecx
182
    shr    ebx,9
183
    cmp    ecx,ebx
184
    jg     .endloop1             ;if end of file?
185
    mov    ebx,ecx
186
    test   eax,eax
187
    jnz    .endloop1             ;check io errors
188
    pop    edx
189
    add    ebx,8                 ;go to next page
190
    add    edx,4
191
    jmp    .loop1
192
 
193
.endloop1:
194
    add    esp,8+4                 ;pop linear address of page table entry and pointer to file name
195
    call   new_start_application_fl.add_app_parameters
196
    mov    [esp+28],eax
197
    popad
198
    ret
9 halyavin 199
 
200
.cleanfailed_mem1:
201
;there is mem for directory entry, but there is no mem for pages
202
;so free directory entry
203
    mov    eax,[new_process_place]
204
    shl    eax,8
205
    mov    eax,[0x80000+eax+0xB8]
206
    call   MEM_Free_Page
1 ha 207
.cleanfailed_mem:
9 halyavin 208
;there is no mem for directory entry, display message.
1 ha 209
    mov    esi,start_not_enough_memory
210
    call   sys_msg_board_str
211
.cleanfailed:                    ;clean process name
9 halyavin 212
;can't read file, clean process name.
213
;this avoid problems with panel application.
1 ha 214
    mov    edi,[new_process_place]
215
    shl    edi,8
216
    add    edi,0x80000
217
    mov    ecx,11
218
    mov    eax,' '
219
    cld
220
    rep    stosb
221
.failed:
9 halyavin 222
;no more slots
1 ha 223
    add    esp,8+4
224
    mov    [application_table_status],0
225
    popad
226
    sti
227
    mov    eax,-1
228
    ret
229
 
230
;-----------------------------------------------------------------------------
231
new_start_application_fl:
232
;input:
233
;  eax - pointer to filename
234
;  ebx - parameters to pass
40 halyavin 235
;  edx - flags
1 ha 236
;result:
237
;  eax - pid of new process
238
;        or 0 if call fails.
5 halyavin 239
    mov    [appl_path],edi
240
    mov    [appl_path_size],36
1 ha 241
    pushad
242
    mov    esi,new_process_loading
243
    call   sys_msg_board_str     ;write to debug board
244
 
245
;wait application_table_status mutex
246
.table_status:
247
    cli
248
    cmp    [application_table_status],0
249
    jz     .stf
250
    sti
251
    call   change_task
252
    jmp    .table_status
253
.stf:
254
    call   set_application_table_status
255
;we can change system tables now
256
    push   ebx
257
    push   eax
258
    call   find_new_process_place ;find empty process slot
4 poddubny 259
    call   safe_sti
1 ha 260
    test   eax,eax
261
    jz     .failed
262
 
263
    mov    edi,eax
264
    shl    edi,8
265
    add    edi,0x80000
266
    mov    ecx,256/4
267
    xor    eax,eax
268
    cld
269
    rep    stosd                 ;clean extended information about process
270
 
271
;set new process name
272
    mov    eax,[esp]             ;eax - pointer to file name
273
    mov    ebx,[new_process_place]
274
    shl    ebx,8
275
    add    ebx,0x80000
276
    mov    ecx,11
277
    call   memmove
278
 
279
;read header of file
280
    mov    ebx,1                 ;index of first block
281
    mov    ecx,2                 ;number of blocks
282
    mov    edx,0x90000           ;temp area
283
    mov    esi,12                ;file name length
284
    cli
285
    call   fileread              ;read file from RD
4 poddubny 286
    call   safe_sti
1 ha 287
    cmp    eax,0
288
    jne    .cleanfailed
289
;check MENUET signature
290
    cmp    [0x90000],dword 'MENU'
291
    jnz    .cleanfailed
292
    cmp    [0x90004],word 'ET'
293
    jnz    .cleanfailed
294
 
295
    call   get_app_params        ;parse header fields
296
    cmp    esi,0
21 poddubny 297
    jz     .cleanfailed
1 ha 298
 
299
    mov    eax,[new_process_place]
300
    call   create_app_cr3_table   ;create page directory for new process
301
    test   eax,eax
302
    jz     .cleanfailed_mem
303
 
304
    call   MEM_Get_Linear_Address ;calculate linear address of it
305
 
306
    mov    ebx,std_application_base_address
307
    mov    ecx,[app_mem]
308
    add    ecx,4095
309
    shr    ecx,12
310
    mov    edx,eax
311
    call   mem_alloc_specified_region ;allocate memory for application
312
    test   eax,eax
9 halyavin 313
    jz     .cleanfailed_mem1
1 ha 314
 
315
    mov    eax,[edx+(std_application_base_address shr 20)]
316
    and    eax,not (4096-1)      ;eax - physical address of first (for application memory) page table
317
    call   MEM_Get_Linear_Address
318
    mov    edx,eax
319
 
320
;read file
321
    mov    ebx,1
322
    mov    esi,12                ;length of file name
323
.loop1:
324
;edx = linear address of current page table entry
325
;ebx = index of current block in file
326
    push   edx
327
    mov    eax,[edx]
328
    and    eax,not (4096-1)
329
    call   MEM_Get_Linear_Address
330
    mov    edx,eax               ;read file block to current page
331
    mov    eax,[esp+4]           ;restore pointer to file name
332
    mov    ecx,8                 ;number of blocks read
333
    push   ebx
334
    cli
335
    call   fileread
336
;ebx=file size
4 poddubny 337
    call   safe_sti
1 ha 338
    pop    ecx
339
    shr    ebx,9
340
    cmp    ecx,ebx
341
    jg     .endloop1             ;if end of file?
342
    mov    ebx,ecx
343
    test   eax,eax
344
    jnz    .endloop1             ;check io errors
345
    pop    edx
346
    add    ebx,8                 ;go to next page
347
    add    edx,4
348
    jmp    .loop1
349
 
350
.endloop1:
351
    add    esp,8                 ;pop linear address of page table entry and pointer to file name
352
    call   .add_app_parameters
353
    mov    [esp+28],eax
354
    popad
355
    ret
356
 
9 halyavin 357
.cleanfailed_mem1:
358
;there is mem for directory entry, but there is no mem for pages
359
;so free directory entry
360
    mov    eax,[new_process_place]
361
    shl    eax,8
362
    mov    eax,[0x80000+eax+0xB8]
363
    call   MEM_Free_Page
1 ha 364
.cleanfailed_mem:
9 halyavin 365
;there is no mem for directory entry, display message.
1 ha 366
    mov    esi,start_not_enough_memory
367
    call   sys_msg_board_str
368
.cleanfailed:                    ;clean process name
9 halyavin 369
;can't read file, clean process name.
370
;this avoid problems with panel application.
1 ha 371
    mov    edi,[new_process_place]
372
    shl    edi,8
373
    add    edi,0x80000
374
    mov    ecx,11
375
    mov    eax,' '
376
    cld
377
    rep    stosb
378
.failed:
9 halyavin 379
;no more slots
1 ha 380
    add    esp,8
381
    mov    [application_table_status],0
382
    popad
4 poddubny 383
    call   safe_sti
1 ha 384
    mov    eax,-1
385
    ret
386
 
387
.add_app_parameters:
388
;input:
389
;  [esp] - pointer to parameters
390
;  [esp+4]-[esp+36] pushad registers.
391
;result
392
;  eax - pid of new process
393
;        or zero if failed
394
    cli
395
    mov    ebx,[new_process_place]
396
    cmp    ebx,[0x3004]
397
    jle    .noinc
398
    inc    dword [0x3004]        ;update number of processes
399
.noinc:
400
 
401
;   mov    ebx,[new_process_place]
402
;set 0x8c field of extended information about process
403
;(size of application memory)
404
    shl    ebx,8
405
    mov    eax,[app_mem]
406
    mov    [second_base_address+0x80000+0x8c+ebx],eax
407
;set 0x10 field of information about process
408
;(application base address)
409
;    mov    ebx,[new_process_place]
410
;    shl    ebx,5
411
    shr    ebx,3
412
    mov    dword [second_base_address+0x3000+ebx+0x10],std_application_base_address
413
 
414
;add command line parameters
415
.add_command_line:
416
    mov    edx,[app_i_param]
417
    test   edx,edx
418
    jz     .no_command_line      ;application don't need parameters
419
    mov    eax,[esp+4]
420
    test   eax,eax
421
    jz     .no_command_line      ;no parameters specified
422
;calculate parameter length
423
    mov    esi,eax
424
    xor    ecx,ecx
425
.command_line_len:
426
    cmp    byte [esi],0
427
    jz     .command_line_len_end
428
    inc    esi
429
    inc    ecx
430
    cmp    ecx,256
431
    jl     .command_line_len
432
 
433
.command_line_len_end:
434
;ecx - parameter length
435
;edx - address of parameters in new process address space
436
    mov    ebx,eax               ;ebx - address of parameters in our address space
437
    mov    eax,[new_process_place]
438
    call   write_process_memory  ;copy parameters to new process address space
439
 
440
.no_command_line:
5 halyavin 441
;******************************************************************
442
    mov    edx,[app_i_icon]
443
    test   edx,edx
444
    jz     .no_command_line_1      ;application don't need path of file
445
    mov    ebx,[appl_path]
446
    mov    ecx,[appl_path_size]
447
    mov    eax,[new_process_place]
448
    call   write_process_memory  ;copy path of file to new process address space
449
.no_command_line_1:
450
;******************************************************************
1 ha 451
    mov    ebx,[new_process_place]
452
    mov    eax,ebx
453
    shl    ebx,5
454
    add    ebx,0x3000            ;ebx - pointer to information about process
455
    mov    [ebx+0xe],al          ;set window number on screen = process slot
456
 
457
    mov    [ebx],dword 1+2+4     ;set default event flags (see 40 function)
458
 
459
    inc    dword [process_number]
460
    mov    eax,[process_number]
461
    mov    [ebx+4],eax           ;set PID
462
 
463
    mov    ecx,ebx
464
    add    ecx,draw_data-0x3000  ;ecx - pointer to draw data
465
;set draw data to full screen
466
    mov    [ecx+0],dword 0
467
    mov    [ecx+4],dword 0
468
    mov    eax,[0xfe00]
469
    mov    [ecx+8],eax
470
    mov    eax,[0xfe04]
471
    mov    [ecx+12],eax
472
;set cr3 register in TSS of application
473
    mov    ecx,[new_process_place]
474
    shl    ecx,8
475
    mov    eax,[0x800B8+ecx]
476
    add    eax,8+16              ;add flags
477
    mov    [l.cr3],eax
478
 
479
    mov    eax,[app_start]
480
    mov    [l.eip],eax           ;set eip in TSS
481
    mov    eax,[app_esp]
482
    mov    [l.esp],eax           ;set stack in TSS
483
 
484
;gdt
4 poddubny 485
    ;mov    ebx,[new_process_place]
486
    ;shl    ebx,3
1 ha 487
    mov    ax,app_code           ;ax - selector of code segment
4 poddubny 488
    ;add    ax,bx
1 ha 489
    mov    [l.cs],ax
490
    mov    ax,app_data
4 poddubny 491
    ;add    ax,bx                 ;ax - selector of data segment
1 ha 492
    mov    [l.ss],ax
493
    mov    [l.ds],ax
494
    mov    [l.es],ax
495
    mov    [l.fs],ax
496
    mov    ax,graph_data         ;ax - selector of graphic segment
497
    mov    [l.gs],ax
498
    mov    [l.io],word 128
499
    mov    [l.eflags],dword 0x11202
500
    mov    [l.ss0],os_data
4 poddubny 501
    mov    ebx,[new_process_place]
502
    shl    ebx,12
14 poddubny 503
    add    ebx,sysint_stack_data+4096
3 halyavin 504
    mov    [l.esp0],ebx
1 ha 505
 
506
;copy tss to it place
507
    mov    eax,tss_sceleton
508
    mov    ebx,[new_process_place]
509
    imul   ebx,tss_step
510
    add    ebx,tss_data          ;ebx - address of application TSS
511
    mov    ecx,120
512
    call   memmove
513
 
514
;Add IO access table - bit array of permitted ports
515
    or     eax,-1
516
    mov    edi,[new_process_place]
517
    imul   edi,tss_step
518
    add    edi,tss_data+128
519
    mov    ecx,2048
520
    cld
521
    rep    stosd                 ;full access to 2048*8=16384 ports
522
 
523
    mov    ecx,ebx               ;ecx - address of application TSS
524
    mov    edi,[new_process_place]
525
    shl    edi,3
526
;set TSS descriptor
527
    mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
528
    mov    [edi+gdts+tss0+2],cx  ;part of offset
529
    mov    eax,ecx
530
    shr    eax,16
531
    mov    [edi+gdts+tss0+4],al  ;part of offset
532
    mov    [edi+gdts+tss0+7],ah  ;part of offset
533
    mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
534
 
535
 
536
;flush keyboard and buttons queue
537
    mov    [0xf400],byte 0
538
    mov    [0xf500],byte 0
539
 
540
    mov    edi,[new_process_place]
541
    shl    edi,5
542
    add    edi,window_data
543
    mov    ebx,[new_process_place]
544
    movzx  esi,word [0xC000+ebx*2]
545
    lea    esi,[0xC400+esi*2]
546
    call   windowactivate        ;gui initialization
547
 
548
    mov    ebx,[new_process_place]
549
    shl    ebx,5
550
    mov    [0x3000+ebx+0xa],byte 0 ;set process state - running
40 halyavin 551
; set if debuggee
552
    test   byte [esp+28], 1
553
    jz     .no_debug
554
    mov    [0x3000+ebx+0xa],byte 1 ;set process state - suspended
555
    mov    eax,[0x3000]
556
    mov    [0x80000+ebx*8+0xac],eax ;set debugger PID - current
557
.no_debug:
1 ha 558
 
559
    mov    esi,new_process_running
560
    call   sys_msg_board_str     ;output information about succefull startup
561
 
562
;    add    esp,4                 ;pop pointer to parameters
563
;    popad
564
    mov    eax,[process_number]  ;set result
565
    mov    [application_table_status],0 ;unlock application_table_status mutex
4 poddubny 566
    call   safe_sti
1 ha 567
    ret    4
568
;-----------------------------------------------------------------------------
569
new_sys_threads:
570
;eax=1 - create thread
571
;   ebx=thread start
572
;   ecx=thread stack value
573
;result:
574
;   eax=pid
40 halyavin 575
    xor    edx,edx	; flags=0
1 ha 576
    pushad
577
 
578
    cmp    eax,1
579
    jnz    .ret                  ;other subfunctions
580
    mov    esi,new_process_loading
581
    call   sys_msg_board_str
582
;lock application_table_status mutex
583
.table_status:
584
    cli
585
    cmp    [application_table_status],0
586
    je     .stf
587
    sti
588
    call   change_task
589
    jmp    .table_status
590
.stf:
591
    call   set_application_table_status
592
;find free process slot
593
 
594
    call   find_new_process_place
595
    test   eax,eax
596
    jz     .failed
597
 
598
;set parameters for thread
599
    xor    eax,eax
600
    mov    [app_i_param],eax
601
    mov    [app_i_icon],eax
602
    mov    [app_start],ebx
603
    mov    [app_esp],ecx
604
 
605
    mov    esi,[0x3000]
606
    shl    esi,8
607
    add    esi,0x80000
608
    mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
609
 
610
    mov    edi,[new_process_place]
611
    shl    edi,8
612
    add    edi,0x80000
613
    mov    edx,edi               ;edx=edi - pointer to extended infomation about new thread
614
    mov    ecx,256/4
615
    rep    stosd                 ;clean extended information about new thread
616
    mov    edi,edx
617
    mov    ecx,11
618
    rep    movsb                 ;copy process name
619
    mov    eax,[ebx+0x8c]
620
    mov    [app_mem],eax         ;set memory size
621
    mov    eax,[ebx+0xb8]
622
    mov    [edx+0xb8],eax        ;copy page directory
623
;    mov    eax,[new_process_place]
624
;    mov    ebx,[0x3000]
625
;    call   addreference_app_cr3_table
626
 
627
    push   0                     ;no parameters
628
    call    new_start_application_fl.add_app_parameters ;start thread
629
    mov    [esp+28],eax
630
    popad
631
    ret
632
 
633
.failed:
634
    sti
635
    popad
636
    mov    eax,-1
637
    ret
638
.ret:
639
    popad
640
    ret
641
;-----------------------------------------------------------------------------
642
new_mem_resize:
643
;input:
644
;  ebx - new size
645
;result:
646
;  [esp+36]:=0 - normal
647
;  [esp+36]:=1 - error
648
;This function set new application memory size.
649
    mov    esi,ebx               ;save new size
650
    add    ebx,4095
651
    and    ebx,not (4096-1)      ;round up size
652
    mov    ecx,[0x3000]
653
    shl    ecx,8
654
    mov    edx,[0x8008C+ecx]
655
    add    edx,4095
656
    and    edx,not (4096-1)      ;old size
657
    mov    eax,[0x800B8+ecx]
658
    call   MEM_Get_Linear_Address
659
;eax - linear address of page directory
660
    call   MEM_Heap_Lock         ;guarantee that two threads willn't
661
                                 ;change memory size simultaneously
662
    cmp    ebx,edx
663
;    mov    esi,ebx               ;save new size
664
    jg     .expand
665
 
666
.free:
667
    sub    edx,ebx
668
    jz     .unlock               ;do nothing
669
    mov    ecx,edx
670
    shr    ecx,12
671
    add    ebx,std_application_base_address
672
    call   mem_free_specified_region  ;free unnecessary pages
673
    jmp    .unlock
674
 
675
.expand:
676
    sub    ebx,edx
677
    mov    ecx,ebx
678
    shr    ecx,12
679
    mov    ebx,edx
680
    add    ebx,std_application_base_address
681
    call   mem_alloc_specified_region ;alloc necessary pages
682
    test   eax,eax
683
    jz     .failed               ;not enough memory
684
 
685
.unlock:
686
    mov    ebx,esi
687
    mov    eax,[0x3000]
688
    shl    eax,8
689
    mov    [eax+0x8008c],ebx     ;write new memory size
690
;search threads and update
691
;application memory size infomation
692
    mov    ecx,[eax+0x800b8]
693
    mov    eax,2
694
 
695
.search_threads:
696
;eax = current slot
697
;ebx = new memory size
698
;ecx = page directory
699
    cmp    eax,[0x3004]
700
    jg     .search_threads_end
701
    mov    edx,eax
702
    shl    edx,5
703
    cmp    word [0x3000+edx+0xa],9 ;if slot empty?
704
    jz     .search_threads_next
705
    shl    edx,3
706
    cmp    [edx+0x800b8],ecx     ;if it is our thread?
707
    jnz    .search_threads_next
708
    mov    [edx+0x8008c],ebx     ;update memory size
709
.search_threads_next:
710
    inc    eax
711
    jmp    .search_threads
712
.search_threads_end:
713
 
714
    call   MEM_Heap_UnLock
715
    mov    dword [esp+36],0
716
    ret
717
 
718
.failed:
719
    call   MEM_Heap_UnLock
720
    mov    dword [esp+36],1
721
    ret
722
;-----------------------------------------------------------------------------
723
pid_to_slot:
724
;Input:
725
;  eax - pid of process
726
;Output:
727
;  eax - slot of process or 0 if process don't exists
728
;Search process by PID.
729
    push   ebx
730
    push   ecx
731
    mov    ebx,[0x3004]
732
    shl    ebx,5
733
    mov    ecx,2*32
734
 
735
.loop:
736
;ecx=offset of current process info entry
737
;ebx=maximum permitted offset
738
    cmp    byte [second_base_address+0x3000+ecx+0xa],9
739
    jz     .endloop              ;skip empty slots
740
    cmp    [second_base_address+0x3000+ecx+0x4],eax ;check PID
741
    jz     .pid_found
742
.endloop:
743
    add    ecx,32
744
    cmp    ecx,ebx
745
    jle    .loop
746
 
747
    pop    ecx
748
    pop    ebx
749
    xor    eax,eax
750
    ret
751
 
752
.pid_found:
753
    shr    ecx,5
754
    mov    eax,ecx               ;convert offset to index of slot
755
    pop    ecx
756
    pop    ebx
757
    ret
758
;-----------------------------------------------------------------------------
759
is_new_process:
760
;Input:
761
;  eax - process slot
762
;Output:
763
;  eax=1 - it is new process
764
;  eax=0 - it is old process
765
;    shl   eax,5
766
;    mov   eax,[second_base_address+0x3000+eax+0x10]
767
;    cmp   eax,std_application_base_address  ;check base address of application
768
;    jz    .new_process
769
;    xor   eax,eax
770
;    ret
771
 
772
;.new_process:
773
    mov   eax,1
774
    ret
775
;-----------------------------------------------------------------------------
776
write_process_memory:
777
;Input:
778
;  eax - process slot
779
;  ebx - buffer address
780
;  ecx - buffer size
781
;  edx - start address in other process
782
;Output:
783
;  eax - number of bytes written
784
    pushad
785
    shl  eax,8
786
    mov  eax,[0x80000+eax+0xB8]
787
    call MEM_Get_Linear_Address
788
    mov  ebp,eax
789
;ebp=linear address of page directory of other process.
790
    add  edx,std_application_base_address  ;convert to linear address
791
    test ecx,ecx
792
    jle  .ret
793
 
794
.write_loop:
795
;ebx = current buffer address
796
;ecx>0 = current size
797
;edx = current address in other process
798
;ebp = linear address of page directory
799
 
800
    call MEM_Heap_Lock           ;cli
801
    mov  esi,edx
802
    shr  esi,22
803
    mov  eax,[ebp+4*esi]         ;find page directory entry
804
    and  eax,not (4096-1)        ;clear flags
805
    test eax,eax
806
    jz   .page_not_found
807
    call MEM_Get_Linear_Address  ;calculate linear address of page table
808
    test eax,eax
809
    jz   .page_not_found
810
    mov  esi,edx
811
    shr  esi,12
812
    and  esi,1023
813
    mov  eax,[eax+4*esi]         ;find page table entry
814
    and  eax,not (4096-1)
815
    test eax,eax
816
    jz   .page_not_found
817
    call MEM_Get_Linear_Address  ;calculate linear address of page
818
    test eax,eax
819
    jz   .page_not_found
820
    mov  edi,eax
821
    call MEM_Add_Reference_Linear;guarantee that page willn't disappear
822
    call MEM_Heap_UnLock         ;sti
823
 
824
    mov  esi,edx
825
    and  esi,4095
826
    add  edi,esi                 ;add offset in page
827
;edi = linear address corresponding edx in other process
828
    sub  esi,4096
829
    neg  esi                     ;esi - number of remaining bytes in page
830
    cmp  esi,ecx
831
    jl   .min_ecx
832
    mov  esi,ecx
833
.min_ecx:                        ;esi=min(ecx,esi) - number of bytes to write
834
    sub  ecx,esi
835
    push ecx
836
    mov  ecx,esi                 ;ecx - number of bytes to write
837
    mov  esi,ebx                 ;esi - source, edi - destination
838
    add  edx,ecx                 ;move pointer in address space of other process
839
    push edi
840
 
841
;move ecx bytes
842
    test ecx,3
843
    jnz  .not_aligned
844
    shr  ecx,2
845
    rep  movsd
846
    jmp  .next_iter
847
.not_aligned:
848
    rep  movsb
849
.next_iter:
850
 
851
    pop  eax
852
    and  eax,not (4096-1)        ;eax - linear address of current page
853
    call MEM_Free_Page_Linear    ;free reference
854
    mov  ebx,esi                 ;new pointer to buffer - movsb automaticaly advance it.
855
    pop  ecx                     ;restore number of remaining bytes
856
    test ecx,ecx
857
    jnz  .write_loop
858
.ret:
859
    popad
860
    mov  eax,ecx
861
    ret
862
 
863
.page_not_found:
864
    call MEM_Heap_UnLock         ;error has appeared in critical region
865
    sub  ecx,[esp+24]            ;[esp+24]<-->ecx
866
    neg  ecx                     ;ecx=number_of_written_bytes
867
    mov  [esp+28],ecx            ;[esp+28]<-->eax
868
    popad
869
    ret
870
;-----------------------------------------------------------------------------
871
syscall_test:
872
;for testing memory manager from applications.
873
    mov  edx,ecx
874
    mov  ecx,ebx
875
    call trans_address
876
    mov  ebx,eax
877
    mov  eax,[0x3000]
878
    call read_process_memory
879
    ret
880
;-----------------------------------------------------------------------------
881
read_process_memory:
882
;Input:
883
;  eax - process slot
884
;  ebx - buffer address
885
;  ecx - buffer size
886
;  edx - start address in other process
887
;Output:
888
;  eax - number of bytes read.
889
    pushad
890
    shl  eax,8
891
    mov  eax,[0x80000+eax+0xB8]
892
    call MEM_Get_Linear_Address
893
    mov  ebp,eax
894
    add  edx,std_application_base_address
895
.read_loop:
896
;ebx = current buffer address
897
;ecx>0 = current size
898
;edx = current address in other process
899
;ebp = linear address of page directory
900
 
901
    call MEM_Heap_Lock           ;cli
902
    mov  esi,edx
903
    shr  esi,22
904
    mov  eax,[ebp+4*esi]         ;find page directory entry
905
    and  eax,not (4096-1)
906
    test eax,eax
907
    jz   .page_not_found
908
    call MEM_Get_Linear_Address
909
    test eax,eax
910
    jz   .page_not_found
911
    mov  esi,edx
912
    shr  esi,12
913
    and  esi,1023
914
    mov  eax,[eax+4*esi]         ;find page table entry
915
    and  eax,not (4096-1)
916
    test eax,eax
917
    jz   .page_not_found
918
    call MEM_Get_Linear_Address  ;calculate linear address of page
919
    test eax,eax
920
    jz   .page_not_found
921
    mov  esi,eax
922
    call MEM_Add_Reference_Linear;guarantee that page willn't disappear
923
    call MEM_Heap_UnLock         ;sti
924
 
925
    mov  edi,edx
926
    and  edi,4095
927
    add  esi,edi                 ;add offset in page
928
;esi = linear address corresponding edx in other process
929
    sub  edi,4096
930
    neg  edi
931
 
932
;edi=min(edi,ecx) - number of bytes to copy
933
    cmp  edi,ecx
934
    jl   .min_ecx
935
    mov  edi,ecx
936
.min_ecx:
937
 
938
    sub  ecx,edi                 ;update size of remaining bytes
939
    add  edx,edi                 ;update current pointer in other address space.
940
    push ecx
941
    mov  ecx,edi                 ;ecx - number of bytes to read
942
    mov  edi,ebx                 ;esi - source, edi - destination
943
    push esi
944
;move ecx bytes
945
    test ecx,3
946
    jnz  .not_aligned
947
    shr  ecx,2
948
    rep  movsd
949
    jmp  .next_iter
950
.not_aligned:
951
    rep  movsb
952
.next_iter:
953
    pop  eax
954
    and  eax,not (4096-1)        ;eax - linear address of current page
955
    call MEM_Free_Page_Linear    ;free reference
956
    mov  ebx,edi                 ;new pointer to buffer - movsb automaticaly advance it.
957
    pop  ecx                     ;restore number of remaining bytes
958
    test ecx,ecx
959
    jnz  .read_loop
960
 
961
    popad
962
    mov  eax,ecx
963
    ret
964
 
965
.page_not_found:
966
    call MEM_Heap_UnLock         ;error has appeared in critical region
967
    sub  ecx,[esp+24]            ;[esp+24]<-->ecx
968
    neg  ecx                     ;ecx=number_of_read_bytes
969
    mov  [esp+28],ecx            ;[esp+28]<-->eax
970
    popad
971
    ret
972
;-----------------------------------------------------------------------------
973
check_region:
974
;input:
975
;  ebx - start of buffer
976
;  ecx - size of buffer
977
;result:
978
;  eax = 1 region lays in app memory
979
;  eax = 0 region don't lays in app memory
980
    mov  eax,[0x3000]
981
    jmp  check_process_region
982
;-----------------------------------------------------------------------------
983
check_process_region:
984
;input:
985
;  eax - slot
986
;  ebx - start of buffer
987
;  ecx - size of buffer
988
;result:
989
;  eax = 1 region lays in app memory
990
;  eax = 0 region don't lays in app memory
991
    test ecx,ecx
992
    jle  .ok
993
    shl  eax,5
994
    cmp  word [0x3000+eax+0xa],0
995
    jnz  .failed
996
    shl  eax,3
997
    mov  eax,[0x80000+eax+0xb8]
998
    test eax,eax
999
    jz   .failed
1000
    call MEM_Get_Linear_Address
1001
    push ebx
1002
    push ecx
1003
    push edx
1004
    mov  edx,ebx
1005
    and  edx,not (4096-1)
1006
    sub  ebx,edx
1007
    add  ecx,ebx
1008
    mov  ebx,edx
1009
    add  ecx,(4096-1)
1010
    and  ecx,not (4096-1)
1011
.loop:
1012
;eax - linear address of page directory
1013
;ebx - current page
1014
;ecx - current size
1015
    mov  edx,ebx
1016
    shr  edx,22
1017
    mov  edx,[eax+4*edx]
1018
    and  edx,not (4096-1)
1019
    test edx,edx
1020
    jz   .failed1
1021
    push eax
1022
    mov  eax,edx
1023
    call MEM_Get_Linear_Address
1024
    mov  edx,ebx
1025
    shr  edx,12
1026
    and  edx,(1024-1)
1027
    mov  eax,[eax+4*edx]
1028
    and  eax,not (4096-1)
1029
    test eax,eax
1030
    pop  eax
1031
    jz   .failed1
1032
    add  ebx,4096
1033
    sub  ecx,4096
1034
    jg   .loop
1035
    pop  edx
1036
    pop  ecx
1037
    pop  ebx
1038
.ok:
1039
    mov  eax,1
1040
    ret
1041
 
1042
.failed1:
1043
    pop  edx
1044
    pop  ecx
1045
    pop  ebx
1046
.failed:
1047
    xor  eax,eax
1048
    ret
1049
;-----------------------------------------------------------------------------
1050
new_sys_ipc:
1051
;input:
1052
;  eax=1 - set ipc buffer area
1053
;    ebx=address of buffer
1054
;    ecx=size of buffer
1055
;  eax=2 - send message
1056
;    ebx=PID
1057
;    ecx=address of message
1058
;    edx=size of message
1059
    cmp  eax,1
1060
    jnz  .no_ipc_def
1061
;set ipc buffer area
1062
    mov  edi,[0x3000]
1063
    shl  edi,8
1064
    add  edi,0x80000
1065
    cli
1066
    mov  [edi+0xA0],ebx          ;set fields in extended information area
1067
    mov  [edi+0xA4],ecx
1068
    sti
1069
    mov  [esp+36],dword 0        ;success
1070
    ret
1071
 
1072
.no_ipc_def:
1073
    cmp  eax,2
1074
    jnz  .no_ipc_send
1075
;send message
1076
    cli
1077
;obtain slot from PID
1078
    mov  eax,ebx
1079
    call pid_to_slot
1080
    test eax,eax
1081
    jz   .no_pid
1082
    mov  ebp,eax
1083
;ebp = slot of other process
1084
    shl  eax,8
1085
    mov  edi,[eax+0x80000+0xa0]  ;is ipc area defined?
1086
    test edi,edi
1087
    jz   .no_ipc_area
1088
    mov  esi,[eax+0x80000+0xa4]  ;esi - size of buffer
1089
    push dword -1                ;temp variable for read_process_memory
1090
    mov  ebx,esp
1091
    push ecx
1092
    push edx
1093
    mov  ecx,4                   ;read 4 bytes
1094
    mov  eax,ebp
1095
    mov  edx,edi                 ;from beginning of buffer.
1096
    call read_process_memory
1097
    mov  eax,[esp+8]
1098
    test eax,eax
1099
    jnz  .ipc_blocked            ;if dword [buffer]<>0 - ipc blocked now
1100
    add  edx,4                   ;move to next 4 bytes
1101
    mov  eax,ebp
1102
    call read_process_memory     ;read size of occupied space in buffer
1103
    sub  esi,8
1104
    sub  esi,[esp]
1105
    sub  esi,[esp+8]             ;esi=(buffer size)-(occupied size)-(message size)-(header of message size)
1106
    js   .buffer_overflow        ;esi<0 - not enough memory in buffer
1107
    mov  esi,[esp+8]             ;previous offset
1108
    add  dword [esp+8],8
1109
    mov  edi,[esp]
1110
    add  [esp+8],edi             ;add (size of message)+(size of header of message) to [buffer+4]
1111
    mov  eax,ebp
1112
    call write_process_memory
1113
    add  edx,esi
1114
    sub  edx,4                   ;move to beginning of place for our message
1115
    mov  eax,[second_base_address+0x3010]
1116
    mov  eax,[eax+0x4]           ;eax - our PID
1117
    mov  [esp+8],eax
1118
    mov  eax,ebp
1119
    call write_process_memory    ;write PID
1120
    mov  ebx,esp                 ;address of size of message
1121
    mov  eax,ebp
1122
    add  edx,4
1123
    call write_process_memory    ;write size of message
1124
    add  edx,4
1125
    pop  ecx                     ;ecx - size of message
1126
    pop  eax
1127
    call trans_address
1128
    mov  ebx,eax                 ;ebx - linear address of message
1129
    add  esp,4                   ;pop temporary variable
1130
    mov  eax,ebp
1131
    call write_process_memory    ;write message
1132
    sti
1133
;awake other process
1134
    shl  ebp,8
1135
    mov  eax,ebp
1136
    or   [eax+0x800A8],dword 0x40
1137
 
1138
    cmp  dword [check_idle_semaphore],20
1139
    jge  .ipc_no_cis
1140
    mov  dword [check_idle_semaphore],5
1141
.ipc_no_cis:
1142
    mov  dword [esp+36],0
1143
    ret
1144
.no_ipc_send:
1145
    mov  dword [esp+36],-1
1146
    ret
1147
.no_pid:
1148
    sti
1149
    mov  dword [esp+36],4
1150
    ret
1151
.no_ipc_area:
1152
    sti
1153
    mov  dword [esp+36],1
1154
    ret
1155
.ipc_blocked:
1156
    sti
1157
    add  esp,12
1158
    mov  dword [esp+36],2
1159
    ret
1160
.buffer_overflow:
1161
    sti
1162
    add  esp,12
1163
    mov  dword [esp+36],3
1164
    ret
1165
;-----------------------------------------------------------------------------
1166
trans_address:
1167
;Input
1168
;  eax - application address
1169
;Output
1170
;  eax - linear address for kernel
1171
    add   eax,std_application_base_address
1172
    ret
1173
;-----------------------------------------------------------------------------
1174
new_start_application_hd:
1175
;eax - file name (kernel address)
1176
;ebx - file name length
1177
;ecx - work area (kernel address)
40 halyavin 1178
;edx - flags
1 ha 1179
;ebp - parameters
5 halyavin 1180
    mov    [appl_path],edi
1 ha 1181
    pushad
1182
 
1183
    mov    esi,new_process_loading
1184
    call   sys_msg_board_str     ;write message to message board
1185
 
1186
;lock application_table_status mutex
1187
.table_status:
1188
    cli
1189
    cmp    [application_table_status],0
1190
    jz     .stf
1191
    sti
1192
    call   change_task
1193
    jmp    .table_status
1194
.stf:
1195
    call   set_application_table_status
1196
 
1197
    push   ebp
1198
    push   ebx
1199
    push   eax
1200
    push   ecx
1201
    call   find_new_process_place ;find new process slot
1202
    sti
1203
    test   eax,eax
1204
    jz     .failed
1205
 
5 halyavin 1206
;write application name
1207
    xor    eax,eax
1208
    mov    [appl_path_size],eax
1 ha 1209
    mov    eax,[esp+4]
1210
.find_last_byte:
1211
    cmp    byte [eax],0
1212
    jz     .find_last_byte_end
1213
    inc    eax
5 halyavin 1214
    inc    [appl_path_size]
1 ha 1215
    jmp    .find_last_byte
5 halyavin 1216
.find_last_byte_end:
1217
    add    [appl_path_size],24
1 ha 1218
    lea    esi,[eax-11]          ;last 11 bytes = application name
1219
    mov    edi,[new_process_place]
1220
    shl    edi,8
1221
    add    edi,0x80000
1222
    mov    ecx,11
1223
    cld
1224
    rep    movsb                 ;copy name to extended information about process
1225
 
1226
;read header
1227
    mov    eax,[esp+4]           ;file name
1228
    mov    esi,[esp]             ;work area
1229
    mov    ecx,1                 ;read from first block
1230
    mov    edx,1                 ;read 1 block
1231
    call   read_hd_file
1232
    test   eax,eax
1233
    jnz    .cleanfailed
1234
 
1235
    mov    esi,[esp]
1236
;check menuet signature
1237
    cmp    [esi+1024+0],dword 'MENU'  ;read_hd_file function write file to +1024 offset
1238
    jnz    .cleanfailed
1239
    cmp    [esi+1024+4],word 'ET'
1240
    jnz    .cleanfailed
1241
    add    esi,1024
1242
    mov    edi,0x90000
1243
    mov    ecx,512/4
1244
    cld
1245
    rep    movsd                 ;copy first block to 0x90000 address for get_app_params function
1246
    call   get_app_params
1247
    test   esi,esi
1248
    jz     .cleanfailed
1249
 
1250
    mov    eax,[new_process_place]
1251
    call   create_app_cr3_table  ;create page directory
1252
    test   eax,eax
1253
    jz     .cleanfailed_mem
1254
 
1255
    call   MEM_Get_Linear_Address
1256
 
1257
    mov    ebx,std_application_base_address
1258
    mov    ecx,[app_mem]
1259
    add    ecx,4096-1
1260
    shr    ecx,12
1261
    mov    edx,eax               ;edx - linear address of page directory
1262
    call   mem_alloc_specified_region ;allocate memory for application
1263
    test   eax,eax
9 halyavin 1264
    jz     .cleanfailed_mem1
1 ha 1265
 
1266
    add    edx,(std_application_base_address shr 20)
1267
    mov    eax,[edx]
1268
    and    eax,not (4096-1)
1269
    call   MEM_Get_Linear_Address
1270
    push   edx                   ;save pointer to first page table
1271
    mov    edx,eax
1272
;read file
1273
    mov    ecx,1
1274
    xor    ebp,ebp
1275
.loop1:
1276
;[esp] - pointer to current page directory entry
1277
;edx - pointer to current page table
1278
;ebp - offset in page
1279
;ecx - current cluster
1280
    push   edx
1281
    mov    eax,[esp+12]          ;file name
1282
    mov    ebx,[esp+16]          ;file name length
1283
    mov    esi,[esp+8]           ;work area
1284
    mov    edx,1                 ;number of blocks to read
1285
    push   ecx
1286
    push   ebp
1287
    cli
1288
    call   read_hd_file
1289
    sti
1290
    pop    ebp
1291
    test   eax,eax
1292
    jnz    .endloop1             ;check io errors
1293
 
1294
    mov    esi,[esp+8+4]         ;work area
1295
    add    esi,1024
1296
    mov    eax,[esp+4]           ;current page table
1297
    mov    eax,[eax]
1298
    and    eax,not (4096-1)
1299
    call   MEM_Get_Linear_Address;calculate linear page address
1300
    lea    edi,[eax+ebp]         ;add page offset
1301
    mov    ecx,512/4
1302
    cld
1303
    rep    movsd                 ;copy data
1304
 
1305
    pop    ecx
1306
    inc    ecx                   ;next block
1307
    mov    eax,[app_i_end] ;todo: precalculate ([app_i_end]+4095)/4096
1308
    add    eax,512-1
1309
    shr    eax,9                 ;calculate application image size
1310
    cmp    ecx,eax
1311
    jg     .endloop11
1312
    pop    edx
1313
    add    ebp,512               ;new offset
1314
    test   ebp,4096
1315
    jz     .loop1
1316
    xor    ebp,ebp
1317
    add    edx,4                 ;go to next page
1318
    test   edx,(4096-1)
1319
    jnz    .loop1
1320
    add    dword [esp],4         ;go to next directory entry
1321
    mov    eax,[esp]
1322
    mov    eax,[eax]
1323
    and    eax,not (4096-1)
1324
    call   MEM_Get_Linear_Address
1325
    mov    edx,eax
1326
    jmp    .loop1
1327
.endloop1:
1328
    add    esp,4                 ;pop ecx
1329
.endloop11:
1330
    add    esp,4+4               ;pop edx, pop edx
1331
 
1332
;add_app_parameters
1333
    add    esp,12                ;now pointer to parameters is on the top of the stack
1334
    call   new_start_application_fl.add_app_parameters ;start process
1335
    mov    [esp+28],eax
1336
    popad
1337
    ret
1338
 
9 halyavin 1339
.cleanfailed_mem1:
1340
;there is mem for directory entry, but there is no mem for pages
1341
;so free directory entry
1342
    mov    eax,[new_process_place]
1343
    shl    eax,8
1344
    mov    eax,[0x80000+eax+0xB8]
1345
    call   MEM_Free_Page
1 ha 1346
.cleanfailed_mem:
9 halyavin 1347
;there is no mem for directory entry, display message.
1 ha 1348
    mov    esi,start_not_enough_memory
1349
    call   sys_msg_board_str
1350
.cleanfailed:                    ;clean process name
9 halyavin 1351
;can't read file, clean process name.
1352
;this avoid problems with panel application.
1 ha 1353
    mov    edi,[new_process_place]
1354
    shl    edi,8
1355
    add    edi,0x80000
1356
    mov    ecx,11
1357
    mov    eax,' '
1358
    cld
1359
    rep    stosb
1360
.failed:
9 halyavin 1361
;no more slots
1 ha 1362
    add    esp,16
1363
    popad
1364
    mov    eax,-1
1365
    mov    [application_table_status],0
1366
    sti
1367
    ret
1368
end if
40 halyavin 1369
 
1370
	include 'debug.inc'