Rev 2082 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 361 | bondari | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2005 Sergey Bondari |
| 361 | bondari | 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 | |||
| 1888 | jermar | 29 | /** @addtogroup ia32 |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 1888 | jermar | 35 | #ifndef KERN_ia32_MEMSTR_H_ |
| 36 | #define KERN_ia32_MEMSTR_H_ |
||
| 361 | bondari | 37 | |
| 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 | * |
||
| 393 | bondari | 45 | * @param dst Destination |
| 46 | * @param src Source |
||
| 47 | * @param cnt Number of bytes |
||
| 48 | * @return Destination |
||
| 361 | bondari | 49 | */ |
| 50 | static inline void * memcpy(void * dst, const void * src, size_t cnt) |
||
| 51 | { |
||
| 1780 | jermar | 52 | unative_t d0, d1, d2; |
| 361 | bondari | 53 | |
| 2082 | decky | 54 | asm volatile( |
| 361 | bondari | 55 | /* copy all full dwords */ |
| 56 | "rep movsl\n\t" |
||
| 57 | /* load count again */ |
||
| 58 | "movl %4, %%ecx\n\t" |
||
| 59 | /* ecx = ecx mod 4 */ |
||
| 60 | "andl $3, %%ecx\n\t" |
||
| 61 | /* are there last <=3 bytes? */ |
||
| 62 | "jz 1f\n\t" |
||
| 63 | /* copy last <=3 bytes */ |
||
| 64 | "rep movsb\n\t" |
||
| 65 | /* exit from asm block */ |
||
| 66 | "1:\n" |
||
| 67 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) |
||
| 1780 | jermar | 68 | : "0" ((unative_t) (cnt / 4)), "g" ((unative_t) cnt), "1" ((unative_t) dst), "2" ((unative_t) src) |
| 361 | bondari | 69 | : "memory"); |
| 70 | |||
| 71 | return dst; |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 392 | bondari | 75 | /** Compare memory regions for equality |
| 390 | bondari | 76 | * |
| 77 | * Compare a given number of bytes (3rd argument) |
||
| 78 | * at memory locations defined by 1st and 2nd argument |
||
| 392 | bondari | 79 | * for equality. If bytes are equal function returns 0. |
| 390 | bondari | 80 | * |
| 393 | bondari | 81 | * @param src Region 1 |
| 82 | * @param dst Region 2 |
||
| 83 | * @param cnt Number of bytes |
||
| 84 | * @return Zero if bytes are equal, non-zero otherwise |
||
| 390 | bondari | 85 | */ |
| 419 | bondari | 86 | static inline int memcmp(const void * src, const void * dst, size_t cnt) |
| 390 | bondari | 87 | { |
| 1780 | jermar | 88 | uint32_t d0, d1, d2; |
| 390 | bondari | 89 | int ret; |
| 90 | |||
| 2082 | decky | 91 | asm ( |
| 390 | bondari | 92 | "repe cmpsb\n\t" |
| 93 | "je 1f\n\t" |
||
| 94 | "movl %3, %0\n\t" |
||
| 95 | "addl $1, %0\n\t" |
||
| 96 | "1:\n" |
||
| 97 | : "=a" (ret), "=%S" (d0), "=&D" (d1), "=&c" (d2) |
||
| 1780 | jermar | 98 | : "0" (0), "1" ((unative_t) src), "2" ((unative_t) dst), "3" ((unative_t) cnt) |
| 390 | bondari | 99 | ); |
| 100 | |||
| 101 | return ret; |
||
| 102 | } |
||
| 361 | bondari | 103 | |
| 392 | bondari | 104 | /** Fill memory with words |
| 105 | * Fill a given number of words (2nd argument) |
||
| 106 | * at memory defined by 1st argument with the |
||
| 107 | * word value defined by 3rd argument. |
||
| 108 | * |
||
| 393 | bondari | 109 | * @param dst Destination |
| 110 | * @param cnt Number of words |
||
| 111 | * @param x Value to fill |
||
| 392 | bondari | 112 | */ |
| 1780 | jermar | 113 | static inline void memsetw(uintptr_t dst, size_t cnt, uint16_t x) |
| 392 | bondari | 114 | { |
| 1780 | jermar | 115 | uint32_t d0, d1; |
| 392 | bondari | 116 | |
| 2082 | decky | 117 | asm volatile ( |
| 392 | bondari | 118 | "rep stosw\n\t" |
| 119 | : "=&D" (d0), "=&c" (d1), "=a" (x) |
||
| 120 | : "0" (dst), "1" (cnt), "2" (x) |
||
| 121 | : "memory" |
||
| 122 | ); |
||
| 123 | |||
| 124 | } |
||
| 125 | |||
| 126 | /** Fill memory with bytes |
||
| 127 | * Fill a given number of bytes (2nd argument) |
||
| 128 | * at memory defined by 1st argument with the |
||
| 129 | * word value defined by 3rd argument. |
||
| 130 | * |
||
| 393 | bondari | 131 | * @param dst Destination |
| 132 | * @param cnt Number of bytes |
||
| 133 | * @param x Value to fill |
||
| 392 | bondari | 134 | */ |
| 1780 | jermar | 135 | static inline void memsetb(uintptr_t dst, size_t cnt, uint8_t x) |
| 392 | bondari | 136 | { |
| 1780 | jermar | 137 | uint32_t d0, d1; |
| 392 | bondari | 138 | |
| 2082 | decky | 139 | asm volatile ( |
| 392 | bondari | 140 | "rep stosb\n\t" |
| 141 | : "=&D" (d0), "=&c" (d1), "=a" (x) |
||
| 142 | : "0" (dst), "1" (cnt), "2" (x) |
||
| 143 | : "memory" |
||
| 144 | ); |
||
| 145 | |||
| 146 | } |
||
| 147 | |||
| 361 | bondari | 148 | #endif |
| 1702 | cejka | 149 | |
| 1888 | jermar | 150 | /** @} |
| 1702 | cejka | 151 | */ |