Subversion Repositories Kolibri OS

Rev

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

Rev 1992 Rev 2089
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2009. 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: 1992 $
8
$Revision: 2089 $
9
 
9
 
10
 
10
 
11
DRV_COMPAT   equ  5  ;minimal required drivers version
11
DRV_COMPAT   equ  5  ;minimal required drivers version
12
DRV_CURRENT  equ  5  ;current drivers model version
12
DRV_CURRENT  equ  6  ;current drivers model version
13
 
13
 
14
DRV_VERSION equ (DRV_COMPAT shl 16) or DRV_CURRENT
14
DRV_VERSION equ (DRV_COMPAT shl 16) or DRV_CURRENT
15
PID_KERNEL  equ 1    ;os_idle thread
15
PID_KERNEL  equ 1    ;os_idle thread
16
 
16
 
17
align 4
17
align 4
18
proc attach_int_handler stdcall, irq:dword, handler:dword, access_rights:dword
18
proc attach_int_handler stdcall, irq:dword, handler:dword, access_rights:dword
-
 
19
 
-
 
20
         pushfd
-
 
21
         cli
19
 
22
 
20
         push ebx
23
         push ebx
21
 
24
 
22
         mov  ebx, [irq]                   ;irq num
25
         mov  ebx, [irq]                   ;irq num
23
         test ebx, ebx
26
         test ebx, ebx
24
         jz   .err
27
         jz   .err
25
         cmp  ebx, 15                      ; hidnplayr says: we only have 16 IRQ's
28
         cmp  ebx, 15                      ; hidnplayr says: we only have 16 IRQ's
26
         ja   .err
29
         ja   .err
27
         mov  eax, [handler]
30
         mov  eax, [handler]
28
         test eax, eax
31
         test eax, eax
29
         jz   .err
32
         jz   .err
30
         cmp  [irq_owner + 4 * ebx], 0
-
 
31
         je   @f
-
 
32
 
-
 
33
         mov  ecx, [irq_rights + 4 * ebx]  ; Rights : 0 - full access, 1 - read only, 2 - forbidden
-
 
34
         test ecx, ecx
-
 
35
         jnz  .err
-
 
36
 
-
 
37
@@:
33
 
38
         mov  [irq_tab+ebx*4], eax
-
 
39
 
-
 
40
         mov  eax, [access_rights]
-
 
41
         mov  [irq_rights + 4 * ebx], eax
-
 
42
 
-
 
43
         mov  [irq_owner + 4 * ebx], PID_KERNEL  ; all handlers belong to a kernel
-
 
44
 
34
         mov  [irq_tab+ebx*4], eax
45
         stdcall enable_irq, [irq]
35
         stdcall enable_irq, [irq]
46
         pop ebx
36
         pop ebx
47
         mov eax, 1
37
         mov eax, 1
-
 
38
         popfd
48
         ret
39
         ret
49
.err:
40
.err:
50
         pop ebx
41
         pop ebx
51
         xor eax, eax
42
         xor eax, eax
-
 
43
         popfd
52
         ret
44
         ret
53
endp
45
endp
54
 
-
 
55
uglobal
-
 
56
 
-
 
57
	irq_rights	 rd	 16
-
 
58
 
-
 
59
endg
-
 
60
 
46
 
61
proc get_int_handler stdcall, irq:dword
47
proc get_int_handler stdcall, irq:dword
62
 
48
 
63
	mov	eax, [irq]
49
	mov	eax, [irq]
64
 
-
 
65
	cmp	[irq_rights + 4 * eax], dword 1
-
 
66
	ja	.err
-
 
67
 
50
 
68
	mov	eax, [irq_tab + 4 * eax]
51
	mov	eax, [irq_tab + 4 * eax]
69
	ret
-
 
70
 
-
 
71
     .err:
-
 
72
	xor	eax, eax
-
 
73
	ret
-
 
74
 
52
	ret
75
endp
53
endp
76
 
54
 
77
align 4
55
align 4
78
proc  detach_int_handler
56
proc  detach_int_handler
79
 
57
 
80
	   ret
58
	   ret
81
endp
59
endp
82
 
60
 
83
align 4
61
align 4
84
proc enable_irq stdcall, irq_line:dword
62
proc enable_irq stdcall, irq_line:dword
85
	   mov ebx, [irq_line]
63
	   mov ebx, [irq_line]
86
	   mov edx, 0x21
64
	   mov edx, 0x21
87
	   cmp ebx, 8
65
	   cmp ebx, 8
88
	   jb @F
66
	   jb @F
89
	   mov edx, 0xA1
67
	   mov edx, 0xA1
90
	   sub ebx,8
68
	   sub ebx,8
91
@@:
69
@@:
92
	   in al,dx
70
	   in al,dx
93
	   btr eax, ebx
71
	   btr eax, ebx
94
	   out dx, al
72
	   out dx, al
95
	   ret
73
	   ret
96
endp
74
endp
97
 
75
 
98
align 16
76
align 16
99
;; proc irq_serv
77
;; proc irq_serv
100
 
78
 
101
irq_serv:
79
irq_serv:
102
 
80
 
103
.irq_1:
81
.irq_1:
104
	   push 1
82
	   push 1
105
	   jmp .main
83
	   jmp .main
106
align 4
84
align 4
107
.irq_2:
85
.irq_2:
108
	   push 2
86
	   push 2
109
	   jmp .main
87
	   jmp .main
110
align 4
88
align 4
111
.irq_3:
89
.irq_3:
112
	   push 3
90
	   push 3
113
	   jmp .main
91
	   jmp .main
114
align 4
92
align 4
115
.irq_4:
93
.irq_4:
116
	   push 4
94
	   push 4
117
	   jmp .main
95
	   jmp .main
118
align 4
96
align 4
119
.irq_5:
97
.irq_5:
120
	   push 5
98
	   push 5
121
	   jmp .main
99
	   jmp .main
122
align 4
100
align 4
123
.irq_6:
101
.irq_6:
124
	   push 6
102
	   push 6
125
	   jmp .main
103
	   jmp .main
126
align 4
104
align 4
127
.irq_7:
105
.irq_7:
128
	   push 7
106
	   push 7
129
	   jmp .main
107
	   jmp .main
130
align 4
108
align 4
131
.irq_8:
109
.irq_8:
132
	   push 8
110
	   push 8
133
	   jmp .main
111
	   jmp .main
134
align 4
112
align 4
135
.irq_9:
113
.irq_9:
136
	   push 9
114
	   push 9
137
	   jmp .main
115
	   jmp .main
138
align 4
116
align 4
139
.irq_10:
117
.irq_10:
140
	   push 10
118
	   push 10
141
	   jmp .main
119
	   jmp .main
142
align 4
120
align 4
143
.irq_11:
121
.irq_11:
144
	   push 11
122
	   push 11
145
	   jmp .main
123
	   jmp .main
146
align 4
124
align 4
147
.irq_12:
125
.irq_12:
148
	   push 12
126
	   push 12
149
	   jmp .main
127
	   jmp .main
150
; align 4
128
; align 4
151
; .irq_13:
129
; .irq_13:
152
;	   push 13
130
;	   push 13
153
;	   jmp .main
131
;	   jmp .main
154
align 4
132
align 4
155
.irq_14:
133
.irq_14:
156
	   push 14
134
	   push 14
157
	   jmp .main
135
	   jmp .main
158
align 4
136
align 4
159
.irq_15:
137
.irq_15:
160
	   push 15
138
	   push 15
161
	   jmp .main
139
	   jmp .main
162
 
140
 
163
align 16
141
align 16
164
.main:
142
.main:
165
	   save_ring3_context
143
	   save_ring3_context
166
	   mov	 eax, [esp + 32]
144
	   mov	 eax, [esp + 32]
167
	   mov	 bx, app_data  ;os_data
145
	   mov	 bx, app_data  ;os_data
168
	   mov	 ds, bx
146
	   mov	 ds, bx
169
	   mov	 es, bx
147
	   mov	 es, bx
170
 
148
 
171
	   cmp	 [v86_irqhooks+eax*8], 0
149
	   cmp	 [v86_irqhooks+eax*8], 0
172
	   jnz	 v86_irq
150
	   jnz	 v86_irq
173
 
151
 
174
	   cmp   al, 6
152
	   cmp   al, 6
175
	   jnz   @f
153
	   jnz   @f
176
	   push  eax
154
	   push  eax
177
	   call  [fdc_irq_func]
155
	   call  [fdc_irq_func]
178
	   pop   eax
156
	   pop   eax
179
@@:
157
@@:
180
 
158
 
181
	   cmp   al, 14
159
	   cmp   al, 14
182
	   jnz   @f
160
	   jnz   @f
183
	   push  eax
161
	   push  eax
184
	   call  [irq14_func]
162
	   call  [irq14_func]
185
	   pop   eax
163
	   pop   eax
186
@@:
164
@@:
187
	   cmp   al, 15
165
	   cmp   al, 15
188
	   jnz   @f
166
	   jnz   @f
189
	   push  eax
167
	   push  eax
190
	   call  [irq15_func]
168
	   call  [irq15_func]
191
	   pop   eax
169
	   pop   eax
192
@@:
170
@@:
193
 
171
 
194
	   mov ebx, [irq_tab+eax*4]
172
	   mov ebx, [irq_tab+eax*4]
195
	   test ebx, ebx
173
	   test ebx, ebx
196
	   jz .exit
174
	   jz .exit
197
 
175
 
198
	   call ebx
176
	   call ebx
199
	   mov	[check_idle_semaphore],5
177
	   mov	[check_idle_semaphore],5
200
 
178
 
201
.exit:
179
.exit:
202
 
180
 
203
	   cmp dword [esp + 32], 8
181
       cmp dword [esp + 32], 8
204
	   mov al, 0x20
182
	   mov al, 0x20
205
	   jb @f
183
	   jb @f
206
	   out 0xa0, al
184
       out 0xa0, al
207
@@:
185
@@:
208
	   out 0x20, al
186
	   out 0x20, al
209
 
187
 
210
	   restore_ring3_context
188
	   restore_ring3_context
211
	   add	 esp, 4
189
	   add	 esp, 4
212
 
190
 
213
	   iret
191
	   iret
214
 
192
 
215
align 4
193
align 4
216
proc get_notify stdcall, p_ev:dword
194
proc get_notify stdcall, p_ev:dword
217
 
195
 
218
.wait:
196
.wait:
219
	   mov ebx,[current_slot]
197
	   mov ebx,[current_slot]
220
	   test dword [ebx+APPDATA.event_mask],EVENT_NOTIFY
198
	   test dword [ebx+APPDATA.event_mask],EVENT_NOTIFY
221
	   jz @f
199
	   jz @f
222
	   and dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY
200
	   and dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY
223
	   mov edi, [p_ev]
201
	   mov edi, [p_ev]
224
	   mov dword [edi], EV_INTR
202
	   mov dword [edi], EV_INTR
225
	   mov eax, [ebx+APPDATA.event]
203
	   mov eax, [ebx+APPDATA.event]
226
	   mov dword [edi+4], eax
204
	   mov dword [edi+4], eax
227
	   ret
205
	   ret
228
@@:
206
@@:
229
	   call change_task
207
	   call change_task
230
	   jmp .wait
208
	   jmp .wait
231
endp
209
endp
232
 
210
 
233
align 4
211
align 4
234
proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
212
proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
235
	   push ebx
213
	   push ebx
236
	   xor eax, eax
214
	   xor eax, eax
237
	   xor ebx, ebx
215
	   xor ebx, ebx
238
	   mov ah, byte [bus]
216
	   mov ah, byte [bus]
239
	   mov al, 6
217
	   mov al, 6
240
	   mov bh, byte [devfn]
218
	   mov bh, byte [devfn]
241
	   mov bl, byte [reg]
219
	   mov bl, byte [reg]
242
	   call pci_read_reg
220
	   call pci_read_reg
243
	   pop ebx
221
	   pop ebx
244
	   ret
222
	   ret
245
endp
223
endp
246
 
224
 
247
align 4
225
align 4
248
proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword
226
proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword
249
	   push ebx
227
	   push ebx
250
	   xor eax, eax
228
	   xor eax, eax
251
	   xor ebx, ebx
229
	   xor ebx, ebx
252
	   mov ah, byte [bus]
230
	   mov ah, byte [bus]
253
	   mov al, 5
231
	   mov al, 5
254
	   mov bh, byte [devfn]
232
	   mov bh, byte [devfn]
255
	   mov bl, byte [reg]
233
	   mov bl, byte [reg]
256
	   call pci_read_reg
234
	   call pci_read_reg
257
	   pop ebx
235
	   pop ebx
258
	   ret
236
	   ret
259
endp
237
endp
260
 
238
 
261
align 4
239
align 4
262
proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword
240
proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword
263
	   push ebx
241
	   push ebx
264
	   xor eax, eax
242
	   xor eax, eax
265
	   xor ebx, ebx
243
	   xor ebx, ebx
266
	   mov ah, byte [bus]
244
	   mov ah, byte [bus]
267
	   mov al, 4
245
	   mov al, 4
268
	   mov bh, byte [devfn]
246
	   mov bh, byte [devfn]
269
	   mov bl, byte [reg]
247
	   mov bl, byte [reg]
270
	   call pci_read_reg
248
	   call pci_read_reg
271
	   pop ebx
249
	   pop ebx
272
	   ret
250
	   ret
273
endp
251
endp
274
 
252
 
275
align 4
253
align 4
276
proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
254
proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
277
	   push ebx
255
	   push ebx
278
	   xor eax, eax
256
	   xor eax, eax
279
	   xor ebx, ebx
257
	   xor ebx, ebx
280
	   mov ah, byte [bus]
258
	   mov ah, byte [bus]
