5 * - General arch-specific stuff
8 #include <threads_int.h>
13 #define DEBUG_TO_SERIAL 1
14 #define SERIAL_PORT 0x3F8
15 #define GDB_SERIAL_PORT 0x2F8
19 extern struct sShortSpinlock glDebug_Lock;
20 extern struct sShortSpinlock glThreadListLock;
22 extern int GetCPUNum(void);
25 Uint64 __udivdi3(Uint64 Num, Uint64 Den);
26 Uint64 __umoddi3(Uint64 Num, Uint64 Den);
29 int gbDebug_SerialSetup = 0;
30 int gbGDB_SerialSetup = 0;
34 * \brief Determine if a short spinlock is locked
35 * \param Lock Lock pointer
37 int IS_LOCKED(struct sShortSpinlock *Lock)
43 * \brief Check if the current CPU has the lock
44 * \param Lock Lock pointer
46 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
48 #if STACKED_LOCKS == 1
49 return Lock->Lock == GetCPUNum() + 1;
50 #elif STACKED_LOCKS == 2
51 return Lock->Lock == Proc_GetCurThread();
58 * \brief Acquire a Short Spinlock
59 * \param Lock Lock pointer
61 * This type of mutex should only be used for very short sections of code,
62 * or in places where a Mutex_* would be overkill, such as appending
63 * an element to linked list (usually two assignement lines in C)
65 * \note This type of lock halts interrupts, so ensure that no timing
66 * functions are called while it is held. As a matter of fact, spend as
67 * little time as possible with this lock held
68 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
70 void SHORTLOCK(struct sShortSpinlock *Lock)
76 #if STACKED_LOCKS == 1
77 int cpu = GetCPUNum() + 1;
78 #elif STACKED_LOCKS == 2
79 void *thread = Proc_GetCurThread();
83 // Save interrupt state
84 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
85 IF &= 0x200; // AND out all but the interrupt flag
88 #if STACKED_LOCKS == 1
89 if( Lock->Lock == cpu ) {
93 #elif STACKED_LOCKS == 2
94 if( Lock->Lock == thread ) {
101 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
103 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
104 Debug("%p obtaining %p (Called by %p)", __builtin_return_address(0), Lock, __builtin_return_address(1));
108 // Wait for another CPU to release
111 // If r/m32 == EAX, set ZF and set r/m32 = r32
112 // Else, clear ZF and set EAX = r/m32
113 #if STACKED_LOCKS == 1
114 __ASM__("lock cmpxchgl %2, (%3)"
116 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
118 #elif STACKED_LOCKS == 2
119 __ASM__("lock cmpxchgl %2, (%3)"
121 : "a"(0), "r"(thread), "r"(&Lock->Lock)
124 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
127 #if LOCK_DISABLE_INTS
128 if( v ) __ASM__("sti"); // Re-enable interrupts
132 #if LOCK_DISABLE_INTS
138 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
140 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
141 //Debug("Lock %p locked by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
147 * \brief Release a short lock
148 * \param Lock Lock pointer
150 void SHORTREL(struct sShortSpinlock *Lock)
160 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
162 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
163 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
167 #if LOCK_DISABLE_INTS
168 // Lock->IF can change anytime once Lock->Lock is zeroed
183 int putDebugChar(char ch)
185 if(!gbGDB_SerialSetup) {
186 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
187 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
188 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
189 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
190 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
191 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
192 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
193 gbGDB_SerialSetup = 1;
195 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
196 outb(GDB_SERIAL_PORT, ch);
199 int getDebugChar(void)
201 if(!gbGDB_SerialSetup) {
202 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
203 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
204 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
205 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
206 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
207 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
208 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
209 gbGDB_SerialSetup = 1;
211 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
212 return inb(GDB_SERIAL_PORT);
214 #endif /* USE_GDB_STUB */
216 void Debug_PutCharDebug(char ch)
219 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
223 if(!gbDebug_SerialSetup) {
224 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
225 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
226 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
227 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
228 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
229 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
230 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
231 gbDebug_SerialSetup = 1;
233 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
234 outb(SERIAL_PORT, ch);
238 void Debug_PutStringDebug(const char *String)
241 Debug_PutCharDebug(*String++);
244 // === IO Commands ===
245 void outb(Uint16 Port, Uint8 Data)
247 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
249 void outw(Uint16 Port, Uint16 Data)
251 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
253 void outd(Uint16 Port, Uint32 Data)
255 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
257 Uint8 inb(Uint16 Port)
260 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
263 Uint16 inw(Uint16 Port)
266 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
269 Uint32 ind(Uint16 Port)
272 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
277 * \fn void *memset(void *Dest, int Val, size_t Num)
278 * \brief Do a byte granuality set of Dest
280 void *memset(void *Dest, int Val, size_t Num)
282 Uint32 val = Val&0xFF;
285 __asm__ __volatile__ (
289 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
293 * \brief Set double words
295 void *memsetd(void *Dest, Uint32 Val, size_t Num)
297 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
302 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
303 * \brief Compare two pieces of memory
305 int memcmp(const void *m1, const void *m2, size_t Num)
307 const Uint8 *d1 = m1;
308 const Uint8 *d2 = m2;
309 if( Num == 0 ) return 0; // No bytes are always identical
322 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
323 * \brief Copy \a Num bytes from \a Src to \a Dest
325 void *memcpy(void *Dest, const void *Src, size_t Num)
327 // Debug("\nmemcpy:Num=0x%x by %p", Num, __builtin_return_address(0));
328 if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
329 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
331 __asm__ __volatile__ (
335 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
340 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
341 * \brief Copy \a Num DWORDs from \a Src to \a Dest
343 void *memcpyd(void *Dest, const void *Src, size_t Num)
345 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
349 Uint64 DivMod64U(Uint64 Num, Uint64 Div, Uint64 *Rem)
352 if( Div < 0x100000000ULL && Num < 0xFFFFFFFF * Div ) {
354 __asm__ __volatile__(
356 : "=a" (ret_32), "=d" (rem)
357 : "a" ( (Uint32)(Num & 0xFFFFFFFF) ), "d" ((Uint32)(Num >> 32)), "r" (Div)
363 ret = __udivdi3(Num, Div);
364 if(Rem) *Rem = __umoddi3(Num, Div);
369 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
370 * \brief Divide two 64-bit integers
372 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
378 if(Den == 0) __asm__ __volatile__ ("int $0x0");
380 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
381 return (Uint32)Num / (Uint32)Den;
382 if(Den == 1) return Num;
383 if(Den == 2) return Num >> 1; // Speed Hacks
384 if(Den == 4) return Num >> 2; // Speed Hacks
385 if(Den == 8) return Num >> 3; // Speed Hacks
386 if(Den == 16) return Num >> 4; // Speed Hacks
387 if(Den == 32) return Num >> 5; // Speed Hacks
388 if(Den == 1024) return Num >> 10; // Speed Hacks
389 if(Den == 2048) return Num >> 11; // Speed Hacks
390 if(Den == 4096) return Num >> 12;
391 if(Num < Den) return 0;
392 if(Num < Den*2) return 1;
393 if(Num == Den*2) return 2;
399 __asm__ __volatile__ (
400 "fildq %2\n\t" // Num
401 "fildq %1\n\t" // Den
405 : "m" (P[0]), "m" (P[1])
408 //Log("%llx / %llx = %llx\n", Num, Den, q);
410 // Restoring division, from wikipedia
411 // http://en.wikipedia.org/wiki/Division_(digital)
412 P[0] = Num; P[1] = 0;
416 P[1] = (P[1] << 1) | (P[0] >> 63);
423 if( !(P[1] & (1ULL<<63)) ) {
424 q |= (Uint64)1 << (63-i);
437 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
438 * \brief Get the modulus of two 64-bit integers
440 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
442 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
443 if(Den == 1) return 0; // Speed Hacks
444 if(Den == 2) return Num & 1; // Speed Hacks
445 if(Den == 4) return Num & 3; // Speed Hacks
446 if(Den == 8) return Num & 7; // Speed Hacks
447 if(Den == 16) return Num & 15; // Speed Hacks
448 if(Den == 32) return Num & 31; // Speed Hacks
449 if(Den == 1024) return Num & 1023; // Speed Hacks
450 if(Den == 2048) return Num & 2047; // Speed Hacks
451 if(Den == 4096) return Num & 4095; // Speed Hacks
453 if(Num >> 32 == 0 && Den >> 32 == 0)
454 return (Uint32)Num % (Uint32)Den;
456 return Num - __udivdi3(Num, Den) * Den;
461 EXPORT(memcpy); EXPORT(memset);
463 //EXPORT(memcpyw); EXPORT(memsetw);
464 EXPORT(memcpyd); EXPORT(memsetd);
465 EXPORT(inb); EXPORT(inw); EXPORT(ind);
466 EXPORT(outb); EXPORT(outw); EXPORT(outd);
467 EXPORT(__udivdi3); EXPORT(__umoddi3);