Subversion Repositories Kolibri OS

Rev

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

Rev 9930 Rev 9932
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
4
;;  Distributed under terms of the GNU General Public License.  ;;
4
;;  Distributed under terms of the GNU General Public License.  ;;
5
;;                                                              ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
7
 
8
$Revision: 9930 $
8
$Revision: 9932 $
9
 
9
 
10
 
10
 
11
GREEDY_KERNEL  = 0
11
GREEDY_KERNEL  = 0
12
 
12
 
13
struct  APP_HEADER_00_
13
struct  APP_HEADER_00_
14
        banner          dq ?
14
        banner          dq ?
15
        version         dd ?    ;+8
15
        version         dd ?    ;+8
16
        start           dd ?    ;+12
16
        start           dd ?    ;+12
17
        i_end           dd ?    ;+16
17
        i_end           dd ?    ;+16
18
        mem_size        dd ?    ;+20
18
        mem_size        dd ?    ;+20
19
        i_param         dd ?    ;+24
19
        i_param         dd ?    ;+24
20
ends
20
ends
21
 
21
 
22
struct  APP_HEADER_01_
22
struct  APP_HEADER_01_
23
        banner          dq ?
23
        banner          dq ?
24
        version         dd ?    ;+8
24
        version         dd ?    ;+8
25
        start           dd ?    ;+12
25
        start           dd ?    ;+12
26
        i_end           dd ?    ;+16
26
        i_end           dd ?    ;+16
27
        mem_size        dd ?    ;+20
27
        mem_size        dd ?    ;+20
28
        stack_top       dd ?    ;+24
28
        stack_top       dd ?    ;+24
29
        i_param         dd ?    ;+28
29
        i_param         dd ?    ;+28
30
        i_icon          dd ?    ;+32
30
        i_icon          dd ?    ;+32
31
ends
31
ends
32
 
32
 
33
struct  APP_HDR
33
struct  APP_HDR
34
        cmdline         rd 1    ;0x00
34
        cmdline         rd 1    ;0x00
35
        path            rd 1    ;0x04
35
        path            rd 1    ;0x04
36
        eip             rd 1    ;0x08
36
        eip             rd 1    ;0x08
37
        esp             rd 1    ;0x0C
37
        esp             rd 1    ;0x0C
38
        _edata          rd 1    ;0x10
38
        _edata          rd 1    ;0x10
39
        _emem           rd 1    ;0x14
39
        _emem           rd 1    ;0x14
40
        img_base        rd 1    ;0x18
40
        img_base        rd 1    ;0x18
41
        img_size        rd 1
41
        img_size        rd 1
42
        filename_size   rd 1
42
        filename_size   rd 1
43
        cmdline_size    rd 1
43
        cmdline_size    rd 1
44
        path_string     rd 1
44
        path_string     rd 1
45
ends
45
ends
46
 
46
 
47
macro _clear_ op
47
macro _clear_ op
48
{  mov ecx, op/4
48
{  mov ecx, op/4
49
        xor     eax, eax
49
        xor     eax, eax
50
        cld
50
        cld
51
        rep stosd
51
        rep stosd
52
}
52
}
53
 
53
 
54
align 4
54
align 4
55
_strnlen:
55
_strnlen:
56
        mov     edx, ecx
56
        mov     edx, ecx
57
        xor     eax, eax
57
        xor     eax, eax
58
        repne scasb
58
        repne scasb
59
        jne     @F
59
        jne     @F
60
        inc     ecx
60
        inc     ecx
61
@@:
61
@@:
62
        mov     eax, edx
62
        mov     eax, edx
63
        sub     eax, ecx
63
        sub     eax, ecx
64
        retn
64
        retn
65
 
65
 
66
fs_execute_from_sysdir:
66
fs_execute_from_sysdir:
67
        xor     ebx, ebx
67
        xor     ebx, ebx
68
fs_execute_from_sysdir_param:
68
fs_execute_from_sysdir_param:
69
        stdcall kernel_alloc, maxPathLength
69
        stdcall kernel_alloc, maxPathLength
70
        push    eax ebx
70
        push    eax ebx
71
        mov     esi, ebp
71
        mov     esi, ebp
72
        mov     edi, eax
72
        mov     edi, eax
73
        xor     eax, eax
73
        xor     eax, eax
74
        call    getFullPath
74
        call    getFullPath
75
        pop     ecx ebx
75
        pop     ecx ebx
76
        xor     edx, edx
76
        xor     edx, edx
77
; @brief Executes a program
77
; @brief Executes a program
78
; @param edx Flags
78
; @param edx Flags
79
; @param ecx Commandline
79
; @param ecx Commandline
80
; @param ebx Absolute file path
80
; @param ebx Absolute file path
81
; @param eax String length
81
; @param eax String length
82
; @returns Negated error code or new process number
82
; @returns Negated error code or new process number
83
proc fs_execute
83
proc fs_execute
84
    locals
84
    locals
85
        cmdline         rd  1
85
        cmdline         rd  1
86
        flags           rd  1
86
        flags           rd  1
87
        slot            rd  1  ; index of new thread slot
87
        slot            rd  1  ; index of new thread slot
88
        slot_base       rd  1  ; base address of it
88
        slot_base       rd  1  ; base address of it
89
; app header data
89
; app header data
90
        hdr_cmdline     rd  1
90
        hdr_cmdline     rd  1
91
        hdr_path        rd  1
91
        hdr_path        rd  1
92
        hdr_eip         rd  1
92
        hdr_eip         rd  1
93
        hdr_esp         rd  1
93
        hdr_esp         rd  1
94
        hdr_edata       rd  1
94
        hdr_edata       rd  1
95
        hdr_emem        rd  1
95
        hdr_emem        rd  1
96
        file_base       rd  1
96
        file_base       rd  1
97
        file_size       rd  1
97
        file_size       rd  1
98
        filename_size   rd  1
98
        filename_size   rd  1
99
        cmdline_size    rd  1
99
        cmdline_size    rd  1
100
        path_string     rd  1
100
        path_string     rd  1
101
    endl
101
    endl
102
 
102
 
103
        and     edx, 1  ;clear flags user. TODO: add api for start apps with user flags
103
        and     edx, 1  ;clear flags user. TODO: add api for start apps with user flags
104
        mov     [flags], edx
104
        mov     [flags], edx
105
        mov     [cmdline], ecx
105
        mov     [cmdline], ecx
106
        mov     [path_string], ebx
106
        mov     [path_string], ebx
107
        mov     [filename_size], eax
107
        mov     [filename_size], eax
108
        mov     esi, -ERROR_FILE_NOT_FOUND
108
        mov     esi, -ERROR_FILE_NOT_FOUND
109
        test    eax, eax
109
        test    eax, eax
110
        jz      .err_file
110
        jz      .err_file
111
        stdcall load_file, ebx
111
        stdcall load_file, ebx
112
        test    eax, eax
112
        test    eax, eax
113
        jz      .err_file
113
        jz      .err_file
114
 
114
 
115
        mov     [file_base], eax
115
        mov     [file_base], eax
116
        mov     [file_size], ebx
116
        mov     [file_size], ebx
117
        lea     ebx, [hdr_cmdline]
117
        lea     ebx, [hdr_cmdline]
118
        call    test_app_header  ; fill our app header data locals with values from header of given program (if its correct)
118
        call    test_app_header  ; fill our app header data locals with values from header of given program (if its correct)
119
        mov     esi, -TASKMAN_ERROR_NOT_A_EXECUTABLE
119
        mov     esi, -TASKMAN_ERROR_NOT_A_EXECUTABLE
120
        test    eax, eax
120
        test    eax, eax
121
        jz      .err_hdr
121
        jz      .err_hdr
122
 
122
 
123
        call    lock_application_table
123
        call    lock_application_table
124
        call    alloc_thread_slot   ; create a slot for new thread
124
        call    alloc_thread_slot   ; create a slot for new thread
125
        mov     esi, -TASKMAN_ERROR_TOO_MANY_PROCESSES
125
        mov     esi, -TASKMAN_ERROR_TOO_MANY_PROCESSES
126
        test    eax, eax
126
        test    eax, eax
127
        jz      .err_0
127
        jz      .err_0
128
 
128
 
129
        mov     [slot], eax
129
        mov     [slot], eax
130
        shl     eax, BSF sizeof.APPDATA
130
        shl     eax, BSF sizeof.APPDATA
131
        lea     edi, [SLOT_BASE + eax]
131
        lea     edi, [SLOT_BASE + eax]
132
        mov     [slot_base], edi
132
        mov     [slot_base], edi
133
; clean extended information about process
133
; clean extended information about process
134
        mov     ecx, sizeof.APPDATA/4
134
        mov     ecx, sizeof.APPDATA/4
135
        xor     eax, eax
135
        xor     eax, eax
136
        cld
136
        cld
137
        rep stosd
137
        rep stosd
138
; write application name ( APPDATA.appname )
138
; write application name ( APPDATA.appname )
139
        stdcall strrchr, [path_string], '/'
139
        stdcall strrchr, [path_string], '/'
140
        lea     esi, [eax+1]    ; -> name without path
140
        lea     esi, [eax+1]    ; -> name without path
141
        mov     ecx, 11
141
        mov     ecx, 11
142
        mov     edi, [slot_base]
142
        mov     edi, [slot_base]
143
@@:
143
@@:
144
        call    utf8to16
144
        call    utf8to16
145
        call    uni2ansi_char
145
        call    uni2ansi_char
146
        cmp     al, '.'
146
        cmp     al, '.'