281
	   mov al, 8
259
	   mov al, 8
282
	   mov bh, byte [devfn]
260
	   mov bh, byte [devfn]
283
	   mov bl, byte [reg]
261
	   mov bl, byte [reg]
284
	   mov ecx, [val]
262
	   mov ecx, [val]
285
	   call pci_write_reg
263
	   call pci_write_reg
286
	   pop ebx
264
	   pop ebx
287
	   ret
265
	   ret
288
endp
266
endp
289
 
267
 
290
align 4
268
align 4
291
proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
269
proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
292
	   push ebx
270
	   push ebx
293
	   xor eax, eax
271
	   xor eax, eax
294
	   xor ebx, ebx
272
	   xor ebx, ebx
295
	   mov ah, byte [bus]
273
	   mov ah, byte [bus]
296
	   mov al, 9
274
	   mov al, 9
297
	   mov bh, byte [devfn]
275
	   mov bh, byte [devfn]
298
	   mov bl, byte [reg]
276
	   mov bl, byte [reg]
299
	   mov ecx, [val]
277
	   mov ecx, [val]
300
	   call pci_write_reg
278
	   call pci_write_reg
301
	   pop ebx
279
	   pop ebx
302
	   ret
280
	   ret
303
endp
281
endp
304
 
282
 
305
align 4
283
align 4
306
proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
284
proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
307
	   push ebx
285
	   push ebx
308
	   xor eax, eax
286
	   xor eax, eax
309
	   xor ebx, ebx
287
	   xor ebx, ebx
310
	   mov ah, byte [bus]
288
	   mov ah, byte [bus]
311
	   mov al, 10
289
	   mov al, 10
312
	   mov bh, byte [devfn]
290
	   mov bh, byte [devfn]
313
	   mov bl, byte [reg]
291
	   mov bl, byte [reg]
314
	   mov ecx, [val]
292
	   mov ecx, [val]
315
	   call pci_write_reg
293
	   call pci_write_reg
316
	   pop ebx
294
	   pop ebx
317
	   ret
295
	   ret
318
endp
296
endp
319
 
297
 
320
handle	   equ	IOCTL.handle
298
handle	   equ	IOCTL.handle
321
io_code    equ	IOCTL.io_code
299
io_code    equ	IOCTL.io_code
322
input	   equ	IOCTL.input
300
input	   equ	IOCTL.input
323
inp_size   equ	IOCTL.inp_size
301
inp_size   equ	IOCTL.inp_size
324
output	   equ	IOCTL.output
302
output	   equ	IOCTL.output
325
out_size   equ	IOCTL.out_size
303
out_size   equ	IOCTL.out_size
326
 
304
 
327
 
305
 
328
align 4
306
align 4
329
proc srv_handler stdcall, ioctl:dword
307
proc srv_handler stdcall, ioctl:dword
330
	   mov esi, [ioctl]
308
	   mov esi, [ioctl]
331
	   test esi, esi
309
	   test esi, esi
332
	   jz .err
310
	   jz .err
333
 
311
 
334
	   mov edi, [esi+handle]
312
	   mov edi, [esi+handle]
335
	   cmp [edi+SRV.magic], ' SRV'
313
	   cmp [edi+SRV.magic], ' SRV'
336
	   jne .fail
314
	   jne .fail
337
 
315
 
338
       cmp [edi+SRV.size], SRV.sizeof
316
       cmp [edi+SRV.size], SRV.sizeof
339
	   jne .fail
317
	   jne .fail
340
 
318
 
341
	   stdcall [edi+SRV.srv_proc], esi
319
	   stdcall [edi+SRV.srv_proc], esi
342
	   ret
320
	   ret
343
.fail:
321
.fail:
344
	   xor eax, eax
322
	   xor eax, eax
345
	   not eax
323
	   not eax
346
	   mov [esi+output], eax
324
	   mov [esi+output], eax
347
	   mov [esi+out_size], 4
325
	   mov [esi+out_size], 4
348
	   ret
326
	   ret
349
.err:
327
.err:
350
	   xor eax, eax
328
	   xor eax, eax
351
	   not eax
329
	   not eax
352
	   ret
330
	   ret
353
endp
331
endp
354
 
332
 
355
; param
333
; param
356
;  ecx= io_control
334
;  ecx= io_control
357
;
335
;
358
; retval
336
; retval
359
;  eax= error code
337
;  eax= error code
360
 
338
 
361
align 4
339
align 4
362
srv_handlerEx:
340
srv_handlerEx:
363
	   cmp ecx, OS_BASE
341
	   cmp ecx, OS_BASE
364
	   jae .fail
342
	   jae .fail
365
 
343
 
366
	   mov eax, [ecx+handle]
344
	   mov eax, [ecx+handle]
367
	   cmp [eax+SRV.magic], ' SRV'
345
	   cmp [eax+SRV.magic], ' SRV'
368
	   jne .fail
346
	   jne .fail
369
 
347
 
370
       cmp [eax+SRV.size], SRV.sizeof
348
       cmp [eax+SRV.size], SRV.sizeof
371
	   jne .fail
349
	   jne .fail
372
 
350
 
373
	   stdcall [eax+SRV.srv_proc], ecx
351
	   stdcall [eax+SRV.srv_proc], ecx
374
	   ret
352
	   ret
375
.fail:
353
.fail:
376
	   or eax, -1
354
	   or eax, -1
377
	   ret
355
	   ret
378
 
356
 
379
restore  handle
357
restore  handle
380
restore  io_code
358
restore  io_code
381
restore  input
359
restore  input
382
restore  inp_size
360
restore  inp_size
383
restore  output
361
restore  output
384
restore  out_size
362
restore  out_size
385
 
363
 
386
align 4
364
align 4
387
proc get_service stdcall, sz_name:dword
365
proc get_service stdcall, sz_name:dword
388
	   mov eax, [sz_name]
366
	   mov eax, [sz_name]
389
	   test eax, eax
367
	   test eax, eax
390
	   jnz @F
368
	   jnz @F
391
	   ret
369
	   ret
392
@@:
370
@@:
393
	   mov edx, [srv.fd]
371
	   mov edx, [srv.fd]
394
@@:
372
@@:
395
	   cmp edx, srv.fd-SRV_FD_OFFSET
373
	   cmp edx, srv.fd-SRV_FD_OFFSET
396
	   je .not_load
374
	   je .not_load
397
 
375
 
398
	   stdcall strncmp, edx, [sz_name], 16
376
	   stdcall strncmp, edx, [sz_name], 16
399
	   test eax, eax
377
	   test eax, eax
400
	   je .ok
378
	   je .ok
401
 
379
 
402
	   mov edx, [edx+SRV.fd]
380
	   mov edx, [edx+SRV.fd]
403
	   jmp @B
381
	   jmp @B
404
.not_load:
382
.not_load:
405
	   pop ebp
383
	   pop ebp
406
	   jmp load_driver
384
	   jmp load_driver
407
.ok:
385
.ok:
408
	   mov eax, edx
386
	   mov eax, edx
409
	   ret
387
	   ret
410
endp
388
endp
411
 
389
 
412
align 4
390
align 4
413
proc reg_service stdcall, name:dword, handler:dword
391
proc reg_service stdcall, name:dword, handler:dword
414
 
392
 
415
	   push ebx
393
	   push ebx
416
 
394
 
417
           xor eax, eax
395
           xor eax, eax
418
 
396
 
419
	   cmp [name], eax
397
	   cmp [name], eax
420
	   je .fail
398
	   je .fail
421
 
399
 
422
	   cmp [handler], eax
400
	   cmp [handler], eax
423
	   je .fail
401
	   je .fail
424
 
402
 
425
       mov eax, SRV.sizeof
403
       mov eax, SRV.sizeof
426
       call malloc
404
       call malloc
427
	   test eax, eax
405
	   test eax, eax
428
	   jz .fail
406
	   jz .fail
429
 
407
 
430
	   push esi
408
	   push esi
431
	   push edi
409
	   push edi
432
	   mov edi, eax
410
	   mov edi, eax
433
	   mov esi, [name]
411
	   mov esi, [name]
434
       movsd
412
       movsd
435
       movsd
413
       movsd
436
       movsd
414
       movsd
437
       movsd
415
       movsd
438
	   pop edi
416
	   pop edi
439
	   pop esi
417
	   pop esi
440
 
418
 
441
	   mov [eax+SRV.magic], ' SRV'
419
	   mov [eax+SRV.magic], ' SRV'
442
       mov [eax+SRV.size], SRV.sizeof
420
       mov [eax+SRV.size], SRV.sizeof
443
 
421
 
444
	   mov ebx, srv.fd-SRV_FD_OFFSET
422
	   mov ebx, srv.fd-SRV_FD_OFFSET
445
	   mov edx, [ebx+SRV.fd]
423
	   mov edx, [ebx+SRV.fd]
446
	   mov [eax+SRV.fd], edx
424
	   mov [eax+SRV.fd], edx
447
	   mov [eax+SRV.bk], ebx
425
	   mov [eax+SRV.bk], ebx
448
	   mov [ebx+SRV.fd], eax
426
	   mov [ebx+SRV.fd], eax
449
	   mov [edx+SRV.bk], eax
427
	   mov [edx+SRV.bk], eax
450
 
428
 
451
	   mov ecx, [handler]
429
	   mov ecx, [handler]
452
	   mov [eax+SRV.srv_proc], ecx
430
	   mov [eax+SRV.srv_proc], ecx
453
	   pop ebx
431
	   pop ebx
454
	   ret
432
	   ret
455
.fail:
433
.fail:
456
	   xor eax, eax
434
	   xor eax, eax
457
           pop ebx
435
           pop ebx
458
	   ret
436
	   ret
459
endp
437
endp
460
 
438
 
461
align 4
439
align 4
462
proc get_proc stdcall, exp:dword, sz_name:dword
440
proc get_proc stdcall, exp:dword, sz_name:dword
463
 
441
 
464
	   mov edx, [exp]
442
	   mov edx, [exp]
465
.next:
443
.next:
466
	   mov eax, [edx]
444
	   mov eax, [edx]
467
	   test eax, eax
445
	   test eax, eax
468
	   jz .end
446
	   jz .end
469
 
447
 
470
	   push edx
448
	   push edx
471
	   stdcall strncmp, eax, [sz_name], 16
449
	   stdcall strncmp, eax, [sz_name], 16
472
	   pop edx
450
	   pop edx
473
	   test eax, eax
451
	   test eax, eax
474
	   jz .ok
452
	   jz .ok
475
 
453
 
476
	   add edx,8
454
	   add edx,8
477
	   jmp .next
455
	   jmp .next
478
.ok:
456
.ok:
479
	   mov eax, [edx+4]
457
	   mov eax, [edx+4]
480
.end:
458
.end:
481
	   ret
459
	   ret
482
endp
460
endp
483
 
461
 
484
align 4
462
align 4
485
proc get_coff_sym stdcall, pSym:dword,count:dword, sz_sym:dword
463
proc get_coff_sym stdcall, pSym:dword,count:dword, sz_sym:dword
486
 
464
 
487
@@:
465
@@:
488
	   stdcall strncmp, [pSym], [sz_sym], 8
466
	   stdcall strncmp, [pSym], [sz_sym], 8
489
	   test eax,eax
467
	   test eax,eax
490
	   jz .ok
468
	   jz .ok
491
	   add [pSym], 18
469
	   add [pSym], 18
492
	   dec [count]
470
	   dec [count]
493
	   jnz @b
471
	   jnz @b
494
	   xor eax, eax
472
	   xor eax, eax
495
	   ret
473
	   ret
496
.ok:
474
.ok:
497
	   mov eax, [pSym]
475
	   mov eax, [pSym]
498
	   mov eax, [eax+8]
476
	   mov eax, [eax+8]
499
	   ret
477
	   ret
500
endp
478
endp
501
 
479
 
502
align 4
480
align 4
503
proc get_curr_task
481
proc get_curr_task
504
	   mov eax,[CURRENT_TASK]
482
	   mov eax,[CURRENT_TASK]
505
	   shl eax, 8
483
	   shl eax, 8
506
	   ret
484
	   ret
507
endp
485
endp
508
 
486
 
509
align 4
487
align 4
510
proc get_fileinfo stdcall, file_name:dword, info:dword
488
proc get_fileinfo stdcall, file_name:dword, info:dword
511
	   locals
489
	   locals
512
	     cmd     dd ?
490
	     cmd     dd ?
513
	     offset  dd ?
491
	     offset  dd ?
514
		     dd ?
492
		     dd ?
515
	     count   dd ?
493
	     count   dd ?
516
	     buff    dd ?
494
	     buff    dd ?
517
		     db ?
495
		     db ?
518
	     name    dd ?
496
	     name    dd ?
519
	   endl
497
	   endl
520
 
498
 
521
	   xor eax, eax
499
	   xor eax, eax
522
	   mov ebx, [file_name]
500
	   mov ebx, [file_name]
523
	   mov ecx, [info]
501
	   mov ecx, [info]
524
 
502
 
525
	   mov [cmd], 5
503
	   mov [cmd], 5
526
	   mov [offset], eax
504
	   mov [offset], eax
527
	   mov [offset+4], eax
505
	   mov [offset+4], eax
528
	   mov [count], eax
506
	   mov [count], eax
529
	   mov [buff], ecx
507
	   mov [buff], ecx
530
	   mov byte [buff+4], al
508
	   mov byte [buff+4], al
531
	   mov [name], ebx
509
	   mov [name], ebx
532
 
510
 
533
	   mov eax, 70
511
	   mov eax, 70
534
	   lea ebx, [cmd]
512
	   lea ebx, [cmd]
535
	   int 0x40
513
	   int 0x40
536
	   ret
514
	   ret
