Subversion Repositories Kolibri OS

Rev

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