x86_64 support, requiring a slight refactor to the build system.
[tpg/acess2.git] / Kernel / arch / x86_64 / lib.c
1 /*
2  */
3 #include <acess.h>
4 #include <arch.h>
5
6 // === CODE ===
7
8 void outb(Uint16 Port, Uint8 Data)
9 {
10         __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
11 }
12 void outw(Uint16 Port, Uint16 Data)
13 {
14         __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
15 }
16 void outd(Uint16 Port, Uint32 Data)
17 {
18         __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
19 }
20 Uint8 inb(Uint16 Port)
21 {
22         Uint8   ret;
23         __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
24         return ret;
25 }
26 Uint16 inw(Uint16 Port)
27 {
28         Uint16  ret;
29         __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
30         return ret;
31 }
32 Uint32 ind(Uint16 Port)
33 {
34         Uint32  ret;
35         __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
36         return ret;
37 }
38
39 // === Endianness ===
40 Uint32 BigEndian32(Uint32 Value)
41 {
42         Uint32  ret;
43         ret = (Value >> 24);
44         ret |= ((Value >> 16) & 0xFF) << 8;
45         ret |= ((Value >>  8) & 0xFF) << 16;
46         ret |= ((Value >>  0) & 0xFF) << 24;
47         return ret;
48 }
49
50 Uint16 BigEndian16(Uint16 Value)
51 {
52         return  (Value>>8)|(Value<<8);
53 }
54
55 // === Memory Manipulation ===
56 void *memcpy(void *__dest, const void *__src, size_t __count)
57 {
58         if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) )
59                 __asm__ __volatile__ ("rep movsb" : : "D"(__dest),"S"(__src),"c"(__count));
60         else {
61                 const Uint8     *src = __src;
62                 Uint8   *dst = __dest;
63                 while( (tVAddr)src & 7 && __count ) {
64                         *dst++ = *src++;
65                         __count --;
66                 }
67
68                 __asm__ __volatile__ ("rep movsq" : : "D"(dst),"S"(src),"c"(__count/8));
69                 src += __count & ~7;
70                 dst += __count & ~7;
71                 __count = __count & 7;
72                 while( __count-- )
73                         *dst++ = *src++;
74         }
75         return __dest;
76 }
77
78 void *memset(void *__dest, int __val, size_t __count)
79 {
80         if( __val != 0 || ((tVAddr)__dest & 7) != 0 )
81                 __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
82         else {
83                 Uint8   *dst = __dest;
84
85                 __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
86                 dst += __count & ~7;
87                 __count = __count & 7;
88                 while( __count-- )
89                         *dst++ = 0;
90         }
91         return __dest;
92 }
93

UCC git Repository :: git.ucc.asn.au