147
        jz      @f
147
        jz      @f
148
        test    al, al
148
        test    al, al
149
        jz      @f
149
        jz      @f
150
        stosb
150
        stosb
151
        loop    @b
151
        loop    @b
152
@@:
152
@@:
153
        mov     edi, [cmdline]
153
        mov     edi, [cmdline]
154
        xor     eax, eax
154
        xor     eax, eax
155
        test    edi, edi
155
        test    edi, edi
156
        jz      @f
156
        jz      @f
157
        mov     ecx, 65535
157
        mov     ecx, 65535
158
        call    _strnlen
158
        call    _strnlen
159
        cmp     eax, 256
159
        cmp     eax, 256
160
        jb      @f
160
        jb      @f
161
; if cmdline length >= 256 then increase needed memory size by this length
161
; if cmdline length >= 256 then increase needed memory size by this length
162
        lea     ebx, [eax+1]
162
        lea     ebx, [eax+1]
163
        add     [hdr_emem], ebx
163
        add     [hdr_emem], ebx
164
@@:
164
@@:
165
        mov     [cmdline_size], eax
165
        mov     [cmdline_size], eax
166
        stdcall create_process, [hdr_emem]  ; create a new process
166
        stdcall create_process, [hdr_emem]  ; create a new process
167
        mov     esi, -TASKMAN_ERROR_OUT_OF_MEMORY
167
        mov     esi, -TASKMAN_ERROR_OUT_OF_MEMORY
168
        test    eax, eax
168
        test    eax, eax
169
        jz      .err_hdr
169
        jz      .err_hdr
170
 
170
 
171
; add new process to the list
171
; add new process to the list
172
        mov     ebx, [sys_proc + LHEAD.prev]
172
        mov     ebx, [sys_proc + LHEAD.prev]
173
        __list_add eax, ebx, sys_proc
173
        __list_add eax, ebx, sys_proc
174
; fill the structure fields:
174
; fill the structure fields:
175
        mov     ebx, [hdr_emem]
175
        mov     ebx, [hdr_emem]
176
        mov     [eax + PROC.mem_used], ebx
176
        mov     [eax + PROC.mem_used], ebx
177
 
177
 
178
; write that main thread of app belongs to new process
178
; write that main thread of app belongs to new process
179
        mov     ebx, [slot_base]
179
        mov     ebx, [slot_base]
180
        mov     [ebx + APPDATA.process], eax
180
        mov     [ebx + APPDATA.process], eax
181
 
181
 
182
; initialize the thread list of process: at this moment it consists only of one main thread
182
; initialize the thread list of process: at this moment it consists only of one main thread
183
        lea     edx, [ebx + APPDATA.list]
183
        lea     edx, [ebx + APPDATA.list]
184
        lea     ecx, [eax + PROC.thr_list]
184
        lea     ecx, [eax + PROC.thr_list]
185
        list_add_tail edx, ecx
185
        list_add_tail edx, ecx
186
 
186
 
187
; allocate space and copy app header data locals and cmdline string there, put pointer to exec_params of new thread
187
; allocate space and copy app header data locals and cmdline string there, put pointer to exec_params of new thread
188
        mov     eax, [cmdline_size]
188
        mov     eax, [cmdline_size]
189
        add     eax, sizeof.APP_HDR
189
        add     eax, sizeof.APP_HDR
190
        stdcall kernel_alloc, eax
190
        stdcall kernel_alloc, eax
191
        mov     [ebx + APPDATA.exec_params], eax
191
        mov     [ebx + APPDATA.exec_params], eax
192
        mov     edi, eax
192
        mov     edi, eax
193
        lea     esi, [hdr_cmdline]
193
        lea     esi, [hdr_cmdline]
194
        mov     ecx, sizeof.APP_HDR/4
194
        mov     ecx, sizeof.APP_HDR/4
195
        rep movsd
195
        rep movsd
196
        mov     ecx, [cmdline_size]
196
        mov     ecx, [cmdline_size]
197
        mov     esi, [cmdline]
197
        mov     esi, [cmdline]
198
        rep movsb
198
        rep movsb
199
; set other parameters of application
199
; set other parameters of application
200
        lea     eax, [hdr_cmdline]
200
        lea     eax, [hdr_cmdline]
201
        stdcall set_app_params , [slot], eax, [flags]
201
        stdcall set_app_params , [slot], eax, [flags]
202
        mov     eax, [process_number]   ; return process number
202
        mov     eax, [process_number]   ; return process number
203
        call    unlock_application_table
203
        call    unlock_application_table
204
        ret
204
        ret
205
 
205
 
206
.err_0:
206
.err_0:
207
        call    unlock_application_table
207
        call    unlock_application_table
208
.err_hdr:
208
.err_hdr:
209
        stdcall kernel_free, [file_base]
209
        stdcall kernel_free, [file_base]
210
.err_file:
210
.err_file:
211
        stdcall kernel_free, [path_string]
211
        stdcall kernel_free, [path_string]
212
        mov     eax, esi
212
        mov     eax, esi
213
        ret
213
        ret
214
endp
214
endp
215
 
215
 
216
align 4
216
align 4
217
test_app_header:
217
test_app_header:
218
       virtual at eax
218
       virtual at eax
219
         APP_HEADER_00 APP_HEADER_00_
219
         APP_HEADER_00 APP_HEADER_00_
220
       end virtual
220
       end virtual
221
       virtual at eax
221
       virtual at eax
222
         APP_HEADER_01 APP_HEADER_01_
222
         APP_HEADER_01 APP_HEADER_01_
223
       end virtual
223
       end virtual
224
 
224
 
225
        cmp     dword [eax], 'MENU'
225
        cmp     dword [eax], 'MENU'
226
        jne     .fail
226
        jne     .fail
227
        cmp     word [eax+4], 'ET'
227
        cmp     word [eax+4], 'ET'
228
        jne     .fail
228
        jne     .fail
229
 
229
 
230
        cmp     [eax+6], word '00'
230
        cmp     [eax+6], word '00'
231
        jne     .check_01_header
231
        jne     .check_01_header
232
 
232
 
233
        mov     ecx, [APP_HEADER_00.start]
233
        mov     ecx, [APP_HEADER_00.start]
234
        mov     [ebx + APP_HDR.eip], ecx
234
        mov     [ebx + APP_HDR.eip], ecx
235
        mov     edx, [APP_HEADER_00.mem_size]
235
        mov     edx, [APP_HEADER_00.mem_size]
236
        mov     [ebx + APP_HDR._emem], edx
236
        mov     [ebx + APP_HDR._emem], edx
237
 
237
 
238
        cmp     edx, [APP_HEADER_00.i_end]
238
        cmp     edx, [APP_HEADER_00.i_end]
239
        jb      .fail
239
        jb      .fail
240
 
240
 
241
        cmp     edx, OS_BASE ;check memory
241
        cmp     edx, OS_BASE ;check memory
242
        jae     .fail
242
        jae     .fail
243
        mov     ecx, [pg_data.pages_free]
243
        mov     ecx, [pg_data.pages_free]
244
        shl     ecx, 12 ; ecx * 4kb
244
        shl     ecx, 12 ; ecx * 4kb
245
        cmp     edx, ecx
245
        cmp     edx, ecx
246
        jae     .fail
246
        jae     .fail
247
 
247
 
248
        shr     edx, 1
248
        shr     edx, 1
249
        sub     edx, 0x10
249
        sub     edx, 0x10
250
        mov     [ebx + APP_HDR.esp], edx
250
        mov     [ebx + APP_HDR.esp], edx
251
        mov     ecx, [APP_HEADER_00.i_param]
251
        mov     ecx, [APP_HEADER_00.i_param]
252
        mov     [ebx + APP_HDR.cmdline], ecx
252
        mov     [ebx + APP_HDR.cmdline], ecx
253
        mov     [ebx + APP_HDR.path], 0
253
        mov     [ebx + APP_HDR.path], 0
254
        mov     edx, [APP_HEADER_00.i_end]
254
        mov     edx, [APP_HEADER_00.i_end]
255
        mov     [ebx + APP_HDR._edata], edx
255
        mov     [ebx + APP_HDR._edata], edx
256
        ret
256
        ret
257
 
257
 
258
 .check_01_header:
258
 .check_01_header:
259
 
259
 
260
        cmp     [eax+6], word '01'
260
        cmp     [eax+6], word '01'
261
        je      @f
261
        je      @f
262
        cmp     [eax+6], word '02'
262
        cmp     [eax+6], word '02'
263
        jne     .fail
263
        jne     .fail
264
@@:
264
@@:
265
        mov     ecx, [APP_HEADER_01.start]
265
        mov     ecx, [APP_HEADER_01.start]
266
        mov     [ebx + 0x08], ecx
266
        mov     [ebx + 0x08], ecx
267
        mov     edx, [APP_HEADER_01.mem_size]
267
        mov     edx, [APP_HEADER_01.mem_size]
268
 
268
 
269
; \begin{diamond}[20.08.2006]
269
; \begin{diamond}[20.08.2006]
270
; sanity check (functions 19,58 load app_i_end bytes and that must
270
; sanity check (functions 19,58 load app_i_end bytes and that must
271
; fit in allocated memory to prevent kernel faults)
271
; fit in allocated memory to prevent kernel faults)
272
        cmp     edx, [APP_HEADER_01.i_end]
272
        cmp     edx, [APP_HEADER_01.i_end]
273
        jb      .fail
273
        jb      .fail
