Subversion Repositories Kolibri OS

Rev

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

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