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
30FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31FILE_SECBOOT ( PERMITTED );
32
33#include <string.h>
34#include <config/defaults.h>
35
36/* Use generic_memcpy_reverse() if we cannot safely set the direction flag */
37#ifdef UNSAFE_STD
38#define USE_GENERIC_MEMCPY_REVERSE 1
39#else
40#define USE_GENERIC_MEMCPY_REVERSE 0
41#endif
42
43/**
44 * Copy memory area
45 *
46 * @v dest Destination address
47 * @v src Source address
48 * @v len Length
49 * @ret dest Destination address
50 */
51void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
52 size_t len ) {
53 void *edi = dest;
54 const void *esi = src;
55 int discard_ecx;
56
57 /* We often do large dword-aligned and dword-length block
58 * moves. Using movsl rather than movsb speeds these up by
59 * around 32%.
60 */
61 __asm__ __volatile__ ( "rep movsl"
62 : "=&D" ( edi ), "=&S" ( esi ),
63 "=&c" ( discard_ecx )
64 : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
65 : "memory" );
66 __asm__ __volatile__ ( "rep movsb"
67 : "=&D" ( edi ), "=&S" ( esi ),
68 "=&c" ( discard_ecx )
69 : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
70 : "memory" );
71 return dest;
72}
73
74/**
75 * Copy memory area backwards
76 *
77 * @v dest Destination address
78 * @v src Source address
79 * @v len Length
80 * @ret dest Destination address
81 */
82void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
83 const void *src,
84 size_t len ) {
85 void *edi = ( dest + len - 1 );
86 const void *esi = ( src + len - 1 );
87 int discard_ecx;
88
89 /* Use unoptimised version if we are not permitted to modify
90 * the direction flag.
91 */
94
95 /* Assume memmove() is not performance-critical, and perform a
96 * bytewise copy for simplicity.
97 */
98 __asm__ __volatile__ ( "std\n\t"
99 "rep movsb\n\t"
100 "cld\n\t"
101 : "=&D" ( edi ), "=&S" ( esi ),
102 "=&c" ( discard_ecx )
103 : "0" ( edi ), "1" ( esi ),
104 "2" ( len )
105 : "memory" );
106 return dest;
107}
108
109
110/**
111 * Copy (possibly overlapping) memory area
112 *
113 * @v dest Destination address
114 * @v src Source address
115 * @v len Length
116 * @ret dest Destination address
117 */
118void * __memmove ( void *dest, const void *src, size_t len ) {
119
120 if ( dest <= src ) {
121 return __memcpy ( dest, src, len );
122 } else {
123 return __memcpy_reverse ( dest, src, len );
124 }
125}
uint32_t discard_ecx
Definition hyperv.h:31
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
void * __memcpy_reverse(void *dest, const void *src, size_t len)
void * __memcpy(void *dest, const void *src, size_t len)
const void * esi
Definition string.h:59
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" retur dest)
Definition string.h:151
void * edi
Definition string.h:60
static const void * src
Definition string.h:48
ring len
Length.
Definition dwmac.h:226
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define __attribute__(x)
Definition compiler.h:10
String functions.
__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 * generic_memcpy_reverse(void *dest, const void *src, size_t len)
Copy memory region (backwards)
Definition string.c:81
void * __memmove(void *dest, const void *src, size_t len)
Copy (possibly overlapping) memory area.
Definition x86_string.c:118
#define USE_GENERIC_MEMCPY_REVERSE
Definition x86_string.c:40