Subversion Repositories HelenOS-historic

Rev

Rev 532 | Rev 1702 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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