Subversion Repositories Kolibri OS

Rev

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

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