274
; \end{diamond}[20.08.2006]
274
; \end{diamond}[20.08.2006]
275
        cmp     edx, OS_BASE ;check memory
275
        cmp     edx, OS_BASE ;check memory
276
        jae     .fail
276
        jae     .fail
277
        mov     ecx, [pg_data.pages_free]
277
        mov     ecx, [pg_data.pages_free]
278
        shl     ecx, 12 ; ecx * 4kb
278
        shl     ecx, 12 ; ecx * 4kb
279
        cmp     edx, ecx
279
        cmp     edx, ecx
280
        jae     .fail
280
        jae     .fail
281
 
281
 
282
        mov     [ebx + APP_HDR._emem], edx
282
        mov     [ebx + APP_HDR._emem], edx
283
        mov     ecx, [APP_HEADER_01.stack_top]
283
        mov     ecx, [APP_HEADER_01.stack_top]
284
        mov     [ebx + APP_HDR.esp], ecx
284
        mov     [ebx + APP_HDR.esp], ecx
285
        mov     edx, [APP_HEADER_01.i_param]
285
        mov     edx, [APP_HEADER_01.i_param]
286
        mov     [ebx + APP_HDR.cmdline], edx
286
        mov     [ebx + APP_HDR.cmdline], edx
287
        mov     ecx, [APP_HEADER_01.i_icon]
287
        mov     ecx, [APP_HEADER_01.i_icon]
288
        mov     [ebx + APP_HDR.path], ecx
288
        mov     [ebx + APP_HDR.path], ecx
289
        mov     edx, [APP_HEADER_01.i_end]
289
        mov     edx, [APP_HEADER_01.i_end]
290
        mov     [ebx + APP_HDR._edata], edx
290
        mov     [ebx + APP_HDR._edata], edx
291
        ret
291
        ret
292
.fail:
292
.fail:
293
        xor     eax, eax
293
        xor     eax, eax
294
        ret
294
        ret
295
 
295
 
296
align 4
296
align 4
297
alloc_thread_slot:
297
alloc_thread_slot:
298
;input:
298
;input:
299
;  none
299
;  none
300
;result:
300
;result:
301
;  eax=[new_thread_slot]<>0 - ok
301
;  eax=[new_thread_slot]<>0 - ok
302
;      0 - failed.
302
;      0 - failed.
303
;This function find least empty slot.
303
;This function find least empty slot.
304
;It doesn't increase [thread_count]!
304
;It doesn't increase [thread_count]!
305
 
305
 
306
 
306
 
307
        mov     edx, thr_slot_map
307
        mov     edx, thr_slot_map
308
        pushfd
308
        pushfd
309
        cli
309
        cli
310
.l1:
310
.l1:
311
        bsf     eax, [edx]
311
        bsf     eax, [edx]
312
        jnz     .found
312
        jnz     .found
313
        add     edx, 4
313
        add     edx, 4
314
        cmp     edx, thr_slot_map + 32
314
        cmp     edx, thr_slot_map + 32
315
        jb      .l1
315
        jb      .l1
316
 
316
 
317
        popfd
317
        popfd
318
        xor     eax, eax
318
        xor     eax, eax
319
        ret
319
        ret
320
.found:
320
.found:
321
        btr     [edx], eax
321
        btr     [edx], eax
322
        sub     edx, thr_slot_map
322
        sub     edx, thr_slot_map
323
        lea     eax, [eax + edx*8]
323
        lea     eax, [eax + edx*8]
324
        popfd
324
        popfd
325
        ret
325
        ret
326
 
326
 
327
align 4
327
align 4
328
proc create_process stdcall, app_size:dword
328
proc create_process stdcall, app_size:dword
329
       locals
329
       locals
330
         process     dd ?
330
         process     dd ?
331
         app_tabs    dd ?
331
         app_tabs    dd ?
332
       endl
332
       endl
333
 
333
 
334
        push    ebx
334
        push    ebx
335
        push    esi
335
        push    esi
336
        push    edi
336
        push    edi
337
 
337
 
338
        xor     eax, eax
338
        xor     eax, eax
339
        mov     [process], eax
339
        mov     [process], eax
340
 
340
 
341
        mov     eax, [app_size]
341
        mov     eax, [app_size]
342
        add     eax, 0x3FFFFF
342
        add     eax, 0x3FFFFF
343
        shr     eax, 22
343
        shr     eax, 22
344
        mov     [app_tabs], eax
344
        mov     [app_tabs], eax
345
 
345
 
346
        stdcall kernel_alloc, 0x2000
346
        stdcall kernel_alloc, 0x2000
347
        test    eax, eax
347
        test    eax, eax
348
        jz      .fail
348
        jz      .fail
349
        mov     [process], eax
349
        mov     [process], eax
350
 
350
 
351
        lea     edi, [eax + PROC.heap_lock]
351
        lea     edi, [eax + PROC.heap_lock]
352
        mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
352
        mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
353
 
353
 
354
        list_init eax
354
        list_init eax
355
        add     eax, PROC.thr_list
355
        add     eax, PROC.thr_list
356
        list_init eax
356
        list_init eax
357
 
357
 
358
        xor     eax, eax
358
        xor     eax, eax
359
        cld
359
        cld
360
        rep stosd
360
        rep stosd
361
 
361
 
362
        mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
362
        mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
363
        mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
363
        mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
364
        mov     ecx, (PROC.pdt_0 - PROC.htab)/4
364
        mov     ecx, (PROC.pdt_0 - PROC.htab)/4
365
        add     edi, 8
365
        add     edi, 8
366
        inc     eax
366
        inc     eax
367
@@:
367
@@:
368
        stosd
368
        stosd
369
        inc     eax
369
        inc     eax
370
        cmp     eax, ecx
370
        cmp     eax, ecx
371
        jbe     @B
371
        jbe     @B
372
 
372
 
373
        mov     eax, edi
373
        mov     eax, edi
374
        call    get_pg_addr
374
        call    get_pg_addr
375
        mov     [edi - PAGE_SIZE + PROC.pdt_0_phys], eax
375
        mov     [edi - PAGE_SIZE + PROC.pdt_0_phys], eax
376
 
376
 
377
        mov     ecx, (OS_BASE shr 20)/4
377
        mov     ecx, (OS_BASE shr 20)/4
378
        xor     eax, eax
378
        xor     eax, eax
379
        rep stosd
379
        rep stosd
380
 
380
 
381
        mov     ecx, (OS_BASE shr 20)/4
381
        mov     ecx, (OS_BASE shr 20)/4
382
        mov     esi, sys_proc + PROC.pdt_0 + (OS_BASE shr 20)
382
        mov     esi, sys_proc + PROC.pdt_0 + (OS_BASE shr 20)
383
        rep movsd
383
        rep movsd
384
 
384
 
385
        mov     eax, [edi - 8192 + PROC.pdt_0_phys]
385
        mov     eax, [edi - 8192 + PROC.pdt_0_phys]
386
        or      eax, PG_SWR
386
        or      eax, PG_SWR
387
        mov     [edi - PAGE_SIZE + (page_tabs shr 20)], eax
387
        mov     [edi - PAGE_SIZE + (page_tabs shr 20)], eax
388
 
388
 
389
        lea     edx, [edi - PAGE_SIZE]
389
        lea     edx, [edi - PAGE_SIZE]
390
        mov     esi, [app_tabs]
390
        mov     esi, [app_tabs]
391
 
391
 
392
.alloc_page_dir:
392
.alloc_page_dir:
393
        call    alloc_page
393
        call    alloc_page
394
        test    eax, eax
394
        test    eax, eax
395
        jz      .fail
395
        jz      .fail
396
        or      eax, PG_UWR
396
        or      eax, PG_UWR
397
        mov     [edx], eax
397
        mov     [edx], eax
398
 
398
 
399
        mov     edi, [tmp_task_ptab]
399
        mov     edi, [tmp_task_ptab]
400
        stdcall map_page, edi, eax, PG_SWR
400
        stdcall map_page, edi, eax, PG_SWR
401
        mov     ecx, 1024
401
        mov     ecx, PAGE_SIZE/4
402
        xor     eax, eax
402
        xor     eax, eax
403
        rep stosd
403
        rep stosd
404
 
404
 
405
        add     edx, 4
405
        add     edx, 4
406
        dec     esi
406
        dec     esi
407
        jnz     .alloc_page_dir
407
        jnz     .alloc_page_dir
408
 
408
 
409
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
409
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
410
        mov     eax, [process]
410
        mov     eax, [process]
411
 
411
 
412
        pop     edi
412
        pop     edi
413
        pop     esi
413
        pop     esi
414
        pop     ebx
414
        pop     ebx
415
        ret
415
        ret
416
.fail:
416
.fail:
417
        mov     ecx, [process]
417
        mov     ecx, [process]
418
        jcxz    @F
418
        jcxz    @F
419
 
419
 
420
        call    destroy_process
420
        call    destroy_process
421
@@:
421
@@:
422
        xor     eax, eax
422
        xor     eax, eax
423
        pop     edi
423
        pop     edi
424
        pop     esi
424
        pop     esi
425
        pop     ebx
425
        pop     ebx
426
        ret
426
        ret
427
endp
427
endp
428
 
428
 
429
align 4
429
align 4
430
proc destroy_page_table stdcall, pg_tab:dword
430
proc destroy_page_table stdcall, pg_tab:dword
431
 
431
 
432
        push    esi
432
        push    esi
433
 
433
 
434
        mov     esi, [pg_tab]
434
        mov     esi, [pg_tab]
435
        mov     ecx, 1024
435
        mov     ecx, 1024