537
endp
515
endp
538
 
516
 
539
align 4
517
align 4
540
proc read_file stdcall,file_name:dword, buffer:dword, off:dword,\
518
proc read_file stdcall,file_name:dword, buffer:dword, off:dword,\
541
				     bytes:dword
519
				     bytes:dword
542
	   locals
520
	   locals
543
	     cmd     dd ?
521
	     cmd     dd ?
544
	     offset  dd ?
522
	     offset  dd ?
545
		     dd ?
523
		     dd ?
546
	     count   dd ?
524
	     count   dd ?
547
	     buff    dd ?
525
	     buff    dd ?
548
		     db ?
526
		     db ?
549
	     name    dd ?
527
	     name    dd ?
550
	   endl
528
	   endl
551
 
529
 
552
	   xor eax, eax
530
	   xor eax, eax
553
	   mov ebx, [file_name]
531
	   mov ebx, [file_name]
554
	   mov ecx, [off]
532
	   mov ecx, [off]
555
	   mov edx, [bytes]
533
	   mov edx, [bytes]
556
	   mov esi, [buffer]
534
	   mov esi, [buffer]
557
 
535
 
558
	   mov [cmd], eax
536
	   mov [cmd], eax
559
	   mov [offset], ecx
537
	   mov [offset], ecx
560
	   mov [offset+4], eax
538
	   mov [offset+4], eax
561
	   mov [count], edx
539
	   mov [count], edx
562
	   mov [buff], esi
540
	   mov [buff], esi
563
	   mov byte [buff+4], al
541
	   mov byte [buff+4], al
564
	   mov [name], ebx
542
	   mov [name], ebx
565
 
543
 
566
	   pushad
544
	   pushad
567
	   lea ebx, [cmd]
545
	   lea ebx, [cmd]
568
	   call file_system_lfn
546
	   call file_system_lfn
569
	   popad
547
	   popad
570
	   ret
548
	   ret
571
endp
549
endp
572
 
550
 
573
; description
551
; description
574
;  allocate kernel memory and loads the specified file
552
;  allocate kernel memory and loads the specified file
575
;
553
;
576
; param
554
; param
577
;  file_name= full path to file
555
;  file_name= full path to file
578
;
556
;
579
; retval
557
; retval
580
;  eax= file image in kernel memory
558
;  eax= file image in kernel memory
581
;  ebx= size of file
559
;  ebx= size of file
582
;
560
;
583
; warging
561
; warging
584
;  You mast call kernel_free() to delete each file
562
;  You mast call kernel_free() to delete each file
585
;  loaded by the load_file() function
563
;  loaded by the load_file() function
586
 
564
 
587
align 4
565
align 4
588
proc load_file stdcall, file_name:dword
566
proc load_file stdcall, file_name:dword
589
	   locals
567
	   locals
590
	     attr	dd ?
568
	     attr	dd ?
591
	     flags	dd ?
569
	     flags	dd ?
592
	     cr_time	dd ?
570
	     cr_time	dd ?
593
	     cr_date	dd ?
571
	     cr_date	dd ?
594
	     acc_time	dd ?
572
	     acc_time	dd ?
595
	     acc_date	dd ?
573
	     acc_date	dd ?
596
	     mod_time	dd ?
574
	     mod_time	dd ?
597
	     mod_date	dd ?
575
	     mod_date	dd ?
598
	     file_size	dd ?
576
	     file_size	dd ?
599
 
577
 
600
	     file	dd ?
578
	     file	dd ?
601
	     file2	dd ?
579
	     file2	dd ?
602
	   endl
580
	   endl
603
 
581
 
604
	   push esi
582
	   push esi
605
	   push edi
583
	   push edi
606
 
584
 
607
	   lea eax, [attr]
585
	   lea eax, [attr]
608
	   stdcall get_fileinfo, [file_name], eax
586
	   stdcall get_fileinfo, [file_name], eax
609
	   test eax, eax
587
	   test eax, eax
610
	   jnz .fail
588
	   jnz .fail
611
 
589
 
612
	   mov eax, [file_size]
590
	   mov eax, [file_size]
613
	   cmp eax, 1024*1024*16
591
	   cmp eax, 1024*1024*16
614
	   ja .fail
592
	   ja .fail
615
 
593
 
616
	   stdcall kernel_alloc, [file_size]
594
	   stdcall kernel_alloc, [file_size]
617
	   mov [file], eax
595
	   mov [file], eax
618
       test eax, eax
596
       test eax, eax
619
       jz .fail
597
       jz .fail
620
 
598
 
621
	   stdcall read_file, [file_name], eax, dword 0, [file_size]
599
	   stdcall read_file, [file_name], eax, dword 0, [file_size]
622
	   cmp ebx, [file_size]
600
	   cmp ebx, [file_size]
623
	   jne .cleanup
601
	   jne .cleanup
624
 
602
 
625
	   mov eax, [file]
603
	   mov eax, [file]
626
	   cmp dword [eax], 0x4B43504B
604
	   cmp dword [eax], 0x4B43504B
627
	   jne .exit
605
	   jne .exit
628
	   mov ebx, [eax+4]
606
	   mov ebx, [eax+4]
629
	   mov [file_size], ebx
607
	   mov [file_size], ebx
630
	   stdcall kernel_alloc, ebx
608
	   stdcall kernel_alloc, ebx
631
 
609
 
632
	   test eax, eax
610
	   test eax, eax
633
	   jz .cleanup
611
	   jz .cleanup
634
 
612
 
635
	   mov [file2], eax
613
	   mov [file2], eax
636
       pushfd
614
       pushfd
637
       cli
615
       cli
638
	   stdcall unpack, [file], eax
616
	   stdcall unpack, [file], eax
639
       popfd
617
       popfd
640
	   stdcall kernel_free, [file]
618
	   stdcall kernel_free, [file]
641
	   mov eax, [file2]
619
	   mov eax, [file2]
642
	   mov ebx, [file_size]
620
	   mov ebx, [file_size]
643
.exit:
621
.exit:
644
	   push eax
622
	   push eax
645
	   lea edi, [eax+ebx]	  ;cleanup remain space
623
	   lea edi, [eax+ebx]	  ;cleanup remain space
646
	   mov ecx, 4096	  ;from file end
624
	   mov ecx, 4096	  ;from file end
647
	   and ebx, 4095
625
	   and ebx, 4095
648
	   jz  @f
626
	   jz  @f
649
	   sub ecx, ebx
627
	   sub ecx, ebx
650
	   xor eax, eax
628
	   xor eax, eax
651
	   cld
629
	   cld
652
	   rep stosb
630
	   rep stosb
653
@@:
631
@@:
654
	   mov ebx, [file_size]
632
	   mov ebx, [file_size]
655
	   pop eax
633
	   pop eax
656
	   pop edi
634
	   pop edi
657
	   pop esi
635
	   pop esi
658
	   ret
636
	   ret
659
.cleanup:
637
.cleanup:
660
	   stdcall kernel_free, [file]
638
	   stdcall kernel_free, [file]
661
.fail:
639
.fail:
662
	   xor eax, eax
640
	   xor eax, eax
663
	   xor ebx, ebx
641
	   xor ebx, ebx
664
	   pop edi
642
	   pop edi
665
	   pop esi
643
	   pop esi
666
	   ret
644
	   ret
667
endp
645
endp
668
 
646
 
669
align 4
647
align 4
670
proc get_proc_ex stdcall, proc_name:dword, imports:dword
648
proc get_proc_ex stdcall, proc_name:dword, imports:dword
671
 
649
 
672
.look_up:
650
.look_up:
673
	   mov edx, [imports]
651
	   mov edx, [imports]
674
	   test edx, edx
652
	   test edx, edx
675
	   jz .end
653
	   jz .end
676
	   mov edx, [edx]
654
	   mov edx, [edx]
677
	   test edx, edx
655
	   test edx, edx
678
	   jz .end
656
	   jz .end
679
.next:
657
.next:
680
	   mov eax, [edx]
658
	   mov eax, [edx]
681
	   test eax, eax
659
	   test eax, eax
682
	   jz .next_table
660
	   jz .next_table
683
 
661
 
684
	   push edx
662
	   push edx
685
       stdcall strncmp, eax, [proc_name], 256
663
       stdcall strncmp, eax, [proc_name], 256
686
	   pop edx
664
	   pop edx
687
	   test eax, eax
665
	   test eax, eax
688
	   jz .ok
666
	   jz .ok
689
 
667
 
690
	   add edx,8
668
	   add edx,8
691
	   jmp .next
669
	   jmp .next
692
.next_table:
670
.next_table:
693
	   add [imports], 4
671
	   add [imports], 4
694
	   jmp .look_up
672
	   jmp .look_up
695
.ok:
673
.ok:
696
	   mov eax, [edx+4]
674
	   mov eax, [edx+4]
697
	   ret
675
	   ret
698
.end:
676
.end:
699
	   xor eax, eax
677
	   xor eax, eax
700
	   ret
678
	   ret
701
endp
679
endp
702
 
680
 
703
align 4
681
align 4
704
proc fix_coff_symbols stdcall uses ebx esi, sec:dword, symbols:dword,\
682
proc fix_coff_symbols stdcall uses ebx esi, sec:dword, symbols:dword,\
705
		      sym_count:dword, strings:dword, imports:dword
683
		      sym_count:dword, strings:dword, imports:dword
706
	   locals
684
	   locals
707
	     retval dd ?
685
	     retval dd ?
708
	   endl
686
	   endl
709
 
687
 
710
	   mov edi, [symbols]
688
	   mov edi, [symbols]
711
	   mov [retval], 1
689
	   mov [retval], 1
712
.fix:
690
.fix:
713
	   movzx ebx, [edi+CSYM.SectionNumber]
691
	   movzx ebx, [edi+CSYM.SectionNumber]
714
	   test ebx, ebx
692
	   test ebx, ebx
715
	   jnz .internal
693
	   jnz .internal
716
	   mov eax, dword [edi+CSYM.Name]
694
	   mov eax, dword [edi+CSYM.Name]
717
	   test eax, eax
695
	   test eax, eax
718
	   jnz @F
696
	   jnz @F
719
 
697
 
720
	   mov edi, [edi+4]
698
	   mov edi, [edi+4]
721
	   add edi, [strings]
699
	   add edi, [strings]
722
@@:
700
@@:
723
	   push edi
701
	   push edi
724
	   stdcall get_proc_ex, edi,[imports]
702
	   stdcall get_proc_ex, edi,[imports]
725
	   pop edi
703
	   pop edi
726
 
704
 
727
	   xor ebx, ebx
705
	   xor ebx, ebx
728
	   test eax, eax
706
	   test eax, eax
729
	   jnz @F
707
	   jnz @F
730
 
708
 
731
	   mov esi, msg_unresolved
709
	   mov esi, msg_unresolved
732
	   call sys_msg_board_str
710
	   call sys_msg_board_str
733
	   mov esi, edi
711
	   mov esi, edi
734
	   call sys_msg_board_str
712
	   call sys_msg_board_str
735
	   mov esi, msg_CR
713
	   mov esi, msg_CR
736
	   call sys_msg_board_str
714
	   call sys_msg_board_str
737
 
715
 
738
	   mov [retval],0
716
	   mov [retval],0
739
@@:
717
@@:
740
	   mov edi, [symbols]
718
	   mov edi, [symbols]
741
	   mov [edi+CSYM.Value], eax
719
	   mov [edi+CSYM.Value], eax
742
	   jmp .next
720
	   jmp .next
743
.internal:
721
.internal:
744
	   cmp bx, -1
722
	   cmp bx, -1
745
	   je .next
723
	   je .next
746
	   cmp bx, -2
724
	   cmp bx, -2
747
	   je .next
725
	   je .next
748
 
726
 
749
	   dec ebx
727
	   dec ebx
750
	   shl ebx, 3
728
	   shl ebx, 3
751
	   lea ebx, [ebx+ebx*4]
729
	   lea ebx, [ebx+ebx*4]
752
	   add ebx, [sec]
730
	   add ebx, [sec]
753
 
731
 
754
	   mov eax, [ebx+CFS.VirtualAddress]
732
	   mov eax, [ebx+CFS.VirtualAddress]
755
	   add [edi+CSYM.Value], eax
733
	   add [edi+CSYM.Value], eax
756
.next:
734
.next:
757
	   add edi, CSYM_SIZE
735
	   add edi, CSYM_SIZE
758
	   mov [symbols], edi
736
	   mov [symbols], edi
759
	   dec [sym_count]
737
	   dec [sym_count]
760
	   jnz .fix
738
	   jnz .fix
761
	   mov eax, [retval]
739
	   mov eax, [retval]
762
	   ret
740
	   ret
763
endp
741
endp
764
 
742
 
765
align 4
743
align 4
766
proc fix_coff_relocs stdcall uses ebx esi, coff:dword, sym:dword, \
744
proc fix_coff_relocs stdcall uses ebx esi, coff:dword, sym:dword, \
767
	delta:dword
745
	delta:dword
768
	   locals
746
	   locals
769
	     n_sec     dd ?
747
	     n_sec     dd ?
770
	   endl
748
	   endl
771
 
749
 
772
	   mov eax, [coff]
750
	   mov eax, [coff]
773
	   movzx ebx, [eax+CFH.nSections]
751
	   movzx ebx, [eax+CFH.nSections]
774
	   mov [n_sec], ebx
752
	   mov [n_sec], ebx
775
	   lea esi, [eax+20]
753
	   lea esi, [eax+20]
776
.fix_sec:
754
.fix_sec:
777
	   mov edi, [esi+CFS.PtrReloc]
755
	   mov edi, [esi+CFS.PtrReloc]
778
	   add edi, [coff]
756
	   add edi, [coff]
779
 
757
 
