Subversion Repositories Kolibri OS

Rev

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

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