Subversion Repositories Kolibri OS

Rev

Rev 593 | Rev 750 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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