iPXE
x86_string.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 /** @file
25  *
26  * Optimised string operations
27  *
28  */
29 
30 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 
32 #include <string.h>
33 #include <config/defaults.h>
34 
35 /* Use generic_memcpy_reverse() if we cannot safely set the direction flag */
36 #ifdef UNSAFE_STD
37 #define USE_GENERIC_MEMCPY_REVERSE 1
38 #else
39 #define USE_GENERIC_MEMCPY_REVERSE 0
40 #endif
41 
42 /**
43  * Copy memory area
44  *
45  * @v dest Destination address
46  * @v src Source address
47  * @v len Length
48  * @ret dest Destination address
49  */
50 void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
51  size_t len ) {
52  void *edi = dest;
53  const void *esi = src;
54  int discard_ecx;
55 
56  /* We often do large dword-aligned and dword-length block
57  * moves. Using movsl rather than movsb speeds these up by
58  * around 32%.
59  */
60  __asm__ __volatile__ ( "rep movsl"
61  : "=&D" ( edi ), "=&S" ( esi ),
62  "=&c" ( discard_ecx )
63  : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
64  : "memory" );
65  __asm__ __volatile__ ( "rep movsb"
66  : "=&D" ( edi ), "=&S" ( esi ),
67  "=&c" ( discard_ecx )
68  : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
69  : "memory" );
70  return dest;
71 }
72 
73 /**
74  * Copy memory area backwards
75  *
76  * @v dest Destination address
77  * @v src Source address
78  * @v len Length
79  * @ret dest Destination address
80  */
81 void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
82  const void *src,
83  size_t len ) {
84  void *edi = ( dest + len - 1 );
85  const void *esi = ( src + len - 1 );
86  int discard_ecx;
87 
88  /* Use unoptimised version if we are not permitted to modify
89  * the direction flag.
90  */
92  return generic_memcpy_reverse ( dest, src, len );
93 
94  /* Assume memmove() is not performance-critical, and perform a
95  * bytewise copy for simplicity.
96  */
97  __asm__ __volatile__ ( "std\n\t"
98  "rep movsb\n\t"
99  "cld\n\t"
100  : "=&D" ( edi ), "=&S" ( esi ),
101  "=&c" ( discard_ecx )
102  : "0" ( edi ), "1" ( esi ),
103  "2" ( len )
104  : "memory" );
105  return dest;
106 }
107 
108 
109 /**
110  * Copy (possibly overlapping) memory area
111  *
112  * @v dest Destination address
113  * @v src Source address
114  * @v len Length
115  * @ret dest Destination address
116  */
117 void * __memmove ( void *dest, const void *src, size_t len ) {
118 
119  if ( dest <= src ) {
120  return __memcpy ( dest, src, len );
121  } else {
122  return __memcpy_reverse ( dest, src, len );
123  }
124 }
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void const void * src
Definition: crypto.h:244
void * __memmove(void *dest, const void *src, size_t len)
Copy (possibly overlapping) memory area.
Definition: x86_string.c:117
const void * esi
Definition: string.h:58
void * __memcpy_reverse(void *dest, const void *src, size_t len)
static void * dest
Definition: strings.h:176
void * generic_memcpy_reverse(void *dest, const void *src, size_t len)
Copy memory region (backwards)
Definition: string.c:80
uint32_t discard_ecx
Definition: hyperv.h:31
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
void * edi
Definition: string.h:59
#define USE_GENERIC_MEMCPY_REVERSE
Definition: x86_string.c:39
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
void * __attribute__((noinline))
Copy memory area.
Definition: x86_string.c:50
uint32_t len
Length.
Definition: ena.h:14
void * __memcpy(void *dest, const void *src, size_t len)
String functions.