Subversion Repositories Kolibri OS

Rev

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

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