2 * AcessOS Microkernel Version
10 extern int GetCPUNum(void);
14 * \brief Determine if a short spinlock is locked
15 * \param Lock Lock pointer
17 int IS_LOCKED(struct sShortSpinlock *Lock)
23 * \brief Check if the current CPU has the lock
24 * \param Lock Lock pointer
26 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
28 #if STACKED_LOCKS == 1
29 return Lock->Lock == GetCPUNum() + 1;
30 #elif STACKED_LOCKS == 2
31 return Lock->Lock == Proc_GetCurThread();
38 * \brief Acquire a Short Spinlock
39 * \param Lock Lock pointer
41 * This type of mutex should only be used for very short sections of code,
42 * or in places where a Mutex_* would be overkill, such as appending
43 * an element to linked list (usually two assignement lines in C)
45 * \note This type of lock halts interrupts, so ensure that no timing
46 * functions are called while it is held. As a matter of fact, spend as
47 * little time as possible with this lock held
48 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
50 void SHORTLOCK(struct sShortSpinlock *Lock)
56 #if STACKED_LOCKS == 1
57 int cpu = GetCPUNum() + 1;
58 #elif STACKED_LOCKS == 2
59 void *thread = Proc_GetCurThread();
63 // Save interrupt state
64 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
65 IF &= 0x200; // AND out all but the interrupt flag
68 #if STACKED_LOCKS == 1
69 if( Lock->Lock == cpu ) {
73 #elif STACKED_LOCKS == 2
74 if( Lock->Lock == thread ) {
80 // Wait for another CPU to release
83 // If r/m32 == EAX, set ZF and set r/m32 = r32
84 // Else, clear ZF and set EAX = r/m32
85 #if STACKED_LOCKS == 1
86 __ASM__("lock cmpxchgl %2, (%3)"
88 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
90 #elif STACKED_LOCKS == 2
91 __ASM__("lock cmpxchgl %2, (%3)"
93 : "a"(0), "r"(thread), "r"(&Lock->Lock)
96 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
100 if( v ) __ASM__("sti"); // Re-enable interrupts
104 #if LOCK_DISABLE_INTS
110 Log_Log("LOCK", "%p locked by %p\n", Lock, __builtin_return_address(0));
114 * \brief Release a short lock
115 * \param Lock Lock pointer
117 void SHORTREL(struct sShortSpinlock *Lock)
120 Log_Log("LOCK", "%p released by %p\n", Lock, __builtin_return_address(0));
130 #if LOCK_DISABLE_INTS
131 // Lock->IF can change anytime once Lock->Lock is zeroed
144 // === IO Commands ===
145 void outb(Uint16 Port, Uint8 Data)
147 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
149 void outw(Uint16 Port, Uint16 Data)
151 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
153 void outd(Uint16 Port, Uint32 Data)
155 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
157 Uint8 inb(Uint16 Port)
160 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
163 Uint16 inw(Uint16 Port)
166 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
169 Uint32 ind(Uint16 Port)
172 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
177 * \fn void *memset(void *Dest, int Val, size_t Num)
178 * \brief Do a byte granuality set of Dest
180 void *memset(void *Dest, int Val, size_t Num)
182 Uint32 val = Val&0xFF;
185 __asm__ __volatile__ (
189 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
193 * \brief Set double words
195 void *memsetd(void *Dest, Uint32 Val, size_t Num)
197 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
202 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
203 * \brief Compare two pieces of memory
205 int memcmp(const void *m1, const void *m2, size_t Num)
207 if( Num == 0 ) return 0; // No bytes are always identical
211 if(*(Uint8*)m1 != *(Uint8*)m2)
212 return *(Uint8*)m1 - *(Uint8*)m2;
220 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
221 * \brief Copy \a Num bytes from \a Src to \a Dest
223 void *memcpy(void *Dest, const void *Src, size_t Num)
225 if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
226 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
228 __asm__ __volatile__ (
232 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
237 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
238 * \brief Copy \a Num DWORDs from \a Src to \a Dest
240 void *memcpyd(void *Dest, const void *Src, size_t Num)
242 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
247 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
248 * \brief Divide two 64-bit integers
250 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
256 if(Den == 0) __asm__ __volatile__ ("int $0x0");
258 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
259 return (Uint32)Num / (Uint32)Den;
260 if(Den == 1) return Num;
261 if(Den == 2) return Num >> 1; // Speed Hacks
262 if(Den == 4) return Num >> 2; // Speed Hacks
263 if(Den == 8) return Num >> 3; // Speed Hacks
264 if(Den == 16) return Num >> 4; // Speed Hacks
265 if(Den == 32) return Num >> 5; // Speed Hacks
266 if(Den == 1024) return Num >> 10; // Speed Hacks
267 if(Den == 2048) return Num >> 11; // Speed Hacks
268 if(Den == 4096) return Num >> 12;
269 if(Num < Den) return 0;
270 if(Num < Den*2) return 1;
271 if(Num == Den*2) return 2;
273 // Restoring division, from wikipedia
274 // http://en.wikipedia.org/wiki/Division_(digital)
275 P[0] = Num; P[1] = 0;
279 P[1] = (P[1] << 1) | (P[0] >> 63);
286 if( !(P[1] & (1ULL<<63)) ) {
287 q |= (Uint64)1 << (63-i);
299 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
300 * \brief Get the modulus of two 64-bit integers
302 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
304 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
305 if(Den == 1) return 0; // Speed Hacks
306 if(Den == 2) return Num & 1; // Speed Hacks
307 if(Den == 4) return Num & 3; // Speed Hacks
308 if(Den == 8) return Num & 7; // Speed Hacks
309 if(Den == 16) return Num & 15; // Speed Hacks
310 if(Den == 32) return Num & 31; // Speed Hacks
311 if(Den == 1024) return Num & 1023; // Speed Hacks
312 if(Den == 2048) return Num & 2047; // Speed Hacks
313 if(Den == 4096) return Num & 4095; // Speed Hacks
315 if(Num >> 32 == 0 && Den >> 32 == 0)
316 return (Uint32)Num % (Uint32)Den;
318 return Num - __udivdi3(Num, Den) * Den;
321 Uint16 LittleEndian16(Uint16 Val)
325 Uint16 BigEndian16(Uint16 Val)
327 return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
329 Uint32 LittleEndian32(Uint32 Val)
333 Uint32 BigEndian32(Uint32 Val)
335 return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
339 EXPORT(memcpy); EXPORT(memset);
341 //EXPORT(memcpyw); EXPORT(memsetw);
342 EXPORT(memcpyd); EXPORT(memsetd);
343 EXPORT(inb); EXPORT(inw); EXPORT(ind);
344 EXPORT(outb); EXPORT(outw); EXPORT(outd);
345 EXPORT(__udivdi3); EXPORT(__umoddi3);
347 EXPORT(LittleEndian16); EXPORT(BigEndian16);
348 EXPORT(LittleEndian32); EXPORT(BigEndian32);