Subversion Repositories Kolibri OS

Rev

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

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