436
.free:
436
.free:
437
        mov     eax, [esi]
437
        mov     eax, [esi]
438
        test    eax, 1
438
        test    eax, 1
439
        jz      .next
439
        jz      .next
440
        test    eax, 2
440
        test    eax, 2
441
        jz      .next
441
        jz      .next
442
        test    eax, 1 shl 9
442
        test    eax, 1 shl 9
443
        jnz     .next                     ;skip shared pages
443
        jnz     .next                     ;skip shared pages
444
        call    free_page
444
        call    free_page
445
.next:
445
.next:
446
        add     esi, 4
446
        add     esi, 4
447
        dec     ecx
447
        dec     ecx
448
        jnz     .free
448
        jnz     .free
449
        pop     esi
449
        pop     esi
450
        ret
450
        ret
451
endp
451
endp
452
 
452
 
453
align 4
453
align 4
454
destroy_process: ;fastcall ecx= ptr to process
454
destroy_process: ;fastcall ecx= ptr to process
455
 
455
 
456
        lea     eax, [ecx+PROC.thr_list]
456
        lea     eax, [ecx+PROC.thr_list]
457
        cmp     eax, [eax+LHEAD.next]
457
        cmp     eax, [eax+LHEAD.next]
458
        jne     .exit
458
        jne     .exit
459
 
459
 
460
align 4
460
align 4
461
.internal:
461
.internal:
462
        push    ecx
462
        push    ecx
463
 
463
 
464
        mov     esi, ecx
464
        mov     esi, ecx
465
        list_del esi
465
        list_del esi
466
 
466
 
467
        mov     esi, [esi + PROC.dlls_list_ptr]
467
        mov     esi, [esi + PROC.dlls_list_ptr]
468
        call    destroy_all_hdlls
468
        call    destroy_all_hdlls
469
 
469
 
470
        mov     esi, [esp]
470
        mov     esi, [esp]
471
        add     esi, PROC.pdt_0
471
        add     esi, PROC.pdt_0
472
        mov     edi, (0x80000000 shr 20)/4
472
        mov     edi, (0x80000000 shr 20)/4
473
.destroy:
473
.destroy:
474
        mov     eax, [esi]
474
        mov     eax, [esi]
475
        test    eax, 1
475
        test    eax, 1
476
        jz      .next
476
        jz      .next
477
        and     eax, -PAGE_SIZE
477
        and     eax, -PAGE_SIZE
478
        stdcall map_page, [tmp_task_ptab], eax, PG_SWR
478
        stdcall map_page, [tmp_task_ptab], eax, PG_SWR
479
        stdcall destroy_page_table, [tmp_task_ptab]
479
        stdcall destroy_page_table, [tmp_task_ptab]
480
        mov     eax, [esi]
480
        mov     eax, [esi]
481
        call    free_page
481
        call    free_page
482
.next:
482
.next:
483
        add     esi, 4
483
        add     esi, 4
484
        dec     edi
484
        dec     edi
485
        jnz     .destroy
485
        jnz     .destroy
486
 
486
 
487
        call    kernel_free     ;ecx still in stack
487
        call    kernel_free     ;ecx still in stack
488
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
488
        stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
489
.exit:
489
.exit:
490
        ret
490
        ret
491
 
491
 
492
align 4
492
align 4
493
get_pid:
493
get_pid:
494
        mov     eax, [current_slot]
494
        mov     eax, [current_slot]
495
        mov     eax, [eax + APPDATA.tid]
495
        mov     eax, [eax + APPDATA.tid]
496
        ret
496
        ret
497
 
497
 
498
pid_to_slot:
498
pid_to_slot:
499
;Input:
499
;Input:
500
;  eax - pid of process
500
;  eax - pid of process
501
;Output:
501
;Output:
502
;  eax - slot of process or 0 if process don't exists
502
;  eax - slot of process or 0 if process don't exists
503
;Search process by PID.
503
;Search process by PID.
504
        push    ebx
504
        push    ebx
505
        push    ecx
505
        push    ecx
506
        mov     ebx, [thread_count]
506
        mov     ebx, [thread_count]
507
        shl     ebx, BSF sizeof.APPDATA ; multiply by size
507
        shl     ebx, BSF sizeof.APPDATA ; multiply by size
508
        ; skip first process in the task table
508
        ; skip first process in the task table
509
        mov     ecx, sizeof.APPDATA
509
        mov     ecx, sizeof.APPDATA
510
 
510
 
511
.loop:
511
.loop:
512
;ecx = offset of current process info entry
512
;ecx = offset of current process info entry
513
;ebx = maximum permitted offset
513
;ebx = maximum permitted offset
514
        cmp     [SLOT_BASE + ecx + APPDATA.state], TSTATE_FREE
514
        cmp     [SLOT_BASE + ecx + APPDATA.state], TSTATE_FREE
515
        jz      .endloop ;skip empty slots
515
        jz      .endloop ;skip empty slots
516
        cmp     [SLOT_BASE + ecx + APPDATA.tid], eax
516
        cmp     [SLOT_BASE + ecx + APPDATA.tid], eax
517
        jz      .pid_found
517
        jz      .pid_found
518
.endloop:
518
.endloop:
519
        add     ecx, sizeof.APPDATA
519
        add     ecx, sizeof.APPDATA
520
        cmp     ecx, ebx
520
        cmp     ecx, ebx
521
        jle     .loop
521
        jle     .loop
522
 
522
 
523
        pop     ecx
523
        pop     ecx
524
        pop     ebx
524
        pop     ebx
525
        xor     eax, eax
525
        xor     eax, eax
526
        ret
526
        ret
527
 
527
 
528
.pid_found:
528
.pid_found:
529
        shr     ecx, BSF sizeof.APPDATA ; divide by size
529
        shr     ecx, BSF sizeof.APPDATA ; divide by size
530
        mov     eax, ecx ;convert offset to index of slot
530
        mov     eax, ecx ;convert offset to index of slot
531
        pop     ecx
531
        pop     ecx
532
        pop     ebx
532
        pop     ebx
533
        ret
533
        ret
534
 
534
 
535
 
535
 
536
align 4
536
align 4
537
proc read_process_memory
537
proc read_process_memory
538
;Input:
538
;Input:
539
;  eax - process slot
539
;  eax - process slot
540
;  ecx - buffer address
540
;  ecx - buffer address
541
;  edx - buffer size
541
;  edx - buffer size
542
;  esi - start address in other process
542
;  esi - start address in other process
543
;Output:
543
;Output:
544
;  eax - number of bytes read.
544
;  eax - number of bytes read.
545
       locals
545
       locals
546
         slot   dd ?
546
         slot   dd ?
547
         buff   dd ?
547
         buff   dd ?
548
         r_count    dd ?
548
         r_count    dd ?
549
         offset dd ?
549
         offset dd ?
550
         tmp_r_cnt  dd ?
550
         tmp_r_cnt  dd ?
551
       endl
551
       endl
552
 
552
 
553
        mov     [slot], eax
553
        mov     [slot], eax
554
        mov     [buff], ecx
554
        mov     [buff], ecx
555
        and     [r_count], 0
555
        and     [r_count], 0
556
        mov     [tmp_r_cnt], edx
556
        mov     [tmp_r_cnt], edx
557
        mov     [offset], esi
557
        mov     [offset], esi
558
 
558
 
559
        pushad
559
        pushad
560
.read_mem:
560
.read_mem:
561
        mov     edx, [offset]
561
        mov     edx, [offset]
562
        mov     ebx, [tmp_r_cnt]
562
        mov     ebx, [tmp_r_cnt]
563
 
563
 
564
        mov     ecx, 0x400000
564
        mov     ecx, 0x400000
565
        and     edx, 0x3FFFFF
565
        and     edx, 0x3FFFFF
566
        sub     ecx, edx
566
        sub     ecx, edx
567
        cmp     ecx, ebx
567
        cmp     ecx, ebx
568
        jbe     @f
568
        jbe     @f
569
        mov     ecx, ebx
569
        mov     ecx, ebx
570
@@:
570
@@:
571
        cmp     ecx, 0x8000
571
        cmp     ecx, 0x8000
572
        jna     @F
572
        jna     @F
573
        mov     ecx, 0x8000
573
        mov     ecx, 0x8000
574
@@:
574
@@:
575
        mov     ebx, [offset]
575
        mov     ebx, [offset]
576
 
576
 
577
        push    ecx
577
        push    ecx
578
        stdcall map_memEx, [proc_mem_map], \
578
        stdcall map_memEx, [proc_mem_map], \
579
                [slot], ebx, ecx, PG_READ
579
                [slot], ebx, ecx, PG_READ
580
        pop     ecx
580
        pop     ecx
581
 
581
 
582
        mov     esi, [offset]
582
        mov     esi, [offset]
583
        and     esi, 0xfff
583
        and     esi, 0xfff
584
        sub     eax, esi
584
        sub     eax, esi
585
        jbe     .ret
585
        jbe     .ret
586
        cmp     ecx, eax
586
        cmp     ecx, eax
587
        jbe     @f
587
        jbe     @f
588
        mov     ecx, eax
588
        mov     ecx, eax
589
        mov     [tmp_r_cnt], eax
589
        mov     [tmp_r_cnt], eax
590
@@:
590
@@:
591
        add     esi, [proc_mem_map]
591
        add     esi, [proc_mem_map]
592
        mov     edi, [buff]
592
        mov     edi, [buff]
593
        mov     edx, ecx
593
        mov     edx, ecx
594
        rep movsb
594
        rep movsb
