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