2 * AcessOS Microkernel Version
8 extern int GetCPUNum(void);
12 * \brief Determine if a short spinlock is locked
13 * \param Lock Lock pointer
15 int IS_LOCKED(struct sShortSpinlock *Lock)
21 * \brief Check if the current CPU has the lock
22 * \param Lock Lock pointer
24 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
26 #if STACKED_LOCKS == 1
27 return Lock->Lock == GetCPUNum() + 1;
28 #elif STACKED_LOCKS == 2
29 return Lock->Lock == Proc_GetCurThread();
36 * \brief Acquire a Short Spinlock
37 * \param Lock Lock pointer
39 * This type of mutex should only be used for very short sections of code,
40 * or in places where a Mutex_* would be overkill, such as appending
41 * an element to linked list (usually two assignement lines in C)
43 * \note This type of lock halts interrupts, so ensure that no timing
44 * functions are called while it is held. As a matter of fact, spend as
45 * little time as possible with this lock held
46 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
48 void SHORTLOCK(struct sShortSpinlock *Lock)
54 #if STACKED_LOCKS == 1
55 int cpu = GetCPUNum() + 1;
56 #elif STACKED_LOCKS == 2
57 void *thread = Proc_GetCurThread();
61 // Save interrupt state and clear interrupts
62 __ASM__ ("pushf;\n\tpop %%eax\n\tcli" : "=a"(IF));
63 IF &= 0x200; // AND out all but the interrupt flag
66 #if STACKED_LOCKS == 1
67 if( Lock->Lock == cpu ) {
71 #elif STACKED_LOCKS == 2
72 if( Lock->Lock == thread ) {
78 // Wait for another CPU to release
81 // If r/m32 == EAX, set ZF and set r/m32 = r32
82 // Else, clear ZF and set EAX = r/m32
83 #if STACKED_LOCKS == 1
84 __ASM__("lock cmpxchgl %2, (%3)"
86 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
88 #elif STACKED_LOCKS == 2
89 __ASM__("lock cmpxchgl %2, (%3)"
91 : "a"(0), "r"(thread), "r"(&Lock->Lock)
94 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
103 * \brief Release a short lock
104 * \param Lock Lock pointer
106 void SHORTREL(struct sShortSpinlock *Lock)
115 #if LOCK_DISABLE_INTS
116 // Lock->IF can change anytime once Lock->Lock is zeroed
129 // === IO Commands ===
130 void outb(Uint16 Port, Uint8 Data)
132 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
134 void outw(Uint16 Port, Uint16 Data)
136 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
138 void outd(Uint16 Port, Uint32 Data)
140 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
142 Uint8 inb(Uint16 Port)
145 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
148 Uint16 inw(Uint16 Port)
151 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
154 Uint32 ind(Uint16 Port)
157 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
162 * \fn void *memset(void *Dest, int Val, size_t Num)
163 * \brief Do a byte granuality set of Dest
165 void *memset(void *Dest, int Val, size_t Num)
167 Uint32 val = Val&0xFF;
170 __asm__ __volatile__ (
174 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
178 * \brief Set double words
180 void *memsetd(void *Dest, Uint32 Val, size_t Num)
182 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
187 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
188 * \brief Compare two pieces of memory
190 int memcmp(const void *m1, const void *m2, size_t Num)
192 if( Num == 0 ) return 1; // No bytes are always identical
196 if(*(Uint8*)m1 != *(Uint8*)m2) break;
200 return *(Uint8*)m1 - *(Uint8*)m2;
204 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
205 * \brief Copy \a Num bytes from \a Src to \a Dest
207 void *memcpy(void *Dest, const void *Src, size_t Num)
209 if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
210 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
212 __asm__ __volatile__ (
216 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
221 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
222 * \brief Copy \a Num DWORDs from \a Src to \a Dest
224 void *memcpyd(void *Dest, const void *Src, size_t Num)
226 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
231 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
232 * \brief Divide two 64-bit integers
234 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
240 if(Den == 0) __asm__ __volatile__ ("int $0x0");
242 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
243 return (Uint32)Num / (Uint32)Den;
244 if(Den == 1) return Num;
245 if(Den == 2) return Num >> 1; // Speed Hacks
246 if(Den == 4) return Num >> 2; // Speed Hacks
247 if(Den == 8) return Num >> 3; // Speed Hacks
248 if(Den == 16) return Num >> 4; // Speed Hacks
249 if(Den == 32) return Num >> 5; // Speed Hacks
250 if(Den == 1024) return Num >> 10; // Speed Hacks
251 if(Den == 2048) return Num >> 11; // Speed Hacks
252 if(Den == 4096) return Num >> 12;
253 if(Num < Den) return 0;
254 if(Num < Den*2) return 1;
255 if(Num == Den*2) return 2;
257 // Restoring division, from wikipedia
258 // http://en.wikipedia.org/wiki/Division_(digital)
259 P[0] = Num; P[1] = 0;
263 P[1] = (P[1] << 1) | (P[0] >> 63);
270 if( !(P[1] & (1ULL<<63)) ) {
271 q |= (Uint64)1 << (63-i);
283 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
284 * \brief Get the modulus of two 64-bit integers
286 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
288 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
289 if(Den == 1) return 0; // Speed Hacks
290 if(Den == 2) return Num & 1; // Speed Hacks
291 if(Den == 4) return Num & 3; // Speed Hacks
292 if(Den == 8) return Num & 7; // Speed Hacks
293 if(Den == 16) return Num & 15; // Speed Hacks
294 if(Den == 32) return Num & 31; // Speed Hacks
295 if(Den == 1024) return Num & 1023; // Speed Hacks
296 if(Den == 2048) return Num & 2047; // Speed Hacks
297 if(Den == 4096) return Num & 4095; // Speed Hacks
299 if(Num >> 32 == 0 && Den >> 32 == 0)
300 return (Uint32)Num % (Uint32)Den;
302 return Num - __udivdi3(Num, Den) * Den;
305 Uint16 LittleEndian16(Uint16 Val)
309 Uint16 BigEndian16(Uint16 Val)
311 return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
313 Uint32 LittleEndian32(Uint32 Val)
317 Uint32 BigEndian32(Uint32 Val)
319 return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
323 EXPORT(memcpy); EXPORT(memset);
325 //EXPORT(memcpyw); EXPORT(memsetw);
326 EXPORT(memcpyd); EXPORT(memsetd);
327 EXPORT(inb); EXPORT(inw); EXPORT(ind);
328 EXPORT(outb); EXPORT(outw); EXPORT(outd);
329 EXPORT(__udivdi3); EXPORT(__umoddi3);
331 EXPORT(LittleEndian16); EXPORT(BigEndian16);
332 EXPORT(LittleEndian32); EXPORT(BigEndian32);