595
        add     [r_count], edx
595
        add     [r_count], edx
596
 
596
 
597
        add     [offset], edx
597
        add     [offset], edx
598
        sub     [tmp_r_cnt], edx
598
        sub     [tmp_r_cnt], edx
599
        jnz     .read_mem
599
        jnz     .read_mem
600
.ret:
600
.ret:
601
        popad
601
        popad
602
        mov     eax, [r_count]
602
        mov     eax, [r_count]
603
        ret
603
        ret
604
endp
604
endp
605
 
605
 
606
align 4
606
align 4
607
proc write_process_memory
607
proc write_process_memory
608
;Input:
608
;Input:
609
;  eax - process slot
609
;  eax - process slot
610
;  ecx - buffer address
610
;  ecx - buffer address
611
;  edx - buffer size
611
;  edx - buffer size
612
;  esi - start address in other process
612
;  esi - start address in other process
613
;Output:
613
;Output:
614
;  eax - number of bytes written
614
;  eax - number of bytes written
615
 
615
 
616
       locals
616
       locals
617
         slot   dd ?
617
         slot   dd ?
618
         buff   dd ?
618
         buff   dd ?
619
         w_count    dd ?
619
         w_count    dd ?
620
         offset dd ?
620
         offset dd ?
621
         tmp_w_cnt  dd ?
621
         tmp_w_cnt  dd ?
622
       endl
622
       endl
623
 
623
 
624
        mov     [slot], eax
624
        mov     [slot], eax
625
        mov     [buff], ecx
625
        mov     [buff], ecx
626
        and     [w_count], 0
626
        and     [w_count], 0
627
        mov     [tmp_w_cnt], edx
627
        mov     [tmp_w_cnt], edx
628
        mov     [offset], esi
628
        mov     [offset], esi
629
 
629
 
630
        pushad
630
        pushad
631
.read_mem:
631
.read_mem:
632
        mov     edx, [offset]
632
        mov     edx, [offset]
633
        mov     ebx, [tmp_w_cnt]
633
        mov     ebx, [tmp_w_cnt]
634
 
634
 
635
        mov     ecx, 0x400000
635
        mov     ecx, 0x400000
636
        and     edx, 0x3FFFFF
636
        and     edx, 0x3FFFFF
637
        sub     ecx, edx
637
        sub     ecx, edx
638
        cmp     ecx, ebx
638
        cmp     ecx, ebx
639
        jbe     @f
639
        jbe     @f
640
        mov     ecx, ebx
640
        mov     ecx, ebx
641
@@:
641
@@:
642
        cmp     ecx, 0x8000
642
        cmp     ecx, 0x8000
643
        jna     @F
643
        jna     @F
644
        mov     ecx, 0x8000
644
        mov     ecx, 0x8000
645
@@:
645
@@:
646
        mov     ebx, [offset]
646
        mov     ebx, [offset]
647
        push    ecx
647
        push    ecx
648
        stdcall map_memEx, [proc_mem_map], \
648
        stdcall map_memEx, [proc_mem_map], \
649
                [slot], ebx, ecx, PG_SWR
649
                [slot], ebx, ecx, PG_SWR
650
        pop     ecx
650
        pop     ecx
651
 
651
 
652
        mov     edi, [offset]
652
        mov     edi, [offset]
653
        and     edi, 0xfff
653
        and     edi, 0xfff
654
        sub     eax, edi
654
        sub     eax, edi
655
        jbe     .ret
655
        jbe     .ret
656
        cmp     ecx, eax
656
        cmp     ecx, eax
657
        jbe     @f
657
        jbe     @f
658
        mov     ecx, eax
658
        mov     ecx, eax
659
        mov     [tmp_w_cnt], eax
659
        mov     [tmp_w_cnt], eax
660
@@:
660
@@:
661
        add     edi, [proc_mem_map]
661
        add     edi, [proc_mem_map]
662
        mov     esi, [buff]
662
        mov     esi, [buff]
663
        mov     edx, ecx
663
        mov     edx, ecx
664
        rep movsb
664
        rep movsb
665
 
665
 
666
        add     [w_count], edx
666
        add     [w_count], edx
667
        add     [offset], edx
667
        add     [offset], edx
668
        sub     [tmp_w_cnt], edx
668
        sub     [tmp_w_cnt], edx
669
        jnz     .read_mem
669
        jnz     .read_mem
670
.ret:
670
.ret:
671
        popad
671
        popad
672
        mov     eax, [w_count]
672
        mov     eax, [w_count]
673
        ret
673
        ret
674
endp
674
endp
675
 
675
 
676
;ebx = 1 - kernel thread
676
;ebx = 1 - kernel thread
677
;ecx=thread entry point
677
;ecx=thread entry point
678
;edx=thread stack pointer
678
;edx=thread stack pointer
679
;creation flags  0x01 - debugged
679
;creation flags  0x01 - debugged
680
;                0x02 - kernel
680
;                0x02 - kernel
681
 
681
 
682
align 4
682
align 4
683
proc new_sys_threads
683
proc new_sys_threads
684
       locals
684
       locals
685
         slot          dd ?
685
         slot          dd ?
686
         flags         dd ?
686
         flags         dd ?
687
         app_cmdline   dd ? ;0x00
687
         app_cmdline   dd ? ;0x00
688
         app_path      dd ? ;0x04
688
         app_path      dd ? ;0x04
689
         app_eip       dd ? ;0x08
689
         app_eip       dd ? ;0x08
690
         app_esp       dd ? ;0x0C
690
         app_esp       dd ? ;0x0C
691
         app_mem       dd ? ;0x10
691
         app_mem       dd ? ;0x10
692
       endl
692
       endl
693
 
693
 
694
        shl     ebx, 1
694
        shl     ebx, 1
695
        mov     [flags], ebx
695
        mov     [flags], ebx
696
 
696
 
697
        xor     eax, eax
697
        xor     eax, eax
698
        mov     [app_eip], ecx
698
        mov     [app_eip], ecx
699
        mov     [app_cmdline], eax
699
        mov     [app_cmdline], eax
700
        mov     [app_esp], edx
700
        mov     [app_esp], edx
701
        mov     [app_path], eax
701
        mov     [app_path], eax
702
 
702
 
703
        call    lock_application_table
703
        call    lock_application_table
704
 
704
 
705
        call    alloc_thread_slot
705
        call    alloc_thread_slot
706
        test    eax, eax
706
        test    eax, eax
707
        jz      .failed
707
        jz      .failed
708
 
708
 
709
        mov     [slot], eax
709
        mov     [slot], eax
710
 
710
 
711
        mov     esi, [current_slot]
711
        mov     esi, [current_slot]
712
        mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
712
        mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
713
 
713
 
714
        mov     edi, eax
714
        mov     edi, eax
715
        shl     edi, BSF sizeof.APPDATA
715
        shl     edi, BSF sizeof.APPDATA
716
        add     edi, SLOT_BASE
716
        add     edi, SLOT_BASE
717
        mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
717
        mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
718
        mov     ecx, sizeof.APPDATA/4
718
        mov     ecx, sizeof.APPDATA/4
719
        xor     eax, eax
719
        xor     eax, eax
720
        cld
720
        cld
721
        rep stosd             ;clean extended information about new thread
721
        rep stosd             ;clean extended information about new thread
722
        mov     esi, ebx
722
        mov     esi, ebx
723
        mov     edi, edx
723
        mov     edi, edx
724
        mov     ecx, 11
724
        mov     ecx, 11
725
        rep movsb             ;copy process name
725
        rep movsb             ;copy process name
726
 
726
 
727
 
727
 
728
        mov     eax, [ebx + APPDATA.tls_base]
728
        mov     eax, [ebx + APPDATA.tls_base]
729
        test    eax, eax
729
        test    eax, eax
730
        jz      @F
730
        jz      @F
731
 
731
 
732
        push    edx
732
        push    edx
733
        stdcall user_alloc, PAGE_SIZE
733
        stdcall user_alloc, PAGE_SIZE
734
        pop     edx
734
        pop     edx
735
        test    eax, eax
735
        test    eax, eax
736
        jz      .failed1;eax=0
736
        jz      .failed1;eax=0
737
@@:
737
@@:
738
        mov     [edx + APPDATA.tls_base], eax
738
        mov     [edx + APPDATA.tls_base], eax
739
 
739
 
740
        mov     eax, [ebx + APPDATA.process]
740
        mov     eax, [ebx + APPDATA.process]
741
        mov     [edx + APPDATA.process], eax
741
        mov     [edx + APPDATA.process], eax
742
 
742
 
743
        lea     ebx, [edx + APPDATA.list]
743
        lea     ebx, [edx + APPDATA.list]
744
        lea     ecx, [eax + PROC.thr_list]
744
        lea     ecx, [eax + PROC.thr_list]
745
        list_add_tail ebx, ecx               ;add thread to process child's list
745
        list_add_tail ebx, ecx               ;add thread to process child's list
746
 
746
 
747
        lea     eax, [app_cmdline]
747
        lea     eax, [app_cmdline]
748
        stdcall set_app_params , [slot], eax, [flags]
748
        stdcall set_app_params , [slot], eax, [flags]
749
 
749
 
750
        mov     eax, [process_number]           ;set result
750
        mov     eax, [process_number]           ;set result
751
        call    unlock_application_table
751
        call    unlock_application_table
752
        ret
752
        ret
753
.failed:
753
.failed:
754
        xor     eax, eax
754
        xor     eax, eax
755
.failed1:
755
.failed1:
756
        call    unlock_application_table
