1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
title "User Mode Zero and Move Memory functions"
;++
;
; Copyright (c) 1989 Microsoft Corporation
;
; Module Name:
;
; movemem.asm
;
; Abstract:
;
; This module implements functions to zero and copy blocks of memory
;
;
; Author:
;
; Steven R. Wood (stevewo) 25-May-1990
;
; Environment:
;
; User mode only.
;
; Revision History:
;
;--
.386p
.xlist
include ks386.inc
include callconv.inc ; calling convention macros
.list
if DBG
_DATA SEGMENT DWORD PUBLIC 'DATA'
public _RtlpZeroCount
public _RtlpZeroBytes
_RtlpZeroCount dd 0
_RtlpZeroBytes dd 0
ifndef BLDR_KERNEL_RUNTIME
_MsgUnalignedPtr db 'RTL: RtlCompare/FillMemoryUlong called with unaligned pointer (%x)\n',0
endif
_DATA ENDS
ifndef BLDR_KERNEL_RUNTIME
ifdef NTOS_KERNEL_RUNTIME
extrn _KdDebuggerEnabled:BYTE
endif
EXTRNP _DbgBreakPoint,0
extrn _DbgPrint:near
endif
endif
;
; Alignment parameters for zeroing and moving memory.
;
ZERO_MEMORY_ALIGNMENT = 4
ZERO_MEMORY_ALIGNMENT_LOG2 = 2
ZERO_MEMORY_ALIGNMENT_MASK = ZERO_MEMORY_ALIGNMENT - 1
MEMORY_ALIGNMENT = 4
MEMORY_ALIGNMENT_LOG2 = 2
MEMORY_ALIGNMENT_MASK = MEMORY_ALIGNMENT - 1
;
; Alignment for functions in this module
;
CODE_ALIGNMENT macro
align 16
endm
_TEXT$00 SEGMENT PARA PUBLIC 'CODE'
ASSUME DS:FLAT, ES:FLAT, SS:NOTHING, FS:NOTHING, GS:NOTHING
page , 132
subttl "RtlCompareMemory"
;++
;
; ULONG
; RtlCompareMemory (
; IN PVOID Source1,
; IN PVOID Source2,
; IN ULONG Length
; )
;
; Routine Description:
;
; This function compares two blocks of memory and returns the number
; of bytes that compared equal.
;
; Arguments:
;
; Source1 (esp+4) - Supplies a pointer to the first block of memory to
; compare.
;
; Source2 (esp+8) - Supplies a pointer to the second block of memory to
; compare.
;
; Length (esp+12) - Supplies the Length, in bytes, of the memory to be
; compared.
;
; Return Value:
;
; The number of bytes that compared equal is returned as the function
; value. If all bytes compared equal, then the length of the orginal
; block of memory is returned.
;
;--
RcmSource1 equ [esp+12]
RcmSource2 equ [esp+16]
RcmLength equ [esp+20]
CODE_ALIGNMENT
cPublicProc _RtlCompareMemory,3
cPublicFpo 3,0
push esi ; save registers
push edi ;
cld ; clear direction
mov esi,RcmSource1 ; (esi) -> first block to compare
mov edi,RcmSource2 ; (edi) -> second block to compare
;
; Compare dwords, if any.
;
rcm10: mov ecx,RcmLength ; (ecx) = length in bytes
shr ecx,2 ; (ecx) = length in dwords
jz rcm20 ; no dwords, try bytes
repe cmpsd ; compare dwords
jnz rcm40 ; mismatch, go find byte
;
; Compare residual bytes, if any.
;
rcm20: mov ecx,RcmLength ; (ecx) = length in bytes
and ecx,3 ; (ecx) = length mod 4
jz rcm30 ; 0 odd bytes, go do dwords
repe cmpsb ; compare odd bytes
jnz rcm50 ; mismatch, go report how far we got
;
; All bytes in the block match.
;
rcm30: mov eax,RcmLength ; set number of matching bytes
pop edi ; restore registers
pop esi ;
stdRET _RtlCompareMemory
;
; When we come to rcm40, esi (and edi) points to the dword after the
; one which caused the mismatch. Back up 1 dword and find the byte.
; Since we know the dword didn't match, we can assume one byte won't.
;
rcm40: sub esi,4 ; back up
sub edi,4 ; back up
mov ecx,5 ; ensure that ecx doesn't count out
repe cmpsb ; find mismatch byte
;
; When we come to rcm50, esi points to the byte after the one that
; did not match, which is TWO after the last byte that did match.
;
rcm50: dec esi ; back up
sub esi,RcmSource1 ; compute bytes that matched
mov eax,esi ;
pop edi ; restore registers
pop esi ;
stdRET _RtlCompareMemory
stdENDP _RtlCompareMemory
subttl "RtlCompareMemory"
EcmlSource equ [esp + 4 + 4]
EcmlLength equ [esp + 4 + 8]
EcmlPattern equ [esp + 4 + 12]
; end of arguments
CODE_ALIGNMENT
cPublicProc _RtlCompareMemoryUlong ,3
;
; Save the non-volatile registers that we will use, without the benefit of
; a frame pointer. No exception handling in this routine.
;
push edi
;
; Setup the registers for using REP STOS instruction to zero memory.
;
; edi -> memory to zero
; ecx = number of 32-bits words to zero
; edx = number of extra 8-bit bytes to zero at the end (0 - 3)
; eax = value to store in destination
; direction flag is clear for auto-increment
;
mov edi,EcmlSource
if DBG
ifndef BLDR_KERNEL_RUNTIME
test edi,3
jz @F
push edi
push offset FLAT:_MsgUnalignedPtr
call _DbgPrint
add esp, 2 * 4
ifdef NTOS_KERNEL_RUNTIME
cmp _KdDebuggerEnabled,0
else
mov eax,fs:[PcTeb]
mov eax,[eax].TebPeb
cmp byte ptr [eax].PebBeingDebugged,0
endif
je @F
call _DbgBreakPoint@0
@@:
endif
endif
mov ecx,EcmlLength
mov eax,EcmlPattern
shr ecx,ZERO_MEMORY_ALIGNMENT_LOG2
;
; If number of 32-bit words to compare is non-zero, then do it.
;
repe scasd
je @F
sub edi,4
@@:
sub edi,EcmlSource
mov eax,edi
pop edi
stdRET _RtlCompareMemoryUlong
stdENDP _RtlCompareMemoryUlong
subttl "RtlFillMemory"
;++
;
; VOID
; RtlFillMemory (
; IN PVOID Destination,
; IN ULONG Length,
; IN UCHAR Fill
; )
;
; Routine Description:
;
; This function fills memory with a byte value.
;
; Arguments:
;
; Destination - Supplies a pointer to the memory to zero.
;
; Length - Supplies the Length, in bytes, of the memory to be zeroed.
;
; Fill - Supplies the byte value to fill memory with.
;
; Return Value:
;
; None.
;
;--
; definitions for arguments
; (TOS) = Return address
EfmDestination equ [esp + 4 + 4]
EfmLength equ [esp + 4 + 8]
EfmFill equ byte ptr [esp + 4 + 12]
; end of arguments
CODE_ALIGNMENT
cPublicProc _RtlFillMemory ,3
cPublicFpo 3,1
;
; Save the non-volatile registers that we will use, without the benefit of
; a frame pointer. No exception handling in this routine.
;
push edi
;
; Setup the registers for using REP STOS instruction to zero memory.
;
; edi -> memory to zero
; ecx = number of 32-bits words to zero
; edx = number of extra 8-bit bytes to zero at the end (0 - 3)
; eax = value to store in destination
; direction flag is clear for auto-increment
;
mov edi,EfmDestination
mov ecx,EfmLength
mov al,EfmFill
mov ah,al
shl eax,16
mov al,EfmFill
mov ah,al
cld
mov edx,ecx
and edx,ZERO_MEMORY_ALIGNMENT_MASK
shr ecx,ZERO_MEMORY_ALIGNMENT_LOG2
;
; If number of 32-bit words to zero is non-zero, then do it.
;
rep stosd
;
; If number of extra 8-bit bytes to zero is non-zero, then do it. In either
; case restore non-volatile registers and return.
;
or ecx,edx
jnz @F
pop edi
stdRET _RtlFillMemory
@@:
rep stosb
pop edi
stdRET _RtlFillMemory
stdENDP _RtlFillMemory
subttl "RtlFillMemory"
;++
;
; VOID
; RtlFillMemoryUlong (
; IN PVOID Destination,
; IN ULONG Length,
; IN ULONG Fill
; )
;
; Routine Description:
;
; This function fills memory with a 32-bit value. The Destination pointer
; must be aligned on a 4 byte boundary and the low order two bits of the
; Length parameter are ignored.
;
; Arguments:
;
; Destination - Supplies a pointer to the memory to zero.
;
; Length - Supplies the Length, in bytes, of the memory to be zeroed.
;
; Fill - Supplies the 32-bit value to fill memory with.
;
; Return Value:
;
; None.
;
;--
; definitions for arguments
; (TOS) = Return address
EfmlDestination equ [esp + 4 + 4]
EfmlLength equ [esp + 4 + 8]
EfmlFill equ [esp + 4 + 12]
; end of arguments
CODE_ALIGNMENT
cPublicProc _RtlFillMemoryUlong ,3
cPublicFpo 3,1
;
; Save the non-volatile registers that we will use, without the benefit of
; a frame pointer. No exception handling in this routine.
;
push edi
;
; Setup the registers for using REP STOS instruction to zero memory.
;
; edi -> memory to zero
; ecx = number of 32-bits words to zero
; edx = number of extra 8-bit bytes to zero at the end (0 - 3)
; eax = value to store in destination
; direction flag is clear for auto-increment
;
mov edi,EfmlDestination
if DBG
ifndef BLDR_KERNEL_RUNTIME
test edi,3
jz @F
push edi
push offset FLAT:_MsgUnalignedPtr
call _DbgPrint
add esp, 2 * 4
ifdef NTOS_KERNEL_RUNTIME
cmp _KdDebuggerEnabled,0
else
mov eax,fs:[PcTeb]
mov eax,[eax].TebPeb
cmp byte ptr [eax].PebBeingDebugged,0
endif
je @F
call _DbgBreakPoint@0
@@:
endif
endif
mov ecx,EfmlLength
mov eax,EfmlFill
shr ecx,ZERO_MEMORY_ALIGNMENT_LOG2
;
; If number of 32-bit words to zero is non-zero, then do it.
;
rep stosd
pop edi
stdRET _RtlFillMemoryUlong
stdENDP _RtlFillMemoryUlong
subttl "RtlZeroMemory"
;++
;
; VOID
; RtlZeroMemory (
; IN PVOID Destination,
; IN ULONG Length
; )
;
; Routine Description:
;
; This function zeros memory.
;
; Arguments:
;
; Destination - Supplies a pointer to the memory to zero.
;
; Length - Supplies the Length, in bytes, of the memory to be zeroed.
;
; Return Value:
;
; None.
;
;--
; definitions for arguments
; (TOS) = Return address
EzmDestination equ [esp + 4 + 4]
EzmLength equ [esp + 4 + 8]
; end of arguments
CODE_ALIGNMENT
cPublicProc _RtlZeroMemory ,2
cPublicFpo 2,1
;
; Save the non-volatile registers that we will use, without the benefit of
; a frame pointer. No exception handling in this routine.
;
push edi
;
; Setup the registers for using REP STOS instruction to zero memory.
;
; edi -> memory to zero
; ecx = number of 32-bits words to zero
; edx = number of extra 8-bit bytes to zero at the end (0 - 3)
; eax = zero (value to store in destination)
; direction flag is clear for auto-increment
;
mov edi,EzmDestination
mov ecx,EzmLength
xor eax,eax
cld
mov edx,ecx
and edx,ZERO_MEMORY_ALIGNMENT_MASK
shr ecx,ZERO_MEMORY_ALIGNMENT_LOG2
;
; If number of 32-bit words to zero is non-zero, then do it.
;
rep stosd
;
; If number of extra 8-bit bytes to zero is non-zero, then do it. In either
; case restore non-volatile registers and return.
;
or ecx,edx
jnz @F
pop edi
stdRET _RtlZeroMemory
@@:
rep stosb
pop edi
stdRET _RtlZeroMemory
stdENDP _RtlZeroMemory
page , 132
subttl "RtlMoveMemory"
;++
;
; VOID
; RtlMoveMemory (
; IN PVOID Destination,
; IN PVOID Source OPTIONAL,
; IN ULONG Length
; )
;
; Routine Description:
;
; This function moves memory either forward or backward, aligned or
; unaligned, in 4-byte blocks, followed by any remaining bytes.
;
; Arguments:
;
; Destination - Supplies a pointer to the destination of the move.
;
; Source - Supplies a pointer to the memory to move.
;
; Length - Supplies the Length, in bytes, of the memory to be moved.
;
; Return Value:
;
; None.
;
;--
; Definitions of arguments
; (TOS) = Return address
EmmDestination equ [esp + 8 + 4]
EmmSource equ [esp + 8 + 8]
EmmLength equ [esp + 8 + 12]
; End of arguments
CODE_ALIGNMENT
cPublicProc _RtlMoveMemory ,3
cPublicFpo 3,2
;
; Save the non-volatile registers that we will use, without the benefit of
; a frame pointer. No exception handling in this routine.
;
push esi
push edi
;
; Setup the registers for using REP MOVS instruction to move memory.
;
; esi -> memory to move (NULL implies the destination will be zeroed)
; edi -> destination of move
; ecx = number of 32-bits words to move
; edx = number of extra 8-bit bytes to move at the end (0 - 3)
; direction flag is clear for auto-increment
;
mov esi,EmmSource
mov edi,EmmDestination
mov ecx,EmmLength
if DBG
inc _RtlpZeroCount
add _RtlpZeroBytes,ecx
endif
cld
cmp esi,edi ; Special case if Source > Destination
jbe overlap
nooverlap:
mov edx,ecx
and edx,MEMORY_ALIGNMENT_MASK
shr ecx,MEMORY_ALIGNMENT_LOG2
;
; If number of 32-bit words to move is non-zero, then do it.
;
rep movsd
;
; If number of extra 8-bit bytes to move is non-zero, then do it. In either
; case restore non-volatile registers and return.
;
or ecx,edx
jnz @F
pop edi
pop esi
stdRET _RtlMoveMemory
@@:
rep movsb
movedone:
pop edi
pop esi
stdRET _RtlMoveMemory
;
; Here to handle special case when Source > Destination and therefore is a
; potential overlapping move. If Source == Destination, then nothing to do.
; Otherwise, increment the Source and Destination pointers by Length and do
; the move backwards, a byte at a time.
;
overlap:
je movedone
mov eax,edi
sub eax,esi
cmp ecx,eax
jbe nooverlap
std
add esi,ecx
add edi,ecx
dec esi
dec edi
rep movsb
cld
jmp short movedone
stdENDP _RtlMoveMemory
_TEXT$00 ends
end
|