780
	   movzx ecx, [esi+CFS.NumReloc]
758
	   movzx ecx, [esi+CFS.NumReloc]
781
	   test ecx, ecx
759
	   test ecx, ecx
782
	   jz .next
760
	   jz .next
783
.reloc_loop:
761
.reloc_loop:
784
	   mov ebx, [edi+CRELOC.SymIndex]
762
	   mov ebx, [edi+CRELOC.SymIndex]
785
	   add ebx,ebx
763
	   add ebx,ebx
786
	   lea ebx,[ebx+ebx*8]
764
	   lea ebx,[ebx+ebx*8]
787
	   add ebx, [sym]
765
	   add ebx, [sym]
788
 
766
 
789
	   mov edx, [ebx+CSYM.Value]
767
	   mov edx, [ebx+CSYM.Value]
790
 
768
 
791
	   cmp [edi+CRELOC.Type], 6
769
	   cmp [edi+CRELOC.Type], 6
792
	   je .dir_32
770
	   je .dir_32
793
 
771
 
794
	   cmp [edi+CRELOC.Type], 20
772
	   cmp [edi+CRELOC.Type], 20
795
	   jne .next_reloc
773
	   jne .next_reloc
796
.rel_32:
774
.rel_32:
797
	   mov eax, [edi+CRELOC.VirtualAddress]
775
	   mov eax, [edi+CRELOC.VirtualAddress]
798
	   add eax, [esi+CFS.VirtualAddress]
776
	   add eax, [esi+CFS.VirtualAddress]
799
	   sub edx, eax
777
	   sub edx, eax
800
	   sub edx, 4
778
	   sub edx, 4
801
	   jmp .fix
779
	   jmp .fix
802
.dir_32:
780
.dir_32:
803
	   mov eax, [edi+CRELOC.VirtualAddress]
781
	   mov eax, [edi+CRELOC.VirtualAddress]
804
	   add eax, [esi+CFS.VirtualAddress]
782
	   add eax, [esi+CFS.VirtualAddress]
805
.fix:
783
.fix:
806
	   add eax, [delta]
784
	   add eax, [delta]
807
	   add [eax], edx
785
	   add [eax], edx
808
.next_reloc:
786
.next_reloc:
809
	   add edi, 10
787
	   add edi, 10
810
	   dec ecx
788
	   dec ecx
811
	   jnz .reloc_loop
789
	   jnz .reloc_loop
812
.next:
790
.next:
813
	   add esi, COFF_SECTION_SIZE
791
	   add esi, COFF_SECTION_SIZE
814
	   dec [n_sec]
792
	   dec [n_sec]
815
	   jnz .fix_sec
793
	   jnz .fix_sec
816
.exit:
794
.exit:
817
	   ret
795
	   ret
818
endp
796
endp
819
 
797
 
820
proc rebase_coff stdcall uses ebx esi, coff:dword, sym:dword, \
798
proc rebase_coff stdcall uses ebx esi, coff:dword, sym:dword, \
821
	delta:dword
799
	delta:dword
822
	   locals
800
	   locals
823
	     n_sec     dd ?
801
	     n_sec     dd ?
824
	   endl
802
	   endl
825
 
803
 
826
	   mov eax, [coff]
804
	   mov eax, [coff]
827
	   movzx ebx, [eax+CFH.nSections]
805
	   movzx ebx, [eax+CFH.nSections]
828
	   mov [n_sec], ebx
806
	   mov [n_sec], ebx
829
	   lea esi, [eax+20]
807
	   lea esi, [eax+20]
830
	   mov edx, [delta]
808
	   mov edx, [delta]
831
.fix_sec:
809
.fix_sec:
832
	   mov edi, [esi+CFS.PtrReloc]
810
	   mov edi, [esi+CFS.PtrReloc]
833
	   add edi, [coff]
811
	   add edi, [coff]
834
 
812
 
835
	   movzx ecx, [esi+CFS.NumReloc]
813
	   movzx ecx, [esi+CFS.NumReloc]
836
	   test ecx, ecx
814
	   test ecx, ecx
837
	   jz .next
815
	   jz .next
838
.reloc_loop:
816
.reloc_loop:
839
	   cmp [edi+CRELOC.Type], 6
817
	   cmp [edi+CRELOC.Type], 6
840
	   jne .next_reloc
818
	   jne .next_reloc
841
.dir_32:
819
.dir_32:
842
	   mov eax, [edi+CRELOC.VirtualAddress]
820
	   mov eax, [edi+CRELOC.VirtualAddress]
843
	   add eax, [esi+CFS.VirtualAddress]
821
	   add eax, [esi+CFS.VirtualAddress]
844
	   add [eax+edx], edx
822
	   add [eax+edx], edx
845
.next_reloc:
823
.next_reloc:
846
	   add edi, 10
824
	   add edi, 10
847
	   dec ecx
825
	   dec ecx
848
	   jnz .reloc_loop
826
	   jnz .reloc_loop
849
.next:
827
.next:
850
	   add esi, COFF_SECTION_SIZE
828
	   add esi, COFF_SECTION_SIZE
851
	   dec [n_sec]
829
	   dec [n_sec]
852
	   jnz .fix_sec
830
	   jnz .fix_sec
853
.exit:
831
.exit:
854
	   ret
832
	   ret
855
endp
833
endp
856
 
834
 
857
align 4
835
align 4
858
proc load_driver stdcall, driver_name:dword
836
proc load_driver stdcall, driver_name:dword
859
	   locals
837
	   locals
860
	     coff      dd ?
838
	     coff      dd ?
861
	     sym       dd ?
839
	     sym       dd ?
862
	     strings   dd ?
840
	     strings   dd ?
863
	     img_size  dd ?
841
	     img_size  dd ?
864
	     img_base  dd ?
842
	     img_base  dd ?
865
	     start     dd ?
843
	     start     dd ?
866
 
844
 
867
	     exports   dd ?   ;fake exports table
845
	     exports   dd ?   ;fake exports table
868
		       dd ?
846
		       dd ?
869
	     file_name rb 13+16+4+1	 ; '/sys/drivers/.obj'
847
	     file_name rb 13+16+4+1	 ; '/sys/drivers/.obj'
870
	   endl
848
	   endl
871
 
849
 
872
	   lea	   edx, [file_name]
850
	   lea	   edx, [file_name]
873
	   mov	   dword [edx], '/sys'
851
	   mov	   dword [edx], '/sys'
874
	   mov	   dword [edx+4], '/dri'
852
	   mov	   dword [edx+4], '/dri'
875
	   mov	   dword [edx+8], 'vers'
853
	   mov	   dword [edx+8], 'vers'
876
	   mov	   byte [edx+12], '/'
854
	   mov	   byte [edx+12], '/'
877
	   mov	   esi, [driver_name]
855
	   mov	   esi, [driver_name]
878
.redo:
856
.redo:
879
           lea     edx, [file_name]
857
           lea     edx, [file_name]
880
	   lea	   edi, [edx+13]
858
	   lea	   edi, [edx+13]
881
	   mov	   ecx, 16
859
	   mov	   ecx, 16
882
@@:
860
@@:
883
	   lodsb
861
	   lodsb
884
	   test    al, al
862
	   test    al, al
885
	   jz	   @f
863
	   jz	   @f
886
	   stosb
864
	   stosb
887
	   loop    @b
865
	   loop    @b
888
@@:
866
@@:
889
	   mov	   dword [edi], '.obj'
867
	   mov	   dword [edi], '.obj'
890
	   mov	   byte [edi+4], 0
868
	   mov	   byte [edi+4], 0
891
	   stdcall load_file, edx
869
	   stdcall load_file, edx
892
 
870
 
893
	   test eax, eax
871
	   test eax, eax
894
	   jz .exit
872
	   jz .exit
895
 
873
 
896
	   mov [coff], eax
874
	   mov [coff], eax
897
 
875
 
898
	   movzx ecx, [eax+CFH.nSections]
876
	   movzx ecx, [eax+CFH.nSections]
899
	   xor ebx, ebx
877
	   xor ebx, ebx
900
 
878
 
901
	   lea edx, [eax+20]
879
	   lea edx, [eax+20]
902
@@:
880
@@:
903
	   add ebx, [edx+CFS.SizeOfRawData]
881
	   add ebx, [edx+CFS.SizeOfRawData]
904
	   add ebx, 15
882
	   add ebx, 15
905
	   and ebx, not 15
883
	   and ebx, not 15
906
	   add edx, COFF_SECTION_SIZE
884
	   add edx, COFF_SECTION_SIZE
907
	   dec ecx
885
	   dec ecx
908
	   jnz @B
886
	   jnz @B
909
	   mov [img_size], ebx
887
	   mov [img_size], ebx
910
 
888
 
911
	   stdcall kernel_alloc, ebx
889
	   stdcall kernel_alloc, ebx
912
	   test eax, eax
890
	   test eax, eax
913
	   jz .fail
891
	   jz .fail
914
	   mov [img_base], eax
892
	   mov [img_base], eax
915
 
893
 
916
	   mov edi, eax
894
	   mov edi, eax
917
	   xor eax, eax
895
	   xor eax, eax
918
	   mov ecx, [img_size]
896
	   mov ecx, [img_size]
919
	   add ecx, 4095
897
	   add ecx, 4095
920
	   and ecx, not 4095
898
	   and ecx, not 4095
921
	   shr ecx, 2
899
	   shr ecx, 2
922
	   cld
900
	   cld
923
	   rep stosd
901
	   rep stosd
924
 
902
 
925
	   mov edx, [coff]
903
	   mov edx, [coff]
926
	   movzx ebx, [edx+CFH.nSections]
904
	   movzx ebx, [edx+CFH.nSections]
927
	   mov edi, [img_base]
905
	   mov edi, [img_base]
928
	   lea eax, [edx+20]
906
	   lea eax, [edx+20]
929
@@:
907
@@:
930
	   mov [eax+CFS.VirtualAddress], edi
908
	   mov [eax+CFS.VirtualAddress], edi
931
	   mov esi, [eax+CFS.PtrRawData]
909
	   mov esi, [eax+CFS.PtrRawData]
932
	   test esi, esi
910
	   test esi, esi
933
	   jnz .copy
911
	   jnz .copy
934
	   add edi, [eax+CFS.SizeOfRawData]
912
	   add edi, [eax+CFS.SizeOfRawData]
935
	   jmp .next
913
	   jmp .next
936
.copy:
914
.copy:
937
	   add esi, edx
915
	   add esi, edx
938
	   mov ecx, [eax+CFS.SizeOfRawData]
916
	   mov ecx, [eax+CFS.SizeOfRawData]
939
	   cld
917
	   cld
940
	   rep movsb
918
	   rep movsb
941
.next:
919
.next:
942
	   add edi, 15
920
	   add edi, 15
943
	   and edi, not 15
921
	   and edi, not 15
944
	   add eax, COFF_SECTION_SIZE
922
	   add eax, COFF_SECTION_SIZE
945
	   dec ebx
923
	   dec ebx
946
	   jnz @B
924
	   jnz @B
947
 
925
 
948
	   mov ebx, [edx+CFH.pSymTable]
926
	   mov ebx, [edx+CFH.pSymTable]
949
	   add ebx, edx
927
	   add ebx, edx
950
	   mov [sym], ebx
928
	   mov [sym], ebx
951
	   mov ecx, [edx+CFH.nSymbols]
929
	   mov ecx, [edx+CFH.nSymbols]
952
	   add ecx,ecx
930
	   add ecx,ecx
953
	   lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE
931
	   lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE
954
	   add ecx, [sym]
932
	   add ecx, [sym]
955
	   mov [strings], ecx
933
	   mov [strings], ecx
956
 
934
 
957
	   lea ebx, [exports]
935
	   lea ebx, [exports]
958
	   mov dword [ebx], kernel_export
936
	   mov dword [ebx], kernel_export
959
	   mov dword [ebx+4], 0
937
	   mov dword [ebx+4], 0
960
	   lea eax, [edx+20]
938
	   lea eax, [edx+20]
961
 
939
 
962
	   stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\
940
	   stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\
963
				     [strings], ebx
941
				     [strings], ebx
964
	   test eax, eax
942
	   test eax, eax
965
	   jz .link_fail
943
	   jz .link_fail
966
 
944
 
967
	   mov ebx, [coff]
945
	   mov ebx, [coff]
968
	   stdcall fix_coff_relocs, ebx, [sym], 0
946
	   stdcall fix_coff_relocs, ebx, [sym], 0
969
 
947
 
970
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szVersion
948
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szVersion
971
	   test eax, eax
949
	   test eax, eax
972
	   jz .link_fail
950
	   jz .link_fail
973
 
951
 
974
	   mov eax, [eax]
952
	   mov eax, [eax]
975
	   shr eax, 16
953
	   shr eax, 16
976
	   cmp eax, DRV_COMPAT
954
	   cmp eax, DRV_COMPAT
977
	   jb .ver_fail
955
	   jb .ver_fail
978
 
956
 
979
	   cmp eax, DRV_CURRENT
957
	   cmp eax, DRV_CURRENT
980
	   ja .ver_fail
958
	   ja .ver_fail
981
 
959
 
982
	   mov ebx, [coff]
960
	   mov ebx, [coff]
983
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szSTART
961
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szSTART
984
	   mov [start], eax
962
	   mov [start], eax
985
 
963
 
986
	   stdcall kernel_free, [coff]
964
	   stdcall kernel_free, [coff]
987
 
965
 
988
	   mov ebx, [start]
966
	   mov ebx, [start]
989
	   stdcall ebx, DRV_ENTRY
967
	   stdcall ebx, DRV_ENTRY
990
	   test eax, eax
968
	   test eax, eax
991
	   jnz .ok
969
	   jnz .ok
992
 
