aa64: finalise context switch, page fault handler and syscall
[lunaix-os.git] / lunaix-os / arch / aarch64 / klibc / string.c
1 #include <klibc/string.h>
2 #include <lunaix/compiler.h>
3
4 void*
5 memcpy(void* dest, const void* src, unsigned long num)
6 {
7     if (unlikely(!num))
8         return dest;
9         
10     asm volatile(
11         "1:                         \n"
12         "ldrb x0, [%[src],  %[l]]   \n"
13         "strb x0, [%[dest], %[l]]   \n"
14         "sub  %[l], %[l], %0        \n"
15         "cbnz  %[l], 1b             \n"
16         ::
17         "I"(1),
18         [src]  "r"(dest),
19         [dest] "r"(src),
20         [l]    "r"(num)
21         : "x0" "x1"
22     );
23
24     return dest;
25 }
26
27 void*
28 memset(void* ptr, int value, unsigned long num)
29 {
30     asm volatile(
31         "1:                             \n"
32         "strb %[val], [%[dest], %[l]]   \n"
33         "sub  %[l], %[l], %0            \n"
34         "cbnz %[l], 1b                  \n"
35         ::
36         "I"(1),
37         [val]  "r"(value),
38         [dest] "r"(ptr),
39         [l]    "r"(num)
40         : "x1"
41     );
42
43     return ptr;
44 }
45
46 unsigned long
47 strlen(const char* str)
48 {
49     unsigned long register _c asm("x0");
50
51     _c = 0;
52     asm volatile(
53         "1:                     \n"
54         "ldrb x1, [%[ptr], %0]  \n"
55         "add  %0, %0, %1        \n"
56         "cbnz %0, 1b            \n"
57         :
58         "=r"(_c)
59         :
60         "I"(1),
61         [ptr]  "r"(str)
62         : "x1"
63     );
64
65     return _c;
66 }
67
68 unsigned long
69 strnlen(const char* str, unsigned long max_len)
70 {
71     unsigned long register _c asm("x0");
72
73     _c = 0;
74     asm volatile(
75         "1:                     \n"
76         "ldrb x1, [%[ptr], %0]  \n"
77         "add  %0, %0, %1        \n"
78         "sub  x2, %[len], %0    \n"
79         "ands x2, x2, x1        \n"
80         "bne  1b                \n"
81         :
82         "=r"(_c)
83         :
84         "I"(1),
85         [ptr]  "r"(str),
86         [len]  "r"(max_len)
87         : "x1", "x2"
88     );
89
90     return _c;
91 }