2 * AcessOS Microkernel Version
8 void Spinlock(int *lock)
11 while(v) __asm__ __volatile__ ("lock xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(lock));
14 void Release(int *lock)
16 __asm__ __volatile__ ("lock andl $0, (%0)"::"r"(lock));
19 // === IO Commands ===
20 void outb(Uint16 Port, Uint8 Data)
22 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
24 void outw(Uint16 Port, Uint16 Data)
26 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
28 void outd(Uint16 Port, Uint32 Data)
30 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
32 Uint8 inb(Uint16 Port)
35 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
38 Uint16 inw(Uint16 Port)
41 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
44 Uint32 ind(Uint16 Port)
47 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
52 * \fn void *memset(void *Dest, int Val, Uint Num)
53 * \brief Do a byte granuality set of Dest
55 void *memset(void *Dest, int Val, Uint Num)
57 __asm__ __volatile__ (
61 :: "D" (Dest), "a" (Val), "c" (Num/4), "r" (Num&3));
65 * \fn void *memsetd(void *Dest, Uint Val, Uint Num)
67 void *memsetd(void *Dest, Uint Val, Uint Num)
69 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
74 * \fn int memcmp(const void *m1, const void *m2, Uint Num)
75 * \brief Compare two pieces of memory
77 int memcmp(const void *m1, const void *m2, Uint Num)
81 if(*(Uint8*)m1 != *(Uint8*)m2) break;
85 return *(Uint8*)m1 - *(Uint8*)m2;
89 * \fn void *memcpy(void *Dest, const void *Src, Uint Num)
90 * \brief Copy \a Num bytes from \a Src to \a Dest
92 void *memcpy(void *Dest, const void *Src, Uint Num)
94 if((Uint)Dest & 3 || (Uint)Src & 3)
95 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
97 __asm__ __volatile__ (
101 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
106 * \fn void *memcpyd(void *Dest, const void *Src, Uint Num)
107 * \brief Copy \a Num DWORDs from \a Src to \a Dest
109 void *memcpyd(void *Dest, const void *Src, Uint Num)
111 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
116 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
117 * \brief Divide two 64-bit integers
119 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
123 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
124 if(Den == 1) return Num; // Speed Hacks
125 if(Den == 2) return Num >> 1; // Speed Hacks
126 if(Den == 4) return Num >> 2; // Speed Hacks
127 if(Den == 8) return Num >> 3; // Speed Hacks
128 if(Den == 16) return Num >> 4; // Speed Hacks
129 if(Den == 32) return Num >> 5; // Speed Hacks
130 if(Den == 1024) return Num >> 10; // Speed Hacks
131 if(Den == 2048) return Num >> 11; // Speed Hacks
132 if(Den == 4096) return Num >> 12;
134 if(Num >> 32 == 0 && Den >> 32 == 0)
135 return (Uint32)Num / (Uint32)Den;
137 //Log("__udivdi3: (Num={0x%x:%x}, Den={0x%x:%x})",
138 // Num>>32, Num&0xFFFFFFFF,
139 // Den>>32, Den&0xFFFFFFFF);
149 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
150 * \brief Get the modulus of two 64-bit integers
152 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
154 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
155 if(Den == 1) return 0; // Speed Hacks
156 if(Den == 2) return Num & 1; // Speed Hacks
157 if(Den == 4) return Num & 3; // Speed Hacks
158 if(Den == 8) return Num & 7; // Speed Hacks
159 if(Den == 16) return Num & 15; // Speed Hacks
160 if(Den == 32) return Num & 31; // Speed Hacks
161 if(Den == 1024) return Num & 1023; // Speed Hacks
162 if(Den == 2048) return Num & 2047; // Speed Hacks
163 if(Den == 4096) return Num & 4095; // Speed Hacks
165 if(Num >> 32 == 0 && Den >> 32 == 0)
166 return (Uint32)Num % (Uint32)Den;
173 Uint16 LittleEndian16(Uint16 Val)
177 Uint16 BigEndian16(Uint16 Val)
179 return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
181 Uint32 LittleEndian32(Uint32 Val)
185 Uint32 BigEndian32(Uint32 Val)
187 return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
191 EXPORT(memcpy); EXPORT(memset);
193 //EXPORT(memcpyw); EXPORT(memsetw);
194 EXPORT(memcpyd); EXPORT(memsetd);
195 EXPORT(inb); EXPORT(inw); EXPORT(ind);
196 EXPORT(outb); EXPORT(outw); EXPORT(outd);
197 EXPORT(__udivdi3); EXPORT(__umoddi3);
199 EXPORT(LittleEndian16); EXPORT(BigEndian16);
200 EXPORT(LittleEndian32); EXPORT(BigEndian32);