756
        call    unlock_application_table
757
        dec     eax     ;-1
757
        dec     eax     ;-1
758
        ret
758
        ret
759
endp
759
endp
760
 
760
 
761
proc map_process_image stdcall, img_size:dword, file_base:dword, file_size:dword
761
proc map_process_image stdcall, img_size:dword, file_base:dword, file_size:dword
762
 
762
 
763
        mov     edx, [img_size]
763
        mov     edx, [img_size]
764
        mov     esi, [file_base]
764
        mov     esi, [file_base]
765
        mov     ecx, [file_size]
765
        mov     ecx, [file_size]
766
        add     edx, PAGE_SIZE-1
766
        add     edx, PAGE_SIZE-1
767
        add     ecx, PAGE_SIZE-1
767
        add     ecx, PAGE_SIZE-1
768
        shr     edx, 12        ; total pages
768
        shr     edx, 12        ; total pages
769
        shr     ecx, 12        ; image pages
769
        shr     ecx, 12        ; image pages
770
 
770
 
771
        mov     edi, page_tabs
771
        mov     edi, page_tabs
772
        shr     esi, 10
772
        shr     esi, 10
773
        add     esi, edi
773
        add     esi, edi
774
 
774
 
775
.map_image:
775
.map_image:
776
        lodsd
776
        lodsd
777
        and     eax, -PAGE_SIZE
777
        and     eax, -PAGE_SIZE
778
        or      eax, PG_UWR
778
        or      eax, PG_UWR
779
        stosd
779
        stosd
780
        dec     edx
780
        dec     edx
781
        loop    .map_image
781
        loop    .map_image
782
 
782
 
783
        test    edx, edx
783
        test    edx, edx
784
        jz      .done
784
        jz      .done
785
.map_bss:
785
.map_bss:
786
        call    alloc_page
786
        call    alloc_page
787
        test    eax, eax
787
        test    eax, eax
788
        jz      .fail
788
        jz      .fail
789
 
789
 
790
        or      eax, PG_UWR
790
        or      eax, PG_UWR
791
        stosd
791
        stosd
792
        dec     edx
792
        dec     edx
793
        jnz     .map_bss
793
        jnz     .map_bss
794
 
794
 
795
        mov     edi, [file_size]
795
        mov     edi, [file_size]
796
        mov     ecx, [img_size]
796
        mov     ecx, [img_size]
797
        add     edi, PAGE_SIZE-1
797
        add     edi, PAGE_SIZE-1
798
        and     edi, -PAGE_SIZE
798
        and     edi, -PAGE_SIZE
799
        add     ecx, PAGE_SIZE-1
799
        add     ecx, PAGE_SIZE-1
800
        and     ecx, -PAGE_SIZE
800
        and     ecx, -PAGE_SIZE
801
        sub     ecx, edi
801
        sub     ecx, edi
802
        shr     ecx, 2
802
        shr     ecx, 2
803
        xor     eax, eax
803
        xor     eax, eax
804
        rep stosd
804
        rep stosd
805
.done:
805
.done:
806
.fail:
806
.fail:
807
        ret
807
        ret
808
endp
808
endp
809
 
809
 
810
align 4
810
align 4
811
common_app_entry:
811
common_app_entry:
812
        mov     ebp, [current_slot]
812
        mov     ebp, [current_slot]
813
        mov     ebp, [ebp + APPDATA.exec_params]
813
        mov     ebp, [ebp + APPDATA.exec_params]
814
        test    ebp, ebp
814
        test    ebp, ebp
815
        jz      .exit
815
        jz      .exit
816
; APPDATA.exec_params have first thread only,
816
; APPDATA.exec_params have first thread only,
817
; so second and next threads don't get here (they jump to .exit) 
817
; so second and next threads don't get here (they jump to .exit) 
818
        stdcall map_process_image, [ebp + APP_HDR._emem],\
818
        stdcall map_process_image, [ebp + APP_HDR._emem],\
819
                [ebp + APP_HDR.img_base], [ebp + APP_HDR.img_size]
819
                [ebp + APP_HDR.img_base], [ebp + APP_HDR.img_size]
820
        mov     esi, [ebp + APP_HDR.path_string]
820
        mov     esi, [ebp + APP_HDR.path_string]
821
        mov     edi, [ebp + APP_HDR.path]
821
        mov     edi, [ebp + APP_HDR.path]
822
        mov     ecx, [ebp + APP_HDR.filename_size]
822
        mov     ecx, [ebp + APP_HDR.filename_size]
823
        cmp     ecx, 1023
823
        cmp     ecx, 1023
824
        jc      @f
824
        jc      @f
825
        mov     ecx, 1022
825
        mov     ecx, 1022
826
@@:
826
@@:
827
        push    esi
827
        push    esi
828
        test    edi, edi
828
        test    edi, edi
829
        jz      @f
829
        jz      @f
830
        stdcall is_region_userspace, edi, [ebp + APP_HDR.filename_size]
830
        stdcall is_region_userspace, edi, [ebp + APP_HDR.filename_size]
831
        jnz     @f
831
        jnz     @f
832
        mov     al, '/'
832
        mov     al, '/'
833
        stosb
833
        stosb
834
        rep movsb
834
        rep movsb
835
        mov     byte [edi], 0
835
        mov     byte [edi], 0
836
@@:
836
@@:
837
        call    kernel_free
837
        call    kernel_free
838
        mov     edi, [ebp + APP_HDR.cmdline]
838
        mov     edi, [ebp + APP_HDR.cmdline]
839
        test    edi, edi
839
        test    edi, edi
840
        jz      .check_tls_header
840
        jz      .check_tls_header
841
        lea     esi, [ebp + sizeof.APP_HDR]
841
        lea     esi, [ebp + sizeof.APP_HDR]
842
        mov     ecx, [ebp + APP_HDR.cmdline_size]
842
        mov     ecx, [ebp + APP_HDR.cmdline_size]
843
        cmp     ecx, 256
843
        cmp     ecx, 256
844
        jb      .copy_cmdline
844
        jb      .copy_cmdline
845
        mov     edi, [ebp + APP_HDR._emem]
845
        mov     edi, [ebp + APP_HDR._emem]
846
        add     edi, PAGE_SIZE-1
846
        add     edi, PAGE_SIZE-1
847
        and     edi, -PAGE_SIZE
847
        and     edi, -PAGE_SIZE
848
        sub     edi, ecx
848
        sub     edi, ecx
849
        dec     edi
849
        dec     edi
850
        cmp     word [6], '00'
850
        cmp     word [6], '00'
851
        jne     @f
851
        jne     @f
852
        mov     [APP_HEADER_00_.i_param], edi
852
        mov     [APP_HEADER_00_.i_param], edi
853
        jmp     .copy_cmdline
853
        jmp     .copy_cmdline
854
@@:
854
@@:
855
        mov     [APP_HEADER_01_.i_param], edi
855
        mov     [APP_HEADER_01_.i_param], edi
856
.copy_cmdline:
856
.copy_cmdline:
857
        inc     ecx  ; keep in mind about 0 in the end
857
        inc     ecx  ; keep in mind about 0 in the end
858
        stdcall is_region_userspace, edi, ecx
858
        stdcall is_region_userspace, edi, ecx
859
        jnz     .check_tls_header
859
        jnz     .check_tls_header
860
        dec     ecx
860
        dec     ecx
861
        rep movsb
861
        rep movsb
862
        mov     byte [edi], 0
862
        mov     byte [edi], 0
863
.check_tls_header:
863
.check_tls_header:
864
        cmp     word [6], '02'
864
        cmp     word [6], '02'
865
        jne     .try_load_dll ;.cleanup
865
        jne     .try_load_dll ;.cleanup
866
        call    init_heap
866
        call    init_heap
867
        stdcall user_alloc, PAGE_SIZE
867
        stdcall user_alloc, PAGE_SIZE
868
        mov     edx, [current_slot]
868
        mov     edx, [current_slot]
869
        mov     [edx + APPDATA.tls_base], eax
869
        mov     [edx + APPDATA.tls_base], eax
870
        mov     [tls_data_l+2], ax
870
        mov     [tls_data_l+2], ax
871
        shr     eax, 16
871
        shr     eax, 16
872
        mov     [tls_data_l+4], al
872
        mov     [tls_data_l+4], al
873
        mov     [tls_data_l+7], ah
873
        mov     [tls_data_l+7], ah
874
        mov     dx, app_tls
874
        mov     dx, app_tls
875
        mov     fs, dx       
875
        mov     fs, dx       