970
 
993
	   stdcall kernel_free, [img_base]
971
	   stdcall kernel_free, [img_base]
994
           cmp     dword [file_name+13], 'SOUN'
972
           cmp     dword [file_name+13], 'SOUN'
995
           jnz     @f
973
           jnz     @f
996
           cmp     dword [file_name+17], 'D.ob'
974
           cmp     dword [file_name+17], 'D.ob'
997
           jnz     @f
975
           jnz     @f
998
           cmp     word [file_name+21], 'j'
976
           cmp     word [file_name+21], 'j'
999
           jnz     @f
977
           jnz     @f
1000
           mov     esi, aSis
978
           mov     esi, aSis
1001
           jmp     .redo
979
           jmp     .redo
1002
@@:
980
@@:
1003
	   xor eax, eax
981
	   xor eax, eax
1004
	   ret
982
	   ret
1005
.ok:
983
.ok:
1006
	   mov ebx, [img_base]
984
	   mov ebx, [img_base]
1007
	   mov [eax+SRV.base], ebx
985
	   mov [eax+SRV.base], ebx
1008
	   mov ecx, [start]
986
	   mov ecx, [start]
1009
	   mov [eax+SRV.entry], ecx
987
	   mov [eax+SRV.entry], ecx
1010
	   ret
988
	   ret
1011
 
989
 
1012
.ver_fail:
990
.ver_fail:
1013
	   mov esi, msg_CR
991
	   mov esi, msg_CR
1014
	   call sys_msg_board_str
992
	   call sys_msg_board_str
1015
	   mov esi, [driver_name]
993
	   mov esi, [driver_name]
1016
	   call sys_msg_board_str
994
	   call sys_msg_board_str
1017
	   mov esi, msg_CR
995
	   mov esi, msg_CR
1018
	   call sys_msg_board_str
996
	   call sys_msg_board_str
1019
	   mov esi, msg_version
997
	   mov esi, msg_version
1020
	   call sys_msg_board_str
998
	   call sys_msg_board_str
1021
	   mov esi, msg_www
999
	   mov esi, msg_www
1022
	   call sys_msg_board_str
1000
	   call sys_msg_board_str
1023
	   jmp .cleanup
1001
	   jmp .cleanup
1024
 
1002
 
1025
.link_fail:
1003
.link_fail:
1026
	   mov esi, msg_module
1004
	   mov esi, msg_module
1027
	   call sys_msg_board_str
1005
	   call sys_msg_board_str
1028
	   mov esi, [driver_name]
1006
	   mov esi, [driver_name]
1029
	   call sys_msg_board_str
1007
	   call sys_msg_board_str
1030
	   mov esi, msg_CR
1008
	   mov esi, msg_CR
1031
	   call sys_msg_board_str
1009
	   call sys_msg_board_str
1032
.cleanup:
1010
.cleanup:
1033
	   stdcall kernel_free,[img_base]
1011
	   stdcall kernel_free,[img_base]
1034
.fail:
1012
.fail:
1035
	   stdcall kernel_free, [coff]
1013
	   stdcall kernel_free, [coff]
1036
.exit:
1014
.exit:
1037
	   xor eax, eax
1015
	   xor eax, eax
1038
	   ret
1016
	   ret
1039
endp
1017
endp
1040
 
1018
 
1041
; in: edx -> COFF_SECTION struct
1019
; in: edx -> COFF_SECTION struct
1042
; out: eax = alignment as mask for bits to drop
1020
; out: eax = alignment as mask for bits to drop
1043
coff_get_align:
1021
coff_get_align:
1044
; Rules:
1022
; Rules:
1045
; - if alignment is not given, use default = 4K;
1023
; - if alignment is not given, use default = 4K;
1046
; - if alignment is given and is no more than 4K, use it;
1024
; - if alignment is given and is no more than 4K, use it;
1047
; - if alignment is more than 4K, revert to 4K.
1025
; - if alignment is more than 4K, revert to 4K.
1048
	push	ecx
1026
	push	ecx
1049
	mov	cl, byte [edx+CFS.Characteristics+2]
1027
	mov	cl, byte [edx+CFS.Characteristics+2]
1050
	mov	eax, 1
1028
	mov	eax, 1
1051
	shr	cl, 4
1029
	shr	cl, 4
1052
	dec	cl
1030
	dec	cl
1053
	js	.default
1031
	js	.default
1054
	cmp	cl, 12
1032
	cmp	cl, 12
1055
	jbe	@f
1033
	jbe	@f
1056
.default:
1034
.default:
1057
	mov	cl, 12
1035
	mov	cl, 12
1058
@@:
1036
@@:
1059
	shl	eax, cl
1037
	shl	eax, cl
1060
	pop	ecx
1038
	pop	ecx
1061
	dec	eax
1039
	dec	eax
1062
	ret
1040
	ret
1063
 
1041
 
1064
align 4
1042
align 4
1065
proc load_library stdcall, file_name:dword
1043
proc load_library stdcall, file_name:dword
1066
	   locals
1044
	   locals
1067
	     fullname  rb 260
1045
	     fullname  rb 260
1068
	     fileinfo  rb 40
1046
	     fileinfo  rb 40
1069
	     coff      dd ?
1047
	     coff      dd ?
1070
	     img_base  dd ?
1048
	     img_base  dd ?
1071
	   endl
1049
	   endl
1072
 
1050
 
1073
	   cli
1051
	   cli
1074
 
1052
 
1075
; resolve file name
1053
; resolve file name
1076
	   mov ebx, [file_name]
1054
	   mov ebx, [file_name]
1077
	   lea edi, [fullname+1]
1055
	   lea edi, [fullname+1]
1078
	   mov byte [edi-1], '/'
1056
	   mov byte [edi-1], '/'
1079
	   stdcall get_full_file_name, edi, 259
1057
	   stdcall get_full_file_name, edi, 259
1080
	   test al, al
1058
	   test al, al
1081
	   jz .fail
1059
	   jz .fail
1082
 
1060
 
1083
; scan for required DLL in list of already loaded for this process,
1061
; scan for required DLL in list of already loaded for this process,
1084
; ignore timestamp
1062
; ignore timestamp
1085
	   mov esi, [CURRENT_TASK]
1063
	   mov esi, [CURRENT_TASK]
1086
	   shl esi, 8
1064
	   shl esi, 8
1087
	   lea edi, [fullname]
1065
	   lea edi, [fullname]
1088
	   mov ebx, [esi+SLOT_BASE+APPDATA.dlls_list_ptr]
1066
	   mov ebx, [esi+SLOT_BASE+APPDATA.dlls_list_ptr]
1089
	   test ebx, ebx
1067
	   test ebx, ebx
1090
	   jz  .not_in_process
1068
	   jz  .not_in_process
1091
	   mov esi, [ebx+HDLL.fd]
1069
	   mov esi, [ebx+HDLL.fd]
1092
.scan_in_process:
1070
.scan_in_process:
1093
	   cmp esi, ebx
1071
	   cmp esi, ebx
1094
	   jz .not_in_process
1072
	   jz .not_in_process
1095
	   mov eax, [esi+HDLL.parent]
1073
	   mov eax, [esi+HDLL.parent]
1096
	   add eax, DLLDESCR.name
1074
	   add eax, DLLDESCR.name
1097
	   stdcall strncmp, eax, edi, -1
1075
	   stdcall strncmp, eax, edi, -1
1098
	   test eax, eax
1076
	   test eax, eax
1099
	   jnz .next_in_process
1077
	   jnz .next_in_process
1100
; simple variant: load DLL which is already loaded in this process
1078
; simple variant: load DLL which is already loaded in this process
1101
; just increment reference counters and return address of exports table
1079
; just increment reference counters and return address of exports table
1102
	   inc [esi+HDLL.refcount]
1080
	   inc [esi+HDLL.refcount]
1103
	   mov ecx, [esi+HDLL.parent]
1081
	   mov ecx, [esi+HDLL.parent]
1104
	   inc [ecx+DLLDESCR.refcount]
1082
	   inc [ecx+DLLDESCR.refcount]
1105
	   mov eax, [ecx+DLLDESCR.exports]
1083
	   mov eax, [ecx+DLLDESCR.exports]
1106
	   sub eax, [ecx+DLLDESCR.defaultbase]
1084
	   sub eax, [ecx+DLLDESCR.defaultbase]
1107
	   add eax, [esi+HDLL.base]
1085
	   add eax, [esi+HDLL.base]
1108
	   ret
1086
	   ret
1109
.next_in_process:
1087
.next_in_process:
1110
	   mov esi, [esi+HDLL.fd]
1088
	   mov esi, [esi+HDLL.fd]
1111
	   jmp .scan_in_process
1089
	   jmp .scan_in_process
1112
.not_in_process:
1090
.not_in_process:
1113
 
1091
 
1114
; scan in full list, compare timestamp
1092
; scan in full list, compare timestamp
1115
	   lea eax, [fileinfo]
1093
	   lea eax, [fileinfo]
1116
	   stdcall get_fileinfo, edi, eax
1094
	   stdcall get_fileinfo, edi, eax
1117
	   test eax, eax
1095
	   test eax, eax
1118
	   jnz .fail
1096
	   jnz .fail
1119
	   mov esi, [dll_list.fd]
1097
	   mov esi, [dll_list.fd]
1120
.scan_for_dlls:
1098
.scan_for_dlls:
1121
	   cmp esi, dll_list
1099
	   cmp esi, dll_list
1122
	   jz .load_new
1100
	   jz .load_new
1123
	   lea eax, [esi+DLLDESCR.name]
1101
	   lea eax, [esi+DLLDESCR.name]
1124
	   stdcall strncmp, eax, edi, -1
1102
	   stdcall strncmp, eax, edi, -1
1125
	   test eax, eax
1103
	   test eax, eax
1126
	   jnz .continue_scan
1104
	   jnz .continue_scan
1127
.test_prev_dll:
1105
.test_prev_dll:
1128
	   mov eax, dword [fileinfo+24]	; last modified time
1106
	   mov eax, dword [fileinfo+24]	; last modified time
1129
	   mov edx, dword [fileinfo+28]	; last modified date
1107
	   mov edx, dword [fileinfo+28]	; last modified date
1130
	   cmp dword [esi+DLLDESCR.timestamp], eax
1108
	   cmp dword [esi+DLLDESCR.timestamp], eax
1131
	   jnz .continue_scan
1109
	   jnz .continue_scan
1132
	   cmp dword [esi+DLLDESCR.timestamp+4], edx
1110
	   cmp dword [esi+DLLDESCR.timestamp+4], edx
1133
	   jz .dll_already_loaded
1111
	   jz .dll_already_loaded
1134
.continue_scan:
1112
.continue_scan:
1135
	   mov esi, [esi+DLLDESCR.fd]
1113
	   mov esi, [esi+DLLDESCR.fd]
1136
	   jmp .scan_for_dlls
1114
	   jmp .scan_for_dlls
1137
 
1115
 
1138
; new DLL
1116
; new DLL
1139
.load_new:
1117
.load_new:
1140
; load file
1118
; load file
1141
	   stdcall load_file, edi
1119
	   stdcall load_file, edi
1142
	   test eax, eax
1120
	   test eax, eax
1143
	   jz .fail
1121
	   jz .fail
1144
	   mov [coff], eax
1122
	   mov [coff], eax
1145
	   mov dword [fileinfo+32], ebx
1123
	   mov dword [fileinfo+32], ebx
1146
 
1124
 
1147
; allocate DLLDESCR struct; size is DLLDESCR.sizeof plus size of DLL name
1125
; allocate DLLDESCR struct; size is DLLDESCR.sizeof plus size of DLL name
1148
	   mov esi, edi
1126
	   mov esi, edi
1149
	   mov ecx, -1
1127
	   mov ecx, -1
1150
	   xor eax, eax
1128
	   xor eax, eax
1151
	   repnz scasb
1129
	   repnz scasb
1152
	   not ecx
1130
	   not ecx
1153
	   lea eax, [ecx+DLLDESCR.sizeof]
1131
	   lea eax, [ecx+DLLDESCR.sizeof]
1154
	   push ecx
1132
	   push ecx
1155
	   call malloc
1133
	   call malloc
1156
	   pop ecx
1134
	   pop ecx
1157
	   test eax, eax
1135
	   test eax, eax
1158
	   jz .fail_and_free_coff
1136
	   jz .fail_and_free_coff
1159
; save timestamp
1137
; save timestamp
1160
	   lea edi, [eax+DLLDESCR.name]
1138
	   lea edi, [eax+DLLDESCR.name]
1161
	   rep movsb
1139
	   rep movsb
1162
	   mov esi, eax
1140
	   mov esi, eax
1163
	   mov eax, dword [fileinfo+24]
1141
	   mov eax, dword [fileinfo+24]
1164
	   mov dword [esi+DLLDESCR.timestamp], eax
1142
	   mov dword [esi+DLLDESCR.timestamp], eax
1165
	   mov eax, dword [fileinfo+28]
1143
	   mov eax, dword [fileinfo+28]
1166
	   mov dword [esi+DLLDESCR.timestamp+4], eax
1144
	   mov dword [esi+DLLDESCR.timestamp+4], eax
1167
; initialize DLLDESCR struct
1145
; initialize DLLDESCR struct
1168
	   and dword [esi+DLLDESCR.refcount], 0 ; no HDLLs yet; later it will be incremented
1146
	   and dword [esi+DLLDESCR.refcount], 0 ; no HDLLs yet; later it will be incremented
1169
	   mov [esi+DLLDESCR.fd], dll_list
1147
	   mov [esi+DLLDESCR.fd], dll_list
1170
	   mov eax, [dll_list.bk]
1148
	   mov eax, [dll_list.bk]
1171
	   mov [dll_list.bk], esi
