Subversion Repositories Kolibri OS

Rev

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

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