[a68003f] | 1 | /*
|
---|
[7e9769f] | 2 | * Copyright (C) 2005 Sergey Bondari
|
---|
[a68003f] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[06e1e95] | 29 | /** @addtogroup amd64
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_amd64_MEMSTR_H_
|
---|
| 36 | #define KERN_amd64_MEMSTR_H_
|
---|
[a68003f] | 37 |
|
---|
[56d40fe] | 38 | /** Copy memory
|
---|
| 39 | *
|
---|
| 40 | * Copy a given number of bytes (3rd argument)
|
---|
| 41 | * from the memory location defined by 2nd argument
|
---|
| 42 | * to the memory location defined by 1st argument.
|
---|
| 43 | * The memory areas cannot overlap.
|
---|
| 44 | *
|
---|
| 45 | * @param dst Destination
|
---|
| 46 | * @param src Source
|
---|
| 47 | * @param cnt Number of bytes
|
---|
| 48 | * @return Destination
|
---|
| 49 | */
|
---|
| 50 | static inline void * memcpy(void * dst, const void * src, size_t cnt)
|
---|
| 51 | {
|
---|
[7f1c620] | 52 | unative_t d0, d1, d2;
|
---|
[56d40fe] | 53 |
|
---|
| 54 | __asm__ __volatile__(
|
---|
| 55 | "rep movsq\n\t"
|
---|
| 56 | "movq %4, %%rcx\n\t"
|
---|
| 57 | "andq $7, %%rcx\n\t"
|
---|
| 58 | "jz 1f\n\t"
|
---|
| 59 | "rep movsb\n\t"
|
---|
| 60 | "1:\n"
|
---|
| 61 | : "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
---|
[7f1c620] | 62 | : "0" ((unative_t)(cnt / 8)), "g" ((unative_t)cnt), "1" ((unative_t) dst), "2" ((unative_t) src)
|
---|
[56d40fe] | 63 | : "memory");
|
---|
| 64 |
|
---|
| 65 | return dst;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | /** Compare memory regions for equality
|
---|
| 70 | *
|
---|
| 71 | * Compare a given number of bytes (3rd argument)
|
---|
| 72 | * at memory locations defined by 1st and 2nd argument
|
---|
| 73 | * for equality. If bytes are equal function returns 0.
|
---|
| 74 | *
|
---|
| 75 | * @param src Region 1
|
---|
| 76 | * @param dst Region 2
|
---|
| 77 | * @param cnt Number of bytes
|
---|
| 78 | * @return Zero if bytes are equal, non-zero otherwise
|
---|
| 79 | */
|
---|
| 80 | static inline int memcmp(const void * src, const void * dst, size_t cnt)
|
---|
| 81 | {
|
---|
[7f1c620] | 82 | unative_t d0, d1, d2;
|
---|
| 83 | unative_t ret;
|
---|
[56d40fe] | 84 |
|
---|
| 85 | __asm__ (
|
---|
| 86 | "repe cmpsb\n\t"
|
---|
| 87 | "je 1f\n\t"
|
---|
| 88 | "movq %3, %0\n\t"
|
---|
| 89 | "addq $1, %0\n\t"
|
---|
| 90 | "1:\n"
|
---|
| 91 | : "=a" (ret), "=%S" (d0), "=&D" (d1), "=&c" (d2)
|
---|
[7f1c620] | 92 | : "0" (0), "1" (src), "2" (dst), "3" ((unative_t)cnt)
|
---|
[56d40fe] | 93 | );
|
---|
| 94 |
|
---|
| 95 | return ret;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Fill memory with words
|
---|
| 99 | * Fill a given number of words (2nd argument)
|
---|
| 100 | * at memory defined by 1st argument with the
|
---|
| 101 | * word value defined by 3rd argument.
|
---|
| 102 | *
|
---|
| 103 | * @param dst Destination
|
---|
| 104 | * @param cnt Number of words
|
---|
| 105 | * @param x Value to fill
|
---|
| 106 | */
|
---|
[7f1c620] | 107 | static inline void memsetw(uintptr_t dst, size_t cnt, uint16_t x)
|
---|
[56d40fe] | 108 | {
|
---|
[7f1c620] | 109 | unative_t d0, d1;
|
---|
[56d40fe] | 110 |
|
---|
| 111 | __asm__ __volatile__ (
|
---|
| 112 | "rep stosw\n\t"
|
---|
| 113 | : "=&D" (d0), "=&c" (d1), "=a" (x)
|
---|
[7f1c620] | 114 | : "0" (dst), "1" ((unative_t)cnt), "2" (x)
|
---|
[56d40fe] | 115 | : "memory"
|
---|
| 116 | );
|
---|
| 117 |
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Fill memory with bytes
|
---|
| 121 | * Fill a given number of bytes (2nd argument)
|
---|
| 122 | * at memory defined by 1st argument with the
|
---|
| 123 | * word value defined by 3rd argument.
|
---|
| 124 | *
|
---|
| 125 | * @param dst Destination
|
---|
| 126 | * @param cnt Number of bytes
|
---|
| 127 | * @param x Value to fill
|
---|
| 128 | */
|
---|
[7f1c620] | 129 | static inline void memsetb(uintptr_t dst, size_t cnt, uint8_t x)
|
---|
[56d40fe] | 130 | {
|
---|
[7f1c620] | 131 | unative_t d0, d1;
|
---|
[56d40fe] | 132 |
|
---|
| 133 | __asm__ __volatile__ (
|
---|
| 134 | "rep stosb\n\t"
|
---|
| 135 | : "=&D" (d0), "=&c" (d1), "=a" (x)
|
---|
[7f1c620] | 136 | : "0" (dst), "1" ((unative_t)cnt), "2" (x)
|
---|
[56d40fe] | 137 | : "memory"
|
---|
| 138 | );
|
---|
[a68003f] | 139 |
|
---|
[56d40fe] | 140 | }
|
---|
[a68003f] | 141 |
|
---|
| 142 | #endif
|
---|
[b45c443] | 143 |
|
---|
[06e1e95] | 144 | /** @}
|
---|
[b45c443] | 145 | */
|
---|