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