876
; { Patch by Coldy, For DLL autoload    
876
; { Patch by Coldy, For DLL autoload    
877
.try_load_dll:         
877
.try_load_dll:         
878
; Test app header version 
878
; Test app header version 
879
        mov     ecx, dword[ebp + APP_HDR.img_base]
879
        mov     ecx, dword[ebp + APP_HDR.img_base]
880
        cmp     dword[ecx+8], 2
880
        cmp     dword[ecx+8], 2
881
        jne     .cleanup
881
        jne     .cleanup
882
;if APP_HEADER.version = 2 => load lib/dll.obj & change eip to APP_STARTUP_THUNK
882
;if APP_HEADER.version = 2 => load lib/dll.obj & change eip to APP_STARTUP_THUNK
883
        DEBUGF 1, 'K : App header version 2\n'
883
        DEBUGF 1, 'K : App header version 2\n'
884
        stdcall load_library, dll_lib_path, 0
884
        stdcall load_library, dll_lib_path, 0
885
        cmp     eax, 0
885
        cmp     eax, 0
886
        jne     @f
886
        jne     @f
887
; Something went wrong (TODO: Next 2 line is code copy after .cleanup)  
887
; Something went wrong (TODO: Next 2 line is code copy after .cleanup)  
888
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
888
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
889
        stdcall kernel_free, ebp
889
        stdcall kernel_free, ebp
890
        DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!\n'
890
        DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!\n'
891
        mov     ebx, dll_error_msg
891
        mov     ebx, dll_error_msg
892
        mov     ebp, notifyapp
892
        mov     ebp, notifyapp
893
        call    fs_execute_from_sysdir_param
893
        call    fs_execute_from_sysdir_param
894
; Terminate process (TODO: Need jump to .cleanup after sys_end ?) 
894
; Terminate process (TODO: Need jump to .cleanup after sys_end ?) 
895
        call    sys_end
895
        call    sys_end
896
        
896
        
897
@@:       
897
@@:       
898
; Find APP_STARTUP_THUNK in DLL.OBJ
898
; Find APP_STARTUP_THUNK in DLL.OBJ
899
        sub     eax, 4
899
        sub     eax, 4
900
        mov     eax, [eax]
900
        mov     eax, [eax]
901
        
901
        
902
;.change_eip:
902
;.change_eip:
903
        mov     ecx, [current_slot]
903
        mov     ecx, [current_slot]
904
        mov     ecx, [ecx + APPDATA.pl0_stack]
904
        mov     ecx, [ecx + APPDATA.pl0_stack]
905
        mov     [ecx+REG_EIP], eax
905
        mov     [ecx+REG_EIP], eax
906
        
906
        
907
; } End patch by Coldy, For DLL autoload
907
; } End patch by Coldy, For DLL autoload
908
.cleanup:
908
.cleanup:
909
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
909
        stdcall free_kernel_space, [ebp+APP_HDR.img_base]
910
        stdcall kernel_free, ebp
910
        stdcall kernel_free, ebp
911
        mov     ebx, [current_slot]
911
        mov     ebx, [current_slot]
912
        cmp     [ebx + APPDATA.debugger_slot], 0
912
        cmp     [ebx + APPDATA.debugger_slot], 0
913
        je      .exit
913
        je      .exit
914
        mov     [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
914
        mov     [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
915
        mov     [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
915
        mov     [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
916
        call    change_task
916
        call    change_task
917
.exit:
917
.exit:
918
        popad
918
        popad
919
        iretd
919
        iretd
920
 
920
 
921
EFL_IF      = 0x0200
921
EFL_IF      = 0x0200
922
EFL_IOPL1   = 0x1000
922
EFL_IOPL1   = 0x1000
923
EFL_IOPL2   = 0x2000
923
EFL_IOPL2   = 0x2000
924
EFL_IOPL3   = 0x3000
924
EFL_IOPL3   = 0x3000
925
 
925
 
926
align 4
926
align 4
927
proc set_app_params stdcall,slot:dword, params:dword, flags:dword
927
proc set_app_params stdcall,slot:dword, params:dword, flags:dword
928
 
928
 
929
       locals
929
       locals
930
         pl0_stack dd ?
930
         pl0_stack dd ?
931
       endl
931
       endl
932
 
932
 
933
        mov     eax, [xsave_area_size]
933
        mov     eax, [xsave_area_size]
934
        add     eax, RING0_STACK_SIZE
934
        add     eax, RING0_STACK_SIZE
935
        stdcall kernel_alloc, eax
935
        stdcall kernel_alloc, eax
936
        mov     [pl0_stack], eax
936
        mov     [pl0_stack], eax
937
 
937
 
938
        lea     edi, [eax+RING0_STACK_SIZE]
938
        lea     edi, [eax+RING0_STACK_SIZE]
939
 
939
 
940
        mov     eax, [slot]
940
        mov     eax, [slot]
941
        mov     ebx, eax
941
        mov     ebx, eax
942
 
942
 
943
        shl     eax, BSF sizeof.APPDATA
943
        shl     eax, BSF sizeof.APPDATA
944
        mov     [SLOT_BASE + eax + APPDATA.fpu_state], edi
944
        mov     [SLOT_BASE + eax + APPDATA.fpu_state], edi
945
        mov     [SLOT_BASE + eax + APPDATA.exc_handler], 0
945
        mov     [SLOT_BASE + eax + APPDATA.exc_handler], 0
946
        mov     [SLOT_BASE + eax + APPDATA.except_mask], 0
946
        mov     [SLOT_BASE + eax + APPDATA.except_mask], 0
947
        mov     [SLOT_BASE + eax + APPDATA.terminate_protection], 80000001h
947
        mov     [SLOT_BASE + eax + APPDATA.terminate_protection], 80000001h
948
 
948
 
949
;set default io permission map
949
;set default io permission map
950
        mov     ecx, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map]
950
        mov     ecx, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map]
951
        mov     [SLOT_BASE + eax + APPDATA.io_map], ecx
951
        mov     [SLOT_BASE + eax + APPDATA.io_map], ecx
952
        mov     ecx, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4]
952
        mov     ecx, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4]
953
        mov     [SLOT_BASE + eax + APPDATA.io_map + 4], ecx
953
        mov     [SLOT_BASE + eax + APPDATA.io_map + 4], ecx
954
 
954
 
955
        mov     esi, fpu_data
955
        mov     esi, fpu_data
956
        mov     ecx, [xsave_area_size]
956
        mov     ecx, [xsave_area_size]
957
        add     ecx, 3
957
        add     ecx, 3
958
        shr     ecx, 2
958
        shr     ecx, 2
959
        rep movsd
959
        rep movsd
960
 
960
 
961
        cmp     [thread_count], ebx
961
        cmp     [thread_count], ebx
962
        adc     [thread_count], 0   ; update number of processes
962
        adc     [thread_count], 0   ; update number of processes
963
        shl     ebx, BSF sizeof.APPDATA
963
        shl     ebx, BSF sizeof.APPDATA
964
        lea     edx, [SLOT_BASE + ebx + APP_EV_OFFSET]
964
        lea     edx, [SLOT_BASE + ebx + APP_EV_OFFSET]
965
        mov     [SLOT_BASE + ebx + APPDATA.fd_ev], edx
965
        mov     [SLOT_BASE + ebx + APPDATA.fd_ev], edx
966
        mov     [SLOT_BASE + ebx + APPDATA.bk_ev], edx
966
        mov     [SLOT_BASE + ebx + APPDATA.bk_ev], edx
967
 
967
 
968
        add     edx, APP_OBJ_OFFSET - APP_EV_OFFSET
968
        add     edx, APP_OBJ_OFFSET - APP_EV_OFFSET
969
        mov     [SLOT_BASE + ebx + APPDATA.fd_obj], edx
969
        mov     [SLOT_BASE + ebx + APPDATA.fd_obj], edx
970
        mov     [SLOT_BASE + ebx + APPDATA.bk_obj], edx
970
        mov     [SLOT_BASE + ebx + APPDATA.bk_obj], edx
971
 
971
 
972
        mov     eax, [pl0_stack]
972
        mov     eax, [pl0_stack]
973
        mov     [SLOT_BASE + ebx + APPDATA.pl0_stack], eax
973
        mov     [SLOT_BASE + ebx + APPDATA.pl0_stack], eax
974
        add     eax, RING0_STACK_SIZE
974
        add     eax, RING0_STACK_SIZE
975
        mov     [SLOT_BASE + ebx + APPDATA.saved_esp0], eax
975
        mov     [SLOT_BASE + ebx + APPDATA.saved_esp0], eax
976
 
976
 
977
        push    ebx
977
        push    ebx
978
        stdcall kernel_alloc, maxPathLength             ;TODO
978
        stdcall kernel_alloc, maxPathLength             ;TODO
979
        pop     ebx
979
        pop     ebx
980
        mov     esi, [current_slot]
980
        mov     esi, [current_slot]
981
        mov     esi, [esi + APPDATA.cur_dir]
981
        mov     esi, [esi + APPDATA.cur_dir]
982
        mov     ecx, maxPathLength/4
982
        mov     ecx, maxPathLength/4
983
        mov     edi, eax
983
        mov     edi, eax
984
        mov     [SLOT_BASE + ebx + APPDATA.cur_dir], eax
984
        mov     [SLOT_BASE + ebx + APPDATA.cur_dir], eax
985
        rep movsd
985
        rep movsd
986
 
986
 