1149
	   mov [dll_list.bk], esi
1172
	   mov [esi+DLLDESCR.bk], eax
1150
	   mov [esi+DLLDESCR.bk], eax
1173
	   mov [eax+DLLDESCR.fd], esi
1151
	   mov [eax+DLLDESCR.fd], esi
1174
 
1152
 
1175
; calculate size of loaded DLL
1153
; calculate size of loaded DLL
1176
	   mov edx, [coff]
1154
	   mov edx, [coff]
1177
	   movzx ecx, [edx+CFH.nSections]
1155
	   movzx ecx, [edx+CFH.nSections]
1178
	   xor ebx, ebx
1156
	   xor ebx, ebx
1179
 
1157
 
1180
	   add edx, 20
1158
	   add edx, 20
1181
@@:
1159
@@:
1182
	   call coff_get_align
1160
	   call coff_get_align
1183
	   add ebx, eax
1161
	   add ebx, eax
1184
	   not eax
1162
	   not eax
1185
	   and ebx, eax
1163
	   and ebx, eax
1186
	   add ebx, [edx+CFS.SizeOfRawData]
1164
	   add ebx, [edx+CFS.SizeOfRawData]
1187
	   add edx, COFF_SECTION_SIZE
1165
	   add edx, COFF_SECTION_SIZE
1188
	   dec ecx
1166
	   dec ecx
1189
	   jnz @B
1167
	   jnz @B
1190
; it must be nonzero and not too big
1168
; it must be nonzero and not too big
1191
	   mov [esi+DLLDESCR.size], ebx
1169
	   mov [esi+DLLDESCR.size], ebx
1192
	   test ebx, ebx
1170
	   test ebx, ebx
1193
	   jz .fail_and_free_dll
1171
	   jz .fail_and_free_dll
1194
	   cmp ebx, MAX_DEFAULT_DLL_ADDR-MIN_DEFAULT_DLL_ADDR
1172
	   cmp ebx, MAX_DEFAULT_DLL_ADDR-MIN_DEFAULT_DLL_ADDR
1195
	   ja .fail_and_free_dll
1173
	   ja .fail_and_free_dll
1196
; allocate memory for kernel-side image
1174
; allocate memory for kernel-side image
1197
	   stdcall kernel_alloc, ebx
1175
	   stdcall kernel_alloc, ebx
1198
	   test eax, eax
1176
	   test eax, eax
1199
	   jz .fail_and_free_dll
1177
	   jz .fail_and_free_dll
1200
	   mov [esi+DLLDESCR.data], eax
1178
	   mov [esi+DLLDESCR.data], eax
1201
; calculate preferred base address
1179
; calculate preferred base address
1202
	   add ebx, 0x1FFF
1180
	   add ebx, 0x1FFF
1203
	   and ebx, not 0xFFF
1181
	   and ebx, not 0xFFF
1204
	   mov ecx, [dll_cur_addr]
1182
	   mov ecx, [dll_cur_addr]
1205
	   lea edx, [ecx+ebx]
1183
	   lea edx, [ecx+ebx]
1206
	   cmp edx, MAX_DEFAULT_DLL_ADDR
1184
	   cmp edx, MAX_DEFAULT_DLL_ADDR
1207
	   jb @f
1185
	   jb @f
1208
	   mov ecx, MIN_DEFAULT_DLL_ADDR
1186
	   mov ecx, MIN_DEFAULT_DLL_ADDR
1209
	   lea edx, [ecx+ebx]
1187
	   lea edx, [ecx+ebx]
1210
@@:
1188
@@:
1211
	   mov [esi+DLLDESCR.defaultbase], ecx
1189
	   mov [esi+DLLDESCR.defaultbase], ecx
1212
	   mov [dll_cur_addr], edx
1190
	   mov [dll_cur_addr], edx
1213
 
1191
 
1214
; copy sections and set correct values for VirtualAddress'es in headers
1192
; copy sections and set correct values for VirtualAddress'es in headers
1215
	   push esi
1193
	   push esi
1216
	   mov edx, [coff]
1194
	   mov edx, [coff]
1217
	   movzx ebx, [edx+CFH.nSections]
1195
	   movzx ebx, [edx+CFH.nSections]
1218
	   mov edi, eax
1196
	   mov edi, eax
1219
	   add edx, 20
1197
	   add edx, 20
1220
	   cld
1198
	   cld
1221
@@:
1199
@@:
1222
	   call coff_get_align
1200
	   call coff_get_align
1223
	   add ecx, eax
1201
	   add ecx, eax
1224
	   add edi, eax
1202
	   add edi, eax
1225
	   not eax
1203
	   not eax
1226
	   and ecx, eax
1204
	   and ecx, eax
1227
	   and edi, eax
1205
	   and edi, eax
1228
	   mov [edx+CFS.VirtualAddress], ecx
1206
	   mov [edx+CFS.VirtualAddress], ecx
1229
	   add ecx, [edx+CFS.SizeOfRawData]
1207
	   add ecx, [edx+CFS.SizeOfRawData]
1230
	   mov esi, [edx+CFS.PtrRawData]
1208
	   mov esi, [edx+CFS.PtrRawData]
1231
	   push ecx
1209
	   push ecx
1232
	   mov ecx, [edx+CFS.SizeOfRawData]
1210
	   mov ecx, [edx+CFS.SizeOfRawData]
1233
	   test esi, esi
1211
	   test esi, esi
1234
	   jnz .copy
1212
	   jnz .copy
1235
	   xor eax, eax
1213
	   xor eax, eax
1236
	   rep stosb
1214
	   rep stosb
1237
	   jmp .next
1215
	   jmp .next
1238
.copy:
1216
.copy:
1239
	   add esi, [coff]
1217
	   add esi, [coff]
1240
	   rep movsb
1218
	   rep movsb
1241
.next:
1219
.next:
1242
           pop ecx
1220
           pop ecx
1243
	   add edx, COFF_SECTION_SIZE
1221
	   add edx, COFF_SECTION_SIZE
1244
	   dec ebx
1222
	   dec ebx
1245
	   jnz @B
1223
	   jnz @B
1246
	   pop esi
1224
	   pop esi
1247
 
1225
 
1248
; save some additional data from COFF file
1226
; save some additional data from COFF file
1249
; later we will use COFF header, headers for sections and symbol table
1227
; later we will use COFF header, headers for sections and symbol table
1250
; and also relocations table for all sections
1228
; and also relocations table for all sections
1251
	   mov edx, [coff]
1229
	   mov edx, [coff]
1252
	   mov ebx, [edx+CFH.pSymTable]
1230
	   mov ebx, [edx+CFH.pSymTable]
1253
	   mov edi, dword [fileinfo+32]
1231
	   mov edi, dword [fileinfo+32]
1254
	   sub edi, ebx
1232
	   sub edi, ebx
1255
	   jc .fail_and_free_data
1233
	   jc .fail_and_free_data
1256
	   mov [esi+DLLDESCR.symbols_lim], edi
1234
	   mov [esi+DLLDESCR.symbols_lim], edi
1257
	   add ebx, edx
1235
	   add ebx, edx
1258
	   movzx ecx, [edx+CFH.nSections]
1236
	   movzx ecx, [edx+CFH.nSections]
1259
	   lea ecx, [ecx*5]
1237
	   lea ecx, [ecx*5]
1260
	   lea edi, [edi+ecx*8+20]
1238
	   lea edi, [edi+ecx*8+20]
1261
	   add edx, 20
1239
	   add edx, 20
1262
@@:
1240
@@:
1263
	   movzx eax, [edx+CFS.NumReloc]
1241
	   movzx eax, [edx+CFS.NumReloc]
1264
	   lea eax, [eax*5]
1242
	   lea eax, [eax*5]
1265
	   lea edi, [edi+eax*2]
1243
	   lea edi, [edi+eax*2]
1266
	   add edx, COFF_SECTION_SIZE
1244
	   add edx, COFF_SECTION_SIZE
1267
	   sub ecx, 5
1245
	   sub ecx, 5
1268
	   jnz @b
1246
	   jnz @b
1269
	   stdcall kernel_alloc, edi
1247
	   stdcall kernel_alloc, edi
1270
	   test eax, eax
1248
	   test eax, eax
1271
	   jz  .fail_and_free_data
1249
	   jz  .fail_and_free_data
1272
	   mov edx, [coff]
1250
	   mov edx, [coff]
1273
	   movzx ecx, [edx+CFH.nSections]
1251
	   movzx ecx, [edx+CFH.nSections]
1274
	   lea ecx, [ecx*5]
1252
	   lea ecx, [ecx*5]
1275
	   lea ecx, [ecx*2+5]
1253
	   lea ecx, [ecx*2+5]
1276
	   mov [esi+DLLDESCR.coff_hdr], eax
1254
	   mov [esi+DLLDESCR.coff_hdr], eax
1277
	   push esi
1255
	   push esi
1278
	   mov esi, edx
1256
	   mov esi, edx
1279
	   mov edi, eax
1257
	   mov edi, eax
1280
	   rep movsd
1258
	   rep movsd
1281
	   pop esi
1259
	   pop esi
1282
	   mov [esi+DLLDESCR.symbols_ptr], edi
1260
	   mov [esi+DLLDESCR.symbols_ptr], edi
1283
	   push esi
1261
	   push esi
1284
	   mov ecx, [edx+CFH.nSymbols]
1262
	   mov ecx, [edx+CFH.nSymbols]
1285
	   mov [esi+DLLDESCR.symbols_num], ecx
1263
	   mov [esi+DLLDESCR.symbols_num], ecx
1286
	   mov ecx, [esi+DLLDESCR.symbols_lim]
1264
	   mov ecx, [esi+DLLDESCR.symbols_lim]
1287
	   mov esi, ebx
1265
	   mov esi, ebx
1288
	   rep movsb
1266
	   rep movsb
1289
	   pop esi
1267
	   pop esi
1290
	   mov ebx, [esi+DLLDESCR.coff_hdr]
1268
	   mov ebx, [esi+DLLDESCR.coff_hdr]
1291
	   push esi
1269
	   push esi
1292
	   movzx eax, [edx+CFH.nSections]
1270
	   movzx eax, [edx+CFH.nSections]
1293
	   lea edx, [ebx+20]
1271
	   lea edx, [ebx+20]
1294
@@:
1272
@@:
1295
           movzx ecx, [edx+CFS.NumReloc]
1273
           movzx ecx, [edx+CFS.NumReloc]
1296
           lea ecx, [ecx*5]
1274
           lea ecx, [ecx*5]
1297
           mov esi, [edx+CFS.PtrReloc]
1275
           mov esi, [edx+CFS.PtrReloc]
1298
           mov [edx+CFS.PtrReloc], edi
1276
           mov [edx+CFS.PtrReloc], edi
1299
           sub [edx+CFS.PtrReloc], ebx
1277
           sub [edx+CFS.PtrReloc], ebx
1300
           add esi, [coff]
1278
           add esi, [coff]
1301
           shr ecx, 1
1279
           shr ecx, 1
1302
           rep movsd
1280
           rep movsd
1303
           adc ecx, ecx
1281
           adc ecx, ecx
1304
           rep movsw
1282
           rep movsw
1305
           add edx, COFF_SECTION_SIZE
1283
           add edx, COFF_SECTION_SIZE
1306
           dec eax
1284
           dec eax
1307
           jnz @b
1285
           jnz @b
1308
	   pop esi
1286
	   pop esi
1309
 
1287
 
1310
; fixup symbols
1288
; fixup symbols
1311
	   mov edx, ebx
1289
	   mov edx, ebx
1312
	   mov eax, [ebx+CFH.nSymbols]
1290
	   mov eax, [ebx+CFH.nSymbols]
1313
	   add edx, 20
1291
	   add edx, 20
1314
	   mov ecx, [esi+DLLDESCR.symbols_num]
1292
	   mov ecx, [esi+DLLDESCR.symbols_num]
1315
	   lea ecx, [ecx*9]
1293
	   lea ecx, [ecx*9]
1316
	   add ecx, ecx
1294
	   add ecx, ecx
1317
	   add ecx, [esi+DLLDESCR.symbols_ptr]
1295
	   add ecx, [esi+DLLDESCR.symbols_ptr]
1318
 
1296
 
1319
	   stdcall fix_coff_symbols, edx, [esi+DLLDESCR.symbols_ptr], eax,\
1297
	   stdcall fix_coff_symbols, edx, [esi+DLLDESCR.symbols_ptr], eax,\
1320
				     ecx, 0
1298
				     ecx, 0
1321
;	   test eax, eax
1299
;	   test eax, eax
1322
;	   jnz @F
1300
;	   jnz @F
1323
;
1301
;
1324
;@@:
1302
;@@:
1325
 
1303
 
1326
	   stdcall get_coff_sym,[esi+DLLDESCR.symbols_ptr],[ebx+CFH.nSymbols],szEXPORTS
1304
	   stdcall get_coff_sym,[esi+DLLDESCR.symbols_ptr],[ebx+CFH.nSymbols],szEXPORTS
1327
	   test eax, eax
1305
	   test eax, eax
1328
	   jnz @F
1306
	   jnz @F
1329
 
1307
 
1330
	   stdcall get_coff_sym,[esi+DLLDESCR.symbols_ptr],[ebx+CFH.nSymbols],sz_EXPORTS
1308
	   stdcall get_coff_sym,[esi+DLLDESCR.symbols_ptr],[ebx+CFH.nSymbols],sz_EXPORTS
1331
@@:
1309
@@:
1332
	   mov [esi+DLLDESCR.exports], eax
1310
	   mov [esi+DLLDESCR.exports], eax
1333
 
1311
 
