Subversion Repositories Kolibri OS

Rev

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

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