987
        mov     [SLOT_BASE + ebx + APPDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
987
        mov     [SLOT_BASE + ebx + APPDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
988
        inc     dword [process_number]
988
        inc     dword [process_number]
989
        mov     eax, [process_number]
989
        mov     eax, [process_number]
990
        mov     [SLOT_BASE + ebx + APPDATA.tid], eax    ;set TID
990
        mov     [SLOT_BASE + ebx + APPDATA.tid], eax    ;set TID
991
 
991
 
992
        mov     eax, [slot]
992
        mov     eax, [slot]
993
        mov     [SLOT_BASE + ebx + APPDATA.wnd_number], al
993
        mov     [SLOT_BASE + ebx + APPDATA.wnd_number], al
994
        ;mov     ebx, eax
994
        ;mov     ebx, eax
995
        shl     eax, BSF sizeof.WDATA
995
        shl     eax, BSF sizeof.WDATA
996
        add     eax, window_data
996
        add     eax, window_data
997
        mov     ecx, eax
997
        mov     ecx, eax
998
        mov     [SLOT_BASE + ebx + APPDATA.window], eax
998
        mov     [SLOT_BASE + ebx + APPDATA.window], eax
999
        mov     [eax + WDATA.thread], ebx
999
        mov     [eax + WDATA.thread], ebx
1000
        add     [eax + WDATA.thread], SLOT_BASE
1000
        add     [eax + WDATA.thread], SLOT_BASE
1001
 
1001
 
1002
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1002
; set window state to 'normal' (non-minimized/maximized/rolled-up) state
1003
        mov     [eax + WDATA.fl_wstate], WSTATE_NORMAL + WSTATE_USED
1003
        mov     [eax + WDATA.fl_wstate], WSTATE_NORMAL + WSTATE_USED
1004
        mov     [eax + WDATA.fl_redraw], WSTATE_REDRAW
1004
        mov     [eax + WDATA.fl_redraw], WSTATE_REDRAW
1005
 
1005
 
1006
        mov     edx, [def_cursor]
1006
        mov     edx, [def_cursor]
1007
        mov     [eax + WDATA.cursor], edx
1007
        mov     [eax + WDATA.cursor], edx
1008
 
1008
 
1009
;set draw data to full screen
1009
;set draw data to full screen
1010
        xor     eax, eax
1010
        xor     eax, eax
1011
        mov     [ecx + WDATA.draw_data.left], eax
1011
        mov     [ecx + WDATA.draw_data.left], eax
1012
        mov     [ecx + WDATA.draw_data.top], eax
1012
        mov     [ecx + WDATA.draw_data.top], eax
1013
        mov     eax, [screen_workarea.right]
1013
        mov     eax, [screen_workarea.right]
1014
        mov     [ecx + WDATA.draw_data.right], eax
1014
        mov     [ecx + WDATA.draw_data.right], eax
1015
        mov     eax, [screen_workarea.bottom]
1015
        mov     eax, [screen_workarea.bottom]
1016
        mov     [ecx + WDATA.draw_data.bottom], eax
1016
        mov     [ecx + WDATA.draw_data.bottom], eax
1017
 
1017
 
1018
        mov     ebx, [pl0_stack]
1018
        mov     ebx, [pl0_stack]
1019
        mov     esi, [params]
1019
        mov     esi, [params]
1020
        lea     ecx, [ebx + REG_EIP]
1020
        lea     ecx, [ebx + REG_EIP]
1021
        xor     eax, eax
1021
        xor     eax, eax
1022
 
1022
 
1023
        mov     [ebx + REG_RET], dword common_app_entry
1023
        mov     [ebx + REG_RET], dword common_app_entry
1024
        mov     [ebx + REG_EDI], eax
1024
        mov     [ebx + REG_EDI], eax
1025
        mov     [ebx + REG_ESI], eax
1025
        mov     [ebx + REG_ESI], eax
1026
        mov     [ebx + REG_EBP], eax
1026
        mov     [ebx + REG_EBP], eax
1027
        mov     [ebx + REG_ESP], ecx;ebx+REG_EIP
1027
        mov     [ebx + REG_ESP], ecx;ebx+REG_EIP
1028
        mov     [ebx + REG_EBX], eax
1028
        mov     [ebx + REG_EBX], eax
1029
        mov     [ebx + REG_EDX], eax
1029
        mov     [ebx + REG_EDX], eax
1030
        mov     [ebx + REG_ECX], eax
1030
        mov     [ebx + REG_ECX], eax
1031
        mov     [ebx + REG_EAX], eax
1031
        mov     [ebx + REG_EAX], eax
1032
 
1032
 
1033
        mov     eax, [esi + APP_HDR.eip]
1033
        mov     eax, [esi + APP_HDR.eip]
1034
        mov     [ebx + REG_EIP], eax
1034
        mov     [ebx + REG_EIP], eax
1035
        mov     [ebx + REG_CS], dword app_code
1035
        mov     [ebx + REG_CS], dword app_code
1036
        mov     ecx, USER_PRIORITY
1036
        mov     ecx, USER_PRIORITY
1037
 
1037
 
1038
        test    byte [flags], 2
1038
        test    byte [flags], 2
1039
        jz      @F
1039
        jz      @F
1040
 
1040
 
1041
        mov     [ebx + REG_CS], dword os_code ; kernel thread
1041
        mov     [ebx + REG_CS], dword os_code ; kernel thread
1042
        mov     ecx, MAX_PRIORITY
1042
        mov     ecx, MAX_PRIORITY
1043
@@:
1043
@@:
1044
        mov     [ebx + REG_EFLAGS], dword EFL_IOPL1+EFL_IF
1044
        mov     [ebx + REG_EFLAGS], dword EFL_IOPL1+EFL_IF
1045
 
1045
 
1046
        mov     eax, [esi + APP_HDR.esp]
1046
        mov     eax, [esi + APP_HDR.esp]
1047
        mov     [ebx + REG_APP_ESP], eax
1047
        mov     [ebx + REG_APP_ESP], eax
1048
        mov     [ebx + REG_SS], dword app_data
1048
        mov     [ebx + REG_SS], dword app_data
1049
 
1049
 
1050
        lea     edx, [ebx + REG_RET]
1050
        lea     edx, [ebx + REG_RET]
1051
        mov     ebx, [slot]
1051
        mov     ebx, [slot]
1052
        shl     ebx, BSF sizeof.APPDATA
1052
        shl     ebx, BSF sizeof.APPDATA
1053
        mov     [SLOT_BASE + ebx + APPDATA.saved_esp], edx
1053
        mov     [SLOT_BASE + ebx + APPDATA.saved_esp], edx
1054
 
1054
 
1055
        xor     edx, edx; process state - running
1055
        xor     edx, edx; process state - running
1056
; set if debuggee
1056
; set if debuggee
1057
        test    byte [flags], 1
1057
        test    byte [flags], 1
1058
        jz      .no_debug
1058
        jz      .no_debug
1059
        mov     eax, [current_slot_idx]
1059
        mov     eax, [current_slot_idx]
1060
        mov     [SLOT_BASE + ebx + APPDATA.debugger_slot], eax
1060
        mov     [SLOT_BASE + ebx + APPDATA.debugger_slot], eax
1061
.no_debug:
1061
.no_debug:
1062
        mov     [SLOT_BASE + ebx + APPDATA.state], dl
1062
        mov     [SLOT_BASE + ebx + APPDATA.state], dl
1063
        lea     edx, [SLOT_BASE + ebx]
1063
        lea     edx, [SLOT_BASE + ebx]
1064
        call    scheduler_add_thread
1064
        call    scheduler_add_thread
1065
        ret
1065
        ret
1066
endp
1066
endp
1067
 
1067
 
1068
align 4
1068
align 4
1069
get_stack_base:
1069
get_stack_base:
1070
        mov     eax, [current_slot]
1070
        mov     eax, [current_slot]
1071
        mov     eax, [eax + APPDATA.pl0_stack]
1071
        mov     eax, [eax + APPDATA.pl0_stack]
1072
        ret
1072
        ret
1073
 
1073
 
1074
align 4
1074
align 4
1075
get_curr_slot:
1075
get_curr_slot:
1076
        mov     eax, [current_slot]
1076
        mov     eax, [current_slot]
1077
        ret
1077
        ret
1078
 
1078
 
1079
pid_to_appdata:
1079
pid_to_appdata:
1080
;Input:
1080
;Input:
1081
;  eax - pid of process
1081
;  eax - pid of process
1082
;Output:
1082
;Output:
1083
;  eax - 0 - not found or pointer on APPDATA
1083
;  eax - 0 - not found or pointer on APPDATA
1084
        push    ebx
1084
        push    ebx
1085
        push    ecx
1085
        push    ecx
1086
        mov     ebx, [thread_count]
1086
        mov     ebx, [thread_count]
1087
        shl     ebx, BSF sizeof.APPDATA ; multiply by size
1087
        shl     ebx, BSF sizeof.APPDATA ; multiply by size
1088
        ; skip first process in the task table
1088
        ; skip first process in the task table
-
 
1089
        mov     ecx, SLOT_BASE
-
 
1090
        add     ebx, ecx
1089
.loop:
1091
.loop:
1090
        add     ecx, sizeof.APPDATA
1092
        add     ecx, sizeof.APPDATA
1091
        cmp     [SLOT_BASE + ecx + APPDATA.state], TSTATE_FREE
1093
        cmp     [ecx + APPDATA.state], TSTATE_FREE
1092
        jz      @f  ;skip empty slots
1094
        jz      @f  ;skip empty slots
1093
        cmp     [SLOT_BASE + ecx + APPDATA.tid], eax
1095
        cmp     [ecx + APPDATA.tid], eax
1094
        jz      .pid_found
1096
        jz      .pid_found
1095
        ;ecx = offset of current process info entry
-
 
1096
        ;ebx = maximum permitted offset
-
 
1097
@@:
1097
@@:
1098
        cmp     ecx, ebx
1098
        cmp     ecx, ebx
1099
        jb      .loop
1099
        jb      .loop
1100
 
1100
 
1101
        pop     ecx
1101
        pop     ecx
1102
        pop     ebx
1102
        pop     ebx
1103
        xor     eax, eax
1103
        xor     eax, eax
1104
        ret
1104
        ret
1105
.pid_found:
1105
.pid_found:
1106
        mov     eax, ecx
1106
        mov     eax, ecx
1107
        pop     ecx
1107
        pop     ecx
1108
        pop     ebx
1108
        pop     ebx
1109
        ret
1109
        ret
1110
 
1110
 
1111
include "debug.inc"
1111
include "debug.inc"