1334
; fix relocs in the hidden copy in kernel memory to default address
1312
; fix relocs in the hidden copy in kernel memory to default address
1335
; it is first fix; usually this will be enough, but second fix
1313
; it is first fix; usually this will be enough, but second fix
1336
; can be necessary if real load address will not equal assumption
1314
; can be necessary if real load address will not equal assumption
1337
	   mov eax, [esi+DLLDESCR.data]
1315
	   mov eax, [esi+DLLDESCR.data]
1338
	   sub eax, [esi+DLLDESCR.defaultbase]
1316
	   sub eax, [esi+DLLDESCR.defaultbase]
1339
	   stdcall fix_coff_relocs, ebx, [esi+DLLDESCR.symbols_ptr], eax
1317
	   stdcall fix_coff_relocs, ebx, [esi+DLLDESCR.symbols_ptr], eax
1340
 
1318
 
1341
	   stdcall kernel_free, [coff]
1319
	   stdcall kernel_free, [coff]
1342
 
1320
 
1343
.dll_already_loaded:
1321
.dll_already_loaded:
1344
	   inc [esi+DLLDESCR.refcount]
1322
	   inc [esi+DLLDESCR.refcount]
1345
	   push esi
1323
	   push esi
1346
	   call init_heap
1324
	   call init_heap
1347
	   pop  esi
1325
	   pop  esi
1348
 
1326
 
1349
	   mov edi, [esi+DLLDESCR.size]
1327
	   mov edi, [esi+DLLDESCR.size]
1350
	   stdcall user_alloc_at, [esi+DLLDESCR.defaultbase], edi
1328
	   stdcall user_alloc_at, [esi+DLLDESCR.defaultbase], edi
1351
	   test eax, eax
1329
	   test eax, eax
1352
	   jnz @f
1330
	   jnz @f
1353
	   stdcall user_alloc, edi
1331
	   stdcall user_alloc, edi
1354
	   test eax, eax
1332
	   test eax, eax
1355
	   jz  .fail_and_dereference
1333
	   jz  .fail_and_dereference
1356
@@:
1334
@@:
1357
	   mov [img_base], eax
1335
	   mov [img_base], eax
1358
	   mov eax, HDLL.sizeof
1336
	   mov eax, HDLL.sizeof
1359
	   call malloc
1337
	   call malloc
1360
	   test eax, eax
1338
	   test eax, eax
1361
	   jz  .fail_and_free_user
1339
	   jz  .fail_and_free_user
1362
	   mov ebx, [CURRENT_TASK]
1340
	   mov ebx, [CURRENT_TASK]
1363
	   shl ebx, 5
1341
	   shl ebx, 5
1364
	   mov edx, [CURRENT_TASK+ebx+TASKDATA.pid]
1342
	   mov edx, [CURRENT_TASK+ebx+TASKDATA.pid]
1365
	   mov [eax+HDLL.pid], edx
1343
	   mov [eax+HDLL.pid], edx
1366
	   push eax
1344
	   push eax
1367
	   call init_dlls_in_thread
1345
	   call init_dlls_in_thread
1368
	   pop  ebx
1346
	   pop  ebx
1369
	   test eax, eax
1347
	   test eax, eax
1370
	   jz  .fail_and_free_user
1348
	   jz  .fail_and_free_user
1371
	   mov edx, [eax+HDLL.fd]
1349
	   mov edx, [eax+HDLL.fd]
1372
	   mov [ebx+HDLL.fd], edx
1350
	   mov [ebx+HDLL.fd], edx
1373
	   mov [ebx+HDLL.bk], eax
1351
	   mov [ebx+HDLL.bk], eax
1374
	   mov [eax+HDLL.fd], ebx
1352
	   mov [eax+HDLL.fd], ebx
1375
	   mov [edx+HDLL.bk], ebx
1353
	   mov [edx+HDLL.bk], ebx
1376
	   mov eax, ebx
1354
	   mov eax, ebx
1377
	   mov ebx, [img_base]
1355
	   mov ebx, [img_base]
1378
	   mov [eax+HDLL.base], ebx
1356
	   mov [eax+HDLL.base], ebx
1379
	   mov [eax+HDLL.size], edi
1357
	   mov [eax+HDLL.size], edi
1380
	   mov [eax+HDLL.refcount], 1
1358
	   mov [eax+HDLL.refcount], 1
1381
	   mov [eax+HDLL.parent], esi
1359
	   mov [eax+HDLL.parent], esi
1382
	   mov edx, ebx
1360
	   mov edx, ebx
1383
	   shr edx, 12
1361
	   shr edx, 12
1384
	   or dword [page_tabs+(edx-1)*4], DONT_FREE_BLOCK
1362
	   or dword [page_tabs+(edx-1)*4], DONT_FREE_BLOCK
1385
; copy entries of page table from kernel-side image to usermode
1363
; copy entries of page table from kernel-side image to usermode
1386
; use copy-on-write for user-mode image, so map as readonly
1364
; use copy-on-write for user-mode image, so map as readonly
1387
	   xor edi, edi
1365
	   xor edi, edi
1388
	   mov ecx, [esi+DLLDESCR.data]
1366
	   mov ecx, [esi+DLLDESCR.data]
1389
	   shr ecx, 12
1367
	   shr ecx, 12
1390
.map_pages_loop:
1368
.map_pages_loop:
1391
	   mov eax, [page_tabs+ecx*4]
1369
	   mov eax, [page_tabs+ecx*4]
1392
	   and eax, not 0xFFF
1370
	   and eax, not 0xFFF
1393
	   or al, PG_USER
1371
	   or al, PG_USER
1394
	   xchg eax, [page_tabs+edx*4]
1372
	   xchg eax, [page_tabs+edx*4]
1395
	   test al, 1
1373
	   test al, 1
1396
	   jz @f
1374
	   jz @f
1397
	   call free_page
1375
	   call free_page
1398
@@:
1376
@@:
1399
	   invlpg [ebx+edi]
1377
	   invlpg [ebx+edi]
1400
	   inc ecx
1378
	   inc ecx
1401
	   inc edx
1379
	   inc edx
1402
	   add edi, 0x1000
1380
	   add edi, 0x1000
1403
	   cmp edi, [esi+DLLDESCR.size]
1381
	   cmp edi, [esi+DLLDESCR.size]
1404
	   jb .map_pages_loop
1382
	   jb .map_pages_loop
1405
 
1383
 
1406
; if real user-mode base is not equal to preferred base, relocate image
1384
; if real user-mode base is not equal to preferred base, relocate image
1407
	   sub ebx, [esi+DLLDESCR.defaultbase]
1385
	   sub ebx, [esi+DLLDESCR.defaultbase]
1408
	   jz @f
1386
	   jz @f
1409
	   stdcall rebase_coff, [esi+DLLDESCR.coff_hdr], [esi+DLLDESCR.symbols_ptr], ebx
1387
	   stdcall rebase_coff, [esi+DLLDESCR.coff_hdr], [esi+DLLDESCR.symbols_ptr], ebx
1410
@@:
1388
@@:
1411
 
1389
 
1412
	   mov eax, [esi+DLLDESCR.exports]
1390
	   mov eax, [esi+DLLDESCR.exports]
1413
	   sub eax, [esi+DLLDESCR.defaultbase]
1391
	   sub eax, [esi+DLLDESCR.defaultbase]
1414
	   add eax, [img_base]
1392
	   add eax, [img_base]
1415
	   ret
1393
	   ret
1416
.fail_and_free_data:
1394
.fail_and_free_data:
1417
	   stdcall kernel_free, [esi+DLLDESCR.data]
1395
	   stdcall kernel_free, [esi+DLLDESCR.data]
1418
.fail_and_free_dll:
1396
.fail_and_free_dll:
1419
	   mov eax, esi
1397
	   mov eax, esi
1420
	   call free
1398
	   call free
1421
.fail_and_free_coff:
1399
.fail_and_free_coff:
1422
	   stdcall kernel_free, [coff]
1400
	   stdcall kernel_free, [coff]
1423
.fail:
1401
.fail:
1424
	   xor eax, eax
1402
	   xor eax, eax
1425
	   ret
1403
	   ret
1426
.fail_and_free_user:
1404
.fail_and_free_user:
1427
	   stdcall user_free, [img_base]
1405
	   stdcall user_free, [img_base]
1428
.fail_and_dereference:
1406
.fail_and_dereference:
1429
	   mov eax, 1	; delete 1 reference
1407
	   mov eax, 1	; delete 1 reference
1430
	   call dereference_dll
1408
	   call dereference_dll
1431
	   xor eax, eax
1409
	   xor eax, eax
1432
	   ret
1410
	   ret
1433
endp
1411
endp
1434
 
1412
 
1435
; initialize [APPDATA.dlls_list_ptr] for given thread
1413
; initialize [APPDATA.dlls_list_ptr] for given thread
1436
; DLL is per-process object, so APPDATA.dlls_list_ptr must be
1414
; DLL is per-process object, so APPDATA.dlls_list_ptr must be
1437
; kept in sync for all threads of one process.
1415
; kept in sync for all threads of one process.
1438
; out: eax = APPDATA.dlls_list_ptr if all is OK,
1416
; out: eax = APPDATA.dlls_list_ptr if all is OK,
1439
; NULL if memory allocation failed
1417
; NULL if memory allocation failed
1440
init_dlls_in_thread:
1418
init_dlls_in_thread:
1441
	mov	ebx, [current_slot]
1419
	mov	ebx, [current_slot]
1442
	mov	eax, [ebx+APPDATA.dlls_list_ptr]
1420
	mov	eax, [ebx+APPDATA.dlls_list_ptr]
1443
	test	eax, eax
1421
	test	eax, eax
1444
	jnz	.ret
1422
	jnz	.ret
1445
	push	[ebx+APPDATA.dir_table]
1423
	push	[ebx+APPDATA.dir_table]
1446
	mov	eax, 8
1424
	mov	eax, 8
1447
	call	malloc
1425
	call	malloc
1448
	pop	edx
1426
	pop	edx
1449
	test	eax, eax
1427
	test	eax, eax
1450
	jz	.ret
1428
	jz	.ret
1451
	mov	[eax], eax
1429
	mov	[eax], eax
1452
	mov	[eax+4], eax
1430
	mov	[eax+4], eax
1453
	mov	ecx, [TASK_COUNT]
1431
	mov	ecx, [TASK_COUNT]
1454
	mov	ebx, SLOT_BASE+256
1432
	mov	ebx, SLOT_BASE+256
1455
.set:
1433
.set:
1456
	cmp	[ebx+APPDATA.dir_table], edx
1434
	cmp	[ebx+APPDATA.dir_table], edx
1457
	jnz	@f
1435
	jnz	@f
1458
	mov	[ebx+APPDATA.dlls_list_ptr], eax
1436
	mov	[ebx+APPDATA.dlls_list_ptr], eax
1459
@@:
1437
@@:
1460
	add	ebx, 256
1438
	add	ebx, 256
1461
	dec	ecx
1439
	dec	ecx
1462
	jnz	.set
1440
	jnz	.set
1463
.ret:
1441
.ret:
1464
	ret
1442
	ret
1465
 
1443
 
1466
; in: eax = number of references to delete, esi -> DLLDESCR struc
1444
; in: eax = number of references to delete, esi -> DLLDESCR struc
1467
dereference_dll:
1445
dereference_dll:
1468
	sub	[esi+DLLDESCR.refcount], eax
1446
	sub	[esi+DLLDESCR.refcount], eax
1469
	jnz	.ret
1447
	jnz	.ret
1470
	mov	eax, [esi+DLLDESCR.fd]
1448
	mov	eax, [esi+DLLDESCR.fd]
1471
	mov	edx, [esi+DLLDESCR.bk]
1449
	mov	edx, [esi+DLLDESCR.bk]
1472
	mov	[eax+DLLDESCR.bk], edx
1450
	mov	[eax+DLLDESCR.bk], edx
1473
	mov	[edx+DLLDESCR.fd], eax
1451
	mov	[edx+DLLDESCR.fd], eax
1474
	stdcall	kernel_free, [esi+DLLDESCR.coff_hdr]
1452
	stdcall	kernel_free, [esi+DLLDESCR.coff_hdr]
1475
	stdcall	kernel_free, [esi+DLLDESCR.data]
1453
	stdcall	kernel_free, [esi+DLLDESCR.data]
1476
	mov	eax, esi
1454
	mov	eax, esi
1477
	call	free
1455
	call	free
1478
.ret:
1456
.ret:
1479
	ret
1457
	ret
1480
 
1458
 
1481
destroy_hdll:
1459
destroy_hdll:
1482
	push	ebx ecx esi edi
1460
	push	ebx ecx esi edi
1483
	push	eax
1461
	push	eax
1484
	mov	ebx, [eax+HDLL.base]
1462
	mov	ebx, [eax+HDLL.base]
1485
	mov	esi, [eax+HDLL.parent]
1463
	mov	esi, [eax+HDLL.parent]
1486
	mov	edx, [esi+DLLDESCR.size]
1464
	mov	edx, [esi+DLLDESCR.size]
1487
; The following actions require the context of application where HDLL is mapped.
1465
; The following actions require the context of application where HDLL is mapped.
1488
; However, destroy_hdll can be called in the context of OS thread when
1466
; However, destroy_hdll can be called in the context of OS thread when
1489
; cleaning up objects created by the application which is destroyed.
1467
; cleaning up objects created by the application which is destroyed.
1490
; So remember current cr3 and set it to page table of target.
1468
; So remember current cr3 and set it to page table of target.
1491
	mov	eax, [ecx+APPDATA.dir_table]
1469
	mov	eax, [ecx+APPDATA.dir_table]
