2 * AcessOS Microkernel Version
6 #include <threads_int.h>
11 #define DEBUG_TO_SERIAL 1
12 #define SERIAL_PORT 0x3F8
13 #define GDB_SERIAL_PORT 0x2F8
17 extern struct sShortSpinlock glDebug_Lock;
18 extern struct sShortSpinlock glThreadListLock;
20 extern int GetCPUNum(void);
23 Uint64 __udivdi3(Uint64 Num, Uint64 Den);
24 Uint64 __umoddi3(Uint64 Num, Uint64 Den);
27 int gbDebug_SerialSetup = 0;
28 int gbGDB_SerialSetup = 0;
32 * \brief Determine if a short spinlock is locked
33 * \param Lock Lock pointer
35 int IS_LOCKED(struct sShortSpinlock *Lock)
41 * \brief Check if the current CPU has the lock
42 * \param Lock Lock pointer
44 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
46 #if STACKED_LOCKS == 1
47 return Lock->Lock == GetCPUNum() + 1;
48 #elif STACKED_LOCKS == 2
49 return Lock->Lock == Proc_GetCurThread();
56 * \brief Acquire a Short Spinlock
57 * \param Lock Lock pointer
59 * This type of mutex should only be used for very short sections of code,
60 * or in places where a Mutex_* would be overkill, such as appending
61 * an element to linked list (usually two assignement lines in C)
63 * \note This type of lock halts interrupts, so ensure that no timing
64 * functions are called while it is held. As a matter of fact, spend as
65 * little time as possible with this lock held
66 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
68 void SHORTLOCK(struct sShortSpinlock *Lock)
74 #if STACKED_LOCKS == 1
75 int cpu = GetCPUNum() + 1;
76 #elif STACKED_LOCKS == 2
77 void *thread = Proc_GetCurThread();
81 // Save interrupt state
82 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
83 IF &= 0x200; // AND out all but the interrupt flag
86 #if STACKED_LOCKS == 1
87 if( Lock->Lock == cpu ) {
91 #elif STACKED_LOCKS == 2
92 if( Lock->Lock == thread ) {
99 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
101 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
102 Debug("%p obtaining %p (Called by %p)", __builtin_return_address(0), Lock, __builtin_return_address(1));
106 // Wait for another CPU to release
109 // If r/m32 == EAX, set ZF and set r/m32 = r32
110 // Else, clear ZF and set EAX = r/m32
111 #if STACKED_LOCKS == 1
112 __ASM__("lock cmpxchgl %2, (%3)"
114 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
116 #elif STACKED_LOCKS == 2
117 __ASM__("lock cmpxchgl %2, (%3)"
119 : "a"(0), "r"(thread), "r"(&Lock->Lock)
122 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
125 #if LOCK_DISABLE_INTS
126 if( v ) __ASM__("sti"); // Re-enable interrupts
130 #if LOCK_DISABLE_INTS
136 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
138 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
139 //Debug("Lock %p locked by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
145 * \brief Release a short lock
146 * \param Lock Lock pointer
148 void SHORTREL(struct sShortSpinlock *Lock)
158 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
160 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
161 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
165 #if LOCK_DISABLE_INTS
166 // Lock->IF can change anytime once Lock->Lock is zeroed
181 int putDebugChar(char ch)
183 if(!gbGDB_SerialSetup) {
184 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
185 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
186 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
187 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
188 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
189 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
190 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
191 gbGDB_SerialSetup = 1;
193 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
194 outb(GDB_SERIAL_PORT, ch);
197 int getDebugChar(void)
199 if(!gbGDB_SerialSetup) {
200 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
201 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
202 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
203 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
204 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
205 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
206 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
207 gbGDB_SerialSetup = 1;
209 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
210 return inb(GDB_SERIAL_PORT);
212 #endif /* USE_GDB_STUB */
214 void Debug_PutCharDebug(char ch)
217 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
221 if(!gbDebug_SerialSetup) {
222 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
223 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
224 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
225 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
226 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
227 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
228 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
229 gbDebug_SerialSetup = 1;
231 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
232 outb(SERIAL_PORT, ch);
236 void Debug_PutStringDebug(const char *String)
239 Debug_PutCharDebug(*String++);
242 // === IO Commands ===
243 void outb(Uint16 Port, Uint8 Data)
245 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
247 void outw(Uint16 Port, Uint16 Data)
249 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
251 void outd(Uint16 Port, Uint32 Data)
253 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
255 Uint8 inb(Uint16 Port)
258 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
261 Uint16 inw(Uint16 Port)
264 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
267 Uint32 ind(Uint16 Port)
270 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
275 * \fn void *memset(void *Dest, int Val, size_t Num)
276 * \brief Do a byte granuality set of Dest
278 void *memset(void *Dest, int Val, size_t Num)
280 Uint32 val = Val&0xFF;
283 __asm__ __volatile__ (
287 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
291 * \brief Set double words
293 void *memsetd(void *Dest, Uint32 Val, size_t Num)
295 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
300 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
301 * \brief Compare two pieces of memory
303 int memcmp(const void *m1, const void *m2, size_t Num)
305 const Uint8 *d1 = m1;
306 const Uint8 *d2 = m2;
307 if( Num == 0 ) return 0; // No bytes are always identical
320 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
321 * \brief Copy \a Num bytes from \a Src to \a Dest
323 void *memcpy(void *Dest, const void *Src, size_t Num)
325 if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
326 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
328 __asm__ __volatile__ (
332 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
337 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
338 * \brief Copy \a Num DWORDs from \a Src to \a Dest
340 void *memcpyd(void *Dest, const void *Src, size_t Num)
342 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
347 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
348 * \brief Divide two 64-bit integers
350 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
356 if(Den == 0) __asm__ __volatile__ ("int $0x0");
358 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
359 return (Uint32)Num / (Uint32)Den;
360 if(Den == 1) return Num;
361 if(Den == 2) return Num >> 1; // Speed Hacks
362 if(Den == 4) return Num >> 2; // Speed Hacks
363 if(Den == 8) return Num >> 3; // Speed Hacks
364 if(Den == 16) return Num >> 4; // Speed Hacks
365 if(Den == 32) return Num >> 5; // Speed Hacks
366 if(Den == 1024) return Num >> 10; // Speed Hacks
367 if(Den == 2048) return Num >> 11; // Speed Hacks
368 if(Den == 4096) return Num >> 12;
369 if(Num < Den) return 0;
370 if(Num < Den*2) return 1;
371 if(Num == Den*2) return 2;
377 __asm__ __volatile__ (
378 "fildq %2\n\t" // Num
379 "fildq %1\n\t" // Den
383 : "m" (P[0]), "m" (P[1])
386 //Log("%llx / %llx = %llx\n", Num, Den, q);
388 // Restoring division, from wikipedia
389 // http://en.wikipedia.org/wiki/Division_(digital)
390 P[0] = Num; P[1] = 0;
394 P[1] = (P[1] << 1) | (P[0] >> 63);
401 if( !(P[1] & (1ULL<<63)) ) {
402 q |= (Uint64)1 << (63-i);
415 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
416 * \brief Get the modulus of two 64-bit integers
418 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
420 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
421 if(Den == 1) return 0; // Speed Hacks
422 if(Den == 2) return Num & 1; // Speed Hacks
423 if(Den == 4) return Num & 3; // Speed Hacks
424 if(Den == 8) return Num & 7; // Speed Hacks
425 if(Den == 16) return Num & 15; // Speed Hacks
426 if(Den == 32) return Num & 31; // Speed Hacks
427 if(Den == 1024) return Num & 1023; // Speed Hacks
428 if(Den == 2048) return Num & 2047; // Speed Hacks
429 if(Den == 4096) return Num & 4095; // Speed Hacks
431 if(Num >> 32 == 0 && Den >> 32 == 0)
432 return (Uint32)Num % (Uint32)Den;
434 return Num - __udivdi3(Num, Den) * Den;
439 EXPORT(memcpy); EXPORT(memset);
441 //EXPORT(memcpyw); EXPORT(memsetw);
442 EXPORT(memcpyd); EXPORT(memsetd);
443 EXPORT(inb); EXPORT(inw); EXPORT(ind);
444 EXPORT(outb); EXPORT(outw); EXPORT(outd);
445 EXPORT(__udivdi3); EXPORT(__umoddi3);