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
62 __ASM__ ("pushf;\n\tpop %0" : "=r"(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));
98 if( v ) __ASM__("sti"); // Re-enable interrupts
102 #if LOCK_DISABLE_INTS
108 * \brief Release a short lock
109 * \param Lock Lock pointer
111 void SHORTREL(struct sShortSpinlock *Lock)
120 #if LOCK_DISABLE_INTS
121 // Lock->IF can change anytime once Lock->Lock is zeroed
134 // === IO Commands ===
135 void outb(Uint16 Port, Uint8 Data)
137 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
139 void outw(Uint16 Port, Uint16 Data)
141 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
143 void outd(Uint16 Port, Uint32 Data)
145 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
147 Uint8 inb(Uint16 Port)
150 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
153 Uint16 inw(Uint16 Port)
156 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
159 Uint32 ind(Uint16 Port)
162 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
167 * \fn void *memset(void *Dest, int Val, size_t Num)
168 * \brief Do a byte granuality set of Dest
170 void *memset(void *Dest, int Val, size_t Num)
172 Uint32 val = Val&0xFF;
175 __asm__ __volatile__ (
179 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
183 * \brief Set double words
185 void *memsetd(void *Dest, Uint32 Val, size_t Num)
187 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
192 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
193 * \brief Compare two pieces of memory
195 int memcmp(const void *m1, const void *m2, size_t Num)
197 if( Num == 0 ) return 0; // No bytes are always identical
201 if(*(Uint8*)m1 != *(Uint8*)m2)
202 return *(Uint8*)m1 - *(Uint8*)m2;
210 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
211 * \brief Copy \a Num bytes from \a Src to \a Dest
213 void *memcpy(void *Dest, const void *Src, size_t Num)
215 if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
216 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
218 __asm__ __volatile__ (
222 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
227 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
228 * \brief Copy \a Num DWORDs from \a Src to \a Dest
230 void *memcpyd(void *Dest, const void *Src, size_t Num)
232 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
237 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
238 * \brief Divide two 64-bit integers
240 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
246 if(Den == 0) __asm__ __volatile__ ("int $0x0");
248 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
249 return (Uint32)Num / (Uint32)Den;
250 if(Den == 1) return Num;
251 if(Den == 2) return Num >> 1; // Speed Hacks
252 if(Den == 4) return Num >> 2; // Speed Hacks
253 if(Den == 8) return Num >> 3; // Speed Hacks
254 if(Den == 16) return Num >> 4; // Speed Hacks
255 if(Den == 32) return Num >> 5; // Speed Hacks
256 if(Den == 1024) return Num >> 10; // Speed Hacks
257 if(Den == 2048) return Num >> 11; // Speed Hacks
258 if(Den == 4096) return Num >> 12;
259 if(Num < Den) return 0;
260 if(Num < Den*2) return 1;
261 if(Num == Den*2) return 2;
263 // Restoring division, from wikipedia
264 // http://en.wikipedia.org/wiki/Division_(digital)
265 P[0] = Num; P[1] = 0;
269 P[1] = (P[1] << 1) | (P[0] >> 63);
276 if( !(P[1] & (1ULL<<63)) ) {
277 q |= (Uint64)1 << (63-i);
289 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
290 * \brief Get the modulus of two 64-bit integers
292 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
294 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
295 if(Den == 1) return 0; // Speed Hacks
296 if(Den == 2) return Num & 1; // Speed Hacks
297 if(Den == 4) return Num & 3; // Speed Hacks
298 if(Den == 8) return Num & 7; // Speed Hacks
299 if(Den == 16) return Num & 15; // Speed Hacks
300 if(Den == 32) return Num & 31; // Speed Hacks
301 if(Den == 1024) return Num & 1023; // Speed Hacks
302 if(Den == 2048) return Num & 2047; // Speed Hacks
303 if(Den == 4096) return Num & 4095; // Speed Hacks
305 if(Num >> 32 == 0 && Den >> 32 == 0)
306 return (Uint32)Num % (Uint32)Den;
308 return Num - __udivdi3(Num, Den) * Den;
311 Uint16 LittleEndian16(Uint16 Val)
315 Uint16 BigEndian16(Uint16 Val)
317 return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
319 Uint32 LittleEndian32(Uint32 Val)
323 Uint32 BigEndian32(Uint32 Val)
325 return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
329 EXPORT(memcpy); EXPORT(memset);
331 //EXPORT(memcpyw); EXPORT(memsetw);
332 EXPORT(memcpyd); EXPORT(memsetd);
333 EXPORT(inb); EXPORT(inw); EXPORT(ind);
334 EXPORT(outb); EXPORT(outw); EXPORT(outd);
335 EXPORT(__udivdi3); EXPORT(__umoddi3);
337 EXPORT(LittleEndian16); EXPORT(BigEndian16);
338 EXPORT(LittleEndian32); EXPORT(BigEndian32);