1492
; Because we cheat with cr3, disable interrupts: task switch would restore
1470
; Because we cheat with cr3, disable interrupts: task switch would restore
1493
; page table from APPDATA of current thread.
1471
; page table from APPDATA of current thread.
1494
; Also set [current_slot] because it is used by user_free.
1472
; Also set [current_slot] because it is used by user_free.
1495
	pushf
1473
	pushf
1496
	cli
1474
	cli
1497
	push	[current_slot]
1475
	push	[current_slot]
1498
	mov	[current_slot], ecx
1476
	mov	[current_slot], ecx
1499
	mov	ecx, cr3
1477
	mov	ecx, cr3
1500
	push	ecx
1478
	push	ecx
1501
	mov	cr3, eax
1479
	mov	cr3, eax
1502
	push	ebx	; argument for user_free
1480
	push	ebx	; argument for user_free
1503
	mov	eax, ebx
1481
	mov	eax, ebx
1504
	shr	ebx, 12
1482
	shr	ebx, 12
1505
	push	ebx
1483
	push	ebx
1506
	mov	esi, [esi+DLLDESCR.data]
1484
	mov	esi, [esi+DLLDESCR.data]
1507
	shr	esi, 12
1485
	shr	esi, 12
1508
.unmap_loop:
1486
.unmap_loop:
1509
	push	eax
1487
	push	eax
1510
	mov	eax, 2
1488
	mov	eax, 2
1511
	xchg	eax, [page_tabs+ebx*4]
1489
	xchg	eax, [page_tabs+ebx*4]
1512
	mov	ecx, [page_tabs+esi*4]
1490
	mov	ecx, [page_tabs+esi*4]
1513
	and	eax, not 0xFFF
1491
	and	eax, not 0xFFF
1514
	and	ecx, not 0xFFF
1492
	and	ecx, not 0xFFF
1515
	cmp	eax, ecx
1493
	cmp	eax, ecx
1516
	jz	@f
1494
	jz	@f
1517
	call	free_page
1495
	call	free_page
1518
@@:
1496
@@:
1519
	pop	eax
1497
	pop	eax
1520
	invlpg	[eax]
1498
	invlpg	[eax]
1521
	add	eax, 0x1000
1499
	add	eax, 0x1000
1522
	inc	ebx
1500
	inc	ebx
1523
	inc	esi
1501
	inc	esi
1524
	sub	edx, 0x1000
1502
	sub	edx, 0x1000
1525
	ja	.unmap_loop
1503
	ja	.unmap_loop
1526
	pop	ebx
1504
	pop	ebx
1527
	and	dword [page_tabs+(ebx-1)*4], not DONT_FREE_BLOCK
1505
	and	dword [page_tabs+(ebx-1)*4], not DONT_FREE_BLOCK
1528
	call	user_free
1506
	call	user_free
1529
; Restore context.
1507
; Restore context.
1530
	pop	eax
1508
	pop	eax
1531
	mov	cr3, eax
1509
	mov	cr3, eax
1532
	pop	[current_slot]
1510
	pop	[current_slot]
1533
	popf
1511
	popf
1534
; Ok, cheating is done.
1512
; Ok, cheating is done.
1535
	pop	eax
1513
	pop	eax
1536
	push	eax
1514
	push	eax
1537
	mov	esi, [eax+HDLL.parent]
1515
	mov	esi, [eax+HDLL.parent]
1538
	mov	eax, [eax+HDLL.refcount]
1516
	mov	eax, [eax+HDLL.refcount]
1539
	call	dereference_dll
1517
	call	dereference_dll
1540
	pop	eax
1518
	pop	eax
1541
	mov	edx, [eax+HDLL.bk]
1519
	mov	edx, [eax+HDLL.bk]
1542
	mov	ebx, [eax+HDLL.fd]
1520
	mov	ebx, [eax+HDLL.fd]
1543
	mov	[ebx+HDLL.bk], edx
1521
	mov	[ebx+HDLL.bk], edx
1544
	mov	[edx+HDLL.fd], ebx
1522
	mov	[edx+HDLL.fd], ebx
1545
	call	free
1523
	call	free
1546
	pop	edi esi ecx ebx
1524
	pop	edi esi ecx ebx
1547
	ret
1525
	ret
1548
 
1526
 
1549
; ecx -> APPDATA for slot, esi = dlls_list_ptr
1527
; ecx -> APPDATA for slot, esi = dlls_list_ptr
1550
destroy_all_hdlls:
1528
destroy_all_hdlls:
1551
	test	esi, esi
1529
	test	esi, esi
1552
	jz	.ret
1530
	jz	.ret
1553
.loop:
1531
.loop:
1554
	mov	eax, [esi+HDLL.fd]
1532
	mov	eax, [esi+HDLL.fd]
1555
	cmp	eax, esi
1533
	cmp	eax, esi
1556
	jz	free
1534
	jz	free
1557
	call	destroy_hdll
1535
	call	destroy_hdll
1558
	jmp	.loop
1536
	jmp	.loop
1559
.ret:
1537
.ret:
1560
	ret
1538
	ret
1561
 
1539
 
1562
align 4
1540
align 4
1563
stop_all_services:
1541
stop_all_services:
1564
       push ebp
1542
       push ebp
1565
	   mov edx, [srv.fd]
1543
	   mov edx, [srv.fd]
1566
.next:
1544
.next:
1567
	   cmp edx,  srv.fd-SRV_FD_OFFSET
1545
	   cmp edx,  srv.fd-SRV_FD_OFFSET
1568
	   je .done
1546
	   je .done
1569
	   cmp [edx+SRV.magic], ' SRV'
1547
	   cmp [edx+SRV.magic], ' SRV'
1570
	   jne .next
1548
	   jne .next
1571
       cmp [edx+SRV.size], SRV.sizeof
1549
       cmp [edx+SRV.size], SRV.sizeof
1572
	   jne .next
1550
	   jne .next
1573
 
1551
 
1574
	   mov ebx, [edx+SRV.entry]
1552
	   mov ebx, [edx+SRV.entry]
1575
	   mov edx, [edx+SRV.fd]
1553
	   mov edx, [edx+SRV.fd]
1576
	   test ebx, ebx
1554
	   test ebx, ebx
1577
	   jz .next
1555
	   jz .next
1578
 
1556
 
1579
	   push edx
1557
	   push edx
1580
       mov ebp, esp
1558
       mov ebp, esp
1581
       push  0
1559
       push  0
1582
       push -1
1560
       push -1
1583
       call ebx
1561
       call ebx
1584
       mov esp, ebp
1562
       mov esp, ebp
1585
	   pop edx
1563
	   pop edx
1586
	   jmp .next
1564
	   jmp .next
1587
.done:
1565
.done:
1588
       pop ebp
1566
       pop ebp
1589
	   ret
1567
	   ret
1590
 
1568
 
1591
; param
1569
; param
1592
;  eax= size
1570
;  eax= size
1593
;  ebx= pid
1571
;  ebx= pid
1594
 
1572
 
1595
align 4
1573
align 4
1596
create_kernel_object:
1574
create_kernel_object:
1597
 
1575
 
1598
	   push ebx
1576
	   push ebx
1599
	   call malloc
1577
	   call malloc
1600
	   pop ebx
1578
	   pop ebx
1601
	   test eax, eax
1579
	   test eax, eax
1602
	   jz .fail
1580
	   jz .fail
1603
 
1581
 
1604
	   mov ecx,[current_slot]
1582
	   mov ecx,[current_slot]
1605
	   add ecx, APP_OBJ_OFFSET
1583
	   add ecx, APP_OBJ_OFFSET
1606
 
1584
 
1607
	   pushfd
1585
	   pushfd
1608
	   cli
1586
	   cli
1609
	   mov edx, [ecx+APPOBJ.fd]
1587
	   mov edx, [ecx+APPOBJ.fd]
1610
	   mov [eax+APPOBJ.fd], edx
1588
	   mov [eax+APPOBJ.fd], edx
1611
	   mov [eax+APPOBJ.bk], ecx
1589
	   mov [eax+APPOBJ.bk], ecx
1612
	   mov [eax+APPOBJ.pid], ebx
1590
	   mov [eax+APPOBJ.pid], ebx
1613
 
1591
 
1614
	   mov [ecx+APPOBJ.fd], eax
1592
	   mov [ecx+APPOBJ.fd], eax
1615
	   mov [edx+APPOBJ.bk], eax
1593
	   mov [edx+APPOBJ.bk], eax
1616
	   popfd
1594
	   popfd
1617
.fail:
1595
.fail:
1618
	   ret
1596
	   ret
1619
 
1597
 
1620
; param
1598
; param
1621
;  eax= object
1599
;  eax= object
1622
 
1600
 
1623
align 4
1601
align 4
1624
destroy_kernel_object:
1602
destroy_kernel_object:
1625
 
1603
 
1626
	   pushfd
1604
	   pushfd
1627
	   cli
1605
	   cli
1628
	   mov ebx, [eax+APPOBJ.fd]
1606
	   mov ebx, [eax+APPOBJ.fd]
1629
	   mov ecx, [eax+APPOBJ.bk]
1607
	   mov ecx, [eax+APPOBJ.bk]
1630
	   mov [ebx+APPOBJ.bk], ecx
1608
	   mov [ebx+APPOBJ.bk], ecx
1631
	   mov [ecx+APPOBJ.fd], ebx
1609
	   mov [ecx+APPOBJ.fd], ebx
1632
	   popfd
1610
	   popfd
1633
 
1611
 
1634
	   xor edx, edx        ;clear common header
1612
	   xor edx, edx        ;clear common header
1635
	   mov [eax], edx
1613
	   mov [eax], edx
1636
	   mov [eax+4], edx
1614
	   mov [eax+4], edx
1637
	   mov [eax+8], edx
1615
	   mov [eax+8], edx
1638
	   mov [eax+12], edx
1616
	   mov [eax+12], edx
1639
	   mov [eax+16], edx
1617
	   mov [eax+16], edx
1640
 
1618
 
1641
	   call free	       ;release object memory
1619
	   call free	       ;release object memory
1642
	   ret
1620
	   ret
1643
 
1621
 
1644
 
1622
 
1645
 
1623
 
1646
if 0
1624
if 0
1647
 
1625
 
1648
irq:
1626
irq:
1649
 
1627
 
1650
.irq0:
1628
.irq0:
1651
	   pusfd
1629
	   pusfd
1652
	   pushad
1630
	   pushad
1653
	   push IRQ_0
1631
	   push IRQ_0
1654
	   jmp .master
1632
	   jmp .master
1655
.irq_1:
1633
.irq_1:
1656
	   pusfd
1634
	   pusfd
1657
	   pushad
1635
	   pushad
1658
	   push IRQ_1
1636
	   push IRQ_1
1659
	   jmp .master
1637
	   jmp .master
1660
 
1638
 
1661
.master:
1639
.master:
1662
	   mov ax, app_data
1640
	   mov ax, app_data
1663
	   mov ds, eax
1641
	   mov ds, eax
1664
	   mov es, eax
1642
	   mov es, eax
1665
	   mov ebx, [esp+4]  ;IRQ_xx
1643
	   mov ebx, [esp+4]  ;IRQ_xx
1666
	   mov eax, [irq_handlers+ebx+4]
1644
	   mov eax, [irq_handlers+ebx+4]
1667
	   call intr_handler
1645
	   call intr_handler
1668
	   mov ecx, [esp+4]
1646
	   mov ecx, [esp+4]
1669
	   cmp [irq_actids+ecx*4], 0
1647
	   cmp [irq_actids+ecx*4], 0
1670
	   je @F
1648
	   je @F
1671
	   in al, 0x21
1649
	   in al, 0x21
1672
	   bts eax, ecx
1650
	   bts eax, ecx
1673
	   out 0x21, al
1651
	   out 0x21, al
1674
	   mov al, 0x20
1652
	   mov al, 0x20
1675
	   out 0x20, al
1653
	   out 0x20, al
1676
	   jmp .restart
1654
	   jmp .restart
1677
 
1655
 
1678
.slave:
1656
.slave:
1679
	   mov ax, app_data
1657
	   mov ax, app_data
1680
	   mov ds, eax
1658
	   mov ds, eax
1681
	   mov es, eax
1659
	   mov es, eax
1682
	   mov ebx, [esp+4]  ;IRQ_xx
1660
	   mov ebx, [esp+4]  ;IRQ_xx
1683
	   mov eax, [irq_handlers+ebx+4]
1661
	   mov eax, [irq_handlers+ebx+4]
1684
	   call intr_handler
1662
	   call intr_handler
1685
	   mov ecx, [esp+4]
1663
	   mov ecx, [esp+4]
1686
	   sub ecx, 8
1664
	   sub ecx, 8
1687
	   cmp [irq_actids+ecx*4], 0
1665
	   cmp [irq_actids+ecx*4], 0
1688
	   je @F
1666
	   je @F
1689
	   in al, 0xA1
1667
	   in al, 0xA1
1690
	   bts eax, ecx
1668
	   bts eax, ecx
1691
	   out 0xA1, al
1669
	   out 0xA1, al
1692
	   mov al, 0x20
1670
	   mov al, 0x20
1693
	   out 0xA0, al
1671
	   out 0xA0, al
1694
	   out 0x20, al
1672
	   out 0x20, al
1695
.restart:
1673
.restart:
1696
	   mov ebx, [next_slot]
1674
	   mov ebx, [next_slot]
1697
	   test ebx, ebx
1675
	   test ebx, ebx
1698
	   jz @F
1676
	   jz @F
1699
	   mov [next_task],0
1677
	   mov [next_task],0
1700
	   mov esi, [prev_slot]
1678
	   mov esi, [prev_slot]
1701
	   call do_change_task
1679
	   call do_change_task
1702
	   add esp, 4
1680
	   add esp, 4
1703
	   iretd
1681
	   iretd
1704
 
1682
 
1705
end if
1683
end if